Debian Codegraph @

plugins/dm.stimresponse/ResponseEditor.cpp

raw
1 #include "ResponseEditor.h"
2
3 #include "wxutil/dataview/TreeModel.h"
4 #include "wxutil/menu/IconTextMenuItem.h"
5 #include "string/convert.h"
6
7 #include "i18n.h"
8 #include "EffectEditor.h"
9
10 #include <wx/combobox.h>
11 #include <wx/bmpcbox.h>
12 #include <wx/button.h>
13 #include <wx/menu.h>
14 #include <wx/spinctrl.h>
15 #include <wx/textctrl.h>
16 #include <wx/checkbox.h>
17
18 #include "wxutil/ChoiceHelper.h"
19
20 namespace ui
21 {
22
23 ResponseEditor::ResponseEditor(wxWindow* mainPanel, StimTypes& stimTypes) :
24 ClassEditor(mainPanel, stimTypes),
25 _mainPanel(mainPanel)
26 {
27 setupPage();
28
29 createContextMenu();
30 update();
31 }
32
33 void ResponseEditor::setEntity(const SREntityPtr& entity)
34 {
35 // Pass the call to the base class
36 ClassEditor::setEntity(entity);
37
38 if (entity)
39 {
40 wxutil::TreeModel::Ptr responseStore = _entity->getResponseStore();
41 _list->AssociateModel(responseStore.get());
42
43 // Trigger column width reevaluation
44 responseStore->ItemChanged(responseStore->GetRoot());
45
46 // Clear the treeview
47 wxutil::TreeModel* effectsModel =
48 static_cast<wxutil::TreeModel*>(_effectWidgets.view->GetModel());
49 effectsModel->Clear();
50 }
51 else
52 {
53 // wxWidgets 3.0.0 crashes when associating a NULL model, so use a dummy model
54 // to release the old one
55 wxutil::TreeModel* dummyStore = new wxutil::TreeModel(SREntity::getColumns(), true);
56 _list->AssociateModel(dummyStore);
57 }
58 }
59
60 void ResponseEditor::update()
61 {
62 _updatesDisabled = true;
63
64 wxPanel* mainPanel = findNamedObject<wxPanel>(_mainPanel, "SREditorResponsePanel");
65 auto removeButton = findNamedObject<wxButton>(_mainPanel, "RemoveResponseButton");
66
67 int index = getIndexFromSelection();
68
69 if (index > 0 && _entity != NULL)
70 {
71 mainPanel->Enable(true);
72
73 StimResponse& sr = _entity->get(index);
74
75 // Get the iter into the liststore pointing at the correct STIM_YYYY type
76 std::string typeToFind = sr.get("type");
77 wxutil::ChoiceHelper:: SelectComboItemByStoredString (_type, typeToFind);
78
79 // Active
80 _propertyWidgets.active->SetValue(sr.get("state") == "1");
81
82 // Use Radius
83 bool useRandomEffects = (sr.get("random_effects") != "");
84 _propertyWidgets.randomEffectsToggle->SetValue(useRandomEffects);
85 _propertyWidgets.randomEffectsEntry->SetValue(sr.get("random_effects"));
86 _propertyWidgets.randomEffectsEntry->Enable(useRandomEffects);
87
88 // Use Chance
89 bool useChance = (sr.get("chance") != "");
90 _propertyWidgets.chanceToggle->SetValue(useChance);
91 _propertyWidgets.chanceEntry->SetValue(string::convert<double>(sr.get("chance")));
92 _propertyWidgets.chanceEntry->Enable(useChance);
93
94 wxutil::TreeModel::Ptr effectsModel = sr.createEffectsStore();
95
96 // It's important to unselect everything before swapping the model
97 // otherwise wxDataViewCtrl will keep invalid items in its internal selection list
98 _effectWidgets.view->UnselectAll();
99
100 _effectWidgets.view->AssociateModel(effectsModel.get());
101 effectsModel->ItemChanged(effectsModel->GetRoot()); // trigger column width re-evaluation
102
103 // Disable the editing of inherited properties completely
104 if (sr.inherited())
105 {
106 mainPanel->Enable(false);
107 }
108
109 // Update the delete context menu item
110 _contextMenu.remove->Enable(!sr.inherited());
111 removeButton->Enable(!sr.inherited());
112
113 // If there is anything selected, the duplicate item is always active
114 _contextMenu.duplicate->Enable(true);
115
116 // Update the "enable/disable" menu items
117 bool state = sr.get("state") == "1";
118 _contextMenu.enable->Enable(!state);
119 _contextMenu.disable->Enable(state);
120
121 // The response effect list may be empty, so force an update of the
122 // context menu sensitivity, in the case the "selection changed"
123 // signal doesn't get called
124 updateEffectContextMenu();
125 }
126 else
127 {
128 // Nothing selected
129 mainPanel->Enable(false);
130
131 // Clear the effect tree view
132 wxutil::TreeModel* effectsModel =
133 static_cast<wxutil::TreeModel*>(_effectWidgets.view->GetModel());
134 effectsModel->Clear();
135
136 _contextMenu.enable->Enable(false);
137 _contextMenu.disable->Enable(false);
138 _contextMenu.remove->Enable(false);
139 _contextMenu.duplicate->Enable(false);
140
141 removeButton->Enable(false);
142 }
143
144 _updatesDisabled = false;
145 }
146
147 void ResponseEditor::setupPage()
148 {
149 wxPanel* listPanel = findNamedObject<wxPanel>(_mainPanel, "SREditorResponseList");
150 createListView(listPanel);
151
152 #ifdef USE_BMP_COMBO_BOX
153 // Response property section
154 _type = findNamedObject<wxBitmapComboBox>(_mainPanel, "ResponseEditorTypeCombo");
155 #else
156 {
157 // Response property section
158 wxControl* typeBox = findNamedObject<wxControl>(_mainPanel, "ResponseEditorTypeCombo");
159
160 // Replace the bitmap combo with an ordinary one
161 wxComboBox* combo = new wxComboBox(typeBox->GetParent(), wxID_ANY);
162 typeBox->GetContainingSizer()->Add(combo, 1, wxEXPAND);
163 typeBox->Destroy();
164
165 _type = combo;
166 _type->SetName("ResponseEditorTypeCombo");
167 }
168 #endif
169
170 _type->Connect(wxEVT_COMBOBOX, wxCommandEventHandler(ResponseEditor::onStimTypeSelect), NULL, this);
171
172 // Active
173 _propertyWidgets.active = findNamedObject<wxCheckBox>(_mainPanel, "ResponseEditorActive");
174
175 // Random Effects Toggle
176 _propertyWidgets.randomEffectsToggle = findNamedObject<wxCheckBox>(_mainPanel, "ResponseEditorRandomFX");
177 _propertyWidgets.randomEffectsEntry = findNamedObject<wxTextCtrl>(_mainPanel, "ResponseEditorRandomFXValue");
178
179 // Chance variable
180 _propertyWidgets.chanceToggle = findNamedObject<wxCheckBox>(_mainPanel, "ResponseEditorChance");
181
182 wxPanel* chancePanel = findNamedObject<wxPanel>(_mainPanel, "ResponseEditorChanceValuePanel");
183
184 _propertyWidgets.chanceEntry = new wxSpinCtrlDouble(chancePanel, wxID_ANY);
185 _propertyWidgets.chanceEntry->SetRange(0.0, 1.0);
186 _propertyWidgets.chanceEntry->SetIncrement(0.01);
187 _propertyWidgets.chanceEntry->SetValue(0);
188
189 chancePanel->GetSizer()->Add(_propertyWidgets.chanceEntry, 1);
190
191 // Connect the signals
192 connectCheckButton(_propertyWidgets.active);
193 connectCheckButton(_propertyWidgets.randomEffectsToggle);
194 connectCheckButton(_propertyWidgets.chanceToggle);
195
196 connectEntry(_propertyWidgets.randomEffectsEntry, "random_effects");
197
198 connectSpinButton(_propertyWidgets.chanceEntry, "chance");
199
200 makeLabelBold(_mainPanel, "ResponseEditorFXLabel");
201
202 createEffectWidgets();
203
204 #ifdef USE_BMP_COMBO_BOX
205 _addType = findNamedObject<wxBitmapComboBox>(_mainPanel, "ResponseTypeComboBox");
206 #else
207 {
208 // Type selector box
209 wxControl* addTypeBox = findNamedObject<wxControl>(_mainPanel, "ResponseTypeComboBox");
210
211 // Replace the bitmap combo with an ordinary one
212 wxComboBox* newTypeCombo = new wxComboBox(addTypeBox->GetParent(), wxID_ANY);
213 addTypeBox->GetContainingSizer()->Prepend(newTypeCombo, 1, wxEXPAND | wxRIGHT, 6);
214 addTypeBox->Destroy();
215
216 _addType = newTypeCombo;
217 _addType->SetName("ResponseTypeComboBox");
218 }
219 #endif
220 _addType->Bind(wxEVT_COMBOBOX, std::bind(&ResponseEditor::onAddTypeSelect, this, std::placeholders::_1));
221
222 auto addButton = findNamedObject<wxButton>(_mainPanel, "AddResponseButton");
223 auto removeButton = findNamedObject<wxButton>(_mainPanel, "RemoveResponseButton");
224
225 addButton->Bind(wxEVT_BUTTON, std::bind(&ResponseEditor::onAddSR, this, std::placeholders::_1));
226 removeButton->Bind(wxEVT_BUTTON, std::bind(&ResponseEditor::onRemoveSR, this, std::placeholders::_1));
227
228 reloadStimTypes();
229 }
230
231 void ResponseEditor::reloadStimTypes()
232 {
233 if (_stimTypes.getStimMap().empty())
234 {
235 _stimTypes.reload();
236 }
237
238 _stimTypes.populateComboBox(_addType);
239 _stimTypes.populateComboBox(_type);
240 }
241
242 void ResponseEditor::createEffectWidgets()
243 {
244 wxPanel* effectsPanel = findNamedObject<wxPanel>(_mainPanel, "ResponseEditorFXPanel");
245
246 wxutil::TreeModel::Ptr dummyModel(
247 new wxutil::TreeModel(StimResponse::getColumns(), true)
248 );
249 _effectWidgets.view = wxutil::TreeView::CreateWithModel(effectsPanel, dummyModel.get());
250
251 _effectWidgets.view->SetMinClientSize(wxSize(-1, 150));
252 effectsPanel->GetSizer()->Add(_effectWidgets.view, 1, wxEXPAND);
253
254 // Connect the signals
255 _effectWidgets.view->Connect(wxEVT_DATAVIEW_SELECTION_CHANGED,
256 wxDataViewEventHandler(ResponseEditor::onEffectSelectionChange), nullptr, this);
257
258 _effectWidgets.view->Connect(wxEVT_DATAVIEW_ITEM_ACTIVATED,
259 wxDataViewEventHandler(ResponseEditor::onEffectItemActivated), nullptr, this);
260
261 _effectWidgets.view->Connect(wxEVT_DATAVIEW_ITEM_CONTEXT_MENU,
262 wxDataViewEventHandler(ResponseEditor::onEffectItemContextMenu), nullptr, this);
263
264 // View Columns
265 _effectWidgets.view->AppendTextColumn("#", StimResponse::getColumns().index.getColumnIndex(),
266 wxDATAVIEW_CELL_INERT, wxCOL_WIDTH_AUTOSIZE, wxALIGN_NOT);
267
268 _effectWidgets.view->AppendTextColumn(_("Effect"), StimResponse::getColumns().caption.getColumnIndex(),
269 wxDATAVIEW_CELL_INERT, wxCOL_WIDTH_AUTOSIZE, wxALIGN_NOT);
270
271 _effectWidgets.view->AppendTextColumn(_("Details (double-click to edit)"), StimResponse::getColumns().arguments.getColumnIndex(),
272 wxDATAVIEW_CELL_INERT, wxCOL_WIDTH_AUTOSIZE, wxALIGN_NOT);
273 }
274
275 void ResponseEditor::checkBoxToggled(wxCheckBox* toggleButton)
276 {
277 bool active = toggleButton->GetValue();
278
279 if (toggleButton == _propertyWidgets.active)
280 {
281 setProperty("state", active ? "1" : "0");
282 }
283 else if (toggleButton == _propertyWidgets.randomEffectsToggle)
284 {
285 std::string entryText = _propertyWidgets.randomEffectsEntry->GetValue().ToStdString();
286
287 // Enter a default value for the entry text, if it's empty up till now.
288 if (active)
289 {
290 entryText += (entryText.empty()) ? "1" : "";
291 }
292 else {
293 entryText = "";
294 }
295
296 setProperty("random_effects", entryText);
297 }
298 else if (toggleButton == _propertyWidgets.chanceToggle)
299 {
300 std::string entryText = string::to_string(_propertyWidgets.chanceEntry->GetValue());
301
302 setProperty("chance", active ? entryText : "");
303 }
304 }
305
306 void ResponseEditor::addEffect()
307 {
308 if (_entity == nullptr) return;
309
310 int index = getIndexFromSelection();
311
312 if (index > 0)
313 {
314 StimResponse& sr = _entity->get(index);
315 int effectIndex = getEffectIdFromSelection();
316
317 // Make sure we have a response
318 if (sr.get("class") == "R") {
319 // Add a new effect and update all the widgets
320 sr.addEffect(effectIndex);
321 update();
322 }
323 }
324 }
325
326 void ResponseEditor::removeEffect()
327 {
328 if (_entity == nullptr) return;
329
330 int index = getIndexFromSelection();
331
332 if (index > 0)
333 {
334 StimResponse& sr = _entity->get(index);
335 int effectIndex = getEffectIdFromSelection();
336
337 // Make sure we have a response and anything selected
338 if (sr.get("class") == "R" && effectIndex > 0)
339 {
340 // Remove the effect and update all the widgets
341 sr.deleteEffect(effectIndex);
342 update();
343 }
344 }
345 }
346
347 void ResponseEditor::editEffect()
348 {
349 if (_entity == nullptr) return;
350
351 int index = getIndexFromSelection();
352
353 if (index > 0)
354 {
355 StimResponse& sr = _entity->get(index);
356 int effectIndex = getEffectIdFromSelection();
357
358 // Make sure we have a response and anything selected
359 if (sr.get("class") == "R" && effectIndex > 0)
360 {
361 // Create a new effect editor (self-destructs)
362 EffectEditor* editor = new EffectEditor(_mainPanel, sr, effectIndex, _stimTypes, *this);
363
364 editor->ShowModal();
365 editor->Destroy();
366 }
367 }
368 }
369
370 void ResponseEditor::moveEffect(int direction)
371 {
372 if (_entity == NULL) return;
373
374 int index = getIndexFromSelection();
375
376 if (index > 0)
377 {
378 StimResponse& sr = _entity->get(index);
379 int effectIndex = getEffectIdFromSelection();
380
381 if (sr.get("class") == "R" && effectIndex > 0)
382 {
383 // Move the index (swap the specified indices)
384 sr.moveEffect(effectIndex, effectIndex + direction);
385 update();
386 // Select the moved effect after the update
387 selectEffectIndex(effectIndex + direction);
388 }
389 }
390 }
391
392 void ResponseEditor::updateEffectContextMenu()
393 {
394 // Check if we have anything selected at all
395 int curEffectIndex = getEffectIdFromSelection();
396 int highestEffectIndex = 0;
397
398 bool anythingSelected = curEffectIndex >= 0;
399
400 int index = getIndexFromSelection();
401
402 if (index > 0)
403 {
404 StimResponse& sr = _entity->get(index);
405 highestEffectIndex = sr.highestEffectIndex();
406 }
407
408 bool upActive = anythingSelected && curEffectIndex > 1;
409 bool downActive = anythingSelected && curEffectIndex < highestEffectIndex;
410
411 // Enable or disable the "Delete" context menu items based on the presence
412 // of a selection.
413 _effectWidgets.contextMenu->Enable(_effectWidgets.deleteMenuItem->GetId(), anythingSelected);
414 _effectWidgets.contextMenu->Enable(_effectWidgets.editMenuItem->GetId(), anythingSelected);
415
416 _effectWidgets.contextMenu->Enable(_effectWidgets.upMenuItem->GetId(), upActive);
417 _effectWidgets.contextMenu->Enable(_effectWidgets.downMenuItem->GetId(), downActive);
418 }
419
420 // Create the context menus
421 void ResponseEditor::createContextMenu()
422 {
423 // Menu widgets
424 _contextMenu.menu.reset(new wxMenu);
425
426 // Each menu gets a delete item
427 _contextMenu.enable = _contextMenu.menu->Append(
428 new wxutil::IconTextMenuItem(_("Activate"), "sr_response.png"));
429 _contextMenu.disable = _contextMenu.menu->Append(
430 new wxutil::IconTextMenuItem(_("Deactivate"), "sr_response_inactive.png"));
431 _contextMenu.duplicate = _contextMenu.menu->Append(
432 new wxutil::StockIconTextMenuItem(_("Duplicate"), wxART_COPY));
433 _contextMenu.remove = _contextMenu.menu->Append(
434 new wxutil::StockIconTextMenuItem(_("Delete"), wxART_DELETE));
435
436 _effectWidgets.contextMenu.reset(new wxMenu);
437
438 _effectWidgets.addMenuItem = _effectWidgets.contextMenu->Append(
439 new wxutil::StockIconTextMenuItem(_("Add new Effect"), wxART_PLUS));
440 _effectWidgets.editMenuItem = _effectWidgets.contextMenu->Append(
441 new wxutil::IconTextMenuItem(_("Edit"), "edit.png"));
442
443 _effectWidgets.upMenuItem = _effectWidgets.contextMenu->Append(
444 new wxutil::StockIconTextMenuItem(_("Move Up"), wxART_GO_UP));
445 _effectWidgets.downMenuItem = _effectWidgets.contextMenu->Append(
446 new wxutil::StockIconTextMenuItem(_("Move Down"), wxART_GO_DOWN));
447
448 _effectWidgets.deleteMenuItem = _effectWidgets.contextMenu->Append(
449 new wxutil::StockIconTextMenuItem(_("Delete"), wxART_DELETE));
450
451 // Connect up the signals
452 _contextMenu.menu->Connect(_contextMenu.remove->GetId(), wxEVT_MENU,
453 wxCommandEventHandler(ResponseEditor::onContextMenuDelete), NULL, this);
454 _contextMenu.menu->Connect(_contextMenu.enable->GetId(), wxEVT_MENU,
455 wxCommandEventHandler(ResponseEditor::onContextMenuEnable), NULL, this);
456 _contextMenu.menu->Connect(_contextMenu.disable->GetId(), wxEVT_MENU,
457 wxCommandEventHandler(ResponseEditor::onContextMenuDisable), NULL, this);
458 _contextMenu.menu->Connect(_contextMenu.duplicate->GetId(), wxEVT_MENU,
459 wxCommandEventHandler(ResponseEditor::onContextMenuDuplicate), NULL, this);
460
461 _effectWidgets.contextMenu->Connect(_effectWidgets.deleteMenuItem->GetId(), wxEVT_MENU,
462 wxCommandEventHandler(ResponseEditor::onEffectMenuDelete), NULL, this);
463 _effectWidgets.contextMenu->Connect(_effectWidgets.editMenuItem->GetId(), wxEVT_MENU,
464 wxCommandEventHandler(ResponseEditor::onEffectMenuEdit), NULL, this);
465 _effectWidgets.contextMenu->Connect(_effectWidgets.addMenuItem->GetId(), wxEVT_MENU,
466 wxCommandEventHandler(ResponseEditor::onEffectMenuAdd), NULL, this);
467 _effectWidgets.contextMenu->Connect(_effectWidgets.upMenuItem->GetId(), wxEVT_MENU,
468 wxCommandEventHandler(ResponseEditor::onEffectMenuEffectUp), NULL, this);
469 _effectWidgets.contextMenu->Connect(_effectWidgets.downMenuItem->GetId(), wxEVT_MENU,
470 wxCommandEventHandler(ResponseEditor::onEffectMenuEffectDown), NULL, this);
471 }
472
473 void ResponseEditor::selectEffectIndex(const unsigned int index)
474 {
475 wxutil::TreeModel* model = static_cast<wxutil::TreeModel*>(_effectWidgets.view->GetModel());
476
477 wxDataViewItem item = model->FindInteger(index, StimResponse::getColumns().index);
478
479 if (item.IsOk())
480 {
481 // Set the active row of the list to the given effect
482 _effectWidgets.view->Select(item);
483 }
484 }
485
486 int ResponseEditor::getEffectIdFromSelection()
487 {
488 wxDataViewItem item = _effectWidgets.view->GetSelection();
489
490 if (item.IsOk() && _entity != NULL)
491 {
492 wxutil::TreeModel::Row row(item, *_effectWidgets.view->GetModel());
493 return row[StimResponse::getColumns().index].getInteger();
494 }
495 else
496 {
497 return -1;
498 }
499 }
500
501 void ResponseEditor::openSRListContextMenu()
502 {
503 _list->PopupMenu(_contextMenu.menu.get());
504 }
505
506 void ResponseEditor::selectionChanged()
507 {
508 update();
509 }
510
511 void ResponseEditor::addSR()
512 {
513 if (_entity == NULL) return;
514
515 // Create a new StimResponse object
516 int index = _entity->add();
517
518 // Get a reference to the newly allocated object
519 StimResponse& sr = _entity->get(index);
520 sr.set("class", "R");
521
522 // Get the selected stim type name from the combo box
523 std::string name = getStimTypeIdFromSelector(_addType);
524 sr.set("type", (!name.empty()) ? name : _stimTypes.getFirstName());
525
526 sr.set("state", "1");
527
528 // Update the list stores AFTER the type has been set
529 _entity->updateListStores();
530
531 // Select the newly created response
532 selectIndex(index);
533 }
534
535 void ResponseEditor::onEffectItemContextMenu(wxDataViewEvent& ev)
536 {
537 updateEffectContextMenu();
538
539 _effectWidgets.view->PopupMenu(_effectWidgets.contextMenu.get());
540 }
541
542 // Button click events on TreeViews
543 void ResponseEditor::onEffectItemActivated(wxDataViewEvent& ev)
544 {
545 // Call the effect editor upon double click
546 editEffect();
547 }
548
549 void ResponseEditor::onEffectMenuDelete(wxCommandEvent& ev)
550 {
551 removeEffect();
552 }
553
554 void ResponseEditor::onEffectMenuEdit(wxCommandEvent& ev)
555 {
556 editEffect();
557 }
558
559 void ResponseEditor::onEffectMenuAdd(wxCommandEvent& ev)
560 {
561 addEffect();
562 }
563
564 void ResponseEditor::onEffectMenuEffectUp(wxCommandEvent& ev)
565 {
566 moveEffect(-1);
567 }
568
569 void ResponseEditor::onEffectMenuEffectDown(wxCommandEvent& ev)
570 {
571 moveEffect(+1);
572 }
573
574 // Delete context menu items activated
575 void ResponseEditor::onContextMenuDelete(wxCommandEvent& ev)
576 {
577 // Delete the selected stim from the list
578 removeSR();
579 }
580
581 // Delete context menu items activated
582 void ResponseEditor::onContextMenuAdd(wxCommandEvent& ev)
583 {
584 addSR();
585 }
586
587 // Callback for effects treeview selection changes
588 void ResponseEditor::onEffectSelectionChange(wxDataViewEvent& ev)
589 {
590 if (_updatesDisabled) return; // Callback loop guard
591
592 // Update the sensitivity
593 updateEffectContextMenu();
594 }
595
596 } // namespace ui