From 6effa3032231088a0da5d882666c4ac7451b9223 Mon Sep 17 00:00:00 2001
From: YuSanka <yusanka@gmail.com>
Date: Wed, 30 Dec 2020 11:55:26 +0100
Subject: [PATCH] Move text color selection from ButtonsDescription to
 Preferences dialog (related to #5489)

---
 src/slic3r/GUI/GUI_App.cpp     | 17 ++++++++++++--
 src/slic3r/GUI/GUI_App.hpp     |  1 +
 src/slic3r/GUI/Preferences.cpp | 41 ++++++++++++++++++++++++++++++++++
 src/slic3r/GUI/Preferences.hpp |  4 ++++
 src/slic3r/GUI/Tab.cpp         | 28 +++++++++++------------
 src/slic3r/GUI/Tab.hpp         |  2 +-
 6 files changed, 75 insertions(+), 18 deletions(-)

diff --git a/src/slic3r/GUI/GUI_App.cpp b/src/slic3r/GUI/GUI_App.cpp
index 3e82d6a3c..5fce4deb5 100644
--- a/src/slic3r/GUI/GUI_App.cpp
+++ b/src/slic3r/GUI/GUI_App.cpp
@@ -1024,6 +1024,12 @@ void GUI_App::update_label_colours_from_appconfig()
     }
 }
 
+void GUI_App::update_label_colours()
+{
+    for (Tab* tab : tabs_list)
+        tab->update_label_colours();
+}
+
 void GUI_App::init_fonts()
 {
     m_small_font = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT);
@@ -1057,7 +1063,10 @@ void GUI_App::update_fonts(const MainFrame *main_frame)
     m_code_font.SetPointSize(m_normal_font.GetPointSize());
 }
 
