Refactoring for SubstitutionManager to avoid flickering on any config changes on active TabPage. (SPE-1889)

This commit is contained in:
YuSanka 2023-09-18 11:59:39 +02:00
parent 2aded51161
commit 2c9a21ca9a
2 changed files with 40 additions and 17 deletions

View File

@ -4544,22 +4544,24 @@ void SubstitutionManager::init(DynamicPrintConfig* config, wxWindow* parent, wxF
m_parent = parent;
m_grid_sizer = grid_sizer;
m_em = em_unit(parent);
m_substitutions = m_config->option<ConfigOptionStrings>("gcode_substitutions")->values;
}
void SubstitutionManager::validate_lenth()
{
std::vector<std::string>& substitutions = m_config->option<ConfigOptionStrings>("gcode_substitutions")->values;
if ((substitutions.size() % 4) != 0) {
if ((m_substitutions.size() % 4) != 0) {
WarningDialog(m_parent, "Value of gcode_substitutions parameter will be cut to valid length",
"Invalid length of gcode_substitutions parameter").ShowModal();
substitutions.resize(substitutions.size() - (substitutions.size() % 4));
m_substitutions.resize(m_substitutions.size() - (m_substitutions.size() % 4));
// save changes from m_substitutions to config
m_config->option<ConfigOptionStrings>("gcode_substitutions")->values = m_substitutions;
}
}
bool SubstitutionManager::is_compatibile_with_ui()
{
const std::vector<std::string>& substitutions = m_config->option<ConfigOptionStrings>("gcode_substitutions")->values;
if (int(substitutions.size() / 4) != m_grid_sizer->GetEffectiveRowsCount() - 1) {
if (int(m_substitutions.size() / 4) != m_grid_sizer->GetEffectiveRowsCount() - 1) {
ErrorDialog(m_parent, "Invalid compatibility between UI and BE", false).ShowModal();
return false;
}
@ -4568,8 +4570,7 @@ bool SubstitutionManager::is_compatibile_with_ui()
bool SubstitutionManager::is_valid_id(int substitution_id, const wxString& message)
{
const std::vector<std::string>& substitutions = m_config->option<ConfigOptionStrings>("gcode_substitutions")->values;
if (int(substitutions.size() / 4) < substitution_id) {
if (int(m_substitutions.size() / 4) < substitution_id) {
ErrorDialog(m_parent, message, false).ShowModal();
return false;
}
@ -4600,8 +4601,11 @@ void SubstitutionManager::delete_substitution(int substitution_id)
return;
// delete substitution
std::vector<std::string>& substitutions = m_config->option<ConfigOptionStrings>("gcode_substitutions")->values;
substitutions.erase(std::next(substitutions.begin(), substitution_id * 4), std::next(substitutions.begin(), substitution_id * 4 + 4));
m_substitutions.erase(std::next(m_substitutions.begin(), substitution_id * 4), std::next(m_substitutions.begin(), substitution_id * 4 + 4));
// save changes from m_substitutions to config
m_config->option<ConfigOptionStrings>("gcode_substitutions")->values = m_substitutions;
call_ui_update();
// update grid_sizer
@ -4620,15 +4624,16 @@ void SubstitutionManager::add_substitution( int substitution_id,
if (substitution_id < 0) {
if (m_grid_sizer->IsEmpty()) {
create_legend();
substitution_id = 0;
}
substitution_id = m_grid_sizer->GetEffectiveRowsCount() - 1;
// create new substitution
// it have to be added to config too
std::vector<std::string>& substitutions = m_config->option<ConfigOptionStrings>("gcode_substitutions")->values;
for (size_t i = 0; i < 4; i ++)
substitutions.push_back(std::string());
m_substitutions.push_back(std::string());
// save changes from config to m_substitutions
m_config->option<ConfigOptionStrings>("gcode_substitutions")->values = m_substitutions;
call_after_layout = true;
}
@ -4690,6 +4695,14 @@ void SubstitutionManager::add_substitution( int substitution_id,
auto chb_match_single_line = new wxCheckBox(m_parent, wxID_ANY, _L("Match single line"));
chb_match_single_line->SetValue(match_single_line);
chb_match_single_line->Show(regexp);
chb_match_single_line->Bind(wxEVT_SHOW, [chb_match_single_line, chb_regexp](wxShowEvent& evt) {
if (evt.IsShown() && !chb_regexp->GetValue()) {
// To avoid a case, when ShowAll() is called for m_grid_sizer but chb_match_single_line have to be hidden
chb_match_single_line->Hide();
}
});
params_sizer->Add(chb_match_single_line, 0, wxALIGN_CENTER_VERTICAL | wxRIGHT | wxLEFT, m_em);
for (wxCheckBox* chb : std::initializer_list<wxCheckBox*>{ chb_regexp, chb_case_insensitive, chb_whole_word, chb_match_single_line }) {
@ -4725,10 +4738,17 @@ void SubstitutionManager::add_substitution( int substitution_id,
void SubstitutionManager::update_from_config()
{
std::vector<std::string>& subst = m_config->option<ConfigOptionStrings>("gcode_substitutions")->values;
if (m_substitutions == subst && m_grid_sizer->IsShown(1)) {
// "gcode_substitutions" values didn't changed in config. There is no need to update/recreate controls
return;
}
m_substitutions = subst;
if (!m_grid_sizer->IsEmpty())
m_grid_sizer->Clear(true);
std::vector<std::string>& subst = m_config->option<ConfigOptionStrings>("gcode_substitutions")->values;
if (subst.empty())
hide_delete_all_btn();
else
@ -4745,7 +4765,8 @@ void SubstitutionManager::update_from_config()
void SubstitutionManager::delete_all()
{
m_config->option<ConfigOptionStrings>("gcode_substitutions")->values.clear();
m_substitutions.clear();
m_config->option<ConfigOptionStrings>("gcode_substitutions")->values.clear();
call_ui_update();
if (!m_grid_sizer->IsEmpty())
@ -4756,13 +4777,13 @@ void SubstitutionManager::delete_all()
void SubstitutionManager::edit_substitution(int substitution_id, int opt_pos, const std::string& value)
{
std::vector<std::string>& substitutions = m_config->option<ConfigOptionStrings>("gcode_substitutions")->values;
validate_lenth();
if(!is_compatibile_with_ui() || !is_valid_id(substitution_id, "Invalid substitution_id to edit"))
return;
substitutions[substitution_id * 4 + opt_pos] = value;
m_substitutions[substitution_id * 4 + opt_pos] = value;
// save changes from m_substitutions to config
m_config->option<ConfigOptionStrings>("gcode_substitutions")->values = m_substitutions;
call_ui_update();
}

View File

@ -70,6 +70,8 @@ class SubstitutionManager
std::function<void()> m_cb_edited_substitution{ nullptr };
std::function<void()> m_cb_hide_delete_all_btn{ nullptr };
std::vector<std::string> m_substitutions;
void validate_lenth();
bool is_compatibile_with_ui();
bool is_valid_id(int substitution_id, const wxString& message);