From a71c7c728f50d7f923b4492db2c1178c6aaeee53 Mon Sep 17 00:00:00 2001 From: codereader Date: Sun, 8 Aug 2021 16:31:06 +0200 Subject: [PATCH] #5697: Implement the copy filter functionality --- radiant/ui/filters/editor/FilterDialog.cpp | 84 ++++++++++++---------- radiant/ui/filters/editor/FilterDialog.h | 2 + 2 files changed, 49 insertions(+), 37 deletions(-) diff --git a/radiant/ui/filters/editor/FilterDialog.cpp b/radiant/ui/filters/editor/FilterDialog.cpp index fd0931324a..9630c530ba 100644 --- a/radiant/ui/filters/editor/FilterDialog.cpp +++ b/radiant/ui/filters/editor/FilterDialog.cpp @@ -249,50 +249,53 @@ void FilterDialog::onSave(wxCommandEvent& ev) EndModal(wxID_OK); } -void FilterDialog::onAddFilter(wxCommandEvent& ev) +void FilterDialog::showEditDialogForNewFilter(const FilterPtr& newFilter) { - // Construct a new filter with an empty name (this indicates it has not been there before when saving) - FilterPtr workingCopy(new Filter("", false, false)); - workingCopy->name = _("NewFilter"); + // Instantiate a new editor, will block + auto* editor = new FilterEditor(*newFilter, this, false); - // Instantiate a new editor, will block - FilterEditor* editor = new FilterEditor(*workingCopy, this, false); + auto editorResult = editor->ShowModal(); - int editorResult = editor->ShowModal(); - - editor->Destroy(); + editor->Destroy(); - if (editorResult != wxID_OK) - { - // User hit cancel, we're done - return; - } + if (editorResult != wxID_OK) + { + // User hit cancel, we're done + return; + } - if (workingCopy->rules.empty()) - { - // Empty ruleset, notify user - IDialogPtr dialog = GlobalDialogManager().createMessageBox(_("Empty Filter"), - _("No rules defined for this filter, cannot insert."), ui::IDialog::MESSAGE_ERROR); + if (newFilter->rules.empty()) + { + // Empty ruleset, notify user + auto dialog = GlobalDialogManager().createMessageBox(_("Empty Filter"), + _("No rules defined for this filter, cannot insert."), ui::IDialog::MESSAGE_ERROR); - dialog->run(); - return; - } + dialog->run(); + return; + } - std::pair result = _filters.insert( - FilterMap::value_type(workingCopy->name, workingCopy) - ); + auto result = _filters.emplace(newFilter->name, newFilter); - if (!result.second) - { - // Empty ruleset, notify user - IDialogPtr dialog = GlobalDialogManager().createMessageBox(_("Name Conflict"), - _("Cannot add, filter with same name already exists."), ui::IDialog::MESSAGE_ERROR); + if (!result.second) + { + // Empty ruleset, notify user + auto dialog = GlobalDialogManager().createMessageBox(_("Name Conflict"), + _("Cannot add, filter with same name already exists."), ui::IDialog::MESSAGE_ERROR); - dialog->run(); - return; - } + dialog->run(); + return; + } - update(); + update(); +} + +void FilterDialog::onAddFilter(wxCommandEvent& ev) +{ + // Construct a new filter with an empty name (this indicates it has not been there before when saving) + auto newFilter = std::make_shared("", false, false); + newFilter->name = _("NewFilter"); + + showEditDialogForNewFilter(newFilter); } void FilterDialog::onViewFilter(wxCommandEvent& ev) @@ -403,10 +406,17 @@ void FilterDialog::onCopyFilter(wxCommandEvent& ev) return; // not found or read-only } - // TODO: Copy + // Construct a new filter with an empty name (this indicates it has not been there before when saving) + auto newFilter = std::make_shared("", false, false); + newFilter->name = f->second->name + " " + _("Copy"); - // Update all widgets - update(); + // Copy all the rules + for (const auto& existingRule : f->second->rules) + { + newFilter->rules.push_back(existingRule); + } + + showEditDialogForNewFilter(newFilter); } void FilterDialog::onFilterSelectionChanged(wxDataViewEvent& ev) diff --git a/radiant/ui/filters/editor/FilterDialog.h b/radiant/ui/filters/editor/FilterDialog.h index 22a8b8d717..10a46d3e5c 100644 --- a/radiant/ui/filters/editor/FilterDialog.h +++ b/radiant/ui/filters/editor/FilterDialog.h @@ -88,6 +88,8 @@ class FilterDialog : void onDeleteFilter(wxCommandEvent& ev); void onCopyFilter(wxCommandEvent& ev); + void showEditDialogForNewFilter(const FilterPtr& filter); + void onFilterSelectionChanged(wxDataViewEvent& ev); };