diff options
author | enricoturri1966 <enricoturri@seznam.cz> | 2020-08-10 15:22:46 +0300 |
---|---|---|
committer | enricoturri1966 <enricoturri@seznam.cz> | 2020-08-10 15:22:46 +0300 |
commit | dea641183cdf00921888689c00153cf365e72aab (patch) | |
tree | 32e821f99344c27927f61b827c62b344e9c0a826 /src/slic3r/GUI | |
parent | 6ed2cb661de4d5175ebb8ee75b53c33009c9d1a8 (diff) | |
parent | f9e47b27028b7f513cea29843249894ae105ebc7 (diff) |
Merge branch 'master' of https://github.com/prusa3d/PrusaSlicer into et_gcode_viewer
Diffstat (limited to 'src/slic3r/GUI')
49 files changed, 2570 insertions, 5285 deletions
diff --git a/src/slic3r/GUI/3DBed.cpp b/src/slic3r/GUI/3DBed.cpp index cbb74411e..11fa745f4 100644 --- a/src/slic3r/GUI/3DBed.cpp +++ b/src/slic3r/GUI/3DBed.cpp @@ -10,7 +10,7 @@ #endif // ENABLE_GCODE_VIEWER #include "GUI_App.hpp" -#include "PresetBundle.hpp" +#include "libslic3r/PresetBundle.hpp" #include "GLCanvas3D.hpp" #if ENABLE_GCODE_VIEWER #include "3DScene.hpp" diff --git a/src/slic3r/GUI/3DScene.cpp b/src/slic3r/GUI/3DScene.cpp index c9c7b72f3..70fec670c 100644 --- a/src/slic3r/GUI/3DScene.cpp +++ b/src/slic3r/GUI/3DScene.cpp @@ -11,7 +11,6 @@ #include "GUI_App.hpp" #if ENABLE_ENVIRONMENT_MAP #include "Plater.hpp" -#include "AppConfig.hpp" #endif // ENABLE_ENVIRONMENT_MAP #include "libslic3r/ExtrusionEntity.hpp" @@ -29,6 +28,7 @@ #include "slic3r/GUI/BitmapCache.hpp" #include "libslic3r/Format/STL.hpp" #include "libslic3r/Utils.hpp" +#include "libslic3r/AppConfig.hpp" #include <stdio.h> #include <stdlib.h> diff --git a/src/slic3r/GUI/AppConfig.cpp b/src/slic3r/GUI/AppConfig.cpp deleted file mode 100644 index 93589e536..000000000 --- a/src/slic3r/GUI/AppConfig.cpp +++ /dev/null @@ -1,397 +0,0 @@ -#include "libslic3r/libslic3r.h" -#include "libslic3r/Utils.hpp" -#include "AppConfig.hpp" - -#include <utility> -#include <vector> -#include <stdexcept> - -#include <boost/filesystem/path.hpp> -#include <boost/filesystem/operations.hpp> -#include <boost/nowide/cenv.hpp> -#include <boost/nowide/fstream.hpp> -#include <boost/property_tree/ini_parser.hpp> -#include <boost/property_tree/ptree_fwd.hpp> -#include <boost/algorithm/string/predicate.hpp> -#include <boost/format/format_fwd.hpp> - -#include <wx/string.h> -#include "I18N.hpp" - -namespace Slic3r { - -static const std::string VENDOR_PREFIX = "vendor:"; -static const std::string MODEL_PREFIX = "model:"; -static const std::string VERSION_CHECK_URL = "https://files.prusa3d.com/wp-content/uploads/repository/PrusaSlicer-settings-master/live/PrusaSlicer.version"; - -const std::string AppConfig::SECTION_FILAMENTS = "filaments"; -const std::string AppConfig::SECTION_MATERIALS = "sla_materials"; - -void AppConfig::reset() -{ - m_storage.clear(); - set_defaults(); -}; - -// Override missing or keys with their defaults. -void AppConfig::set_defaults() -{ - // Reset the empty fields to defaults. - if (get("autocenter").empty()) - set("autocenter", "0"); - // Disable background processing by default as it is not stable. - if (get("background_processing").empty()) - set("background_processing", "0"); - // If set, the "Controller" tab for the control of the printer over serial line and the serial port settings are hidden. - // By default, Prusa has the controller hidden. - if (get("no_controller").empty()) - set("no_controller", "1"); - // If set, the "- default -" selections of print/filament/printer are suppressed, if there is a valid preset available. - if (get("no_defaults").empty()) - set("no_defaults", "1"); - if (get("show_incompatible_presets").empty()) - set("show_incompatible_presets", "0"); - - if (get("version_check").empty()) - set("version_check", "1"); - if (get("preset_update").empty()) - set("preset_update", "1"); - - if (get("export_sources_full_pathnames").empty()) - set("export_sources_full_pathnames", "0"); - - // remove old 'use_legacy_opengl' parameter from this config, if present - if (!get("use_legacy_opengl").empty()) - erase("", "use_legacy_opengl"); - -#ifdef __APPLE__ - if (get("use_retina_opengl").empty()) - set("use_retina_opengl", "1"); -#endif - - if (get("single_instance").empty()) - set("single_instance", "0"); - - if (get("remember_output_path").empty()) - set("remember_output_path", "1"); - - if (get("remember_output_path_removable").empty()) - set("remember_output_path_removable", "1"); - - if (get("use_custom_toolbar_size").empty()) - set("use_custom_toolbar_size", "0"); - - if (get("custom_toolbar_size").empty()) - set("custom_toolbar_size", "100"); - - if (get("auto_toolbar_size").empty()) - set("auto_toolbar_size", "100"); - - if (get("use_perspective_camera").empty()) - set("use_perspective_camera", "1"); - - if (get("use_free_camera").empty()) - set("use_free_camera", "0"); - -#if ENABLE_ENVIRONMENT_MAP - if (get("use_environment_map").empty()) - set("use_environment_map", "0"); -#endif // ENABLE_ENVIRONMENT_MAP - - if (get("use_inches").empty()) - set("use_inches", "0"); - - // Remove legacy window positions/sizes - erase("", "main_frame_maximized"); - erase("", "main_frame_pos"); - erase("", "main_frame_size"); - erase("", "object_settings_maximized"); - erase("", "object_settings_pos"); - erase("", "object_settings_size"); -} - -void AppConfig::load() -{ - // 1) Read the complete config file into a boost::property_tree. - namespace pt = boost::property_tree; - pt::ptree tree; - boost::nowide::ifstream ifs(AppConfig::config_path()); - try { - pt::read_ini(ifs, tree); - } catch (pt::ptree_error& ex) { - // Error while parsing config file. We'll customize the error message and rethrow to be displayed. - throw std::runtime_error( - _utf8(L("Error parsing PrusaSlicer config file, it is probably corrupted. " - "Try to manually delete the file to recover from the error. Your user profiles will not be affected.")) + - "\n\n" + AppConfig::config_path() + "\n\n" + ex.what()); - } - - // 2) Parse the property_tree, extract the sections and key / value pairs. - for (const auto §ion : tree) { - if (section.second.empty()) { - // This may be a top level (no section) entry, or an empty section. - std::string data = section.second.data(); - if (! data.empty()) - // If there is a non-empty data, then it must be a top-level (without a section) config entry. - m_storage[""][section.first] = data; - } else if (boost::starts_with(section.first, VENDOR_PREFIX)) { - // This is a vendor section listing enabled model / variants - const auto vendor_name = section.first.substr(VENDOR_PREFIX.size()); - auto &vendor = m_vendors[vendor_name]; - for (const auto &kvp : section.second) { - if (! boost::starts_with(kvp.first, MODEL_PREFIX)) { continue; } - const auto model_name = kvp.first.substr(MODEL_PREFIX.size()); - std::vector<std::string> variants; - if (! unescape_strings_cstyle(kvp.second.data(), variants)) { continue; } - for (const auto &variant : variants) { - vendor[model_name].insert(variant); - } - } - } else { - // This must be a section name. Read the entries of a section. - std::map<std::string, std::string> &storage = m_storage[section.first]; - for (auto &kvp : section.second) - storage[kvp.first] = kvp.second.data(); - } - } - - // Figure out if datadir has legacy presets - auto ini_ver = Semver::parse(get("version")); - m_legacy_datadir = false; - if (ini_ver) { - m_orig_version = *ini_ver; - // Make 1.40.0 alphas compare well - ini_ver->set_metadata(boost::none); - ini_ver->set_prerelease(boost::none); - m_legacy_datadir = ini_ver < Semver(1, 40, 0); - } - - // Override missing or keys with their defaults. - this->set_defaults(); - m_dirty = false; -} - -void AppConfig::save() -{ - // The config is first written to a file with a PID suffix and then moved - // to avoid race conditions with multiple instances of Slic3r - const auto path = config_path(); - std::string path_pid = (boost::format("%1%.%2%") % path % get_current_pid()).str(); - - boost::nowide::ofstream c; - c.open(path_pid, std::ios::out | std::ios::trunc); - c << "# " << Slic3r::header_slic3r_generated() << std::endl; - // Make sure the "no" category is written first. - for (const std::pair<std::string, std::string> &kvp : m_storage[""]) - c << kvp.first << " = " << kvp.second << std::endl; - // Write the other categories. - for (const auto category : m_storage) { - if (category.first.empty()) - continue; - c << std::endl << "[" << category.first << "]" << std::endl; - for (const std::pair<std::string, std::string> &kvp : category.second) - c << kvp.first << " = " << kvp.second << std::endl; - } - // Write vendor sections - for (const auto &vendor : m_vendors) { - size_t size_sum = 0; - for (const auto &model : vendor.second) { size_sum += model.second.size(); } - if (size_sum == 0) { continue; } - - c << std::endl << "[" << VENDOR_PREFIX << vendor.first << "]" << std::endl; - - for (const auto &model : vendor.second) { - if (model.second.size() == 0) { continue; } - const std::vector<std::string> variants(model.second.begin(), model.second.end()); - const auto escaped = escape_strings_cstyle(variants); - c << MODEL_PREFIX << model.first << " = " << escaped << std::endl; - } - } - c.close(); - - rename_file(path_pid, path); - m_dirty = false; -} - -bool AppConfig::get_variant(const std::string &vendor, const std::string &model, const std::string &variant) const -{ - const auto it_v = m_vendors.find(vendor); - if (it_v == m_vendors.end()) { return false; } - const auto it_m = it_v->second.find(model); - return it_m == it_v->second.end() ? false : it_m->second.find(variant) != it_m->second.end(); -} - -void AppConfig::set_variant(const std::string &vendor, const std::string &model, const std::string &variant, bool enable) -{ - if (enable) { - if (get_variant(vendor, model, variant)) { return; } - m_vendors[vendor][model].insert(variant); - } else { - auto it_v = m_vendors.find(vendor); - if (it_v == m_vendors.end()) { return; } - auto it_m = it_v->second.find(model); - if (it_m == it_v->second.end()) { return; } - auto it_var = it_m->second.find(variant); - if (it_var == it_m->second.end()) { return; } - it_m->second.erase(it_var); - } - // If we got here, there was an update - m_dirty = true; -} - -void AppConfig::set_vendors(const AppConfig &from) -{ - m_vendors = from.m_vendors; - m_dirty = true; -} - -std::string AppConfig::get_last_dir() const -{ - const auto it = m_storage.find("recent"); - if (it != m_storage.end()) { - { - const auto it2 = it->second.find("skein_directory"); - if (it2 != it->second.end() && ! it2->second.empty()) - return it2->second; - } - { - const auto it2 = it->second.find("config_directory"); - if (it2 != it->second.end() && ! it2->second.empty()) - return it2->second; - } - } - return std::string(); -} - -std::vector<std::string> AppConfig::get_recent_projects() const -{ - std::vector<std::string> ret; - const auto it = m_storage.find("recent_projects"); - if (it != m_storage.end()) - { - for (const std::map<std::string, std::string>::value_type& item : it->second) - { - ret.push_back(item.second); - } - } - return ret; -} - -void AppConfig::set_recent_projects(const std::vector<std::string>& recent_projects) -{ - auto it = m_storage.find("recent_projects"); - if (it == m_storage.end()) - it = m_storage.insert(std::map<std::string, std::map<std::string, std::string>>::value_type("recent_projects", std::map<std::string, std::string>())).first; - - it->second.clear(); - for (unsigned int i = 0; i < (unsigned int)recent_projects.size(); ++i) - { - it->second[std::to_string(i + 1)] = recent_projects[i]; - } -} - -void AppConfig::set_mouse_device(const std::string& name, double translation_speed, double translation_deadzone, float rotation_speed, float rotation_deadzone, double zoom_speed, bool swap_yz) -{ - std::string key = std::string("mouse_device:") + name; - auto it = m_storage.find(key); - if (it == m_storage.end()) - it = m_storage.insert(std::map<std::string, std::map<std::string, std::string>>::value_type(key, std::map<std::string, std::string>())).first; - - it->second.clear(); - it->second["translation_speed"] = std::to_string(translation_speed); - it->second["translation_deadzone"] = std::to_string(translation_deadzone); - it->second["rotation_speed"] = std::to_string(rotation_speed); - it->second["rotation_deadzone"] = std::to_string(rotation_deadzone); - it->second["zoom_speed"] = std::to_string(zoom_speed); - it->second["swap_yz"] = swap_yz ? "1" : "0"; -} - -std::vector<std::string> AppConfig::get_mouse_device_names() const -{ - static constexpr const char *prefix = "mouse_device:"; - static const size_t prefix_len = strlen(prefix); - std::vector<std::string> out; - for (const std::pair<std::string, std::map<std::string, std::string>>& key_value_pair : m_storage) - if (boost::starts_with(key_value_pair.first, prefix) && key_value_pair.first.size() > prefix_len) - out.emplace_back(key_value_pair.first.substr(prefix_len)); - return out; -} - -void AppConfig::update_config_dir(const std::string &dir) -{ - this->set("recent", "config_directory", dir); -} - -void AppConfig::update_skein_dir(const std::string &dir) -{ - this->set("recent", "skein_directory", dir); -} -/* -std::string AppConfig::get_last_output_dir(const std::string &alt) const -{ - - const auto it = m_storage.find(""); - if (it != m_storage.end()) { - const auto it2 = it->second.find("last_output_path"); - const auto it3 = it->second.find("remember_output_path"); - if (it2 != it->second.end() && it3 != it->second.end() && ! it2->second.empty() && it3->second == "1") - return it2->second; - } - return alt; -} - -void AppConfig::update_last_output_dir(const std::string &dir) -{ - this->set("", "last_output_path", dir); -} -*/ -std::string AppConfig::get_last_output_dir(const std::string& alt, const bool removable) const -{ - std::string s1 = (removable ? "last_output_path_removable" : "last_output_path"); - std::string s2 = (removable ? "remember_output_path_removable" : "remember_output_path"); - const auto it = m_storage.find(""); - if (it != m_storage.end()) { - const auto it2 = it->second.find(s1); - const auto it3 = it->second.find(s2); - if (it2 != it->second.end() && it3 != it->second.end() && !it2->second.empty() && it3->second == "1") - return it2->second; - } - return alt; -} - -void AppConfig::update_last_output_dir(const std::string& dir, const bool removable) -{ - this->set("", (removable ? "last_output_path_removable" : "last_output_path"), dir); -} - - -void AppConfig::reset_selections() -{ - auto it = m_storage.find("presets"); - if (it != m_storage.end()) { - it->second.erase("print"); - it->second.erase("filament"); - it->second.erase("sla_print"); - it->second.erase("sla_material"); - it->second.erase("printer"); - m_dirty = true; - } -} - -std::string AppConfig::config_path() -{ - return (boost::filesystem::path(Slic3r::data_dir()) / (SLIC3R_APP_KEY ".ini")).make_preferred().string(); -} - -std::string AppConfig::version_check_url() const -{ - auto from_settings = get("version_check_url"); - return from_settings.empty() ? VERSION_CHECK_URL : from_settings; -} - -bool AppConfig::exists() -{ - return boost::filesystem::exists(AppConfig::config_path()); -} - -}; // namespace Slic3r diff --git a/src/slic3r/GUI/AppConfig.hpp b/src/slic3r/GUI/AppConfig.hpp deleted file mode 100644 index 1e90d32e0..000000000 --- a/src/slic3r/GUI/AppConfig.hpp +++ /dev/null @@ -1,191 +0,0 @@ -#ifndef slic3r_AppConfig_hpp_ -#define slic3r_AppConfig_hpp_ - -#include <set> -#include <map> -#include <string> - -#include <boost/algorithm/string/trim_all.hpp> - -#include "libslic3r/Config.hpp" -#include "libslic3r/Semver.hpp" - -namespace Slic3r { - -class AppConfig -{ -public: - AppConfig() : - m_dirty(false), - m_orig_version(Semver::invalid()), - m_legacy_datadir(false) - { - this->reset(); - } - - // Clear and reset to defaults. - void reset(); - // Override missing or keys with their defaults. - void set_defaults(); - - // Load the slic3r.ini from a user profile directory (or a datadir, if configured). - void load(); - // Store the slic3r.ini into a user profile directory (or a datadir, if configured). - void save(); - - // Does this config need to be saved? - bool dirty() const { return m_dirty; } - - // Const accessor, it will return false if a section or a key does not exist. - bool get(const std::string §ion, const std::string &key, std::string &value) const - { - value.clear(); - auto it = m_storage.find(section); - if (it == m_storage.end()) - return false; - auto it2 = it->second.find(key); - if (it2 == it->second.end()) - return false; - value = it2->second; - return true; - } - std::string get(const std::string §ion, const std::string &key) const - { std::string value; this->get(section, key, value); return value; } - std::string get(const std::string &key) const - { std::string value; this->get("", key, value); return value; } - void set(const std::string §ion, const std::string &key, const std::string &value) - { -#ifndef _NDEBUG - std::string key_trimmed = key; - boost::trim_all(key_trimmed); - assert(key_trimmed == key); - assert(! key_trimmed.empty()); -#endif // _NDEBUG - std::string &old = m_storage[section][key]; - if (old != value) { - old = value; - m_dirty = true; - } - } - void set(const std::string &key, const std::string &value) - { this->set("", key, value); } - bool has(const std::string §ion, const std::string &key) const - { - auto it = m_storage.find(section); - if (it == m_storage.end()) - return false; - auto it2 = it->second.find(key); - return it2 != it->second.end() && ! it2->second.empty(); - } - bool has(const std::string &key) const - { return this->has("", key); } - - void erase(const std::string §ion, const std::string &key) - { - auto it = m_storage.find(section); - if (it != m_storage.end()) { - it->second.erase(key); - } - } - - bool has_section(const std::string §ion) const - { return m_storage.find(section) != m_storage.end(); } - const std::map<std::string, std::string>& get_section(const std::string §ion) const - { return m_storage.find(section)->second; } - void set_section(const std::string §ion, const std::map<std::string, std::string>& data) - { m_storage[section] = data; } - void clear_section(const std::string §ion) - { m_storage[section].clear(); } - - typedef std::map<std::string, std::map<std::string, std::set<std::string>>> VendorMap; - bool get_variant(const std::string &vendor, const std::string &model, const std::string &variant) const; - void set_variant(const std::string &vendor, const std::string &model, const std::string &variant, bool enable); - void set_vendors(const AppConfig &from); - void set_vendors(const VendorMap &vendors) { m_vendors = vendors; m_dirty = true; } - void set_vendors(VendorMap &&vendors) { m_vendors = std::move(vendors); m_dirty = true; } - const VendorMap& vendors() const { return m_vendors; } - - // return recent/skein_directory or recent/config_directory or empty string. - std::string get_last_dir() const; - void update_config_dir(const std::string &dir); - void update_skein_dir(const std::string &dir); - - //std::string get_last_output_dir(const std::string &alt) const; - //void update_last_output_dir(const std::string &dir); - std::string get_last_output_dir(const std::string& alt, const bool removable = false) const; - void update_last_output_dir(const std::string &dir, const bool removable = false); - - // reset the current print / filament / printer selections, so that - // the PresetBundle::load_selections(const AppConfig &config) call will select - // the first non-default preset when called. - void reset_selections(); - - // Get the default config path from Slic3r::data_dir(). - static std::string config_path(); - - // Returns true if the user's data directory comes from before Slic3r 1.40.0 (no updating) - bool legacy_datadir() const { return m_legacy_datadir; } - void set_legacy_datadir(bool value) { m_legacy_datadir = value; } - - // Get the Slic3r version check url. - // This returns a hardcoded string unless it is overriden by "version_check_url" in the ini file. - std::string version_check_url() const; - - // Returns the original Slic3r version found in the ini file before it was overwritten - // by the current version - Semver orig_version() const { return m_orig_version; } - - // Does the config file exist? - static bool exists(); - - std::vector<std::string> get_recent_projects() const; - void set_recent_projects(const std::vector<std::string>& recent_projects); - - void set_mouse_device(const std::string& name, double translation_speed, double translation_deadzone, float rotation_speed, float rotation_deadzone, double zoom_speed, bool swap_yz); - std::vector<std::string> get_mouse_device_names() const; - bool get_mouse_device_translation_speed(const std::string& name, double& speed) const - { return get_3dmouse_device_numeric_value(name, "translation_speed", speed); } - bool get_mouse_device_translation_deadzone(const std::string& name, double& deadzone) const - { return get_3dmouse_device_numeric_value(name, "translation_deadzone", deadzone); } - bool get_mouse_device_rotation_speed(const std::string& name, float& speed) const - { return get_3dmouse_device_numeric_value(name, "rotation_speed", speed); } - bool get_mouse_device_rotation_deadzone(const std::string& name, float& deadzone) const - { return get_3dmouse_device_numeric_value(name, "rotation_deadzone", deadzone); } - bool get_mouse_device_zoom_speed(const std::string& name, double& speed) const - { return get_3dmouse_device_numeric_value(name, "zoom_speed", speed); } - bool get_mouse_device_swap_yz(const std::string& name, bool& swap) const - { return get_3dmouse_device_numeric_value(name, "swap_yz", swap); } - - static const std::string SECTION_FILAMENTS; - static const std::string SECTION_MATERIALS; - -private: - template<typename T> - bool get_3dmouse_device_numeric_value(const std::string &device_name, const char *parameter_name, T &out) const - { - std::string key = std::string("mouse_device:") + device_name; - auto it = m_storage.find(key); - if (it == m_storage.end()) - return false; - auto it_val = it->second.find(parameter_name); - if (it_val == it->second.end()) - return false; - out = T(::atof(it_val->second.c_str())); - return true; - } - - // Map of section, name -> value - std::map<std::string, std::map<std::string, std::string>> m_storage; - // Map of enabled vendors / models / variants - VendorMap m_vendors; - // Has any value been modified since the config.ini has been last saved or loaded? - bool m_dirty; - // Original version found in the ini file before it was overwritten - Semver m_orig_version; - // Whether the existing version is before system profiles & configuration updating - bool m_legacy_datadir; -}; - -}; // namespace Slic3r - -#endif /* slic3r_AppConfig_hpp_ */ diff --git a/src/slic3r/GUI/Camera.cpp b/src/slic3r/GUI/Camera.cpp index 866c7e979..3bd22590f 100644 --- a/src/slic3r/GUI/Camera.cpp +++ b/src/slic3r/GUI/Camera.cpp @@ -1,8 +1,8 @@ #include "libslic3r/libslic3r.h" +#include "libslic3r/AppConfig.hpp" #include "Camera.hpp" #include "GUI_App.hpp" -#include "AppConfig.hpp" #if ENABLE_CAMERA_STATISTICS #include "Mouse3DController.hpp" #include "Plater.hpp" diff --git a/src/slic3r/GUI/ConfigManipulation.cpp b/src/slic3r/GUI/ConfigManipulation.cpp index 3e301566b..5836b8a2c 100644 --- a/src/slic3r/GUI/ConfigManipulation.cpp +++ b/src/slic3r/GUI/ConfigManipulation.cpp @@ -2,7 +2,7 @@ #include "ConfigManipulation.hpp" #include "I18N.hpp" #include "GUI_App.hpp" -#include "PresetBundle.hpp" +#include "libslic3r/PresetBundle.hpp" #include <wx/msgdlg.h> diff --git a/src/slic3r/GUI/ConfigWizard_private.hpp b/src/slic3r/GUI/ConfigWizard_private.hpp index c99c5952b..9921552a7 100644 --- a/src/slic3r/GUI/ConfigWizard_private.hpp +++ b/src/slic3r/GUI/ConfigWizard_private.hpp @@ -20,9 +20,8 @@ #include <wx/radiobut.h> #include "libslic3r/PrintConfig.hpp" +#include "libslic3r/PresetBundle.hpp" #include "slic3r/Utils/PresetUpdater.hpp" -#include "AppConfig.hpp" -#include "PresetBundle.hpp" #include "BedShapeDialog.hpp" #include "GUI.hpp" #include "wxExtensions.hpp" diff --git a/src/slic3r/GUI/Field.cpp b/src/slic3r/GUI/Field.cpp index 3a06c3056..9cb3d726d 100644 --- a/src/slic3r/GUI/Field.cpp +++ b/src/slic3r/GUI/Field.cpp @@ -295,6 +295,7 @@ void Field::msw_rescale(bool rescale_sidetext) { m_Undo_to_sys_btn->msw_rescale(); m_Undo_btn->msw_rescale(); + m_blinking_bmp->msw_rescale(); // update em_unit value m_em_unit = em_unit(m_parent); @@ -1079,6 +1080,8 @@ boost::any& Choice::get_value() m_value = static_cast<SLADisplayOrientation>(ret_enum); else if (m_opt_id.compare("support_pillar_connection_mode") == 0) m_value = static_cast<SLAPillarConnectionMode>(ret_enum); + else if (m_opt_id == "authorization_type") + m_value = static_cast<AuthorizationType>(ret_enum); } else if (m_opt.gui_type == "f_enum_open") { const int ret_enum = field->GetSelection(); diff --git a/src/slic3r/GUI/Field.hpp b/src/slic3r/GUI/Field.hpp index 484b2059f..1a4997756 100644 --- a/src/slic3r/GUI/Field.hpp +++ b/src/slic3r/GUI/Field.hpp @@ -151,6 +151,8 @@ public: virtual wxSizer* getSizer() { return nullptr; } virtual wxWindow* getWindow() { return nullptr; } + wxStaticText* getLabel() { return m_Label; } + bool is_matched(const std::string& string, const std::string& pattern); void get_value_by_opt_type(wxString& str, const bool check_value = true); diff --git a/src/slic3r/GUI/GLCanvas3D.cpp b/src/slic3r/GUI/GLCanvas3D.cpp index 35be45559..db8d9f953 100644 --- a/src/slic3r/GUI/GLCanvas3D.cpp +++ b/src/slic3r/GUI/GLCanvas3D.cpp @@ -15,11 +15,11 @@ #include "libslic3r/Utils.hpp" #include "libslic3r/Technologies.hpp" #include "libslic3r/Tesselate.hpp" +#include "libslic3r/PresetBundle.hpp" #include "slic3r/GUI/3DScene.hpp" #include "slic3r/GUI/BackgroundSlicingProcess.hpp" #include "slic3r/GUI/GLShader.hpp" #include "slic3r/GUI/GUI.hpp" -#include "slic3r/GUI/PresetBundle.hpp" #include "slic3r/GUI/Tab.hpp" #include "slic3r/GUI/GUI_Preview.hpp" #include "slic3r/GUI/OpenGLManager.hpp" diff --git a/src/slic3r/GUI/GUI.cpp b/src/slic3r/GUI/GUI.cpp index 9baf952b2..913716dfd 100644 --- a/src/slic3r/GUI/GUI.cpp +++ b/src/slic3r/GUI/GUI.cpp @@ -194,6 +194,8 @@ void change_opt_value(DynamicPrintConfig& config, const t_config_option_key& opt config.set_key_value(opt_key, new ConfigOptionEnum<SLADisplayOrientation>(boost::any_cast<SLADisplayOrientation>(value))); else if(opt_key.compare("support_pillar_connection_mode") == 0) config.set_key_value(opt_key, new ConfigOptionEnum<SLAPillarConnectionMode>(boost::any_cast<SLAPillarConnectionMode>(value))); + else if(opt_key == "authorization_type") + config.set_key_value(opt_key, new ConfigOptionEnum<AuthorizationType>(boost::any_cast<AuthorizationType>(value))); } break; case coPoints:{ diff --git a/src/slic3r/GUI/GUI_App.cpp b/src/slic3r/GUI/GUI_App.cpp index f47f7f9f1..18c5fd4f5 100644 --- a/src/slic3r/GUI/GUI_App.cpp +++ b/src/slic3r/GUI/GUI_App.cpp @@ -28,14 +28,16 @@ #include <wx/log.h> #include <wx/intl.h> +#include <wx/dialog.h> +#include <wx/textctrl.h> + #include "libslic3r/Utils.hpp" #include "libslic3r/Model.hpp" #include "libslic3r/I18N.hpp" +#include "libslic3r/PresetBundle.hpp" #include "GUI.hpp" #include "GUI_Utils.hpp" -#include "AppConfig.hpp" -#include "PresetBundle.hpp" #include "3DScene.hpp" #include "MainFrame.hpp" #include "Plater.hpp" @@ -323,7 +325,13 @@ void GUI_App::init_app_config() // load settings app_conf_exists = app_config->exists(); if (app_conf_exists) { - app_config->load(); + std::string error = app_config->load(); + if (!error.empty()) + // Error while parsing config file. We'll customize the error message and rethrow to be displayed. + throw std::runtime_error( + _u8L("Error parsing PrusaSlicer config file, it is probably corrupted. " + "Try to manually delete the file to recover from the error. Your user profiles will not be affected.") + + "\n\n" + AppConfig::config_path() + "\n\n" + error); } } @@ -635,6 +643,27 @@ 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() +{ + std::vector<std::string> preset_names = PhysicalPrinter::presets_with_print_host_information(preset_bundle->printers); + if (preset_names.empty()) + return; + + wxString msg_text = _L("You have next presets with saved options for \"Print Host upload\"") + ":"; + for (const std::string& preset_name : preset_names) + msg_text += "\n \"" + from_u8(preset_name) + "\","; + msg_text.RemoveLast(); + msg_text += "\n\n" + _L("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("By default new Printer devices will be named as \"Printer N\" during its creation.\n" + "Note: This name can be changed later from the physical printers settings"); + + wxMessageDialog(nullptr, msg_text, _L("Information"), wxOK | wxICON_INFORMATION).ShowModal(); + + preset_bundle->physical_printers.load_printers_from_presets(preset_bundle->printers); +} + void GUI_App::recreate_GUI(const wxString& msg_name) { mainframe->shutdown(); @@ -957,7 +986,7 @@ bool GUI_App::load_language(wxString language, bool initial) m_imgui->set_language(into_u8(language_info->CanonicalName)); //FIXME This is a temporary workaround, the correct solution is to switch to "C" locale during file import / export only. wxSetlocale(LC_NUMERIC, "C"); - Preset::update_suffix_modified(); + Preset::update_suffix_modified((" (" + _L("modified") + ")").ToUTF8().data()); return true; } @@ -1178,6 +1207,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 4c3f73640..191d7c264 100644 --- a/src/slic3r/GUI/GUI_App.hpp +++ b/src/slic3r/GUI/GUI_App.hpp @@ -3,10 +3,10 @@ #include <memory> #include <string> -#include "Preset.hpp" #include "ImGuiWrapper.hpp" #include "ConfigWizard.hpp" #include "OpenGLManager.hpp" +#include "libslic3r/Preset.hpp" #include <wx/app.h> #include <wx/colour.h> @@ -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/GUI_ObjectLayers.cpp b/src/slic3r/GUI/GUI_ObjectLayers.cpp index b1a5512d4..90a725fbf 100644 --- a/src/slic3r/GUI/GUI_ObjectLayers.cpp +++ b/src/slic3r/GUI/GUI_ObjectLayers.cpp @@ -3,7 +3,7 @@ #include "OptionsGroup.hpp" #include "GUI_App.hpp" -#include "PresetBundle.hpp" +#include "libslic3r/PresetBundle.hpp" #include "libslic3r/Model.hpp" #include "GLCanvas3D.hpp" #include "Plater.hpp" diff --git a/src/slic3r/GUI/GUI_ObjectList.cpp b/src/slic3r/GUI/GUI_ObjectList.cpp index c10853f69..a326eea7b 100644 --- a/src/slic3r/GUI/GUI_ObjectList.cpp +++ b/src/slic3r/GUI/GUI_ObjectList.cpp @@ -1,4 +1,5 @@ #include "libslic3r/libslic3r.h" +#include "libslic3r/PresetBundle.hpp" #include "GUI_ObjectList.hpp" #include "GUI_ObjectManipulation.hpp" #include "GUI_ObjectLayers.hpp" @@ -7,7 +8,6 @@ #include "Plater.hpp" #include "OptionsGroup.hpp" -#include "PresetBundle.hpp" #include "Tab.hpp" #include "wxExtensions.hpp" #include "libslic3r/Model.hpp" @@ -88,9 +88,6 @@ ObjectList::ObjectList(wxWindow* parent) : { // Fill CATEGORY_ICON { - // Note: `this` isn't passed to create_scaled_bitmap() here because of bugs in the widget, - // see note in PresetBundle::load_compatible_bitmaps() - // ptFFF CATEGORY_ICON[L("Layers and Perimeters")] = create_scaled_bitmap("layers"); CATEGORY_ICON[L("Infill")] = create_scaled_bitmap("infill"); diff --git a/src/slic3r/GUI/GUI_ObjectManipulation.cpp b/src/slic3r/GUI/GUI_ObjectManipulation.cpp index 2c35fc316..7243e8c73 100644 --- a/src/slic3r/GUI/GUI_ObjectManipulation.cpp +++ b/src/slic3r/GUI/GUI_ObjectManipulation.cpp @@ -6,7 +6,7 @@ #include "OptionsGroup.hpp" #include "GUI_App.hpp" #include "wxExtensions.hpp" -#include "PresetBundle.hpp" +#include "libslic3r/PresetBundle.hpp" #include "libslic3r/Model.hpp" #include "libslic3r/Geometry.hpp" #include "Selection.hpp" diff --git a/src/slic3r/GUI/GUI_ObjectSettings.cpp b/src/slic3r/GUI/GUI_ObjectSettings.cpp index ef78123a4..398cd51d4 100644 --- a/src/slic3r/GUI/GUI_ObjectSettings.cpp +++ b/src/slic3r/GUI/GUI_ObjectSettings.cpp @@ -4,8 +4,8 @@ #include "OptionsGroup.hpp" #include "GUI_App.hpp" #include "wxExtensions.hpp" -#include "PresetBundle.hpp" #include "Plater.hpp" +#include "libslic3r/PresetBundle.hpp" #include "libslic3r/Model.hpp" #include <boost/algorithm/string.hpp> diff --git a/src/slic3r/GUI/GUI_Preview.cpp b/src/slic3r/GUI/GUI_Preview.cpp index 03e062d65..2c5a6fe88 100644 --- a/src/slic3r/GUI/GUI_Preview.cpp +++ b/src/slic3r/GUI/GUI_Preview.cpp @@ -10,7 +10,7 @@ #include "BackgroundSlicingProcess.hpp" #include "OpenGLManager.hpp" #include "GLCanvas3D.hpp" -#include "PresetBundle.hpp" +#include "libslic3r/PresetBundle.hpp" #include "DoubleSlider.hpp" #include "Plater.hpp" #if ENABLE_GCODE_VIEWER diff --git a/src/slic3r/GUI/Gizmos/GLGizmoFdmSupports.cpp b/src/slic3r/GUI/Gizmos/GLGizmoFdmSupports.cpp index 5c4e59036..39ba13410 100644 --- a/src/slic3r/GUI/Gizmos/GLGizmoFdmSupports.cpp +++ b/src/slic3r/GUI/Gizmos/GLGizmoFdmSupports.cpp @@ -6,9 +6,9 @@ #include <GL/glew.h> #include "slic3r/GUI/GUI_App.hpp" -#include "slic3r/GUI/PresetBundle.hpp" #include "slic3r/GUI/Camera.hpp" #include "slic3r/GUI/Plater.hpp" +#include "libslic3r/PresetBundle.hpp" #include "libslic3r/Model.hpp" diff --git a/src/slic3r/GUI/Gizmos/GLGizmoHollow.cpp b/src/slic3r/GUI/Gizmos/GLGizmoHollow.cpp index 658db64ca..273384da2 100644 --- a/src/slic3r/GUI/Gizmos/GLGizmoHollow.cpp +++ b/src/slic3r/GUI/Gizmos/GLGizmoHollow.cpp @@ -9,7 +9,7 @@ #include "slic3r/GUI/GUI_ObjectSettings.hpp" #include "slic3r/GUI/GUI_ObjectList.hpp" #include "slic3r/GUI/Plater.hpp" -#include "slic3r/GUI/PresetBundle.hpp" +#include "libslic3r/PresetBundle.hpp" #include "libslic3r/Model.hpp" diff --git a/src/slic3r/GUI/Gizmos/GLGizmoSlaSupports.cpp b/src/slic3r/GUI/Gizmos/GLGizmoSlaSupports.cpp index 908fe27b1..2856bb35d 100644 --- a/src/slic3r/GUI/Gizmos/GLGizmoSlaSupports.cpp +++ b/src/slic3r/GUI/Gizmos/GLGizmoSlaSupports.cpp @@ -16,7 +16,7 @@ #include "slic3r/GUI/GUI_ObjectSettings.hpp" #include "slic3r/GUI/GUI_ObjectList.hpp" #include "slic3r/GUI/Plater.hpp" -#include "slic3r/GUI/PresetBundle.hpp" +#include "libslic3r/PresetBundle.hpp" #include "libslic3r/SLAPrint.hpp" diff --git a/src/slic3r/GUI/Gizmos/GLGizmosCommon.cpp b/src/slic3r/GUI/Gizmos/GLGizmosCommon.cpp index 28f317c26..a25d9105f 100644 --- a/src/slic3r/GUI/Gizmos/GLGizmosCommon.cpp +++ b/src/slic3r/GUI/Gizmos/GLGizmosCommon.cpp @@ -8,7 +8,7 @@ #include "slic3r/GUI/Camera.hpp" #include "slic3r/GUI/Plater.hpp" -#include "slic3r/GUI/PresetBundle.hpp" +#include "libslic3r/PresetBundle.hpp" #include <GL/glew.h> diff --git a/src/slic3r/GUI/Gizmos/GLGizmosManager.cpp b/src/slic3r/GUI/Gizmos/GLGizmosManager.cpp index 511c68735..c33ba2850 100644 --- a/src/slic3r/GUI/Gizmos/GLGizmosManager.cpp +++ b/src/slic3r/GUI/Gizmos/GLGizmosManager.cpp @@ -5,7 +5,6 @@ #include "slic3r/GUI/Camera.hpp" #include "slic3r/GUI/GUI_App.hpp" #include "slic3r/GUI/GUI_ObjectManipulation.hpp" -#include "slic3r/GUI/PresetBundle.hpp" #include "slic3r/GUI/Plater.hpp" #include "slic3r/Utils/UndoRedo.hpp" @@ -19,6 +18,7 @@ #include "slic3r/GUI/Gizmos/GLGizmoHollow.hpp" #include "libslic3r/Model.hpp" +#include "libslic3r/PresetBundle.hpp" #include <wx/glcanvas.h> diff --git a/src/slic3r/GUI/I18N.hpp b/src/slic3r/GUI/I18N.hpp index 25e46930b..7bad6880e 100644 --- a/src/slic3r/GUI/I18N.hpp +++ b/src/slic3r/GUI/I18N.hpp @@ -12,7 +12,7 @@ #ifndef L // !!! If you needed to translate some wxString, -// !!! please use _(L(string)) +// !!! please use _L(string) // !!! _() - is a standard wxWidgets macro to translate // !!! L() is used only for marking localizable string // !!! It will be used in "xgettext" to create a Locating Message Catalog. diff --git a/src/slic3r/GUI/Jobs/SLAImportJob.cpp b/src/slic3r/GUI/Jobs/SLAImportJob.cpp index 4e9f08ff2..2d5d5b072 100644 --- a/src/slic3r/GUI/Jobs/SLAImportJob.cpp +++ b/src/slic3r/GUI/Jobs/SLAImportJob.cpp @@ -2,13 +2,12 @@ #include "slic3r/GUI/GUI.hpp" #include "slic3r/GUI/GUI_App.hpp" -#include "slic3r/GUI/AppConfig.hpp" #include "slic3r/GUI/Plater.hpp" -#include "slic3r/GUI/PresetBundle.hpp" #include "slic3r/GUI/GUI_ObjectList.hpp" #include "slic3r/Utils/SLAImport.hpp" #include "libslic3r/Model.hpp" +#include "libslic3r/PresetBundle.hpp" #include <wx/dialog.h> #include <wx/stattext.h> diff --git a/src/slic3r/GUI/MainFrame.cpp b/src/slic3r/GUI/MainFrame.cpp index f017900f4..794da80b2 100644 --- a/src/slic3r/GUI/MainFrame.cpp +++ b/src/slic3r/GUI/MainFrame.cpp @@ -15,12 +15,11 @@ #include "libslic3r/Print.hpp" #include "libslic3r/Polygon.hpp" #include "libslic3r/SLAPrint.hpp" +#include "libslic3r/PresetBundle.hpp" #include "Tab.hpp" -#include "PresetBundle.hpp" #include "ProgressStatusBar.hpp" #include "3DScene.hpp" -#include "AppConfig.hpp" #include "PrintHostDialogs.hpp" #include "wxExtensions.hpp" #include "GUI_ObjectList.hpp" @@ -104,11 +103,6 @@ DPIFrame(NULL, wxID_ANY, "", wxDefaultPosition, wxDefaultSize, wxDEFAULT_FRAME_S SLIC3R_VERSION + _(L(" - Remember to check for updates at https://github.com/prusa3d/PrusaSlicer/releases"))); - /* Load default preset bitmaps before a tabpanel initialization, - * but after filling of an em_unit value - */ - wxGetApp().preset_bundle->load_default_preset_bitmaps(); - // initialize tabpanel and menubar init_tabpanel(); #if ENABLE_GCODE_VIEWER @@ -717,11 +711,6 @@ void MainFrame::on_dpi_changed(const wxRect& suggested_rect) #endif // ENABLE_WX_3_1_3_DPI_CHANGED_EVENT this->SetFont(this->normal_font()); - /* Load default preset bitmaps before a tabpanel initialization, - * but after filling of an em_unit value - */ - wxGetApp().preset_bundle->load_default_preset_bitmaps(); - // update Plater wxGetApp().plater()->msw_rescale(); @@ -766,8 +755,6 @@ void MainFrame::on_sys_color_changed() // update label colors in respect to the system mode wxGetApp().init_label_colours(); - wxGetApp().preset_bundle->load_default_preset_bitmaps(); - // update Plater wxGetApp().plater()->sys_color_changed(); diff --git a/src/slic3r/GUI/Mouse3DController.cpp b/src/slic3r/GUI/Mouse3DController.cpp index 33f0d6379..4c5ee2076 100644 --- a/src/slic3r/GUI/Mouse3DController.cpp +++ b/src/slic3r/GUI/Mouse3DController.cpp @@ -1,10 +1,9 @@ #include "libslic3r/libslic3r.h" +#include "libslic3r/PresetBundle.hpp" #include "Mouse3DController.hpp" #include "Camera.hpp" #include "GUI_App.hpp" -#include "PresetBundle.hpp" -#include "AppConfig.hpp" #include "GLCanvas3D.hpp" #include "Plater.hpp" #include "NotificationManager.hpp" diff --git a/src/slic3r/GUI/OptionsGroup.cpp b/src/slic3r/GUI/OptionsGroup.cpp index 819c214a8..1bebb8827 100644 --- a/src/slic3r/GUI/OptionsGroup.cpp +++ b/src/slic3r/GUI/OptionsGroup.cpp @@ -729,31 +729,34 @@ boost::any ConfigOptionsGroup::get_config_value(const DynamicPrintConfig& config opt_key == "fill_pattern" ) { ret = static_cast<int>(config.option<ConfigOptionEnum<InfillPattern>>(opt_key)->value); } - else if (opt_key.compare("ironing_type") == 0 ) { + else if (opt_key == "ironing_type") { ret = static_cast<int>(config.option<ConfigOptionEnum<IroningType>>(opt_key)->value); } - else if (opt_key.compare("gcode_flavor") == 0 ) { + else if (opt_key == "gcode_flavor") { ret = static_cast<int>(config.option<ConfigOptionEnum<GCodeFlavor>>(opt_key)->value); } - else if (opt_key.compare("support_material_pattern") == 0) { + else if (opt_key == "support_material_pattern") { ret = static_cast<int>(config.option<ConfigOptionEnum<SupportMaterialPattern>>(opt_key)->value); } - else if (opt_key.compare("seam_position") == 0) { + else if (opt_key == "seam_position") { ret = static_cast<int>(config.option<ConfigOptionEnum<SeamPosition>>(opt_key)->value); } - else if (opt_key.compare("host_type") == 0) { + else if (opt_key == "host_type") { ret = static_cast<int>(config.option<ConfigOptionEnum<PrintHostType>>(opt_key)->value); } - else if (opt_key.compare("display_orientation") == 0) { + else if (opt_key == "display_orientation") { ret = static_cast<int>(config.option<ConfigOptionEnum<SLADisplayOrientation>>(opt_key)->value); } - else if (opt_key.compare("support_pillar_connection_mode") == 0) { + else if (opt_key == "support_pillar_connection_mode") { ret = static_cast<int>(config.option<ConfigOptionEnum<SLAPillarConnectionMode>>(opt_key)->value); } + else if (opt_key == "authorization_type") { + ret = static_cast<int>(config.option<ConfigOptionEnum<AuthorizationType>>(opt_key)->value); + } } break; case coPoints: - if (opt_key.compare("bed_shape") == 0) + if (opt_key == "bed_shape") ret = config.option<ConfigOptionPoints>(opt_key)->values; else ret = config.option<ConfigOptionPoints>(opt_key)->get_at(idx); diff --git a/src/slic3r/GUI/OptionsGroup.hpp b/src/slic3r/GUI/OptionsGroup.hpp index 2e6f9aa0f..edd4a15bc 100644 --- a/src/slic3r/GUI/OptionsGroup.hpp +++ b/src/slic3r/GUI/OptionsGroup.hpp @@ -149,6 +149,13 @@ public: return true; } + void show_field(const t_config_option_key& opt_key, bool show = true) { + Field* field = get_field(opt_key); + field->getWindow()->Show(show); + field->getLabel()->Show(show); + } + void hide_field(const t_config_option_key& opt_key) { show_field(opt_key, false); } + void set_name(const wxString& new_name) { stb->SetLabel(new_name); } diff --git a/src/slic3r/GUI/PhysicalPrinterDialog.cpp b/src/slic3r/GUI/PhysicalPrinterDialog.cpp new file mode 100644 index 000000000..12d1cd287 --- /dev/null +++ b/src/slic3r/GUI/PhysicalPrinterDialog.cpp @@ -0,0 +1,564 @@ +#include "PhysicalPrinterDialog.hpp" +#include "PresetComboBoxes.hpp" + +#include <cstddef> +#include <vector> +#include <string> +#include <boost/algorithm/string.hpp> + +#include <wx/sizer.h> +#include <wx/stattext.h> +#include <wx/textctrl.h> +#include <wx/button.h> +#include <wx/statbox.h> +#include <wx/wupdlock.h> + +#include "libslic3r/libslic3r.h" +#include "libslic3r/PrintConfig.hpp" +#include "libslic3r/PresetBundle.hpp" + +#include "GUI.hpp" +#include "GUI_App.hpp" +#include "MainFrame.hpp" +#include "format.hpp" +#include "Tab.hpp" +#include "wxExtensions.hpp" +#include "PrintHostDialogs.hpp" +#include "../Utils/ASCIIFolding.hpp" +#include "../Utils/PrintHost.hpp" +#include "../Utils/FixModelByWin10.hpp" +#include "../Utils/UndoRedo.hpp" +#include "RemovableDriveManager.hpp" +#include "BitmapCache.hpp" +#include "BonjourDialog.hpp" + +using Slic3r::GUI::format_wxstr; + +//static const std::pair<unsigned int, unsigned int> THUMBNAIL_SIZE_3MF = { 256, 256 }; + +namespace Slic3r { +namespace GUI { + +#define BORDER_W 10 + +//------------------------------------------ +// PresetForPrinter +//------------------------------------------ + +PresetForPrinter::PresetForPrinter(PhysicalPrinterDialog* parent, const std::string& preset_name) : + m_parent(parent) +{ + m_sizer = new wxBoxSizer(wxVERTICAL); + + m_delete_preset_btn = new ScalableButton(parent, wxID_ANY, "cross", "", wxDefaultSize, wxDefaultPosition, /*wxBU_LEFT | */wxBU_EXACTFIT); + m_delete_preset_btn->SetFont(wxGetApp().normal_font()); + m_delete_preset_btn->SetToolTip(_L("Delete this preset from this printer device")); + m_delete_preset_btn->Bind(wxEVT_BUTTON, &PresetForPrinter::DeletePreset, this); + + m_presets_list = new PresetComboBox(parent, Preset::TYPE_PRINTER); + m_presets_list->set_printer_technology(parent->get_printer_technology()); + + m_presets_list->set_selection_changed_function([this](int selection) { + std::string selected_string = Preset::remove_suffix_modified(m_presets_list->GetString(selection).ToUTF8().data()); + Preset* preset = wxGetApp().preset_bundle->printers.find_preset(selected_string); + assert(preset); + Preset& edited_preset = wxGetApp().preset_bundle->printers.get_edited_preset(); + if (preset->name == edited_preset.name) + preset = &edited_preset; + + // if created physical printer doesn't have any settings, use the settings from the selected preset + if (m_parent->get_printer()->has_empty_config()) { + // update Print Host upload from the selected preset + m_parent->get_printer()->update_from_preset(*preset); + // update values in parent (PhysicalPrinterDialog) + m_parent->update(); + } + + // update PrinterTechnology if it was changed + if (m_presets_list->set_printer_technology(preset->printer_technology())) + m_parent->set_printer_technology(preset->printer_technology()); + + update_full_printer_name(); + }); + m_presets_list->update(preset_name); + + m_info_line = new wxStaticText(parent, wxID_ANY, _L("This printer will be shown in the presets list as") + ":"); + + m_full_printer_name = new wxStaticText(parent, wxID_ANY, ""); + m_full_printer_name->SetFont(wxGetApp().bold_font()); + + wxBoxSizer* preset_sizer = new wxBoxSizer(wxHORIZONTAL); + preset_sizer->Add(m_presets_list , 1, wxEXPAND); + preset_sizer->Add(m_delete_preset_btn , 0, wxEXPAND | wxLEFT, BORDER_W); + + wxBoxSizer* name_sizer = new wxBoxSizer(wxHORIZONTAL); + name_sizer->Add(m_info_line, 0, wxEXPAND); + name_sizer->Add(m_full_printer_name, 0, wxEXPAND | wxLEFT, BORDER_W); + + m_sizer->Add(preset_sizer , 0, wxEXPAND); + m_sizer->Add(name_sizer, 0, wxEXPAND); +} + +PresetForPrinter::~PresetForPrinter() +{ + m_presets_list->Destroy(); + m_delete_preset_btn->Destroy(); + m_info_line->Destroy(); + m_full_printer_name->Destroy(); +} + +void PresetForPrinter::DeletePreset(wxEvent& event) +{ + m_parent->DeletePreset(this); +} + +void PresetForPrinter::update_full_printer_name() +{ + wxString printer_name = m_parent->get_printer_name(); + wxString preset_name = m_presets_list->GetString(m_presets_list->GetSelection()); + + m_full_printer_name->SetLabelText(printer_name + " * " + preset_name); +} + +std::string PresetForPrinter::get_preset_name() +{ + return into_u8(m_presets_list->GetString(m_presets_list->GetSelection())); +} + +void PresetForPrinter::SuppressDelete() +{ + m_delete_preset_btn->Enable(false); + + // this case means that now we have only one related preset for the printer + // So, allow any selection + m_presets_list->set_printer_technology(ptAny); + m_presets_list->update(); +} + +void PresetForPrinter::AllowDelete() +{ + if (!m_delete_preset_btn->IsEnabled()) + m_delete_preset_btn->Enable(); + + m_presets_list->set_printer_technology(m_parent->get_printer_technology()); + m_presets_list->update(); +} + +void PresetForPrinter::msw_rescale() +{ + m_presets_list->msw_rescale(); + m_delete_preset_btn->msw_rescale(); +} + + +//------------------------------------------ +// PhysicalPrinterDialog +//------------------------------------------ + +PhysicalPrinterDialog::PhysicalPrinterDialog(wxString printer_name) + : DPIDialog(NULL, wxID_ANY, _L("Physical Printer"), wxDefaultPosition, wxSize(45 * wxGetApp().em_unit(), -1), wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER) +{ + SetFont(wxGetApp().normal_font()); + SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW)); + + m_default_name = _L("Type here the name of your printer device"); + bool new_printer = true; + + if (printer_name.IsEmpty()) + printer_name = m_default_name; + else { + std::string full_name = into_u8(printer_name); + printer_name = from_u8(PhysicalPrinter::get_short_name(full_name)); + new_printer = false; + } + + wxStaticText* label_top = new wxStaticText(this, wxID_ANY, _L("Descriptive name for the printer device") + ":"); + + m_add_preset_btn = new ScalableButton(this, wxID_ANY, "add_copies", "", wxDefaultSize, wxDefaultPosition, /*wxBU_LEFT | */wxBU_EXACTFIT); + m_add_preset_btn->SetFont(wxGetApp().normal_font()); + m_add_preset_btn->SetToolTip(_L("Add preset for this printer device")); + m_add_preset_btn->Bind(wxEVT_BUTTON, &PhysicalPrinterDialog::AddPreset, this); + + m_printer_name = new wxTextCtrl(this, wxID_ANY, printer_name, wxDefaultPosition, wxDefaultSize); + m_printer_name->Bind(wxEVT_TEXT, [this](wxEvent&) { this->update_full_printer_names(); }); + + PhysicalPrinterCollection& printers = wxGetApp().preset_bundle->physical_printers; + PhysicalPrinter* printer = printers.find_printer(into_u8(printer_name)); + if (!printer) { + const Preset& preset = wxGetApp().preset_bundle->printers.get_edited_preset(); + printer = new PhysicalPrinter(into_u8(printer_name), preset); + // if printer_name is empty it means that new printer is created, so enable all items in the preset list + m_presets.emplace_back(new PresetForPrinter(this, preset.name)); + } + else + { + const std::set<std::string>& preset_names = printer->get_preset_names(); + for (const std::string& preset_name : preset_names) + m_presets.emplace_back(new PresetForPrinter(this, preset_name)); + } + assert(printer); + m_printer = *printer; + + if (m_presets.size() == 1) + m_presets.front()->SuppressDelete(); + + update_full_printer_names(); + + m_config = &m_printer.config; + + m_optgroup = new ConfigOptionsGroup(this, _L("Print Host upload"), m_config); + build_printhost_settings(m_optgroup); + + wxStdDialogButtonSizer* btns = this->CreateStdDialogButtonSizer(wxOK | wxCANCEL); + wxButton* btnOK = static_cast<wxButton*>(this->FindWindowById(wxID_OK, this)); + btnOK->Bind(wxEVT_BUTTON, &PhysicalPrinterDialog::OnOK, this); + + wxBoxSizer* nameSizer = new wxBoxSizer(wxHORIZONTAL); + nameSizer->Add(m_printer_name, 1, wxEXPAND); + nameSizer->Add(m_add_preset_btn, 0, wxEXPAND | wxLEFT, BORDER_W); + + m_presets_sizer = new wxBoxSizer(wxVERTICAL); + for (PresetForPrinter* preset : m_presets) + m_presets_sizer->Add(preset->sizer(), 1, wxEXPAND | wxTOP, BORDER_W); + + wxBoxSizer* topSizer = new wxBoxSizer(wxVERTICAL); + + topSizer->Add(label_top , 0, wxEXPAND | wxLEFT | wxTOP | wxRIGHT, BORDER_W); + topSizer->Add(nameSizer , 0, wxEXPAND | wxLEFT | wxRIGHT, BORDER_W); + topSizer->Add(m_presets_sizer , 0, wxEXPAND | wxLEFT | wxRIGHT, BORDER_W); + topSizer->Add(m_optgroup->sizer , 1, wxEXPAND | wxLEFT | wxTOP | wxRIGHT, BORDER_W); + topSizer->Add(btns , 0, wxEXPAND | wxALL, BORDER_W); + + SetSizer(topSizer); + topSizer->SetSizeHints(this); + + if (new_printer) { + m_printer_name->SetFocus(); + m_printer_name->SelectAll(); + } +} + +PhysicalPrinterDialog::~PhysicalPrinterDialog() +{ + for (PresetForPrinter* preset : m_presets) { + delete preset; + preset = nullptr; + } +} + +void PhysicalPrinterDialog::build_printhost_settings(ConfigOptionsGroup* m_optgroup) +{ + m_optgroup->m_on_change = [this](t_config_option_key opt_key, boost::any value) { + if (opt_key == "authorization_type") + this->update(); + }; + + m_optgroup->append_single_option_line("host_type"); + + auto create_sizer_with_btn = [this](wxWindow* parent, ScalableButton** btn, const std::string& icon_name, const wxString& label) { + *btn = new ScalableButton(parent, wxID_ANY, icon_name, label, wxDefaultSize, wxDefaultPosition, wxBU_LEFT | wxBU_EXACTFIT); + (*btn)->SetFont(wxGetApp().normal_font()); + + auto sizer = new wxBoxSizer(wxHORIZONTAL); + sizer->Add(*btn); + return sizer; + }; + + auto printhost_browse = [=](wxWindow* parent) + { + auto sizer = create_sizer_with_btn(parent, &m_printhost_browse_btn, "browse", _L("Browse") + " " + dots); + m_printhost_browse_btn->Bind(wxEVT_BUTTON, [=](wxCommandEvent& e) { + BonjourDialog dialog(this, Preset::printer_technology(m_printer.config)); + if (dialog.show_and_lookup()) { + m_optgroup->set_value("print_host", std::move(dialog.get_selected()), true); + m_optgroup->get_field("print_host")->field_changed(); + } + }); + + return sizer; + }; + + auto print_host_test = [=](wxWindow* parent) { + auto sizer = create_sizer_with_btn(parent, &m_printhost_test_btn, "test", _L("Test")); + + m_printhost_test_btn->Bind(wxEVT_BUTTON, [this](wxCommandEvent& e) { + std::unique_ptr<PrintHost> host(PrintHost::get_print_host(m_config)); + if (!host) { + const wxString text = _L("Could not get a valid Printer Host reference"); + show_error(this, text); + return; + } + wxString msg; + if (host->test(msg)) { + show_info(this, host->get_test_ok_msg(), _L("Success!")); + } + else { + show_error(this, host->get_test_failed_msg(msg)); + } + }); + + return sizer; + }; + + // Set a wider width for a better alignment + Option option = m_optgroup->get_option("print_host"); + option.opt.width = Field::def_width_wider(); + Line host_line = m_optgroup->create_single_option_line(option); + host_line.append_widget(printhost_browse); + host_line.append_widget(print_host_test); + m_optgroup->append_line(host_line); + + m_optgroup->append_single_option_line("authorization_type"); + + option = m_optgroup->get_option("printhost_apikey"); + option.opt.width = Field::def_width_wider(); + m_optgroup->append_single_option_line(option); + + const auto ca_file_hint = _u8L("HTTPS CA file is optional. It is only needed if you use HTTPS with a self-signed certificate."); + + if (Http::ca_file_supported()) { + option = m_optgroup->get_option("printhost_cafile"); + option.opt.width = Field::def_width_wider(); + Line cafile_line = m_optgroup->create_single_option_line(option); + + auto printhost_cafile_browse = [=](wxWindow* parent) { + auto sizer = create_sizer_with_btn(parent, &m_printhost_cafile_browse_btn, "browse", _L("Browse") + " " + dots); + m_printhost_cafile_browse_btn->Bind(wxEVT_BUTTON, [this, m_optgroup](wxCommandEvent e) { + static const auto filemasks = _L("Certificate files (*.crt, *.pem)|*.crt;*.pem|All files|*.*"); + wxFileDialog openFileDialog(this, _L("Open CA certificate file"), "", "", filemasks, wxFD_OPEN | wxFD_FILE_MUST_EXIST); + if (openFileDialog.ShowModal() != wxID_CANCEL) { + m_optgroup->set_value("printhost_cafile", std::move(openFileDialog.GetPath()), true); + m_optgroup->get_field("printhost_cafile")->field_changed(); + } + }); + + return sizer; + }; + + cafile_line.append_widget(printhost_cafile_browse); + m_optgroup->append_line(cafile_line); + + Line cafile_hint{ "", "" }; + cafile_hint.full_width = 1; + cafile_hint.widget = [this, ca_file_hint](wxWindow* parent) { + auto txt = new wxStaticText(parent, wxID_ANY, ca_file_hint); + auto sizer = new wxBoxSizer(wxHORIZONTAL); + sizer->Add(txt); + return sizer; + }; + m_optgroup->append_line(cafile_hint); + } + else { + Line line{ "", "" }; + line.full_width = 1; + + line.widget = [ca_file_hint](wxWindow* parent) { + std::string info = _u8L("HTTPS CA File") + ":\n\t" + + (boost::format(_u8L("On this system, %s uses HTTPS certificates from the system Certificate Store or Keychain.")) % SLIC3R_APP_NAME).str() + + "\n\t" + _u8L("To use a custom CA file, please import your CA file into Certificate Store / Keychain."); + + //auto txt = new wxStaticText(parent, wxID_ANY, from_u8((boost::format("%1%\n\n\t%2%") % info % ca_file_hint).str())); + auto txt = new wxStaticText(parent, wxID_ANY, from_u8((boost::format("%1%\n\t%2%") % info % ca_file_hint).str())); + txt->SetFont(wxGetApp().normal_font()); + auto sizer = new wxBoxSizer(wxHORIZONTAL); + sizer->Add(txt, 1, wxEXPAND); + return sizer; + }; + + m_optgroup->append_line(line); + } + + for (const std::string& opt_key : std::vector<std::string>{ "login", "password" }) { + option = m_optgroup->get_option(opt_key); + option.opt.width = Field::def_width_wider(); + m_optgroup->append_single_option_line(option); + } + + update(); +} + +void PhysicalPrinterDialog::update() +{ + m_optgroup->reload_config(); + + const PrinterTechnology tech = Preset::printer_technology(*m_config); + // Only offer the host type selection for FFF, for SLA it's always the SL1 printer (at the moment) + if (tech == ptFFF) { + m_optgroup->show_field("host_type"); + m_optgroup->hide_field("authorization_type"); + for (const std::string& opt_key : std::vector<std::string>{ "login", "password" }) + m_optgroup->hide_field(opt_key); + } + else { + m_optgroup->set_value("host_type", int(PrintHostType::htOctoPrint), false); + m_optgroup->hide_field("host_type"); + + m_optgroup->show_field("authorization_type"); + + AuthorizationType auth_type = m_config->option<ConfigOptionEnum<AuthorizationType>>("authorization_type")->value; + m_optgroup->show_field("printhost_apikey", auth_type == AuthorizationType::atKeyPassword); + + for (const std::string& opt_key : std::vector<std::string>{ "login", "password" }) + m_optgroup->show_field(opt_key, auth_type == AuthorizationType::atUserPassword); + } + + this->Layout(); +} + + +wxString PhysicalPrinterDialog::get_printer_name() +{ + return m_printer_name->GetValue(); +} + +void PhysicalPrinterDialog::update_full_printer_names() +{ + for (PresetForPrinter* preset : m_presets) + preset->update_full_printer_name(); + + this->Layout(); +} + +void PhysicalPrinterDialog::set_printer_technology(PrinterTechnology pt) +{ + m_config->set_key_value("printer_technology", new ConfigOptionEnum<PrinterTechnology>(pt)); + update(); +} + +PrinterTechnology PhysicalPrinterDialog::get_printer_technology() +{ + return m_printer.printer_technology(); +} + +void PhysicalPrinterDialog::on_dpi_changed(const wxRect& suggested_rect) +{ + const int& em = em_unit(); + + m_printhost_browse_btn->msw_rescale(); + m_printhost_test_btn->msw_rescale(); + if (m_printhost_cafile_browse_btn) + m_printhost_cafile_browse_btn->msw_rescale(); + + m_optgroup->msw_rescale(); + + msw_buttons_rescale(this, em, { wxID_OK, wxID_CANCEL }); + + for (PresetForPrinter* preset : m_presets) + preset->msw_rescale(); + + const wxSize& size = wxSize(45 * em, 35 * em); + SetMinSize(size); + + Fit(); + Refresh(); +} + +void PhysicalPrinterDialog::OnOK(wxEvent& event) +{ + wxString printer_name = m_printer_name->GetValue(); + if (printer_name.IsEmpty()) { + warning_catcher(this, _L("The supplied name is empty. It can't be saved.")); + return; + } + if (printer_name == m_default_name) { + warning_catcher(this, _L("You should to change a name of your printer device. It can't be saved.")); + return; + } + + PhysicalPrinterCollection& printers = wxGetApp().preset_bundle->physical_printers; + const PhysicalPrinter* existing = printers.find_printer(into_u8(printer_name)); + if (existing && into_u8(printer_name) != printers.get_selected_printer_name()) + { + wxString msg_text = from_u8((boost::format(_u8L("Printer with name \"%1%\" already exists.")) % printer_name).str()); + msg_text += "\n" + _L("Replace?"); + wxMessageDialog dialog(nullptr, msg_text, _L("Warning"), wxICON_WARNING | wxYES | wxNO); + + if (dialog.ShowModal() == wxID_NO) + return; + } + + std::set<std::string> repeat_presets; + m_printer.reset_presets(); + for (PresetForPrinter* preset : m_presets) { + if (!m_printer.add_preset(preset->get_preset_name())) + repeat_presets.emplace(preset->get_preset_name()); + } + + if (!repeat_presets.empty()) + { + wxString repeatable_presets = "\n"; + for (const std::string& preset_name : repeat_presets) + repeatable_presets += " " + from_u8(preset_name) + "\n"; + repeatable_presets += "\n"; + + wxString msg_text = from_u8((boost::format(_u8L("Next printer preset(s) is(are) duplicated:%1%" + "Should I add it(they) just once for the printer \"%2%\" and close the Editing Dialog?")) % repeatable_presets % printer_name).str()); + wxMessageDialog dialog(nullptr, msg_text, _L("Warning"), wxICON_WARNING | wxYES | wxNO); + if (dialog.ShowModal() == wxID_NO) + return; + } + + std::string renamed_from; + // temporary save previous printer name if it was edited + if (m_printer.name != into_u8(m_default_name) && + m_printer.name != into_u8(printer_name)) + renamed_from = m_printer.name; + + //update printer name, if it was changed + m_printer.set_name(into_u8(printer_name)); + + // save new physical printer + printers.save_printer(m_printer, renamed_from); + + if (m_printer.preset_names.find(printers.get_selected_printer_preset_name()) == m_printer.preset_names.end()) { + // select first preset for this printer + printers.select_printer(m_printer); + // refresh preset list on Printer Settings Tab + wxGetApp().get_tab(Preset::TYPE_PRINTER)->select_preset(printers.get_selected_printer_preset_name()); + } + else + wxGetApp().get_tab(Preset::TYPE_PRINTER)->update_preset_choice(); + + event.Skip(); +} + +void PhysicalPrinterDialog::AddPreset(wxEvent& event) +{ + m_presets.emplace_back(new PresetForPrinter(this)); + // enable DELETE button for the first preset, if was disabled + m_presets.front()->AllowDelete(); + + m_presets_sizer->Add(m_presets.back()->sizer(), 1, wxEXPAND | wxTOP, BORDER_W); + update_full_printer_names(); + + this->Fit(); +} + +void PhysicalPrinterDialog::DeletePreset(PresetForPrinter* preset_for_printer) +{ + if (m_presets.size() == 1) { + wxString msg_text = _L("It's not possible to delete last related preset for the printer."); + wxMessageDialog dialog(nullptr, msg_text, _L("Infornation"), wxICON_INFORMATION | wxOK); + dialog.ShowModal(); + return; + } + + assert(preset_for_printer); + auto it = std::find(m_presets.begin(), m_presets.end(), preset_for_printer); + if (it == m_presets.end()) + return; + + const int remove_id = it - m_presets.begin(); + m_presets_sizer->Remove(remove_id); + delete preset_for_printer; + m_presets.erase(it); + + if (m_presets.size() == 1) + m_presets.front()->SuppressDelete(); + + this->Layout(); + this->Fit(); +} + + +}} // namespace Slic3r::GUI diff --git a/src/slic3r/GUI/PhysicalPrinterDialog.hpp b/src/slic3r/GUI/PhysicalPrinterDialog.hpp new file mode 100644 index 000000000..3d0cf2d9f --- /dev/null +++ b/src/slic3r/GUI/PhysicalPrinterDialog.hpp @@ -0,0 +1,105 @@ +#ifndef slic3r_PhysicalPrinterDialog_hpp_ +#define slic3r_PhysicalPrinterDialog_hpp_ + +#include <vector> + +#include <wx/gdicmn.h> + +#include "libslic3r/Preset.hpp" +#include "GUI_Utils.hpp" + +class wxString; +class wxTextCtrl; +class wxStaticText; +class ScalableButton; +class wxBoxSizer; + +namespace Slic3r { + +namespace GUI { + +class PresetComboBox; + +//------------------------------------------ +// PresetForPrinter +//------------------------------------------ +//static std::string g_info_string = " (modified)"; +class PhysicalPrinterDialog; +class PresetForPrinter +{ + PhysicalPrinterDialog* m_parent { nullptr }; + + PresetComboBox* m_presets_list { nullptr }; + ScalableButton* m_delete_preset_btn { nullptr }; + wxStaticText* m_info_line { nullptr }; + wxStaticText* m_full_printer_name { nullptr }; + + wxBoxSizer* m_sizer { nullptr }; + + void DeletePreset(wxEvent& event); + +public: + PresetForPrinter(PhysicalPrinterDialog* parent, const std::string& preset_name = ""); + ~PresetForPrinter(); + + wxBoxSizer* sizer() { return m_sizer; } + void update_full_printer_name(); + std::string get_preset_name(); + void SuppressDelete(); + void AllowDelete(); + + void msw_rescale(); + void on_sys_color_changed() {}; +}; + + +//------------------------------------------ +// PhysicalPrinterDialog +//------------------------------------------ + +class ConfigOptionsGroup; +class PhysicalPrinterDialog : public DPIDialog +{ + PhysicalPrinter m_printer; + wxString m_default_name; + DynamicPrintConfig* m_config { nullptr }; + + wxTextCtrl* m_printer_name { nullptr }; + std::vector<PresetForPrinter*> m_presets; + + ConfigOptionsGroup* m_optgroup { nullptr }; + + ScalableButton* m_add_preset_btn {nullptr}; + ScalableButton* m_printhost_browse_btn {nullptr}; + ScalableButton* m_printhost_test_btn {nullptr}; + ScalableButton* m_printhost_cafile_browse_btn {nullptr}; + + wxBoxSizer* m_presets_sizer {nullptr}; + + void build_printhost_settings(ConfigOptionsGroup* optgroup); + void OnOK(wxEvent& event); + void AddPreset(wxEvent& event); + +public: + PhysicalPrinterDialog(wxString printer_name); + ~PhysicalPrinterDialog(); + + void update(); + wxString get_printer_name(); + void update_full_printer_names(); + PhysicalPrinter* get_printer() {return &m_printer; } + void set_printer_technology(PrinterTechnology pt); + PrinterTechnology get_printer_technology(); + + void DeletePreset(PresetForPrinter* preset_for_printer); + +protected: + void on_dpi_changed(const wxRect& suggested_rect) override; + void on_sys_color_changed() override {}; +}; + + +} // namespace GUI +} // namespace Slic3r + +#endif diff --git a/src/slic3r/GUI/Plater.cpp b/src/slic3r/GUI/Plater.cpp index a03d31bff..0b9f88451 100644 --- a/src/slic3r/GUI/Plater.cpp +++ b/src/slic3r/GUI/Plater.cpp @@ -24,7 +24,6 @@ #include <wx/dnd.h> #include <wx/progdlg.h> #include <wx/wupdlock.h> -#include <wx/colordlg.h> #include <wx/numdlg.h> #include <wx/debug.h> #include <wx/busyinfo.h> @@ -48,6 +47,7 @@ #include "libslic3r/PrintConfig.hpp" #include "libslic3r/SLAPrint.hpp" #include "libslic3r/Utils.hpp" +#include "libslic3r/PresetBundle.hpp" #include "GUI.hpp" #include "GUI_App.hpp" @@ -70,7 +70,6 @@ #include "Jobs/ArrangeJob.hpp" #include "Jobs/RotoptimizeJob.hpp" #include "Jobs/SLAImportJob.hpp" -#include "PresetBundle.hpp" #include "BackgroundSlicingProcess.hpp" #include "ProgressStatusBar.hpp" #include "PrintHostDialogs.hpp" @@ -83,6 +82,7 @@ #include "RemovableDriveManager.hpp" #include "InstanceCheck.hpp" #include "NotificationManager.hpp" +#include "PresetComboBoxes.hpp" #ifdef __APPLE__ #include "Gizmos/GLGizmosManager.hpp" @@ -260,153 +260,6 @@ void SlicedInfo::SetTextAndShow(SlicedInfoIdx idx, const wxString& text, const w info_vec[idx].second->Show(show); } -PresetComboBox::PresetComboBox(wxWindow *parent, Preset::Type preset_type) : -PresetBitmapComboBox(parent, wxSize(15 * wxGetApp().em_unit(), -1)), - preset_type(preset_type), - last_selected(wxNOT_FOUND), - m_em_unit(wxGetApp().em_unit()) -{ - SetFont(wxGetApp().normal_font()); -#ifdef _WIN32 - // Workaround for ignoring CBN_EDITCHANGE events, which are processed after the content of the combo box changes, so that - // the index of the item inside CBN_EDITCHANGE may no more be valid. - EnableTextChangedEvents(false); -#endif /* _WIN32 */ - Bind(wxEVT_COMBOBOX, [this](wxCommandEvent &evt) { - auto selected_item = evt.GetSelection(); - - auto marker = reinterpret_cast<Marker>(this->GetClientData(selected_item)); - if (marker >= LABEL_ITEM_MARKER && marker < LABEL_ITEM_MAX) { - this->SetSelection(this->last_selected); - evt.StopPropagation(); - if (marker >= LABEL_ITEM_WIZARD_PRINTERS) { - ConfigWizard::StartPage sp = ConfigWizard::SP_WELCOME; - switch (marker) { - case LABEL_ITEM_WIZARD_PRINTERS: sp = ConfigWizard::SP_PRINTERS; break; - case LABEL_ITEM_WIZARD_FILAMENTS: sp = ConfigWizard::SP_FILAMENTS; break; - case LABEL_ITEM_WIZARD_MATERIALS: sp = ConfigWizard::SP_MATERIALS; break; - } - wxTheApp->CallAfter([sp]() { wxGetApp().run_wizard(ConfigWizard::RR_USER, sp); }); - } - } else if ( this->last_selected != selected_item || - wxGetApp().get_tab(this->preset_type)->get_presets()->current_is_dirty() ) { - this->last_selected = selected_item; - evt.SetInt(this->preset_type); - evt.Skip(); - } else { - evt.StopPropagation(); - } - }); - - if (preset_type == Slic3r::Preset::TYPE_FILAMENT) - { - Bind(wxEVT_LEFT_DOWN, [this](wxMouseEvent &event) { - PresetBundle* preset_bundle = wxGetApp().preset_bundle; - const Preset* selected_preset = preset_bundle->filaments.find_preset(preset_bundle->filament_presets[extruder_idx]); - // Wide icons are shown if the currently selected preset is not compatible with the current printer, - // and red flag is drown in front of the selected preset. - bool wide_icons = selected_preset != nullptr && !selected_preset->is_compatible; - float scale = m_em_unit*0.1f; - - int shifl_Left = wide_icons ? int(scale * 16 + 0.5) : 0; -#if defined(wxBITMAPCOMBOBOX_OWNERDRAWN_BASED) - shifl_Left += int(scale * 4 + 0.5f); // IMAGE_SPACING_RIGHT = 4 for wxBitmapComboBox -> Space left of image -#endif - int icon_right_pos = shifl_Left + int(scale * (24+4) + 0.5); - int mouse_pos = event.GetLogicalPosition(wxClientDC(this)).x; - if (mouse_pos < shifl_Left || mouse_pos > icon_right_pos ) { - // Let the combo box process the mouse click. - event.Skip(); - return; - } - - // Swallow the mouse click and open the color picker. - - // get current color - DynamicPrintConfig* cfg = wxGetApp().get_tab(Preset::TYPE_PRINTER)->get_config(); - auto colors = static_cast<ConfigOptionStrings*>(cfg->option("extruder_colour")->clone()); - wxColour clr(colors->values[extruder_idx]); - if (!clr.IsOk()) - clr = wxColour(0,0,0); // Don't set alfa to transparence - - auto data = new wxColourData(); - data->SetChooseFull(1); - data->SetColour(clr); - - wxColourDialog dialog(this, data); - dialog.CenterOnParent(); - if (dialog.ShowModal() == wxID_OK) - { - colors->values[extruder_idx] = dialog.GetColourData().GetColour().GetAsString(wxC2S_HTML_SYNTAX).ToStdString(); - - DynamicPrintConfig cfg_new = *cfg; - cfg_new.set_key_value("extruder_colour", colors); - - wxGetApp().get_tab(Preset::TYPE_PRINTER)->load_config(cfg_new); - preset_bundle->update_plater_filament_ui(extruder_idx, this); - wxGetApp().plater()->on_config_change(cfg_new); - } - }); - } - - edit_btn = new ScalableButton(parent, wxID_ANY, "cog"); - edit_btn->SetToolTip(_L("Click to edit preset")); - - edit_btn->Bind(wxEVT_BUTTON, ([preset_type, this](wxCommandEvent) - { - Tab* tab = wxGetApp().get_tab(preset_type); - if (!tab) - return; - - int page_id = wxGetApp().tab_panel()->FindPage(tab); - if (page_id == wxNOT_FOUND) - return; - - wxGetApp().tab_panel()->SetSelection(page_id); - - // Switch to Settings NotePad - wxGetApp().mainframe->select_tab(); - - /* In a case of a multi-material printing, for editing another Filament Preset - * it's needed to select this preset for the "Filament settings" Tab - */ - if (preset_type == Preset::TYPE_FILAMENT && wxGetApp().extruders_edited_cnt() > 1) - { - const std::string& selected_preset = GetString(GetSelection()).ToUTF8().data(); - - // Call select_preset() only if there is new preset and not just modified - if ( !boost::algorithm::ends_with(selected_preset, Preset::suffix_modified()) ) - { - const std::string& preset_name = wxGetApp().preset_bundle->filaments.get_preset_name_by_alias(selected_preset); - tab->select_preset(/*selected_preset*/preset_name); - } - } - })); -} - -PresetComboBox::~PresetComboBox() -{ - if (edit_btn) - edit_btn->Destroy(); -} - - -void PresetComboBox::set_label_marker(int item, LabelItemType label_item_type) -{ - this->SetClientData(item, (void*)label_item_type); -} - -void PresetComboBox::check_selection(int selection) -{ - this->last_selected = selection; -} - -void PresetComboBox::msw_rescale() -{ - m_em_unit = wxGetApp().em_unit(); - edit_btn->msw_rescale(); -} - // Frequently changed parameters class FreqChangedParams : public OG_Settings @@ -704,12 +557,12 @@ struct Sidebar::priv ModeSizer *mode_sizer; wxFlexGridSizer *sizer_presets; - PresetComboBox *combo_print; - std::vector<PresetComboBox*> combos_filament; + PlaterPresetComboBox *combo_print; + std::vector<PlaterPresetComboBox*> combos_filament; wxBoxSizer *sizer_filaments; - PresetComboBox *combo_sla_print; - PresetComboBox *combo_sla_material; - PresetComboBox *combo_printer; + PlaterPresetComboBox *combo_sla_print; + PlaterPresetComboBox *combo_sla_material; + PlaterPresetComboBox *combo_printer; wxBoxSizer *sizer_params; FreqChangedParams *frequently_changed_parameters{ nullptr }; @@ -808,10 +661,10 @@ Sidebar::Sidebar(Plater *parent) p->sizer_filaments = new wxBoxSizer(wxVERTICAL); - auto init_combo = [this](PresetComboBox **combo, wxString label, Preset::Type preset_type, bool filament) { + auto init_combo = [this](PlaterPresetComboBox **combo, wxString label, Preset::Type preset_type, bool filament) { auto *text = new wxStaticText(p->presets_panel, wxID_ANY, label + " :"); text->SetFont(wxGetApp().small_font()); - *combo = new PresetComboBox(p->presets_panel, preset_type); + *combo = new PlaterPresetComboBox(p->presets_panel, preset_type); auto combo_and_btn_sizer = new wxBoxSizer(wxHORIZONTAL); combo_and_btn_sizer->Add(*combo, 1, wxEXPAND); @@ -948,8 +801,8 @@ Sidebar::Sidebar(Plater *parent) Sidebar::~Sidebar() {} -void Sidebar::init_filament_combo(PresetComboBox **combo, const int extr_idx) { - *combo = new PresetComboBox(p->presets_panel, Slic3r::Preset::TYPE_FILAMENT); +void Sidebar::init_filament_combo(PlaterPresetComboBox **combo, const int extr_idx) { + *combo = new PlaterPresetComboBox(p->presets_panel, Slic3r::Preset::TYPE_FILAMENT); // # copy icons from first choice // $choice->SetItemBitmap($_, $choices->[0]->GetItemBitmap($_)) for 0..$#presets; @@ -984,18 +837,18 @@ void Sidebar::update_all_preset_comboboxes() // Update the print choosers to only contain the compatible presets, update the dirty flags. if (print_tech == ptFFF) - preset_bundle.prints.update_plater_ui(p->combo_print); + p->combo_print->update(); else { - preset_bundle.sla_prints.update_plater_ui(p->combo_sla_print); - preset_bundle.sla_materials.update_plater_ui(p->combo_sla_material); + p->combo_sla_print->update(); + p->combo_sla_material->update(); } // Update the printer choosers, update the dirty flags. - preset_bundle.printers.update_plater_ui(p->combo_printer); + p->combo_printer->update(); // Update the filament choosers to only contain the compatible presets, update the color preview, // update the dirty flags. if (print_tech == ptFFF) { - for (size_t i = 0; i < p->combos_filament.size(); ++i) - preset_bundle.update_plater_filament_ui(i, p->combos_filament[i]); + for (PlaterPresetComboBox* cb : p->combos_filament) + cb->update(); } } @@ -1017,23 +870,22 @@ void Sidebar::update_presets(Preset::Type preset_type) preset_bundle.set_filament_preset(0, name); } - for (size_t i = 0; i < filament_cnt; i++) { - preset_bundle.update_plater_filament_ui(i, p->combos_filament[i]); - } + for (size_t i = 0; i < filament_cnt; i++) + p->combos_filament[i]->update(); break; } case Preset::TYPE_PRINT: - preset_bundle.prints.update_plater_ui(p->combo_print); + p->combo_print->update(); break; case Preset::TYPE_SLA_PRINT: - preset_bundle.sla_prints.update_plater_ui(p->combo_sla_print); + p->combo_sla_print->update(); break; case Preset::TYPE_SLA_MATERIAL: - preset_bundle.sla_materials.update_plater_ui(p->combo_sla_material); + p->combo_sla_material->update(); break; case Preset::TYPE_PRINTER: @@ -1069,18 +921,14 @@ void Sidebar::msw_rescale() p->mode_sizer->msw_rescale(); - // Rescale preset comboboxes in respect to the current em_unit ... - for (PresetComboBox* combo : std::vector<PresetComboBox*> { p->combo_print, + for (PlaterPresetComboBox* combo : std::vector<PlaterPresetComboBox*> { p->combo_print, p->combo_sla_print, p->combo_sla_material, p->combo_printer } ) combo->msw_rescale(); - for (PresetComboBox* combo : p->combos_filament) + for (PlaterPresetComboBox* combo : p->combos_filament) combo->msw_rescale(); - // ... then refill them and set min size to correct layout of the sidebar - update_all_preset_comboboxes(); - p->frequently_changed_parameters->msw_rescale(); p->object_list->msw_rescale(); p->object_manipulation->msw_rescale(); @@ -1101,22 +949,16 @@ void Sidebar::msw_rescale() void Sidebar::sys_color_changed() { - // Update preset comboboxes in respect to the system color ... - // combo->msw_rescale() updates icon on button, so use it - for (PresetComboBox* combo : std::vector<PresetComboBox*>{ p->combo_print, + for (PlaterPresetComboBox* combo : std::vector<PlaterPresetComboBox*>{ p->combo_print, p->combo_sla_print, p->combo_sla_material, p->combo_printer }) combo->msw_rescale(); - for (PresetComboBox* combo : p->combos_filament) + for (PlaterPresetComboBox* combo : p->combos_filament) combo->msw_rescale(); - // ... then refill them and set min size to correct layout of the sidebar - update_all_preset_comboboxes(); - p->object_list->sys_color_changed(); p->object_manipulation->sys_color_changed(); -// p->object_settings->msw_rescale(); p->object_layers->sys_color_changed(); // btn...->msw_rescale() updates icon on button, so use it @@ -1479,7 +1321,7 @@ void Sidebar::update_ui_from_settings() update_sliced_info_sizer(); } -std::vector<PresetComboBox*>& Sidebar::combos_filament() +std::vector<PlaterPresetComboBox*>& Sidebar::combos_filament() { return p->combos_filament; } @@ -2339,6 +2181,8 @@ std::vector<size_t> Plater::priv::load_files(const std::vector<fs::path>& input_ if (!config.empty()) { Preset::normalize(config); wxGetApp().preset_bundle->load_config_model(filename.string(), std::move(config)); + if (printer_technology == ptFFF) + CustomGCode::update_custom_gcode_per_print_z_from_config(model.custom_gcode_per_print_z, &wxGetApp().preset_bundle->project_config); wxGetApp().load_current_presets(); is_project_file = true; } @@ -3428,7 +3272,7 @@ void Plater::priv::set_current_panel(wxPanel* panel) void Plater::priv::on_select_preset(wxCommandEvent &evt) { auto preset_type = static_cast<Preset::Type>(evt.GetInt()); - auto *combo = static_cast<PresetComboBox*>(evt.GetEventObject()); + auto *combo = static_cast<PlaterPresetComboBox*>(evt.GetEventObject()); // see https://github.com/prusa3d/PrusaSlicer/issues/3889 // Under OSX: in case of use of a same names written in different case (like "ENDER" and "Ender"), @@ -3447,19 +3291,27 @@ void Plater::priv::on_select_preset(wxCommandEvent &evt) //! instead of //! combo->GetStringSelection().ToUTF8().data()); - const std::string preset_name = wxGetApp().preset_bundle->get_preset_name_by_alias(preset_type, + std::string preset_name = wxGetApp().preset_bundle->get_preset_name_by_alias(preset_type, Preset::remove_suffix_modified(combo->GetString(selection).ToUTF8().data())); if (preset_type == Preset::TYPE_FILAMENT) { wxGetApp().preset_bundle->set_filament_preset(idx, preset_name); } + bool select_preset = !combo->selection_is_changed_according_to_physical_printers(); // TODO: ? if (preset_type == Preset::TYPE_FILAMENT && sidebar->is_multifilament()) { // Only update the plater UI for the 2nd and other filaments. - wxGetApp().preset_bundle->update_plater_filament_ui(idx, combo); + combo->update(); } - else { + else if (select_preset) { + if (preset_type == Preset::TYPE_PRINTER) { + PhysicalPrinterCollection& physical_printers = wxGetApp().preset_bundle->physical_printers; + if(combo->is_selected_physical_printer()) + preset_name = physical_printers.get_selected_printer_preset_name(); + else + physical_printers.unselect_printer(); + } wxWindowUpdateLocker noUpdates(sidebar->presets_panel()); wxGetApp().get_tab(preset_type)->select_preset(preset_name); } @@ -4321,7 +4173,12 @@ void Plater::priv::show_action_buttons(const bool ready_to_slice) const this->ready_to_slice = ready_to_slice; wxWindowUpdateLocker noUpdater(sidebar); - const auto prin_host_opt = config->option<ConfigOptionString>("print_host"); + + DynamicPrintConfig* selected_printer_config = wxGetApp().preset_bundle->physical_printers.get_selected_printer_config(); + if (!selected_printer_config) + selected_printer_config = config; + + const auto prin_host_opt = selected_printer_config->option<ConfigOptionString>("print_host"); const bool send_gcode_shown = prin_host_opt != nullptr && !prin_host_opt->value.empty(); // when a background processing is ON, export_btn and/or send_btn are showing @@ -5301,7 +5158,9 @@ void Plater::send_gcode() { if (p->model.objects.empty()) { return; } - PrintHostJob upload_job(p->config); + // if physical_printer is selected, send gcode for this printer + DynamicPrintConfig* physical_printer_config = wxGetApp().preset_bundle->physical_printers.get_selected_printer_config(); + PrintHostJob upload_job(physical_printer_config ? physical_printer_config : p->config); if (upload_job.empty()) { return; } // Obtain default output path @@ -5412,12 +5271,12 @@ void Plater::on_extruders_change(size_t num_extruders) size_t i = choices.size(); while ( i < num_extruders ) { - PresetComboBox* choice/*{ nullptr }*/; + PlaterPresetComboBox* choice/*{ nullptr }*/; sidebar().init_filament_combo(&choice, i); choices.push_back(choice); // initialize selection - wxGetApp().preset_bundle->update_plater_filament_ui(i, choice); + choice->update(); ++i; } diff --git a/src/slic3r/GUI/Plater.hpp b/src/slic3r/GUI/Plater.hpp index 8e8723e1e..9c1270bce 100644 --- a/src/slic3r/GUI/Plater.hpp +++ b/src/slic3r/GUI/Plater.hpp @@ -6,14 +6,12 @@ #include <boost/filesystem/path.hpp> #include <wx/panel.h> -#include <wx/bmpcbox.h> -#include "Preset.hpp" #include "Selection.hpp" +#include "libslic3r/Preset.hpp" #include "libslic3r/BoundingBox.hpp" #include "Jobs/Job.hpp" -#include "wxExtensions.hpp" #include "Search.hpp" class wxButton; @@ -51,46 +49,13 @@ class NotificationManager; struct Camera; class Bed3D; class GLToolbar; +class PlaterPresetComboBox; using t_optgroups = std::vector <std::shared_ptr<ConfigOptionsGroup>>; class Plater; enum class ActionButtonType : int; -class PresetComboBox : public PresetBitmapComboBox -{ -public: - PresetComboBox(wxWindow *parent, Preset::Type preset_type); - ~PresetComboBox(); - - ScalableButton* edit_btn { nullptr }; - - enum LabelItemType { - LABEL_ITEM_MARKER = 0xffffff01, - LABEL_ITEM_WIZARD_PRINTERS, - LABEL_ITEM_WIZARD_FILAMENTS, - LABEL_ITEM_WIZARD_MATERIALS, - - LABEL_ITEM_MAX, - }; - - void set_label_marker(int item, LabelItemType label_item_type = LABEL_ITEM_MARKER); - void set_extruder_idx(const int extr_idx) { extruder_idx = extr_idx; } - int get_extruder_idx() const { return extruder_idx; } - int em_unit() const { return m_em_unit; } - void check_selection(int selection); - - void msw_rescale(); - -private: - typedef std::size_t Marker; - - Preset::Type preset_type; - int last_selected; - int extruder_idx = -1; - int m_em_unit; -}; - class Sidebar : public wxPanel { ConfigOptionMode m_mode; @@ -102,7 +67,7 @@ public: Sidebar &operator=(const Sidebar &) = delete; ~Sidebar(); - void init_filament_combo(PresetComboBox **combo, const int extr_idx); + void init_filament_combo(PlaterPresetComboBox **combo, const int extr_idx); void remove_unused_filament_combos(const size_t current_extruder_count); void update_all_preset_comboboxes(); void update_presets(Slic3r::Preset::Type preset_type); @@ -141,7 +106,7 @@ public: void update_searcher(); void update_ui_from_settings(); - std::vector<PresetComboBox*>& combos_filament(); + std::vector<PlaterPresetComboBox*>& combos_filament(); Search::OptionsSearcher& get_searcher(); std::string& get_search_line(); diff --git a/src/slic3r/GUI/Preferences.cpp b/src/slic3r/GUI/Preferences.cpp index 4b5808e16..242c3d851 100644 --- a/src/slic3r/GUI/Preferences.cpp +++ b/src/slic3r/GUI/Preferences.cpp @@ -1,8 +1,8 @@ #include "Preferences.hpp" -#include "AppConfig.hpp" #include "OptionsGroup.hpp" #include "GUI_App.hpp" #include "I18N.hpp" +#include "libslic3r/AppConfig.hpp" namespace Slic3r { namespace GUI { diff --git a/src/slic3r/GUI/Preset.cpp b/src/slic3r/GUI/Preset.cpp deleted file mode 100644 index 7cf3b13ac..000000000 --- a/src/slic3r/GUI/Preset.cpp +++ /dev/null @@ -1,1652 +0,0 @@ -#include <cassert> - -#include "Preset.hpp" -#include "AppConfig.hpp" -#include "BitmapCache.hpp" -#include "I18N.hpp" -#include "wxExtensions.hpp" - -#ifdef _MSC_VER - #define WIN32_LEAN_AND_MEAN - #define NOMINMAX - #include <Windows.h> -#endif /* _MSC_VER */ - -#include <algorithm> -#include <fstream> -#include <stdexcept> -#include <unordered_map> -#include <boost/format.hpp> -#include <boost/filesystem.hpp> -#include <boost/filesystem/fstream.hpp> -#include <boost/algorithm/string/predicate.hpp> - -#include <boost/nowide/cenv.hpp> -#include <boost/nowide/convert.hpp> -#include <boost/nowide/cstdio.hpp> -#include <boost/nowide/fstream.hpp> -#include <boost/property_tree/ini_parser.hpp> -#include <boost/property_tree/ptree.hpp> -#include <boost/locale.hpp> -#include <boost/log/trivial.hpp> - -#include <wx/image.h> -#include <wx/choice.h> -#include <wx/bmpcbox.h> -#include <wx/wupdlock.h> - -#include "libslic3r/libslic3r.h" -#include "libslic3r/Utils.hpp" -#include "libslic3r/PlaceholderParser.hpp" -#include "Plater.hpp" - -using boost::property_tree::ptree; - -namespace Slic3r { - -ConfigFileType guess_config_file_type(const ptree &tree) -{ - size_t app_config = 0; - size_t bundle = 0; - size_t config = 0; - for (const ptree::value_type &v : tree) { - if (v.second.empty()) { - if (v.first == "background_processing" || - v.first == "last_output_path" || - v.first == "no_controller" || - v.first == "no_defaults") - ++ app_config; - else if (v.first == "nozzle_diameter" || - v.first == "filament_diameter") - ++ config; - } else if (boost::algorithm::starts_with(v.first, "print:") || - boost::algorithm::starts_with(v.first, "filament:") || - boost::algorithm::starts_with(v.first, "printer:") || - v.first == "settings") - ++ bundle; - else if (v.first == "presets") { - ++ app_config; - ++ bundle; - } else if (v.first == "recent") { - for (auto &kvp : v.second) - if (kvp.first == "config_directory" || kvp.first == "skein_directory") - ++ app_config; - } - } - return (app_config > bundle && app_config > config) ? CONFIG_FILE_TYPE_APP_CONFIG : - (bundle > config) ? CONFIG_FILE_TYPE_CONFIG_BUNDLE : CONFIG_FILE_TYPE_CONFIG; -} - - -VendorProfile VendorProfile::from_ini(const boost::filesystem::path &path, bool load_all) -{ - ptree tree; - boost::filesystem::ifstream ifs(path); - boost::property_tree::read_ini(ifs, tree); - return VendorProfile::from_ini(tree, path, load_all); -} - -static const std::unordered_map<std::string, std::string> pre_family_model_map {{ - { "MK3", "MK3" }, - { "MK3MMU2", "MK3" }, - { "MK2.5", "MK2.5" }, - { "MK2.5MMU2", "MK2.5" }, - { "MK2S", "MK2" }, - { "MK2SMM", "MK2" }, - { "SL1", "SL1" }, -}}; - -VendorProfile VendorProfile::from_ini(const ptree &tree, const boost::filesystem::path &path, bool load_all) -{ - static const std::string printer_model_key = "printer_model:"; - static const std::string filaments_section = "default_filaments"; - static const std::string materials_section = "default_sla_materials"; - - const std::string id = path.stem().string(); - - if (! boost::filesystem::exists(path)) { - throw std::runtime_error((boost::format("Cannot load Vendor Config Bundle `%1%`: File not found: `%2%`.") % id % path).str()); - } - - VendorProfile res(id); - - // Helper to get compulsory fields - auto get_or_throw = [&](const ptree &tree, const std::string &key) -> ptree::const_assoc_iterator - { - auto res = tree.find(key); - if (res == tree.not_found()) { - throw std::runtime_error((boost::format("Vendor Config Bundle `%1%` is not valid: Missing secion or key: `%2%`.") % id % key).str()); - } - return res; - }; - - // Load the header - const auto &vendor_section = get_or_throw(tree, "vendor")->second; - res.name = get_or_throw(vendor_section, "name")->second.data(); - - auto config_version_str = get_or_throw(vendor_section, "config_version")->second.data(); - auto config_version = Semver::parse(config_version_str); - if (! config_version) { - throw std::runtime_error((boost::format("Vendor Config Bundle `%1%` is not valid: Cannot parse config_version: `%2%`.") % id % config_version_str).str()); - } else { - res.config_version = std::move(*config_version); - } - - // Load URLs - const auto config_update_url = vendor_section.find("config_update_url"); - if (config_update_url != vendor_section.not_found()) { - res.config_update_url = config_update_url->second.data(); - } - - const auto changelog_url = vendor_section.find("changelog_url"); - if (changelog_url != vendor_section.not_found()) { - res.changelog_url = changelog_url->second.data(); - } - - if (! load_all) { - return res; - } - - // Load printer models - for (auto §ion : tree) { - if (boost::starts_with(section.first, printer_model_key)) { - VendorProfile::PrinterModel model; - model.id = section.first.substr(printer_model_key.size()); - model.name = section.second.get<std::string>("name", model.id); - - const char *technology_fallback = boost::algorithm::starts_with(model.id, "SL") ? "SLA" : "FFF"; - - auto technology_field = section.second.get<std::string>("technology", technology_fallback); - if (! ConfigOptionEnum<PrinterTechnology>::from_string(technology_field, model.technology)) { - BOOST_LOG_TRIVIAL(error) << boost::format("Vendor bundle: `%1%`: Invalid printer technology field: `%2%`") % id % technology_field; - model.technology = ptFFF; - } - - model.family = section.second.get<std::string>("family", std::string()); - if (model.family.empty() && res.name == "Prusa Research") { - // If no family is specified, it can be inferred for known printers - const auto from_pre_map = pre_family_model_map.find(model.id); - if (from_pre_map != pre_family_model_map.end()) { model.family = from_pre_map->second; } - } -#if 0 - // Remove SLA printers from the initial alpha. - if (model.technology == ptSLA) - continue; -#endif - section.second.get<std::string>("variants", ""); - const auto variants_field = section.second.get<std::string>("variants", ""); - std::vector<std::string> variants; - if (Slic3r::unescape_strings_cstyle(variants_field, variants)) { - for (const std::string &variant_name : variants) { - if (model.variant(variant_name) == nullptr) - model.variants.emplace_back(VendorProfile::PrinterVariant(variant_name)); - } - } else { - BOOST_LOG_TRIVIAL(error) << boost::format("Vendor bundle: `%1%`: Malformed variants field: `%2%`") % id % variants_field; - } - auto default_materials_field = section.second.get<std::string>("default_materials", ""); - if (default_materials_field.empty()) - default_materials_field = section.second.get<std::string>("default_filaments", ""); - if (Slic3r::unescape_strings_cstyle(default_materials_field, model.default_materials)) { - Slic3r::sort_remove_duplicates(model.default_materials); - if (! model.default_materials.empty() && model.default_materials.front().empty()) - // An empty material was inserted into the list of default materials. Remove it. - model.default_materials.erase(model.default_materials.begin()); - } else { - BOOST_LOG_TRIVIAL(error) << boost::format("Vendor bundle: `%1%`: Malformed default_materials field: `%2%`") % id % default_materials_field; - } - model.bed_model = section.second.get<std::string>("bed_model", ""); - model.bed_texture = section.second.get<std::string>("bed_texture", ""); - if (! model.id.empty() && ! model.variants.empty()) - res.models.push_back(std::move(model)); - } - } - - // Load filaments and sla materials to be installed by default - const auto filaments = tree.find(filaments_section); - if (filaments != tree.not_found()) { - for (auto &pair : filaments->second) { - if (pair.second.data() == "1") { - res.default_filaments.insert(pair.first); - } - } - } - const auto materials = tree.find(materials_section); - if (materials != tree.not_found()) { - for (auto &pair : materials->second) { - if (pair.second.data() == "1") { - res.default_sla_materials.insert(pair.first); - } - } - } - - return res; -} - -std::vector<std::string> VendorProfile::families() const -{ - std::vector<std::string> res; - unsigned num_familiies = 0; - - for (auto &model : models) { - if (std::find(res.begin(), res.end(), model.family) == res.end()) { - res.push_back(model.family); - num_familiies++; - } - } - - return res; -} - -// Suffix to be added to a modified preset name in the combo box. -static std::string g_suffix_modified = " (modified)"; -const std::string& Preset::suffix_modified() -{ - return g_suffix_modified; -} - -void Preset::update_suffix_modified() -{ - g_suffix_modified = (" (" + _(L("modified")) + ")").ToUTF8().data(); -} -// Remove an optional "(modified)" suffix from a name. -// This converts a UI name to a unique preset identifier. -std::string Preset::remove_suffix_modified(const std::string &name) -{ - return boost::algorithm::ends_with(name, g_suffix_modified) ? - name.substr(0, name.size() - g_suffix_modified.size()) : - name; -} - -// Update new extruder fields at the printer profile. -void Preset::normalize(DynamicPrintConfig &config) -{ - auto *nozzle_diameter = dynamic_cast<const ConfigOptionFloats*>(config.option("nozzle_diameter")); - if (nozzle_diameter != nullptr) - // Loaded the FFF Printer settings. Verify, that all extruder dependent values have enough values. - config.set_num_extruders((unsigned int)nozzle_diameter->values.size()); - if (config.option("filament_diameter") != nullptr) { - // This config contains single or multiple filament presets. - // Ensure that the filament preset vector options contain the correct number of values. - size_t n = (nozzle_diameter == nullptr) ? 1 : nozzle_diameter->values.size(); - const auto &defaults = FullPrintConfig::defaults(); - for (const std::string &key : Preset::filament_options()) { - if (key == "compatible_prints" || key == "compatible_printers") - continue; - auto *opt = config.option(key, false); - /*assert(opt != nullptr); - assert(opt->is_vector());*/ - if (opt != nullptr && opt->is_vector()) - static_cast<ConfigOptionVectorBase*>(opt)->resize(n, defaults.option(key)); - } - // The following keys are mandatory for the UI, but they are not part of FullPrintConfig, therefore they are handled separately. - for (const std::string &key : { "filament_settings_id" }) { - auto *opt = config.option(key, false); - assert(opt == nullptr || opt->type() == coStrings); - if (opt != nullptr && opt->type() == coStrings) - static_cast<ConfigOptionStrings*>(opt)->values.resize(n, std::string()); - } - } -} - -std::string Preset::remove_invalid_keys(DynamicPrintConfig &config, const DynamicPrintConfig &default_config) -{ - std::string incorrect_keys; - for (const std::string &key : config.keys()) - if (! default_config.has(key)) { - if (incorrect_keys.empty()) - incorrect_keys = key; - else { - incorrect_keys += ", "; - incorrect_keys += key; - } - config.erase(key); - } - return incorrect_keys; -} - -void Preset::save() -{ - this->config.save(this->file); -} - -// Return a label of this preset, consisting of a name and a "(modified)" suffix, if this preset is dirty. -std::string Preset::label() const -{ - return this->name + (this->is_dirty ? g_suffix_modified : ""); -} - -bool is_compatible_with_print(const PresetWithVendorProfile &preset, const PresetWithVendorProfile &active_print, const PresetWithVendorProfile &active_printer) -{ - if (preset.vendor != nullptr && preset.vendor != active_printer.vendor) - // The current profile has a vendor assigned and it is different from the active print's vendor. - return false; - auto &condition = preset.preset.compatible_prints_condition(); - auto *compatible_prints = dynamic_cast<const ConfigOptionStrings*>(preset.preset.config.option("compatible_prints")); - bool has_compatible_prints = compatible_prints != nullptr && ! compatible_prints->values.empty(); - if (! has_compatible_prints && ! condition.empty()) { - try { - return PlaceholderParser::evaluate_boolean_expression(condition, active_print.preset.config); - } catch (const std::runtime_error &err) { - //FIXME in case of an error, return "compatible with everything". - printf("Preset::is_compatible_with_print - parsing error of compatible_prints_condition %s:\n%s\n", active_print.preset.name.c_str(), err.what()); - return true; - } - } - return preset.preset.is_default || active_print.preset.name.empty() || ! has_compatible_prints || - std::find(compatible_prints->values.begin(), compatible_prints->values.end(), active_print.preset.name) != - compatible_prints->values.end(); -} - -bool is_compatible_with_printer(const PresetWithVendorProfile &preset, const PresetWithVendorProfile &active_printer, const DynamicPrintConfig *extra_config) -{ - if (preset.vendor != nullptr && preset.vendor != active_printer.vendor) - // The current profile has a vendor assigned and it is different from the active print's vendor. - return false; - auto &condition = preset.preset.compatible_printers_condition(); - auto *compatible_printers = dynamic_cast<const ConfigOptionStrings*>(preset.preset.config.option("compatible_printers")); - bool has_compatible_printers = compatible_printers != nullptr && ! compatible_printers->values.empty(); - if (! has_compatible_printers && ! condition.empty()) { - try { - return PlaceholderParser::evaluate_boolean_expression(condition, active_printer.preset.config, extra_config); - } catch (const std::runtime_error &err) { - //FIXME in case of an error, return "compatible with everything". - printf("Preset::is_compatible_with_printer - parsing error of compatible_printers_condition %s:\n%s\n", active_printer.preset.name.c_str(), err.what()); - return true; - } - } - return preset.preset.is_default || active_printer.preset.name.empty() || ! has_compatible_printers || - std::find(compatible_printers->values.begin(), compatible_printers->values.end(), active_printer.preset.name) != - compatible_printers->values.end(); -} - -bool is_compatible_with_printer(const PresetWithVendorProfile &preset, const PresetWithVendorProfile &active_printer) -{ - DynamicPrintConfig config; - config.set_key_value("printer_preset", new ConfigOptionString(active_printer.preset.name)); - const ConfigOption *opt = active_printer.preset.config.option("nozzle_diameter"); - if (opt) - config.set_key_value("num_extruders", new ConfigOptionInt((int)static_cast<const ConfigOptionFloats*>(opt)->values.size())); - return is_compatible_with_printer(preset, active_printer, &config); -} - -void Preset::set_visible_from_appconfig(const AppConfig &app_config) -{ - if (vendor == nullptr) { return; } - - if (type == TYPE_PRINTER) { - const std::string &model = config.opt_string("printer_model"); - const std::string &variant = config.opt_string("printer_variant"); - if (model.empty() || variant.empty()) - return; - is_visible = app_config.get_variant(vendor->id, model, variant); - } else if (type == TYPE_FILAMENT || type == TYPE_SLA_MATERIAL) { - const std::string §ion_name = (type == TYPE_FILAMENT) ? AppConfig::SECTION_FILAMENTS : AppConfig::SECTION_MATERIALS; - if (app_config.has_section(section_name)) { - // Check whether this profile is marked as "installed" in PrusaSlicer.ini, - // or whether a profile is marked as "installed", which this profile may have been renamed from. - const std::map<std::string, std::string> &installed = app_config.get_section(section_name); - auto has = [&installed](const std::string &name) { - auto it = installed.find(name); - return it != installed.end() && ! it->second.empty(); - }; - is_visible = has(this->name); - for (auto it = this->renamed_from.begin(); ! is_visible && it != this->renamed_from.end(); ++ it) - is_visible = has(*it); - } - } -} - -const std::vector<std::string>& Preset::print_options() -{ - static std::vector<std::string> s_opts { - "layer_height", "first_layer_height", "perimeters", "spiral_vase", "slice_closing_radius", - "top_solid_layers", "top_solid_min_thickness", "bottom_solid_layers", "bottom_solid_min_thickness", - "extra_perimeters", "ensure_vertical_shell_thickness", "avoid_crossing_perimeters", "thin_walls", "overhangs", - "seam_position", "external_perimeters_first", "fill_density", "fill_pattern", "top_fill_pattern", "bottom_fill_pattern", - "infill_every_layers", "infill_only_where_needed", "solid_infill_every_layers", "fill_angle", "bridge_angle", - "solid_infill_below_area", "only_retract_when_crossing_perimeters", "infill_first", - "ironing", "ironing_type", "ironing_flowrate", "ironing_speed", "ironing_spacing", - "max_print_speed", "max_volumetric_speed", -#ifdef HAS_PRESSURE_EQUALIZER - "max_volumetric_extrusion_rate_slope_positive", "max_volumetric_extrusion_rate_slope_negative", -#endif /* HAS_PRESSURE_EQUALIZER */ - "perimeter_speed", "small_perimeter_speed", "external_perimeter_speed", "infill_speed", "solid_infill_speed", - "top_solid_infill_speed", "support_material_speed", "support_material_xy_spacing", "support_material_interface_speed", - "bridge_speed", "gap_fill_speed", "travel_speed", "first_layer_speed", "perimeter_acceleration", "infill_acceleration", - "bridge_acceleration", "first_layer_acceleration", "default_acceleration", "skirts", "skirt_distance", "skirt_height", "draft_shield", - "min_skirt_length", "brim_width", "support_material", "support_material_auto", "support_material_threshold", "support_material_enforce_layers", - "raft_layers", "support_material_pattern", "support_material_with_sheath", "support_material_spacing", - "support_material_synchronize_layers", "support_material_angle", "support_material_interface_layers", - "support_material_interface_spacing", "support_material_interface_contact_loops", "support_material_contact_distance", - "support_material_buildplate_only", "dont_support_bridges", "notes", "complete_objects", "extruder_clearance_radius", - "extruder_clearance_height", "gcode_comments", "gcode_label_objects", "output_filename_format", "post_process", "perimeter_extruder", - "infill_extruder", "solid_infill_extruder", "support_material_extruder", "support_material_interface_extruder", - "ooze_prevention", "standby_temperature_delta", "interface_shells", "extrusion_width", "first_layer_extrusion_width", - "perimeter_extrusion_width", "external_perimeter_extrusion_width", "infill_extrusion_width", "solid_infill_extrusion_width", - "top_infill_extrusion_width", "support_material_extrusion_width", "infill_overlap", "bridge_flow_ratio", "clip_multipart_objects", - "elefant_foot_compensation", "xy_size_compensation", "threads", "resolution", "wipe_tower", "wipe_tower_x", "wipe_tower_y", - "wipe_tower_width", "wipe_tower_rotation_angle", "wipe_tower_bridging", "single_extruder_multi_material_priming", - "wipe_tower_no_sparse_layers", "compatible_printers", "compatible_printers_condition", "inherits" - }; - return s_opts; -} - -const std::vector<std::string>& Preset::filament_options() -{ - static std::vector<std::string> s_opts { - "filament_colour", "filament_diameter", "filament_type", "filament_soluble", "filament_notes", "filament_max_volumetric_speed", - "extrusion_multiplier", "filament_density", "filament_cost", "filament_loading_speed", "filament_loading_speed_start", "filament_load_time", - "filament_unloading_speed", "filament_unloading_speed_start", "filament_unload_time", "filament_toolchange_delay", "filament_cooling_moves", - "filament_cooling_initial_speed", "filament_cooling_final_speed", "filament_ramming_parameters", "filament_minimal_purge_on_wipe_tower", - "temperature", "first_layer_temperature", "bed_temperature", "first_layer_bed_temperature", "fan_always_on", "cooling", "min_fan_speed", - "max_fan_speed", "bridge_fan_speed", "disable_fan_first_layers", "fan_below_layer_time", "slowdown_below_layer_time", "min_print_speed", - "start_filament_gcode", "end_filament_gcode", - // Retract overrides - "filament_retract_length", "filament_retract_lift", "filament_retract_lift_above", "filament_retract_lift_below", "filament_retract_speed", "filament_deretract_speed", "filament_retract_restart_extra", "filament_retract_before_travel", - "filament_retract_layer_change", "filament_wipe", "filament_retract_before_wipe", - // Profile compatibility - "filament_vendor", "compatible_prints", "compatible_prints_condition", "compatible_printers", "compatible_printers_condition", "inherits" - }; - return s_opts; -} - -const std::vector<std::string>& Preset::printer_options() -{ - static std::vector<std::string> s_opts; - if (s_opts.empty()) { - s_opts = { - "printer_technology", - "bed_shape", "bed_custom_texture", "bed_custom_model", "z_offset", "gcode_flavor", "use_relative_e_distances", "serial_port", "serial_speed", - "use_firmware_retraction", "use_volumetric_e", "variable_layer_height", - "host_type", "print_host", "printhost_apikey", "printhost_cafile", - "single_extruder_multi_material", "start_gcode", "end_gcode", "before_layer_gcode", "layer_gcode", "toolchange_gcode", - "color_change_gcode", "pause_print_gcode", "template_custom_gcode", - "between_objects_gcode", "printer_vendor", "printer_model", "printer_variant", "printer_notes", "cooling_tube_retraction", - "cooling_tube_length", "high_current_on_filament_swap", "parking_pos_retraction", "extra_loading_move", "max_print_height", - "default_print_profile", "inherits", - "remaining_times", "silent_mode", "machine_max_acceleration_extruding", "machine_max_acceleration_retracting", - "machine_max_acceleration_x", "machine_max_acceleration_y", "machine_max_acceleration_z", "machine_max_acceleration_e", - "machine_max_feedrate_x", "machine_max_feedrate_y", "machine_max_feedrate_z", "machine_max_feedrate_e", - "machine_min_extruding_rate", "machine_min_travel_rate", - "machine_max_jerk_x", "machine_max_jerk_y", "machine_max_jerk_z", "machine_max_jerk_e", - "thumbnails" - }; - s_opts.insert(s_opts.end(), Preset::nozzle_options().begin(), Preset::nozzle_options().end()); - } - return s_opts; -} - -// The following nozzle options of a printer profile will be adjusted to match the size -// of the nozzle_diameter vector. -const std::vector<std::string>& Preset::nozzle_options() -{ - return print_config_def.extruder_option_keys(); -} - -const std::vector<std::string>& Preset::sla_print_options() -{ - static std::vector<std::string> s_opts; - if (s_opts.empty()) { - s_opts = { - "layer_height", - "faded_layers", - "supports_enable", - "support_head_front_diameter", - "support_head_penetration", - "support_head_width", - "support_pillar_diameter", - "support_small_pillar_diameter_percent", - "support_max_bridges_on_pillar", - "support_pillar_connection_mode", - "support_buildplate_only", - "support_pillar_widening_factor", - "support_base_diameter", - "support_base_height", - "support_base_safety_distance", - "support_critical_angle", - "support_max_bridge_length", - "support_max_pillar_link_distance", - "support_object_elevation", - "support_points_density_relative", - "support_points_minimal_distance", - "slice_closing_radius", - "pad_enable", - "pad_wall_thickness", - "pad_wall_height", - "pad_brim_size", - "pad_max_merge_distance", - // "pad_edge_radius", - "pad_wall_slope", - "pad_object_gap", - "pad_around_object", - "pad_around_object_everywhere", - "pad_object_connector_stride", - "pad_object_connector_width", - "pad_object_connector_penetration", - "hollowing_enable", - "hollowing_min_thickness", - "hollowing_quality", - "hollowing_closing_distance", - "output_filename_format", - "default_sla_print_profile", - "compatible_printers", - "compatible_printers_condition", - "inherits" - }; - } - return s_opts; -} - -const std::vector<std::string>& Preset::sla_material_options() -{ - static std::vector<std::string> s_opts; - if (s_opts.empty()) { - s_opts = { - "material_type", - "initial_layer_height", - "bottle_cost", - "bottle_volume", - "bottle_weight", - "material_density", - "exposure_time", - "initial_exposure_time", - "material_correction", - "material_notes", - "material_vendor", - "default_sla_material_profile", - "compatible_prints", "compatible_prints_condition", - "compatible_printers", "compatible_printers_condition", "inherits" - }; - } - return s_opts; -} - -const std::vector<std::string>& Preset::sla_printer_options() -{ - static std::vector<std::string> s_opts; - if (s_opts.empty()) { - s_opts = { - "printer_technology", - "bed_shape", "bed_custom_texture", "bed_custom_model", "max_print_height", - "display_width", "display_height", "display_pixels_x", "display_pixels_y", - "display_mirror_x", "display_mirror_y", - "display_orientation", - "fast_tilt_time", "slow_tilt_time", "area_fill", - "relative_correction", - "absolute_correction", - "elefant_foot_compensation", - "elefant_foot_min_width", - "gamma_correction", - "min_exposure_time", "max_exposure_time", - "min_initial_exposure_time", "max_initial_exposure_time", - "print_host", "printhost_apikey", "printhost_cafile", - "printer_notes", - "inherits" - }; - } - return s_opts; -} - -PresetCollection::PresetCollection(Preset::Type type, const std::vector<std::string> &keys, const Slic3r::StaticPrintConfig &defaults, const std::string &default_name) : - m_type(type), - m_edited_preset(type, "", false), - m_idx_selected(0), - m_bitmap_main_frame(new wxBitmap), - m_bitmap_add(new wxBitmap), - m_bitmap_cache(new GUI::BitmapCache) -{ - // Insert just the default preset. - this->add_default_preset(keys, defaults, default_name); - m_edited_preset.config.apply(m_presets.front().config); -} - -PresetCollection::~PresetCollection() -{ - delete m_bitmap_main_frame; - m_bitmap_main_frame = nullptr; - delete m_bitmap_add; - m_bitmap_add = nullptr; - delete m_bitmap_cache; - m_bitmap_cache = nullptr; -} - -void PresetCollection::reset(bool delete_files) -{ - if (m_presets.size() > m_num_default_presets) { - if (delete_files) { - // Erase the preset files. - for (Preset &preset : m_presets) - if (! preset.is_default && ! preset.is_external && ! preset.is_system) - boost::nowide::remove(preset.file.c_str()); - } - // Don't use m_presets.resize() here as it requires a default constructor for Preset. - m_presets.erase(m_presets.begin() + m_num_default_presets, m_presets.end()); - this->select_preset(0); - } - m_map_alias_to_profile_name.clear(); - m_map_system_profile_renamed.clear(); -} - -void PresetCollection::add_default_preset(const std::vector<std::string> &keys, const Slic3r::StaticPrintConfig &defaults, const std::string &preset_name) -{ - // Insert just the default preset. - m_presets.emplace_back(Preset(this->type(), preset_name, true)); - m_presets.back().config.apply_only(defaults, keys.empty() ? defaults.keys() : keys); - m_presets.back().loaded = true; - ++ m_num_default_presets; -} - -// Load all presets found in dir_path. -// Throws an exception on error. -void PresetCollection::load_presets(const std::string &dir_path, const std::string &subdir) -{ - boost::filesystem::path dir = boost::filesystem::canonical(boost::filesystem::path(dir_path) / subdir).make_preferred(); - m_dir_path = dir.string(); - std::string errors_cummulative; - // Store the loaded presets into a new vector, otherwise the binary search for already existing presets would be broken. - // (see the "Preset already present, not loading" message). - std::deque<Preset> presets_loaded; - for (auto &dir_entry : boost::filesystem::directory_iterator(dir)) - if (Slic3r::is_ini_file(dir_entry)) { - std::string name = dir_entry.path().filename().string(); - // Remove the .ini suffix. - name.erase(name.size() - 4); - if (this->find_preset(name, false)) { - // This happens when there's is a preset (most likely legacy one) with the same name as a system preset - // that's already been loaded from a bundle. - BOOST_LOG_TRIVIAL(warning) << "Preset already present, not loading: " << name; - continue; - } - try { - Preset preset(m_type, name, false); - preset.file = dir_entry.path().string(); - // Load the preset file, apply preset values on top of defaults. - try { - DynamicPrintConfig config; - config.load_from_ini(preset.file); - // Find a default preset for the config. The PrintPresetCollection provides different default preset based on the "printer_technology" field. - const Preset &default_preset = this->default_preset_for(config); - preset.config = default_preset.config; - preset.config.apply(std::move(config)); - Preset::normalize(preset.config); - // Report configuration fields, which are misplaced into a wrong group. - std::string incorrect_keys = Preset::remove_invalid_keys(config, default_preset.config); - if (! incorrect_keys.empty()) - BOOST_LOG_TRIVIAL(error) << "Error in a preset file: The preset \"" << - preset.file << "\" contains the following incorrect keys: " << incorrect_keys << ", which were removed"; - preset.loaded = true; - } catch (const std::ifstream::failure &err) { - throw std::runtime_error(std::string("The selected preset cannot be loaded: ") + preset.file + "\n\tReason: " + err.what()); - } catch (const std::runtime_error &err) { - throw std::runtime_error(std::string("Failed loading the preset file: ") + preset.file + "\n\tReason: " + err.what()); - } - presets_loaded.emplace_back(preset); - } catch (const std::runtime_error &err) { - errors_cummulative += err.what(); - errors_cummulative += "\n"; - } - } - m_presets.insert(m_presets.end(), std::make_move_iterator(presets_loaded.begin()), std::make_move_iterator(presets_loaded.end())); - std::sort(m_presets.begin() + m_num_default_presets, m_presets.end()); - this->select_preset(first_visible_idx()); - if (! errors_cummulative.empty()) - throw std::runtime_error(errors_cummulative); -} - -// Load a preset from an already parsed config file, insert it into the sorted sequence of presets -// and select it, losing previous modifications. -Preset& PresetCollection::load_preset(const std::string &path, const std::string &name, const DynamicPrintConfig &config, bool select) -{ - DynamicPrintConfig cfg(this->default_preset().config); - cfg.apply_only(config, cfg.keys(), true); - return this->load_preset(path, name, std::move(cfg), select); -} - -enum class ProfileHostParams -{ - Same, - Different, - Anonymized, -}; - -static ProfileHostParams profile_host_params_same_or_anonymized(const DynamicPrintConfig &cfg_old, const DynamicPrintConfig &cfg_new) -{ - auto opt_print_host_old = cfg_old.option<ConfigOptionString>("print_host"); - auto opt_printhost_apikey_old = cfg_old.option<ConfigOptionString>("printhost_apikey"); - auto opt_printhost_cafile_old = cfg_old.option<ConfigOptionString>("printhost_cafile"); - - auto opt_print_host_new = cfg_new.option<ConfigOptionString>("print_host"); - auto opt_printhost_apikey_new = cfg_new.option<ConfigOptionString>("printhost_apikey"); - auto opt_printhost_cafile_new = cfg_new.option<ConfigOptionString>("printhost_cafile"); - - // If the new print host data is undefined, use the old data. - bool new_print_host_undefined = (opt_print_host_new == nullptr || opt_print_host_new ->empty()) && - (opt_printhost_apikey_new == nullptr || opt_printhost_apikey_new ->empty()) && - (opt_printhost_cafile_new == nullptr || opt_printhost_cafile_new ->empty()); - if (new_print_host_undefined) - return ProfileHostParams::Anonymized; - - auto opt_same = [](const ConfigOptionString *l, const ConfigOptionString *r) { - return ((l == nullptr || l->empty()) && (r == nullptr || r->empty())) || - (l != nullptr && r != nullptr && l->value == r->value); - }; - return (opt_same(opt_print_host_old, opt_print_host_new) && opt_same(opt_printhost_apikey_old, opt_printhost_apikey_new) && - opt_same(opt_printhost_cafile_old, opt_printhost_cafile_new)) ? ProfileHostParams::Same : ProfileHostParams::Different; -} - -static bool profile_print_params_same(const DynamicPrintConfig &cfg_old, const DynamicPrintConfig &cfg_new) -{ - t_config_option_keys diff = cfg_old.diff(cfg_new); - // Following keys are used by the UI, not by the slicing core, therefore they are not important - // when comparing profiles for equality. Ignore them. - for (const char *key : { "compatible_prints", "compatible_prints_condition", - "compatible_printers", "compatible_printers_condition", "inherits", - "print_settings_id", "filament_settings_id", "sla_print_settings_id", "sla_material_settings_id", "printer_settings_id", - "printer_model", "printer_variant", "default_print_profile", "default_filament_profile", "default_sla_print_profile", "default_sla_material_profile", - "print_host", "printhost_apikey", "printhost_cafile" }) - diff.erase(std::remove(diff.begin(), diff.end(), key), diff.end()); - // Preset with the same name as stored inside the config exists. - return diff.empty() && profile_host_params_same_or_anonymized(cfg_old, cfg_new) != ProfileHostParams::Different; -} - -// Load a preset from an already parsed config file, insert it into the sorted sequence of presets -// and select it, losing previous modifications. -// In case -Preset& PresetCollection::load_external_preset( - // Path to the profile source file (a G-code, an AMF or 3MF file, a config file) - const std::string &path, - // Name of the profile, derived from the source file name. - const std::string &name, - // Original name of the profile, extracted from the loaded config. Empty, if the name has not been stored. - const std::string &original_name, - // Config to initialize the preset from. - const DynamicPrintConfig &config, - // Select the preset after loading? - bool select) -{ - // Load the preset over a default preset, so that the missing fields are filled in from the default preset. - DynamicPrintConfig cfg(this->default_preset_for(config).config); - cfg.apply_only(config, cfg.keys(), true); - // Is there a preset already loaded with the name stored inside the config? - std::deque<Preset>::iterator it = this->find_preset_internal(original_name); - bool found = it != m_presets.end() && it->name == original_name; - if (! found) { - // Try to match the original_name against the "renamed_from" profile names of loaded system profiles. - it = this->find_preset_renamed(original_name); - found = it != m_presets.end(); - } - if (found) { - if (profile_print_params_same(it->config, cfg)) { - // The preset exists and it matches the values stored inside config. - if (select) - this->select_preset(it - m_presets.begin()); - return *it; - } - if (profile_host_params_same_or_anonymized(it->config, cfg) == ProfileHostParams::Anonymized) { - // The project being loaded is anonymized. Replace the empty host keys of the loaded profile with the data from the original profile. - // See "Octoprint Settings when Opening a .3MF file" GH issue #3244 - auto opt_update = [it, &cfg](const std::string &opt_key) { - auto opt = it->config.option<ConfigOptionString>(opt_key); - if (opt != nullptr) - cfg.set_key_value(opt_key, opt->clone()); - }; - opt_update("print_host"); - opt_update("printhost_apikey"); - opt_update("printhost_cafile"); - } - } - // Update the "inherits" field. - std::string &inherits = Preset::inherits(cfg); - if (found && inherits.empty()) { - // There is a profile with the same name already loaded. Should we update the "inherits" field? - if (it->vendor == nullptr) - inherits = it->inherits(); - else - inherits = it->name; - } - // The external preset does not match an internal preset, load the external preset. - std::string new_name; - for (size_t idx = 0;; ++ idx) { - std::string suffix; - if (original_name.empty()) { - if (idx > 0) - suffix = " (" + std::to_string(idx) + ")"; - } else { - if (idx == 0) - suffix = " (" + original_name + ")"; - else - suffix = " (" + original_name + "-" + std::to_string(idx) + ")"; - } - new_name = name + suffix; - it = this->find_preset_internal(new_name); - if (it == m_presets.end() || it->name != new_name) - // Unique profile name. Insert a new profile. - break; - if (profile_print_params_same(it->config, cfg)) { - // The preset exists and it matches the values stored inside config. - if (select) - this->select_preset(it - m_presets.begin()); - return *it; - } - // Form another profile name. - } - // Insert a new profile. - Preset &preset = this->load_preset(path, new_name, std::move(cfg), select); - preset.is_external = true; - if (&this->get_selected_preset() == &preset) - this->get_edited_preset().is_external = true; - - return preset; -} - -Preset& PresetCollection::load_preset(const std::string &path, const std::string &name, DynamicPrintConfig &&config, bool select) -{ - auto it = this->find_preset_internal(name); - if (it == m_presets.end() || it->name != name) { - // The preset was not found. Create a new preset. - it = m_presets.emplace(it, Preset(m_type, name, false)); - } - Preset &preset = *it; - preset.file = path; - preset.config = std::move(config); - preset.loaded = true; - preset.is_dirty = false; - if (select) - this->select_preset_by_name(name, true); - return preset; -} - -void PresetCollection::save_current_preset(const std::string &new_name, bool detach) -{ - // 1) Find the preset with a new_name or create a new one, - // initialize it with the edited config. - auto it = this->find_preset_internal(new_name); - if (it != m_presets.end() && it->name == new_name) { - // Preset with the same name found. - Preset &preset = *it; - if (preset.is_default || preset.is_external || preset.is_system) - // Cannot overwrite the default preset. - return; - // Overwriting an existing preset. - preset.config = std::move(m_edited_preset.config); - // The newly saved preset will be activated -> make it visible. - preset.is_visible = true; - if (detach) { - // Clear the link to the parent profile. - preset.vendor = nullptr; - preset.inherits().clear(); - preset.alias.clear(); - preset.renamed_from.clear(); - } - } else { - // Creating a new preset. - Preset &preset = *m_presets.insert(it, m_edited_preset); - std::string &inherits = preset.inherits(); - std::string old_name = preset.name; - preset.name = new_name; - preset.file = this->path_from_name(new_name); - preset.vendor = nullptr; - preset.alias.clear(); - preset.renamed_from.clear(); - if (detach) { - // Clear the link to the parent profile. - inherits.clear(); - } else if (preset.is_system) { - // Inheriting from a system preset. - inherits = /* preset.vendor->name + "/" + */ old_name; - } else if (inherits.empty()) { - // Inheriting from a user preset. Link the new preset to the old preset. - // inherits = old_name; - } else { - // Inherited from a user preset. Just maintain the "inherited" flag, - // meaning it will inherit from either the system preset, or the inherited user preset. - } - preset.is_default = false; - preset.is_system = false; - preset.is_external = false; - // The newly saved preset will be activated -> make it visible. - preset.is_visible = true; - // Just system presets have aliases - preset.alias.clear(); - } - // 2) Activate the saved preset. - this->select_preset_by_name(new_name, true); - // 2) Store the active preset to disk. - this->get_selected_preset().save(); -} - -bool PresetCollection::delete_current_preset() -{ - const Preset &selected = this->get_selected_preset(); - if (selected.is_default) - return false; - if (! selected.is_external && ! selected.is_system) { - // Erase the preset file. - boost::nowide::remove(selected.file.c_str()); - } - // Remove the preset from the list. - m_presets.erase(m_presets.begin() + m_idx_selected); - // Find the next visible preset. - size_t new_selected_idx = m_idx_selected; - if (new_selected_idx < m_presets.size()) - for (; new_selected_idx < m_presets.size() && ! m_presets[new_selected_idx].is_visible; ++ new_selected_idx) ; - if (new_selected_idx == m_presets.size()) - for (--new_selected_idx; new_selected_idx > 0 && !m_presets[new_selected_idx].is_visible; --new_selected_idx); - this->select_preset(new_selected_idx); - return true; -} - -bool PresetCollection::delete_preset(const std::string& name) -{ - auto it = this->find_preset_internal(name); - - const Preset& preset = *it; - if (preset.is_default) - return false; - if (!preset.is_external && !preset.is_system) { - // Erase the preset file. - boost::nowide::remove(preset.file.c_str()); - } - m_presets.erase(it); - return true; -} - -void PresetCollection::load_bitmap_default(const std::string &file_name) -{ - *m_bitmap_main_frame = create_scaled_bitmap(file_name); -} - -void PresetCollection::load_bitmap_add(const std::string &file_name) -{ - *m_bitmap_add = create_scaled_bitmap(file_name); -} - -const Preset* PresetCollection::get_selected_preset_parent() const -{ - if (this->get_selected_idx() == size_t(-1)) - // This preset collection has no preset activated yet. Only the get_edited_preset() is valid. - return nullptr; - - const Preset &selected_preset = this->get_selected_preset(); - if (selected_preset.is_system || selected_preset.is_default) - return &selected_preset; - - const Preset &edited_preset = this->get_edited_preset(); - const std::string &inherits = edited_preset.inherits(); - const Preset *preset = nullptr; - if (inherits.empty()) { - if (selected_preset.is_external) - return nullptr; - preset = &this->default_preset(m_type == Preset::Type::TYPE_PRINTER && edited_preset.printer_technology() == ptSLA ? 1 : 0); - } else - preset = this->find_preset(inherits, false); - if (preset == nullptr) { - // Resolve the "renamed_from" field. - assert(! inherits.empty()); - auto it = this->find_preset_renamed(inherits); - if (it != m_presets.end()) - preset = &(*it); - } - return (preset == nullptr/* || preset->is_default*/ || preset->is_external) ? nullptr : preset; -} - -const Preset* PresetCollection::get_preset_parent(const Preset& child) const -{ - const std::string &inherits = child.inherits(); - if (inherits.empty()) -// return this->get_selected_preset().is_system ? &this->get_selected_preset() : nullptr; - return nullptr; - const Preset* preset = this->find_preset(inherits, false); - if (preset == nullptr) { - auto it = this->find_preset_renamed(inherits); - if (it != m_presets.end()) - preset = &(*it); - } - return (preset == nullptr/* || preset->is_default */|| preset->is_external) ? nullptr : preset; -} - -// Return vendor of the first parent profile, for which the vendor is defined, or null if such profile does not exist. -PresetWithVendorProfile PresetCollection::get_preset_with_vendor_profile(const Preset &preset) const -{ - const Preset *p = &preset; - const VendorProfile *v = nullptr; - do { - if (p->vendor != nullptr) { - v = p->vendor; - break; - } - p = this->get_preset_parent(*p); - } while (p != nullptr); - return PresetWithVendorProfile(preset, v); -} - -const std::string& PresetCollection::get_preset_name_by_alias(const std::string& alias) const -{ - for ( - // Find the 1st profile name with the alias. - auto it = Slic3r::lower_bound_by_predicate(m_map_alias_to_profile_name.begin(), m_map_alias_to_profile_name.end(), [&alias](auto &l){ return l.first < alias; }); - // Continue over all profile names with the same alias. - it != m_map_alias_to_profile_name.end() && it->first == alias; ++ it) - if (auto it_preset = this->find_preset_internal(it->second); - it_preset != m_presets.end() && it_preset->name == it->second && - it_preset->is_visible && (it_preset->is_compatible || size_t(it_preset - m_presets.begin()) == m_idx_selected)) - return it_preset->name; - return alias; -} - -const std::string* PresetCollection::get_preset_name_renamed(const std::string &old_name) const -{ - auto it_renamed = m_map_system_profile_renamed.find(old_name); - if (it_renamed != m_map_system_profile_renamed.end()) - return &it_renamed->second; - return nullptr; -} - -const std::string& PresetCollection::get_suffix_modified() { - return g_suffix_modified; -} - -// Return a preset by its name. If the preset is active, a temporary copy is returned. -// If a preset is not found by its name, null is returned. -Preset* PresetCollection::find_preset(const std::string &name, bool first_visible_if_not_found) -{ - Preset key(m_type, name, false); - auto it = this->find_preset_internal(name); - // Ensure that a temporary copy is returned if the preset found is currently selected. - return (it != m_presets.end() && it->name == key.name) ? &this->preset(it - m_presets.begin()) : - first_visible_if_not_found ? &this->first_visible() : nullptr; -} - -// Return index of the first visible preset. Certainly at least the '- default -' preset shall be visible. -size_t PresetCollection::first_visible_idx() const -{ - size_t idx = m_default_suppressed ? m_num_default_presets : 0; - for (; idx < this->m_presets.size(); ++ idx) - if (m_presets[idx].is_visible) - break; - if (idx == m_presets.size()) - idx = 0; - return idx; -} - -void PresetCollection::set_default_suppressed(bool default_suppressed) -{ - if (m_default_suppressed != default_suppressed) { - m_default_suppressed = default_suppressed; - bool default_visible = ! default_suppressed || m_idx_selected < m_num_default_presets; - for (size_t i = 0; i < m_num_default_presets; ++ i) - m_presets[i].is_visible = default_visible; - } -} - -size_t PresetCollection::update_compatible_internal(const PresetWithVendorProfile &active_printer, const PresetWithVendorProfile *active_print, PresetSelectCompatibleType unselect_if_incompatible) -{ - DynamicPrintConfig config; - config.set_key_value("printer_preset", new ConfigOptionString(active_printer.preset.name)); - const ConfigOption *opt = active_printer.preset.config.option("nozzle_diameter"); - if (opt) - config.set_key_value("num_extruders", new ConfigOptionInt((int)static_cast<const ConfigOptionFloats*>(opt)->values.size())); - bool some_compatible = false; - for (size_t idx_preset = m_num_default_presets; idx_preset < m_presets.size(); ++ idx_preset) { - bool selected = idx_preset == m_idx_selected; - Preset &preset_selected = m_presets[idx_preset]; - Preset &preset_edited = selected ? m_edited_preset : preset_selected; - - const PresetWithVendorProfile this_preset_with_vendor_profile = this->get_preset_with_vendor_profile(preset_edited); - bool was_compatible = preset_edited.is_compatible; - preset_edited.is_compatible = is_compatible_with_printer(this_preset_with_vendor_profile, active_printer, &config); - some_compatible |= preset_edited.is_compatible; - if (active_print != nullptr) - preset_edited.is_compatible &= is_compatible_with_print(this_preset_with_vendor_profile, *active_print, active_printer); - if (! preset_edited.is_compatible && selected && - (unselect_if_incompatible == PresetSelectCompatibleType::Always || (unselect_if_incompatible == PresetSelectCompatibleType::OnlyIfWasCompatible && was_compatible))) - m_idx_selected = size_t(-1); - if (selected) - preset_selected.is_compatible = preset_edited.is_compatible; - } - // Update visibility of the default profiles here if the defaults are suppressed, the current profile is not compatible and we don't want to select another compatible profile. - if (m_idx_selected >= m_num_default_presets && m_default_suppressed) - for (size_t i = 0; i < m_num_default_presets; ++ i) - m_presets[i].is_visible = ! some_compatible; - return m_idx_selected; -} - -// Save the preset under a new name. If the name is different from the old one, -// a new preset is stored into the list of presets. -// All presets are marked as not modified and the new preset is activated. -//void PresetCollection::save_current_preset(const std::string &new_name); - -// Delete the current preset, activate the first visible preset. -//void PresetCollection::delete_current_preset(); - -// Update the wxChoice UI component from this list of presets. -// Hide the -void PresetCollection::update_plater_ui(GUI::PresetComboBox *ui) -{ - if (ui == nullptr) - return; - - // Otherwise fill in the list from scratch. - ui->Freeze(); - ui->Clear(); - size_t selected_preset_item = INT_MAX; // some value meaning that no one item is selected - - const Preset &selected_preset = this->get_selected_preset(); - // Show wide icons if the currently selected preset is not compatible with the current printer, - // and draw a red flag in front of the selected preset. - bool wide_icons = ! selected_preset.is_compatible && m_bitmap_incompatible != nullptr; - - /* It's supposed that standard size of an icon is 16px*16px for 100% scaled display. - * So set sizes for solid_colored icons used for filament preset - * and scale them in respect to em_unit value - */ - const float scale_f = ui->em_unit() * 0.1f; - const int icon_height = 16 * scale_f + 0.5f; - const int icon_width = 16 * scale_f + 0.5f; - const int thin_space_icon_width = 4 * scale_f + 0.5f; - const int wide_space_icon_width = 6 * scale_f + 0.5f; - - std::map<wxString, wxBitmap*> nonsys_presets; - wxString selected = ""; - wxString tooltip = ""; - if (!this->m_presets.front().is_visible) - ui->set_label_marker(ui->Append(PresetCollection::separator(L("System presets")), wxNullBitmap)); - for (size_t i = this->m_presets.front().is_visible ? 0 : m_num_default_presets; i < this->m_presets.size(); ++ i) { - const Preset &preset = this->m_presets[i]; - if (! preset.is_visible || (! preset.is_compatible && i != m_idx_selected)) - continue; - - std::string bitmap_key = ""; - // !!! Temporary solution, till refactoring: create and use "sla_printer" icon instead of m_bitmap_main_frame - wxBitmap main_bmp = m_bitmap_main_frame ? *m_bitmap_main_frame : wxNullBitmap; - if (m_type == Preset::TYPE_PRINTER && preset.printer_technology()==ptSLA ) { - bitmap_key = "sla_printer"; - main_bmp = create_scaled_bitmap("sla_printer"); - } - - // If the filament preset is not compatible and there is a "red flag" icon loaded, show it left - // to the filament color image. - if (wide_icons) - bitmap_key += preset.is_compatible ? ",cmpt" : ",ncmpt"; - bitmap_key += (preset.is_system || preset.is_default) ? ",syst" : ",nsyst"; - wxBitmap *bmp = m_bitmap_cache->find(bitmap_key); - if (bmp == nullptr) { - // Create the bitmap with color bars. - std::vector<wxBitmap> bmps; - if (wide_icons) - // Paint a red flag for incompatible presets. - bmps.emplace_back(preset.is_compatible ? m_bitmap_cache->mkclear(icon_width, icon_height) : *m_bitmap_incompatible); - // Paint the color bars. - bmps.emplace_back(m_bitmap_cache->mkclear(thin_space_icon_width, icon_height)); - bmps.emplace_back(main_bmp); - // Paint a lock at the system presets. - bmps.emplace_back(m_bitmap_cache->mkclear(wide_space_icon_width, icon_height)); - bmps.emplace_back((preset.is_system || preset.is_default) ? *m_bitmap_lock : m_bitmap_cache->mkclear(icon_width, icon_height)); - bmp = m_bitmap_cache->insert(bitmap_key, bmps); - } - - const std::string name = preset.alias.empty() ? preset.name : preset.alias; - if (preset.is_default || preset.is_system) { - ui->Append(wxString::FromUTF8((/*preset.*/name + (preset.is_dirty ? g_suffix_modified : "")).c_str()), - (bmp == 0) ? main_bmp : *bmp); - if (i == m_idx_selected || - // just in case: mark selected_preset_item as a first added element - selected_preset_item == INT_MAX) { - selected_preset_item = ui->GetCount() - 1; - tooltip = wxString::FromUTF8(preset.name.c_str()); - } - } - else - { - nonsys_presets.emplace(wxString::FromUTF8((/*preset.*/name + (preset.is_dirty ? g_suffix_modified : "")).c_str()), bmp/*preset.is_compatible*/); - if (i == m_idx_selected) { - selected = wxString::FromUTF8((/*preset.*/name + (preset.is_dirty ? g_suffix_modified : "")).c_str()); - tooltip = wxString::FromUTF8(preset.name.c_str()); - } - } - if (i + 1 == m_num_default_presets) - ui->set_label_marker(ui->Append(PresetCollection::separator(L("System presets")), wxNullBitmap)); - } - if (!nonsys_presets.empty()) - { - ui->set_label_marker(ui->Append(PresetCollection::separator(L("User presets")), wxNullBitmap)); - for (std::map<wxString, wxBitmap*>::iterator it = nonsys_presets.begin(); it != nonsys_presets.end(); ++it) { - ui->Append(it->first, *it->second); - if (it->first == selected || - // just in case: mark selected_preset_item as a first added element - selected_preset_item == INT_MAX) - selected_preset_item = ui->GetCount() - 1; - } - } - if (m_type == Preset::TYPE_PRINTER || m_type == Preset::TYPE_SLA_MATERIAL) { - std::string bitmap_key = ""; - // If the filament preset is not compatible and there is a "red flag" icon loaded, show it left - // to the filament color image. - if (wide_icons) - bitmap_key += "wide,"; - bitmap_key += "edit_preset_list"; - wxBitmap *bmp = m_bitmap_cache->find(bitmap_key); - if (bmp == nullptr) { - // Create the bitmap with color bars. - std::vector<wxBitmap> bmps; - if (wide_icons) - // Paint a red flag for incompatible presets. - bmps.emplace_back(m_bitmap_cache->mkclear(icon_width, icon_height)); - // Paint the color bars. - bmps.emplace_back(m_bitmap_cache->mkclear(thin_space_icon_width, icon_height)); - bmps.emplace_back(*m_bitmap_main_frame); - // Paint a lock at the system presets. - bmps.emplace_back(m_bitmap_cache->mkclear(wide_space_icon_width, icon_height)); -// bmps.emplace_back(m_bitmap_add ? *m_bitmap_add : wxNullBitmap); - bmps.emplace_back(create_scaled_bitmap("edit_uni")); - bmp = m_bitmap_cache->insert(bitmap_key, bmps); - } - if (m_type == Preset::TYPE_SLA_MATERIAL) - ui->set_label_marker(ui->Append(PresetCollection::separator(L("Add/Remove materials")), *bmp), GUI::PresetComboBox::LABEL_ITEM_WIZARD_MATERIALS); - else - ui->set_label_marker(ui->Append(PresetCollection::separator(L("Add/Remove printers")), *bmp), GUI::PresetComboBox::LABEL_ITEM_WIZARD_PRINTERS); - } - - /* But, if selected_preset_item is still equal to INT_MAX, it means that - * there is no presets added to the list. - * So, select last combobox item ("Add/Remove preset") - */ - if (selected_preset_item == INT_MAX) - selected_preset_item = ui->GetCount() - 1; - - ui->SetSelection(selected_preset_item); - ui->SetToolTip(tooltip.IsEmpty() ? ui->GetString(selected_preset_item) : tooltip); - ui->check_selection(selected_preset_item); - ui->Thaw(); - - // Update control min size after rescale (changed Display DPI under MSW) - if (ui->GetMinWidth() != 20 * ui->em_unit()) - ui->SetMinSize(wxSize(20 * ui->em_unit(), ui->GetSize().GetHeight())); -} - -size_t PresetCollection::update_tab_ui(wxBitmapComboBox *ui, bool show_incompatible, const int em/* = 10*/) -{ - if (ui == nullptr) - return 0; - ui->Freeze(); - ui->Clear(); - size_t selected_preset_item = INT_MAX; // some value meaning that no one item is selected - - /* It's supposed that standard size of an icon is 16px*16px for 100% scaled display. - * So set sizes for solid_colored(empty) icons used for preset - * and scale them in respect to em_unit value - */ - const float scale_f = em * 0.1f; - const int icon_height = 16 * scale_f + 0.5f; - const int icon_width = 16 * scale_f + 0.5f; - - std::map<wxString, wxBitmap*> nonsys_presets; - wxString selected = ""; - if (!this->m_presets.front().is_visible) - ui->Append(PresetCollection::separator(L("System presets")), wxNullBitmap); - for (size_t i = this->m_presets.front().is_visible ? 0 : m_num_default_presets; i < this->m_presets.size(); ++i) { - const Preset &preset = this->m_presets[i]; - if (! preset.is_visible || (! show_incompatible && ! preset.is_compatible && i != m_idx_selected)) - continue; - std::string bitmap_key = "tab"; - - // !!! Temporary solution, till refactoring: create and use "sla_printer" icon instead of m_bitmap_main_frame - wxBitmap main_bmp = m_bitmap_main_frame ? *m_bitmap_main_frame : wxNullBitmap; - if (m_type == Preset::TYPE_PRINTER && preset.printer_technology() == ptSLA) { - bitmap_key = "sla_printer"; - main_bmp = create_scaled_bitmap("sla_printer"); - } - - bitmap_key += preset.is_compatible ? ",cmpt" : ",ncmpt"; - bitmap_key += (preset.is_system || preset.is_default) ? ",syst" : ",nsyst"; - wxBitmap *bmp = m_bitmap_cache->find(bitmap_key); - if (bmp == nullptr) { - // Create the bitmap with color bars. - std::vector<wxBitmap> bmps; - const wxBitmap* tmp_bmp = preset.is_compatible ? m_bitmap_compatible : m_bitmap_incompatible; - bmps.emplace_back((tmp_bmp == 0) ? main_bmp : *tmp_bmp); - // Paint a lock at the system presets. - bmps.emplace_back((preset.is_system || preset.is_default) ? *m_bitmap_lock : m_bitmap_cache->mkclear(icon_width, icon_height)); - bmp = m_bitmap_cache->insert(bitmap_key, bmps); - } - - if (preset.is_default || preset.is_system) { - ui->Append(wxString::FromUTF8((preset.name + (preset.is_dirty ? g_suffix_modified : "")).c_str()), - (bmp == 0) ? main_bmp : *bmp); - if (i == m_idx_selected || - // just in case: mark selected_preset_item as a first added element - selected_preset_item == INT_MAX) - selected_preset_item = ui->GetCount() - 1; - } - else - { - nonsys_presets.emplace(wxString::FromUTF8((preset.name + (preset.is_dirty ? g_suffix_modified : "")).c_str()), bmp/*preset.is_compatible*/); - if (i == m_idx_selected) - selected = wxString::FromUTF8((preset.name + (preset.is_dirty ? g_suffix_modified : "")).c_str()); - } - if (i + 1 == m_num_default_presets) - ui->Append(PresetCollection::separator(L("System presets")), wxNullBitmap); - } - if (!nonsys_presets.empty()) - { - ui->Append(PresetCollection::separator(L("User presets")), wxNullBitmap); - for (std::map<wxString, wxBitmap*>::iterator it = nonsys_presets.begin(); it != nonsys_presets.end(); ++it) { - ui->Append(it->first, *it->second); - if (it->first == selected || - // just in case: mark selected_preset_item as a first added element - selected_preset_item == INT_MAX) - selected_preset_item = ui->GetCount() - 1; - } - } - if (m_type == Preset::TYPE_PRINTER) { - wxBitmap *bmp = m_bitmap_cache->find("edit_printer_list"); - if (bmp == nullptr) { - // Create the bitmap with color bars. - std::vector<wxBitmap> bmps; - bmps.emplace_back(*m_bitmap_main_frame); -// bmps.emplace_back(m_bitmap_add ? *m_bitmap_add : wxNullBitmap); - bmps.emplace_back(create_scaled_bitmap("edit_uni")); - bmp = m_bitmap_cache->insert("add_printer_tab", bmps); - } - ui->Append(PresetCollection::separator("Add a new printer"), *bmp); - } - - /* But, if selected_preset_item is still equal to INT_MAX, it means that - * there is no presets added to the list. - * So, select last combobox item ("Add/Remove preset") - */ - if (selected_preset_item == INT_MAX) - selected_preset_item = ui->GetCount() - 1; - - ui->SetSelection(selected_preset_item); - ui->SetToolTip(ui->GetString(selected_preset_item)); - ui->Thaw(); - return selected_preset_item; -} - -// Update a dirty floag of the current preset, update the labels of the UI component accordingly. -// Return true if the dirty flag changed. -bool PresetCollection::update_dirty_ui(wxBitmapComboBox *ui) -{ - wxWindowUpdateLocker noUpdates(ui); - // 1) Update the dirty flag of the current preset. - bool was_dirty = this->get_selected_preset().is_dirty; - bool is_dirty = current_is_dirty(); - this->get_selected_preset().is_dirty = is_dirty; - this->get_edited_preset().is_dirty = is_dirty; - // 2) Update the labels. - for (unsigned int ui_id = 0; ui_id < ui->GetCount(); ++ ui_id) { - std::string old_label = ui->GetString(ui_id).utf8_str().data(); - std::string preset_name = Preset::remove_suffix_modified(old_label); - const Preset *preset = this->find_preset(preset_name, false); -// The old_label could be the "----- system presets ------" or the "------- user presets --------" separator. -// assert(preset != nullptr); - if (preset != nullptr) { - std::string new_label = preset->is_dirty ? preset->name + g_suffix_modified : preset->name; - if (old_label != new_label) - ui->SetString(ui_id, wxString::FromUTF8(new_label.c_str())); - } - } -#ifdef __APPLE__ - // wxWidgets on OSX do not upload the text of the combo box line automatically. - // Force it to update by re-selecting. - ui->SetSelection(ui->GetSelection()); -#endif /* __APPLE __ */ - return was_dirty != is_dirty; -} - -template<class T> -void add_correct_opts_to_diff(const std::string &opt_key, t_config_option_keys& vec, const ConfigBase &other, const ConfigBase &this_c) -{ - const T* opt_init = static_cast<const T*>(other.option(opt_key)); - const T* opt_cur = static_cast<const T*>(this_c.option(opt_key)); - int opt_init_max_id = opt_init->values.size() - 1; - for (int i = 0; i < int(opt_cur->values.size()); i++) - { - int init_id = i <= opt_init_max_id ? i : 0; - if (opt_cur->values[i] != opt_init->values[init_id]) - vec.emplace_back(opt_key + "#" + std::to_string(i)); - } -} - -// Use deep_diff to correct return of changed options, considering individual options for each extruder. -inline t_config_option_keys deep_diff(const ConfigBase &config_this, const ConfigBase &config_other) -{ - t_config_option_keys diff; - for (const t_config_option_key &opt_key : config_this.keys()) { - const ConfigOption *this_opt = config_this.option(opt_key); - const ConfigOption *other_opt = config_other.option(opt_key); - if (this_opt != nullptr && other_opt != nullptr && *this_opt != *other_opt) - { - if (opt_key == "bed_shape" || opt_key == "thumbnails" || opt_key == "compatible_prints" || opt_key == "compatible_printers") { - diff.emplace_back(opt_key); - continue; - } - switch (other_opt->type()) - { - case coInts: add_correct_opts_to_diff<ConfigOptionInts >(opt_key, diff, config_other, config_this); break; - case coBools: add_correct_opts_to_diff<ConfigOptionBools >(opt_key, diff, config_other, config_this); break; - case coFloats: add_correct_opts_to_diff<ConfigOptionFloats >(opt_key, diff, config_other, config_this); break; - case coStrings: add_correct_opts_to_diff<ConfigOptionStrings >(opt_key, diff, config_other, config_this); break; - case coPercents:add_correct_opts_to_diff<ConfigOptionPercents >(opt_key, diff, config_other, config_this); break; - case coPoints: add_correct_opts_to_diff<ConfigOptionPoints >(opt_key, diff, config_other, config_this); break; - default: diff.emplace_back(opt_key); break; - } - } - } - return diff; -} - -std::vector<std::string> PresetCollection::dirty_options(const Preset *edited, const Preset *reference, const bool deep_compare /*= false*/) -{ - std::vector<std::string> changed; - if (edited != nullptr && reference != nullptr) { - changed = deep_compare ? - deep_diff(edited->config, reference->config) : - reference->config.diff(edited->config); - // The "compatible_printers" option key is handled differently from the others: - // It is not mandatory. If the key is missing, it means it is compatible with any printer. - // If the key exists and it is empty, it means it is compatible with no printer. - std::initializer_list<const char*> optional_keys { "compatible_prints", "compatible_printers" }; - for (auto &opt_key : optional_keys) { - if (reference->config.has(opt_key) != edited->config.has(opt_key)) - changed.emplace_back(opt_key); - } - } - return changed; -} - -// Select a new preset. This resets all the edits done to the currently selected preset. -// If the preset with index idx does not exist, a first visible preset is selected. -Preset& PresetCollection::select_preset(size_t idx) -{ - for (Preset &preset : m_presets) - preset.is_dirty = false; - if (idx >= m_presets.size()) - idx = first_visible_idx(); - m_idx_selected = idx; - m_edited_preset = m_presets[idx]; - bool default_visible = ! m_default_suppressed || m_idx_selected < m_num_default_presets; - for (size_t i = 0; i < m_num_default_presets; ++i) - m_presets[i].is_visible = default_visible; - return m_presets[idx]; -} - -bool PresetCollection::select_preset_by_name(const std::string &name_w_suffix, bool force) -{ - std::string name = Preset::remove_suffix_modified(name_w_suffix); - // 1) Try to find the preset by its name. - auto it = this->find_preset_internal(name); - size_t idx = 0; - if (it != m_presets.end() && it->name == name && it->is_visible) - // Preset found by its name and it is visible. - idx = it - m_presets.begin(); - else { - // Find the first visible preset. - for (size_t i = m_default_suppressed ? m_num_default_presets : 0; i < m_presets.size(); ++ i) - if (m_presets[i].is_visible) { - idx = i; - break; - } - // If the first visible preset was not found, return the 0th element, which is the default preset. - } - - // 2) Select the new preset. - if (m_idx_selected != idx || force) { - this->select_preset(idx); - return true; - } - - return false; -} - -bool PresetCollection::select_preset_by_name_strict(const std::string &name) -{ - // 1) Try to find the preset by its name. - auto it = this->find_preset_internal(name); - size_t idx = (size_t)-1; - if (it != m_presets.end() && it->name == name && it->is_visible) - // Preset found by its name. - idx = it - m_presets.begin(); - // 2) Select the new preset. - if (idx != (size_t)-1) { - this->select_preset(idx); - return true; - } - m_idx_selected = idx; - return false; -} - -// Merge one vendor's presets with the other vendor's presets, report duplicates. -std::vector<std::string> PresetCollection::merge_presets(PresetCollection &&other, const VendorMap &new_vendors) -{ - std::vector<std::string> duplicates; - for (Preset &preset : other.m_presets) { - if (preset.is_default || preset.is_external) - continue; - Preset key(m_type, preset.name); - auto it = std::lower_bound(m_presets.begin() + m_num_default_presets, m_presets.end(), key); - if (it == m_presets.end() || it->name != preset.name) { - if (preset.vendor != nullptr) { - // Re-assign a pointer to the vendor structure in the new PresetBundle. - auto it = new_vendors.find(preset.vendor->id); - assert(it != new_vendors.end()); - preset.vendor = &it->second; - } - this->m_presets.emplace(it, std::move(preset)); - } else - duplicates.emplace_back(std::move(preset.name)); - } - return duplicates; -} - -void PresetCollection::update_map_alias_to_profile_name() -{ - m_map_alias_to_profile_name.clear(); - for (const Preset &preset : m_presets) - m_map_alias_to_profile_name.emplace_back(preset.alias, preset.name); - std::sort(m_map_alias_to_profile_name.begin(), m_map_alias_to_profile_name.end(), [](auto &l, auto &r) { return l.first < r.first; }); -} - -void PresetCollection::update_map_system_profile_renamed() -{ - m_map_system_profile_renamed.clear(); - for (Preset &preset : m_presets) - for (const std::string &renamed_from : preset.renamed_from) { - const auto [it, success] = m_map_system_profile_renamed.insert(std::pair<std::string, std::string>(renamed_from, preset.name)); - if (! success) - BOOST_LOG_TRIVIAL(error) << boost::format("Preset name \"%1%\" was marked as renamed from \"%2%\", though preset name \"%3%\" was marked as renamed from \"%2%\" as well.") % preset.name % renamed_from % it->second; - } -} - -std::string PresetCollection::name() const -{ - switch (this->type()) { - case Preset::TYPE_PRINT: return L("print"); - case Preset::TYPE_FILAMENT: return L("filament"); - case Preset::TYPE_SLA_PRINT: return L("SLA print"); - case Preset::TYPE_SLA_MATERIAL: return L("SLA material"); - case Preset::TYPE_PRINTER: return L("printer"); - default: return "invalid"; - } -} - -std::string PresetCollection::section_name() const -{ - switch (this->type()) { - case Preset::TYPE_PRINT: return "print"; - case Preset::TYPE_FILAMENT: return "filament"; - case Preset::TYPE_SLA_PRINT: return "sla_print"; - case Preset::TYPE_SLA_MATERIAL: return "sla_material"; - case Preset::TYPE_PRINTER: return "printer"; - default: return "invalid"; - } -} - -std::vector<std::string> PresetCollection::system_preset_names() const -{ - size_t num = 0; - for (const Preset &preset : m_presets) - if (preset.is_system) - ++ num; - std::vector<std::string> out; - out.reserve(num); - for (const Preset &preset : m_presets) - if (preset.is_system) - out.emplace_back(preset.name); - std::sort(out.begin(), out.end()); - return out; -} - -// Generate a file path from a profile name. Add the ".ini" suffix if it is missing. -std::string PresetCollection::path_from_name(const std::string &new_name) const -{ - std::string file_name = boost::iends_with(new_name, ".ini") ? new_name : (new_name + ".ini"); - return (boost::filesystem::path(m_dir_path) / file_name).make_preferred().string(); -} - -void PresetCollection::clear_bitmap_cache() -{ - m_bitmap_cache->clear(); -} - -wxString PresetCollection::separator(const std::string &label) -{ - return wxString::FromUTF8(PresetCollection::separator_head()) + _(label) + wxString::FromUTF8(PresetCollection::separator_tail()); -} - -const Preset& PrinterPresetCollection::default_preset_for(const DynamicPrintConfig &config) const -{ - const ConfigOptionEnumGeneric *opt_printer_technology = config.opt<ConfigOptionEnumGeneric>("printer_technology"); - return this->default_preset((opt_printer_technology == nullptr || opt_printer_technology->value == ptFFF) ? 0 : 1); -} - -const Preset* PrinterPresetCollection::find_by_model_id(const std::string &model_id) const -{ - if (model_id.empty()) { return nullptr; } - - const auto it = std::find_if(cbegin(), cend(), [&](const Preset &preset) { - return preset.config.opt_string("printer_model") == model_id; - }); - - return it != cend() ? &*it : nullptr; -} - -namespace PresetUtils { - const VendorProfile::PrinterModel* system_printer_model(const Preset &preset) - { - const VendorProfile::PrinterModel *out = nullptr; - if (preset.vendor != nullptr) { - auto *printer_model = preset.config.opt<ConfigOptionString>("printer_model"); - if (printer_model != nullptr && ! printer_model->value.empty()) { - auto it = std::find_if(preset.vendor->models.begin(), preset.vendor->models.end(), [printer_model](const VendorProfile::PrinterModel &pm) { return pm.id == printer_model->value; }); - if (it != preset.vendor->models.end()) - out = &(*it); - } - } - return out; - } -} // namespace PresetUtils - -} // namespace Slic3r diff --git a/src/slic3r/GUI/Preset.hpp b/src/slic3r/GUI/Preset.hpp deleted file mode 100644 index dc0078091..000000000 --- a/src/slic3r/GUI/Preset.hpp +++ /dev/null @@ -1,590 +0,0 @@ -#ifndef slic3r_Preset_hpp_ -#define slic3r_Preset_hpp_ - -#include <deque> -#include <set> -#include <unordered_map> - -#include <boost/filesystem/path.hpp> -#include <boost/property_tree/ptree_fwd.hpp> - -#include "libslic3r/libslic3r.h" -#include "libslic3r/PrintConfig.hpp" -#include "libslic3r/Semver.hpp" - -class wxBitmap; -class wxBitmapComboBox; -class wxChoice; -class wxItemContainer; -class wxString; -class wxWindow; - -namespace Slic3r { - -class AppConfig; -class PresetBundle; - -namespace GUI { - class BitmapCache; - class PresetComboBox; -} - -enum ConfigFileType -{ - CONFIG_FILE_TYPE_UNKNOWN, - CONFIG_FILE_TYPE_APP_CONFIG, - CONFIG_FILE_TYPE_CONFIG, - CONFIG_FILE_TYPE_CONFIG_BUNDLE, -}; - -extern ConfigFileType guess_config_file_type(const boost::property_tree::ptree &tree); - -class VendorProfile -{ -public: - std::string name; - std::string id; - Semver config_version; - std::string config_update_url; - std::string changelog_url; - - struct PrinterVariant { - PrinterVariant() {} - PrinterVariant(const std::string &name) : name(name) {} - std::string name; - }; - - struct PrinterModel { - PrinterModel() {} - std::string id; - std::string name; - PrinterTechnology technology; - std::string family; - std::vector<PrinterVariant> variants; - std::vector<std::string> default_materials; - // Vendor & Printer Model specific print bed model & texture. - std::string bed_model; - std::string bed_texture; - - PrinterVariant* variant(const std::string &name) { - for (auto &v : this->variants) - if (v.name == name) - return &v; - return nullptr; - } - - const PrinterVariant* variant(const std::string &name) const { return const_cast<PrinterModel*>(this)->variant(name); } - }; - std::vector<PrinterModel> models; - - std::set<std::string> default_filaments; - std::set<std::string> default_sla_materials; - - VendorProfile() {} - VendorProfile(std::string id) : id(std::move(id)) {} - - bool valid() const { return ! name.empty() && ! id.empty() && config_version.valid(); } - - // Load VendorProfile from an ini file. - // If `load_all` is false, only the header with basic info (name, version, URLs) is loaded. - static VendorProfile from_ini(const boost::filesystem::path &path, bool load_all=true); - static VendorProfile from_ini(const boost::property_tree::ptree &tree, const boost::filesystem::path &path, bool load_all=true); - - size_t num_variants() const { size_t n = 0; for (auto &model : models) n += model.variants.size(); return n; } - std::vector<std::string> families() const; - - bool operator< (const VendorProfile &rhs) const { return this->id < rhs.id; } - bool operator==(const VendorProfile &rhs) const { return this->id == rhs.id; } -}; - -class Preset; - -// Helper to hold a profile with its vendor definition, where the vendor definition may have been extracted from a parent system preset. -// The parent preset is only accessible through PresetCollection, therefore to allow definition of the various is_compatible_with methods -// outside of the PresetCollection, this composite is returned by PresetCollection::get_preset_with_vendor_profile() when needed. -struct PresetWithVendorProfile { - PresetWithVendorProfile(const Preset &preset, const VendorProfile *vendor) : preset(preset), vendor(vendor) {} - const Preset &preset; - const VendorProfile *vendor; -}; - -// Note: it is imporant that map is used here rather than unordered_map, -// because we need iterators to not be invalidated, -// because Preset and the ConfigWizard hold pointers to VendorProfiles. -// XXX: maybe set is enough (cf. changes in Wizard) -typedef std::map<std::string, VendorProfile> VendorMap; - -class Preset -{ -public: - enum Type - { - TYPE_INVALID, - TYPE_PRINT, - TYPE_SLA_PRINT, - TYPE_FILAMENT, - TYPE_SLA_MATERIAL, - TYPE_PRINTER, - TYPE_COUNT, - }; - - Preset(Type type, const std::string &name, bool is_default = false) : type(type), is_default(is_default), name(name) {} - - Type type = TYPE_INVALID; - - // The preset represents a "default" set of properties, - // pulled from the default values of the PrintConfig (see PrintConfigDef for their definitions). - bool is_default; - // External preset points to a configuration, which has been loaded but not imported - // into the Slic3r default configuration location. - bool is_external = false; - // System preset is read-only. - bool is_system = false; - // Preset is visible, if it is associated with a printer model / variant that is enabled in the AppConfig - // or if it has no printer model / variant association. - // Also the "default" preset is only visible, if it is the only preset in the list. - bool is_visible = true; - // Has this preset been modified? - bool is_dirty = false; - // Is this preset compatible with the currently active printer? - bool is_compatible = true; - - bool is_user() const { return ! this->is_default && ! this->is_system; } - - // Name of the preset, usually derived form the file name. - std::string name; - // File name of the preset. This could be a Print / Filament / Printer preset, - // or a Configuration file bundling the Print + Filament + Printer presets (in that case is_external and possibly is_system will be true), - // or it could be a G-code (again, is_external will be true). - std::string file; - // If this is a system profile, then there should be a vendor data available to display at the UI. - const VendorProfile *vendor = nullptr; - - // Has this profile been loaded? - bool loaded = false; - - // Configuration data, loaded from a file, or set from the defaults. - DynamicPrintConfig config; - - // Alias of the preset - std::string alias; - // List of profile names, from which this profile was renamed at some point of time. - // This list is then used to match profiles by their names when loaded from .gcode, .3mf, .amf, - // and to match the "inherits" field of user profiles with updated system profiles. - std::vector<std::string> renamed_from; - - void save(); - - // Return a label of this preset, consisting of a name and a "(modified)" suffix, if this preset is dirty. - std::string label() const; - - // Set the is_dirty flag if the provided config is different from the active one. - void set_dirty(const DynamicPrintConfig &config) { this->is_dirty = ! this->config.diff(config).empty(); } - void set_dirty(bool dirty = true) { this->is_dirty = dirty; } - void reset_dirty() { this->is_dirty = false; } - - // Returns the name of the preset, from which this preset inherits. - static std::string& inherits(DynamicPrintConfig &cfg) { return cfg.option<ConfigOptionString>("inherits", true)->value; } - std::string& inherits() { return Preset::inherits(this->config); } - const std::string& inherits() const { return Preset::inherits(const_cast<Preset*>(this)->config); } - - // Returns the "compatible_prints_condition". - static std::string& compatible_prints_condition(DynamicPrintConfig &cfg) { return cfg.option<ConfigOptionString>("compatible_prints_condition", true)->value; } - std::string& compatible_prints_condition() { - assert(this->type == TYPE_FILAMENT || this->type == TYPE_SLA_MATERIAL); - return Preset::compatible_prints_condition(this->config); - } - const std::string& compatible_prints_condition() const { return const_cast<Preset*>(this)->compatible_prints_condition(); } - - // Returns the "compatible_printers_condition". - static std::string& compatible_printers_condition(DynamicPrintConfig &cfg) { return cfg.option<ConfigOptionString>("compatible_printers_condition", true)->value; } - std::string& compatible_printers_condition() { - assert(this->type == TYPE_PRINT || this->type == TYPE_SLA_PRINT || this->type == TYPE_FILAMENT || this->type == TYPE_SLA_MATERIAL); - return Preset::compatible_printers_condition(this->config); - } - const std::string& compatible_printers_condition() const { return const_cast<Preset*>(this)->compatible_printers_condition(); } - - // Return a printer technology, return ptFFF if the printer technology is not set. - static PrinterTechnology printer_technology(const DynamicPrintConfig &cfg) { - auto *opt = cfg.option<ConfigOptionEnum<PrinterTechnology>>("printer_technology"); - // The following assert may trigger when importing some legacy profile, - // but it is safer to keep it here to capture the cases where the "printer_technology" key is queried, where it should not. -// assert(opt != nullptr); - return (opt == nullptr) ? ptFFF : opt->value; - } - PrinterTechnology printer_technology() const { return Preset::printer_technology(this->config); } - // This call returns a reference, it may add a new entry into the DynamicPrintConfig. - PrinterTechnology& printer_technology_ref() { return this->config.option<ConfigOptionEnum<PrinterTechnology>>("printer_technology", true)->value; } - - // Set is_visible according to application config - void set_visible_from_appconfig(const AppConfig &app_config); - - // Resize the extruder specific fields, initialize them with the content of the 1st extruder. - void set_num_extruders(unsigned int n) { this->config.set_num_extruders(n); } - - // Sort lexicographically by a preset name. The preset name shall be unique across a single PresetCollection. - bool operator<(const Preset &other) const { return this->name < other.name; } - - static const std::vector<std::string>& print_options(); - static const std::vector<std::string>& filament_options(); - // Printer options contain the nozzle options. - static const std::vector<std::string>& printer_options(); - // Nozzle options of the printer options. - static const std::vector<std::string>& nozzle_options(); - - static const std::vector<std::string>& sla_printer_options(); - static const std::vector<std::string>& sla_material_options(); - static const std::vector<std::string>& sla_print_options(); - - static void update_suffix_modified(); - static const std::string& suffix_modified(); - static std::string remove_suffix_modified(const std::string& name); - static void normalize(DynamicPrintConfig &config); - // Report configuration fields, which are misplaced into a wrong group, remove them from the config. - static std::string remove_invalid_keys(DynamicPrintConfig &config, const DynamicPrintConfig &default_config); - -protected: - friend class PresetCollection; - friend class PresetBundle; -}; - -bool is_compatible_with_print (const PresetWithVendorProfile &preset, const PresetWithVendorProfile &active_print, const PresetWithVendorProfile &active_printer); -bool is_compatible_with_printer(const PresetWithVendorProfile &preset, const PresetWithVendorProfile &active_printer, const DynamicPrintConfig *extra_config); -bool is_compatible_with_printer(const PresetWithVendorProfile &preset, const PresetWithVendorProfile &active_printer); - -enum class PresetSelectCompatibleType { - // Never select a compatible preset if the newly selected profile is not compatible. - Never, - // Only select a compatible preset if the active profile used to be compatible, but it is no more. - OnlyIfWasCompatible, - // Always select a compatible preset if the active profile is no more compatible. - Always -}; - -// Collections of presets of the same type (one of the Print, Filament or Printer type). -class PresetCollection -{ -public: - // Initialize the PresetCollection with the "- default -" preset. - PresetCollection(Preset::Type type, const std::vector<std::string> &keys, const Slic3r::StaticPrintConfig &defaults, const std::string &default_name = "- default -"); - ~PresetCollection(); - - typedef std::deque<Preset>::iterator Iterator; - typedef std::deque<Preset>::const_iterator ConstIterator; - Iterator begin() { return m_presets.begin() + m_num_default_presets; } - ConstIterator begin() const { return m_presets.cbegin() + m_num_default_presets; } - ConstIterator cbegin() const { return m_presets.cbegin() + m_num_default_presets; } - Iterator end() { return m_presets.end(); } - ConstIterator end() const { return m_presets.cend(); } - ConstIterator cend() const { return m_presets.cend(); } - - void reset(bool delete_files); - - Preset::Type type() const { return m_type; } - // Name, to be used on the screen and in error messages. Not localized. - std::string name() const; - // Name, to be used as a section name in config bundle, and as a folder name for presets. - std::string section_name() const; - const std::deque<Preset>& operator()() const { return m_presets; } - - // Add default preset at the start of the collection, increment the m_default_preset counter. - void add_default_preset(const std::vector<std::string> &keys, const Slic3r::StaticPrintConfig &defaults, const std::string &preset_name); - - // Load ini files of the particular type from the provided directory path. - void load_presets(const std::string &dir_path, const std::string &subdir); - - // Load a preset from an already parsed config file, insert it into the sorted sequence of presets - // and select it, losing previous modifications. - Preset& load_preset(const std::string &path, const std::string &name, const DynamicPrintConfig &config, bool select = true); - Preset& load_preset(const std::string &path, const std::string &name, DynamicPrintConfig &&config, bool select = true); - - Preset& load_external_preset( - // Path to the profile source file (a G-code, an AMF or 3MF file, a config file) - const std::string &path, - // Name of the profile, derived from the source file name. - const std::string &name, - // Original name of the profile, extracted from the loaded config. Empty, if the name has not been stored. - const std::string &original_name, - // Config to initialize the preset from. - const DynamicPrintConfig &config, - // Select the preset after loading? - bool select = true); - - // Save the preset under a new name. If the name is different from the old one, - // a new preset is stored into the list of presets. - // All presets are marked as not modified and the new preset is activated. - void save_current_preset(const std::string &new_name, bool detach = false); - - // Delete the current preset, activate the first visible preset. - // returns true if the preset was deleted successfully. - bool delete_current_preset(); - // Delete the current preset, activate the first visible preset. - // returns true if the preset was deleted successfully. - bool delete_preset(const std::string& name); - - // Load default bitmap to be placed at the wxBitmapComboBox of a MainFrame. - void load_bitmap_default(const std::string &file_name); - - // Load "add new printer" bitmap to be placed at the wxBitmapComboBox of a MainFrame. - void load_bitmap_add(const std::string &file_name); - - // Compatible & incompatible marks, to be placed at the wxBitmapComboBox items. - void set_bitmap_compatible (const wxBitmap *bmp) { m_bitmap_compatible = bmp; } - void set_bitmap_incompatible(const wxBitmap *bmp) { m_bitmap_incompatible = bmp; } - void set_bitmap_lock (const wxBitmap *bmp) { m_bitmap_lock = bmp; } - void set_bitmap_lock_open (const wxBitmap *bmp) { m_bitmap_lock_open = bmp; } - - // Enable / disable the "- default -" preset. - void set_default_suppressed(bool default_suppressed); - bool is_default_suppressed() const { return m_default_suppressed; } - - // Select a preset. If an invalid index is provided, the first visible preset is selected. - Preset& select_preset(size_t idx); - // Return the selected preset, without the user modifications applied. - Preset& get_selected_preset() { return m_presets[m_idx_selected]; } - const Preset& get_selected_preset() const { return m_presets[m_idx_selected]; } - size_t get_selected_idx() const { return m_idx_selected; } - // Returns the name of the selected preset, or an empty string if no preset is selected. - std::string get_selected_preset_name() const { return (m_idx_selected == size_t(-1)) ? std::string() : this->get_selected_preset().name; } - // For the current edited preset, return the parent preset if there is one. - // If there is no parent preset, nullptr is returned. - // The parent preset may be a system preset or a user preset, which will be - // reflected by the UI. - const Preset* get_selected_preset_parent() const; - // Get parent preset for a child preset, based on the "inherits" field of a child, - // where the "inherits" profile name is searched for in both m_presets and m_map_system_profile_renamed. - const Preset* get_preset_parent(const Preset& child) const; - // Return the selected preset including the user modifications. - Preset& get_edited_preset() { return m_edited_preset; } - const Preset& get_edited_preset() const { return m_edited_preset; } - - // Return vendor of the first parent profile, for which the vendor is defined, or null if such profile does not exist. - PresetWithVendorProfile get_preset_with_vendor_profile(const Preset &preset) const; - PresetWithVendorProfile get_edited_preset_with_vendor_profile() const { return this->get_preset_with_vendor_profile(this->get_edited_preset()); } - - const std::string& get_preset_name_by_alias(const std::string& alias) const; - const std::string* get_preset_name_renamed(const std::string &old_name) const; - - // used to update preset_choice from Tab - const std::deque<Preset>& get_presets() const { return m_presets; } - size_t get_idx_selected() { return m_idx_selected; } - static const std::string& get_suffix_modified(); - - // Return a preset possibly with modifications. - Preset& default_preset(size_t idx = 0) { assert(idx < m_num_default_presets); return m_presets[idx]; } - const Preset& default_preset(size_t idx = 0) const { assert(idx < m_num_default_presets); return m_presets[idx]; } - virtual const Preset& default_preset_for(const DynamicPrintConfig & /* config */) const { return this->default_preset(); } - // Return a preset by an index. If the preset is active, a temporary copy is returned. - Preset& preset(size_t idx) { return (idx == m_idx_selected) ? m_edited_preset : m_presets[idx]; } - const Preset& preset(size_t idx) const { return const_cast<PresetCollection*>(this)->preset(idx); } - void discard_current_changes() { m_presets[m_idx_selected].reset_dirty(); m_edited_preset = m_presets[m_idx_selected]; } - - // Return a preset by its name. If the preset is active, a temporary copy is returned. - // If a preset is not found by its name, null is returned. - Preset* find_preset(const std::string &name, bool first_visible_if_not_found = false); - const Preset* find_preset(const std::string &name, bool first_visible_if_not_found = false) const - { return const_cast<PresetCollection*>(this)->find_preset(name, first_visible_if_not_found); } - - size_t first_visible_idx() const; - // Return index of the first compatible preset. Certainly at least the '- default -' preset shall be compatible. - // If one of the prefered_alternates is compatible, select it. - template<typename PreferedCondition> - size_t first_compatible_idx(PreferedCondition prefered_condition) const - { - size_t i = m_default_suppressed ? m_num_default_presets : 0; - size_t n = this->m_presets.size(); - size_t i_compatible = n; - for (; i < n; ++ i) - // Since we use the filament selection from Wizard, it's needed to control the preset visibility too - if (m_presets[i].is_compatible && m_presets[i].is_visible) { - if (prefered_condition(m_presets[i].name)) - return i; - if (i_compatible == n) - // Store the first compatible profile into i_compatible. - i_compatible = i; - } - return (i_compatible == n) ? 0 : i_compatible; - } - // Return index of the first compatible preset. Certainly at least the '- default -' preset shall be compatible. - size_t first_compatible_idx() const { return this->first_compatible_idx([](const std::string&){return true;}); } - - // Return index of the first visible preset. Certainly at least the '- default -' preset shall be visible. - // Return the first visible preset. Certainly at least the '- default -' preset shall be visible. - Preset& first_visible() { return this->preset(this->first_visible_idx()); } - const Preset& first_visible() const { return this->preset(this->first_visible_idx()); } - Preset& first_compatible() { return this->preset(this->first_compatible_idx()); } - template<typename PreferedCondition> - Preset& first_compatible(PreferedCondition prefered_condition) { return this->preset(this->first_compatible_idx(prefered_condition)); } - const Preset& first_compatible() const { return this->preset(this->first_compatible_idx()); } - - // Return number of presets including the "- default -" preset. - size_t size() const { return m_presets.size(); } - bool has_defaults_only() const { return m_presets.size() <= m_num_default_presets; } - - // For Print / Filament presets, disable those, which are not compatible with the printer. - template<typename PreferedCondition> - void update_compatible(const PresetWithVendorProfile &active_printer, const PresetWithVendorProfile *active_print, PresetSelectCompatibleType select_other_if_incompatible, PreferedCondition prefered_condition) - { - if (this->update_compatible_internal(active_printer, active_print, select_other_if_incompatible) == (size_t)-1) - // Find some other compatible preset, or the "-- default --" preset. - this->select_preset(this->first_compatible_idx(prefered_condition)); - } - void update_compatible(const PresetWithVendorProfile &active_printer, const PresetWithVendorProfile *active_print, PresetSelectCompatibleType select_other_if_incompatible) - { this->update_compatible(active_printer, active_print, select_other_if_incompatible, [](const std::string&){return true;}); } - - size_t num_visible() const { return std::count_if(m_presets.begin(), m_presets.end(), [](const Preset &preset){return preset.is_visible;}); } - - // Compare the content of get_selected_preset() with get_edited_preset() configs, return true if they differ. - bool current_is_dirty() const { return ! this->current_dirty_options().empty(); } - // Compare the content of get_selected_preset() with get_edited_preset() configs, return the list of keys where they differ. - std::vector<std::string> current_dirty_options(const bool deep_compare = false) const - { return dirty_options(&this->get_edited_preset(), &this->get_selected_preset(), deep_compare); } - // Compare the content of get_selected_preset() with get_edited_preset() configs, return the list of keys where they differ. - std::vector<std::string> current_different_from_parent_options(const bool deep_compare = false) const - { return dirty_options(&this->get_edited_preset(), this->get_selected_preset_parent(), deep_compare); } - - // Return a sorted list of system preset names. - std::vector<std::string> system_preset_names() const; - - // Update the choice UI from the list of presets. - // If show_incompatible, all presets are shown, otherwise only the compatible presets are shown. - // If an incompatible preset is selected, it is shown as well. - size_t update_tab_ui(wxBitmapComboBox *ui, bool show_incompatible, const int em = 10); - // Update the choice UI from the list of presets. - // Only the compatible presets are shown. - // If an incompatible preset is selected, it is shown as well. - void update_plater_ui(GUI::PresetComboBox *ui); - - // Update a dirty floag of the current preset, update the labels of the UI component accordingly. - // Return true if the dirty flag changed. - bool update_dirty_ui(wxBitmapComboBox *ui); - - // Select a profile by its name. Return true if the selection changed. - // Without force, the selection is only updated if the index changes. - // With force, the changes are reverted if the new index is the same as the old index. - bool select_preset_by_name(const std::string &name, bool force); - - // Generate a file path from a profile name. Add the ".ini" suffix if it is missing. - std::string path_from_name(const std::string &new_name) const; - - void clear_bitmap_cache(); - -#ifdef __linux__ - static const char* separator_head() { return "------- "; } - static const char* separator_tail() { return " -------"; } -#else /* __linux__ */ - static const char* separator_head() { return "————— "; } - static const char* separator_tail() { return " —————"; } -#endif /* __linux__ */ - static wxString separator(const std::string &label); - -protected: - // Select a preset, if it exists. If it does not exist, select an invalid (-1) index. - // This is a temporary state, which shall be fixed immediately by the following step. - bool select_preset_by_name_strict(const std::string &name); - - // Merge one vendor's presets with the other vendor's presets, report duplicates. - std::vector<std::string> merge_presets(PresetCollection &&other, const VendorMap &new_vendors); - - // Update m_map_alias_to_profile_name from loaded system profiles. - void update_map_alias_to_profile_name(); - - // Update m_map_system_profile_renamed from loaded system profiles. - void update_map_system_profile_renamed(); - -private: - PresetCollection(); - PresetCollection(const PresetCollection &other); - PresetCollection& operator=(const PresetCollection &other); - - // Find a preset position in the sorted list of presets. - // The "-- default -- " preset is always the first, so it needs - // to be handled differently. - // If a preset does not exist, an iterator is returned indicating where to insert a preset with the same name. - std::deque<Preset>::iterator find_preset_internal(const std::string &name) - { - Preset key(m_type, name); - auto it = std::lower_bound(m_presets.begin() + m_num_default_presets, m_presets.end(), key); - if (it == m_presets.end() || it->name != name) { - // Preset has not been not found in the sorted list of non-default presets. Try the defaults. - for (size_t i = 0; i < m_num_default_presets; ++ i) - if (m_presets[i].name == name) { - it = m_presets.begin() + i; - break; - } - } - return it; - } - std::deque<Preset>::const_iterator find_preset_internal(const std::string &name) const - { return const_cast<PresetCollection*>(this)->find_preset_internal(name); } - std::deque<Preset>::iterator find_preset_renamed(const std::string &name) { - auto it_renamed = m_map_system_profile_renamed.find(name); - auto it = (it_renamed == m_map_system_profile_renamed.end()) ? m_presets.end() : this->find_preset_internal(it_renamed->second); - assert((it_renamed == m_map_system_profile_renamed.end()) || (it != m_presets.end() && it->name == it_renamed->second)); - return it; - } - std::deque<Preset>::const_iterator find_preset_renamed(const std::string &name) const - { return const_cast<PresetCollection*>(this)->find_preset_renamed(name); } - - size_t update_compatible_internal(const PresetWithVendorProfile &active_printer, const PresetWithVendorProfile *active_print, PresetSelectCompatibleType unselect_if_incompatible); - - static std::vector<std::string> dirty_options(const Preset *edited, const Preset *reference, const bool is_printer_type = false); - - // Type of this PresetCollection: TYPE_PRINT, TYPE_FILAMENT or TYPE_PRINTER. - Preset::Type m_type; - // List of presets, starting with the "- default -" preset. - // 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. - std::deque<Preset> m_presets; - // System profiles may have aliases. Map to the full profile name. - std::vector<std::pair<std::string, std::string>> m_map_alias_to_profile_name; - // Map from old system profile name to a current system profile name. - std::map<std::string, std::string> m_map_system_profile_renamed; - // Initially this preset contains a copy of the selected preset. Later on, this copy may be modified by the user. - Preset m_edited_preset; - // Selected preset. - size_t m_idx_selected; - // Is the "- default -" preset suppressed? - bool m_default_suppressed = true; - size_t m_num_default_presets = 0; - // Compatible & incompatible marks, to be placed at the wxBitmapComboBox items of a Plater. - // These bitmaps are not owned by PresetCollection, but by a PresetBundle. - const wxBitmap *m_bitmap_compatible = nullptr; - const wxBitmap *m_bitmap_incompatible = nullptr; - const wxBitmap *m_bitmap_lock = nullptr; - const wxBitmap *m_bitmap_lock_open = nullptr; - // Marks placed at the wxBitmapComboBox of a MainFrame. - // These bitmaps are owned by PresetCollection. - wxBitmap *m_bitmap_main_frame; - // "Add printer profile" icon, owned by PresetCollection. - wxBitmap *m_bitmap_add; - // Path to the directory to store the config files into. - std::string m_dir_path; - - // Caching color bitmaps for the filament combo box. - GUI::BitmapCache *m_bitmap_cache = nullptr; - - // to access select_preset_by_name_strict() - friend class PresetBundle; -}; - -// Printer supports the FFF and SLA technologies, with different set of configuration values, -// therefore this PresetCollection needs to handle two defaults. -class PrinterPresetCollection : public PresetCollection -{ -public: - PrinterPresetCollection(Preset::Type type, const std::vector<std::string> &keys, const Slic3r::StaticPrintConfig &defaults, const std::string &default_name = "- default -") : - PresetCollection(type, keys, defaults, default_name) {} - const Preset& default_preset_for(const DynamicPrintConfig &config) const override; - - const Preset* find_by_model_id(const std::string &model_id) const; -}; - -namespace PresetUtils { - // PrinterModel of a system profile, from which this preset is derived, or null if it is not derived from a system profile. - const VendorProfile::PrinterModel* system_printer_model(const Preset &preset); -} // namespace PresetUtils - -} // namespace Slic3r - -#endif /* slic3r_Preset_hpp_ */ diff --git a/src/slic3r/GUI/PresetBundle.cpp b/src/slic3r/GUI/PresetBundle.cpp deleted file mode 100644 index ba806a0b2..000000000 --- a/src/slic3r/GUI/PresetBundle.cpp +++ /dev/null @@ -1,1757 +0,0 @@ -#include <cassert> - -#include "PresetBundle.hpp" -#include "BitmapCache.hpp" -#include "Plater.hpp" -#include "I18N.hpp" -#include "wxExtensions.hpp" - -#include <algorithm> -#include <fstream> -#include <unordered_set> -#include <boost/filesystem.hpp> -#include <boost/algorithm/clamp.hpp> -#include <boost/algorithm/string/predicate.hpp> - -#include <boost/nowide/cenv.hpp> -#include <boost/nowide/cstdio.hpp> -#include <boost/nowide/fstream.hpp> -#include <boost/property_tree/ini_parser.hpp> -#include <boost/property_tree/ptree.hpp> -#include <boost/locale.hpp> -#include <boost/log/trivial.hpp> - -#include <wx/dcmemory.h> -#include <wx/image.h> -#include <wx/choice.h> -#include <wx/bmpcbox.h> -#include <wx/wupdlock.h> - -#include "libslic3r/libslic3r.h" -#include "libslic3r/Utils.hpp" -#include "libslic3r/Model.hpp" -#include "GUI_App.hpp" - - -// Store the print/filament/printer presets into a "presets" subdirectory of the Slic3rPE config dir. -// This breaks compatibility with the upstream Slic3r if the --datadir is used to switch between the two versions. -// #define SLIC3R_PROFILE_USE_PRESETS_SUBDIR - -namespace Slic3r { - -static std::vector<std::string> s_project_options { - "colorprint_heights", - "wiping_volumes_extruders", - "wiping_volumes_matrix" -}; - -const char *PresetBundle::PRUSA_BUNDLE = "PrusaResearch"; - -PresetBundle::PresetBundle() : - prints(Preset::TYPE_PRINT, Preset::print_options(), static_cast<const HostConfig&>(FullPrintConfig::defaults())), - filaments(Preset::TYPE_FILAMENT, Preset::filament_options(), static_cast<const HostConfig&>(FullPrintConfig::defaults())), - sla_materials(Preset::TYPE_SLA_MATERIAL, Preset::sla_material_options(), static_cast<const SLAMaterialConfig&>(SLAFullPrintConfig::defaults())), - sla_prints(Preset::TYPE_SLA_PRINT, Preset::sla_print_options(), static_cast<const SLAPrintObjectConfig&>(SLAFullPrintConfig::defaults())), - printers(Preset::TYPE_PRINTER, Preset::printer_options(), static_cast<const HostConfig&>(FullPrintConfig::defaults()), "- default FFF -"), - m_bitmapCompatible(new wxBitmap), - m_bitmapIncompatible(new wxBitmap), - m_bitmapLock(new wxBitmap), - m_bitmapLockOpen(new wxBitmap), - m_bitmapCache(new GUI::BitmapCache) -{ - if (wxImage::FindHandler(wxBITMAP_TYPE_PNG) == nullptr) - wxImage::AddHandler(new wxPNGHandler); - - // The following keys are handled by the UI, they do not have a counterpart in any StaticPrintConfig derived classes, - // therefore they need to be handled differently. As they have no counterpart in StaticPrintConfig, they are not being - // initialized based on PrintConfigDef(), but to empty values (zeros, empty vectors, empty strings). - // - // "compatible_printers", "compatible_printers_condition", "inherits", - // "print_settings_id", "filament_settings_id", "printer_settings_id", - // "printer_vendor", "printer_model", "printer_variant", "default_print_profile", "default_filament_profile" - - // Create the ID config keys, as they are not part of the Static print config classes. - this->prints.default_preset().config.optptr("print_settings_id", true); - this->prints.default_preset().compatible_printers_condition(); - this->prints.default_preset().inherits(); - - this->filaments.default_preset().config.option<ConfigOptionStrings>("filament_settings_id", true)->values = { "" }; - this->filaments.default_preset().compatible_printers_condition(); - this->filaments.default_preset().inherits(); - // Set all the nullable values to nils. - this->filaments.default_preset().config.null_nullables(); - - this->sla_materials.default_preset().config.optptr("sla_material_settings_id", true); - this->sla_materials.default_preset().compatible_printers_condition(); - this->sla_materials.default_preset().inherits(); - - this->sla_prints.default_preset().config.optptr("sla_print_settings_id", true); - this->sla_prints.default_preset().config.opt_string("output_filename_format", true) = "[input_filename_base].sl1"; - this->sla_prints.default_preset().compatible_printers_condition(); - this->sla_prints.default_preset().inherits(); - - this->printers.add_default_preset(Preset::sla_printer_options(), static_cast<const SLAMaterialConfig&>(SLAFullPrintConfig::defaults()), "- default SLA -"); - this->printers.preset(1).printer_technology_ref() = ptSLA; - for (size_t i = 0; i < 2; ++ i) { - // The following ugly switch is to avoid printers.preset(0) to return the edited instance, as the 0th default is the current one. - Preset &preset = this->printers.default_preset(i); - preset.config.optptr("printer_settings_id", true); - preset.config.optptr("printer_vendor", true); - preset.config.optptr("printer_model", true); - preset.config.optptr("printer_variant", true); - preset.config.optptr("thumbnails", true); - if (i == 0) { - preset.config.optptr("default_print_profile", true); - preset.config.option<ConfigOptionStrings>("default_filament_profile", true)->values = { "" }; - } - else { - preset.config.optptr("default_sla_print_profile", true); - preset.config.optptr("default_sla_material_profile", true); - } - // default_sla_material_profile - preset.inherits(); - } - - // Load the default preset bitmaps. - // #ys_FIXME_to_delete we'll load them later, using em_unit() -// this->prints .load_bitmap_default("cog"); -// this->sla_prints .load_bitmap_default("package_green.png"); -// this->filaments .load_bitmap_default("spool.png"); -// this->sla_materials.load_bitmap_default("package_green.png"); -// this->printers .load_bitmap_default("printer_empty.png"); -// this->printers .load_bitmap_add("add.png"); -// this->load_compatible_bitmaps(); - - // Re-activate the default presets, so their "edited" preset copies will be updated with the additional configuration values above. - this->prints .select_preset(0); - this->sla_prints .select_preset(0); - this->filaments .select_preset(0); - this->sla_materials.select_preset(0); - this->printers .select_preset(0); - - this->project_config.apply_only(FullPrintConfig::defaults(), s_project_options); -} - -PresetBundle::~PresetBundle() -{ - assert(m_bitmapCompatible != nullptr); - assert(m_bitmapIncompatible != nullptr); - assert(m_bitmapLock != nullptr); - assert(m_bitmapLockOpen != nullptr); - delete m_bitmapCompatible; - m_bitmapCompatible = nullptr; - delete m_bitmapIncompatible; - m_bitmapIncompatible = nullptr; - delete m_bitmapLock; - m_bitmapLock = nullptr; - delete m_bitmapLockOpen; - m_bitmapLockOpen = nullptr; - delete m_bitmapCache; - m_bitmapCache = nullptr; -} - -void PresetBundle::reset(bool delete_files) -{ - // Clear the existing presets, delete their respective files. - this->vendors.clear(); - this->prints .reset(delete_files); - this->sla_prints .reset(delete_files); - this->filaments .reset(delete_files); - this->sla_materials.reset(delete_files); - this->printers .reset(delete_files); - this->filament_presets.clear(); - this->filament_presets.emplace_back(this->filaments.get_selected_preset_name()); - this->obsolete_presets.prints.clear(); - this->obsolete_presets.sla_prints.clear(); - this->obsolete_presets.filaments.clear(); - this->obsolete_presets.sla_materials.clear(); - this->obsolete_presets.printers.clear(); -} - -void PresetBundle::setup_directories() -{ - boost::filesystem::path data_dir = boost::filesystem::path(Slic3r::data_dir()); - std::initializer_list<boost::filesystem::path> paths = { - data_dir, - data_dir / "vendor", - data_dir / "cache", -#ifdef SLIC3R_PROFILE_USE_PRESETS_SUBDIR - // Store the print/filament/printer presets into a "presets" directory. - data_dir / "presets", - data_dir / "presets" / "print", - data_dir / "presets" / "filament", - data_dir / "presets" / "sla_print", - data_dir / "presets" / "sla_material", - data_dir / "presets" / "printer" -#else - // Store the print/filament/printer presets at the same location as the upstream Slic3r. - data_dir / "print", - data_dir / "filament", - data_dir / "sla_print", - data_dir / "sla_material", - data_dir / "printer" -#endif - }; - for (const boost::filesystem::path &path : paths) { - boost::filesystem::path subdir = path; - subdir.make_preferred(); - if (! boost::filesystem::is_directory(subdir) && - ! boost::filesystem::create_directory(subdir)) - throw std::runtime_error(std::string("Slic3r was unable to create its data directory at ") + subdir.string()); - } -} - -void PresetBundle::load_presets(AppConfig &config, const std::string &preferred_model_id) -{ - // First load the vendor specific system presets. - std::string errors_cummulative = this->load_system_presets(); - - const std::string dir_user_presets = data_dir() -#ifdef SLIC3R_PROFILE_USE_PRESETS_SUBDIR - // Store the print/filament/printer presets into a "presets" directory. - + "/presets" -#else - // Store the print/filament/printer presets at the same location as the upstream Slic3r. -#endif - ; - try { - this->prints.load_presets(dir_user_presets, "print"); - } catch (const std::runtime_error &err) { - errors_cummulative += err.what(); - } - try { - this->sla_prints.load_presets(dir_user_presets, "sla_print"); - } catch (const std::runtime_error &err) { - errors_cummulative += err.what(); - } - try { - this->filaments.load_presets(dir_user_presets, "filament"); - } catch (const std::runtime_error &err) { - errors_cummulative += err.what(); - } - try { - this->sla_materials.load_presets(dir_user_presets, "sla_material"); - } catch (const std::runtime_error &err) { - errors_cummulative += err.what(); - } - try { - this->printers.load_presets(dir_user_presets, "printer"); - } catch (const std::runtime_error &err) { - errors_cummulative += err.what(); - } - this->update_multi_material_filament_presets(); - this->update_compatible(PresetSelectCompatibleType::Never); - if (! errors_cummulative.empty()) - throw std::runtime_error(errors_cummulative); - - this->load_selections(config, preferred_model_id); -} - -// Load system presets into this PresetBundle. -// For each vendor, there will be a single PresetBundle loaded. -std::string PresetBundle::load_system_presets() -{ - // Here the vendor specific read only Config Bundles are stored. - boost::filesystem::path dir = (boost::filesystem::path(data_dir()) / "vendor").make_preferred(); - std::string errors_cummulative; - bool first = true; - for (auto &dir_entry : boost::filesystem::directory_iterator(dir)) - if (Slic3r::is_ini_file(dir_entry)) { - std::string name = dir_entry.path().filename().string(); - // Remove the .ini suffix. - name.erase(name.size() - 4); - try { - // Load the config bundle, flatten it. - if (first) { - // Reset this PresetBundle and load the first vendor config. - this->load_configbundle(dir_entry.path().string(), LOAD_CFGBNDLE_SYSTEM); - first = false; - } else { - // Load the other vendor configs, merge them with this PresetBundle. - // Report duplicate profiles. - PresetBundle other; - other.load_configbundle(dir_entry.path().string(), LOAD_CFGBNDLE_SYSTEM); - std::vector<std::string> duplicates = this->merge_presets(std::move(other)); - if (! duplicates.empty()) { - errors_cummulative += "Vendor configuration file " + name + " contains the following presets with names used by other vendors: "; - for (size_t i = 0; i < duplicates.size(); ++ i) { - if (i > 0) - errors_cummulative += ", "; - errors_cummulative += duplicates[i]; - } - } - } - } catch (const std::runtime_error &err) { - errors_cummulative += err.what(); - errors_cummulative += "\n"; - } - } - if (first) { - // No config bundle loaded, reset. - this->reset(false); - } - - this->update_system_maps(); - return errors_cummulative; -} - -// Merge one vendor's presets with the other vendor's presets, report duplicates. -std::vector<std::string> PresetBundle::merge_presets(PresetBundle &&other) -{ - this->vendors.insert(other.vendors.begin(), other.vendors.end()); - std::vector<std::string> duplicate_prints = this->prints .merge_presets(std::move(other.prints), this->vendors); - std::vector<std::string> duplicate_sla_prints = this->sla_prints .merge_presets(std::move(other.sla_prints), this->vendors); - std::vector<std::string> duplicate_filaments = this->filaments .merge_presets(std::move(other.filaments), this->vendors); - std::vector<std::string> duplicate_sla_materials = this->sla_materials.merge_presets(std::move(other.sla_materials), this->vendors); - std::vector<std::string> duplicate_printers = this->printers .merge_presets(std::move(other.printers), this->vendors); - append(this->obsolete_presets.prints, std::move(other.obsolete_presets.prints)); - append(this->obsolete_presets.sla_prints, std::move(other.obsolete_presets.sla_prints)); - append(this->obsolete_presets.filaments, std::move(other.obsolete_presets.filaments)); - append(this->obsolete_presets.sla_materials, std::move(other.obsolete_presets.sla_materials)); - append(this->obsolete_presets.printers, std::move(other.obsolete_presets.printers)); - append(duplicate_prints, std::move(duplicate_sla_prints)); - append(duplicate_prints, std::move(duplicate_filaments)); - append(duplicate_prints, std::move(duplicate_sla_materials)); - append(duplicate_prints, std::move(duplicate_printers)); - return duplicate_prints; -} - -void PresetBundle::update_system_maps() -{ - this->prints .update_map_system_profile_renamed(); - this->sla_prints .update_map_system_profile_renamed(); - this->filaments .update_map_system_profile_renamed(); - this->sla_materials.update_map_system_profile_renamed(); - this->printers .update_map_system_profile_renamed(); - - this->prints .update_map_alias_to_profile_name(); - this->sla_prints .update_map_alias_to_profile_name(); - this->filaments .update_map_alias_to_profile_name(); - this->sla_materials.update_map_alias_to_profile_name(); -} - -static inline std::string remove_ini_suffix(const std::string &name) -{ - std::string out = name; - if (boost::iends_with(out, ".ini")) - out.erase(out.end() - 4, out.end()); - return out; -} - -// Set the "enabled" flag for printer vendors, printer models and printer variants -// based on the user configuration. -// If the "vendor" section is missing, enable all models and variants of the particular vendor. -void PresetBundle::load_installed_printers(const AppConfig &config) -{ - this->update_system_maps(); - for (auto &preset : printers) - preset.set_visible_from_appconfig(config); -} - -const std::string& PresetBundle::get_preset_name_by_alias( const Preset::Type& preset_type, const std::string& alias) const -{ - // there are not aliases for Printers profiles - if (preset_type == Preset::TYPE_PRINTER || preset_type == Preset::TYPE_INVALID) - return alias; - - const PresetCollection& presets = preset_type == Preset::TYPE_PRINT ? prints : - preset_type == Preset::TYPE_SLA_PRINT ? sla_prints : - preset_type == Preset::TYPE_FILAMENT ? filaments : - sla_materials; - - return presets.get_preset_name_by_alias(alias); -} - -void PresetBundle::load_installed_filaments(AppConfig &config) -{ - if (! config.has_section(AppConfig::SECTION_FILAMENTS)) { - // Compatibility with the PrusaSlicer 2.1.1 and older, where the filament profiles were not installable yet. - // Find all filament profiles, which are compatible with installed printers, and act as if these filament profiles - // were installed. - std::unordered_set<const Preset*> compatible_filaments; - for (const Preset &printer : printers) - if (printer.is_visible && printer.printer_technology() == ptFFF) { - const PresetWithVendorProfile printer_with_vendor_profile = printers.get_preset_with_vendor_profile(printer); - for (const Preset &filament : filaments) - if (filament.is_system && is_compatible_with_printer(filaments.get_preset_with_vendor_profile(filament), printer_with_vendor_profile)) - compatible_filaments.insert(&filament); - } - // and mark these filaments as installed, therefore this code will not be executed at the next start of the application. - for (const auto &filament: compatible_filaments) - config.set(AppConfig::SECTION_FILAMENTS, filament->name, "1"); - } - - for (auto &preset : filaments) - preset.set_visible_from_appconfig(config); -} - -void PresetBundle::load_installed_sla_materials(AppConfig &config) -{ - if (! config.has_section(AppConfig::SECTION_MATERIALS)) { - std::unordered_set<const Preset*> comp_sla_materials; - // Compatibility with the PrusaSlicer 2.1.1 and older, where the SLA material profiles were not installable yet. - // Find all SLA material profiles, which are compatible with installed printers, and act as if these SLA material profiles - // were installed. - for (const Preset &printer : printers) - if (printer.is_visible && printer.printer_technology() == ptSLA) { - const PresetWithVendorProfile printer_with_vendor_profile = printers.get_preset_with_vendor_profile(printer); - for (const Preset &material : sla_materials) - if (material.is_system && is_compatible_with_printer(sla_materials.get_preset_with_vendor_profile(material), printer_with_vendor_profile)) - comp_sla_materials.insert(&material); - } - // and mark these SLA materials as installed, therefore this code will not be executed at the next start of the application. - for (const auto &material: comp_sla_materials) - config.set(AppConfig::SECTION_MATERIALS, material->name, "1"); - } - - for (auto &preset : sla_materials) - preset.set_visible_from_appconfig(config); -} - -// Load selections (current print, current filaments, current printer) from config.ini -// This is done on application start up or after updates are applied. -void PresetBundle::load_selections(AppConfig &config, const std::string &preferred_model_id) -{ - // Update visibility of presets based on application vendor / model / variant configuration. - this->load_installed_printers(config); - - // Update visibility of filament and sla material presets - this->load_installed_filaments(config); - this->load_installed_sla_materials(config); - - // Parse the initial print / filament / printer profile names. - std::string initial_print_profile_name = remove_ini_suffix(config.get("presets", "print")); - std::string initial_sla_print_profile_name = remove_ini_suffix(config.get("presets", "sla_print")); - std::string initial_filament_profile_name = remove_ini_suffix(config.get("presets", "filament")); - std::string initial_sla_material_profile_name = remove_ini_suffix(config.get("presets", "sla_material")); - std::string initial_printer_profile_name = remove_ini_suffix(config.get("presets", "printer")); - - // Activate print / filament / printer profiles from either the config, - // or from the preferred_model_id suggestion passed in by ConfigWizard. - // If the printer profile enumerated by the config are not visible, select an alternate preset. - // Do not select alternate profiles for the print / filament profiles as those presets - // will be selected by the following call of this->update_compatible(PresetSelectCompatibleType::Always). - - const Preset *initial_printer = printers.find_preset(initial_printer_profile_name); - const Preset *preferred_printer = printers.find_by_model_id(preferred_model_id); - printers.select_preset_by_name( - (preferred_printer != nullptr && (initial_printer == nullptr || !initial_printer->is_visible)) ? - preferred_printer->name : - initial_printer_profile_name, - true); - - // Selects the profile, leaves it to -1 if the initial profile name is empty or if it was not found. - prints.select_preset_by_name_strict(initial_print_profile_name); - filaments.select_preset_by_name_strict(initial_filament_profile_name); - sla_prints.select_preset_by_name_strict(initial_sla_print_profile_name); - sla_materials.select_preset_by_name_strict(initial_sla_material_profile_name); - - // Load the names of the other filament profiles selected for a multi-material printer. - // Load it even if the current printer technology is SLA. - // The possibly excessive filament names will be later removed with this->update_multi_material_filament_presets() - // once the FFF technology gets selected. - this->filament_presets = { filaments.get_selected_preset_name() }; - for (unsigned int i = 1; i < 1000; ++ i) { - char name[64]; - sprintf(name, "filament_%u", i); - if (! config.has("presets", name)) - break; - this->filament_presets.emplace_back(remove_ini_suffix(config.get("presets", name))); - } - - // Update visibility of presets based on their compatibility with the active printer. - // Always try to select a compatible print and filament preset to the current printer preset, - // as the application may have been closed with an active "external" preset, which does not - // exist. - this->update_compatible(PresetSelectCompatibleType::Always); - this->update_multi_material_filament_presets(); -} - -// Export selections (current print, current filaments, current printer) into config.ini -void PresetBundle::export_selections(AppConfig &config) -{ - assert(this->printers.get_edited_preset().printer_technology() != ptFFF || filament_presets.size() >= 1); - assert(this->printers.get_edited_preset().printer_technology() != ptFFF || filament_presets.size() > 1 || filaments.get_selected_preset_name() == filament_presets.front()); - config.clear_section("presets"); - config.set("presets", "print", prints.get_selected_preset_name()); - config.set("presets", "filament", filament_presets.front()); - for (unsigned i = 1; i < filament_presets.size(); ++i) { - char name[64]; - sprintf(name, "filament_%u", i); - config.set("presets", name, filament_presets[i]); - } - - config.set("presets", "sla_print", sla_prints.get_selected_preset_name()); - config.set("presets", "sla_material", sla_materials.get_selected_preset_name()); - config.set("presets", "printer", printers.get_selected_preset_name()); -} - -void PresetBundle::load_compatible_bitmaps() -{ - *m_bitmapCompatible = create_scaled_bitmap("flag_green"); - *m_bitmapIncompatible = create_scaled_bitmap("flag_red"); - *m_bitmapLock = create_scaled_bitmap("lock_closed"); - *m_bitmapLockOpen = create_scaled_bitmap("lock_open"); - - prints .set_bitmap_compatible(m_bitmapCompatible); - filaments .set_bitmap_compatible(m_bitmapCompatible); - sla_prints .set_bitmap_compatible(m_bitmapCompatible); - sla_materials.set_bitmap_compatible(m_bitmapCompatible); - - prints .set_bitmap_incompatible(m_bitmapIncompatible); - filaments .set_bitmap_incompatible(m_bitmapIncompatible); - sla_prints .set_bitmap_incompatible(m_bitmapIncompatible); - sla_materials.set_bitmap_incompatible(m_bitmapIncompatible); - - prints .set_bitmap_lock(m_bitmapLock); - filaments .set_bitmap_lock(m_bitmapLock); - sla_prints .set_bitmap_lock(m_bitmapLock); - sla_materials.set_bitmap_lock(m_bitmapLock); - printers .set_bitmap_lock(m_bitmapLock); - - prints .set_bitmap_lock_open(m_bitmapLock); - filaments .set_bitmap_lock_open(m_bitmapLock); - sla_prints .set_bitmap_lock_open(m_bitmapLock); - sla_materials.set_bitmap_lock_open(m_bitmapLock); - printers .set_bitmap_lock_open(m_bitmapLock); -} - -DynamicPrintConfig PresetBundle::full_config() const -{ - return (this->printers.get_edited_preset().printer_technology() == ptFFF) ? - this->full_fff_config() : - this->full_sla_config(); -} - -DynamicPrintConfig PresetBundle::full_config_secure() const -{ - DynamicPrintConfig config = this->full_config(); - config.erase("print_host"); - config.erase("printhost_apikey"); - config.erase("printhost_cafile"); - return config; -} - -DynamicPrintConfig PresetBundle::full_fff_config() const -{ - DynamicPrintConfig out; - out.apply(FullPrintConfig::defaults()); - out.apply(this->prints.get_edited_preset().config); - // Add the default filament preset to have the "filament_preset_id" defined. - out.apply(this->filaments.default_preset().config); - out.apply(this->printers.get_edited_preset().config); - out.apply(this->project_config); - - auto *nozzle_diameter = dynamic_cast<const ConfigOptionFloats*>(out.option("nozzle_diameter")); - size_t num_extruders = nozzle_diameter->values.size(); - // Collect the "compatible_printers_condition" and "inherits" values over all presets (print, filaments, printers) into a single vector. - std::vector<std::string> compatible_printers_condition; - std::vector<std::string> compatible_prints_condition; - std::vector<std::string> inherits; - compatible_printers_condition.emplace_back(this->prints.get_edited_preset().compatible_printers_condition()); - inherits .emplace_back(this->prints.get_edited_preset().inherits()); - - if (num_extruders <= 1) { - out.apply(this->filaments.get_edited_preset().config); - compatible_printers_condition.emplace_back(this->filaments.get_edited_preset().compatible_printers_condition()); - compatible_prints_condition .emplace_back(this->filaments.get_edited_preset().compatible_prints_condition()); - inherits .emplace_back(this->filaments.get_edited_preset().inherits()); - } else { - // Retrieve filament presets and build a single config object for them. - // First collect the filament configurations based on the user selection of this->filament_presets. - // Here this->filaments.find_preset() and this->filaments.first_visible() return the edited copy of the preset if active. - std::vector<const DynamicPrintConfig*> filament_configs; - for (const std::string &filament_preset_name : this->filament_presets) - filament_configs.emplace_back(&this->filaments.find_preset(filament_preset_name, true)->config); - while (filament_configs.size() < num_extruders) - filament_configs.emplace_back(&this->filaments.first_visible().config); - for (const DynamicPrintConfig *cfg : filament_configs) { - // The compatible_prints/printers_condition() returns a reference to configuration key, which may not yet exist. - DynamicPrintConfig &cfg_rw = *const_cast<DynamicPrintConfig*>(cfg); - compatible_printers_condition.emplace_back(Preset::compatible_printers_condition(cfg_rw)); - compatible_prints_condition .emplace_back(Preset::compatible_prints_condition(cfg_rw)); - inherits .emplace_back(Preset::inherits(cfg_rw)); - } - // Option values to set a ConfigOptionVector from. - std::vector<const ConfigOption*> filament_opts(num_extruders, nullptr); - // loop through options and apply them to the resulting config. - for (const t_config_option_key &key : this->filaments.default_preset().config.keys()) { - if (key == "compatible_prints" || key == "compatible_printers") - continue; - // Get a destination option. - ConfigOption *opt_dst = out.option(key, false); - if (opt_dst->is_scalar()) { - // Get an option, do not create if it does not exist. - const ConfigOption *opt_src = filament_configs.front()->option(key); - if (opt_src != nullptr) - opt_dst->set(opt_src); - } else { - // Setting a vector value from all filament_configs. - for (size_t i = 0; i < filament_opts.size(); ++ i) - filament_opts[i] = filament_configs[i]->option(key); - static_cast<ConfigOptionVectorBase*>(opt_dst)->set(filament_opts); - } - } - } - - // Don't store the "compatible_printers_condition" for the printer profile, there is none. - inherits.emplace_back(this->printers.get_edited_preset().inherits()); - - // These value types clash between the print and filament profiles. They should be renamed. - out.erase("compatible_prints"); - out.erase("compatible_prints_condition"); - out.erase("compatible_printers"); - out.erase("compatible_printers_condition"); - out.erase("inherits"); - - static const char *keys[] = { "perimeter", "infill", "solid_infill", "support_material", "support_material_interface" }; - for (size_t i = 0; i < sizeof(keys) / sizeof(keys[0]); ++ i) { - std::string key = std::string(keys[i]) + "_extruder"; - auto *opt = dynamic_cast<ConfigOptionInt*>(out.option(key, false)); - assert(opt != nullptr); - opt->value = boost::algorithm::clamp<int>(opt->value, 0, int(num_extruders)); - } - - out.option<ConfigOptionString >("print_settings_id", true)->value = this->prints.get_selected_preset_name(); - out.option<ConfigOptionStrings>("filament_settings_id", true)->values = this->filament_presets; - out.option<ConfigOptionString >("printer_settings_id", true)->value = this->printers.get_selected_preset_name(); - - // Serialize the collected "compatible_printers_condition" and "inherits" fields. - // There will be 1 + num_exturders fields for "inherits" and 2 + num_extruders for "compatible_printers_condition" stored. - // The vector will not be stored if all fields are empty strings. - auto add_if_some_non_empty = [&out](std::vector<std::string> &&values, const std::string &key) { - bool nonempty = false; - for (const std::string &v : values) - if (! v.empty()) { - nonempty = true; - break; - } - if (nonempty) - out.set_key_value(key, new ConfigOptionStrings(std::move(values))); - }; - add_if_some_non_empty(std::move(compatible_printers_condition), "compatible_printers_condition_cummulative"); - add_if_some_non_empty(std::move(compatible_prints_condition), "compatible_prints_condition_cummulative"); - add_if_some_non_empty(std::move(inherits), "inherits_cummulative"); - - out.option<ConfigOptionEnumGeneric>("printer_technology", true)->value = ptFFF; - return out; -} - -DynamicPrintConfig PresetBundle::full_sla_config() const -{ - DynamicPrintConfig out; - out.apply(SLAFullPrintConfig::defaults()); - out.apply(this->sla_prints.get_edited_preset().config); - out.apply(this->sla_materials.get_edited_preset().config); - out.apply(this->printers.get_edited_preset().config); - // There are no project configuration values as of now, the project_config is reserved for FFF printers. -// out.apply(this->project_config); - - // Collect the "compatible_printers_condition" and "inherits" values over all presets (sla_prints, sla_materials, printers) into a single vector. - std::vector<std::string> compatible_printers_condition; - std::vector<std::string> compatible_prints_condition; - std::vector<std::string> inherits; - compatible_printers_condition.emplace_back(this->sla_prints.get_edited_preset().compatible_printers_condition()); - inherits .emplace_back(this->sla_prints.get_edited_preset().inherits()); - compatible_printers_condition.emplace_back(this->sla_materials.get_edited_preset().compatible_printers_condition()); - compatible_prints_condition .emplace_back(this->sla_materials.get_edited_preset().compatible_prints_condition()); - inherits .emplace_back(this->sla_materials.get_edited_preset().inherits()); - inherits .emplace_back(this->printers.get_edited_preset().inherits()); - - // These two value types clash between the print and filament profiles. They should be renamed. - out.erase("compatible_printers"); - out.erase("compatible_printers_condition"); - out.erase("inherits"); - - out.option<ConfigOptionString >("sla_print_settings_id", true)->value = this->sla_prints.get_selected_preset_name(); - out.option<ConfigOptionString >("sla_material_settings_id", true)->value = this->sla_materials.get_selected_preset_name(); - out.option<ConfigOptionString >("printer_settings_id", true)->value = this->printers.get_selected_preset_name(); - - // Serialize the collected "compatible_printers_condition" and "inherits" fields. - // There will be 1 + num_exturders fields for "inherits" and 2 + num_extruders for "compatible_printers_condition" stored. - // The vector will not be stored if all fields are empty strings. - auto add_if_some_non_empty = [&out](std::vector<std::string> &&values, const std::string &key) { - bool nonempty = false; - for (const std::string &v : values) - if (! v.empty()) { - nonempty = true; - break; - } - if (nonempty) - out.set_key_value(key, new ConfigOptionStrings(std::move(values))); - }; - add_if_some_non_empty(std::move(compatible_printers_condition), "compatible_printers_condition_cummulative"); - add_if_some_non_empty(std::move(compatible_prints_condition), "compatible_prints_condition_cummulative"); - add_if_some_non_empty(std::move(inherits), "inherits_cummulative"); - - out.option<ConfigOptionEnumGeneric>("printer_technology", true)->value = ptSLA; - return out; -} - -// Load an external config file containing the print, filament and printer presets. -// Instead of a config file, a G-code may be loaded containing the full set of parameters. -// In the future the configuration will likely be read from an AMF file as well. -// If the file is loaded successfully, its print / filament / printer profiles will be activated. -void PresetBundle::load_config_file(const std::string &path) -{ - if (boost::iends_with(path, ".gcode") || boost::iends_with(path, ".g")) { - DynamicPrintConfig config; - config.apply(FullPrintConfig::defaults()); - config.load_from_gcode_file(path); - Preset::normalize(config); - load_config_file_config(path, true, std::move(config)); - return; - } - - // 1) Try to load the config file into a boost property tree. - boost::property_tree::ptree tree; - try { - boost::nowide::ifstream ifs(path); - boost::property_tree::read_ini(ifs, tree); - } catch (const std::ifstream::failure &err) { - throw std::runtime_error(std::string("The Config Bundle cannot be loaded: ") + path + "\n\tReason: " + err.what()); - } catch (const boost::property_tree::file_parser_error &err) { - throw std::runtime_error((boost::format("Failed loading the Config Bundle \"%1%\": %2% at line %3%") - % err.filename() % err.message() % err.line()).str()); - } catch (const std::runtime_error &err) { - throw std::runtime_error(std::string("Failed loading the preset file: ") + path + "\n\tReason: " + err.what()); - } - - // 2) Continue based on the type of the configuration file. - ConfigFileType config_file_type = guess_config_file_type(tree); - switch (config_file_type) { - case CONFIG_FILE_TYPE_UNKNOWN: - throw std::runtime_error(std::string("Unknown configuration file type: ") + path); - case CONFIG_FILE_TYPE_APP_CONFIG: - throw std::runtime_error(std::string("Invalid configuration file: ") + path + ". This is an application config file."); - case CONFIG_FILE_TYPE_CONFIG: - { - // Initialize a config from full defaults. - DynamicPrintConfig config; - config.apply(FullPrintConfig::defaults()); - config.load(tree); - Preset::normalize(config); - load_config_file_config(path, true, std::move(config)); - break; - } - case CONFIG_FILE_TYPE_CONFIG_BUNDLE: - load_config_file_config_bundle(path, tree); - break; - } -} - -// Load a config file from a boost property_tree. This is a private method called from load_config_file. -void PresetBundle::load_config_file_config(const std::string &name_or_path, bool is_external, DynamicPrintConfig &&config) -{ - PrinterTechnology printer_technology = Preset::printer_technology(config); - - // The "compatible_printers" field should not have been exported into a config.ini or a G-code anyway, - // but some of the alpha versions of Slic3r did. - { - ConfigOption *opt_compatible = config.optptr("compatible_printers"); - if (opt_compatible != nullptr) { - assert(opt_compatible->type() == coStrings); - if (opt_compatible->type() == coStrings) - static_cast<ConfigOptionStrings*>(opt_compatible)->values.clear(); - } - } - - size_t num_extruders = (printer_technology == ptFFF) ? - std::min(config.option<ConfigOptionFloats>("nozzle_diameter" )->values.size(), - config.option<ConfigOptionFloats>("filament_diameter")->values.size()) : - // 1 SLA material - 1; - // Make a copy of the "compatible_printers_condition_cummulative" and "inherits_cummulative" vectors, which - // accumulate values over all presets (print, filaments, printers). - // These values will be distributed into their particular presets when loading. - std::vector<std::string> compatible_printers_condition_values = std::move(config.option<ConfigOptionStrings>("compatible_printers_condition_cummulative", true)->values); - std::vector<std::string> compatible_prints_condition_values = std::move(config.option<ConfigOptionStrings>("compatible_prints_condition_cummulative", true)->values); - std::vector<std::string> inherits_values = std::move(config.option<ConfigOptionStrings>("inherits_cummulative", true)->values); - std::string &compatible_printers_condition = Preset::compatible_printers_condition(config); - std::string &compatible_prints_condition = Preset::compatible_prints_condition(config); - std::string &inherits = Preset::inherits(config); - compatible_printers_condition_values.resize(num_extruders + 2, std::string()); - compatible_prints_condition_values.resize(num_extruders, std::string()); - inherits_values.resize(num_extruders + 2, std::string()); - // The "default_filament_profile" will be later extracted into the printer profile. - switch (printer_technology) { - case ptFFF: - config.option<ConfigOptionString>("default_print_profile", true); - config.option<ConfigOptionStrings>("default_filament_profile", true)->values.resize(num_extruders, std::string()); - break; - case ptSLA: - config.option<ConfigOptionString>("default_sla_print_profile", true); - config.option<ConfigOptionString>("default_sla_material_profile", true); - break; - default: break; - } - - // 1) Create a name from the file name. - // Keep the suffix (.ini, .gcode, .amf, .3mf etc) to differentiate it from the normal profiles. - std::string name = is_external ? boost::filesystem::path(name_or_path).filename().string() : name_or_path; - - // 2) If the loading succeeded, split and load the config into print / filament / printer settings. - // First load the print and printer presets. - - auto load_preset = - [&config, &inherits, &inherits_values, - &compatible_printers_condition, &compatible_printers_condition_values, - &compatible_prints_condition, &compatible_prints_condition_values, - is_external, &name, &name_or_path] - (PresetCollection &presets, size_t idx, const std::string &key) { - // Split the "compatible_printers_condition" and "inherits" values one by one from a single vector to the print & printer profiles. - inherits = inherits_values[idx]; - compatible_printers_condition = compatible_printers_condition_values[idx]; - if (idx > 0 && idx - 1 < compatible_prints_condition_values.size()) - compatible_prints_condition = compatible_prints_condition_values[idx - 1]; - if (is_external) - presets.load_external_preset(name_or_path, name, config.opt_string(key, true), config); - else - presets.load_preset(presets.path_from_name(name), name, config).save(); - }; - - switch (Preset::printer_technology(config)) { - case ptFFF: - { - load_preset(this->prints, 0, "print_settings_id"); - load_preset(this->printers, num_extruders + 1, "printer_settings_id"); - - // 3) Now load the filaments. If there are multiple filament presets, split them and load them. - auto old_filament_profile_names = config.option<ConfigOptionStrings>("filament_settings_id", true); - old_filament_profile_names->values.resize(num_extruders, std::string()); - - if (num_extruders <= 1) { - // Split the "compatible_printers_condition" and "inherits" from the cummulative vectors to separate filament presets. - inherits = inherits_values[1]; - compatible_printers_condition = compatible_printers_condition_values[1]; - compatible_prints_condition = compatible_prints_condition_values.front(); - Preset *loaded = nullptr; - if (is_external) { - loaded = &this->filaments.load_external_preset(name_or_path, name, old_filament_profile_names->values.front(), config); - } else { - loaded = &this->filaments.load_preset(this->filaments.path_from_name(name), name, config); - loaded->save(); - } - this->filament_presets.clear(); - this->filament_presets.emplace_back(loaded->name); - } else { - // Split the filament presets, load each of them separately. - std::vector<DynamicPrintConfig> configs(num_extruders, this->filaments.default_preset().config); - // loop through options and scatter them into configs. - for (const t_config_option_key &key : this->filaments.default_preset().config.keys()) { - const ConfigOption *other_opt = config.option(key); - if (other_opt == nullptr) - continue; - if (other_opt->is_scalar()) { - for (size_t i = 0; i < configs.size(); ++ i) - configs[i].option(key, false)->set(other_opt); - } else if (key != "compatible_printers" && key != "compatible_prints") { - for (size_t i = 0; i < configs.size(); ++ i) - static_cast<ConfigOptionVectorBase*>(configs[i].option(key, false))->set_at(other_opt, 0, i); - } - } - // Load the configs into this->filaments and make them active. - this->filament_presets = std::vector<std::string>(configs.size()); - // To avoid incorrect selection of the first filament preset (means a value of Preset->m_idx_selected) - // in a case when next added preset take a place of previosly selected preset, - // we should add presets from last to first - for (int i = (int)configs.size()-1; i >= 0; i--) { - DynamicPrintConfig &cfg = configs[i]; - // Split the "compatible_printers_condition" and "inherits" from the cummulative vectors to separate filament presets. - cfg.opt_string("compatible_printers_condition", true) = compatible_printers_condition_values[i + 1]; - cfg.opt_string("compatible_prints_condition", true) = compatible_prints_condition_values[i]; - cfg.opt_string("inherits", true) = inherits_values[i + 1]; - // Load all filament presets, but only select the first one in the preset dialog. - Preset *loaded = nullptr; - if (is_external) - loaded = &this->filaments.load_external_preset(name_or_path, name, - (i < int(old_filament_profile_names->values.size())) ? old_filament_profile_names->values[i] : "", - std::move(cfg), i == 0); - else { - // Used by the config wizard when creating a custom setup. - // Therefore this block should only be called for a single extruder. - char suffix[64]; - if (i == 0) - suffix[0] = 0; - else - sprintf(suffix, "%d", (int)i); - std::string new_name = name + suffix; - loaded = &this->filaments.load_preset(this->filaments.path_from_name(new_name), - new_name, std::move(cfg), i == 0); - loaded->save(); - } - this->filament_presets[i] = loaded->name; - } - } - // 4) Load the project config values (the per extruder wipe matrix etc). - this->project_config.apply_only(config, s_project_options); - - update_custom_gcode_per_print_z_from_config(GUI::wxGetApp().plater()->model().custom_gcode_per_print_z, &this->project_config); - - break; - } - case ptSLA: - load_preset(this->sla_prints, 0, "sla_print_settings_id"); - load_preset(this->sla_materials, 1, "sla_material_settings_id"); - load_preset(this->printers, 2, "printer_settings_id"); - break; - default: break; - } - - this->update_compatible(PresetSelectCompatibleType::Never); -} - -// Load the active configuration of a config bundle from a boost property_tree. This is a private method called from load_config_file. -void PresetBundle::load_config_file_config_bundle(const std::string &path, const boost::property_tree::ptree &tree) -{ - // 1) Load the config bundle into a temp data. - PresetBundle tmp_bundle; - // Load the config bundle, don't save the loaded presets to user profile directory. - tmp_bundle.load_configbundle(path, 0); - std::string bundle_name = std::string(" - ") + boost::filesystem::path(path).filename().string(); - - // 2) Extract active configs from the config bundle, copy them and activate them in this bundle. - auto load_one = [this, &path, &bundle_name](PresetCollection &collection_dst, PresetCollection &collection_src, const std::string &preset_name_src, bool activate) -> std::string { - Preset *preset_src = collection_src.find_preset(preset_name_src, false); - Preset *preset_dst = collection_dst.find_preset(preset_name_src, false); - assert(preset_src != nullptr); - std::string preset_name_dst; - if (preset_dst != nullptr && preset_dst->is_default) { - // No need to copy a default preset, it always exists in collection_dst. - if (activate) - collection_dst.select_preset(0); - return preset_name_src; - } else if (preset_dst != nullptr && preset_src->config == preset_dst->config) { - // Don't save as the config exists in the current bundle and its content is the same. - return preset_name_src; - } else { - // Generate a new unique name. - preset_name_dst = preset_name_src + bundle_name; - Preset *preset_dup = nullptr; - for (size_t i = 1; (preset_dup = collection_dst.find_preset(preset_name_dst, false)) != nullptr; ++ i) { - if (preset_src->config == preset_dup->config) - // The preset has been already copied into collection_dst. - return preset_name_dst; - // Try to generate another name. - char buf[64]; - sprintf(buf, " (%d)", (int)i); - preset_name_dst = preset_name_src + buf + bundle_name; - } - } - assert(! preset_name_dst.empty()); - // Save preset_src->config into collection_dst under preset_name_dst. - // The "compatible_printers" field should not have been exported into a config.ini or a G-code anyway, - // but some of the alpha versions of Slic3r did. - ConfigOption *opt_compatible = preset_src->config.optptr("compatible_printers"); - if (opt_compatible != nullptr) { - assert(opt_compatible->type() == coStrings); - if (opt_compatible->type() == coStrings) - static_cast<ConfigOptionStrings*>(opt_compatible)->values.clear(); - } - collection_dst.load_preset(path, preset_name_dst, std::move(preset_src->config), activate).is_external = true; - return preset_name_dst; - }; - load_one(this->prints, tmp_bundle.prints, tmp_bundle.prints .get_selected_preset_name(), true); - load_one(this->sla_prints, tmp_bundle.sla_prints, tmp_bundle.sla_prints .get_selected_preset_name(), true); - load_one(this->filaments, tmp_bundle.filaments, tmp_bundle.filaments .get_selected_preset_name(), true); - load_one(this->sla_materials, tmp_bundle.sla_materials, tmp_bundle.sla_materials.get_selected_preset_name(), true); - load_one(this->printers, tmp_bundle.printers, tmp_bundle.printers .get_selected_preset_name(), true); - this->update_multi_material_filament_presets(); - for (size_t i = 1; i < std::min(tmp_bundle.filament_presets.size(), this->filament_presets.size()); ++ i) - this->filament_presets[i] = load_one(this->filaments, tmp_bundle.filaments, tmp_bundle.filament_presets[i], false); - - this->update_compatible(PresetSelectCompatibleType::Never); -} - -// Process the Config Bundle loaded as a Boost property tree. -// For each print, filament and printer preset (group defined by group_name), apply the inherited presets. -// The presets starting with '*' are considered non-terminal and they are -// removed through the flattening process by this function. -// This function will never fail, but it will produce error messages through boost::log. -// system_profiles will not be flattened, and they will be kept inside the "inherits" field -static void flatten_configbundle_hierarchy(boost::property_tree::ptree &tree, const std::string &group_name, const std::vector<std::string> &system_profiles) -{ - namespace pt = boost::property_tree; - - // 1) For the group given by group_name, initialize the presets. - struct Prst { - Prst(const std::string &name, pt::ptree *node) : name(name), node(node) {} - // Name of this preset. If the name starts with '*', it is an intermediate preset, - // which will not make it into the result. - const std::string name; - // Link to the source boost property tree node, owned by tree. - pt::ptree *node; - // Link to the presets, from which this preset inherits. - std::vector<Prst*> inherits; - // Link to the presets, for which this preset is a direct parent. - std::vector<Prst*> parent_of; - // When running the Kahn's Topological sorting algorithm, this counter is decreased from inherits.size() to zero. - // A cycle is indicated, if the number does not drop to zero after the Kahn's algorithm finishes. - size_t num_incoming_edges_left = 0; - // Sorting by the name, to be used when inserted into std::set. - bool operator==(const Prst &rhs) const { return this->name == rhs.name; } - bool operator< (const Prst &rhs) const { return this->name < rhs.name; } - }; - // Find the presets, store them into a std::map, addressed by their names. - std::set<Prst> presets; - std::string group_name_preset = group_name + ":"; - for (auto §ion : tree) - if (boost::starts_with(section.first, group_name_preset) && section.first.size() > group_name_preset.size()) - presets.emplace(section.first.substr(group_name_preset.size()), §ion.second); - // Fill in the "inherits" and "parent_of" members, report invalid inheritance fields. - for (const Prst &prst : presets) { - // Parse the list of comma separated values, possibly enclosed in quotes. - std::vector<std::string> inherits_names; - std::vector<std::string> inherits_system; - if (Slic3r::unescape_strings_cstyle(prst.node->get<std::string>("inherits", ""), inherits_names)) { - // Resolve the inheritance by name. - std::vector<Prst*> &inherits_nodes = const_cast<Prst&>(prst).inherits; - for (const std::string &node_name : inherits_names) { - auto it_system = std::lower_bound(system_profiles.begin(), system_profiles.end(), node_name); - if (it_system != system_profiles.end() && *it_system == node_name) { - // Loading a user config budnle, this preset is derived from a system profile. - inherits_system.emplace_back(node_name); - } else { - auto it = presets.find(Prst(node_name, nullptr)); - if (it == presets.end()) - BOOST_LOG_TRIVIAL(error) << "flatten_configbundle_hierarchy: The preset " << prst.name << " inherits an unknown preset \"" << node_name << "\""; - else { - inherits_nodes.emplace_back(const_cast<Prst*>(&(*it))); - inherits_nodes.back()->parent_of.emplace_back(const_cast<Prst*>(&prst)); - } - } - } - } else { - BOOST_LOG_TRIVIAL(error) << "flatten_configbundle_hierarchy: The preset " << prst.name << " has an invalid \"inherits\" field"; - } - // Remove the "inherits" key, it has no meaning outside of the config bundle. - const_cast<pt::ptree*>(prst.node)->erase("inherits"); - if (! inherits_system.empty()) { - // Loaded a user config bundle, where a profile inherits a system profile. - // User profile should be derived from a single system profile only. - assert(inherits_system.size() == 1); - if (inherits_system.size() > 1) - BOOST_LOG_TRIVIAL(error) << "flatten_configbundle_hierarchy: The preset " << prst.name << " inherits from more than single system preset"; - prst.node->put("inherits", Slic3r::escape_string_cstyle(inherits_system.front())); - } - } - - // 2) Create a linear ordering for the directed acyclic graph of preset inheritance. - // https://en.wikipedia.org/wiki/Topological_sorting - // Kahn's algorithm. - std::vector<Prst*> sorted; - { - // Initialize S with the set of all nodes with no incoming edge. - std::deque<Prst*> S; - for (const Prst &prst : presets) - if (prst.inherits.empty()) - S.emplace_back(const_cast<Prst*>(&prst)); - else - const_cast<Prst*>(&prst)->num_incoming_edges_left = prst.inherits.size(); - while (! S.empty()) { - Prst *n = S.front(); - S.pop_front(); - sorted.emplace_back(n); - for (Prst *m : n->parent_of) { - assert(m->num_incoming_edges_left > 0); - if (-- m->num_incoming_edges_left == 0) { - // We have visited all parents of m. - S.emplace_back(m); - } - } - } - if (sorted.size() < presets.size()) { - for (const Prst &prst : presets) - if (prst.num_incoming_edges_left) - BOOST_LOG_TRIVIAL(error) << "flatten_configbundle_hierarchy: The preset " << prst.name << " has cyclic dependencies"; - } - } - - // Apply the dependencies in their topological ordering. - for (Prst *prst : sorted) { - // Merge the preset nodes in their order of application. - // Iterate in a reverse order, so the last change will be placed first in merged. - for (auto it_inherits = prst->inherits.rbegin(); it_inherits != prst->inherits.rend(); ++ it_inherits) - for (auto it = (*it_inherits)->node->begin(); it != (*it_inherits)->node->end(); ++ it) - if (it->first == "renamed_from") { - // Don't inherit "renamed_from" flag, it does not make sense. The "renamed_from" flag only makes sense for a concrete preset. - if (boost::starts_with((*it_inherits)->name, "*")) - BOOST_LOG_TRIVIAL(error) << boost::format("Nonpublic intermediate preset %1% contains a \"renamed_from\" field, which is ignored") % (*it_inherits)->name; - } else if (prst->node->find(it->first) == prst->node->not_found()) - prst->node->add_child(it->first, it->second); - } - - // Remove the "internal" presets from the ptree. These presets are marked with '*'. - group_name_preset += '*'; - for (auto it_section = tree.begin(); it_section != tree.end(); ) { - if (boost::starts_with(it_section->first, group_name_preset) && it_section->first.size() > group_name_preset.size()) - // Remove the "internal" preset from the ptree. - it_section = tree.erase(it_section); - else - // Keep the preset. - ++ it_section; - } -} - -// preset_bundle is set when loading user config bundles, which must not overwrite the system profiles. -static void flatten_configbundle_hierarchy(boost::property_tree::ptree &tree, const PresetBundle *preset_bundle) -{ - flatten_configbundle_hierarchy(tree, "print", preset_bundle ? preset_bundle->prints.system_preset_names() : std::vector<std::string>()); - flatten_configbundle_hierarchy(tree, "filament", preset_bundle ? preset_bundle->filaments.system_preset_names() : std::vector<std::string>()); - flatten_configbundle_hierarchy(tree, "sla_print", preset_bundle ? preset_bundle->sla_prints.system_preset_names() : std::vector<std::string>()); - flatten_configbundle_hierarchy(tree, "sla_material", preset_bundle ? preset_bundle->sla_materials.system_preset_names() : std::vector<std::string>()); - flatten_configbundle_hierarchy(tree, "printer", preset_bundle ? preset_bundle->printers.system_preset_names() : std::vector<std::string>()); -} - -// Load a config bundle file, into presets and store the loaded presets into separate files -// of the local configuration directory. -size_t PresetBundle::load_configbundle(const std::string &path, unsigned int flags) -{ - if (flags & (LOAD_CFGBNDLE_RESET_USER_PROFILE | LOAD_CFGBNDLE_SYSTEM)) - // Reset this bundle, delete user profile files if LOAD_CFGBNDLE_SAVE. - this->reset(flags & LOAD_CFGBNDLE_SAVE); - - // 1) Read the complete config file into a boost::property_tree. - namespace pt = boost::property_tree; - pt::ptree tree; - boost::nowide::ifstream ifs(path); - pt::read_ini(ifs, tree); - - const VendorProfile *vendor_profile = nullptr; - if (flags & (LOAD_CFGBNDLE_SYSTEM | LOAD_CFGBUNDLE_VENDOR_ONLY)) { - auto vp = VendorProfile::from_ini(tree, path); - if (vp.models.size() == 0) { - BOOST_LOG_TRIVIAL(error) << boost::format("Vendor bundle: `%1%`: No printer model defined.") % path; - return 0; - } else if (vp.num_variants() == 0) { - BOOST_LOG_TRIVIAL(error) << boost::format("Vendor bundle: `%1%`: No printer variant defined") % path; - return 0; - } - vendor_profile = &this->vendors.insert({vp.id, vp}).first->second; - } - - if (flags & LOAD_CFGBUNDLE_VENDOR_ONLY) { - return 0; - } - - // 1.5) Flatten the config bundle by applying the inheritance rules. Internal profiles (with names starting with '*') are removed. - // If loading a user config bundle, do not flatten with the system profiles, but keep the "inherits" flag intact. - flatten_configbundle_hierarchy(tree, ((flags & LOAD_CFGBNDLE_SYSTEM) == 0) ? this : nullptr); - - // 2) Parse the property_tree, extract the active preset names and the profiles, save them into local config files. - // Parse the obsolete preset names, to be deleted when upgrading from the old configuration structure. - std::vector<std::string> loaded_prints; - std::vector<std::string> loaded_filaments; - std::vector<std::string> loaded_sla_prints; - std::vector<std::string> loaded_sla_materials; - std::vector<std::string> loaded_printers; - std::string active_print; - std::vector<std::string> active_filaments; - std::string active_sla_print; - std::string active_sla_material; - std::string active_printer; - size_t presets_loaded = 0; - for (const auto §ion : tree) { - PresetCollection *presets = nullptr; - std::vector<std::string> *loaded = nullptr; - std::string preset_name; - if (boost::starts_with(section.first, "print:")) { - presets = &this->prints; - loaded = &loaded_prints; - preset_name = section.first.substr(6); - } else if (boost::starts_with(section.first, "filament:")) { - presets = &this->filaments; - loaded = &loaded_filaments; - preset_name = section.first.substr(9); - } else if (boost::starts_with(section.first, "sla_print:")) { - presets = &this->sla_prints; - loaded = &loaded_sla_prints; - preset_name = section.first.substr(10); - } else if (boost::starts_with(section.first, "sla_material:")) { - presets = &this->sla_materials; - loaded = &loaded_sla_materials; - preset_name = section.first.substr(13); - } else if (boost::starts_with(section.first, "printer:")) { - presets = &this->printers; - loaded = &loaded_printers; - preset_name = section.first.substr(8); - } else if (section.first == "presets") { - // Load the names of the active presets. - for (auto &kvp : section.second) { - if (kvp.first == "print") { - active_print = kvp.second.data(); - } else if (boost::starts_with(kvp.first, "filament")) { - int idx = 0; - if (kvp.first == "filament" || sscanf(kvp.first.c_str(), "filament_%d", &idx) == 1) { - if (int(active_filaments.size()) <= idx) - active_filaments.resize(idx + 1, std::string()); - active_filaments[idx] = kvp.second.data(); - } - } else if (kvp.first == "sla_print") { - active_sla_print = kvp.second.data(); - } else if (kvp.first == "sla_material") { - active_sla_material = kvp.second.data(); - } else if (kvp.first == "printer") { - active_printer = kvp.second.data(); - } - } - } else if (section.first == "obsolete_presets") { - // Parse the names of obsolete presets. These presets will be deleted from user's - // profile directory on installation of this vendor preset. - for (auto &kvp : section.second) { - std::vector<std::string> *dst = nullptr; - if (kvp.first == "print") - dst = &this->obsolete_presets.prints; - else if (kvp.first == "filament") - dst = &this->obsolete_presets.filaments; - else if (kvp.first == "sla_print") - dst = &this->obsolete_presets.sla_prints; - else if (kvp.first == "sla_material") - dst = &this->obsolete_presets.sla_materials; - else if (kvp.first == "printer") - dst = &this->obsolete_presets.printers; - if (dst) - unescape_strings_cstyle(kvp.second.data(), *dst); - } - } else if (section.first == "settings") { - // Load the settings. - for (auto &kvp : section.second) { - if (kvp.first == "autocenter") { - } - } - } else - // Ignore an unknown section. - continue; - if (presets != nullptr) { - // Load the print, filament or printer preset. - const DynamicPrintConfig *default_config = nullptr; - DynamicPrintConfig config; - std::string alias_name; - std::vector<std::string> renamed_from; - auto parse_config_section = [§ion, &alias_name, &renamed_from, &path](DynamicPrintConfig &config) { - for (auto &kvp : section.second) { - if (kvp.first == "alias") - alias_name = kvp.second.data(); - else if (kvp.first == "renamed_from") { - if (! unescape_strings_cstyle(kvp.second.data(), renamed_from)) { - BOOST_LOG_TRIVIAL(error) << "Error in a Vendor Config Bundle \"" << path << "\": The preset \"" << - section.first << "\" contains invalid \"renamed_from\" key, which is being ignored."; - } - } - config.set_deserialize(kvp.first, kvp.second.data()); - } - }; - if (presets == &this->printers) { - // Select the default config based on the printer_technology field extracted from kvp. - DynamicPrintConfig config_src; - parse_config_section(config_src); - default_config = &presets->default_preset_for(config_src).config; - config = *default_config; - config.apply(config_src); - } else { - default_config = &presets->default_preset().config; - config = *default_config; - parse_config_section(config); - } - Preset::normalize(config); - // Report configuration fields, which are misplaced into a wrong group. - std::string incorrect_keys = Preset::remove_invalid_keys(config, *default_config); - if (! incorrect_keys.empty()) - BOOST_LOG_TRIVIAL(error) << "Error in a Vendor Config Bundle \"" << path << "\": The printer preset \"" << - section.first << "\" contains the following incorrect keys: " << incorrect_keys << ", which were removed"; - if ((flags & LOAD_CFGBNDLE_SYSTEM) && presets == &printers) { - // Filter out printer presets, which are not mentioned in the vendor profile. - // These presets are considered not installed. - auto printer_model = config.opt_string("printer_model"); - if (printer_model.empty()) { - BOOST_LOG_TRIVIAL(error) << "Error in a Vendor Config Bundle \"" << path << "\": The printer preset \"" << - section.first << "\" defines no printer model, it will be ignored."; - continue; - } - auto printer_variant = config.opt_string("printer_variant"); - if (printer_variant.empty()) { - BOOST_LOG_TRIVIAL(error) << "Error in a Vendor Config Bundle \"" << path << "\": The printer preset \"" << - section.first << "\" defines no printer variant, it will be ignored."; - continue; - } - auto it_model = std::find_if(vendor_profile->models.cbegin(), vendor_profile->models.cend(), - [&](const VendorProfile::PrinterModel &m) { return m.id == printer_model; } - ); - if (it_model == vendor_profile->models.end()) { - BOOST_LOG_TRIVIAL(error) << "Error in a Vendor Config Bundle \"" << path << "\": The printer preset \"" << - section.first << "\" defines invalid printer model \"" << printer_model << "\", it will be ignored."; - continue; - } - auto it_variant = it_model->variant(printer_variant); - if (it_variant == nullptr) { - BOOST_LOG_TRIVIAL(error) << "Error in a Vendor Config Bundle \"" << path << "\": The printer preset \"" << - section.first << "\" defines invalid printer variant \"" << printer_variant << "\", it will be ignored."; - continue; - } - const Preset *preset_existing = presets->find_preset(section.first, false); - if (preset_existing != nullptr) { - BOOST_LOG_TRIVIAL(error) << "Error in a Vendor Config Bundle \"" << path << "\": The printer preset \"" << - section.first << "\" has already been loaded from another Confing Bundle."; - continue; - } - } else if ((flags & LOAD_CFGBNDLE_SYSTEM) == 0) { - // This is a user config bundle. - const Preset *existing = presets->find_preset(preset_name, false); - if (existing != nullptr) { - if (existing->is_system) { - assert(existing->vendor != nullptr); - BOOST_LOG_TRIVIAL(error) << "Error in a user provided Config Bundle \"" << path << "\": The " << presets->name() << " preset \"" << - existing->name << "\" is a system preset of vendor " << existing->vendor->name << " and it will be ignored."; - continue; - } else { - assert(existing->vendor == nullptr); - BOOST_LOG_TRIVIAL(trace) << "A " << presets->name() << " preset \"" << existing->name << "\" was overwritten with a preset from user Config Bundle \"" << path << "\""; - } - } else { - BOOST_LOG_TRIVIAL(trace) << "A new " << presets->name() << " preset \"" << preset_name << "\" was imported from user Config Bundle \"" << path << "\""; - } - } - // Decide a full path to this .ini file. - auto file_name = boost::algorithm::iends_with(preset_name, ".ini") ? preset_name : preset_name + ".ini"; - auto file_path = (boost::filesystem::path(data_dir()) -#ifdef SLIC3R_PROFILE_USE_PRESETS_SUBDIR - // Store the print/filament/printer presets into a "presets" directory. - / "presets" -#else - // Store the print/filament/printer presets at the same location as the upstream Slic3r. -#endif - / presets->section_name() / file_name).make_preferred(); - // Load the preset into the list of presets, save it to disk. - Preset &loaded = presets->load_preset(file_path.string(), preset_name, std::move(config), false); - if (flags & LOAD_CFGBNDLE_SAVE) - loaded.save(); - if (flags & LOAD_CFGBNDLE_SYSTEM) { - loaded.is_system = true; - loaded.vendor = vendor_profile; - } - - // Derive the profile logical name aka alias from the preset name if the alias was not stated explicitely. - if (alias_name.empty()) { - size_t end_pos = preset_name.find_first_of("@"); - if (end_pos != std::string::npos) { - alias_name = preset_name.substr(0, end_pos); - if (renamed_from.empty()) - // Add the preset name with the '@' character removed into the "renamed_from" list. - renamed_from.emplace_back(alias_name + preset_name.substr(end_pos + 1)); - boost::trim_right(alias_name); - } - } - if (alias_name.empty()) - loaded.alias = preset_name; - else - loaded.alias = std::move(alias_name); - loaded.renamed_from = std::move(renamed_from); - - ++ presets_loaded; - } - } - - // 3) Activate the presets. - if ((flags & LOAD_CFGBNDLE_SYSTEM) == 0) { - if (! active_print.empty()) - prints.select_preset_by_name(active_print, true); - if (! active_sla_print.empty()) - sla_materials.select_preset_by_name(active_sla_print, true); - if (! active_sla_material.empty()) - sla_materials.select_preset_by_name(active_sla_material, true); - if (! active_printer.empty()) - printers.select_preset_by_name(active_printer, true); - // Activate the first filament preset. - if (! active_filaments.empty() && ! active_filaments.front().empty()) - filaments.select_preset_by_name(active_filaments.front(), true); - this->update_multi_material_filament_presets(); - for (size_t i = 0; i < std::min(this->filament_presets.size(), active_filaments.size()); ++ i) - this->filament_presets[i] = filaments.find_preset(active_filaments[i], true)->name; - this->update_compatible(PresetSelectCompatibleType::Never); - } - - return presets_loaded; -} - -void PresetBundle::update_multi_material_filament_presets() -{ - if (printers.get_edited_preset().printer_technology() != ptFFF) - return; - - // Verify and select the filament presets. - auto *nozzle_diameter = static_cast<const ConfigOptionFloats*>(printers.get_edited_preset().config.option("nozzle_diameter")); - size_t num_extruders = nozzle_diameter->values.size(); - // Verify validity of the current filament presets. - for (size_t i = 0; i < std::min(this->filament_presets.size(), num_extruders); ++ i) - this->filament_presets[i] = this->filaments.find_preset(this->filament_presets[i], true)->name; - // Append the rest of filament presets. - this->filament_presets.resize(num_extruders, this->filament_presets.empty() ? this->filaments.first_visible().name : this->filament_presets.back()); - - // Now verify if wiping_volumes_matrix has proper size (it is used to deduce number of extruders in wipe tower generator): - std::vector<double> old_matrix = this->project_config.option<ConfigOptionFloats>("wiping_volumes_matrix")->values; - size_t old_number_of_extruders = size_t(sqrt(old_matrix.size())+EPSILON); - if (num_extruders != old_number_of_extruders) { - // First verify if purging volumes presets for each extruder matches number of extruders - std::vector<double>& extruders = this->project_config.option<ConfigOptionFloats>("wiping_volumes_extruders")->values; - while (extruders.size() < 2*num_extruders) { - extruders.push_back(extruders.size()>1 ? extruders[0] : 50.); // copy the values from the first extruder - extruders.push_back(extruders.size()>1 ? extruders[1] : 50.); - } - while (extruders.size() > 2*num_extruders) { - extruders.pop_back(); - extruders.pop_back(); - } - - std::vector<double> new_matrix; - for (unsigned int i=0;i<num_extruders;++i) - for (unsigned int j=0;j<num_extruders;++j) { - // append the value for this pair from the old matrix (if it's there): - if (i<old_number_of_extruders && j<old_number_of_extruders) - new_matrix.push_back(old_matrix[i*old_number_of_extruders + j]); - else - new_matrix.push_back( i==j ? 0. : extruders[2*i]+extruders[2*j+1]); // so it matches new extruder volumes - } - this->project_config.option<ConfigOptionFloats>("wiping_volumes_matrix")->values = new_matrix; - } -} - -void PresetBundle::update_compatible(PresetSelectCompatibleType select_other_print_if_incompatible, PresetSelectCompatibleType select_other_filament_if_incompatible) -{ - const Preset &printer_preset = this->printers.get_edited_preset(); - const PresetWithVendorProfile printer_preset_with_vendor_profile = this->printers.get_preset_with_vendor_profile(printer_preset); - - switch (printer_preset.printer_technology()) { - case ptFFF: - { - assert(printer_preset.config.has("default_print_profile")); - assert(printer_preset.config.has("default_filament_profile")); - const std::string &prefered_print_profile = printer_preset.config.opt_string("default_print_profile"); - const std::vector<std::string> &prefered_filament_profiles = printer_preset.config.option<ConfigOptionStrings>("default_filament_profile")->values; - prefered_print_profile.empty() ? - this->prints.update_compatible(printer_preset_with_vendor_profile, nullptr, select_other_print_if_incompatible) : - this->prints.update_compatible(printer_preset_with_vendor_profile, nullptr, select_other_print_if_incompatible, - [&prefered_print_profile](const std::string& profile_name) { return profile_name == prefered_print_profile; }); - const PresetWithVendorProfile print_preset_with_vendor_profile = this->prints.get_edited_preset_with_vendor_profile(); - // Remember whether the filament profiles were compatible before updating the filament compatibility. - std::vector<char> filament_preset_was_compatible(this->filament_presets.size(), false); - for (size_t idx = 0; idx < this->filament_presets.size(); ++ idx) { - std::string &filament_name = this->filament_presets[idx]; - Preset *preset = this->filaments.find_preset(filament_name, false); - filament_preset_was_compatible[idx] = preset != nullptr && preset->is_compatible; - } - prefered_filament_profiles.empty() ? - this->filaments.update_compatible(printer_preset_with_vendor_profile, &print_preset_with_vendor_profile, select_other_filament_if_incompatible) : - this->filaments.update_compatible(printer_preset_with_vendor_profile, &print_preset_with_vendor_profile, select_other_filament_if_incompatible, - [&prefered_filament_profiles](const std::string& profile_name) - { return std::find(prefered_filament_profiles.begin(), prefered_filament_profiles.end(), profile_name) != prefered_filament_profiles.end(); }); - if (select_other_filament_if_incompatible != PresetSelectCompatibleType::Never) { - // Verify validity of the current filament presets. - if (this->filament_presets.size() == 1) { - if (select_other_filament_if_incompatible == PresetSelectCompatibleType::Always || filament_preset_was_compatible.front()) - this->filament_presets.front() = this->filaments.get_edited_preset().name; - } else { - for (size_t idx = 0; idx < this->filament_presets.size(); ++ idx) { - std::string &filament_name = this->filament_presets[idx]; - Preset *preset = this->filaments.find_preset(filament_name, false); - if (preset == nullptr || (! preset->is_compatible && (select_other_filament_if_incompatible == PresetSelectCompatibleType::Always || filament_preset_was_compatible[idx]))) { - // Pick a compatible profile. If there are prefered_filament_profiles, use them. - if (prefered_filament_profiles.empty()) - filament_name = this->filaments.first_compatible().name; - else { - const std::string &preferred = (idx < prefered_filament_profiles.size()) ? - prefered_filament_profiles[idx] : prefered_filament_profiles.front(); - filament_name = this->filaments.first_compatible( - [&preferred](const std::string& profile_name) { return profile_name == preferred; }).name; - } - } - } - } - } - break; - } - case ptSLA: - { - assert(printer_preset.config.has("default_sla_print_profile")); - assert(printer_preset.config.has("default_sla_material_profile")); - const PresetWithVendorProfile sla_print_preset_with_vendor_profile = this->sla_prints.get_edited_preset_with_vendor_profile(); - const std::string &prefered_sla_print_profile = printer_preset.config.opt_string("default_sla_print_profile"); - (prefered_sla_print_profile.empty()) ? - this->sla_prints.update_compatible(printer_preset_with_vendor_profile, nullptr, select_other_print_if_incompatible) : - this->sla_prints.update_compatible(printer_preset_with_vendor_profile, nullptr, select_other_print_if_incompatible, - [&prefered_sla_print_profile](const std::string& profile_name){ return profile_name == prefered_sla_print_profile; }); - const std::string &prefered_sla_material_profile = printer_preset.config.opt_string("default_sla_material_profile"); - prefered_sla_material_profile.empty() ? - this->sla_materials.update_compatible(printer_preset_with_vendor_profile, &sla_print_preset_with_vendor_profile, select_other_filament_if_incompatible) : - this->sla_materials.update_compatible(printer_preset_with_vendor_profile, &sla_print_preset_with_vendor_profile, select_other_filament_if_incompatible, - [&prefered_sla_material_profile](const std::string& profile_name){ return profile_name == prefered_sla_material_profile; }); - break; - } - default: break; - } -} - -void PresetBundle::export_configbundle(const std::string &path, bool export_system_settings) -{ - boost::nowide::ofstream c; - c.open(path, std::ios::out | std::ios::trunc); - - // Put a comment at the first line including the time stamp and Slic3r version. - c << "# " << Slic3r::header_slic3r_generated() << std::endl; - - // Export the print, filament and printer profiles. - - for (const PresetCollection *presets : { - (const PresetCollection*)&this->prints, (const PresetCollection*)&this->filaments, - (const PresetCollection*)&this->sla_prints, (const PresetCollection*)&this->sla_materials, - (const PresetCollection*)&this->printers }) { - for (const Preset &preset : (*presets)()) { - if (preset.is_default || preset.is_external || (preset.is_system && ! export_system_settings)) - // Only export the common presets, not external files or the default preset. - continue; - c << std::endl << "[" << presets->section_name() << ":" << preset.name << "]" << std::endl; - for (const std::string &opt_key : preset.config.keys()) - c << opt_key << " = " << preset.config.opt_serialize(opt_key) << std::endl; - } - } - - // Export the names of the active presets. - c << std::endl << "[presets]" << std::endl; - c << "print = " << this->prints.get_selected_preset_name() << std::endl; - c << "sla_print = " << this->sla_prints.get_selected_preset_name() << std::endl; - c << "sla_material = " << this->sla_materials.get_selected_preset_name() << std::endl; - c << "printer = " << this->printers.get_selected_preset_name() << std::endl; - for (size_t i = 0; i < this->filament_presets.size(); ++ i) { - char suffix[64]; - if (i > 0) - sprintf(suffix, "_%d", (int)i); - else - suffix[0] = 0; - c << "filament" << suffix << " = " << this->filament_presets[i] << std::endl; - } - -#if 0 - // Export the following setting values from the provided setting repository. - static const char *settings_keys[] = { "autocenter" }; - c << "[settings]" << std::endl; - for (size_t i = 0; i < sizeof(settings_keys) / sizeof(settings_keys[0]); ++ i) - c << settings_keys[i] << " = " << settings.serialize(settings_keys[i]) << std::endl; -#endif - - c.close(); -} - -// Set the filament preset name. As the name could come from the UI selection box, -// an optional "(modified)" suffix will be removed from the filament name. -void PresetBundle::set_filament_preset(size_t idx, const std::string &name) -{ - if (name.find_first_of(PresetCollection::separator_head()) == 0) - return; - - if (idx >= filament_presets.size()) - filament_presets.resize(idx + 1, filaments.default_preset().name); - filament_presets[idx] = Preset::remove_suffix_modified(name); -} - -void PresetBundle::load_default_preset_bitmaps() -{ - // Clear bitmap cache, before load new scaled default preset bitmaps - m_bitmapCache->clear(); - this->prints.clear_bitmap_cache(); - this->sla_prints.clear_bitmap_cache(); - this->filaments.clear_bitmap_cache(); - this->sla_materials.clear_bitmap_cache(); - this->printers.clear_bitmap_cache(); - - this->prints.load_bitmap_default("cog"); - this->sla_prints.load_bitmap_default("cog"); - this->filaments.load_bitmap_default("spool.png"); - this->sla_materials.load_bitmap_default("resin"); - this->printers.load_bitmap_default("printer"); - this->printers.load_bitmap_add("add.png"); - this->load_compatible_bitmaps(); -} - -void PresetBundle::update_plater_filament_ui(unsigned int idx_extruder, GUI::PresetComboBox *ui) -{ - if (ui == nullptr || this->printers.get_edited_preset().printer_technology() == ptSLA || - this->filament_presets.size() <= idx_extruder ) - return; - - unsigned char rgb[3]; - std::string extruder_color = this->printers.get_edited_preset().config.opt_string("extruder_colour", idx_extruder); - if (!m_bitmapCache->parse_color(extruder_color, rgb)) - // Extruder color is not defined. - extruder_color.clear(); - - // Fill in the list from scratch. - ui->Freeze(); - ui->Clear(); - size_t selected_preset_item = INT_MAX; // some value meaning that no one item is selected - - const Preset *selected_preset = this->filaments.find_preset(this->filament_presets[idx_extruder]); - // Show wide icons if the currently selected preset is not compatible with the current printer, - // and draw a red flag in front of the selected preset. - bool wide_icons = selected_preset != nullptr && ! selected_preset->is_compatible && m_bitmapIncompatible != nullptr; - assert(selected_preset != nullptr); - std::map<wxString, wxBitmap*> nonsys_presets; - wxString selected_str = ""; - if (!this->filaments().front().is_visible) - ui->set_label_marker(ui->Append(PresetCollection::separator(L("System presets")), wxNullBitmap)); - - /* It's supposed that standard size of an icon is 16px*16px for 100% scaled display. - * So set sizes for solid_colored icons used for filament preset - * and scale them in respect to em_unit value - */ - const float scale_f = ui->em_unit() * 0.1f; - - // To avoid the errors of number rounding for different combination of monitor configuration, - // let use scaled 8px, as a smallest icon unit - const int icon_unit = 8 * scale_f + 0.5f; - const int normal_icon_width = 2 * icon_unit; //16 * scale_f + 0.5f; - const int thin_icon_width = icon_unit; //8 * scale_f + 0.5f; - const int wide_icon_width = 3 * icon_unit; //24 * scale_f + 0.5f; - - const int space_icon_width = 2 * scale_f + 0.5f; - - // To avoid asserts, each added bitmap to wxBitmapCombobox should be the same size, so - // set a bitmap height to m_bitmapLock->GetHeight() - // - // To avoid asserts, each added bitmap to wxBitmapCombobox should be the same size. - // But for some display scaling (for example 125% or 175%) normal_icon_width differs from icon width. - // So: - // for nonsystem presets set a width of empty bitmap to m_bitmapLock->GetWidth() - // for compatible presets set a width of empty bitmap to m_bitmapIncompatible->GetWidth() - // - // Note, under OSX we should use a Scaled Height/Width because of Retina scale -#ifdef __APPLE__ - const int icon_height = m_bitmapLock->GetScaledHeight(); - const int lock_icon_width = m_bitmapLock->GetScaledWidth(); - const int flag_icon_width = m_bitmapIncompatible->GetScaledWidth(); -#else - const int icon_height = m_bitmapLock->GetHeight(); - const int lock_icon_width = m_bitmapLock->GetWidth(); - const int flag_icon_width = m_bitmapIncompatible->GetWidth(); -#endif - - wxString tooltip = ""; - - for (int i = this->filaments().front().is_visible ? 0 : 1; i < int(this->filaments().size()); ++i) { - const Preset &preset = this->filaments.preset(i); - bool selected = this->filament_presets[idx_extruder] == preset.name; - if (! preset.is_visible || (! preset.is_compatible && ! selected)) - continue; - // Assign an extruder color to the selected item if the extruder color is defined. - std::string filament_rgb = preset.config.opt_string("filament_colour", 0); - std::string extruder_rgb = (selected && !extruder_color.empty()) ? extruder_color : filament_rgb; - bool single_bar = filament_rgb == extruder_rgb; - std::string bitmap_key = single_bar ? filament_rgb : filament_rgb + extruder_rgb; - // If the filament preset is not compatible and there is a "red flag" icon loaded, show it left - // to the filament color image. - if (wide_icons) - bitmap_key += preset.is_compatible ? ",cmpt" : ",ncmpt"; - bitmap_key += (preset.is_system || preset.is_default) ? ",syst" : ",nsyst"; - if (preset.is_dirty) - bitmap_key += ",drty"; - wxBitmap *bitmap = m_bitmapCache->find(bitmap_key); - if (bitmap == nullptr) { - // Create the bitmap with color bars. - std::vector<wxBitmap> bmps; - if (wide_icons) - // Paint a red flag for incompatible presets. - bmps.emplace_back(preset.is_compatible ? m_bitmapCache->mkclear(flag_icon_width, icon_height) : *m_bitmapIncompatible); - // Paint the color bars. - m_bitmapCache->parse_color(filament_rgb, rgb); - bmps.emplace_back(m_bitmapCache->mksolid(single_bar ? wide_icon_width : normal_icon_width, icon_height, rgb)); - if (! single_bar) { - m_bitmapCache->parse_color(extruder_rgb, rgb); - bmps.emplace_back(m_bitmapCache->mksolid(thin_icon_width, icon_height, rgb)); - } - // Paint a lock at the system presets. - bmps.emplace_back(m_bitmapCache->mkclear(space_icon_width, icon_height)); - bmps.emplace_back((preset.is_system || preset.is_default) ? *m_bitmapLock : m_bitmapCache->mkclear(lock_icon_width, icon_height)); -// (preset.is_dirty ? *m_bitmapLockOpen : *m_bitmapLock) : m_bitmapCache->mkclear(16, 16)); - bitmap = m_bitmapCache->insert(bitmap_key, bmps); - } - - const std::string name = preset.alias.empty() ? preset.name : preset.alias; - if (preset.is_default || preset.is_system) { - ui->Append(wxString::FromUTF8((/*preset.*/name + (preset.is_dirty ? Preset::suffix_modified() : "")).c_str()), - (bitmap == 0) ? wxNullBitmap : *bitmap); - if (selected || - // just in case: mark selected_preset_item as a first added element - selected_preset_item == INT_MAX ) { - selected_preset_item = ui->GetCount() - 1; - tooltip = wxString::FromUTF8(preset.name.c_str()); - } - } - else - { - nonsys_presets.emplace(wxString::FromUTF8((/*preset.*/name + (preset.is_dirty ? Preset::suffix_modified() : "")).c_str()), - (bitmap == 0) ? &wxNullBitmap : bitmap); - if (selected) { - selected_str = wxString::FromUTF8((/*preset.*/name + (preset.is_dirty ? Preset::suffix_modified() : "")).c_str()); - tooltip = wxString::FromUTF8(preset.name.c_str()); - } - } - if (preset.is_default) - ui->set_label_marker(ui->Append(PresetCollection::separator(L("System presets")), wxNullBitmap)); - } - - if (!nonsys_presets.empty()) - { - ui->set_label_marker(ui->Append(PresetCollection::separator(L("User presets")), wxNullBitmap)); - for (std::map<wxString, wxBitmap*>::iterator it = nonsys_presets.begin(); it != nonsys_presets.end(); ++it) { - ui->Append(it->first, *it->second); - if (it->first == selected_str || - // just in case: mark selected_preset_item as a first added element - selected_preset_item == INT_MAX) { - selected_preset_item = ui->GetCount() - 1; - } - } - } - - std::string bitmap_key = ""; - if (wide_icons) - bitmap_key += "wide,"; - bitmap_key += "edit_preset_list"; - wxBitmap* bmp = m_bitmapCache->find(bitmap_key); - if (bmp == nullptr) { - // Create the bitmap with color bars. - std::vector<wxBitmap> bmps; - if (wide_icons) - // Paint a red flag for incompatible presets. - bmps.emplace_back(m_bitmapCache->mkclear(flag_icon_width, icon_height)); - // Paint the color bars + a lock at the system presets. - bmps.emplace_back(m_bitmapCache->mkclear(wide_icon_width+space_icon_width, icon_height)); - bmps.emplace_back(create_scaled_bitmap("edit_uni")); - bmp = m_bitmapCache->insert(bitmap_key, bmps); - } - ui->set_label_marker(ui->Append(PresetCollection::separator(L("Add/Remove filaments")), *bmp), GUI::PresetComboBox::LABEL_ITEM_WIZARD_FILAMENTS); - - /* But, if selected_preset_item is still equal to INT_MAX, it means that - * there is no presets added to the list. - * So, select last combobox item ("Add/Remove filaments") - */ - if (selected_preset_item == INT_MAX) - selected_preset_item = ui->GetCount() - 1; - - ui->SetSelection(selected_preset_item); - ui->SetToolTip(tooltip.IsEmpty() ? ui->GetString(selected_preset_item) : tooltip); - ui->check_selection(selected_preset_item); - ui->Thaw(); - - // Update control min size after rescale (changed Display DPI under MSW) - if (ui->GetMinWidth() != 20 * ui->em_unit()) - ui->SetMinSize(wxSize(20 * ui->em_unit(), ui->GetSize().GetHeight())); -} - -void PresetBundle::set_default_suppressed(bool default_suppressed) -{ - prints.set_default_suppressed(default_suppressed); - filaments.set_default_suppressed(default_suppressed); - sla_prints.set_default_suppressed(default_suppressed); - sla_materials.set_default_suppressed(default_suppressed); - printers.set_default_suppressed(default_suppressed); -} - -} // namespace Slic3r diff --git a/src/slic3r/GUI/PresetBundle.hpp b/src/slic3r/GUI/PresetBundle.hpp deleted file mode 100644 index bf1bba21d..000000000 --- a/src/slic3r/GUI/PresetBundle.hpp +++ /dev/null @@ -1,185 +0,0 @@ -#ifndef slic3r_PresetBundle_hpp_ -#define slic3r_PresetBundle_hpp_ - -#include "AppConfig.hpp" -#include "Preset.hpp" - -#include <memory> -#include <set> -#include <unordered_map> -#include <boost/filesystem/path.hpp> - -class wxWindow; - -namespace Slic3r { - -namespace GUI { - class BitmapCache; -}; - -// Bundle of Print + Filament + Printer presets. -class PresetBundle -{ -public: - PresetBundle(); - ~PresetBundle(); - - // Remove all the presets but the "-- default --". - // Optionally remove all the files referenced by the presets from the user profile directory. - void reset(bool delete_files); - - void setup_directories(); - - // Load ini files of all types (print, filament, printer) from Slic3r::data_dir() / presets. - // Load selections (current print, current filaments, current printer) from config.ini - // This is done just once on application start up. - void load_presets(AppConfig &config, const std::string &preferred_model_id = ""); - - // Export selections (current print, current filaments, current printer) into config.ini - void export_selections(AppConfig &config); - - PresetCollection prints; - PresetCollection sla_prints; - PresetCollection filaments; - PresetCollection sla_materials; - PresetCollection& materials(PrinterTechnology pt) { return pt == ptFFF ? this->filaments : this->sla_materials; } - const PresetCollection& materials(PrinterTechnology pt) const { return pt == ptFFF ? this->filaments : this->sla_materials; } - PrinterPresetCollection printers; - // Filament preset names for a multi-extruder or multi-material print. - // extruders.size() should be the same as printers.get_edited_preset().config.nozzle_diameter.size() - std::vector<std::string> filament_presets; - - // The project configuration values are kept separated from the print/filament/printer preset, - // they are being serialized / deserialized from / to the .amf, .3mf, .config, .gcode, - // and they are being used by slicing core. - DynamicPrintConfig project_config; - - // There will be an entry for each system profile loaded, - // and the system profiles will point to the VendorProfile instances owned by PresetBundle::vendors. - VendorMap vendors; - - struct ObsoletePresets { - std::vector<std::string> prints; - std::vector<std::string> sla_prints; - std::vector<std::string> filaments; - std::vector<std::string> sla_materials; - std::vector<std::string> printers; - }; - ObsoletePresets obsolete_presets; - - bool has_defauls_only() const - { return prints.has_defaults_only() && filaments.has_defaults_only() && printers.has_defaults_only(); } - - DynamicPrintConfig full_config() const; - // full_config() with the "printhost_apikey" and "printhost_cafile" removed. - DynamicPrintConfig full_config_secure() const; - - // Load user configuration and store it into the user profiles. - // This method is called by the configuration wizard. - void load_config(const std::string &name, DynamicPrintConfig config) - { this->load_config_file_config(name, false, std::move(config)); } - - // Load configuration that comes from a model file containing configuration, such as 3MF et al. - // This method is called by the Plater. - void load_config_model(const std::string &name, DynamicPrintConfig config) - { this->load_config_file_config(name, true, std::move(config)); } - - // Load an external config file containing the print, filament and printer presets. - // Instead of a config file, a G-code may be loaded containing the full set of parameters. - // In the future the configuration will likely be read from an AMF file as well. - // If the file is loaded successfully, its print / filament / printer profiles will be activated. - void load_config_file(const std::string &path); - - // Load a config bundle file, into presets and store the loaded presets into separate files - // of the local configuration directory. - // Load settings into the provided settings instance. - // Activate the presets stored in the config bundle. - // Returns the number of presets loaded successfully. - enum { - // Save the profiles, which have been loaded. - LOAD_CFGBNDLE_SAVE = 1, - // Delete all old config profiles before loading. - LOAD_CFGBNDLE_RESET_USER_PROFILE = 2, - // Load a system config bundle. - LOAD_CFGBNDLE_SYSTEM = 4, - LOAD_CFGBUNDLE_VENDOR_ONLY = 8, - }; - // Load the config bundle, store it to the user profile directory by default. - size_t load_configbundle(const std::string &path, unsigned int flags = LOAD_CFGBNDLE_SAVE); - - // Export a config bundle file containing all the presets and the names of the active presets. - void export_configbundle(const std::string &path, bool export_system_settings = false); - - // Update a filament selection combo box on the plater for an idx_extruder. - void update_plater_filament_ui(unsigned int idx_extruder, GUI::PresetComboBox *ui); - - // Enable / disable the "- default -" preset. - void set_default_suppressed(bool default_suppressed); - - // Set the filament preset name. As the name could come from the UI selection box, - // an optional "(modified)" suffix will be removed from the filament name. - void set_filament_preset(size_t idx, const std::string &name); - - // Read out the number of extruders from an active printer preset, - // update size and content of filament_presets. - void update_multi_material_filament_presets(); - - // Update the is_compatible flag of all print and filament presets depending on whether they are marked - // as compatible with the currently selected printer (and print in case of filament presets). - // Also updates the is_visible flag of each preset. - // If select_other_if_incompatible is true, then the print or filament preset is switched to some compatible - // preset if the current print or filament preset is not compatible. - void update_compatible(PresetSelectCompatibleType select_other_print_if_incompatible, PresetSelectCompatibleType select_other_filament_if_incompatible); - void update_compatible(PresetSelectCompatibleType select_other_if_incompatible) { this->update_compatible(select_other_if_incompatible, select_other_if_incompatible); } - - void load_default_preset_bitmaps(); - - // Set the is_visible flag for printer vendors, printer models and printer variants - // based on the user configuration. - // If the "vendor" section is missing, enable all models and variants of the particular vendor. - void load_installed_printers(const AppConfig &config); - - const std::string& get_preset_name_by_alias(const Preset::Type& preset_type, const std::string& alias) const; - - static const char *PRUSA_BUNDLE; -private: - std::string load_system_presets(); - // Merge one vendor's presets with the other vendor's presets, report duplicates. - std::vector<std::string> merge_presets(PresetBundle &&other); - // Update renamed_from and alias maps of system profiles. - void update_system_maps(); - - // Set the is_visible flag for filaments and sla materials, - // apply defaults based on enabled printers when no filaments/materials are installed. - void load_installed_filaments(AppConfig &config); - void load_installed_sla_materials(AppConfig &config); - - // Load selections (current print, current filaments, current printer) from config.ini - // This is done just once on application start up. - void load_selections(AppConfig &config, const std::string &preferred_model_id = ""); - - // Load print, filament & printer presets from a config. If it is an external config, then the name is extracted from the external path. - // and the external config is just referenced, not stored into user profile directory. - // If it is not an external config, then the config will be stored into the user profile directory. - void load_config_file_config(const std::string &name_or_path, bool is_external, DynamicPrintConfig &&config); - void load_config_file_config_bundle(const std::string &path, const boost::property_tree::ptree &tree); - void load_compatible_bitmaps(); - - DynamicPrintConfig full_fff_config() const; - DynamicPrintConfig full_sla_config() const; - - // Indicator, that the preset is compatible with the selected printer. - wxBitmap *m_bitmapCompatible; - // Indicator, that the preset is NOT compatible with the selected printer. - wxBitmap *m_bitmapIncompatible; - // Indicator, that the preset is system and not modified. - wxBitmap *m_bitmapLock; - // Indicator, that the preset is system and user modified. - wxBitmap *m_bitmapLockOpen; - // Caching color bitmaps for the filament combo box. - GUI::BitmapCache *m_bitmapCache; -}; - -} // namespace Slic3r - -#endif /* slic3r_PresetBundle_hpp_ */ diff --git a/src/slic3r/GUI/PresetComboBoxes.cpp b/src/slic3r/GUI/PresetComboBoxes.cpp new file mode 100644 index 000000000..33ae4f54e --- /dev/null +++ b/src/slic3r/GUI/PresetComboBoxes.cpp @@ -0,0 +1,1331 @@ +#include "PresetComboBoxes.hpp" + +#include <cstddef> +#include <vector> +#include <string> +#include <boost/algorithm/string.hpp> + +#include <wx/sizer.h> +#include <wx/stattext.h> +#include <wx/textctrl.h> +#include <wx/button.h> +#include <wx/statbox.h> +#include <wx/colordlg.h> +#include <wx/wupdlock.h> +#include <wx/menu.h> + +#include "libslic3r/libslic3r.h" +#include "libslic3r/PrintConfig.hpp" +#include "libslic3r/PresetBundle.hpp" + +#include "GUI.hpp" +#include "GUI_App.hpp" +#include "Plater.hpp" +#include "MainFrame.hpp" +#include "format.hpp" +#include "Tab.hpp" +#include "ConfigWizard.hpp" +#include "../Utils/ASCIIFolding.hpp" +#include "../Utils/FixModelByWin10.hpp" +#include "../Utils/UndoRedo.hpp" +#include "BitmapCache.hpp" +#include "PhysicalPrinterDialog.hpp" + +using Slic3r::GUI::format_wxstr; + +namespace Slic3r { +namespace GUI { + +#define BORDER_W 10 + +// --------------------------------- +// *** PresetComboBox *** +// --------------------------------- + +/* For PresetComboBox we use bitmaps that are created from images that are already scaled appropriately for Retina + * (Contrary to the intuition, the `scale` argument for Bitmap's constructor doesn't mean + * "please scale this to such and such" but rather + * "the wxImage is already sized for backing scale such and such". ) + * Unfortunately, the constructor changes the size of wxBitmap too. + * Thus We need to use unscaled size value for bitmaps that we use + * to avoid scaled size of control items. + * For this purpose control drawing methods and + * control size calculation methods (virtual) are overridden. + **/ + +PresetComboBox::PresetComboBox(wxWindow* parent, Preset::Type preset_type, const wxSize& size) : + wxBitmapComboBox(parent, wxID_ANY, wxEmptyString, wxDefaultPosition, size, 0, nullptr, wxCB_READONLY), + m_type(preset_type), + m_last_selected(wxNOT_FOUND), + m_em_unit(em_unit(this)), + m_preset_bundle(wxGetApp().preset_bundle) +{ + SetFont(wxGetApp().normal_font()); +#ifdef _WIN32 + // Workaround for ignoring CBN_EDITCHANGE events, which are processed after the content of the combo box changes, so that + // the index of the item inside CBN_EDITCHANGE may no more be valid. + EnableTextChangedEvents(false); +#endif /* _WIN32 */ + + switch (m_type) + { + case Preset::TYPE_PRINT: { + m_collection = &m_preset_bundle->prints; + m_main_bitmap_name = "cog"; + break; + } + case Preset::TYPE_FILAMENT: { + m_collection = &m_preset_bundle->filaments; + m_main_bitmap_name = "spool"; + break; + } + case Preset::TYPE_SLA_PRINT: { + m_collection = &m_preset_bundle->sla_prints; + m_main_bitmap_name = "cog"; + break; + } + case Preset::TYPE_SLA_MATERIAL: { + m_collection = &m_preset_bundle->sla_materials; + m_main_bitmap_name = "resin"; + break; + } + case Preset::TYPE_PRINTER: { + m_collection = &m_preset_bundle->printers; + m_main_bitmap_name = "printer"; + break; + } + default: break; + } + + m_bitmapCompatible = ScalableBitmap(this, "flag_green"); + m_bitmapIncompatible = ScalableBitmap(this, "flag_red"); + + // parameters for an icon's drawing + fill_width_height(); + + Bind(wxEVT_COMBOBOX, [this](wxCommandEvent& evt) { + // see https://github.com/prusa3d/PrusaSlicer/issues/3889 + // Under OSX: in case of use of a same names written in different case (like "ENDER" and "Ender") + // m_presets_choice->GetSelection() will return first item, because search in PopupListCtrl is case-insensitive. + // So, use GetSelection() from event parameter + auto selected_item = evt.GetSelection(); + + auto marker = reinterpret_cast<Marker>(this->GetClientData(selected_item)); + if (marker >= LABEL_ITEM_DISABLED && marker < LABEL_ITEM_MAX) + this->SetSelection(this->m_last_selected); + else if (on_selection_changed && (m_last_selected != selected_item || m_collection->current_is_dirty())) { + m_last_selected = selected_item; + on_selection_changed(selected_item); + evt.StopPropagation(); + } + evt.Skip(); + }); +} + +PresetComboBox::~PresetComboBox() +{ +} + +BitmapCache& PresetComboBox::bitmap_cache() +{ + static BitmapCache bmps; + return bmps; +} + +void PresetComboBox::set_label_marker(int item, LabelItemType label_item_type) +{ + this->SetClientData(item, (void*)label_item_type); +} + +bool PresetComboBox::set_printer_technology(PrinterTechnology pt) +{ + if (printer_technology != pt) { + printer_technology = pt; + return true; + } + return false; +} + +void PresetComboBox::invalidate_selection() +{ + m_last_selected = INT_MAX; // this value means that no one item is selected +} + +void PresetComboBox::validate_selection(bool predicate/*=false*/) +{ + if (predicate || + // just in case: mark m_last_selected as a first added element + m_last_selected == INT_MAX) + m_last_selected = GetCount() - 1; +} + +void PresetComboBox::update_selection() +{ + /* If selected_preset_item is still equal to INT_MAX, it means that + * there is no presets added to the list. + * So, select last combobox item ("Add/Remove preset") + */ + validate_selection(); + + SetSelection(m_last_selected); + SetToolTip(GetString(m_last_selected)); +} + +void PresetComboBox::update(std::string select_preset_name) +{ + Freeze(); + Clear(); + invalidate_selection(); + + const std::deque<Preset>& presets = m_collection->get_presets(); + + std::map<wxString, std::pair<wxBitmap*, bool>> nonsys_presets; + std::map<wxString, wxBitmap*> incomp_presets; + + wxString selected = ""; + if (!presets.front().is_visible) + set_label_marker(Append(separator(L("System presets")), wxNullBitmap)); + + for (size_t i = presets.front().is_visible ? 0 : m_collection->num_default_presets(); i < presets.size(); ++i) + { + const Preset& preset = presets[i]; + if (!preset.is_visible || !preset.is_compatible) + continue; + + // marker used for disable incompatible printer models for the selected physical printer + bool is_enabled = m_type == Preset::TYPE_PRINTER && printer_technology != ptAny ? preset.printer_technology() == printer_technology : true; + if (select_preset_name.empty() && is_enabled) + select_preset_name = preset.name; + + std::string bitmap_key = "cb"; + if (m_type == Preset::TYPE_PRINTER) { + bitmap_key += "_printer"; + if (preset.printer_technology() == ptSLA) + bitmap_key += "_sla"; + } + std::string main_icon_name = m_type == Preset::TYPE_PRINTER && preset.printer_technology() == ptSLA ? "sla_printer" : m_main_bitmap_name; + + wxBitmap* bmp = get_bmp(bitmap_key, main_icon_name, "lock_closed", is_enabled, preset.is_compatible, preset.is_system || preset.is_default); + assert(bmp); + + if (!is_enabled) + incomp_presets.emplace(wxString::FromUTF8((preset.name + (preset.is_dirty ? Preset::suffix_modified() : "")).c_str()), bmp); + else if (preset.is_default || preset.is_system) + { + Append(wxString::FromUTF8((preset.name + (preset.is_dirty ? Preset::suffix_modified() : "")).c_str()), *bmp); + validate_selection(preset.name == select_preset_name); + } + else + { + nonsys_presets.emplace(wxString::FromUTF8((preset.name + (preset.is_dirty ? Preset::suffix_modified() : "")).c_str()), std::pair<wxBitmap*, bool>(bmp, is_enabled)); + if (preset.name == select_preset_name || (select_preset_name.empty() && is_enabled)) + selected = wxString::FromUTF8((preset.name + (preset.is_dirty ? Preset::suffix_modified() : "")).c_str()); + } + if (i + 1 == m_collection->num_default_presets()) + set_label_marker(Append(separator(L("System presets")), wxNullBitmap)); + } + if (!nonsys_presets.empty()) + { + set_label_marker(Append(separator(L("User presets")), wxNullBitmap)); + for (std::map<wxString, std::pair<wxBitmap*, bool>>::iterator it = nonsys_presets.begin(); it != nonsys_presets.end(); ++it) { + int item_id = Append(it->first, *it->second.first); + bool is_enabled = it->second.second; + if (!is_enabled) + set_label_marker(item_id, LABEL_ITEM_DISABLED); + validate_selection(it->first == selected); + } + } + if (!incomp_presets.empty()) + { + set_label_marker(Append(separator(L("Incompatible presets")), wxNullBitmap)); + for (std::map<wxString, wxBitmap*>::iterator it = incomp_presets.begin(); it != incomp_presets.end(); ++it) { + set_label_marker(Append(it->first, *it->second), LABEL_ITEM_DISABLED); + } + } + + update_selection(); + Thaw(); +} + +void PresetComboBox::update() +{ + this->update(into_u8(this->GetString(this->GetSelection()))); +} + +void PresetComboBox::msw_rescale() +{ + m_em_unit = em_unit(this); + + m_bitmapIncompatible.msw_rescale(); + m_bitmapCompatible.msw_rescale(); + + // parameters for an icon's drawing + fill_width_height(); + + // update the control to redraw the icons + update(); +} + +void PresetComboBox::fill_width_height() +{ + // To avoid asserts, each added bitmap to wxBitmapCombobox should be the same size, so + // set a bitmap's height to m_bitmapCompatible->GetHeight() and norm_icon_width to m_bitmapCompatible->GetWidth() + icon_height = m_bitmapCompatible.GetBmpHeight(); + norm_icon_width = m_bitmapCompatible.GetBmpWidth(); + + /* It's supposed that standard size of an icon is 16px*16px for 100% scaled display. + * So set sizes for solid_colored icons used for filament preset + * and scale them in respect to em_unit value + */ + const float scale_f = (float)m_em_unit * 0.1f; + + thin_icon_width = lroundf(8 * scale_f); // analogue to 8px; + wide_icon_width = norm_icon_width + thin_icon_width; + + space_icon_width = lroundf(2 * scale_f); + thin_space_icon_width = 2 * space_icon_width; + wide_space_icon_width = 3 * space_icon_width; +} + +wxString PresetComboBox::separator(const std::string& label) +{ + return wxString::FromUTF8(separator_head()) + _(label) + wxString::FromUTF8(separator_tail()); +} + +wxBitmap* PresetComboBox::get_bmp( std::string bitmap_key, bool wide_icons, const std::string& main_icon_name, + bool is_compatible/* = true*/, bool is_system/* = false*/, bool is_single_bar/* = false*/, + std::string filament_rgb/* = ""*/, std::string extruder_rgb/* = ""*/) +{ + // If the filament preset is not compatible and there is a "red flag" icon loaded, show it left + // to the filament color image. + if (wide_icons) + bitmap_key += is_compatible ? ",cmpt" : ",ncmpt"; + + bitmap_key += is_system ? ",syst" : ",nsyst"; + bitmap_key += ",h" + std::to_string(icon_height); + + wxBitmap* bmp = bitmap_cache().find(bitmap_key); + if (bmp == nullptr) { + // Create the bitmap with color bars. + std::vector<wxBitmap> bmps; + if (wide_icons) + // Paint a red flag for incompatible presets. + bmps.emplace_back(is_compatible ? bitmap_cache().mkclear(norm_icon_width, icon_height) : m_bitmapIncompatible.bmp()); + + if (m_type == Preset::TYPE_FILAMENT) + { + unsigned char rgb[3]; + // Paint the color bars. + bitmap_cache().parse_color(filament_rgb, rgb); + bmps.emplace_back(bitmap_cache().mksolid(is_single_bar ? wide_icon_width : norm_icon_width, icon_height, rgb)); + if (!is_single_bar) { + bitmap_cache().parse_color(extruder_rgb, rgb); + bmps.emplace_back(bitmap_cache().mksolid(thin_icon_width, icon_height, rgb)); + } + // Paint a lock at the system presets. + bmps.emplace_back(bitmap_cache().mkclear(space_icon_width, icon_height)); + } + else + { + // Paint the color bars. + bmps.emplace_back(bitmap_cache().mkclear(thin_space_icon_width, icon_height)); + bmps.emplace_back(create_scaled_bitmap(main_icon_name)); + // Paint a lock at the system presets. + bmps.emplace_back(bitmap_cache().mkclear(wide_space_icon_width, icon_height)); + } + bmps.emplace_back(is_system ? create_scaled_bitmap("lock_closed") : bitmap_cache().mkclear(norm_icon_width, icon_height)); + bmp = bitmap_cache().insert(bitmap_key, bmps); + } + + return bmp; +} + +wxBitmap* PresetComboBox::get_bmp( std::string bitmap_key, const std::string& main_icon_name, const std::string& next_icon_name, + bool is_enabled/* = true*/, bool is_compatible/* = true*/, bool is_system/* = false*/) +{ + bitmap_key += !is_enabled ? "_disabled" : ""; + bitmap_key += is_compatible ? ",cmpt" : ",ncmpt"; + bitmap_key += is_system ? ",syst" : ",nsyst"; + bitmap_key += ",h" + std::to_string(icon_height); + + wxBitmap* bmp = bitmap_cache().find(bitmap_key); + if (bmp == nullptr) { + // Create the bitmap with color bars. + std::vector<wxBitmap> bmps; + bmps.emplace_back(m_type == Preset::TYPE_PRINTER ? create_scaled_bitmap(main_icon_name, this, 16, !is_enabled) : + is_compatible ? m_bitmapCompatible.bmp() : m_bitmapIncompatible.bmp()); + // Paint a lock at the system presets. + bmps.emplace_back(is_system ? create_scaled_bitmap(next_icon_name, this, 16, !is_enabled) : bitmap_cache().mkclear(norm_icon_width, icon_height)); + bmp = bitmap_cache().insert(bitmap_key, bmps); + } + + return bmp; +} + +bool PresetComboBox::is_selected_physical_printer() +{ + auto selected_item = this->GetSelection(); + auto marker = reinterpret_cast<Marker>(this->GetClientData(selected_item)); + return marker == LABEL_ITEM_PHYSICAL_PRINTER; +} + +bool PresetComboBox::selection_is_changed_according_to_physical_printers() +{ + if (m_type != Preset::TYPE_PRINTER || !is_selected_physical_printer()) + return false; + + PhysicalPrinterCollection& physical_printers = m_preset_bundle->physical_printers; + + std::string selected_string = this->GetString(this->GetSelection()).ToUTF8().data(); + + std::string old_printer_full_name, old_printer_preset; + if (physical_printers.has_selection()) { + old_printer_full_name = physical_printers.get_selected_full_printer_name(); + old_printer_preset = physical_printers.get_selected_printer_preset_name(); + } + else + old_printer_preset = m_collection->get_edited_preset().name; + // Select related printer preset on the Printer Settings Tab + physical_printers.select_printer(selected_string); + std::string preset_name = physical_printers.get_selected_printer_preset_name(); + + // 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<PlaterPresetComboBox*>(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<TabPresetComboBox*>(this)!=nullptr) + wxGetApp().sidebar().update_presets(m_type); + + this->update(); + return true; + } + + Tab* tab = wxGetApp().get_tab(Preset::TYPE_PRINTER); + if (tab) + tab->select_preset(preset_name, false, old_printer_full_name); + return true; +} + +#ifdef __APPLE__ +bool PresetComboBox::OnAddBitmap(const wxBitmap& bitmap) +{ + if (bitmap.IsOk()) + { + // we should use scaled! size values of bitmap + int width = (int)bitmap.GetScaledWidth(); + int height = (int)bitmap.GetScaledHeight(); + + if (m_usedImgSize.x < 0) + { + // If size not yet determined, get it from this image. + m_usedImgSize.x = width; + m_usedImgSize.y = height; + + // Adjust control size to vertically fit the bitmap + wxWindow* ctrl = GetControl(); + ctrl->InvalidateBestSize(); + wxSize newSz = ctrl->GetBestSize(); + wxSize sz = ctrl->GetSize(); + if (newSz.y > sz.y) + ctrl->SetSize(sz.x, newSz.y); + else + DetermineIndent(); + } + + wxCHECK_MSG(width == m_usedImgSize.x && height == m_usedImgSize.y, + false, + "you can only add images of same size"); + + return true; + } + + return false; +} + +void PresetComboBox::OnDrawItem(wxDC& dc, + const wxRect& rect, + int item, + int flags) const +{ + const wxBitmap& bmp = *(wxBitmap*)m_bitmaps[item]; + if (bmp.IsOk()) + { + // we should use scaled! size values of bitmap + wxCoord w = bmp.GetScaledWidth(); + wxCoord h = bmp.GetScaledHeight(); + + const int imgSpacingLeft = 4; + + // Draw the image centered + dc.DrawBitmap(bmp, + rect.x + (m_usedImgSize.x - w) / 2 + imgSpacingLeft, + rect.y + (rect.height - h) / 2, + true); + } + + wxString text = GetString(item); + if (!text.empty()) + dc.DrawText(text, + rect.x + m_imgAreaWidth + 1, + rect.y + (rect.height - dc.GetCharHeight()) / 2); +} +#endif + + +// --------------------------------- +// *** PlaterPresetComboBox *** +// --------------------------------- + +PlaterPresetComboBox::PlaterPresetComboBox(wxWindow *parent, Preset::Type preset_type) : + PresetComboBox(parent, preset_type, wxSize(15 * wxGetApp().em_unit(), -1)) +{ + Bind(wxEVT_COMBOBOX, [this](wxCommandEvent &evt) { + auto selected_item = evt.GetSelection(); + + auto marker = reinterpret_cast<Marker>(this->GetClientData(selected_item)); + if (marker >= LABEL_ITEM_MARKER && marker < LABEL_ITEM_MAX) { + this->SetSelection(this->m_last_selected); + evt.StopPropagation(); + if (marker == LABEL_ITEM_WIZARD_PRINTERS) + show_add_menu(); + else + { + ConfigWizard::StartPage sp = ConfigWizard::SP_WELCOME; + switch (marker) { + case LABEL_ITEM_WIZARD_FILAMENTS: sp = ConfigWizard::SP_FILAMENTS; break; + case LABEL_ITEM_WIZARD_MATERIALS: sp = ConfigWizard::SP_MATERIALS; break; + default: break; + } + wxTheApp->CallAfter([sp]() { wxGetApp().run_wizard(ConfigWizard::RR_USER, sp); }); + } + } else if (marker == LABEL_ITEM_PHYSICAL_PRINTER || this->m_last_selected != selected_item || m_collection->current_is_dirty() ) { + this->m_last_selected = selected_item; + evt.SetInt(this->m_type); + evt.Skip(); + } else { + evt.StopPropagation(); + } + }); + + if (m_type == Preset::TYPE_FILAMENT) + { + Bind(wxEVT_LEFT_DOWN, [this](wxMouseEvent &event) { + const Preset* selected_preset = m_collection->find_preset(m_preset_bundle->filament_presets[m_extruder_idx]); + // Wide icons are shown if the currently selected preset is not compatible with the current printer, + // and red flag is drown in front of the selected preset. + bool wide_icons = selected_preset != nullptr && !selected_preset->is_compatible; + float scale = m_em_unit*0.1f; + + int shifl_Left = wide_icons ? int(scale * 16 + 0.5) : 0; +#if defined(wxBITMAPCOMBOBOX_OWNERDRAWN_BASED) + shifl_Left += int(scale * 4 + 0.5f); // IMAGE_SPACING_RIGHT = 4 for wxBitmapComboBox -> Space left of image +#endif + int icon_right_pos = shifl_Left + int(scale * (24+4) + 0.5); + int mouse_pos = event.GetLogicalPosition(wxClientDC(this)).x; + if (mouse_pos < shifl_Left || mouse_pos > icon_right_pos ) { + // Let the combo box process the mouse click. + event.Skip(); + return; + } + + // Swallow the mouse click and open the color picker. + + // get current color + DynamicPrintConfig* cfg = wxGetApp().get_tab(Preset::TYPE_PRINTER)->get_config(); + auto colors = static_cast<ConfigOptionStrings*>(cfg->option("extruder_colour")->clone()); + wxColour clr(colors->values[m_extruder_idx]); + if (!clr.IsOk()) + clr = wxColour(0,0,0); // Don't set alfa to transparence + + auto data = new wxColourData(); + data->SetChooseFull(1); + data->SetColour(clr); + + wxColourDialog dialog(this, data); + dialog.CenterOnParent(); + if (dialog.ShowModal() == wxID_OK) + { + colors->values[m_extruder_idx] = dialog.GetColourData().GetColour().GetAsString(wxC2S_HTML_SYNTAX).ToStdString(); + + DynamicPrintConfig cfg_new = *cfg; + cfg_new.set_key_value("extruder_colour", colors); + + wxGetApp().get_tab(Preset::TYPE_PRINTER)->load_config(cfg_new); + this->update(); + wxGetApp().plater()->on_config_change(cfg_new); + } + }); + } + + edit_btn = new ScalableButton(parent, wxID_ANY, "cog"); + edit_btn->SetToolTip(_L("Click to edit preset")); + + edit_btn->Bind(wxEVT_BUTTON, [this](wxCommandEvent) + { + // In a case of a physical printer, for its editing open PhysicalPrinterDialog + if (m_type == Preset::TYPE_PRINTER/* && this->is_selected_physical_printer()*/) { + this->show_edit_menu(); + return; + } + + if (!switch_to_tab()) + return; + + /* In a case of a multi-material printing, for editing another Filament Preset + * it's needed to select this preset for the "Filament settings" Tab + */ + if (m_type == Preset::TYPE_FILAMENT && wxGetApp().extruders_edited_cnt() > 1) + { + const std::string& selected_preset = GetString(GetSelection()).ToUTF8().data(); + + // Call select_preset() only if there is new preset and not just modified + if ( !boost::algorithm::ends_with(selected_preset, Preset::suffix_modified()) ) + { + const std::string& preset_name = wxGetApp().preset_bundle->filaments.get_preset_name_by_alias(selected_preset); + wxGetApp().get_tab(m_type)->select_preset(preset_name); + } + } + }); +} + +PlaterPresetComboBox::~PlaterPresetComboBox() +{ + if (edit_btn) + edit_btn->Destroy(); +} + +bool PlaterPresetComboBox::switch_to_tab() +{ + Tab* tab = wxGetApp().get_tab(m_type); + if (!tab) + return false; + + int page_id = wxGetApp().tab_panel()->FindPage(tab); + if (page_id == wxNOT_FOUND) + return false; + + wxGetApp().tab_panel()->SetSelection(page_id); + // Switch to Settings NotePad + wxGetApp().mainframe->select_tab(); + return true; +} + +void PlaterPresetComboBox::show_add_menu() +{ + wxMenu* menu = new wxMenu(); + + append_menu_item(menu, wxID_ANY, _L("Add/Remove presets"), "", + [this](wxCommandEvent&) { + wxTheApp->CallAfter([]() { wxGetApp().run_wizard(ConfigWizard::RR_USER, ConfigWizard::SP_PRINTERS); }); + }, "edit_uni", menu, []() { return true; }, wxGetApp().plater()); + + append_menu_item(menu, wxID_ANY, _L("Add physical printer"), "", + [this](wxCommandEvent&) { + PhysicalPrinterDialog dlg(wxEmptyString); + if (dlg.ShowModal() == wxID_OK) + update(); + }, "edit_uni", menu, []() { return true; }, wxGetApp().plater()); + + wxGetApp().plater()->PopupMenu(menu); +} + +void PlaterPresetComboBox::show_edit_menu() +{ + wxMenu* menu = new wxMenu(); + + append_menu_item(menu, wxID_ANY, _L("Edit preset"), "", + [this](wxCommandEvent&) { this->switch_to_tab(); }, "cog", menu, []() { return true; }, wxGetApp().plater()); + + if (this->is_selected_physical_printer()) { + append_menu_item(menu, wxID_ANY, _L("Edit physical printer"), "", + [this](wxCommandEvent&) { + PhysicalPrinterDialog dlg(this->GetString(this->GetSelection())); + if (dlg.ShowModal() == wxID_OK) + update(); + }, "cog", menu, []() { return true; }, wxGetApp().plater()); + + append_menu_item(menu, wxID_ANY, _L("Delete physical printer"), "", + [this](wxCommandEvent&) { + const std::string& printer_name = m_preset_bundle->physical_printers.get_selected_full_printer_name(); + if (printer_name.empty()) + return; + + const wxString msg = from_u8((boost::format(_u8L("Are you sure you want to delete \"%1%\" printer?")) % printer_name).str()); + if (wxMessageDialog(this, msg, _L("Delete Physical Printer"), wxYES_NO | wxNO_DEFAULT | wxICON_QUESTION).ShowModal() != wxID_YES) + return; + + m_preset_bundle->physical_printers.delete_selected_printer(); + + wxGetApp().get_tab(m_type)->update_preset_choice(); + update(); + }, "cross", menu, []() { return true; }, wxGetApp().plater()); + } + else + append_menu_item(menu, wxID_ANY, _L("Add/Remove presets"), "", + [this](wxCommandEvent&) { + wxTheApp->CallAfter([]() { wxGetApp().run_wizard(ConfigWizard::RR_USER, ConfigWizard::SP_PRINTERS); }); + }, "edit_uni", menu, []() { return true; }, wxGetApp().plater()); + + append_menu_item(menu, wxID_ANY, _L("Add physical printer"), "", + [this](wxCommandEvent&) { + PhysicalPrinterDialog dlg(wxEmptyString); + if (dlg.ShowModal() == wxID_OK) + update(); + }, "edit_uni", menu, []() { return true; }, wxGetApp().plater()); + + wxGetApp().plater()->PopupMenu(menu); +} + +// Only the compatible presets are shown. +// If an incompatible preset is selected, it is shown as well. +void PlaterPresetComboBox::update() +{ + if (m_type == Preset::TYPE_FILAMENT && + (m_preset_bundle->printers.get_edited_preset().printer_technology() == ptSLA || + m_preset_bundle->filament_presets.size() <= m_extruder_idx) ) + return; + + // Otherwise fill in the list from scratch. + this->Freeze(); + this->Clear(); + invalidate_selection(); + + const Preset* selected_filament_preset; + std::string extruder_color; + if (m_type == Preset::TYPE_FILAMENT) + { + unsigned char rgb[3]; + extruder_color = m_preset_bundle->printers.get_edited_preset().config.opt_string("extruder_colour", (unsigned int)m_extruder_idx); + if (!bitmap_cache().parse_color(extruder_color, rgb)) + // Extruder color is not defined. + extruder_color.clear(); + selected_filament_preset = m_collection->find_preset(m_preset_bundle->filament_presets[m_extruder_idx]); + assert(selected_filament_preset); + } + + const Preset& selected_preset = m_type == Preset::TYPE_FILAMENT ? *selected_filament_preset : m_collection->get_selected_preset(); + // Show wide icons if the currently selected preset is not compatible with the current printer, + // and draw a red flag in front of the selected preset. + bool wide_icons = !selected_preset.is_compatible; + + std::map<wxString, wxBitmap*> nonsys_presets; + + wxString selected = ""; + wxString tooltip = ""; + const std::deque<Preset>& presets = m_collection->get_presets(); + + if (!presets.front().is_visible) + this->set_label_marker(this->Append(separator(L("System presets")), wxNullBitmap)); + + for (size_t i = presets.front().is_visible ? 0 : m_collection->num_default_presets(); i < presets.size(); ++i) + { + const Preset& preset = presets[i]; + bool is_selected = m_type == Preset::TYPE_FILAMENT ? + m_preset_bundle->filament_presets[m_extruder_idx] == preset.name : + // The case, when some physical printer is selected + m_type == Preset::TYPE_PRINTER && m_preset_bundle->physical_printers.has_selection() ? false : + i == m_collection->get_selected_idx(); + + if (!preset.is_visible || (!preset.is_compatible && !is_selected)) + continue; + + std::string bitmap_key, filament_rgb, extruder_rgb; + std::string bitmap_type_name = bitmap_key = m_type == Preset::TYPE_PRINTER && preset.printer_technology() == ptSLA ? "sla_printer" : m_main_bitmap_name; + + bool single_bar = false; + if (m_type == Preset::TYPE_FILAMENT) + { + // Assign an extruder color to the selected item if the extruder color is defined. + filament_rgb = preset.config.opt_string("filament_colour", 0); + extruder_rgb = (selected && !extruder_color.empty()) ? extruder_color : filament_rgb; + single_bar = filament_rgb == extruder_rgb; + + bitmap_key += single_bar ? filament_rgb : filament_rgb + extruder_rgb; + } + + wxBitmap* bmp = get_bmp(bitmap_key, wide_icons, bitmap_type_name, + preset.is_compatible, preset.is_system || preset.is_default, + single_bar, filament_rgb, extruder_rgb); + assert(bmp); + + const std::string name = preset.alias.empty() ? preset.name : preset.alias; + if (preset.is_default || preset.is_system) { + Append(wxString::FromUTF8((name + (preset.is_dirty ? Preset::suffix_modified() : "")).c_str()), *bmp); + validate_selection(is_selected); + if (is_selected) + tooltip = wxString::FromUTF8(preset.name.c_str()); + } + else + { + nonsys_presets.emplace(wxString::FromUTF8((name + (preset.is_dirty ? Preset::suffix_modified() : "")).c_str()), bmp); + if (is_selected) { + selected = wxString::FromUTF8((name + (preset.is_dirty ? Preset::suffix_modified() : "")).c_str()); + tooltip = wxString::FromUTF8(preset.name.c_str()); + } + } + if (i + 1 == m_collection->num_default_presets()) + set_label_marker(Append(separator(L("System presets")), wxNullBitmap)); + } + if (!nonsys_presets.empty()) + { + set_label_marker(Append(separator(L("User presets")), wxNullBitmap)); + for (std::map<wxString, wxBitmap*>::iterator it = nonsys_presets.begin(); it != nonsys_presets.end(); ++it) { + Append(it->first, *it->second); + validate_selection(it->first == selected); + } + } + + if (m_type == Preset::TYPE_PRINTER) + { + // add Physical printers, if any exists + if (!m_preset_bundle->physical_printers.empty()) { + set_label_marker(Append(separator(L("Physical printers")), wxNullBitmap)); + const PhysicalPrinterCollection& ph_printers = m_preset_bundle->physical_printers; + + for (PhysicalPrinterCollection::ConstIterator it = ph_printers.begin(); it != ph_printers.end(); ++it) { + for (const std::string preset_name : it->get_preset_names()) { + Preset* preset = m_collection->find_preset(preset_name); + if (!preset) + continue; + std::string main_icon_name, bitmap_key = main_icon_name = preset->printer_technology() == ptSLA ? "sla_printer" : m_main_bitmap_name; + wxBitmap* bmp = get_bmp(main_icon_name, wide_icons, main_icon_name); + assert(bmp); + + set_label_marker(Append(wxString::FromUTF8((it->get_full_name(preset_name) + (preset->is_dirty ? Preset::suffix_modified() : "")).c_str()), *bmp), LABEL_ITEM_PHYSICAL_PRINTER); + validate_selection(ph_printers.is_selected(it, preset_name)); + } + } + } + } + + if (m_type == Preset::TYPE_PRINTER || m_type == Preset::TYPE_SLA_MATERIAL) { + wxBitmap* bmp = get_bmp("edit_preset_list", wide_icons, "edit_uni"); + assert(bmp); + + if (m_type == Preset::TYPE_SLA_MATERIAL) + set_label_marker(Append(separator(L("Add/Remove materials")), *bmp), LABEL_ITEM_WIZARD_MATERIALS); + else + set_label_marker(Append(separator(L("Add/Remove printers")), *bmp), LABEL_ITEM_WIZARD_PRINTERS); + } + + update_selection(); + Thaw(); + + if (!tooltip.IsEmpty()) + SetToolTip(tooltip); + + // Update control min size after rescale (changed Display DPI under MSW) + if (GetMinWidth() != 20 * m_em_unit) + SetMinSize(wxSize(20 * m_em_unit, GetSize().GetHeight())); +} + +void PlaterPresetComboBox::msw_rescale() +{ + PresetComboBox::msw_rescale(); + edit_btn->msw_rescale(); +} + + +// --------------------------------- +// *** TabPresetComboBox *** +// --------------------------------- + +TabPresetComboBox::TabPresetComboBox(wxWindow* parent, Preset::Type preset_type) : + PresetComboBox(parent, preset_type, wxSize(35 * wxGetApp().em_unit(), -1)) +{ + Bind(wxEVT_COMBOBOX, [this](wxCommandEvent& evt) { + // see https://github.com/prusa3d/PrusaSlicer/issues/3889 + // Under OSX: in case of use of a same names written in different case (like "ENDER" and "Ender") + // m_presets_choice->GetSelection() will return first item, because search in PopupListCtrl is case-insensitive. + // So, use GetSelection() from event parameter + auto selected_item = evt.GetSelection(); + + auto marker = reinterpret_cast<Marker>(this->GetClientData(selected_item)); + if (marker >= LABEL_ITEM_DISABLED && marker < LABEL_ITEM_MAX) { + this->SetSelection(this->m_last_selected); + if (marker == LABEL_ITEM_WIZARD_PRINTERS) + wxTheApp->CallAfter([this]() { + wxGetApp().run_wizard(ConfigWizard::RR_USER, ConfigWizard::SP_PRINTERS); + + // update combobox if its parent is a PhysicalPrinterDialog + PhysicalPrinterDialog* parent = dynamic_cast<PhysicalPrinterDialog*>(this->GetParent()); + if (parent != nullptr) + update(); + }); + } + else if (on_selection_changed && (m_last_selected != selected_item || m_collection->current_is_dirty()) ) { + m_last_selected = selected_item; + on_selection_changed(selected_item); + } + + evt.StopPropagation(); + }); +} + +// Update the choice UI from the list of presets. +// If show_incompatible, all presets are shown, otherwise only the compatible presets are shown. +// If an incompatible preset is selected, it is shown as well. +void TabPresetComboBox::update() +{ + Freeze(); + Clear(); + invalidate_selection(); + + const std::deque<Preset>& presets = m_collection->get_presets(); + + std::map<wxString, std::pair<wxBitmap*, bool>> nonsys_presets; + wxString selected = ""; + if (!presets.front().is_visible) + set_label_marker(Append(separator(L("System presets")), wxNullBitmap)); + int idx_selected = m_collection->get_selected_idx(); + + PrinterTechnology proper_pt = ptAny; + if (m_type == Preset::TYPE_PRINTER && m_preset_bundle->physical_printers.has_selection()) { + std::string sel_preset_name = m_preset_bundle->physical_printers.get_selected_printer_preset_name(); + Preset* preset = m_collection->find_preset(sel_preset_name); + if (preset) + proper_pt = preset->printer_technology(); + else + m_preset_bundle->physical_printers.unselect_printer(); + } + + for (size_t i = presets.front().is_visible ? 0 : m_collection->num_default_presets(); i < presets.size(); ++i) + { + const Preset& preset = presets[i]; + if (!preset.is_visible || (!show_incompatible && !preset.is_compatible && i != idx_selected)) + continue; + + // marker used for disable incompatible printer models for the selected physical printer + bool is_enabled = true; + + std::string bitmap_key = "tab"; + if (m_type == Preset::TYPE_PRINTER) { + bitmap_key += "_printer"; + if (preset.printer_technology() == ptSLA) + bitmap_key += "_sla"; + } + std::string main_icon_name = m_type == Preset::TYPE_PRINTER && preset.printer_technology() == ptSLA ? "sla_printer" : m_main_bitmap_name; + + wxBitmap* bmp = get_bmp(bitmap_key, main_icon_name, "lock_closed", is_enabled, preset.is_compatible, preset.is_system || preset.is_default); + assert(bmp); + + if (preset.is_default || preset.is_system) { + int item_id = Append(wxString::FromUTF8((preset.name + (preset.is_dirty ? Preset::suffix_modified() : "")).c_str()), *bmp); + if (!is_enabled) + set_label_marker(item_id, LABEL_ITEM_DISABLED); + validate_selection(i == idx_selected); + } + else + { + std::pair<wxBitmap*, bool> pair(bmp, is_enabled); + nonsys_presets.emplace(wxString::FromUTF8((preset.name + (preset.is_dirty ? Preset::suffix_modified() : "")).c_str()), std::pair<wxBitmap*, bool>(bmp, is_enabled)); + if (i == idx_selected) + selected = wxString::FromUTF8((preset.name + (preset.is_dirty ? Preset::suffix_modified() : "")).c_str()); + } + if (i + 1 == m_collection->num_default_presets()) + set_label_marker(Append(separator(L("System presets")), wxNullBitmap)); + } + if (!nonsys_presets.empty()) + { + set_label_marker(Append(separator(L("User presets")), wxNullBitmap)); + for (std::map<wxString, std::pair<wxBitmap*, bool>>::iterator it = nonsys_presets.begin(); it != nonsys_presets.end(); ++it) { + int item_id = Append(it->first, *it->second.first); + bool is_enabled = it->second.second; + if (!is_enabled) + set_label_marker(item_id, LABEL_ITEM_DISABLED); + validate_selection(it->first == selected); + } + } + + if (m_type == Preset::TYPE_PRINTER) + { + // add Physical printers, if any exists + if (!m_preset_bundle->physical_printers.empty()) { + set_label_marker(Append(separator(L("Physical printers")), wxNullBitmap)); + const PhysicalPrinterCollection& ph_printers = m_preset_bundle->physical_printers; + + for (PhysicalPrinterCollection::ConstIterator it = ph_printers.begin(); it != ph_printers.end(); ++it) { + for (const std::string preset_name : it->get_preset_names()) { + Preset* preset = m_collection->find_preset(preset_name); + if (!preset) + continue; + std::string main_icon_name = preset->printer_technology() == ptSLA ? "sla_printer" : m_main_bitmap_name; + + wxBitmap* bmp = get_bmp(main_icon_name, main_icon_name, "", true, true, false); + assert(bmp); + + set_label_marker(Append(wxString::FromUTF8((it->get_full_name(preset_name) + (preset->is_dirty ? Preset::suffix_modified() : "")).c_str()), *bmp), LABEL_ITEM_PHYSICAL_PRINTER); + validate_selection(ph_printers.is_selected(it, preset_name)); + } + } + } + + // add "Add/Remove printers" item + std::string icon_name = "edit_uni"; + wxBitmap* bmp = get_bmp("edit_preset_list, tab,", icon_name, ""); + assert(bmp); + + set_label_marker(Append(separator(L("Add/Remove printers")), *bmp), LABEL_ITEM_WIZARD_PRINTERS); + } + + update_selection(); + Thaw(); +} + +void TabPresetComboBox::msw_rescale() +{ + PresetComboBox::msw_rescale(); + wxSize sz = wxSize(35 * m_em_unit, -1); + SetMinSize(sz); + SetSize(sz); +} + +void TabPresetComboBox::update_dirty() +{ + // 1) Update the dirty flag of the current preset. + m_collection->update_dirty(); + + // 2) Update the labels. + wxWindowUpdateLocker noUpdates(this); + for (unsigned int ui_id = 0; ui_id < GetCount(); ++ui_id) { + auto marker = reinterpret_cast<Marker>(this->GetClientData(ui_id)); + if (marker >= LABEL_ITEM_MARKER) + continue; + + std::string old_label = GetString(ui_id).utf8_str().data(); + std::string preset_name = Preset::remove_suffix_modified(old_label); + std::string ph_printer_name; + + if (marker == LABEL_ITEM_PHYSICAL_PRINTER) { + ph_printer_name = PhysicalPrinter::get_short_name(preset_name); + preset_name = PhysicalPrinter::get_preset_name(preset_name); + } + + const Preset* preset = m_collection->find_preset(preset_name, false); + if (preset) { + std::string new_label = preset->is_dirty ? preset->name + Preset::suffix_modified() : preset->name; + + if (marker == LABEL_ITEM_PHYSICAL_PRINTER) + new_label = ph_printer_name + PhysicalPrinter::separator() + new_label; + + if (old_label != new_label) + SetString(ui_id, wxString::FromUTF8(new_label.c_str())); + } + } +#ifdef __APPLE__ + // wxWidgets on OSX do not upload the text of the combo box line automatically. + // Force it to update by re-selecting. + SetSelection(GetSelection()); +#endif /* __APPLE __ */ +} + + +//----------------------------------------------- +// SavePresetDialog::Item +//----------------------------------------------- + +SavePresetDialog::Item::Item(Preset::Type type, const std::string& suffix, wxBoxSizer* sizer, SavePresetDialog* parent): + m_type(type), + m_parent(parent) +{ + Tab* tab = wxGetApp().get_tab(m_type); + assert(tab); + m_presets = tab->get_presets(); + + const Preset& sel_preset = m_presets->get_selected_preset(); + std::string preset_name = sel_preset.is_default ? "Untitled" : + sel_preset.is_system ? (boost::format(("%1% - %2%")) % sel_preset.name % suffix).str() : + sel_preset.name; + + // if name contains extension + if (boost::iends_with(preset_name, ".ini")) { + size_t len = preset_name.length() - 4; + preset_name.resize(len); + } + + std::vector<std::string> values; + for (const Preset& preset : *m_presets) { + if (preset.is_default || preset.is_system || preset.is_external) + continue; + values.push_back(preset.name); + } + + wxStaticText* label_top = new wxStaticText(m_parent, wxID_ANY, from_u8((boost::format(_utf8(L("Save %s as:"))) % into_u8(tab->title())).str())); + + m_valid_bmp = new wxStaticBitmap(m_parent, wxID_ANY, create_scaled_bitmap("tick_mark", m_parent)); + + m_combo = new wxComboBox(m_parent, wxID_ANY, from_u8(preset_name)); + for (const std::string& value : values) + m_combo->Append(from_u8(value)); + + m_combo->Bind(wxEVT_TEXT, [this](wxCommandEvent&) { update(); }); +#ifdef __WXOSX__ + // Under OSX wxEVT_TEXT wasn't invoked after change selection in combobox, + // So process wxEVT_COMBOBOX too + m_combo->Bind(wxEVT_COMBOBOX, [this](wxCommandEvent&) { update(); }); +#endif //__WXOSX__ + + m_valid_label = new wxStaticText(m_parent, wxID_ANY, ""); + m_valid_label->SetFont(wxGetApp().bold_font()); + + wxBoxSizer* combo_sizer = new wxBoxSizer(wxHORIZONTAL); + combo_sizer->Add(m_valid_bmp, 0, wxALIGN_CENTER_VERTICAL | wxRIGHT, BORDER_W); + combo_sizer->Add(m_combo, 1, wxEXPAND, BORDER_W); + + sizer->Add(label_top, 0, wxEXPAND | wxTOP| wxBOTTOM, BORDER_W); + sizer->Add(combo_sizer, 0, wxEXPAND | wxBOTTOM, BORDER_W); + sizer->Add(m_valid_label, 0, wxEXPAND | wxLEFT, 3*BORDER_W); + + if (m_type == Preset::TYPE_PRINTER) + m_parent->add_info_for_edit_ph_printer(sizer); + + update(); +} + +void SavePresetDialog::Item::update() +{ + m_preset_name = into_u8(m_combo->GetValue()); + + m_valid_type = Valid; + wxString info_line; + + const char* unusable_symbols = "<>[]:/\\|?*\""; + + const std::string unusable_suffix = PresetCollection::get_suffix_modified();//"(modified)"; + for (size_t i = 0; i < std::strlen(unusable_symbols); i++) { + if (m_preset_name.find_first_of(unusable_symbols[i]) != std::string::npos) { + info_line = _L("The supplied name is not valid;") + "\n" + + _L("the following characters are not allowed:") + " " + unusable_symbols; + m_valid_type = NoValid; + break; + } + } + + if (m_valid_type == Valid && m_preset_name.find(unusable_suffix) != std::string::npos) { + info_line = _L("The supplied name is not valid;") + "\n" + + _L("the following suffix is not allowed:") + "\n\t" + + from_u8(PresetCollection::get_suffix_modified()); + m_valid_type = NoValid; + } + + if (m_valid_type == Valid && m_preset_name == "- default -") { + info_line = _L("The supplied name is not available."); + m_valid_type = NoValid; + } + + const Preset* existing = m_presets->find_preset(m_preset_name, false); + if (m_valid_type == Valid && existing && (existing->is_default || existing->is_system)) { + info_line = _L("Cannot overwrite a system profile."); + m_valid_type = NoValid; + } + if (m_valid_type == Valid && existing && (existing->is_external)) { + info_line = _L("Cannot overwrite an external profile."); + m_valid_type = NoValid; + } + if (m_valid_type == Valid && existing && m_preset_name != m_presets->get_selected_preset_name()) + { + info_line = from_u8((boost::format(_u8L("Preset with name \"%1%\" already exists.")) % m_preset_name).str()) + "\n" + + _L("Note: This preset will be replaced after saving"); + m_valid_type = Warning; + } + + m_valid_label->SetLabel(info_line); + m_valid_label->Show(!info_line.IsEmpty()); + + update_valid_bmp(); + + if (m_type == Preset::TYPE_PRINTER) + m_parent->update_info_for_edit_ph_printer(m_preset_name); + + m_parent->layout(); +} + +void SavePresetDialog::Item::update_valid_bmp() +{ + std::string bmp_name = m_valid_type == Warning ? "exclamation" : + m_valid_type == NoValid ? "cross" : "tick_mark" ; + m_valid_bmp->SetBitmap(create_scaled_bitmap(bmp_name, m_parent)); +} + +void SavePresetDialog::Item::accept() +{ + if (m_valid_type == Warning) + m_presets->delete_preset(m_preset_name); +} + + +//----------------------------------------------- +// SavePresetDialog +//----------------------------------------------- + +SavePresetDialog::SavePresetDialog(Preset::Type type, const std::string& suffix) + : DPIDialog(nullptr, wxID_ANY, _L("Save preset"), wxDefaultPosition, wxSize(45 * wxGetApp().em_unit(), 5 * wxGetApp().em_unit()), wxDEFAULT_DIALOG_STYLE | wxICON_WARNING | wxRESIZE_BORDER) +{ + SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW)); + + wxBoxSizer* topSizer = new wxBoxSizer(wxVERTICAL); + + m_presets_sizer = new wxBoxSizer(wxVERTICAL); + + // Add first item + m_items.emplace_back(type, suffix, m_presets_sizer, this); + + // Add dialog's buttons + wxStdDialogButtonSizer* btns = this->CreateStdDialogButtonSizer(wxOK | wxCANCEL); + wxButton* btnOK = static_cast<wxButton*>(this->FindWindowById(wxID_OK, this)); + btnOK->Bind(wxEVT_BUTTON, [this](wxCommandEvent&) { accept(); }); + btnOK->Bind(wxEVT_UPDATE_UI, [this](wxUpdateUIEvent& evt) { evt.Enable(enable_ok_btn()); }); + + topSizer->Add(m_presets_sizer, 0, wxEXPAND | wxALL, BORDER_W); + topSizer->Add(btns, 0, wxEXPAND | wxALL, BORDER_W); + + SetSizer(topSizer); + topSizer->SetSizeHints(this); +} + +void SavePresetDialog::AddItem(Preset::Type type, const std::string& suffix) +{ + m_items.emplace_back(type, suffix, m_presets_sizer, this); +} + +std::string SavePresetDialog::get_name() +{ + return m_items.front().preset_name(); +} + +std::string SavePresetDialog::get_name(Preset::Type type) +{ + for (Item& item : m_items) + if (item.type() == type) + return item.preset_name(); + return ""; +} + +bool SavePresetDialog::enable_ok_btn() const +{ + for (Item item : m_items) + if (!item.is_valid()) + return false; + + return true; +} + +void SavePresetDialog::add_info_for_edit_ph_printer(wxBoxSizer* sizer) +{ + PhysicalPrinterCollection& printers = wxGetApp().preset_bundle->physical_printers; + m_ph_printer_name = printers.get_selected_printer_name(); + m_old_preset_name = printers.get_selected_printer_preset_name(); + + wxString msg_text = from_u8((boost::format(_u8L("You have selected physical printer \"%1%\" \n" + "with related printer preset \"%2%\"")) % + m_ph_printer_name % m_old_preset_name).str()); + m_label = new wxStaticText(this, wxID_ANY, msg_text); + m_label->SetFont(wxGetApp().bold_font()); + + wxString choices[] = {"","",""}; + + m_action_radio_box = new wxRadioBox(this, wxID_ANY, "", wxDefaultPosition, wxDefaultSize, + WXSIZEOF(choices), choices, 3, wxRA_SPECIFY_ROWS); + m_action_radio_box->SetSelection(0); + m_action_radio_box->Bind(wxEVT_RADIOBOX, [this](wxCommandEvent& e) { + m_action = (ActionType)e.GetSelection(); }); + m_action = ChangePreset; + + m_radio_sizer = new wxBoxSizer(wxHORIZONTAL); + m_radio_sizer->Add(m_action_radio_box, 1, wxEXPAND | wxTOP, 2*BORDER_W); + + sizer->Add(m_label, 0, wxEXPAND | wxLEFT | wxTOP, 3*BORDER_W); + sizer->Add(m_radio_sizer, 1, wxEXPAND | wxLEFT, 3*BORDER_W); +} + +void SavePresetDialog::update_info_for_edit_ph_printer(const std::string& preset_name) +{ + bool show = wxGetApp().preset_bundle->physical_printers.has_selection() && m_old_preset_name != preset_name; + + m_label->Show(show); + m_radio_sizer->ShowItems(show); + if (!show) { + this->SetMinSize(wxSize(100,50)); + return; + } + + wxString msg_text = from_u8((boost::format(_u8L("What would you like to do with \"%1%\" preset after saving?")) % preset_name).str()); + m_action_radio_box->SetLabel(msg_text); + + wxString choices[] = { from_u8((boost::format(_u8L("Change \"%1%\" to \"%2%\" for this physical printer \"%3%\"")) % m_old_preset_name % preset_name % m_ph_printer_name).str()), + from_u8((boost::format(_u8L("Add \"%1%\" as a next preset for the the physical printer \"%2%\"")) % preset_name % m_ph_printer_name).str()), + from_u8((boost::format(_u8L("Just switch to \"%1%\" preset")) % preset_name).str()) }; + + int n = 0; + for(const wxString& label: choices) + m_action_radio_box->SetString(n++, label); +} + +void SavePresetDialog::layout() +{ + this->Layout(); + this->Fit(); +} + +void SavePresetDialog::on_dpi_changed(const wxRect& suggested_rect) +{ + const int& em = em_unit(); + + msw_buttons_rescale(this, em, { wxID_OK, wxID_CANCEL }); + + for (Item& item : m_items) + item.update_valid_bmp(); + + //const wxSize& size = wxSize(45 * em, 35 * em); + SetMinSize(/*size*/wxSize(100, 50)); + + Fit(); + Refresh(); +} + +void SavePresetDialog::update_physical_printers(const std::string& preset_name) +{ + if (m_action == UndefAction) + return; + + PhysicalPrinterCollection& physical_printers = wxGetApp().preset_bundle->physical_printers; + if (!physical_printers.has_selection()) + return; + + std::string printer_preset_name = physical_printers.get_selected_printer_preset_name(); + + if (m_action == Switch) + // unselect physical printer, if it was selected + physical_printers.unselect_printer(); + else + { + PhysicalPrinter printer = physical_printers.get_selected_printer(); + + if (m_action == ChangePreset) + printer.delete_preset(printer_preset_name); + + if (printer.add_preset(preset_name)) + physical_printers.save_printer(printer); + + physical_printers.select_printer(printer.get_full_name(preset_name)); + } +} + +void SavePresetDialog::accept() +{ + for (Item& item : m_items) { + item.accept(); + if (item.type() == Preset::TYPE_PRINTER) + update_physical_printers(item.preset_name()); + } + + EndModal(wxID_OK); +} + + + +}} // namespace Slic3r::GUI diff --git a/src/slic3r/GUI/PresetComboBoxes.hpp b/src/slic3r/GUI/PresetComboBoxes.hpp new file mode 100644 index 000000000..7f51f775e --- /dev/null +++ b/src/slic3r/GUI/PresetComboBoxes.hpp @@ -0,0 +1,276 @@ +#ifndef slic3r_PresetComboBoxes_hpp_ +#define slic3r_PresetComboBoxes_hpp_ + +#include <wx/bmpcbox.h> +#include <wx/gdicmn.h> + +#include "libslic3r/Preset.hpp" +#include "wxExtensions.hpp" +#include "GUI_Utils.hpp" + +class wxString; +class wxTextCtrl; +class wxStaticText; +class ScalableButton; +class wxBoxSizer; +class wxComboBox; +class wxStaticBitmap; +class wxRadioBox; + +namespace Slic3r { + +namespace GUI { + +class BitmapCache; + +// --------------------------------- +// *** PresetComboBox *** +// --------------------------------- + +// BitmapComboBox used to presets list on Sidebar and Tabs +class PresetComboBox : public wxBitmapComboBox +{ +public: + PresetComboBox(wxWindow* parent, Preset::Type preset_type, const wxSize& size = wxDefaultSize); + ~PresetComboBox(); + + enum LabelItemType { + LABEL_ITEM_PHYSICAL_PRINTER = 0xffffff01, + LABEL_ITEM_DISABLED, + LABEL_ITEM_MARKER, + LABEL_ITEM_PHYSICAL_PRINTERS, + LABEL_ITEM_WIZARD_PRINTERS, + LABEL_ITEM_WIZARD_FILAMENTS, + LABEL_ITEM_WIZARD_MATERIALS, + + LABEL_ITEM_MAX, + }; + + void set_label_marker(int item, LabelItemType label_item_type = LABEL_ITEM_MARKER); + bool set_printer_technology(PrinterTechnology pt); + + void set_selection_changed_function(std::function<void(int)> sel_changed) { on_selection_changed = sel_changed; } + + bool is_selected_physical_printer(); + + // Return true, if physical printer was selected + // and next internal selection was accomplished + bool selection_is_changed_according_to_physical_printers(); + + void update(std::string select_preset); + + virtual void update(); + virtual void msw_rescale(); + +protected: + typedef std::size_t Marker; + std::function<void(int)> on_selection_changed { nullptr }; + + Preset::Type m_type; + std::string m_main_bitmap_name; + + PresetBundle* m_preset_bundle {nullptr}; + PresetCollection* m_collection {nullptr}; + + // Caching bitmaps for the all bitmaps, used in preset comboboxes + static BitmapCache& bitmap_cache(); + + // Indicator, that the preset is compatible with the selected printer. + ScalableBitmap m_bitmapCompatible; + // Indicator, that the preset is NOT compatible with the selected printer. + ScalableBitmap m_bitmapIncompatible; + + int m_last_selected; + int m_em_unit; + + // parameters for an icon's drawing + int icon_height; + int norm_icon_width; + int thin_icon_width; + int wide_icon_width; + int space_icon_width; + int thin_space_icon_width; + int wide_space_icon_width; + + PrinterTechnology printer_technology {ptAny}; + + void invalidate_selection(); + void validate_selection(bool predicate = false); + void update_selection(); + +#ifdef __linux__ + static const char* separator_head() { return "------- "; } + static const char* separator_tail() { return " -------"; } +#else // __linux__ + static const char* separator_head() { return "————— "; } + static const char* separator_tail() { return " —————"; } +#endif // __linux__ + static wxString separator(const std::string& label); + + wxBitmap* get_bmp( std::string bitmap_key, bool wide_icons, const std::string& main_icon_name, + bool is_compatible = true, bool is_system = false, bool is_single_bar = false, + std::string filament_rgb = "", std::string extruder_rgb = ""); + + wxBitmap* get_bmp( std::string bitmap_key, const std::string& main_icon_name, const std::string& next_icon_name, + bool is_enabled = true, bool is_compatible = true, bool is_system = false); + +#ifdef __APPLE__ + /* For PresetComboBox we use bitmaps that are created from images that are already scaled appropriately for Retina + * (Contrary to the intuition, the `scale` argument for Bitmap's constructor doesn't mean + * "please scale this to such and such" but rather + * "the wxImage is already sized for backing scale such and such". ) + * Unfortunately, the constructor changes the size of wxBitmap too. + * Thus We need to use unscaled size value for bitmaps that we use + * to avoid scaled size of control items. + * For this purpose control drawing methods and + * control size calculation methods (virtual) are overridden. + **/ + virtual bool OnAddBitmap(const wxBitmap& bitmap) override; + virtual void OnDrawItem(wxDC& dc, const wxRect& rect, int item, int flags) const override; +#endif + +private: + void fill_width_height(); +}; + + +// --------------------------------- +// *** PlaterPresetComboBox *** +// --------------------------------- + +class PlaterPresetComboBox : public PresetComboBox +{ +public: + PlaterPresetComboBox(wxWindow *parent, Preset::Type preset_type); + ~PlaterPresetComboBox(); + + ScalableButton* edit_btn { nullptr }; + + void set_extruder_idx(const int extr_idx) { m_extruder_idx = extr_idx; } + int get_extruder_idx() const { return m_extruder_idx; } + + bool switch_to_tab(); + void show_add_menu(); + void show_edit_menu(); + + void update() override; + void msw_rescale() override; + +private: + int m_extruder_idx = -1; +}; + + +// --------------------------------- +// *** TabPresetComboBox *** +// --------------------------------- + +class TabPresetComboBox : public PresetComboBox +{ + bool show_incompatible {false}; + bool m_enable_all {false}; + +public: + TabPresetComboBox(wxWindow *parent, Preset::Type preset_type); + ~TabPresetComboBox() {} + void set_show_incompatible_presets(bool show_incompatible_presets) { + show_incompatible = show_incompatible_presets; + } + + void update() override; + void update_dirty(); + void msw_rescale() override; + + void set_enable_all(bool enable=true) { m_enable_all = enable; } + + PresetCollection* presets() const { return m_collection; } + Preset::Type type() const { return m_type; } +}; + + +//------------------------------------------------ +// SavePresetDialog +//------------------------------------------------ + +class SavePresetDialog : public DPIDialog +{ + enum ActionType + { + ChangePreset, + AddPreset, + Switch, + UndefAction + }; + + struct Item + { + enum ValidationType + { + Valid, + NoValid, + Warning + }; + + Item(Preset::Type type, const std::string& suffix, wxBoxSizer* sizer, SavePresetDialog* parent); + + void update_valid_bmp(); + void accept(); + + bool is_valid() const { return m_valid_type != NoValid; } + Preset::Type type() const { return m_type; } + std::string preset_name() const { return m_preset_name; } + + private: + Preset::Type m_type; + ValidationType m_valid_type; + std::string m_preset_name; + + SavePresetDialog* m_parent {nullptr}; + wxStaticBitmap* m_valid_bmp {nullptr}; + wxComboBox* m_combo {nullptr}; + wxStaticText* m_valid_label {nullptr}; + + PresetCollection* m_presets {nullptr}; + + void update(); + }; + + std::vector<Item> m_items; + + wxBoxSizer* m_presets_sizer {nullptr}; + wxStaticText* m_label {nullptr}; + wxRadioBox* m_action_radio_box {nullptr}; + wxBoxSizer* m_radio_sizer {nullptr}; + ActionType m_action {UndefAction}; + + std::string m_ph_printer_name; + std::string m_old_preset_name; + +public: + + SavePresetDialog(Preset::Type type, const std::string& suffix); + ~SavePresetDialog() {} + + void AddItem(Preset::Type type, const std::string& suffix); + + std::string get_name(); + std::string get_name(Preset::Type type); + + bool enable_ok_btn() const; + void add_info_for_edit_ph_printer(wxBoxSizer *sizer); + void update_info_for_edit_ph_printer(const std::string &preset_name); + void layout(); + +protected: + void on_dpi_changed(const wxRect& suggested_rect) override; + void on_sys_color_changed() override {} + +private: + void update_physical_printers(const std::string& preset_name); + void accept(); +}; + +} // namespace GUI +} // namespace Slic3r + +#endif diff --git a/src/slic3r/GUI/PresetHints.cpp b/src/slic3r/GUI/PresetHints.cpp index 24afeb526..c40c4c6ac 100644 --- a/src/slic3r/GUI/PresetHints.cpp +++ b/src/slic3r/GUI/PresetHints.cpp @@ -4,7 +4,6 @@ #include "libslic3r/Slicing.hpp" #include "libslic3r/libslic3r.h" -#include "PresetBundle.hpp" #include "PresetHints.hpp" #include <wx/intl.h> diff --git a/src/slic3r/GUI/PresetHints.hpp b/src/slic3r/GUI/PresetHints.hpp index be049c2c8..a61310f40 100644 --- a/src/slic3r/GUI/PresetHints.hpp +++ b/src/slic3r/GUI/PresetHints.hpp @@ -3,7 +3,7 @@ #include <string> -#include "PresetBundle.hpp" +#include "libslic3r/PresetBundle.hpp" namespace Slic3r { diff --git a/src/slic3r/GUI/PrintHostDialogs.cpp b/src/slic3r/GUI/PrintHostDialogs.cpp index bae60e47f..216af5df4 100644 --- a/src/slic3r/GUI/PrintHostDialogs.cpp +++ b/src/slic3r/GUI/PrintHostDialogs.cpp @@ -15,11 +15,11 @@ #include "GUI.hpp" #include "GUI_App.hpp" -#include "AppConfig.hpp" #include "MsgDialog.hpp" #include "I18N.hpp" #include "../Utils/PrintHost.hpp" #include "wxExtensions.hpp" +#include "libslic3r/AppConfig.hpp" namespace fs = boost::filesystem; diff --git a/src/slic3r/GUI/Search.cpp b/src/slic3r/GUI/Search.cpp index 012af342a..2a2af5336 100644 --- a/src/slic3r/GUI/Search.cpp +++ b/src/slic3r/GUI/Search.cpp @@ -9,10 +9,10 @@ #include "wx/dataview.h" #include "libslic3r/PrintConfig.hpp" +#include "libslic3r/PresetBundle.hpp" #include "GUI_App.hpp" #include "Plater.hpp" #include "Tab.hpp" -#include "PresetBundle.hpp" #define FTS_FUZZY_MATCH_IMPLEMENTATION #include "fts_fuzzy_match.h" diff --git a/src/slic3r/GUI/Search.hpp b/src/slic3r/GUI/Search.hpp index 9701e6808..8202222e9 100644 --- a/src/slic3r/GUI/Search.hpp +++ b/src/slic3r/GUI/Search.hpp @@ -14,8 +14,8 @@ #include <wx/dialog.h> #include "GUI_Utils.hpp" -#include "Preset.hpp" #include "wxExtensions.hpp" +#include "libslic3r/Preset.hpp" namespace Slic3r { diff --git a/src/slic3r/GUI/Tab.cpp b/src/slic3r/GUI/Tab.cpp index 86b483a8d..4f4beb202 100644 --- a/src/slic3r/GUI/Tab.cpp +++ b/src/slic3r/GUI/Tab.cpp @@ -1,8 +1,8 @@ // #include "libslic3r/GCodeSender.hpp" #include "slic3r/Utils/Serial.hpp" #include "Tab.hpp" -#include "PresetBundle.hpp" #include "PresetHints.hpp" +#include "libslic3r/PresetBundle.hpp" #include "libslic3r/Utils.hpp" #include "libslic3r/Model.hpp" @@ -27,14 +27,15 @@ #include <boost/algorithm/string/predicate.hpp> #include "wxExtensions.hpp" +#include "PresetComboBoxes.hpp" #include <wx/wupdlock.h> #include "GUI_App.hpp" #include "GUI_ObjectList.hpp" -#include "ConfigWizard.hpp" #include "Plater.hpp" #include "MainFrame.hpp" #include "format.hpp" +#include "PhysicalPrinterDialog.hpp" namespace Slic3r { namespace GUI { @@ -160,10 +161,17 @@ void Tab::create_preset_tab() #endif //__WXOSX__ // preset chooser - m_presets_choice = new PresetBitmapComboBox(panel, wxSize(35 * m_em_unit, -1)); + m_presets_choice = new TabPresetComboBox(panel, m_type); + m_presets_choice->set_selection_changed_function([this](int selection) { + if (!m_presets_choice->selection_is_changed_according_to_physical_printers()) + { + if (m_type == Preset::TYPE_PRINTER && !m_presets_choice->is_selected_physical_printer()) + m_preset_bundle->physical_printers.unselect_printer(); - // search combox -// m_search = new Search::SearchCtrl(panel); + // select preset + select_preset(m_presets_choice->GetString(selection).ToUTF8().data()); + } + }); auto color = wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW); @@ -173,6 +181,8 @@ void Tab::create_preset_tab() add_scaled_button(panel, &m_btn_save_preset, "save"); add_scaled_button(panel, &m_btn_delete_preset, "cross"); + if (m_type == Preset::Type::TYPE_PRINTER) + add_scaled_button(panel, &m_btn_edit_ph_printer, "cog"); m_show_incompatible_presets = false; add_scaled_bitmap(this, m_bmp_show_incompatible_presets, "flag_red"); @@ -184,6 +194,8 @@ void Tab::create_preset_tab() m_btn_save_preset->SetToolTip(from_u8((boost::format(_utf8(L("Save current %s"))) % m_title).str())); m_btn_delete_preset->SetToolTip(_(L("Delete this preset"))); m_btn_delete_preset->Disable(); + if (m_btn_edit_ph_printer) + m_btn_edit_ph_printer->Disable(); add_scaled_button(panel, &m_question_btn, "question"); m_question_btn->SetToolTip(_(L("Hover the cursor over buttons to find more information \n" @@ -238,6 +250,10 @@ void Tab::create_preset_tab() m_hsizer->Add(m_btn_save_preset, 0, wxALIGN_CENTER_VERTICAL); m_hsizer->AddSpacer(int(4 * scale_factor)); m_hsizer->Add(m_btn_delete_preset, 0, wxALIGN_CENTER_VERTICAL); + if (m_btn_edit_ph_printer) { + m_hsizer->AddSpacer(int(4 * scale_factor)); + m_hsizer->Add(m_btn_edit_ph_printer, 0, wxALIGN_CENTER_VERTICAL); + } m_hsizer->AddSpacer(int(/*16*/8 * scale_factor)); m_hsizer->Add(m_btn_hide_incompatible_presets, 0, wxALIGN_CENTER_VERTICAL); m_hsizer->AddSpacer(int(8 * scale_factor)); @@ -278,41 +294,19 @@ void Tab::create_preset_tab() m_treectrl->Bind(wxEVT_TREE_SEL_CHANGED, &Tab::OnTreeSelChange, this); m_treectrl->Bind(wxEVT_KEY_DOWN, &Tab::OnKeyDown, this); - m_presets_choice->Bind(wxEVT_COMBOBOX, ([this](wxCommandEvent e) { - //! Because of The MSW and GTK version of wxBitmapComboBox derived from wxComboBox, - //! but the OSX version derived from wxOwnerDrawnCombo, instead of: - //! select_preset(m_presets_choice->GetStringSelection().ToUTF8().data()); - //! we doing next: - // int selected_item = m_presets_choice->GetSelection(); - - // see https://github.com/prusa3d/PrusaSlicer/issues/3889 - // Under OSX: in case of use of a same names written in different case (like "ENDER" and "Ender") - // m_presets_choice->GetSelection() will return first item, because search in PopupListCtrl is case-insensitive. - // So, use GetSelection() from event parameter - int selected_item = e.GetSelection(); - if (m_selected_preset_item == size_t(selected_item) && !m_presets->current_is_dirty()) - return; - if (selected_item >= 0) { - std::string selected_string = m_presets_choice->GetString(selected_item).ToUTF8().data(); - if (selected_string.find(PresetCollection::separator_head()) == 0 - /*selected_string == "------- System presets -------" || - selected_string == "------- User presets -------"*/) { - m_presets_choice->SetSelection(m_selected_preset_item); - if (wxString::FromUTF8(selected_string.c_str()) == PresetCollection::separator(L("Add a new printer"))) - wxTheApp->CallAfter([]() { wxGetApp().run_wizard(ConfigWizard::RR_USER); }); - return; - } - m_selected_preset_item = selected_item; - select_preset(selected_string); - } - })); - m_btn_save_preset->Bind(wxEVT_BUTTON, ([this](wxCommandEvent e) { save_preset(); })); m_btn_delete_preset->Bind(wxEVT_BUTTON, ([this](wxCommandEvent e) { delete_preset(); })); m_btn_hide_incompatible_presets->Bind(wxEVT_BUTTON, ([this](wxCommandEvent e) { toggle_show_hide_incompatible(); })); + if (m_btn_edit_ph_printer) + m_btn_edit_ph_printer->Bind(wxEVT_BUTTON, ([this](wxCommandEvent e) { + PhysicalPrinterDialog dlg(m_presets_choice->GetString(m_presets_choice->GetSelection())); + if (dlg.ShowModal() == wxID_OK) + update_tab_ui(); + })); + // Fill cache for mode bitmaps m_mode_bitmap_cache.reserve(3); m_mode_bitmap_cache.push_back(ScalableBitmap(this, "mode_simple" , mode_icon_px_size())); @@ -778,14 +772,14 @@ void Tab::on_roll_back_value(const bool to_sys /*= true*/) // comparing the selected preset config with $self->{config}. void Tab::update_dirty() { - m_presets->update_dirty_ui(m_presets_choice); + m_presets_choice->update_dirty(); on_presets_changed(); update_changed_ui(); } void Tab::update_tab_ui() { - m_selected_preset_item = m_presets->update_tab_ui(m_presets_choice, m_show_incompatible_presets, m_em_unit); + m_presets_choice->update(); } // Load a provied DynamicConfig into the tab, modifying the active preset. @@ -847,20 +841,20 @@ void Tab::update_visibility() void Tab::msw_rescale() { - m_em_unit = wxGetApp().em_unit(); + m_em_unit = em_unit(m_parent); m_mode_sizer->msw_rescale(); + m_presets_choice->msw_rescale(); - m_presets_choice->SetSize(35 * m_em_unit, -1); m_treectrl->SetMinSize(wxSize(20 * m_em_unit, -1)); - update_tab_ui(); - // rescale buttons and cached bitmaps for (const auto btn : m_scaled_buttons) btn->msw_rescale(); for (const auto bmp : m_scaled_bitmaps) bmp->msw_rescale(); + for (const auto ikon : m_blinking_ikons) + ikon.second->msw_rescale(); for (ScalableBitmap& bmp : m_mode_bitmap_cache) bmp.msw_rescale(); @@ -963,7 +957,7 @@ void Tab::load_key_value(const std::string& opt_key, const boost::any& value, bo // Don't select another profile if this profile happens to become incompatible. m_preset_bundle->update_compatible(PresetSelectCompatibleType::Never); } - m_presets->update_dirty_ui(m_presets_choice); + m_presets_choice->update_dirty(); on_presets_changed(); update(); } @@ -2148,11 +2142,10 @@ void TabPrinter::build_fff() line.append_widget(serial_test); optgroup->append_line(line); } -#endif optgroup = page->new_optgroup(L("Print Host upload")); build_printhost(optgroup.get()); - +#endif optgroup = page->new_optgroup(L("Firmware")); optgroup->append_single_option_line("gcode_flavor"); optgroup->append_single_option_line("silent_mode"); @@ -2310,8 +2303,10 @@ void TabPrinter::build_sla() optgroup->append_single_option_line("min_initial_exposure_time"); optgroup->append_single_option_line("max_initial_exposure_time"); + /* optgroup = page->new_optgroup(L("Print Host upload")); build_printhost(optgroup.get()); + */ const int notes_field_height = 25; // 250 @@ -2699,11 +2694,13 @@ void TabPrinter::update_fff() m_serial_test_btn->Disable(); } + /* { std::unique_ptr<PrintHost> host(PrintHost::get_print_host(m_config)); m_print_host_test_btn->Enable(!m_config->opt_string("print_host").empty() && host->can_test()); m_printhost_browse_btn->Enable(host->has_auto_discovery()); } + */ bool have_multiple_extruders = m_extruders_count > 1; get_field("toolchange_gcode")->toggle(have_multiple_extruders); @@ -2805,7 +2802,7 @@ void Tab::load_current_preset() { const Preset& preset = m_presets->get_edited_preset(); - (preset.is_default || preset.is_system) ? m_btn_delete_preset->Disable() : m_btn_delete_preset->Enable(true); + update_btns_enabling(); update(); if (m_type == Slic3r::Preset::TYPE_PRINTER) { @@ -2942,10 +2939,31 @@ void Tab::update_page_tree_visibility() } +void Tab::update_btns_enabling() +{ + // we can't delete last preset from the physical printer + if (m_type == Preset::TYPE_PRINTER && m_preset_bundle->physical_printers.has_selection()) + m_btn_delete_preset->Enable(m_preset_bundle->physical_printers.get_selected_printer().preset_names.size() > 1); + else { + const Preset& preset = m_presets->get_edited_preset(); + m_btn_delete_preset->Enable(!preset.is_default && !preset.is_system); + } + + // we can edit physical printer only if it's selected in the list + if (m_btn_edit_ph_printer) + m_btn_edit_ph_printer->Enable(m_preset_bundle->physical_printers.has_selection()); +} + +void Tab::update_preset_choice() +{ + m_presets_choice->update(); + update_btns_enabling(); +} + // Called by the UI combo box when the user switches profiles, and also to delete the current profile. // Select a preset by a name.If !defined(name), then the default preset is selected. // If the current profile is modified, user is asked to save the changes. -void Tab::select_preset(std::string preset_name, bool delete_current) +void Tab::select_preset(std::string preset_name, bool delete_current /*=false*/, const std::string& last_selected_ph_printer_name/* =""*/) { if (preset_name.empty()) { if (delete_current) { @@ -3053,7 +3071,16 @@ void Tab::select_preset(std::string preset_name, bool delete_current) } if (canceled) { + if (m_type == Preset::TYPE_PRINTER) { + if (!last_selected_ph_printer_name.empty() && + m_presets->get_edited_preset().name == PhysicalPrinter::get_preset_name(last_selected_ph_printer_name)) { + // If preset selection was canceled and previously was selected physical printer, we should select it back + m_preset_bundle->physical_printers.select_printer(last_selected_ph_printer_name); + } + } + update_tab_ui(); + // Trigger the on_presets_changed event so that we also restore the previous value in the plater selector, // if this action was initiated from the plater. on_presets_changed(); @@ -3095,6 +3122,7 @@ void Tab::select_preset(std::string preset_name, bool delete_current) else if (printer_technology == ptSLA && m_dependent_tabs.front() != Preset::Type::TYPE_SLA_PRINT) m_dependent_tabs = { Preset::Type::TYPE_SLA_PRINT, Preset::Type::TYPE_SLA_MATERIAL }; } + load_current_preset(); } } @@ -3234,56 +3262,10 @@ void Tab::save_preset(std::string name /*= ""*/, bool detach) std::string suffix = detach ? _utf8(L("Detached")) : _CTX_utf8(L_CONTEXT("Copy", "PresetName"), "PresetName"); if (name.empty()) { - const Preset &preset = m_presets->get_selected_preset(); - auto default_name = preset.is_default ? "Untitled" : -// preset.is_system ? (boost::format(_CTX_utf8(L_CONTEXT("%1% - Copy", "PresetName"), "PresetName")) % preset.name).str() : - preset.is_system ? (boost::format(("%1% - %2%")) % preset.name % suffix).str() : - preset.name; - - bool have_extention = boost::iends_with(default_name, ".ini"); - if (have_extention) { - size_t len = default_name.length()-4; - default_name.resize(len); - } - //[map $_->name, grep !$_->default && !$_->external, @{$self->{presets}}], - std::vector<std::string> values; - for (size_t i = 0; i < m_presets->size(); ++i) { - const Preset &preset = m_presets->preset(i); - if (preset.is_default || preset.is_system || preset.is_external) - continue; - values.push_back(preset.name); - } - - SavePresetWindow dlg(parent()); - dlg.build(title(), default_name, values); + SavePresetDialog dlg(m_type, suffix); if (dlg.ShowModal() != wxID_OK) return; name = dlg.get_name(); - if (name == "") { - show_error(this, _(L("The supplied name is empty. It can't be saved."))); - return; - } - const Preset *existing = m_presets->find_preset(name, false); - if (existing && (existing->is_default || existing->is_system)) { - show_error(this, _(L("Cannot overwrite a system profile."))); - return; - } - if (existing && (existing->is_external)) { - show_error(this, _(L("Cannot overwrite an external profile."))); - return; - } - if (existing && name != preset.name) - { - wxString msg_text = GUI::from_u8((boost::format(_utf8(L("Preset with name \"%1%\" already exists."))) % name).str()); - msg_text += "\n" + _(L("Replace?")); - wxMessageDialog dialog(nullptr, msg_text, _(L("Warning")), wxICON_WARNING | wxYES | wxNO); - - if (dialog.ShowModal() == wxID_NO) - return; - - // Remove the preset from the list. - m_presets->delete_preset(name); - } } // Save the preset into Slic3r::data_dir / presets / section_name / preset_name.ini @@ -3345,13 +3327,70 @@ void Tab::delete_preset() // Don't let the user delete the ' - default - ' configuration. std::string action = current_preset.is_external ? _utf8(L("remove")) : _utf8(L("delete")); // TRN remove/delete - const wxString msg = from_u8((boost::format(_utf8(L("Are you sure you want to %1% the selected preset?"))) % action).str()); + + PhysicalPrinterCollection& physical_printers = m_preset_bundle->physical_printers; + wxString msg; + if (m_presets_choice->is_selected_physical_printer()) + msg = from_u8((boost::format(_u8L("Are you sure you want to delete \"%1%\" preset from the physical printer \"%2%\"?")) + % current_preset.name % physical_printers.get_selected_printer_name()).str()); + else + { + if (m_type == Preset::TYPE_PRINTER && !physical_printers.empty()) + { + // Check preset for delete in physical printers + // Ask a customer about next action, if there is a printer with just one preset and this preset is equal to delete + std::vector<std::string> ph_printers = physical_printers.get_printers_with_preset(current_preset.name); + std::vector<std::string> ph_printers_only = physical_printers.get_printers_with_only_preset(current_preset.name); + + if (!ph_printers.empty()) { + msg += _L("Next physical printer(s) has/have selected preset") + ":"; + for (const std::string& printer : ph_printers) + msg += "\n \"" + from_u8(printer) + "\","; + msg.RemoveLast(); + msg += "\n" + _L("Note, that selected preset will be deleted from this/those printer(s) too.")+ "\n\n"; + } + + if (!ph_printers_only.empty()) { + msg += _L("Next physical printer(s) has/have one and only selected preset") + ":"; + for (const std::string& printer : ph_printers_only) + msg += "\n \"" + from_u8(printer) + "\","; + msg.RemoveLast(); + msg += "\n" + _L("Note, that this/those printer(s) will be deleted after deleting of the selected preset.") + "\n\n"; + } + } + + msg += from_u8((boost::format(_u8L("Are you sure you want to %1% the selected preset?")) % action).str()); + } + action = current_preset.is_external ? _utf8(L("Remove")) : _utf8(L("Delete")); // TRN Remove/Delete wxString title = from_u8((boost::format(_utf8(L("%1% Preset"))) % action).str()); //action + _(L(" Preset")); if (current_preset.is_default || wxID_YES != wxMessageDialog(parent(), msg, title, wxYES_NO | wxNO_DEFAULT | wxICON_QUESTION).ShowModal()) return; + + // if we just delete preset from the physical printer + if (m_presets_choice->is_selected_physical_printer()) { + PhysicalPrinter& printer = physical_printers.get_selected_printer(); + + if (printer.preset_names.size() == 1) { + wxMessageDialog dialog(nullptr, _L("It's a last for this physical printer. We can't delete it"), _L("Information"), wxICON_INFORMATION | wxOK); + dialog.ShowModal(); + return; + } + // just delete this preset from the current physical printer + printer.delete_preset(m_presets->get_edited_preset().name); + // select first from the possible presets for this printer + physical_printers.select_printer(printer); + + this->select_preset(physical_printers.get_selected_printer_preset_name()); + return; + } + + // delete selected preset from printers and printer, if it's needed + if (m_type == Preset::TYPE_PRINTER && !physical_printers.empty()) + physical_printers.delete_preset_from_printers(current_preset.name); + // Select will handle of the preset dependencies, of saving & closing the depending profiles, and // finally of deleting the preset. this->select_preset("", true); @@ -3360,6 +3399,7 @@ void Tab::delete_preset() void Tab::toggle_show_hide_incompatible() { m_show_incompatible_presets = !m_show_incompatible_presets; + m_presets_choice->set_show_incompatible_presets(m_show_incompatible_presets); update_show_hide_incompatible_button(); update_tab_ui(); } diff --git a/src/slic3r/GUI/Tab.hpp b/src/slic3r/GUI/Tab.hpp index 5805809bf..24f25e2d7 100644 --- a/src/slic3r/GUI/Tab.hpp +++ b/src/slic3r/GUI/Tab.hpp @@ -33,12 +33,14 @@ #include "Event.hpp" #include "wxExtensions.hpp" #include "ConfigManipulation.hpp" -#include "Preset.hpp" #include "OptionsGroup.hpp" +#include "libslic3r/Preset.hpp" namespace Slic3r { namespace GUI { +class TabPresetComboBox; + // Single Tab page containing a{ vsizer } of{ optgroups } // package Slic3r::GUI::Tab::Page; using ConfigOptionsGroupShp = std::shared_ptr<ConfigOptionsGroup>; @@ -113,10 +115,11 @@ protected: Preset::Type m_type; std::string m_name; const wxString m_title; - PresetBitmapComboBox* m_presets_choice; + TabPresetComboBox* m_presets_choice; ScalableButton* m_search_btn; ScalableButton* m_btn_save_preset; ScalableButton* m_btn_delete_preset; + ScalableButton* m_btn_edit_ph_printer {nullptr}; ScalableButton* m_btn_hide_incompatible_presets; wxBoxSizer* m_hsizer; wxBoxSizer* m_left_sizer; @@ -206,8 +209,6 @@ protected: bool m_is_nonsys_values{ true }; bool m_postpone_update_ui {false}; - size_t m_selected_preset_item{ 0 }; - void set_type(); int m_em_unit; @@ -275,8 +276,10 @@ public: void load_current_preset(); void rebuild_page_tree(); void update_page_tree_visibility(); - // Select a new preset, possibly delete the current one. - void select_preset(std::string preset_name = "", bool delete_current = false); + void update_btns_enabling(); + void update_preset_choice(); + // Select a new preset, possibly delete the current one. + void select_preset(std::string preset_name = "", bool delete_current = false, const std::string& last_selected_ph_printer_name = ""); bool may_discard_current_dirty_preset(PresetCollection* presets = nullptr, const std::string& new_printer_name = ""); bool may_switch_to_SLA_preset(); @@ -320,7 +323,6 @@ public: DynamicPrintConfig* get_config() { return m_config; } PresetCollection* get_presets() { return m_presets; } - size_t get_selected_preset_item() { return m_selected_preset_item; } void on_value_change(const std::string& opt_key, const boost::any& value); diff --git a/src/slic3r/GUI/wxExtensions.cpp b/src/slic3r/GUI/wxExtensions.cpp index c42933b9b..37794b5af 100644 --- a/src/slic3r/GUI/wxExtensions.cpp +++ b/src/slic3r/GUI/wxExtensions.cpp @@ -304,94 +304,6 @@ void wxCheckListBoxComboPopup::OnListBoxSelection(wxCommandEvent& evt) } -namespace Slic3r { -namespace GUI { - -// *** PresetBitmapComboBox *** - -/* For PresetBitmapComboBox we use bitmaps that are created from images that are already scaled appropriately for Retina - * (Contrary to the intuition, the `scale` argument for Bitmap's constructor doesn't mean - * "please scale this to such and such" but rather - * "the wxImage is already sized for backing scale such and such". ) - * Unfortunately, the constructor changes the size of wxBitmap too. - * Thus We need to use unscaled size value for bitmaps that we use - * to avoid scaled size of control items. - * For this purpose control drawing methods and - * control size calculation methods (virtual) are overridden. - **/ - -PresetBitmapComboBox::PresetBitmapComboBox(wxWindow* parent, const wxSize& size) : - wxBitmapComboBox(parent, wxID_ANY, wxEmptyString, wxDefaultPosition, size, 0, nullptr, wxCB_READONLY) -{} - -#ifdef __APPLE__ -bool PresetBitmapComboBox::OnAddBitmap(const wxBitmap& bitmap) -{ - if (bitmap.IsOk()) - { - // we should use scaled! size values of bitmap - int width = (int)bitmap.GetScaledWidth(); - int height = (int)bitmap.GetScaledHeight(); - - if (m_usedImgSize.x < 0) - { - // If size not yet determined, get it from this image. - m_usedImgSize.x = width; - m_usedImgSize.y = height; - - // Adjust control size to vertically fit the bitmap - wxWindow* ctrl = GetControl(); - ctrl->InvalidateBestSize(); - wxSize newSz = ctrl->GetBestSize(); - wxSize sz = ctrl->GetSize(); - if (newSz.y > sz.y) - ctrl->SetSize(sz.x, newSz.y); - else - DetermineIndent(); - } - - wxCHECK_MSG(width == m_usedImgSize.x && height == m_usedImgSize.y, - false, - "you can only add images of same size"); - - return true; - } - - return false; -} - -void PresetBitmapComboBox::OnDrawItem(wxDC& dc, - const wxRect& rect, - int item, - int flags) const -{ - const wxBitmap& bmp = *(wxBitmap*)m_bitmaps[item]; - if (bmp.IsOk()) - { - // we should use scaled! size values of bitmap - wxCoord w = bmp.GetScaledWidth(); - wxCoord h = bmp.GetScaledHeight(); - - const int imgSpacingLeft = 4; - - // Draw the image centered - dc.DrawBitmap(bmp, - rect.x + (m_usedImgSize.x - w) / 2 + imgSpacingLeft, - rect.y + (rect.height - h) / 2, - true); - } - - wxString text = GetString(item); - if (!text.empty()) - dc.DrawText(text, - rect.x + m_imgAreaWidth + 1, - rect.y + (rect.height - dc.GetCharHeight()) / 2); -} -#endif -} -} - - // *** wxDataViewTreeCtrlComboPopup *** const unsigned int wxDataViewTreeCtrlComboPopup::DefaultWidth = 270; @@ -823,11 +735,12 @@ void MenuWithSeparators::SetSecondSeparator() // ---------------------------------------------------------------------------- ScalableBitmap::ScalableBitmap( wxWindow *parent, const std::string& icon_name/* = ""*/, - const int px_cnt/* = 16*/): + const int px_cnt/* = 16*/, + const bool grayscale/* = false*/): m_parent(parent), m_icon_name(icon_name), m_px_cnt(px_cnt) { - m_bmp = create_scaled_bitmap(icon_name, parent, px_cnt); + m_bmp = create_scaled_bitmap(icon_name, parent, px_cnt, grayscale); } wxSize ScalableBitmap::GetBmpSize() const @@ -860,7 +773,7 @@ int ScalableBitmap::GetBmpHeight() const void ScalableBitmap::msw_rescale() { - m_bmp = create_scaled_bitmap(m_icon_name, m_parent, m_px_cnt); + m_bmp = create_scaled_bitmap(m_icon_name, m_parent, m_px_cnt, m_grayscale); } // ---------------------------------------------------------------------------- diff --git a/src/slic3r/GUI/wxExtensions.hpp b/src/slic3r/GUI/wxExtensions.hpp index 254dbfad3..b2c71c4a0 100644 --- a/src/slic3r/GUI/wxExtensions.hpp +++ b/src/slic3r/GUI/wxExtensions.hpp @@ -94,37 +94,6 @@ public: void OnListBoxSelection(wxCommandEvent& evt); }; -namespace Slic3r { -namespace GUI { -// *** PresetBitmapComboBox *** - -// BitmapComboBox used to presets list on Sidebar and Tabs -class PresetBitmapComboBox: public wxBitmapComboBox -{ -public: - PresetBitmapComboBox(wxWindow* parent, const wxSize& size = wxDefaultSize); - ~PresetBitmapComboBox() {} - -#ifdef __APPLE__ -protected: - /* For PresetBitmapComboBox we use bitmaps that are created from images that are already scaled appropriately for Retina - * (Contrary to the intuition, the `scale` argument for Bitmap's constructor doesn't mean - * "please scale this to such and such" but rather - * "the wxImage is already sized for backing scale such and such". ) - * Unfortunately, the constructor changes the size of wxBitmap too. - * Thus We need to use unscaled size value for bitmaps that we use - * to avoid scaled size of control items. - * For this purpose control drawing methods and - * control size calculation methods (virtual) are overridden. - **/ - virtual bool OnAddBitmap(const wxBitmap& bitmap) override; - virtual void OnDrawItem(wxDC& dc, const wxRect& rect, int item, int flags) const override; -#endif -}; - -} -} - // *** wxDataViewTreeCtrlComboBox *** @@ -160,7 +129,8 @@ public: ScalableBitmap() {}; ScalableBitmap( wxWindow *parent, const std::string& icon_name = "", - const int px_cnt = 16); + const int px_cnt = 16, + const bool grayscale = false); ~ScalableBitmap() {} @@ -181,6 +151,7 @@ private: wxBitmap m_bmp = wxBitmap(); std::string m_icon_name = ""; int m_px_cnt {16}; + bool m_grayscale {false}; }; |