From c5152d04f8be8324eb0518ef04144bd13778496f Mon Sep 17 00:00:00 2001
From: YuSanka <yusanka@gmail.com>
Date: Mon, 28 Jan 2019 12:13:53 +0100
Subject: [PATCH] Set options value from edited preset instead of default
 preset, when call "Add Settings"

+ added code for custom editing of the frequently used settings bundle (it's commented for this moment)
+ some code refactoring
---
 src/slic3r/GUI/GUI_ObjectList.cpp | 145 ++++++++++++++++++++++++------
 src/slic3r/GUI/GUI_ObjectList.hpp |  14 +++
 2 files changed, 132 insertions(+), 27 deletions(-)

diff --git a/src/slic3r/GUI/GUI_ObjectList.cpp b/src/slic3r/GUI/GUI_ObjectList.cpp
index 62c00d02d..24e864498 100644
--- a/src/slic3r/GUI/GUI_ObjectList.cpp
+++ b/src/slic3r/GUI/GUI_ObjectList.cpp
@@ -21,8 +21,6 @@ namespace GUI
 
 wxDEFINE_EVENT(EVT_OBJ_LIST_OBJECT_SELECT, SimpleEvent);
 
-typedef std::map<std::string, std::vector<std::string>> FreqSettingsBundle;
-    
 // pt_FFF
 FreqSettingsBundle FREQ_SETTINGS_BUNDLE_FFF =
 {
@@ -587,9 +585,9 @@ void ObjectList::OnDrop(wxDataViewEvent &event)
 
 // Context Menu
 
-std::vector<std::string> get_options(const bool is_part, const bool is_sla)
+std::vector<std::string> ObjectList::get_options(const bool is_part)
 {
-    if (is_sla) {
+    if (wxGetApp().plater()->printer_technology() == ptSLA) {
         SLAPrintObjectConfig full_sla_config;
         auto options = full_sla_config.keys();
         options.erase(find(options.begin(), options.end(), "layer_height"));
@@ -605,13 +603,8 @@ std::vector<std::string> get_options(const bool is_part, const bool is_sla)
     }
     return options;
 }
-
-std::vector<std::string> get_options(const bool is_part)
-{
-    return get_options(is_part, wxGetApp().plater()->printer_technology() == ptSLA);
-}
     
-const std::vector<std::string>& get_options_for_bundle(const wxString& bundle_name)
+const std::vector<std::string>& ObjectList::get_options_for_bundle(const wxString& bundle_name)
 {
     const FreqSettingsBundle& bundle = wxGetApp().plater()->printer_technology() == ptSLA ? 
                                        FREQ_SETTINGS_BUNDLE_SLA : FREQ_SETTINGS_BUNDLE_FFF;
@@ -621,14 +614,24 @@ const std::vector<std::string>& get_options_for_bundle(const wxString& bundle_na
         if (bundle_name == _(it.first))
             return it.second;
     }
+#if 0
+    // if "Quick menu" is selected
+    FreqSettingsBundle& bundle_quick = wxGetApp().plater()->printer_technology() == ptSLA ?
+                                       m_freq_settings_sla: m_freq_settings_fff;
+
+    for (auto& it : bundle_quick)
+    {
+        if ( bundle_name == wxString::Format(_(L("Quick Add Settings (%s)")), _(it.first)) )
+            return it.second;
+    }
+#endif
+
     return std::vector<std::string> {};
 }
 
-//				  category ->		vector 			 ( option	;  label )
-typedef std::map< std::string, std::vector< std::pair<std::string, std::string> > > settings_menu_hierarchy;
-void get_options_menu(settings_menu_hierarchy& settings_menu, const bool is_part, const bool is_sla)
+void ObjectList::get_options_menu(settings_menu_hierarchy& settings_menu, const bool is_part)
 {
-    auto options = get_options(is_part, is_sla);
+    auto options = get_options(is_part);
 
     auto extruders_cnt = wxGetApp().preset_bundle->printers.get_selected_preset().printer_technology() == ptSLA ? 1 :
         wxGetApp().preset_bundle->printers.get_edited_preset().config.option<ConfigOptionFloats>("nozzle_diameter")->values.size();
@@ -653,11 +656,6 @@ void get_options_menu(settings_menu_hierarchy& settings_menu, const bool is_part
     }
 }
 
-void get_options_menu(settings_menu_hierarchy& settings_menu, const bool is_part)
-{
-    get_options_menu(settings_menu, is_part, wxGetApp().plater()->printer_technology() == ptSLA);
-}
-
 void ObjectList::get_settings_choice(const wxString& category_name)
 {
     wxArrayString names;
@@ -691,10 +689,59 @@ void ObjectList::get_settings_choice(const wxString& category_name)
     if (wxGetSelectedChoices(selections, _(L("Select showing settings")), category_name, names) == -1)
         return;
 
+    const int selection_cnt = selections.size();
+#if 0
+    if (selection_cnt > 0) 
+    {
+        // Add selected items to the "Quick menu"
+        FreqSettingsBundle& freq_settings = wxGetApp().plater()->printer_technology() == ptSLA ?
+                                            m_freq_settings_sla : m_freq_settings_fff;
+        bool changed_existing = false;
+
+        std::vector<std::string> tmp_freq_cat = {};
+        
+        for (auto& cat : freq_settings)
+        {
+            if (_(cat.first) == category_name)
+            {
+                std::vector<std::string>& freq_settings_category = cat.second;
+                freq_settings_category.clear();
+                freq_settings_category.reserve(selection_cnt);
+                for (auto sel : selections)
+                    freq_settings_category.push_back((*settings_list)[sel].first);
+
+                changed_existing = true;
+                break;
+            }
+        }
+
+        if (!changed_existing)
+        {
+            // Create new "Quick menu" item
+            for (auto& cat : settings_menu)
+            {
+                if (_(cat.first) == category_name)
+                {
+                    freq_settings[cat.first] = std::vector<std::string> {};
+
+                    std::vector<std::string>& freq_settings_category = freq_settings.find(cat.first)->second;
+                    freq_settings_category.reserve(selection_cnt);
+                    for (auto sel : selections)
+                        freq_settings_category.push_back((*settings_list)[sel].first);
+                    break;
+                }
+            }
+        }
+    }
+#endif
+
     std::vector <std::string> selected_options;
+    selected_options.reserve(selection_cnt);
     for (auto sel : selections)
         selected_options.push_back((*settings_list)[sel].first);
 
+    const DynamicPrintConfig& from_config = wxGetApp().preset_bundle->prints.get_edited_preset().config;
+
     for (auto& setting : (*settings_list))
     {
         auto& opt_key = setting.first;
@@ -703,8 +750,17 @@ void ObjectList::get_settings_choice(const wxString& category_name)
             m_config->erase(opt_key);
 
         if (find(opt_keys.begin(), opt_keys.end(), opt_key) == opt_keys.end() &&
-            find(selected_options.begin(), selected_options.end(), opt_key) != selected_options.end())
-            m_config->set_key_value(opt_key, m_default_config->option(opt_key)->clone());
+            find(selected_options.begin(), selected_options.end(), opt_key) != selected_options.end()) {
+            const ConfigOption* option = from_config.option(opt_key);
+            if (!option) {
+                // if current option doesn't exist in prints.get_edited_preset(),
+                // get it from m_default_config
+                if (m_default_config) delete m_default_config;
+                m_default_config = DynamicPrintConfig::new_from_defaults_keys(get_options(false));
+                option = m_default_config->option(opt_key);
+            }
+            m_config->set_key_value(opt_key, option->clone());
+        }
     }
 
 
@@ -718,10 +774,20 @@ void ObjectList::get_freq_settings_choice(const wxString& bundle_name)
 
     auto opt_keys = m_config->keys();
 
+    const DynamicPrintConfig& from_config = wxGetApp().preset_bundle->prints.get_edited_preset().config;
     for (auto& opt_key : options)
     {
-        if ( find(opt_keys.begin(), opt_keys.end(), opt_key) == opt_keys.end() )
-            m_config->set_key_value(opt_key, m_default_config->option(opt_key)->clone());
+        if (find(opt_keys.begin(), opt_keys.end(), opt_key) == opt_keys.end()) {
+            const ConfigOption* option = from_config.option(opt_key);
+            if (!option) {
+                // if current option doesn't exist in prints.get_edited_preset(),
+                // get it from m_default_config
+                if (m_default_config) delete m_default_config;
+                m_default_config = DynamicPrintConfig::new_from_defaults_keys(get_options(false));
+                option = m_default_config->option(opt_key);
+            }
+            m_config->set_key_value(opt_key, option->clone());
+        }
     }
 
     // Add settings item for object
@@ -835,7 +901,20 @@ wxMenuItem* ObjectList::append_menu_item_settings(wxMenu* menu_)
         if (settings_id != wxNOT_FOUND)
             menu->Destroy(settings_id);
     }
-
+#if 0
+    for (auto& it : m_freq_settings_fff)
+    {
+        settings_id = menu->FindItem(wxString::Format(_(L("Quick Add Settings (%s)")), _(it.first)));
+        if (settings_id != wxNOT_FOUND)
+            menu->Destroy(settings_id);
+    }
+    for (auto& it : m_freq_settings_sla)
+    {
+        settings_id = menu->FindItem(wxString::Format(_(L("Quick Add Settings (%s)")), _(it.first)));
+        if (settings_id != wxNOT_FOUND)
+            menu->Destroy(settings_id);
+    }
+#endif
     menu->DestroySeparators(); // delete old separators
 
     const auto sel_vol = get_selected_model_volume();
@@ -937,6 +1016,7 @@ wxMenu* ObjectList::create_settings_popupmenu(wxMenu *parent_menu)
 
 void ObjectList::create_freq_settings_popupmenu(wxMenu *menu)
 {
+    // Add default settings bundles
     const FreqSettingsBundle& bundle = wxGetApp().plater()->printer_technology() == ptFFF ?
                                      FREQ_SETTINGS_BUNDLE_FFF : FREQ_SETTINGS_BUNDLE_SLA;
 
@@ -951,6 +1031,20 @@ void ObjectList::create_freq_settings_popupmenu(wxMenu *menu)
                         [menu, this](wxCommandEvent& event) { get_freq_settings_choice(menu->GetLabel(event.GetId())); }, 
                         CATEGORY_ICON.find(it.first) == CATEGORY_ICON.end() ? wxNullBitmap : CATEGORY_ICON.at(it.first), menu); 
     }
+#if 0
+    // Add "Quick" settings bundles
+    const FreqSettingsBundle& bundle_quick = wxGetApp().plater()->printer_technology() == ptFFF ?
+                                             m_freq_settings_fff : m_freq_settings_sla;
+
+    for (auto& it : bundle_quick) {
+        if (it.first.empty() || it.first == "Extruders" && extruders_cnt == 1) 
+            continue;
+
+        append_menu_item(menu, wxID_ANY, wxString::Format(_(L("Quick Add Settings (%s)")), _(it.first)), "",
+                        [menu, this](wxCommandEvent& event) { get_freq_settings_choice(menu->GetLabel(event.GetId())); }, 
+                        CATEGORY_ICON.find(it.first) == CATEGORY_ICON.end() ? wxNullBitmap : CATEGORY_ICON.at(it.first), menu); 
+    }
+#endif
 }
 
 void ObjectList::update_opt_keys(t_config_option_keys& opt_keys)
