From 6d28d68e4a87e3e53baf4d5c84d19a8bf4e56a61 Mon Sep 17 00:00:00 2001 From: YuSanka Date: Tue, 21 Jul 2020 16:21:18 +0200 Subject: [PATCH] PhysicalPrinter : Implemented synchronizations from user printer profiles with "Print Host upload" information to the new physical printers --- src/libslic3r/Preset.cpp | 102 ++++++++++++++++++++++------ src/libslic3r/Preset.hpp | 11 ++- src/libslic3r/PresetBundle.cpp | 4 +- src/slic3r/GUI/GUI_App.cpp | 44 ++++++++++++ src/slic3r/GUI/GUI_App.hpp | 1 + src/slic3r/GUI/PresetComboBoxes.cpp | 5 +- 6 files changed, 138 insertions(+), 29 deletions(-) diff --git a/src/libslic3r/Preset.cpp b/src/libslic3r/Preset.cpp index 401de0fc3..ae74bffd7 100644 --- a/src/libslic3r/Preset.cpp +++ b/src/libslic3r/Preset.cpp @@ -1370,6 +1370,37 @@ const std::vector& PhysicalPrinter::printer_options() return s_opts; } +const std::vector& PhysicalPrinter::print_host_options() +{ + static std::vector s_opts; + if (s_opts.empty()) { + s_opts = { + "print_host", + "printhost_apikey", + "printhost_cafile" + }; + } + return s_opts; +} + +bool PhysicalPrinter::has_print_host_information(const PrinterPresetCollection& printer_presets) +{ + for (const Preset& preset : printer_presets) + if (has_print_host_information(preset.config)) + return true; + + return false; +} + +bool PhysicalPrinter::has_print_host_information(const DynamicPrintConfig& config) +{ + for (const std::string& opt : print_host_options()) + if (!config.opt_string(opt).empty()) + return true; + + return false; +} + const std::set& PhysicalPrinter::get_preset_names() const { return preset_names; @@ -1532,42 +1563,69 @@ void PhysicalPrinterCollection::load_printers(const std::string& dir_path, const // if there is saved user presets, contains information about "Print Host upload", // Create default printers with this presets -// Throws an exception on error. -void PhysicalPrinterCollection::load_printers(const PrinterPresetCollection& printer_presets, std::string def_printer_name/* = ""*/) +// Note! "Print Host upload" options will be cleared after physical printer creations +void PhysicalPrinterCollection::load_printers_from_presets(PrinterPresetCollection& printer_presets, std::string def_printer_name) { - if (def_printer_name.empty()) - def_printer_name = "Printer"; - int cnt=0; - std::string errors_cummulative; - // Store the loaded printers into a new vector - std::deque printers_loaded; - for (const Preset& preset: printer_presets) { - const DynamicPrintConfig& config = preset.config; - if (!config.opt_string("print_host").empty() || - !config.opt_string("printhost_apikey").empty() || - !config.opt_string("printhost_cafile").empty() ) { - PhysicalPrinter printer((boost::format("%1% %2%") % def_printer_name % ++cnt ).str(), preset); - printer.loaded = true; - printers_loaded.emplace_back(printer); + for (Preset& preset: printer_presets) { + DynamicPrintConfig& config = preset.config; + const std::vector& options = PhysicalPrinter::print_host_options(); - save_printer(printer); + for(const std::string& option : options) { + if (!config.opt_string(option).empty()) { + // check if printer with those "Print Host upload" options already exist + PhysicalPrinter* existed_printer = find_printer_with_same_config(config); + if (existed_printer) + // just add preset for this printer + existed_printer->add_preset(preset.name); + else { + // create new printer from this preset + PhysicalPrinter printer((boost::format("%1% %2%") % def_printer_name % ++cnt ).str(), preset); + printer.loaded = true; + save_printer(printer); + } + + // erase "Print Host upload" information from the preset + for (const std::string& opt : options) + config.opt_string(opt).clear(); + // save changes for preset + preset.save(); + + // update those changes for edited preset if it's equal to the preset + Preset& edited = printer_presets.get_edited_preset(); + if (preset.name == edited.name) { + for (const std::string& opt : options) + edited.config.opt_string(opt).clear(); + } + + break; + } } } - - if (!errors_cummulative.empty()) - throw std::runtime_error(errors_cummulative); } PhysicalPrinter* PhysicalPrinterCollection::find_printer( const std::string& name, bool first_visible_if_not_found) { - PhysicalPrinter key(name); auto it = this->find_printer_internal(name); // Ensure that a temporary copy is returned if the preset found is currently selected. - return (it != m_printers.end() && it->name == key.name) ? &this->printer(it - m_printers.begin()) : + return (it != m_printers.end() && it->name == name) ? &this->printer(it - m_printers.begin()) : first_visible_if_not_found ? &this->printer(0) : nullptr; } +PhysicalPrinter* PhysicalPrinterCollection::find_printer_with_same_config(const DynamicPrintConfig& config) +{ + for (const PhysicalPrinter& printer :*this) { + bool is_equal = true; + for (const std::string& opt : PhysicalPrinter::print_host_options()) + if (is_equal && printer.config.opt_string(opt) != config.opt_string(opt)) + is_equal = false; + + if (is_equal) + return find_printer(printer.name); + } + return nullptr; +} + // Generate a file path from a profile name. Add the ".ini" suffix if it is missing. std::string PhysicalPrinterCollection::path_from_name(const std::string& new_name) const { diff --git a/src/libslic3r/Preset.hpp b/src/libslic3r/Preset.hpp index 02f831136..98b805b4e 100644 --- a/src/libslic3r/Preset.hpp +++ b/src/libslic3r/Preset.hpp @@ -549,12 +549,15 @@ public: // set of presets used with this physical printer std::set preset_names; - static std::string separator(); - // Has this profile been loaded? bool loaded = false; + static std::string separator(); static const std::vector& printer_options(); + static const std::vector& print_host_options(); + static bool has_print_host_information(const PrinterPresetCollection& printer_presets); + static bool has_print_host_information(const DynamicPrintConfig& config); + const std::set& get_preset_names() const; bool has_empty_config() const; @@ -626,7 +629,7 @@ public: // Load ini files of the particular type from the provided directory path. void load_printers(const std::string& dir_path, const std::string& subdir); - void load_printers(const PrinterPresetCollection &printer_presets, std::string def_printer_name = ""); + void load_printers_from_presets(PrinterPresetCollection &printer_presets, std::string def_printer_name); // Save the printer under a new name. If the name is different from the old one, // a new printer is stored into the list of printers. @@ -704,6 +707,8 @@ private: return const_cast(this)->find_printer_internal(name); } + PhysicalPrinter* find_printer_with_same_config( const DynamicPrintConfig &config); + // List of printers // Use deque to force the container to allocate an object per each entry, // so that the addresses of the presets don't change during resizing of the container. diff --git a/src/libslic3r/PresetBundle.cpp b/src/libslic3r/PresetBundle.cpp index 0e215a2ae..d074c77d7 100644 --- a/src/libslic3r/PresetBundle.cpp +++ b/src/libslic3r/PresetBundle.cpp @@ -201,7 +201,6 @@ void PresetBundle::load_presets(AppConfig &config, const std::string &preferred_ } try { this->physical_printers.load_printers(dir_user_presets, "physical_printer"); - this->physical_printers.load_printers(this->printers); } catch (const std::runtime_error &err) { errors_cummulative += err.what(); } @@ -436,8 +435,7 @@ void PresetBundle::load_selections(AppConfig &config, const std::string &preferr std::string initial_physical_printer_name = remove_ini_suffix(config.get("extras", "physical_printer")); // Activate physical printer from the config - const PhysicalPrinter* initial_physical_printer = physical_printers.find_printer(initial_physical_printer_name); - if (initial_physical_printer) + if (!initial_physical_printer_name.empty()) physical_printers.select_printer_by_name(initial_physical_printer_name); } diff --git a/src/slic3r/GUI/GUI_App.cpp b/src/slic3r/GUI/GUI_App.cpp index 089a38c6f..f7689c6e9 100644 --- a/src/slic3r/GUI/GUI_App.cpp +++ b/src/slic3r/GUI/GUI_App.cpp @@ -28,6 +28,9 @@ #include #include +#include +#include + #include "libslic3r/Utils.hpp" #include "libslic3r/Model.hpp" #include "libslic3r/I18N.hpp" @@ -629,6 +632,43 @@ void GUI_App::set_auto_toolbar_icon_scale(float scale) const app_config->set("auto_toolbar_size", val); } +// check user printer_presets for the containing information about "Print Host upload" +void GUI_App::check_printer_presets() +{ + if (!PhysicalPrinter::has_print_host_information(preset_bundle->printers)) + return; + + wxString msg_text = _L("You have presets with saved options for \"Print Host upload\".\n" + "But from this version of PrusaSlicer we don't show/use this information in Printer Settings.\n" + "Now, this information will be exposed in physical printers settings.") + "\n\n" + + _L("Enter the name for the Printer device used by defaul during its creation.\n" + "Note: This name can be changed later from the physical printers settings") + ":"; + wxString msg_header = _L("Name for printer device"); + + // get custom gcode + wxTextEntryDialog dlg(nullptr, msg_text, msg_header, _L("Printer"), wxTextEntryDialogStyle); + + // detect TextCtrl and OK button + wxTextCtrl* textctrl{ nullptr }; + wxWindowList& dlg_items = dlg.GetChildren(); + for (auto item : dlg_items) { + textctrl = dynamic_cast(item); + if (textctrl) + break; + } + + if (textctrl) { + textctrl->SetSelection(0, textctrl->GetLastPosition()); + + wxButton* btn_OK = static_cast(dlg.FindWindowById(wxID_OK)); + btn_OK->Bind(wxEVT_UPDATE_UI, [textctrl](wxUpdateUIEvent& evt) { + evt.Enable(!textctrl->IsEmpty()); + }, btn_OK->GetId()); + } + if (dlg.ShowModal() == wxID_OK) + preset_bundle->physical_printers.load_printers_from_presets(preset_bundle->printers, into_u8(dlg.GetValue())); +} + void GUI_App::recreate_GUI(const wxString& msg_name) { mainframe->shutdown(); @@ -1171,6 +1211,10 @@ bool GUI_App::checked_tab(Tab* tab) // Update UI / Tabs to reflect changes in the currently loaded presets void GUI_App::load_current_presets() { + // check printer_presets for the containing information about "Print Host upload" + // and create physical printer from it, if any exists + check_printer_presets(); + PrinterTechnology printer_technology = preset_bundle->printers.get_edited_preset().printer_technology(); this->plater()->set_printer_technology(printer_technology); for (Tab *tab : tabs_list) diff --git a/src/slic3r/GUI/GUI_App.hpp b/src/slic3r/GUI/GUI_App.hpp index 23567695c..db551610b 100644 --- a/src/slic3r/GUI/GUI_App.hpp +++ b/src/slic3r/GUI/GUI_App.hpp @@ -150,6 +150,7 @@ public: wxSize get_min_size() const; float toolbar_icon_scale(const bool is_limited = false) const; void set_auto_toolbar_icon_scale(float scale) const; + void check_printer_presets(); void recreate_GUI(const wxString& message); void system_info(); diff --git a/src/slic3r/GUI/PresetComboBoxes.cpp b/src/slic3r/GUI/PresetComboBoxes.cpp index 2d74344bd..e527ef9c9 100644 --- a/src/slic3r/GUI/PresetComboBoxes.cpp +++ b/src/slic3r/GUI/PresetComboBoxes.cpp @@ -359,8 +359,11 @@ bool PresetComboBox::selection_is_changed_according_to_physical_printers() // if new preset wasn't selected, there is no need to call update preset selection if (old_printer_preset == preset_name) { // we need just to update according Plater<->Tab PresetComboBox - if (dynamic_cast(this)!=nullptr) + if (dynamic_cast(this)!=nullptr) { wxGetApp().get_tab(m_type)->update_preset_choice(); + // Synchronize config.ini with the current selections. + m_preset_bundle->export_selections(*wxGetApp().app_config); + } else if (dynamic_cast(this)!=nullptr) wxGetApp().sidebar().update_presets(m_type);