Update for PresetComboBoxes

All "Printer-PresetName" pairs are like a separated items now

+ some code refactoring for PresetComboBoxes::update()
This commit is contained in:
YuSanka 2020-07-17 14:32:38 +02:00
parent c5197f3350
commit 5eac36a310
8 changed files with 489 additions and 270 deletions

View file

@ -1370,11 +1370,6 @@ const std::vector<std::string>& PhysicalPrinter::printer_options()
return s_opts;
}
const std::string& PhysicalPrinter::get_preset_name() const
{
return config.opt_string("preset_name");
}
const std::set<std::string>& PhysicalPrinter::get_preset_names() const
{
return preset_names;
@ -1415,8 +1410,6 @@ void PhysicalPrinter::update_from_preset(const Preset& preset)
// add preset names to the options list
auto ret = preset_names.emplace(preset.name);
update_preset_names_in_config();
update_full_name();
}
void PhysicalPrinter::update_from_config(const DynamicPrintConfig& new_config)
@ -1431,8 +1424,6 @@ void PhysicalPrinter::update_from_config(const DynamicPrintConfig& new_config)
preset_names.emplace(val);
}
preset_names = values;
update_full_name();
}
void PhysicalPrinter::reset_presets()
@ -1454,26 +1445,26 @@ PhysicalPrinter::PhysicalPrinter(const std::string& name, const Preset& preset)
void PhysicalPrinter::set_name(const std::string& name)
{
this->name = name;
update_full_name();
}
void PhysicalPrinter::update_full_name()
std::string PhysicalPrinter::get_full_name(std::string preset_name) const
{
full_name = name + separator() + get_preset_name();
return name + separator() + preset_name;
}
std::string PhysicalPrinter::get_short_name(std::string full_name)
{
int pos = full_name.find(separator());
boost::erase_tail(full_name, full_name.length() - pos);
if (pos > 0)
boost::erase_tail(full_name, full_name.length() - pos);
return full_name;
}
std::string PhysicalPrinter::get_preset_name(std::string full_name)
std::string PhysicalPrinter::get_preset_name(std::string name)
{
int pos = full_name.find(separator());
boost::erase_head(full_name, pos + 2);
return full_name;
int pos = name.find(separator());
boost::erase_head(name, pos + 3);
return Preset::remove_suffix_modified(name);
}
@ -1563,7 +1554,6 @@ void PhysicalPrinterCollection::save_printer(const PhysicalPrinter& edited_print
it->config = std::move(edited_printer.config);
it->name = edited_printer.name;
it->preset_names = edited_printer.preset_names;
it->full_name = edited_printer.full_name;
}
else {
// Creating a new printer.
@ -1615,17 +1605,43 @@ bool PhysicalPrinterCollection::delete_selected_printer()
return true;
}
PhysicalPrinter& PhysicalPrinterCollection::select_printer_by_name(std::string name)
std::string PhysicalPrinterCollection::get_selected_full_printer_name() const
{
name = PhysicalPrinter::get_short_name(name);
auto it = this->find_printer_internal(name);
return (m_idx_selected == size_t(-1)) ? std::string() : this->get_selected_printer().get_full_name(m_selected_preset);
}
PhysicalPrinter& PhysicalPrinterCollection::select_printer_by_name(const std::string& full_name)
{
std::string printer_name = PhysicalPrinter::get_short_name(full_name);
auto it = this->find_printer_internal(printer_name);
assert(it != m_printers.end());
// update idx_selected
m_idx_selected = it - m_printers.begin();
m_idx_selected = it - m_printers.begin();
// update name of the currently selected preset
m_selected_preset = it->get_preset_name(full_name);
if (m_selected_preset.empty())
m_selected_preset = *it->preset_names.begin();
return *it;
}
bool PhysicalPrinterCollection::has_selection() const
{
return m_idx_selected != size_t(-1);
}
void PhysicalPrinterCollection::unselect_printer()
{
m_idx_selected = size_t(-1);
m_selected_preset.clear();
}
bool PhysicalPrinterCollection::is_selected(PhysicalPrinterCollection::ConstIterator it, const std::string& preset_name) const
{
return m_idx_selected == it - m_printers.begin() &&
m_selected_preset == preset_name;
}
namespace PresetUtils {
const VendorProfile::PrinterModel* system_printer_model(const Preset &preset)

View file

@ -539,12 +539,9 @@ public:
PhysicalPrinter(const std::string& name) : name(name){}
PhysicalPrinter(const std::string& name, const Preset& preset);
void set_name(const std::string &name);
void update_full_name();
// Name of the Physical Printer, usually derived form the file name.
std::string name;
// Full name of the Physical Printer, included related preset name
std::string full_name;
// File name of the Physical Printer.
std::string file;
// Configuration data, loaded from a file, or set from the defaults.
@ -558,8 +555,6 @@ public:
bool loaded = false;
static const std::vector<std::string>& printer_options();
const std::string& get_preset_name() const;
const std::set<std::string>& get_preset_names() const;
bool has_empty_config() const;
@ -588,6 +583,9 @@ public:
// Sort lexicographically by a preset name. The preset name shall be unique across a single PresetCollection.
bool operator<(const PhysicalPrinter& other) const { return this->name < other.name; }
// get full printer name included a name of the preset
std::string get_full_name(std::string preset_name) const;
// get printer name from the full name uncluded preset name
static std::string get_short_name(std::string full_name);
@ -641,22 +639,29 @@ public:
bool delete_selected_printer();
// Return the selected preset, without the user modifications applied.
PhysicalPrinter& get_selected_printer() { return m_printers[m_idx_selected]; }
const PhysicalPrinter& get_selected_printer() const { return m_printers[m_idx_selected]; }
size_t get_selected_idx() const { return m_idx_selected; }
PhysicalPrinter& get_selected_printer() { return m_printers[m_idx_selected]; }
const PhysicalPrinter& get_selected_printer() const { return m_printers[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_printer_name() const { return (m_idx_selected == size_t(-1)) ? std::string() : this->get_selected_printer().name; }
// Returns the full name of the selected preset, or an empty string if no preset is selected.
std::string get_selected_full_printer_name() const { return (m_idx_selected == size_t(-1)) ? std::string() : this->get_selected_printer().full_name; }
std::string get_selected_printer_name() const { return (m_idx_selected == size_t(-1)) ? std::string() : this->get_selected_printer().name; }
// Returns the config of the selected printer, or nullptr if no printer is selected.
DynamicPrintConfig* get_selected_printer_config() { return (m_idx_selected == size_t(-1)) ? nullptr : &(this->get_selected_printer().config); }
// Returns the config of the selected printer, or nullptr if no printer is selected.
PrinterTechnology get_selected_printer_technology() { return (m_idx_selected == size_t(-1)) ? PrinterTechnology::ptAny : this->get_selected_printer().printer_technology(); }
// Each physical printer can have a several related preset,
// so, use the next functions to get an exact names of selections in the list:
// Returns the full name of the selected printer, or an empty string if no preset is selected.
std::string get_selected_full_printer_name() const;
// Returns the printer model of the selected preset, or an empty string if no preset is selected.
std::string get_selected_printer_preset_name() const { return (m_idx_selected == size_t(-1)) ? std::string() : this->get_selected_printer().get_preset_name(); }
// Returns the config of the selected preset, or nullptr if no preset is selected.
DynamicPrintConfig* get_selected_printer_config() { return (m_idx_selected == size_t(-1)) ? nullptr : &(this->get_selected_printer().config); }
std::string get_selected_printer_preset_name() const { return (m_idx_selected == size_t(-1)) ? std::string() : m_selected_preset; }
// select printer with name and return reference on it
PhysicalPrinter& select_printer_by_name(std::string name);
bool has_selection() const { return m_idx_selected != size_t(-1); }
void unselect_printer() { m_idx_selected = size_t(-1); }
PhysicalPrinter& select_printer_by_name(const std::string& full_name);
bool has_selection() const;
void unselect_printer() ;
bool is_selected(ConstIterator it, const std::string &preset_name) const;
// Return a printer by an index. If the printer is active, a temporary copy is returned.
PhysicalPrinter& printer(size_t idx) { return m_printers[idx]; }
@ -698,6 +703,8 @@ private:
// Selected printer.
size_t m_idx_selected = size_t(-1);
// The name of the preset which is currently select for this printer
std::string m_selected_preset;
// Path to the directory to store the config files into.
std::string m_dir_path;

View file

@ -458,7 +458,7 @@ void PresetBundle::export_selections(AppConfig &config)
config.set("presets", "sla_material", sla_materials.get_selected_preset_name());
config.set("presets", "printer", printers.get_selected_preset_name());
config.set("extras", "physical_printer", physical_printers.get_selected_printer_name());
config.set("extras", "physical_printer", physical_printers.get_selected_full_printer_name());
}
DynamicPrintConfig PresetBundle::full_config() const