diff options
Diffstat (limited to 'xs/src/slic3r/GUI/Preset.hpp')
-rw-r--r-- | xs/src/slic3r/GUI/Preset.hpp | 215 |
1 files changed, 200 insertions, 15 deletions
diff --git a/xs/src/slic3r/GUI/Preset.hpp b/xs/src/slic3r/GUI/Preset.hpp index f5c138347..821d7dc54 100644 --- a/xs/src/slic3r/GUI/Preset.hpp +++ b/xs/src/slic3r/GUI/Preset.hpp @@ -3,8 +3,12 @@ #include <deque> +#include <boost/filesystem/path.hpp> +#include <boost/property_tree/ptree_fwd.hpp> + #include "../../libslic3r/libslic3r.h" #include "../../libslic3r/PrintConfig.hpp" +#include "slic3r/Utils/Semver.hpp" class wxBitmap; class wxChoice; @@ -13,6 +17,13 @@ class wxItemContainer; namespace Slic3r { +class AppConfig; +class PresetBundle; + +namespace GUI { + class BitmapCache; +} + enum ConfigFileType { CONFIG_FILE_TYPE_UNKNOWN, @@ -23,6 +34,48 @@ enum ConfigFileType 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; + + 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::vector<PrinterVariant> variants; + 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; + + VendorProfile() {} + VendorProfile(std::string id) : id(std::move(id)) {} + + 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; } + + bool operator< (const VendorProfile &rhs) const { return this->id < rhs.id; } + bool operator==(const VendorProfile &rhs) const { return this->id == rhs.id; } +}; + class Preset { public: @@ -31,6 +84,7 @@ public: TYPE_INVALID, TYPE_PRINT, TYPE_FILAMENT, + TYPE_SLA_MATERIAL, TYPE_PRINTER, }; @@ -44,7 +98,10 @@ public: // External preset points to a configuration, which has been loaded but not imported // into the Slic3r default configuration location. bool is_external = false; - // Preset is visible, if it is compatible with the active Printer. + // 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? @@ -55,9 +112,11 @@ public: // 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 will be true), + // 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; @@ -66,8 +125,7 @@ public: DynamicPrintConfig config; // Load this profile for the following keys only. - // Throws std::runtime_error in case the file cannot be read. - DynamicPrintConfig& load(const std::vector<std::string> &keys); + DynamicPrintConfig& load(const std::vector<std::string> &keys, const StaticPrintConfig &defaults); void save(); @@ -82,9 +140,26 @@ public: bool is_compatible_with_printer(const Preset &active_printer, const DynamicPrintConfig *extra_config) const; bool is_compatible_with_printer(const Preset &active_printer) const; + // 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_printers_condition". + static std::string& compatible_printers_condition(DynamicPrintConfig &cfg) { return cfg.option<ConfigOptionString>("compatible_printers_condition", true)->value; } + std::string& compatible_printers_condition() { return Preset::compatible_printers_condition(this->config); } + const std::string& compatible_printers_condition() const { return Preset::compatible_printers_condition(const_cast<Preset*>(this)->config); } + + static PrinterTechnology& printer_technology(DynamicPrintConfig &cfg) { return cfg.option<ConfigOptionEnum<PrinterTechnology>>("printer_technology", true)->value; } + PrinterTechnology& printer_technology() { return Preset::printer_technology(this->config); } + const PrinterTechnology& printer_technology() const { return Preset::printer_technology(const_cast<Preset*>(this)->config); } + // Mark this preset as compatible if it is compatible with active_printer. bool update_compatible_with_printer(const Preset &active_printer, const DynamicPrintConfig *extra_config); + // 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) { set_num_extruders(this->config, n); } @@ -98,6 +173,11 @@ public: // 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 void update_suffix_modified(); + protected: friend class PresetCollection; friend class PresetBundle; @@ -113,15 +193,25 @@ class PresetCollection { public: // Initialize the PresetCollection with the "- default -" preset. - PresetCollection(Preset::Type type, const std::vector<std::string> &keys); + 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.begin() + m_num_default_presets; } + Iterator end() { return m_presets.end(); } + ConstIterator end() const { return m_presets.end(); } + void reset(bool delete_files); Preset::Type type() const { return m_type; } std::string 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); @@ -130,6 +220,18 @@ public: 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. @@ -144,6 +246,8 @@ public: // 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); @@ -155,10 +259,26 @@ public: Preset& get_selected_preset() { return m_presets[m_idx_selected]; } const Preset& get_selected_preset() const { return m_presets[m_idx_selected]; } int get_selected_idx() const { return m_idx_selected; } - // Return the selected preset including the user modifications. + // 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 == -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 some child preset + 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; } + + // used to update preset_choice from Tab + const std::deque<Preset>& get_presets() { return m_presets; } + int get_idx_selected() { return m_idx_selected; } + static const std::string& get_suffix_modified(); + // Return a preset possibly with modifications. + Preset& default_preset() { return m_presets.front(); } const Preset& default_preset() const { return m_presets.front(); } // Return a preset by an index. If the preset is active, a temporary copy is returned. Preset& preset(size_t idx) { return (int(idx) == m_idx_selected) ? m_edited_preset : m_presets[idx]; } @@ -172,31 +292,66 @@ public: { return const_cast<PresetCollection*>(this)->find_preset(name, first_visible_if_not_found); } size_t first_visible_idx() const; - size_t first_compatible_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) + if (m_presets[i].is_compatible) { + 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. - void update_compatible_with_printer(const Preset &active_printer, bool select_other_if_incompatible); + template<typename PreferedCondition> + void update_compatible_with_printer(const Preset &active_printer, bool select_other_if_incompatible, PreferedCondition prefered_condition) + { + if (this->update_compatible_with_printer_internal(active_printer, 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_with_printer(const Preset &active_printer, bool select_other_if_incompatible) + { this->update_compatible_with_printer(active_printer, 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; + 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); } // 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 update_tab_ui(wxBitmapComboBox *ui, bool show_incompatible); + size_t update_tab_ui(wxBitmapComboBox *ui, bool show_incompatible); // 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. @@ -214,23 +369,44 @@ public: // 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; +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 std::set<VendorProfile> &new_vendors); + private: PresetCollection(); PresetCollection(const PresetCollection &other); PresetCollection& operator=(const PresetCollection &other); - // Find a preset in the sorted list of presets. + // 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() + 1, m_presets.end(), key); - return ((it == m_presets.end() || it->name != name) && m_presets.front().name == name) ? m_presets.begin() : it; + 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); } + size_t update_compatible_with_printer_internal(const Preset &active_printer, bool 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. @@ -242,16 +418,25 @@ private: // Selected preset. int m_idx_selected; // Is the "- default -" preset suppressed? - bool m_default_suppressed = true; + bool m_default_suppressed = true; + size_t m_num_default_presets = 0; // Compatible & incompatible marks, to be placed at the wxBitmapComboBox items of a Platter. // These bitmaps are not owned by PresetCollection, but by a PresetBundle. - const wxBitmap *m_bitmap_compatible = nullptr; + 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; // 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; }; } // namespace Slic3r |