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