-void GUI_App::set_label_clr_modified(const wxColour& clr) {
+void GUI_App::set_label_clr_modified(const wxColour& clr) 
+{
+    if (m_color_label_modified == clr)
+        return;
     m_color_label_modified = clr;
     auto clr_str = wxString::Format(wxT("#%02X%02X%02X"), clr.Red(), clr.Green(), clr.Blue());
     std::string str = clr_str.ToStdString();
@@ -1065,7 +1074,10 @@ void GUI_App::set_label_clr_modified(const wxColour& clr) {
     app_config->save();
 }
 
-void GUI_App::set_label_clr_sys(const wxColour& clr) {
+void GUI_App::set_label_clr_sys(const wxColour& clr)
+{
+    if (m_color_label_sys == clr)
+        return;
     m_color_label_sys = clr;
     auto clr_str = wxString::Format(wxT("#%02X%02X%02X"), clr.Red(), clr.Green(), clr.Blue());
     std::string str = clr_str.ToStdString();
@@ -1220,6 +1232,7 @@ void fatal_error(wxWindow* parent)
 // Update the UI based on the current preferences.
 void GUI_App::update_ui_from_settings(bool apply_free_camera_correction)
 {
+    update_label_colours();
     mainframe->update_ui_from_settings(apply_free_camera_correction);
 }
 
diff --git a/src/slic3r/GUI/GUI_App.hpp b/src/slic3r/GUI/GUI_App.hpp
index 8aceb7a69..132776dcb 100644
--- a/src/slic3r/GUI/GUI_App.hpp
+++ b/src/slic3r/GUI/GUI_App.hpp
@@ -167,6 +167,7 @@ public:
     static bool     dark_mode();
     void            init_label_colours();
     void            update_label_colours_from_appconfig();
+    void            update_label_colours();
     void            init_fonts();
 	void            update_fonts(const MainFrame *main_frame = nullptr);
     void            set_label_clr_modified(const wxColour& clr);
diff --git a/src/slic3r/GUI/Preferences.cpp b/src/slic3r/GUI/Preferences.cpp
index 9c9ea8c4b..0a8a6c886 100644
--- a/src/slic3r/GUI/Preferences.cpp
+++ b/src/slic3r/GUI/Preferences.cpp
@@ -312,6 +312,7 @@ void PreferencesDialog::build()
 		m_icon_size_sizer->ShowItems(app_config->get("use_custom_toolbar_size") == "1");
 
 		create_settings_mode_widget();
+		create_settings_text_color_widget();
 	}
 
 #if ENABLE_ENVIRONMENT_MAP
@@ -379,6 +380,10 @@ void PreferencesDialog::accept()
 		app_config->set(it->first, it->second);
 
 	app_config->save();
+
+	wxGetApp().set_label_clr_sys(m_sys_colour->GetColour());
+	wxGetApp().set_label_clr_modified(m_mod_colour->GetColour());
+
 	EndModal(wxID_OK);
 
 	if (m_settings_layout_changed)
@@ -498,6 +503,42 @@ void PreferencesDialog::create_settings_mode_widget()
 	m_optgroup_gui->sizer->Add(sizer, 0, wxEXPAND);
 }
 
+void PreferencesDialog::create_settings_text_color_widget()
+{
+	wxWindow* parent = m_optgroup_gui->parent();
+
+	wxStaticBox* stb = new wxStaticBox(parent, wxID_ANY, _L("Text color Settings"));
+	if (!wxOSX) stb->SetBackgroundStyle(wxBG_STYLE_PAINT);
+
+	wxSizer* sizer = new wxStaticBoxSizer(stb, wxVERTICAL);
+	wxFlexGridSizer* grid_sizer = new wxFlexGridSizer(2, 10, 20);
+	sizer->Add(grid_sizer, 0, wxEXPAND);
+
+	auto sys_label = new wxStaticText(parent, wxID_ANY, _L("Value is the same as the system value"));
+	sys_label->SetForegroundColour(wxGetApp().get_label_clr_sys());
+	m_sys_colour = new wxColourPickerCtrl(parent, wxID_ANY, wxGetApp().get_label_clr_sys());
+	m_sys_colour->Bind(wxEVT_COLOURPICKER_CHANGED, [this, sys_label](wxCommandEvent&) {
+		sys_label->SetForegroundColour(m_sys_colour->GetColour());
+		sys_label->Refresh();
+	});
+	
+	grid_sizer->Add(m_sys_colour, -1, wxALIGN_CENTRE_VERTICAL);
+	grid_sizer->Add(sys_label, -1, wxALIGN_CENTRE_VERTICAL | wxEXPAND);
+
+	auto mod_label = new wxStaticText(parent, wxID_ANY, _L("Value was changed and is not equal to the system value or the last saved preset"));
+	mod_label->SetForegroundColour(wxGetApp().get_label_clr_modified());
+	m_mod_colour = new wxColourPickerCtrl(parent, wxID_ANY, wxGetApp().get_label_clr_modified());
+	m_mod_colour->Bind(wxEVT_COLOURPICKER_CHANGED, [this, mod_label](wxCommandEvent&) {
+		mod_label->SetForegroundColour(m_mod_colour->GetColour());
+		mod_label->Refresh();
+	});
+
+	grid_sizer->Add(m_mod_colour, -1, wxALIGN_CENTRE_VERTICAL);
+	grid_sizer->Add(mod_label, -1, wxALIGN_CENTRE_VERTICAL | wxEXPAND);
+
+	m_optgroup_gui->sizer->Add(sizer, 0, wxEXPAND | wxTOP, em_unit());
+}
+
 
 } // GUI
 } // Slic3r
diff --git a/src/slic3r/GUI/Preferences.hpp b/src/slic3r/GUI/Preferences.hpp
index 68574fbcc..0da044736 100644
--- a/src/slic3r/GUI/Preferences.hpp
+++ b/src/slic3r/GUI/Preferences.hpp
@@ -8,6 +8,7 @@
 #include <map>
 
 class wxRadioBox;
