OptionsSearcher improvements: Fixed a key for option() and groups_and_categories.

It contains "preset_type;opt_key" now. This key helps to avoid a collisions by using a same options key from different type presets.
Example: Option "elefant_foot_compensation" is in Print presets and SLA_printer presets
This commit is contained in:
YuSanka 2021-03-24 10:45:37 +01:00
parent 10c3e82917
commit 991fa67fd1
7 changed files with 59 additions and 41 deletions

View file

@ -535,7 +535,7 @@ Option ConfigOptionsGroup::get_option(const std::string& opt_key, int opt_index
m_opt_map.emplace(opt_id, pair); m_opt_map.emplace(opt_id, pair);
if (m_use_custom_ctrl) // fill group and category values just for options from Settings Tab if (m_use_custom_ctrl) // fill group and category values just for options from Settings Tab
wxGetApp().sidebar().get_searcher().add_key(opt_id, title, this->config_category()); wxGetApp().sidebar().get_searcher().add_key(opt_id, static_cast<Preset::Type>(this->config_type()), title, this->config_category());
return Option(*m_config->def()->get(opt_key), opt_id); return Option(*m_config->def()->get(opt_key), opt_id);
} }

View file

@ -224,10 +224,11 @@ public:
ConfigOptionsGroup( wxWindow* parent) : ConfigOptionsGroup( wxWindow* parent) :
OptionsGroup(parent, wxEmptyString, true, nullptr) {} OptionsGroup(parent, wxEmptyString, true, nullptr) {}
const std::string& config_category() const throw() { return m_config_category; } const wxString& config_category() const throw() { return m_config_category; }
int config_type() const throw() { return m_config_type; }
const t_opt_map& opt_map() const throw() { return m_opt_map; } const t_opt_map& opt_map() const throw() { return m_opt_map; }
void set_config_category(const std::string &category) { this->m_config_category = category; } void set_config_category_and_type(const wxString &category, int type) { this->m_config_category = category; this->m_config_type = type; }
void set_config(DynamicPrintConfig* config) { m_config = config; m_modelconfig = nullptr; } void set_config(DynamicPrintConfig* config) { m_config = config; m_modelconfig = nullptr; }
Option get_option(const std::string& opt_key, int opt_index = -1); Option get_option(const std::string& opt_key, int opt_index = -1);
Line create_single_option_line(const std::string& title, const wxString& path = wxEmptyString, int idx = -1) /*const*/{ Line create_single_option_line(const std::string& title, const wxString& path = wxEmptyString, int idx = -1) /*const*/{
@ -274,7 +275,8 @@ private:
// If the config is modelconfig, then ModelConfig::touch() has to be called after value change. // If the config is modelconfig, then ModelConfig::touch() has to be called after value change.
ModelConfig* m_modelconfig { nullptr }; ModelConfig* m_modelconfig { nullptr };
t_opt_map m_opt_map; t_opt_map m_opt_map;
std::string m_config_category; wxString m_config_category;
int m_config_type;
// Change an option on m_config, possibly call ModelConfig::touch(). // Change an option on m_config, possibly call ModelConfig::touch().
void change_opt_value(const t_config_option_key& opt_key, const boost::any& value, int opt_index = 0); void change_opt_value(const t_config_option_key& opt_key, const boost::any& value, int opt_index = 0);

View file

@ -992,7 +992,7 @@ void Sidebar::search()
void Sidebar::jump_to_option(size_t selected) void Sidebar::jump_to_option(size_t selected)
{ {
const Search::Option& opt = p->searcher.get_option(selected); const Search::Option& opt = p->searcher.get_option(selected);
wxGetApp().get_tab(opt.type)->activate_option(boost::nowide::narrow(opt.opt_key), boost::nowide::narrow(opt.category)); wxGetApp().get_tab(opt.type)->activate_option(opt.opt_key(), boost::nowide::narrow(opt.category));
// Switch to the Settings NotePad // Switch to the Settings NotePad
// wxGetApp().mainframe->select_tab(); // wxGetApp().mainframe->select_tab();

View file

@ -63,24 +63,29 @@ void change_opt_key(std::string& opt_key, DynamicPrintConfig* config, int& cnt)
opt_key += "#" + std::to_string(0); opt_key += "#" + std::to_string(0);
} }
static std::string get_key(const std::string& opt_key, Preset::Type type)
{
return std::to_string(int(type)) + ";" + opt_key;
}
void OptionsSearcher::append_options(DynamicPrintConfig* config, Preset::Type type, ConfigOptionMode mode) void OptionsSearcher::append_options(DynamicPrintConfig* config, Preset::Type type, ConfigOptionMode mode)
{ {
auto emplace = [this, type](const std::string opt_key, const wxString& label) auto emplace = [this, type](const std::string key, const wxString& label)
{ {
const GroupAndCategory& gc = groups_and_categories[opt_key]; const GroupAndCategory& gc = groups_and_categories[key];
if (gc.group.IsEmpty() || gc.category.IsEmpty()) if (gc.group.IsEmpty() || gc.category.IsEmpty())
return; return;
wxString suffix; wxString suffix;
wxString suffix_local; wxString suffix_local;
if (gc.category == "Machine limits") { if (gc.category == "Machine limits") {
suffix = opt_key.back()=='1' ? L("Stealth") : L("Normal"); suffix = key.back()=='1' ? L("Stealth") : L("Normal");
suffix_local = " " + _(suffix); suffix_local = " " + _(suffix);
suffix = " " + suffix; suffix = " " + suffix;
} }
if (!label.IsEmpty()) if (!label.IsEmpty())
options.emplace_back(Option{ boost::nowide::widen(opt_key), type, options.emplace_back(Option{ boost::nowide::widen(key), type,
(label + suffix).ToStdWstring(), (_(label) + suffix_local).ToStdWstring(), (label + suffix).ToStdWstring(), (_(label) + suffix_local).ToStdWstring(),
gc.group.ToStdWstring(), _(gc.group).ToStdWstring(), gc.group.ToStdWstring(), _(gc.group).ToStdWstring(),
gc.category.ToStdWstring(), GUI::Tab::translate_category(gc.category, type).ToStdWstring() }); gc.category.ToStdWstring(), GUI::Tab::translate_category(gc.category, type).ToStdWstring() });
@ -108,12 +113,13 @@ void OptionsSearcher::append_options(DynamicPrintConfig* config, Preset::Type ty
wxString label = opt.full_label.empty() ? opt.label : opt.full_label; wxString label = opt.full_label.empty() ? opt.label : opt.full_label;
std::string key = get_key(opt_key, type);
if (cnt == 0) if (cnt == 0)
emplace(opt_key, label); emplace(key, label);
else else
for (int i = 0; i < cnt; ++i) for (int i = 0; i < cnt; ++i)
// ! It's very important to use "#". opt_key#n is a real option key used in GroupAndCategory // ! It's very important to use "#". opt_key#n is a real option key used in GroupAndCategory
emplace(opt_key + "#" + std::to_string(i), label); emplace(key + "#" + std::to_string(i), label);
} }
} }
@ -237,10 +243,10 @@ bool OptionsSearcher::search(const std::string& search, bool force/* = false*/)
int score2; int score2;
matches.clear(); matches.clear();
fuzzy_match(wsearch, label, score, matches); fuzzy_match(wsearch, label, score, matches);
if (fuzzy_match(wsearch, opt.opt_key, score2, matches2) && score2 > score) { if (fuzzy_match(wsearch, opt.key, score2, matches2) && score2 > score) {
for (fts::pos_type &pos : matches2) for (fts::pos_type &pos : matches2)
pos += label.size() + 1; pos += label.size() + 1;
label += L"(" + opt.opt_key + L")"; label += L"(" + opt.key + L")";
append(matches, matches2); append(matches, matches2);
score = score2; score = score2;
} }
@ -318,9 +324,9 @@ const Option& OptionsSearcher::get_option(size_t pos_in_filter) const
return options[found[pos_in_filter].option_idx]; return options[found[pos_in_filter].option_idx];
} }
const Option& OptionsSearcher::get_option(const std::string& opt_key) const const Option& OptionsSearcher::get_option(const std::string& opt_key, Preset::Type type) const
{ {
auto it = std::lower_bound(options.begin(), options.end(), Option({ boost::nowide::widen(opt_key) })); auto it = std::lower_bound(options.begin(), options.end(), Option({ boost::nowide::widen(get_key(opt_key, type)) }));
assert(it != options.end()); assert(it != options.end());
return options[it - options.begin()]; return options[it - options.begin()];
@ -342,7 +348,7 @@ static Option create_option(const std::string& opt_key, const wxString& label, P
category = wxString::Format("%s %d", "Extruder", atoi(opt_idx.c_str()) + 1); category = wxString::Format("%s %d", "Extruder", atoi(opt_idx.c_str()) + 1);
} }
return Option{ boost::nowide::widen(opt_key), type, return Option{ boost::nowide::widen(get_key(opt_key, type)), type,
(label + suffix).ToStdWstring(), (_(label) + suffix_local).ToStdWstring(), (label + suffix).ToStdWstring(), (_(label) + suffix_local).ToStdWstring(),
gc.group.ToStdWstring(), _(gc.group).ToStdWstring(), gc.group.ToStdWstring(), _(gc.group).ToStdWstring(),
gc.category.ToStdWstring(), GUI::Tab::translate_category(category, type).ToStdWstring() }; gc.category.ToStdWstring(), GUI::Tab::translate_category(category, type).ToStdWstring() };
@ -350,15 +356,16 @@ static Option create_option(const std::string& opt_key, const wxString& label, P
Option OptionsSearcher::get_option(const std::string& opt_key, const wxString& label, Preset::Type type) const Option OptionsSearcher::get_option(const std::string& opt_key, const wxString& label, Preset::Type type) const
{ {
auto it = std::lower_bound(options.begin(), options.end(), Option({ boost::nowide::widen(opt_key) })); std::string key = get_key(opt_key, type);
if(it->opt_key == boost::nowide::widen(opt_key)) auto it = std::lower_bound(options.begin(), options.end(), Option({ boost::nowide::widen(key) }));
if(it->key == boost::nowide::widen(key))
return options[it - options.begin()]; return options[it - options.begin()];
if (groups_and_categories.find(opt_key) == groups_and_categories.end()) { if (groups_and_categories.find(key) == groups_and_categories.end()) {
size_t pos = opt_key.find('#'); size_t pos = key.find('#');
if (pos == std::string::npos) if (pos == std::string::npos)
return options[it - options.begin()]; return options[it - options.begin()];
std::string zero_opt_key = opt_key.substr(0, pos + 1) + "0"; std::string zero_opt_key = key.substr(0, pos + 1) + "0";
if(groups_and_categories.find(zero_opt_key) == groups_and_categories.end()) if(groups_and_categories.find(zero_opt_key) == groups_and_categories.end())
return options[it - options.begin()]; return options[it - options.begin()];
@ -366,16 +373,16 @@ Option OptionsSearcher::get_option(const std::string& opt_key, const wxString& l
return create_option(opt_key, label, type, groups_and_categories.at(zero_opt_key)); return create_option(opt_key, label, type, groups_and_categories.at(zero_opt_key));
} }
const GroupAndCategory& gc = groups_and_categories.at(opt_key); const GroupAndCategory& gc = groups_and_categories.at(key);
if (gc.group.IsEmpty() || gc.category.IsEmpty()) if (gc.group.IsEmpty() || gc.category.IsEmpty())
return options[it - options.begin()]; return options[it - options.begin()];
return create_option(opt_key, label, type, gc); return create_option(opt_key, label, type, gc);
} }
void OptionsSearcher::add_key(const std::string& opt_key, const wxString& group, const wxString& category) void OptionsSearcher::add_key(const std::string& opt_key, Preset::Type type, const wxString& group, const wxString& category)
{ {
groups_and_categories[opt_key] = GroupAndCategory{group, category}; groups_and_categories[get_key(opt_key, type)] = GroupAndCategory{group, category};
} }

View file

@ -38,11 +38,11 @@ struct GroupAndCategory {
struct Option { struct Option {
// bool operator<(const Option& other) const { return other.label > this->label; } // bool operator<(const Option& other) const { return other.label > this->label; }
bool operator<(const Option& other) const { return other.opt_key > this->opt_key; } bool operator<(const Option& other) const { return other.key > this->key; }
// Fuzzy matching works at a character level. Thus matching with wide characters is a safer bet than with short characters, // Fuzzy matching works at a character level. Thus matching with wide characters is a safer bet than with short characters,
// though for some languages (Chinese?) it may not work correctly. // though for some languages (Chinese?) it may not work correctly.
std::wstring opt_key; std::wstring key;
Preset::Type type {Preset::TYPE_INVALID}; Preset::Type type {Preset::TYPE_INVALID};
std::wstring label; std::wstring label;
std::wstring label_local; std::wstring label_local;
@ -50,6 +50,8 @@ struct Option {
std::wstring group_local; std::wstring group_local;
std::wstring category; std::wstring category;
std::wstring category_local; std::wstring category_local;
std::string opt_key() const { return boost::nowide::narrow(key).substr(2); }
}; };
struct FoundOption { struct FoundOption {
@ -110,13 +112,13 @@ public:
bool search(); bool search();
bool search(const std::string& search, bool force = false); bool search(const std::string& search, bool force = false);
void add_key(const std::string& opt_key, const wxString& group, const wxString& category); void add_key(const std::string& opt_key, Preset::Type type, const wxString& group, const wxString& category);
size_t size() const { return found_size(); } size_t size() const { return found_size(); }
const FoundOption& operator[](const size_t pos) const noexcept { return found[pos]; } const FoundOption& operator[](const size_t pos) const noexcept { return found[pos]; }
const Option& get_option(size_t pos_in_filter) const; const Option& get_option(size_t pos_in_filter) const;
const Option& get_option(const std::string& opt_key) const; const Option& get_option(const std::string& opt_key, Preset::Type type) const;
Option get_option(const std::string& opt_key, const wxString& label, Preset::Type type) const; Option get_option(const std::string& opt_key, const wxString& label, Preset::Type type) const;
const std::vector<FoundOption>& found_options() { return found; } const std::vector<FoundOption>& found_options() { return found; }
@ -125,10 +127,11 @@ public:
void set_printer_technology(PrinterTechnology pt) { printer_technology = pt; } void set_printer_technology(PrinterTechnology pt) { printer_technology = pt; }
void sort_options_by_opt_key() { void sort_options_by_key() {
std::sort(options.begin(), options.end(), [](const Option& o1, const Option& o2) { std::sort(options.begin(), options.end(), [](const Option& o1, const Option& o2) {
return o1.opt_key < o2.opt_key; }); return o1.key < o2.key; });
} }
void sort_options_by_label() { sort_options(); }
}; };

View file

@ -3802,8 +3802,8 @@ wxSizer* TabPrinter::create_bed_shape_widget(wxWindow* parent)
{ {
Search::OptionsSearcher& searcher = wxGetApp().sidebar().get_searcher(); Search::OptionsSearcher& searcher = wxGetApp().sidebar().get_searcher();
const Search::GroupAndCategory& gc = searcher.get_group_and_category("bed_shape"); const Search::GroupAndCategory& gc = searcher.get_group_and_category("bed_shape");
searcher.add_key("bed_custom_texture", gc.group, gc.category); searcher.add_key("bed_custom_texture", m_type, gc.group, gc.category);
searcher.add_key("bed_custom_model", gc.group, gc.category); searcher.add_key("bed_custom_model", m_type, gc.group, gc.category);
} }
return sizer; return sizer;
@ -4044,7 +4044,6 @@ ConfigOptionsGroupShp Page::new_optgroup(const wxString& title, int noncommon_la
{ {
//! config_ have to be "right" //! config_ have to be "right"
ConfigOptionsGroupShp optgroup = std::make_shared<ConfigOptionsGroup>(m_parent, title, m_config, true); ConfigOptionsGroupShp optgroup = std::make_shared<ConfigOptionsGroup>(m_parent, title, m_config, true);
optgroup->set_config_category(m_title.ToStdString());
if (noncommon_label_width >= 0) if (noncommon_label_width >= 0)
optgroup->label_width = noncommon_label_width; optgroup->label_width = noncommon_label_width;
@ -4053,6 +4052,7 @@ ConfigOptionsGroupShp Page::new_optgroup(const wxString& title, int noncommon_la
#else #else
auto tab = parent()->GetParent();// GetParent(); auto tab = parent()->GetParent();// GetParent();
#endif #endif
optgroup->set_config_category_and_type(m_title, static_cast<Tab*>(tab)->type());
optgroup->m_on_change = [tab](t_config_option_key opt_key, boost::any value) { optgroup->m_on_change = [tab](t_config_option_key opt_key, boost::any value) {
//! This function will be called from OptionGroup. //! This function will be called from OptionGroup.
//! Using of CallAfter is redundant. //! Using of CallAfter is redundant.

View file

@ -833,10 +833,10 @@ void UnsavedChangesDialog::build(Preset::Type type, PresetCollection* dependent_
(*btn)->Bind(wxEVT_LEAVE_WINDOW, [this](wxMouseEvent& e) { show_info_line(Action::Undef); e.Skip(); }); (*btn)->Bind(wxEVT_LEAVE_WINDOW, [this](wxMouseEvent& e) { show_info_line(Action::Undef); e.Skip(); });
}; };
const PresetCollection& printers = wxGetApp().preset_bundle->printers; const PresetCollection* switched_presets = type == Preset::TYPE_INVALID ? nullptr : wxGetApp().get_tab(type)->get_presets();
if (dependent_presets && (type == dependent_presets->type() ? if (dependent_presets && switched_presets && (type == dependent_presets->type() ?
dependent_presets->get_edited_preset().printer_technology() == dependent_presets->find_preset(new_selected_preset)->printer_technology() : dependent_presets->get_edited_preset().printer_technology() == dependent_presets->find_preset(new_selected_preset)->printer_technology() :
printers.get_edited_preset().printer_technology() == printers.find_preset(new_selected_preset)->printer_technology())) switched_presets->get_edited_preset().printer_technology() == switched_presets->find_preset(new_selected_preset)->printer_technology()))
add_btn(&m_transfer_btn, m_move_btn_id, "paste_menu", Action::Transfer, _L("Transfer")); add_btn(&m_transfer_btn, m_move_btn_id, "paste_menu", Action::Transfer, _L("Transfer"));
add_btn(&m_discard_btn, m_continue_btn_id, dependent_presets ? "switch_presets" : "exit", Action::Discard, _L("Discard"), false); add_btn(&m_discard_btn, m_continue_btn_id, dependent_presets ? "switch_presets" : "exit", Action::Discard, _L("Discard"), false);
add_btn(&m_save_btn, m_save_btn_id, "save", Action::Save, _L("Save")); add_btn(&m_save_btn, m_save_btn_id, "save", Action::Save, _L("Save"));
@ -1185,7 +1185,7 @@ void UnsavedChangesDialog::update(Preset::Type type, PresetCollection* dependent
void UnsavedChangesDialog::update_tree(Preset::Type type, PresetCollection* presets_) void UnsavedChangesDialog::update_tree(Preset::Type type, PresetCollection* presets_)
{ {
Search::OptionsSearcher& searcher = wxGetApp().sidebar().get_searcher(); Search::OptionsSearcher& searcher = wxGetApp().sidebar().get_searcher();
searcher.sort_options_by_opt_key(); searcher.sort_options_by_key();
// list of the presets with unsaved changes // list of the presets with unsaved changes
std::vector<PresetCollection*> presets_list; std::vector<PresetCollection*> presets_list;
@ -1227,8 +1227,8 @@ void UnsavedChangesDialog::update_tree(Preset::Type type, PresetCollection* pres
} }
for (const std::string& opt_key : dirty_options) { for (const std::string& opt_key : dirty_options) {
const Search::Option& option = searcher.get_option(opt_key); const Search::Option& option = searcher.get_option(opt_key, type);
if (option.opt_key != boost::nowide::widen(opt_key)) { if (option.opt_key() != opt_key) {
// When founded option isn't the correct one. // When founded option isn't the correct one.
// It can be for dirty_options: "default_print_profile", "printer_model", "printer_settings_id", // It can be for dirty_options: "default_print_profile", "printer_model", "printer_settings_id",
// because of they don't exist in searcher // because of they don't exist in searcher
@ -1239,6 +1239,9 @@ void UnsavedChangesDialog::update_tree(Preset::Type type, PresetCollection* pres
get_string_value(opt_key, old_config), get_string_value(opt_key, new_config), category_icon_map.at(option.category)); get_string_value(opt_key, old_config), get_string_value(opt_key, new_config), category_icon_map.at(option.category));
} }
} }
// Revert sort of searcher back
searcher.sort_options_by_label();
} }
void UnsavedChangesDialog::on_dpi_changed(const wxRect& suggested_rect) void UnsavedChangesDialog::on_dpi_changed(const wxRect& suggested_rect)
@ -1546,7 +1549,7 @@ void DiffPresetDialog::update_presets(Preset::Type type)
void DiffPresetDialog::update_tree() void DiffPresetDialog::update_tree()
{ {
Search::OptionsSearcher& searcher = wxGetApp().sidebar().get_searcher(); Search::OptionsSearcher& searcher = wxGetApp().sidebar().get_searcher();
searcher.sort_options_by_opt_key(); searcher.sort_options_by_key();
m_tree->Clear(); m_tree->Clear();
wxString bottom_info = ""; wxString bottom_info = "";
@ -1617,7 +1620,7 @@ void DiffPresetDialog::update_tree()
wxString right_val = get_string_value(opt_key, right_congig); wxString right_val = get_string_value(opt_key, right_congig);
Search::Option option = searcher.get_option(opt_key, get_full_label(opt_key, left_config), type); Search::Option option = searcher.get_option(opt_key, get_full_label(opt_key, left_config), type);
if (option.opt_key != boost::nowide::widen(opt_key)) { if (option.opt_key() != opt_key) {
// temporary solution, just for testing // temporary solution, just for testing
m_tree->Append(opt_key, type, _L("Undef category"), _L("Undef group"), opt_key, left_val, right_val, "question"); m_tree->Append(opt_key, type, _L("Undef category"), _L("Undef group"), opt_key, left_val, right_val, "question");
// When founded option isn't the correct one. // When founded option isn't the correct one.
@ -1642,6 +1645,9 @@ void DiffPresetDialog::update_tree()
Fit(); Fit();
Refresh(); Refresh();
} }
// Revert sort of searcher back
searcher.sort_options_by_label();
} }
void DiffPresetDialog::on_dpi_changed(const wxRect&) void DiffPresetDialog::on_dpi_changed(const wxRect&)