Debian Codegraph @

radiant/ui/filters/editor/FilterDialog.cpp

raw
1 #include "FilterDialog.h"
2
3 #include "i18n.h"
4 #include "ifilter.h"
5 #include "ui/imainframe.h"
6 #include "ui/idialogmanager.h"
7
8 #include "ui/filters/FiltersMainMenu.h"
9
10 #include <wx/panel.h>
11 #include <wx/stattext.h>
12 #include <wx/button.h>
13
14 #include "FilterEditor.h"
15
16 namespace ui
17 {
18
19 namespace
20 {
21 const char* const WINDOW_TITLE = N_("Filter Settings");
22
23 enum Buttons
24 {
25 WIDGET_ADD_FILTER_BUTTON,
26 WIDGET_EDIT_FILTER_BUTTON,
27 WIDGET_VIEW_FILTER_BUTTON,
28 WIDGET_COPY_FILTER_BUTTON,
29 WIDGET_DELETE_FILTER_BUTTON,
30 };
31 }
32
33 FilterDialog::FilterDialog() :
34 DialogBase(_(WINDOW_TITLE)),
35 _filterStore(new wxutil::TreeModel(_columns, true))
36 {
37 // Create the child widgets
38 populateWindow();
39
40 // Load the filters from the filtersystem
41 loadFilters();
42
43 // Refresh dialog contents
44 update();
45
46 FitToScreen(0.4f, 0.6f);
47 }
48
49 void FilterDialog::save()
50 {
51 // In order to have all menu items removed from any event objects
52 // we need to deconstruct the filters menu first
53 FiltersMenu::removeItemsFromMainMenu();
54
55 // Delete filters marked for removal
56 for (FilterMap::const_iterator i = _deletedFilters.begin(); i != _deletedFilters.end(); ++i)
57 {
58 GlobalFilterSystem().removeFilter(i->first);
59 }
60
61 // Save all remaining filters
62 for (FilterMap::const_iterator i = _filters.begin(); i != _filters.end(); ++i)
63 {
64 // Check if the name has changed (or a new filters has been defined)
65 if (i->second->nameHasChanged())
66 {
67 // New filters have their original name set to the empty string
68 if (i->second->getOriginalName().empty())
69 {
70 // Insert a new filter
71 GlobalFilterSystem().addFilter(i->second->name, i->second->rules);
72 }
73 else
74 {
75 // Existing filer, issue the rename command
76 GlobalFilterSystem().renameFilter(i->second->getOriginalName(), i->second->name);
77 }
78 }
79
80 // Save the ruleset (to the new name, in case the filter has been renamed)
81 GlobalFilterSystem().setFilterRules(i->first, i->second->rules);
82 }
83
84 // Trigger an update
85 GlobalFilterSystem().update();
86
87 // Re-build the filters menu
88 FiltersMenu::addItemsToMainMenu();
89 }
90
91 void FilterDialog::loadFilters()
92 {
93 // Clear first, before population
94 _filters.clear();
95
96 GlobalFilterSystem().forEachFilter([&] (const std::string& filterName)
97 {
98 // Get the properties
99 bool state = GlobalFilterSystem().getFilterState(filterName);
100 bool readOnly = GlobalFilterSystem().filterIsReadOnly(filterName);
101
102 auto result = _filters.emplace(
103 filterName, std::make_shared<Filter>(filterName, state, readOnly)
104 );
105
106 // Copy the ruleset from the given filter
107 result.first->second->rules = GlobalFilterSystem().getRuleSet(filterName);
108 });
109 }
110
111 void FilterDialog::update()
112 {
113 // Clear the store first
114 _filterStore->Clear();
115
116 wxDataViewItemAttr black;
117 black.SetColour(wxColor(0,0,0));
118
119 wxDataViewItemAttr grey;
120 grey.SetColour(wxColor(112,112,112));
121
122 for (FilterMap::const_iterator i = _filters.begin(); i != _filters.end(); ++i)
123 {
124 const Filter& filter = *(i->second);
125
126 wxutil::TreeModel::Row row = _filterStore->AddItem();
127
128 row[_columns.name] = i->first;
129 row[_columns.state] = filter.state ? std::string(_("enabled")) : std::string(_("disabled"));
130
131 row[_columns.name].setAttr(filter.readOnly ? grey : black);
132 row[_columns.state].setAttr(filter.readOnly ? grey : black);
133
134 row[_columns.readonly] = filter.readOnly;
135
136 row.SendItemAdded();
137 }
138
139 // Update the button sensitivity
140 updateWidgetSensitivity();
141 }
142
143 void FilterDialog::populateWindow()
144 {
145 loadNamedPanel(this, "FilterDialogMainPanel");
146
147 wxStaticText* label = findNamedObject<wxStaticText>(this, "FilterDialogTopLabel");
148 label->SetFont(label->GetFont().Bold());
149
150 // Pack the treeview into the main window's vbox
151 createFiltersPanel();
152
153 wxButton* okButton = findNamedObject<wxButton>(this, "FilterDialogOkButton");
154 wxButton* cancelButton = findNamedObject<wxButton>(this, "FilterDialogCancelButton");
155
156 okButton->Connect(wxEVT_BUTTON, wxCommandEventHandler(FilterDialog::onSave), NULL, this);
157 cancelButton->Connect(wxEVT_BUTTON, wxCommandEventHandler(FilterDialog::onCancel), NULL, this);
158 }
159
160 void FilterDialog::createFiltersPanel()
161 {
162 wxPanel* parent = findNamedObject<wxPanel>(this, "FilterDialogTreeViewPanel");
163
164 // Create a new treeview
165 _filterView = wxutil::TreeView::CreateWithModel(parent, _filterStore.get());
166
167 _filterView->Connect(wxEVT_DATAVIEW_SELECTION_CHANGED,
168 wxDataViewEventHandler(FilterDialog::onFilterSelectionChanged), NULL, this);
169
170 parent->GetSizer()->Add(_filterView, 1, wxEXPAND | wxLEFT, 12);
171
172 // Display name column with icon
173 _filterView->AppendTextColumn(_("Name"), _columns.name.getColumnIndex(),
174 wxDATAVIEW_CELL_INERT, wxCOL_WIDTH_AUTOSIZE, wxALIGN_NOT, wxDATAVIEW_COL_SORTABLE);
175
176 _filterView->AppendTextColumn(_("State"), _columns.state.getColumnIndex(),
177 wxDATAVIEW_CELL_INERT, wxCOL_WIDTH_AUTOSIZE, wxALIGN_NOT, wxDATAVIEW_COL_SORTABLE);
178
179 // Action buttons
180 _buttons[WIDGET_ADD_FILTER_BUTTON] = findNamedObject<wxButton>(this, "FilterDialogAddButton");
181 _buttons[WIDGET_EDIT_FILTER_BUTTON] = findNamedObject<wxButton>(this, "FilterDialogEditButton");;
182 _buttons[WIDGET_VIEW_FILTER_BUTTON] = findNamedObject<wxButton>(this, "FilterDialogViewButton");;
183 _buttons[WIDGET_DELETE_FILTER_BUTTON] = findNamedObject<wxButton>(this, "FilterDialogDeleteButton");;
184 _buttons[WIDGET_COPY_FILTER_BUTTON] = findNamedObject<wxButton>(this, "FilterDialogCopyButton");;
185
186 _buttons[WIDGET_ADD_FILTER_BUTTON]->Bind(wxEVT_BUTTON, &FilterDialog::onAddFilter, this);
187 _buttons[WIDGET_EDIT_FILTER_BUTTON]->Bind(wxEVT_BUTTON, &FilterDialog::onEditFilter, this);
188 _buttons[WIDGET_VIEW_FILTER_BUTTON]->Bind(wxEVT_BUTTON, &FilterDialog::onViewFilter, this);
189 _buttons[WIDGET_DELETE_FILTER_BUTTON]->Bind(wxEVT_BUTTON, &FilterDialog::onDeleteFilter, this);
190 _buttons[WIDGET_COPY_FILTER_BUTTON]->Bind(wxEVT_BUTTON, &FilterDialog::onCopyFilter, this);
191 }
192
193 void FilterDialog::updateWidgetSensitivity()
194 {
195 if (!_selectedFilter.empty())
196 {
197 // We have a filter, is it read-only?
198 FilterMap::const_iterator i = _filters.find(_selectedFilter);
199
200 if (i != _filters.end())
201 {
202 _buttons[WIDGET_EDIT_FILTER_BUTTON]->Show(!i->second->readOnly);
203 _buttons[WIDGET_VIEW_FILTER_BUTTON]->Show(i->second->readOnly);
204
205 _buttons[WIDGET_EDIT_FILTER_BUTTON]->GetContainingSizer()->Layout();
206
207 _buttons[WIDGET_DELETE_FILTER_BUTTON]->Enable(!i->second->readOnly);
208 _buttons[WIDGET_EDIT_FILTER_BUTTON]->Enable(!i->second->readOnly);
209 _buttons[WIDGET_VIEW_FILTER_BUTTON]->Enable(i->second->readOnly);
210 _buttons[WIDGET_COPY_FILTER_BUTTON]->Enable(true); // always possible to copy filters
211
212 return;
213 }
214 }
215
216 // no valid filter selected
217 _buttons[WIDGET_DELETE_FILTER_BUTTON]->Enable(false);
218 _buttons[WIDGET_EDIT_FILTER_BUTTON]->Enable(false);
219 _buttons[WIDGET_VIEW_FILTER_BUTTON]->Enable(false);
220 _buttons[WIDGET_COPY_FILTER_BUTTON]->Enable(false);
221
222 _buttons[WIDGET_EDIT_FILTER_BUTTON]->Hide();
223 _buttons[WIDGET_VIEW_FILTER_BUTTON]->Show();
224
225 _buttons[WIDGET_EDIT_FILTER_BUTTON]->GetContainingSizer()->Layout();
226 }
227
228 void FilterDialog::ShowDialog(const cmd::ArgumentList& args)
229 {
230 // Instantiate a new instance, blocks GTK
231 FilterDialog* instance = new FilterDialog;
232
233 instance->ShowModal();
234 instance->Destroy();
235 }
236
237 void FilterDialog::onCancel(wxCommandEvent& ev)
238 {
239 // destroy dialog without saving
240 EndModal(wxID_CANCEL);
241 }
242
243 void FilterDialog::onSave(wxCommandEvent& ev)
244 {
245 // Save changes
246 save();
247
248 // Close the dialog
249 EndModal(wxID_OK);
250 }
251
252 void FilterDialog::showEditDialogForNewFilter(const FilterPtr& newFilter)
253 {
254 // Instantiate a new editor, will block
255 auto* editor = new FilterEditor(*newFilter, this, false);
256
257 auto editorResult = editor->ShowModal();
258
259 editor->Destroy();
260
261 if (editorResult != wxID_OK)
262 {
263 // User hit cancel, we're done
264 return;
265 }
266
267 if (newFilter->rules.empty())
268 {
269 // Empty ruleset, notify user
270 auto dialog = GlobalDialogManager().createMessageBox(_("Empty Filter"),
271 _("No rules defined for this filter, cannot insert."), ui::IDialog::MESSAGE_ERROR);
272
273 dialog->run();
274 return;
275 }
276
277 auto result = _filters.emplace(newFilter->name, newFilter);
278
279 if (!result.second)
280 {
281 // Empty ruleset, notify user
282 auto dialog = GlobalDialogManager().createMessageBox(_("Name Conflict"),
283 _("Cannot add, filter with same name already exists."), ui::IDialog::MESSAGE_ERROR);
284
285 dialog->run();
286 return;
287 }
288
289 update();
290 }
291
292 void FilterDialog::onAddFilter(wxCommandEvent& ev)
293 {
294 // Construct a new filter with an empty name (this indicates it has not been there before when saving)
295 auto newFilter = std::make_shared<Filter>("", false, false);
296 newFilter->name = _("NewFilter");
297
298 showEditDialogForNewFilter(newFilter);
299 }
300
301 void FilterDialog::onViewFilter(wxCommandEvent& ev)
302 {
303 // Lookup the Filter object
304 FilterMap::iterator f = _filters.find(_selectedFilter);
305
306 if (f == _filters.end()) {
307 return; // not found
308 }
309
310 // Construct a new filter
311 Filter workingCopy(*(f->second));
312
313 // Instantiate a new editor
314 FilterEditor* editor = new FilterEditor(workingCopy, this, true);
315 editor->ShowModal();
316 editor->Destroy();
317 }
318
319 void FilterDialog::onEditFilter(wxCommandEvent& ev)
320 {
321 // Lookup the Filter object
322 FilterMap::iterator f = _filters.find(_selectedFilter);
323
324 if (f == _filters.end() || f->second->readOnly) {
325 return; // not found or read-only
326 }
327
328 // Copy-construct a new filter
329 Filter workingCopy(*(f->second));
330
331 // Instantiate a new editor
332 FilterEditor* editor = new FilterEditor(workingCopy, this, false);
333
334 int editorResult = editor->ShowModal();
335
336 editor->Destroy();
337
338 if (editorResult != wxID_OK)
339 {
340 // User hit cancel, we're done
341 return;
342 }
343
344 if (workingCopy.rules.empty())
345 {
346 // Empty ruleset, ask user for deletion
347 IDialogPtr dialog = GlobalDialogManager().createMessageBox(_("Empty Filter"),
348 _("No rules defined for this filter. Delete it?"), ui::IDialog::MESSAGE_ASK);
349
350 if (dialog->run() == IDialog::RESULT_YES)
351 {
352 // Move the object from _filters to _deletedfilters
353 _deletedFilters.insert(*f);
354 _filters.erase(f);
355 }
356 else
357 {
358 // Don't delete the empty filter, leave the old one alone
359 }
360 }
361 else
362 {
363 // Ruleset is ok, has the name changed?
364 if (workingCopy.name != f->first)
365 {
366 // Name has changed, relocate the filter object
367 _filters.erase(f->first);
368 _filters[workingCopy.name] = FilterPtr(new Filter(workingCopy));
369 }
370 else
371 {
372 // No name change, just overwrite the filter object
373 *(f->second) = workingCopy;
374 }
375 }
376
377 // Update all widgets
378 update();
379 }
380
381 void FilterDialog::onDeleteFilter(wxCommandEvent& ev)
382 {
383 // Lookup the Filter object
384 FilterMap::iterator f = _filters.find(_selectedFilter);
385
386 if (f == _filters.end() || f->second->readOnly)
387 {
388 return; // not found or read-only
389 }
390
391 // Move the object from _filters to _deletedfilters
392 _deletedFilters.insert(*f);
393 _filters.erase(f);
394
395 // Update all widgets
396 update();
397 }
398
399 void FilterDialog::onCopyFilter(wxCommandEvent& ev)
400 {
401 // Lookup the Filter object
402 FilterMap::iterator f = _filters.find(_selectedFilter);
403
404 if (f == _filters.end())
405 {
406 return; // not found or read-only
407 }
408
409 // Construct a new filter with an empty name (this indicates it has not been there before when saving)
410 auto newFilter = std::make_shared<Filter>("", false, false);
411 newFilter->name = f->second->name + " " + _("Copy");
412
413 // Copy all the rules
414 for (const auto& existingRule : f->second->rules)
415 {
416 newFilter->rules.push_back(existingRule);
417 }
418
419 showEditDialogForNewFilter(newFilter);
420 }
421
422 void FilterDialog::onFilterSelectionChanged(wxDataViewEvent& ev)
423 {
424 wxDataViewItem item = _filterView->GetSelection();
425
426 if (item.IsOk())
427 {
428 wxutil::TreeModel::Row row(item, *_filterStore);
429 _selectedFilter = row[_columns.name];
430 }
431 else
432 {
433 _selectedFilter.clear();
434 }
435
436 updateWidgetSensitivity();
437 }
438
439 } // namespace ui