| 1 |
#include "ClassEditor.h" |
| 2 |
|
| 3 |
#include "string/convert.h" |
| 4 |
#include <iostream> |
| 5 |
#include "i18n.h" |
| 6 |
|
| 7 |
#include <wx/bmpcbox.h> |
| 8 |
#include <wx/combobox.h> |
| 9 |
#include <wx/sizer.h> |
| 10 |
#include <wx/spinctrl.h> |
| 11 |
#include <wx/button.h> |
| 12 |
#include <wx/textctrl.h> |
| 13 |
#include <wx/checkbox.h> |
| 14 |
|
| 15 |
namespace ui |
| 16 |
{ |
| 17 |
|
| 18 |
namespace |
| 19 |
{ |
| 20 |
const int TREE_VIEW_WIDTH = 320; |
| 21 |
const int TREE_VIEW_HEIGHT = 160; |
| 22 |
} |
| 23 |
|
| 24 |
ClassEditor::ClassEditor(wxWindow* mainPanel, StimTypes& stimTypes) : |
| 25 |
_list(nullptr), |
| 26 |
_stimTypes(stimTypes), |
| 27 |
_updatesDisabled(false), |
| 28 |
_type(nullptr), |
| 29 |
_addType(nullptr) |
| 30 |
{} |
| 31 |
|
| 32 |
void ClassEditor::createListView(wxWindow* parent) |
| 33 |
{ |
| 34 |
wxutil::TreeModel::Ptr dummyModel( |
| 35 |
new wxutil::TreeModel(SREntity::getColumns(), true) |
| 36 |
); |
| 37 |
|
| 38 |
_list = wxutil::TreeView::CreateWithModel(parent, dummyModel.get()); |
| 39 |
|
| 40 |
_list->SetMinClientSize(wxSize(TREE_VIEW_WIDTH, TREE_VIEW_HEIGHT)); |
| 41 |
parent->GetSizer()->Add(_list, 1, wxEXPAND); |
| 42 |
|
| 43 |
// Connect the signals to the callbacks |
| 44 |
_list->Connect(wxEVT_DATAVIEW_SELECTION_CHANGED, wxDataViewEventHandler(ClassEditor::onSRSelectionChange), NULL, this); |
| 45 |
_list->Connect(wxEVT_KEY_DOWN, wxKeyEventHandler(ClassEditor::onTreeViewKeyPress), NULL, this); |
| 46 |
_list->Connect(wxEVT_DATAVIEW_ITEM_CONTEXT_MENU, wxDataViewEventHandler(ClassEditor::onContextMenu), NULL, this); |
| 47 |
|
| 48 |
// Add the columns to the treeview |
| 49 |
// ID number |
| 50 |
_list->AppendTextColumn("#", SREntity::getColumns().index.getColumnIndex(), |
| 51 |
wxDATAVIEW_CELL_INERT, wxCOL_WIDTH_AUTOSIZE, wxALIGN_NOT); |
| 52 |
|
| 53 |
// The S/R icon |
| 54 |
_list->AppendBitmapColumn(_("S/R"), SREntity::getColumns().srClass.getColumnIndex(), |
| 55 |
wxDATAVIEW_CELL_INERT, wxCOL_WIDTH_AUTOSIZE, wxALIGN_NOT); |
| 56 |
|
| 57 |
// The Type |
| 58 |
_list->AppendIconTextColumn(_("Type"), SREntity::getColumns().caption.getColumnIndex(), |
| 59 |
wxDATAVIEW_CELL_INERT, wxCOL_WIDTH_AUTOSIZE, wxALIGN_NOT); |
| 60 |
} |
| 61 |
|
| 62 |
void ClassEditor::setEntity(const SREntityPtr& entity) |
| 63 |
{ |
| 64 |
_entity = entity; |
| 65 |
} |
| 66 |
|
| 67 |
int ClassEditor::getIndexFromSelection() |
| 68 |
{ |
| 69 |
wxDataViewItem item = _list->GetSelection(); |
| 70 |
|
| 71 |
if (item.IsOk() && _entity != nullptr) |
| 72 |
{ |
| 73 |
wxutil::TreeModel::Row row(item, *_list->GetModel()); |
| 74 |
return row[SREntity::getColumns().index].getInteger(); |
| 75 |
} |
| 76 |
|
| 77 |
return -1; |
| 78 |
} |
| 79 |
|
| 80 |
void ClassEditor::setProperty(const std::string& key, const std::string& value) |
| 81 |
{ |
| 82 |
int index = getIndexFromSelection(); |
| 83 |
|
| 84 |
if (index > 0) |
| 85 |
{ |
| 86 |
// Don't edit inherited stims/responses |
| 87 |
_entity->setProperty(index, key, value); |
| 88 |
} |
| 89 |
|
| 90 |
// Call the method of the child class to update the widgets |
| 91 |
update(); |
| 92 |
} |
| 93 |
|
| 94 |
void ClassEditor::entryChanged(wxTextCtrl* entry) |
| 95 |
{ |
| 96 |
// Try to find the key this entry widget is associated to |
| 97 |
EntryMap::iterator found = _entryWidgets.find(entry); |
| 98 |
|
| 99 |
if (found != _entryWidgets.end()) |
| 100 |
{ |
| 101 |
std::string entryText = entry->GetValue().ToStdString(); |
| 102 |
|
| 103 |
if (!entryText.empty()) |
| 104 |
{ |
| 105 |
setProperty(found->second, entryText); |
| 106 |
} |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
void ClassEditor::spinButtonChanged(wxSpinCtrl* ctrl) |
| 111 |
{ |
| 112 |
// Try to find the key this spinbutton widget is associated to |
| 113 |
SpinCtrlMap::iterator found = _spinWidgets.find(ctrl); |
| 114 |
|
| 115 |
if (found != _spinWidgets.end()) |
| 116 |
{ |
| 117 |
std::string valueText = string::to_string(ctrl->GetValue()); |
| 118 |
|
| 119 |
if (!valueText.empty()) |
| 120 |
{ |
| 121 |
setProperty(found->second, valueText); |
| 122 |
} |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
void ClassEditor::spinButtonChanged(wxSpinCtrlDouble* ctrl) |
| 127 |
{ |
| 128 |
// Try to find the key this spinbutton widget is associated to |
| 129 |
SpinCtrlMap::iterator found = _spinWidgets.find(ctrl); |
| 130 |
|
| 131 |
if (found != _spinWidgets.end()) |
| 132 |
{ |
| 133 |
std::string valueText = string::to_string(ctrl->GetValue()); |
| 134 |
|
| 135 |
if (!valueText.empty()) |
| 136 |
{ |
| 137 |
setProperty(found->second, valueText); |
| 138 |
} |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
void ClassEditor::reloadStimTypes() |
| 143 |
{ |
| 144 |
if (_type != NULL) |
| 145 |
{ |
| 146 |
_stimTypes.populateComboBox(_type); |
| 147 |
} |
| 148 |
|
| 149 |
if (_addType != NULL) |
| 150 |
{ |
| 151 |
_stimTypes.populateComboBox(_addType); |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
void ClassEditor::removeSR() |
| 156 |
{ |
| 157 |
// Get the selected stim index |
| 158 |
int index = getIndexFromSelection(); |
| 159 |
|
| 160 |
if (index > 0) |
| 161 |
{ |
| 162 |
_entity->remove(index); |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
void ClassEditor::selectIndex(int index) |
| 167 |
{ |
| 168 |
// Setup the selectionfinder to search for the id |
| 169 |
wxutil::TreeModel* model = dynamic_cast<wxutil::TreeModel*>(_list->GetModel()); |
| 170 |
assert(model != NULL); |
| 171 |
|
| 172 |
wxDataViewItem item = model->FindInteger(index, SREntity::getColumns().index); |
| 173 |
|
| 174 |
if (item.IsOk()) |
| 175 |
{ |
| 176 |
// Set the active row of the list to the given effect |
| 177 |
_list->Select(item); |
| 178 |
// Manually trigger the selection change signal |
| 179 |
selectionChanged(); |
| 180 |
} |
| 181 |
} |
| 182 |
|
| 183 |
void ClassEditor::duplicateStimResponse() |
| 184 |
{ |
| 185 |
int index = getIndexFromSelection(); |
| 186 |
|
| 187 |
if (index > 0) |
| 188 |
{ |
| 189 |
int newIndex = _entity->duplicate(index); |
| 190 |
// Select the newly created stim |
| 191 |
selectIndex(newIndex); |
| 192 |
} |
| 193 |
|
| 194 |
// Call the method of the child class to update the widgets |
| 195 |
update(); |
| 196 |
} |
| 197 |
|
| 198 |
void ClassEditor::onSRSelectionChange(wxDataViewEvent& ev) |
| 199 |
{ |
| 200 |
selectionChanged(); |
| 201 |
} |
| 202 |
|
| 203 |
void ClassEditor::onTreeViewKeyPress(wxKeyEvent& ev) |
| 204 |
{ |
| 205 |
if (ev.GetKeyCode() == WXK_DELETE) |
| 206 |
{ |
| 207 |
removeSR(); |
| 208 |
return; |
| 209 |
} |
| 210 |
|
| 211 |
// Propagate further |
| 212 |
ev.Skip(); |
| 213 |
} |
| 214 |
|
| 215 |
void ClassEditor::onSpinCtrlChanged(wxSpinEvent& ev) |
| 216 |
{ |
| 217 |
if (_updatesDisabled) return; // Callback loop guard |
| 218 |
|
| 219 |
spinButtonChanged(dynamic_cast<wxSpinCtrl*>(ev.GetEventObject())); |
| 220 |
} |
| 221 |
|
| 222 |
void ClassEditor::onSpinCtrlDoubleChanged(wxSpinDoubleEvent& ev) |
| 223 |
{ |
| 224 |
if (_updatesDisabled) return; // Callback loop guard |
| 225 |
|
| 226 |
spinButtonChanged(dynamic_cast<wxSpinCtrlDouble*>(ev.GetEventObject())); |
| 227 |
} |
| 228 |
|
| 229 |
void ClassEditor::connectSpinButton(wxSpinCtrl* spinCtrl, const std::string& key) |
| 230 |
{ |
| 231 |
// Associate the spin button with a specific entity key, if not empty |
| 232 |
if (!key.empty()) |
| 233 |
{ |
| 234 |
_spinWidgets[spinCtrl] = key; |
| 235 |
} |
| 236 |
|
| 237 |
// Connect the callback and bind the spinbutton pointer as first argument |
| 238 |
spinCtrl->Connect(wxEVT_SPINCTRL, |
| 239 |
wxSpinEventHandler(ClassEditor::onSpinCtrlChanged), NULL, this); |
| 240 |
} |
| 241 |
|
| 242 |
void ClassEditor::connectSpinButton(wxSpinCtrlDouble* spinCtrl, const std::string& key) |
| 243 |
{ |
| 244 |
// Associate the spin button with a specific entity key, if not empty |
| 245 |
if (!key.empty()) |
| 246 |
{ |
| 247 |
_spinWidgets[spinCtrl] = key; |
| 248 |
} |
| 249 |
|
| 250 |
// Connect the callback and bind the spinbutton pointer as first argument |
| 251 |
spinCtrl->Connect(wxEVT_SPINCTRLDOUBLE, |
| 252 |
wxSpinDoubleEventHandler(ClassEditor::onSpinCtrlDoubleChanged), NULL, this); |
| 253 |
} |
| 254 |
|
| 255 |
void ClassEditor::onEntryChanged(wxCommandEvent& ev) |
| 256 |
{ |
| 257 |
if (_updatesDisabled) return; // Callback loop guard |
| 258 |
|
| 259 |
entryChanged(dynamic_cast<wxTextCtrl*>(ev.GetEventObject())); |
| 260 |
} |
| 261 |
|
| 262 |
void ClassEditor::connectEntry(wxTextCtrl* entry, const std::string& key) |
| 263 |
{ |
| 264 |
// Associate the entry with a specific entity key |
| 265 |
_entryWidgets[entry] = key; |
| 266 |
|
| 267 |
// Connect the callback |
| 268 |
entry->Connect(wxEVT_TEXT, |
| 269 |
wxCommandEventHandler(ClassEditor::onEntryChanged), NULL, this); |
| 270 |
} |
| 271 |
|
| 272 |
void ClassEditor::onCheckboxToggle(wxCommandEvent& ev) |
| 273 |
{ |
| 274 |
if (_updatesDisabled) return; // Callback loop guard |
| 275 |
|
| 276 |
checkBoxToggled(dynamic_cast<wxCheckBox*>(ev.GetEventObject())); |
| 277 |
} |
| 278 |
|
| 279 |
void ClassEditor::connectCheckButton(wxCheckBox* checkButton) |
| 280 |
{ |
| 281 |
// Bind the checkbutton pointer to the callback, it is needed in onCheckboxToggle |
| 282 |
checkButton->Connect(wxEVT_CHECKBOX, |
| 283 |
wxCommandEventHandler(ClassEditor::onCheckboxToggle), NULL, this); |
| 284 |
} |
| 285 |
|
| 286 |
std::string ClassEditor::getStimTypeIdFromSelector(wxComboBox* comboBox) |
| 287 |
{ |
| 288 |
if (comboBox->GetSelection() == -1) return ""; |
| 289 |
|
| 290 |
wxStringClientData* stringData = static_cast<wxStringClientData*>( |
| 291 |
comboBox->GetClientObject(comboBox->GetSelection())); |
| 292 |
|
| 293 |
if (stringData == NULL) return ""; |
| 294 |
|
| 295 |
return stringData->GetData().ToStdString(); |
| 296 |
} |
| 297 |
|
| 298 |
void ClassEditor::onContextMenu(wxDataViewEvent& ev) |
| 299 |
{ |
| 300 |
// Call the subclass implementation |
| 301 |
openSRListContextMenu(); |
| 302 |
} |
| 303 |
|
| 304 |
void ClassEditor::onStimTypeSelect(wxCommandEvent& ev) |
| 305 |
{ |
| 306 |
if (_updatesDisabled || _type == nullptr) return; // Callback loop guard |
| 307 |
|
| 308 |
std::string name = getStimTypeIdFromSelector(_type); |
| 309 |
|
| 310 |
if (!name.empty()) |
| 311 |
{ |
| 312 |
// Write it to the entity |
| 313 |
setProperty("type", name); |
| 314 |
} |
| 315 |
} |
| 316 |
|
| 317 |
void ClassEditor::onAddTypeSelect(wxCommandEvent& ev) |
| 318 |
{ |
| 319 |
if (_updatesDisabled || _addType == nullptr) return; // Callback loop guard |
| 320 |
|
| 321 |
wxComboBox* combo = dynamic_cast<wxComboBox*>(ev.GetEventObject()); |
| 322 |
assert(combo != nullptr); |
| 323 |
|
| 324 |
std::string name = getStimTypeIdFromSelector(combo); |
| 325 |
|
| 326 |
if (!name.empty()) |
| 327 |
{ |
| 328 |
addSR(); |
| 329 |
} |
| 330 |
} |
| 331 |
|
| 332 |
// "Disable" context menu item |
| 333 |
void ClassEditor::onContextMenuDisable(wxCommandEvent& ev) |
| 334 |
{ |
| 335 |
setProperty("state", "0"); |
| 336 |
} |
| 337 |
|
| 338 |
// "Enable" context menu item |
| 339 |
void ClassEditor::onContextMenuEnable(wxCommandEvent& ev) |
| 340 |
{ |
| 341 |
setProperty("state", "1"); |
| 342 |
} |
| 343 |
|
| 344 |
void ClassEditor::onContextMenuDuplicate(wxCommandEvent& ev) |
| 345 |
{ |
| 346 |
duplicateStimResponse(); |
| 347 |
} |
| 348 |
|
| 349 |
void ClassEditor::onAddSR(wxCommandEvent& ev) |
| 350 |
{ |
| 351 |
// Add a S/R |
| 352 |
addSR(); |
| 353 |
} |
| 354 |
|
| 355 |
void ClassEditor::onRemoveSR(wxCommandEvent& ev) |
| 356 |
{ |
| 357 |
// Delete the selected S/R from the list |
| 358 |
removeSR(); |
| 359 |
} |
| 360 |
|
| 361 |
} // namespace ui |