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 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596
|
#include "ResponseEditor.h"
#include "wxutil/dataview/TreeModel.h"
#include "wxutil/menu/IconTextMenuItem.h"
#include "string/convert.h"
#include "i18n.h"
#include "EffectEditor.h"
#include <wx/combobox.h>
#include <wx/bmpcbox.h>
#include <wx/button.h>
#include <wx/menu.h>
#include <wx/spinctrl.h>
#include <wx/textctrl.h>
#include <wx/checkbox.h>
#include "wxutil/ChoiceHelper.h"
namespace ui
{
ResponseEditor::ResponseEditor(wxWindow* mainPanel, StimTypes& stimTypes) :
ClassEditor(mainPanel, stimTypes),
_mainPanel(mainPanel)
{
setupPage();
createContextMenu();
update();
}
void ResponseEditor::setEntity(const SREntityPtr& entity)
{
// Pass the call to the base class
ClassEditor::setEntity(entity);
if (entity)
{
wxutil::TreeModel::Ptr responseStore = _entity->getResponseStore();
_list->AssociateModel(responseStore.get());
// Trigger column width reevaluation
responseStore->ItemChanged(responseStore->GetRoot());
// Clear the treeview
wxutil::TreeModel* effectsModel =
static_cast<wxutil::TreeModel*>(_effectWidgets.view->GetModel());
effectsModel->Clear();
}
else
{
// wxWidgets 3.0.0 crashes when associating a NULL model, so use a dummy model
// to release the old one
wxutil::TreeModel* dummyStore = new wxutil::TreeModel(SREntity::getColumns(), true);
_list->AssociateModel(dummyStore);
}
}
void ResponseEditor::update()
{
_updatesDisabled = true;
wxPanel* mainPanel = findNamedObject<wxPanel>(_mainPanel, "SREditorResponsePanel");
auto removeButton = findNamedObject<wxButton>(_mainPanel, "RemoveResponseButton");
int index = getIndexFromSelection();
if (index > 0 && _entity != NULL)
{
mainPanel->Enable(true);
StimResponse& sr = _entity->get(index);
// Get the iter into the liststore pointing at the correct STIM_YYYY type
std::string typeToFind = sr.get("type");
wxutil::ChoiceHelper:: SelectComboItemByStoredString (_type, typeToFind);
// Active
_propertyWidgets.active->SetValue(sr.get("state") == "1");
// Use Radius
bool useRandomEffects = (sr.get("random_effects") != "");
_propertyWidgets.randomEffectsToggle->SetValue(useRandomEffects);
_propertyWidgets.randomEffectsEntry->SetValue(sr.get("random_effects"));
_propertyWidgets.randomEffectsEntry->Enable(useRandomEffects);
// Use Chance
bool useChance = (sr.get("chance") != "");
_propertyWidgets.chanceToggle->SetValue(useChance);
_propertyWidgets.chanceEntry->SetValue(string::convert<double>(sr.get("chance")));
_propertyWidgets.chanceEntry->Enable(useChance);
wxutil::TreeModel::Ptr effectsModel = sr.createEffectsStore();
// It's important to unselect everything before swapping the model
// otherwise wxDataViewCtrl will keep invalid items in its internal selection list
_effectWidgets.view->UnselectAll();
_effectWidgets.view->AssociateModel(effectsModel.get());
effectsModel->ItemChanged(effectsModel->GetRoot()); // trigger column width re-evaluation
// Disable the editing of inherited properties completely
if (sr.inherited())
{
mainPanel->Enable(false);
}
// Update the delete context menu item
_contextMenu.remove->Enable(!sr.inherited());
removeButton->Enable(!sr.inherited());
// If there is anything selected, the duplicate item is always active
_contextMenu.duplicate->Enable(true);
// Update the "enable/disable" menu items
bool state = sr.get("state") == "1";
_contextMenu.enable->Enable(!state);
_contextMenu.disable->Enable(state);
// The response effect list may be empty, so force an update of the
// context menu sensitivity, in the case the "selection changed"
// signal doesn't get called
updateEffectContextMenu();
}
else
{
// Nothing selected
mainPanel->Enable(false);
// Clear the effect tree view
wxutil::TreeModel* effectsModel =
static_cast<wxutil::TreeModel*>(_effectWidgets.view->GetModel());
effectsModel->Clear();
_contextMenu.enable->Enable(false);
_contextMenu.disable->Enable(false);
_contextMenu.remove->Enable(false);
_contextMenu.duplicate->Enable(false);
removeButton->Enable(false);
}
_updatesDisabled = false;
}
void ResponseEditor::setupPage()
{
wxPanel* listPanel = findNamedObject<wxPanel>(_mainPanel, "SREditorResponseList");
createListView(listPanel);
#ifdef USE_BMP_COMBO_BOX
// Response property section
_type = findNamedObject<wxBitmapComboBox>(_mainPanel, "ResponseEditorTypeCombo");
#else
{
// Response property section
wxControl* typeBox = findNamedObject<wxControl>(_mainPanel, "ResponseEditorTypeCombo");
// Replace the bitmap combo with an ordinary one
wxComboBox* combo = new wxComboBox(typeBox->GetParent(), wxID_ANY);
typeBox->GetContainingSizer()->Add(combo, 1, wxEXPAND);
typeBox->Destroy();
_type = combo;
_type->SetName("ResponseEditorTypeCombo");
}
#endif
_type->Connect(wxEVT_COMBOBOX, wxCommandEventHandler(ResponseEditor::onStimTypeSelect), NULL, this);
// Active
_propertyWidgets.active = findNamedObject<wxCheckBox>(_mainPanel, "ResponseEditorActive");
// Random Effects Toggle
_propertyWidgets.randomEffectsToggle = findNamedObject<wxCheckBox>(_mainPanel, "ResponseEditorRandomFX");
_propertyWidgets.randomEffectsEntry = findNamedObject<wxTextCtrl>(_mainPanel, "ResponseEditorRandomFXValue");
// Chance variable
_propertyWidgets.chanceToggle = findNamedObject<wxCheckBox>(_mainPanel, "ResponseEditorChance");
wxPanel* chancePanel = findNamedObject<wxPanel>(_mainPanel, "ResponseEditorChanceValuePanel");
_propertyWidgets.chanceEntry = new wxSpinCtrlDouble(chancePanel, wxID_ANY);
_propertyWidgets.chanceEntry->SetRange(0.0, 1.0);
_propertyWidgets.chanceEntry->SetIncrement(0.01);
_propertyWidgets.chanceEntry->SetValue(0);
chancePanel->GetSizer()->Add(_propertyWidgets.chanceEntry, 1);
// Connect the signals
connectCheckButton(_propertyWidgets.active);
connectCheckButton(_propertyWidgets.randomEffectsToggle);
connectCheckButton(_propertyWidgets.chanceToggle);
connectEntry(_propertyWidgets.randomEffectsEntry, "random_effects");
connectSpinButton(_propertyWidgets.chanceEntry, "chance");
makeLabelBold(_mainPanel, "ResponseEditorFXLabel");
createEffectWidgets();
#ifdef USE_BMP_COMBO_BOX
_addType = findNamedObject<wxBitmapComboBox>(_mainPanel, "ResponseTypeComboBox");
#else
{
// Type selector box
wxControl* addTypeBox = findNamedObject<wxControl>(_mainPanel, "ResponseTypeComboBox");
// Replace the bitmap combo with an ordinary one
wxComboBox* newTypeCombo = new wxComboBox(addTypeBox->GetParent(), wxID_ANY);
addTypeBox->GetContainingSizer()->Prepend(newTypeCombo, 1, wxEXPAND | wxRIGHT, 6);
addTypeBox->Destroy();
_addType = newTypeCombo;
_addType->SetName("ResponseTypeComboBox");
}
#endif
_addType->Bind(wxEVT_COMBOBOX, std::bind(&ResponseEditor::onAddTypeSelect, this, std::placeholders::_1));
auto addButton = findNamedObject<wxButton>(_mainPanel, "AddResponseButton");
auto removeButton = findNamedObject<wxButton>(_mainPanel, "RemoveResponseButton");
addButton->Bind(wxEVT_BUTTON, std::bind(&ResponseEditor::onAddSR, this, std::placeholders::_1));
removeButton->Bind(wxEVT_BUTTON, std::bind(&ResponseEditor::onRemoveSR, this, std::placeholders::_1));
reloadStimTypes();
}
void ResponseEditor::reloadStimTypes()
{
if (_stimTypes.getStimMap().empty())
{
_stimTypes.reload();
}
_stimTypes.populateComboBox(_addType);
_stimTypes.populateComboBox(_type);
}
void ResponseEditor::createEffectWidgets()
{
wxPanel* effectsPanel = findNamedObject<wxPanel>(_mainPanel, "ResponseEditorFXPanel");
wxutil::TreeModel::Ptr dummyModel(
new wxutil::TreeModel(StimResponse::getColumns(), true)
);
_effectWidgets.view = wxutil::TreeView::CreateWithModel(effectsPanel, dummyModel.get());
_effectWidgets.view->SetMinClientSize(wxSize(-1, 150));
effectsPanel->GetSizer()->Add(_effectWidgets.view, 1, wxEXPAND);
// Connect the signals
_effectWidgets.view->Connect(wxEVT_DATAVIEW_SELECTION_CHANGED,
wxDataViewEventHandler(ResponseEditor::onEffectSelectionChange), nullptr, this);
_effectWidgets.view->Connect(wxEVT_DATAVIEW_ITEM_ACTIVATED,
wxDataViewEventHandler(ResponseEditor::onEffectItemActivated), nullptr, this);
_effectWidgets.view->Connect(wxEVT_DATAVIEW_ITEM_CONTEXT_MENU,
wxDataViewEventHandler(ResponseEditor::onEffectItemContextMenu), nullptr, this);
// View Columns
_effectWidgets.view->AppendTextColumn("#", StimResponse::getColumns().index.getColumnIndex(),
wxDATAVIEW_CELL_INERT, wxCOL_WIDTH_AUTOSIZE, wxALIGN_NOT);
_effectWidgets.view->AppendTextColumn(_("Effect"), StimResponse::getColumns().caption.getColumnIndex(),
wxDATAVIEW_CELL_INERT, wxCOL_WIDTH_AUTOSIZE, wxALIGN_NOT);
_effectWidgets.view->AppendTextColumn(_("Details (double-click to edit)"), StimResponse::getColumns().arguments.getColumnIndex(),
wxDATAVIEW_CELL_INERT, wxCOL_WIDTH_AUTOSIZE, wxALIGN_NOT);
}
void ResponseEditor::checkBoxToggled(wxCheckBox* toggleButton)
{
bool active = toggleButton->GetValue();
if (toggleButton == _propertyWidgets.active)
{
setProperty("state", active ? "1" : "0");
}
else if (toggleButton == _propertyWidgets.randomEffectsToggle)
{
std::string entryText = _propertyWidgets.randomEffectsEntry->GetValue().ToStdString();
// Enter a default value for the entry text, if it's empty up till now.
if (active)
{
entryText += (entryText.empty()) ? "1" : "";
}
else {
entryText = "";
}
setProperty("random_effects", entryText);
}
else if (toggleButton == _propertyWidgets.chanceToggle)
{
std::string entryText = string::to_string(_propertyWidgets.chanceEntry->GetValue());
setProperty("chance", active ? entryText : "");
}
}
void ResponseEditor::addEffect()
{
if (_entity == nullptr) return;
int index = getIndexFromSelection();
if (index > 0)
{
StimResponse& sr = _entity->get(index);
int effectIndex = getEffectIdFromSelection();
// Make sure we have a response
if (sr.get("class") == "R") {
// Add a new effect and update all the widgets
sr.addEffect(effectIndex);
update();
}
}
}
void ResponseEditor::removeEffect()
{
if (_entity == nullptr) return;
int index = getIndexFromSelection();
if (index > 0)
{
StimResponse& sr = _entity->get(index);
int effectIndex = getEffectIdFromSelection();
// Make sure we have a response and anything selected
if (sr.get("class") == "R" && effectIndex > 0)
{
// Remove the effect and update all the widgets
sr.deleteEffect(effectIndex);
update();
}
}
}
void ResponseEditor::editEffect()
{
if (_entity == nullptr) return;
int index = getIndexFromSelection();
if (index > 0)
{
StimResponse& sr = _entity->get(index);
int effectIndex = getEffectIdFromSelection();
// Make sure we have a response and anything selected
if (sr.get("class") == "R" && effectIndex > 0)
{
// Create a new effect editor (self-destructs)
EffectEditor* editor = new EffectEditor(_mainPanel, sr, effectIndex, _stimTypes, *this);
editor->ShowModal();
editor->Destroy();
}
}
}
void ResponseEditor::moveEffect(int direction)
{
if (_entity == NULL) return;
int index = getIndexFromSelection();
if (index > 0)
{
StimResponse& sr = _entity->get(index);
int effectIndex = getEffectIdFromSelection();
if (sr.get("class") == "R" && effectIndex > 0)
{
// Move the index (swap the specified indices)
sr.moveEffect(effectIndex, effectIndex + direction);
update();
// Select the moved effect after the update
selectEffectIndex(effectIndex + direction);
}
}
}
void ResponseEditor::updateEffectContextMenu()
{
// Check if we have anything selected at all
int curEffectIndex = getEffectIdFromSelection();
int highestEffectIndex = 0;
bool anythingSelected = curEffectIndex >= 0;
int index = getIndexFromSelection();
if (index > 0)
{
StimResponse& sr = _entity->get(index);
highestEffectIndex = sr.highestEffectIndex();
}
bool upActive = anythingSelected && curEffectIndex > 1;
bool downActive = anythingSelected && curEffectIndex < highestEffectIndex;
// Enable or disable the "Delete" context menu items based on the presence
// of a selection.
_effectWidgets.contextMenu->Enable(_effectWidgets.deleteMenuItem->GetId(), anythingSelected);
_effectWidgets.contextMenu->Enable(_effectWidgets.editMenuItem->GetId(), anythingSelected);
_effectWidgets.contextMenu->Enable(_effectWidgets.upMenuItem->GetId(), upActive);
_effectWidgets.contextMenu->Enable(_effectWidgets.downMenuItem->GetId(), downActive);
}
// Create the context menus
void ResponseEditor::createContextMenu()
{
// Menu widgets
_contextMenu.menu.reset(new wxMenu);
// Each menu gets a delete item
_contextMenu.enable = _contextMenu.menu->Append(
new wxutil::IconTextMenuItem(_("Activate"), "sr_response.png"));
_contextMenu.disable = _contextMenu.menu->Append(
new wxutil::IconTextMenuItem(_("Deactivate"), "sr_response_inactive.png"));
_contextMenu.duplicate = _contextMenu.menu->Append(
new wxutil::StockIconTextMenuItem(_("Duplicate"), wxART_COPY));
_contextMenu.remove = _contextMenu.menu->Append(
new wxutil::StockIconTextMenuItem(_("Delete"), wxART_DELETE));
_effectWidgets.contextMenu.reset(new wxMenu);
_effectWidgets.addMenuItem = _effectWidgets.contextMenu->Append(
new wxutil::StockIconTextMenuItem(_("Add new Effect"), wxART_PLUS));
_effectWidgets.editMenuItem = _effectWidgets.contextMenu->Append(
new wxutil::IconTextMenuItem(_("Edit"), "edit.png"));
_effectWidgets.upMenuItem = _effectWidgets.contextMenu->Append(
new wxutil::StockIconTextMenuItem(_("Move Up"), wxART_GO_UP));
_effectWidgets.downMenuItem = _effectWidgets.contextMenu->Append(
new wxutil::StockIconTextMenuItem(_("Move Down"), wxART_GO_DOWN));
_effectWidgets.deleteMenuItem = _effectWidgets.contextMenu->Append(
new wxutil::StockIconTextMenuItem(_("Delete"), wxART_DELETE));
// Connect up the signals
_contextMenu.menu->Connect(_contextMenu.remove->GetId(), wxEVT_MENU,
wxCommandEventHandler(ResponseEditor::onContextMenuDelete), NULL, this);
_contextMenu.menu->Connect(_contextMenu.enable->GetId(), wxEVT_MENU,
wxCommandEventHandler(ResponseEditor::onContextMenuEnable), NULL, this);
_contextMenu.menu->Connect(_contextMenu.disable->GetId(), wxEVT_MENU,
wxCommandEventHandler(ResponseEditor::onContextMenuDisable), NULL, this);
_contextMenu.menu->Connect(_contextMenu.duplicate->GetId(), wxEVT_MENU,
wxCommandEventHandler(ResponseEditor::onContextMenuDuplicate), NULL, this);
_effectWidgets.contextMenu->Connect(_effectWidgets.deleteMenuItem->GetId(), wxEVT_MENU,
wxCommandEventHandler(ResponseEditor::onEffectMenuDelete), NULL, this);
_effectWidgets.contextMenu->Connect(_effectWidgets.editMenuItem->GetId(), wxEVT_MENU,
wxCommandEventHandler(ResponseEditor::onEffectMenuEdit), NULL, this);
_effectWidgets.contextMenu->Connect(_effectWidgets.addMenuItem->GetId(), wxEVT_MENU,
wxCommandEventHandler(ResponseEditor::onEffectMenuAdd), NULL, this);
_effectWidgets.contextMenu->Connect(_effectWidgets.upMenuItem->GetId(), wxEVT_MENU,
wxCommandEventHandler(ResponseEditor::onEffectMenuEffectUp), NULL, this);
_effectWidgets.contextMenu->Connect(_effectWidgets.downMenuItem->GetId(), wxEVT_MENU,
wxCommandEventHandler(ResponseEditor::onEffectMenuEffectDown), NULL, this);
}
void ResponseEditor::selectEffectIndex(const unsigned int index)
{
wxutil::TreeModel* model = static_cast<wxutil::TreeModel*>(_effectWidgets.view->GetModel());
wxDataViewItem item = model->FindInteger(index, StimResponse::getColumns().index);
if (item.IsOk())
{
// Set the active row of the list to the given effect
_effectWidgets.view->Select(item);
}
}
int ResponseEditor::getEffectIdFromSelection()
{
wxDataViewItem item = _effectWidgets.view->GetSelection();
if (item.IsOk() && _entity != NULL)
{
wxutil::TreeModel::Row row(item, *_effectWidgets.view->GetModel());
return row[StimResponse::getColumns().index].getInteger();
}
else
{
return -1;
}
}
void ResponseEditor::openSRListContextMenu()
{
_list->PopupMenu(_contextMenu.menu.get());
}
void ResponseEditor::selectionChanged()
{
update();
}
void ResponseEditor::addSR()
{
if (_entity == NULL) return;
// Create a new StimResponse object
int index = _entity->add();
// Get a reference to the newly allocated object
StimResponse& sr = _entity->get(index);
sr.set("class", "R");
// Get the selected stim type name from the combo box
std::string name = getStimTypeIdFromSelector(_addType);
sr.set("type", (!name.empty()) ? name : _stimTypes.getFirstName());
sr.set("state", "1");
// Update the list stores AFTER the type has been set
_entity->updateListStores();
// Select the newly created response
selectIndex(index);
}
void ResponseEditor::onEffectItemContextMenu(wxDataViewEvent& ev)
{
updateEffectContextMenu();
_effectWidgets.view->PopupMenu(_effectWidgets.contextMenu.get());
}
// Button click events on TreeViews
void ResponseEditor::onEffectItemActivated(wxDataViewEvent& ev)
{
// Call the effect editor upon double click
editEffect();
}
void ResponseEditor::onEffectMenuDelete(wxCommandEvent& ev)
{
removeEffect();
}
void ResponseEditor::onEffectMenuEdit(wxCommandEvent& ev)
{
editEffect();
}
void ResponseEditor::onEffectMenuAdd(wxCommandEvent& ev)
{
addEffect();
}
void ResponseEditor::onEffectMenuEffectUp(wxCommandEvent& ev)
{
moveEffect(-1);
}
void ResponseEditor::onEffectMenuEffectDown(wxCommandEvent& ev)
{
moveEffect(+1);
}
// Delete context menu items activated
void ResponseEditor::onContextMenuDelete(wxCommandEvent& ev)
{
// Delete the selected stim from the list
removeSR();
}
// Delete context menu items activated
void ResponseEditor::onContextMenuAdd(wxCommandEvent& ev)
{
addSR();
}
// Callback for effects treeview selection changes
void ResponseEditor::onEffectSelectionChange(wxDataViewEvent& ev)
{
if (_updatesDisabled) return; // Callback loop guard
// Update the sensitivity
updateEffectContextMenu();
}
} // namespace ui
|