Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
cmake_minimum_required(VERSION 3.19)
project(Lazyboard)

set(CMAKE_CXX_STANDARD_REQUIRED 20)
set(CMAKE_C_STANDARD_REQUIRED 99)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

set(CMAKE_AUTOMOC ON)
find_package(Qt6 REQUIRED COMPONENTS Widgets Gui Core)
Expand Down
21 changes: 20 additions & 1 deletion src/front_end/include/theme_manager.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,35 @@
#include <qwidget.h>

#include <memory>
#include <string_view>

#include "../../ffi/raw/config.hxx"

using std::string_view;
using std::unique_ptr;

namespace Lazyboard::front_end {
class ThemeManager {
private:
const char *data(string_view sv) noexcept { return sv.data(); }

private:
struct GuiSettings {
string_view background_color;
string_view foreground_color;
string_view background_button_color;
string_view foreground_button_color;
string_view background_table_header_color;
string_view foreground_table_header_color;
};

private:
void on_invalid_hex_color_error(QMainWindow *main_window);
auto gui_settings(RawAppConfig *raw_app_config) noexcept -> GuiSettings;

public:
void set_main_window_theme(QMainWindow *main_window,
RawAppConfig *raw_app_config);
RawAppConfig *raw_app_config) noexcept;
};
} // namespace Lazyboard::front_end

Expand Down
96 changes: 62 additions & 34 deletions src/front_end/theme_manager.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -2,69 +2,98 @@

#include <qmainwindow.h>
#include <qmessagebox.h>
#include <qobject.h>
#include <qtypes.h>

#include <memory>
#include <sstream>
#include <string>

#include "../ffi/namespace/include/config.hxx"
#include "../front_end_utils/include/error_types.hxx"
#include "../front_end_utils/include/utils.hxx"

#if defined(LAZY_DEBUG)
#include <iostream>
using std::cout;
#endif

#include "../front_end_utils/include/error_types.hxx"
using namespace Lazyboard::ffi;
using Lazyboard::front_end_utils::error_to_string;
using Lazyboard::front_end_utils::ErrorTypes;
using Lazyboard::front_end_utils::is_valid_hex_color;

using std::make_unique;
using std::string;
using std::stringstream;
using Self = Lazyboard::front_end::ThemeManager;

auto Self::gui_settings(RawAppConfig *raw_app_config) noexcept -> GuiSettings {
// clang-format off
auto raw_background_color = raw_app_config->raw_app_gui_settings.background_color;
auto raw_foreground_color = raw_app_config->raw_app_gui_settings.foreground_color;
auto raw_background_button_color = raw_app_config->raw_app_gui_settings.background_button_color;
auto raw_foreground_button_color = raw_app_config->raw_app_gui_settings.foreground_button_color;
auto raw_background_header_table_color = raw_app_config->raw_app_gui_settings.background_table_header_color;
auto raw_foreground_header_table_color = raw_app_config->raw_app_gui_settings.foreground_table_header_color;


auto settings_gui = GuiSettings {
.background_color = raw_background_color,
.foreground_color = raw_foreground_color,
.background_button_color = raw_background_button_color,
.foreground_button_color = raw_foreground_color,
.background_table_header_color = raw_background_header_table_color,
.foreground_table_header_color = raw_foreground_header_table_color,
}; // clang-format on

return settings_gui;
}

void Self::on_invalid_hex_color_error(QMainWindow *main_window) {
// clang-format off
const auto error_name = error_to_string(ErrorTypes::INVALID_HEX_COLOR);
const auto error_message = QString(R"(
Invalid HEX color, please check your TOML configuration
Error name: %1
)").arg(error_name.data()); // clang-format on

QMessageBox::critical(main_window, "Error", error_message);
}

