Debian Codegraph @

plugins/dm.stimresponse/StimResponseEditor.cpp

raw
1 #include "StimResponseEditor.h"
2
3 #include "iregistry.h"
4 #include "iundo.h"
5 #include "iscenegraph.h"
6 #include "itextstream.h"
7 #include "ui/imainframe.h"
8 #include "selectionlib.h"
9 #include "wxutil/dialog/MessageBox.h"
10 #include "string/string.h"
11
12 #include "i18n.h"
13 #include <iostream>
14
15 #include "StimEditor.h"
16 #include "ResponseEditor.h"
17
18 #include <wx/button.h>
19 #include <wx/artprov.h>
20
21 namespace ui
22 {
23
24 namespace
25 {
26 const char* const WINDOW_TITLE = N_("Stim/Response Editor");
27
28 const std::string RKEY_ROOT = "user/ui/stimResponseEditor/";
29 const std::string RKEY_WINDOW_STATE = RKEY_ROOT + "window";
30
31 const char* NO_ENTITY_ERROR = N_("A single entity must be selected to edit "
32 "Stim/Response properties.");
33 }
34
35 StimResponseEditor::StimResponseEditor() :
36 DialogBase(_(WINDOW_TITLE)),
37 _entity(nullptr),
38 _customStimEditor(nullptr)
39 {
40 // Create the widgets
41 populateWindow();
42
43 // Connect the window position tracker
44 _windowPosition.loadFromPath(RKEY_WINDOW_STATE);
45
46 _windowPosition.connect(this);
47 _windowPosition.applyPosition();
48 }
49
50 int StimResponseEditor::ShowModal()
51 {
52 // Restore the position
53 _windowPosition.applyPosition();
54
55 // Reload all the stim types, the map might have changed
56 _stimTypes.reload();
57
58 // Scan the selection for entities
59 rescanSelection();
60
61 // Has the rescan found an entity (the pointer is non-NULL then)
62 if (_entity != NULL)
63 {
64 // Show the last shown page
65 _notebook->SetSelection(_lastShownPage);
66 }
67
68 int returnCode = DialogBase::ShowModal();
69
70 if (returnCode == wxID_OK)
71 {
72 save();
73 }
74
75 _lastShownPage = _notebook->GetSelection();
76
77 // Tell the position tracker to save the information
78 _windowPosition.saveToPath(RKEY_WINDOW_STATE);
79
80 return returnCode;
81 }
82
83 void StimResponseEditor::populateWindow()
84 {
85 auto mainPanel = loadNamedPanel(this, "SREditorMainPanel");
86
87 _notebook = findNamedObject<wxNotebook>(this, "SREditorNotebook");
88
89 _stimEditor = std::make_unique<StimEditor>(mainPanel, _stimTypes);
90 _responseEditor = std::make_unique<ResponseEditor>(mainPanel, _stimTypes);
91
92 // Custom Stim Editor
93 auto customStimPanel = findNamedObject<wxPanel>(mainPanel, "SREditorCustomStimEditorContainer");
94 _customStimEditor = std::make_unique<CustomStimEditor>(customStimPanel, _stimTypes);
95
96 _notebook->Connect(wxEVT_NOTEBOOK_PAGE_CHANGED,
97 wxBookCtrlEventHandler(StimResponseEditor::onPageChanged), nullptr, this);
98
99 findNamedObject<wxButton>(this, "SREditorOkButton")->Bind(
100 wxEVT_BUTTON, [this](wxCommandEvent& ev) { EndModal(wxID_OK); });
101 findNamedObject<wxButton>(this, "SREditorCancelButton")->Bind(
102 wxEVT_BUTTON, [this](wxCommandEvent& ev) { EndModal(wxID_CANCEL); });
103
104 if (_lastShownPage == -1)
105 {
106 _lastShownPage = 0;
107 }
108
109 Layout();
110 Fit();
111 }
112
113 void StimResponseEditor::onPageChanged(wxBookCtrlEvent& ev)
114 {
115 // The stim type list might have changed using the CustomStimEditor,
116 // so let's update the controls when switching pages
117 if (_stimEditor != nullptr)
118 {
119 _stimEditor->reloadStimTypes();
120 }
121
122 if (_responseEditor != nullptr)
123 {
124 _responseEditor->reloadStimTypes();
125 }
126 }
127
128 void StimResponseEditor::rescanSelection()
129 {
130 const SelectionInfo& info = GlobalSelectionSystem().getSelectionInfo();
131
132 _entity = nullptr;
133 _srEntity.reset();
134 _stimEditor->setEntity(_srEntity);
135 _responseEditor->setEntity(_srEntity);
136 _customStimEditor->setEntity(_srEntity);
137
138 if (info.entityCount == 1 && info.totalCount == 1)
139 {
140 // Get the entity instance
141 const scene::INodePtr& node = GlobalSelectionSystem().ultimateSelected();
142
143 _entity = Node_getEntity(node);
144
145 _srEntity = SREntityPtr(new SREntity(_entity, _stimTypes));
146 _stimEditor->setEntity(_srEntity);
147 _responseEditor->setEntity(_srEntity);
148 _customStimEditor->setEntity(_srEntity);
149 }
150
151 if (_entity != NULL)
152 {
153 std::string title = _(WINDOW_TITLE);
154 title += " (" + _entity->getKeyValue("name") + ")";
155 SetTitle(title);
156 }
157 else
158 {
159 SetTitle(_(WINDOW_TITLE));
160 }
161 }
162
163 void StimResponseEditor::save()
164 {
165 // Consistency check can go here
166
167 // Scoped undo object
168 UndoableCommand command("editStimResponse");
169
170 // Save the working set to the entity
171 _srEntity->save(_entity);
172
173 // Save the custom stim types to the storage entity
174 _stimTypes.save();
175 }
176
177 bool StimResponseEditor::Destroy()
178 {
179 // We experience crashes in Linux/GTK during dialog destruction when GTK+
180 // apparently starts sending out the notebook page changed event right
181 // before removing the pages.
182 _notebook->Disconnect(wxEVT_NOTEBOOK_PAGE_CHANGED,
183 wxBookCtrlEventHandler(StimResponseEditor::onPageChanged), nullptr, this);
184
185 return wxutil::DialogBase::Destroy();
186 }
187
188 // Static command target
189 void StimResponseEditor::ShowDialog()
190 {
191 const SelectionInfo& info = GlobalSelectionSystem().getSelectionInfo();
192
193 if (info.entityCount == 1 && info.totalCount == 1)
194 {
195 // Construct a new instance, this enters the main loop
196 StimResponseEditor* editor = new StimResponseEditor;
197
198 editor->ShowModal();
199 editor->Destroy();
200 }
201 else
202 {
203 // Exactly one entity must be selected.
204 wxutil::Messagebox::ShowError(_(NO_ENTITY_ERROR));
205 }
206 }
207
208 int StimResponseEditor::_lastShownPage = -1;
209
210 } // namespace ui