+class wxColourPickerCtrl;
 
 namespace Slic3r {
 namespace GUI {
@@ -25,6 +26,8 @@ class PreferencesDialog : public DPIDialog
 #endif // ENABLE_ENVIRONMENT_MAP
 	wxSizer*                            m_icon_size_sizer;
 	wxRadioBox*							m_layout_mode_box;
+	wxColourPickerCtrl*					m_sys_colour {nullptr};
+	wxColourPickerCtrl*					m_mod_colour {nullptr};
     bool                                isOSX {false};
 	bool								m_settings_layout_changed {false};
 	bool								m_seq_top_layer_only_changed{ false };
@@ -43,6 +46,7 @@ protected:
     void layout();
     void create_icon_size_slider();
     void create_settings_mode_widget();
+    void create_settings_text_color_widget();
 };
 
 } // GUI
diff --git a/src/slic3r/GUI/Tab.cpp b/src/slic3r/GUI/Tab.cpp
index 0656c7e0d..963f3c13b 100644
--- a/src/slic3r/GUI/Tab.cpp
+++ b/src/slic3r/GUI/Tab.cpp
@@ -235,18 +235,11 @@ void Tab::create_preset_tab()
 
     m_undo_btn->Bind(wxEVT_BUTTON, ([this](wxCommandEvent) { on_roll_back_value(); }));
     m_undo_to_sys_btn->Bind(wxEVT_BUTTON, ([this](wxCommandEvent) { on_roll_back_value(true); }));
-    m_question_btn->Bind(wxEVT_BUTTON, ([this](wxCommandEvent)
-    {
+    m_question_btn->Bind(wxEVT_BUTTON, [this](wxCommandEvent) {
         ButtonsDescription dlg(this, m_icon_descriptions);
-        if (dlg.ShowModal() == wxID_OK) {
-            // Colors for ui "decoration"
-            for (Tab *tab : wxGetApp().tabs_list) {
-                tab->m_sys_label_clr = wxGetApp().get_label_clr_sys();
-                tab->m_modified_label_clr = wxGetApp().get_label_clr_modified();
-                tab->update_labels_colour();
-            }
-        }
-    }));
+        if (dlg.ShowModal() == wxID_OK)
+            wxGetApp().update_label_colours();
+    });
     m_search_btn->Bind(wxEVT_BUTTON, [this](wxCommandEvent) { wxGetApp().plater()->search(false); });
 
     // Colors for ui "decoration"
@@ -482,8 +475,13 @@ void Tab::OnActivate()
     Refresh();
 }
 
-void Tab::update_labels_colour()
+void Tab::update_label_colours()
 {
+    if (m_sys_label_clr == wxGetApp().get_label_clr_sys() && m_modified_label_clr == wxGetApp().get_label_clr_modified())
+        return;
+    m_sys_label_clr = wxGetApp().get_label_clr_sys();
+    m_modified_label_clr = wxGetApp().get_label_clr_modified();
+
     //update options "decoration"
     for (const auto opt : m_options_list)
     {
@@ -529,6 +527,8 @@ void Tab::update_labels_colour()
         }
         cur_item = m_treectrl->GetNextVisible(cur_item);
     }
+
+    decorate();
 }
 
 void Tab::decorate()
@@ -992,9 +992,7 @@ void Tab::sys_color_changed()
     m_treectrl->AssignImageList(m_icons);
 
     // Colors for ui "decoration"
-    m_sys_label_clr = wxGetApp().get_label_clr_sys();
-    m_modified_label_clr = wxGetApp().get_label_clr_modified();
-    update_labels_colour();
+    update_label_colours();
 
     // update options_groups
     if (m_active_page)
diff --git a/src/slic3r/GUI/Tab.hpp b/src/slic3r/GUI/Tab.hpp
index 04aa9a0ef..bf0b92163 100644
--- a/src/slic3r/GUI/Tab.hpp
+++ b/src/slic3r/GUI/Tab.hpp
@@ -295,7 +295,7 @@ public:
 	void		toggle_show_hide_incompatible();
 	void		update_show_hide_incompatible_button();
 	void		update_ui_from_settings();
-	void		update_labels_colour();
+	void		update_label_colours();
 	void		decorate();
 	void		update_changed_ui();
 	void		get_sys_and_mod_flags(const std::string& opt_key, bool& sys_page, bool& modified_page);