void Self::set_main_window_theme(QMainWindow *main_window,
RawAppConfig *raw_app_config) {
auto bg_color =
string(raw_app_config->raw_app_gui_settings.background_color);
auto fg_color =
string(raw_app_config->raw_app_gui_settings.foreground_color);
auto bg_button_color =
string(raw_app_config->raw_app_gui_settings.background_button_color);
auto fg_button_color =
string(raw_app_config->raw_app_gui_settings.foreground_button_color);
auto bg_header_table_color = string(
raw_app_config->raw_app_gui_settings.background_table_header_color);
auto fg_header_table_color = string(
raw_app_config->raw_app_gui_settings.foreground_table_header_color);

auto bg_hex = QColor(QString::fromStdString(bg_color));
auto fg_hex = QColor(QString::fromStdString(fg_color));
auto bg_btn_hex = QColor(QString::fromStdString(bg_button_color));
auto fg_btn_hex = QColor(QString::fromStdString(fg_button_color));
auto bg_header_table_hex =
QColor(QString::fromStdString(bg_header_table_color));
auto fg_header_table_hex =
QColor(QString::fromStdString(fg_header_table_color));
RawAppConfig *raw_app_config) noexcept {
auto settings_gui = this->gui_settings(raw_app_config);

// clang-format off
auto bg_color = data(settings_gui.background_color);
auto fg_color = data(settings_gui.foreground_color);
auto bg_button_color = data(settings_gui.background_button_color);
auto fg_button_color = data(settings_gui.foreground_button_color);
auto bg_header_table_color = data(settings_gui.background_table_header_color);
auto fg_header_table_color = data(settings_gui.foreground_table_header_color);
// clang-format on

auto bg_hex = QColor(bg_color);
auto fg_hex = QColor(fg_color);
auto bg_btn_hex = QColor(bg_button_color);
auto fg_btn_hex = QColor(fg_button_color);
auto bg_header_table_hex = QColor(bg_header_table_color);
auto fg_header_table_hex = QColor(fg_header_table_color);
ffi::free_cstr_app_config(raw_app_config);

if (!bg_hex.isValid() || !fg_hex.isValid() || !bg_btn_hex.isValid() ||
!fg_btn_hex.isValid() || !bg_header_table_hex.isValid() ||
!fg_header_table_hex.isValid()) {
// clang-format off
const auto error_name = error_to_string(ErrorTypes::INVALID_HEX_COLOR);
const auto error_message = QString(R"(
Invalid HEX color, please check your TOML configuration
Error name: %1
)").arg(error_name.data()); // clang-format on
const auto valid_color =
is_valid_hex_color(bg_hex, fg_hex, bg_btn_hex, fg_btn_hex,
bg_header_table_hex, fg_header_table_hex);

QMessageBox::critical(main_window, "Error", error_message);
if (!valid_color) {
on_invalid_hex_color_error(main_window);

// clang-format off
#if defined (LAZY_DEBUG)
const auto error_name = error_to_string(ErrorTypes::INVALID_HEX_COLOR);
stringstream string_stream_debug;

string_stream_debug
<< "[DEBUG] Found error when load TOML configuration:\n"
<< "[DEBUG] Error Type: " << error_name << "\n"
Expand Down Expand Up @@ -93,7 +122,6 @@ Error name: %1
R"""(
QPushButton {
background-color: %1;
qproperty-autoFillBackground: true;
color: %2;
}

Expand Down
32 changes: 32 additions & 0 deletions src/front_end_utils/include/utils.hxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#ifndef FRONT_END_UTILS_HXX
#define FRONT_END_UTILS_HXX

#include <qcolor.h>

#include <type_traits>

using std::is_same_v;
using std::stringstream;
namespace Lazyboard::front_end_utils {

template <typename T>
concept is_qcolor = requires(const T& t) { QColor(t); };

template <is_qcolor... Args>
inline constexpr bool is_valid_hex_color(const Args&... args) noexcept {
return (
[&]() {
QColor color(args);

if (!color.isValid()) {
return false;
}

return true;
}(),
...);
}

} // namespace Lazyboard::front_end_utils

#endif