FIX: [5779] fix show alias logic when load preset

Jira: 5779

Change-Id: I4fefe3c1ffbca9bd8296f1b3fdd5de48c6a36a28
This commit is contained in:
maosheng.wei 2024-01-03 17:29:05 +08:00 committed by Lane.Wei
parent a9fea1096f
commit f0359e9304
3 changed files with 48 additions and 7 deletions

View File

@ -2491,10 +2491,26 @@ const std::string* PresetCollection::get_preset_name_renamed(const std::string &
return nullptr;
}
bool PresetCollection::is_alias_exist(const std::string &alias)
bool PresetCollection::is_alias_exist(const std::string &alias, Preset* preset)
{
if (m_map_alias_to_profile_name.end() == m_map_alias_to_profile_name.find(alias)) return false;
auto it = m_map_alias_to_profile_name.find(alias);
if (m_map_alias_to_profile_name.end() == it) return false;
if (!preset) return true;
auto compatible_printers = dynamic_cast<ConfigOptionStrings *>(preset->config.option("compatible_printers"));
if (compatible_printers == nullptr) return true;
for (const std::string &printer_name : compatible_printers->values) {
auto printer_iter = m_printer_hold_alias.find(printer_name);
if (m_printer_hold_alias.end() != printer_iter) {
auto alias_iter = m_printer_hold_alias[printer_name].find(alias);
if (m_printer_hold_alias[printer_name].end() != alias_iter) {
BOOST_LOG_TRIVIAL(info) << __FUNCTION__ << " " << " The alias already exists: " << alias << " and the preset name: " << preset->name;
return true;
}
}
}
return false;
}
const std::string& PresetCollection::get_suffix_modified() {
@ -2859,11 +2875,31 @@ void PresetCollection::set_custom_preset_alias(Preset &preset)
boost::trim_right(alias_name);
}
}
if (alias_name.empty() || is_alias_exist(alias_name))
if (alias_name.empty() || is_alias_exist(alias_name, &preset))
preset.alias = "";
else {
preset.alias = std::move(alias_name);
m_map_alias_to_profile_name[preset.alias].push_back(preset.name);
set_printer_hold_alias(preset.alias, preset);
}
}
}
void PresetCollection::set_printer_hold_alias(const std::string &alias, Preset &preset)
{
auto compatible_printers = dynamic_cast<ConfigOptionStrings *>(preset.config.option("compatible_printers"));
if (compatible_printers == nullptr) return;
for (const std::string &printer_name : compatible_printers->values) {
auto printer_iter = m_printer_hold_alias.find(printer_name);
if (m_printer_hold_alias.end() == printer_iter) {
m_printer_hold_alias[printer_name].insert(alias);
} else {
auto alias_iter = m_printer_hold_alias[printer_name].find(alias);
if (m_printer_hold_alias[printer_name].end() == alias_iter) {
m_printer_hold_alias[printer_name].insert(alias);
} else {
BOOST_LOG_TRIVIAL(info) << __FUNCTION__ << " " << printer_name << "already has alias: " << alias << " and the preset name: " << preset.name;
}
}
}
}

View File

@ -4,6 +4,7 @@
#include <deque>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <functional>
#include <mutex>
#include <boost/filesystem/path.hpp>
@ -558,7 +559,8 @@ public:
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;
bool is_alias_exist(const std::string &alias);
bool is_alias_exist(const std::string &alias, Preset* preset = nullptr);
void set_printer_hold_alias(const std::string &alias, Preset &preset);
// used to update preset_choice from Tab
const std::deque<Preset>& get_presets() const { return m_presets; }
@ -756,6 +758,7 @@ private:
std::deque<Preset> m_presets;
// System profiles may have aliases. Map to the full profile name.
std::map<std::string, std::vector<std::string>> m_map_alias_to_profile_name;
std::unordered_map<std::string, std::unordered_set<std::string>> m_printer_hold_alias;
// 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.

View File

@ -3235,7 +3235,7 @@ std::pair<PresetsConfigSubstitutions, size_t> PresetBundle::load_vendor_configs_
PresetCollection *presets = nullptr;
size_t presets_loaded = 0;
auto parse_subfile = [path, vendor_name, presets_loaded, current_vendor_profile](\
auto parse_subfile = [this, path, vendor_name, presets_loaded, current_vendor_profile](\
ConfigSubstitutionContext& substitution_context,
PresetsConfigSubstitutions& substitutions,
LoadConfigBundleAttributes& flags,
@ -3407,8 +3407,10 @@ std::pair<PresetsConfigSubstitutions, size_t> PresetBundle::load_vendor_configs_
}
if (alias_name.empty())
loaded.alias = preset_name;
else
else {
loaded.alias = std::move(alias_name);
filaments.set_printer_hold_alias(loaded.alias, loaded);
}
loaded.renamed_from = std::move(renamed_from);
if (! substitution_context.empty())
substitutions.push_back({