@@ -1381,9 +1475,6 @@ void ObjectList::part_selection_changed()
                     m_config = &(*m_objects)[obj_idx_]->config;
                 }
             }
-
-            if (m_default_config) delete m_default_config;
-            m_default_config = DynamicPrintConfig::new_from_defaults_keys(get_options(is_part));
         }
     }
 
diff --git a/src/slic3r/GUI/GUI_ObjectList.hpp b/src/slic3r/GUI/GUI_ObjectList.hpp
index 060659049..93a3ba60f 100644
--- a/src/slic3r/GUI/GUI_ObjectList.hpp
+++ b/src/slic3r/GUI/GUI_ObjectList.hpp
@@ -25,6 +25,11 @@ class ModelVolume;
 // FIXME: broken build on mac os because of this is missing:
 typedef std::vector<std::string>    t_config_option_keys;
 
+typedef std::map<std::string, std::vector<std::string>> FreqSettingsBundle;
+
+//				  category ->		vector 			 ( option	;  label )
+typedef std::map< std::string, std::vector< std::pair<std::string, std::string> > > settings_menu_hierarchy;
+
 namespace GUI {
 
 wxDECLARE_EVENT(EVT_OBJ_LIST_OBJECT_SELECT, SimpleEvent);
@@ -129,6 +134,11 @@ class ObjectList : public wxDataViewCtrl
 
     int         m_selected_row = 0;
 
+#if 0
+    FreqSettingsBundle m_freq_settings_fff;
+    FreqSettingsBundle m_freq_settings_sla;
+#endif
+
 public:
     ObjectList(wxWindow* parent);
     ~ObjectList();
@@ -264,6 +274,10 @@ private:
 
     void ItemValueChanged(wxDataViewEvent &event);
     void OnEditingDone(wxDataViewEvent &event);
+
+    std::vector<std::string>        get_options(const bool is_part);
+    const std::vector<std::string>& get_options_for_bundle(const wxString& bundle_name);
+    void                            get_options_menu(settings_menu_hierarchy& settings_menu, const bool is_part);
 };