|
2 | 2 |
|
3 | 3 | #include <qmainwindow.h> |
4 | 4 | #include <qmessagebox.h> |
| 5 | +#include <qobject.h> |
| 6 | +#include <qtypes.h> |
5 | 7 |
|
6 | 8 | #include <memory> |
7 | 9 | #include <sstream> |
8 | 10 | #include <string> |
9 | 11 |
|
10 | 12 | #include "../ffi/namespace/include/config.hxx" |
| 13 | +#include "../front_end_utils/include/error_types.hxx" |
| 14 | +#include "../front_end_utils/include/utils.hxx" |
11 | 15 |
|
12 | 16 | #if defined(LAZY_DEBUG) |
13 | 17 | #include <iostream> |
14 | 18 | using std::cout; |
15 | 19 | #endif |
16 | 20 |
|
17 | | -#include "../front_end_utils/include/error_types.hxx" |
18 | 21 | using namespace Lazyboard::ffi; |
19 | 22 | using Lazyboard::front_end_utils::error_to_string; |
20 | 23 | using Lazyboard::front_end_utils::ErrorTypes; |
| 24 | +using Lazyboard::front_end_utils::is_valid_hex_color; |
21 | 25 |
|
22 | 26 | using std::make_unique; |
23 | 27 | using std::string; |
24 | 28 | using std::stringstream; |
25 | 29 | using Self = Lazyboard::front_end::ThemeManager; |
26 | 30 |
|
| 31 | +auto Self::gui_settings(RawAppConfig *raw_app_config) noexcept -> GuiSettings { |
| 32 | + // clang-format off |
| 33 | + auto raw_background_color = raw_app_config->raw_app_gui_settings.background_color; |
| 34 | + auto raw_foreground_color = raw_app_config->raw_app_gui_settings.foreground_color; |
| 35 | + auto raw_background_button_color = raw_app_config->raw_app_gui_settings.background_button_color; |
| 36 | + auto raw_foreground_button_color = raw_app_config->raw_app_gui_settings.foreground_button_color; |
| 37 | + auto raw_background_header_table_color = raw_app_config->raw_app_gui_settings.background_table_header_color; |
| 38 | + auto raw_foreground_header_table_color = raw_app_config->raw_app_gui_settings.foreground_table_header_color; |
| 39 | + |
| 40 | + |
| 41 | + auto settings_gui = GuiSettings { |
| 42 | + .background_color = raw_background_color, |
| 43 | + .foreground_color = raw_foreground_color, |
| 44 | + .background_button_color = raw_background_button_color, |
| 45 | + .foreground_button_color = raw_foreground_color, |
| 46 | + .background_table_header_color = raw_background_header_table_color, |
| 47 | + .foreground_table_header_color = raw_foreground_header_table_color, |
| 48 | + }; // clang-format on |
| 49 | + |
| 50 | + return settings_gui; |
| 51 | +} |
| 52 | + |
| 53 | +void Self::on_invalid_hex_color_error(QMainWindow *main_window) { |
| 54 | + // clang-format off |
| 55 | + const auto error_name = error_to_string(ErrorTypes::INVALID_HEX_COLOR); |
| 56 | + const auto error_message = QString(R"( |
| 57 | +Invalid HEX color, please check your TOML configuration |
| 58 | +Error name: %1 |
| 59 | + )").arg(error_name.data()); // clang-format on |
| 60 | + |
| 61 | + QMessageBox::critical(main_window, "Error", error_message); |
| 62 | +} |
| 63 | + |
27 | 64 | void Self::set_main_window_theme(QMainWindow *main_window, |
28 | | - RawAppConfig *raw_app_config) { |
29 | | - auto bg_color = |
30 | | - string(raw_app_config->raw_app_gui_settings.background_color); |
31 | | - auto fg_color = |
32 | | - string(raw_app_config->raw_app_gui_settings.foreground_color); |
33 | | - auto bg_button_color = |
34 | | - string(raw_app_config->raw_app_gui_settings.background_button_color); |
35 | | - auto fg_button_color = |
36 | | - string(raw_app_config->raw_app_gui_settings.foreground_button_color); |
37 | | - auto bg_header_table_color = string( |
38 | | - raw_app_config->raw_app_gui_settings.background_table_header_color); |
39 | | - auto fg_header_table_color = string( |
40 | | - raw_app_config->raw_app_gui_settings.foreground_table_header_color); |
41 | | - |
42 | | - auto bg_hex = QColor(QString::fromStdString(bg_color)); |
43 | | - auto fg_hex = QColor(QString::fromStdString(fg_color)); |
44 | | - auto bg_btn_hex = QColor(QString::fromStdString(bg_button_color)); |
45 | | - auto fg_btn_hex = QColor(QString::fromStdString(fg_button_color)); |
46 | | - auto bg_header_table_hex = |
47 | | - QColor(QString::fromStdString(bg_header_table_color)); |
48 | | - auto fg_header_table_hex = |
49 | | - QColor(QString::fromStdString(fg_header_table_color)); |
| 65 | + RawAppConfig *raw_app_config) noexcept { |
| 66 | + auto settings_gui = this->gui_settings(raw_app_config); |
50 | 67 |
|
| 68 | + // clang-format off |
| 69 | + auto bg_color = data(settings_gui.background_color); |
| 70 | + auto fg_color = data(settings_gui.foreground_color); |
| 71 | + auto bg_button_color = data(settings_gui.background_button_color); |
| 72 | + auto fg_button_color = data(settings_gui.foreground_button_color); |
| 73 | + auto bg_header_table_color = data(settings_gui.background_table_header_color); |
| 74 | + auto fg_header_table_color = data(settings_gui.foreground_table_header_color); |
| 75 | + // clang-format on |
| 76 | + |
| 77 | + auto bg_hex = QColor(bg_color); |
| 78 | + auto fg_hex = QColor(fg_color); |
| 79 | + auto bg_btn_hex = QColor(bg_button_color); |
| 80 | + auto fg_btn_hex = QColor(fg_button_color); |
| 81 | + auto bg_header_table_hex = QColor(bg_header_table_color); |
| 82 | + auto fg_header_table_hex = QColor(fg_header_table_color); |
51 | 83 | ffi::free_cstr_app_config(raw_app_config); |
52 | 84 |
|
53 | | - if (!bg_hex.isValid() || !fg_hex.isValid() || !bg_btn_hex.isValid() || |
54 | | - !fg_btn_hex.isValid() || !bg_header_table_hex.isValid() || |
55 | | - !fg_header_table_hex.isValid()) { |
56 | | - // clang-format off |
57 | | - const auto error_name = error_to_string(ErrorTypes::INVALID_HEX_COLOR); |
58 | | - const auto error_message = QString(R"( |
59 | | -Invalid HEX color, please check your TOML configuration |
60 | | -Error name: %1 |
61 | | - )").arg(error_name.data()); // clang-format on |
| 85 | + const auto valid_color = |
| 86 | + is_valid_hex_color(bg_hex, fg_hex, bg_btn_hex, fg_btn_hex, |
| 87 | + bg_header_table_hex, fg_header_table_hex); |
62 | 88 |
|
63 | | - QMessageBox::critical(main_window, "Error", error_message); |
| 89 | + if (!valid_color) { |
| 90 | + on_invalid_hex_color_error(main_window); |
64 | 91 |
|
65 | 92 | // clang-format off |
66 | 93 | #if defined (LAZY_DEBUG) |
| 94 | + const auto error_name = error_to_string(ErrorTypes::INVALID_HEX_COLOR); |
67 | 95 | stringstream string_stream_debug; |
| 96 | + |
68 | 97 | string_stream_debug |
69 | 98 | << "[DEBUG] Found error when load TOML configuration:\n" |
70 | 99 | << "[DEBUG] Error Type: " << error_name << "\n" |
@@ -93,7 +122,6 @@ Error name: %1 |
93 | 122 | R"""( |
94 | 123 | QPushButton { |
95 | 124 | background-color: %1; |
96 | | - qproperty-autoFillBackground: true; |
97 | 125 | color: %2; |
98 | 126 | } |
99 | 127 | |
|
0 commit comments