| 1 |
#include "CustomStimEditor.h" |
| 2 |
|
| 3 |
#include "ui/idialogmanager.h" |
| 4 |
|
| 5 |
#include "string/convert.h" |
| 6 |
#include "i18n.h" |
| 7 |
|
| 8 |
#include "wxutil/menu/IconTextMenuItem.h" |
| 9 |
#include <wx/menu.h> |
| 10 |
#include <wx/textctrl.h> |
| 11 |
#include <wx/stattext.h> |
| 12 |
#include <wx/button.h> |
| 13 |
#include <wx/sizer.h> |
| 14 |
#include <wx/panel.h> |
| 15 |
|
| 16 |
namespace ui |
| 17 |
{ |
| 18 |
|
| 19 |
namespace |
| 20 |
{ |
| 21 |
const int TREE_VIEW_WIDTH = 250; |
| 22 |
const int TREE_VIEW_HEIGHT = 200; |
| 23 |
} |
| 24 |
|
| 25 |
CustomStimEditor::CustomStimEditor(wxWindow* parent, StimTypes& stimTypes) : |
| 26 |
_customStimStore(nullptr), |
| 27 |
_list(nullptr), |
| 28 |
_stimTypes(stimTypes), |
| 29 |
_updatesDisabled(false) |
| 30 |
{ |
| 31 |
populatePage(parent); |
| 32 |
|
| 33 |
// Setup the context menu items and connect them to the callbacks |
| 34 |
createContextMenu(); |
| 35 |
|
| 36 |
// The list may be empty, update the sensitivity |
| 37 |
update(); |
| 38 |
} |
| 39 |
|
| 40 |
void CustomStimEditor::setEntity(const SREntityPtr& entity) |
| 41 |
{ |
| 42 |
_entity = entity; |
| 43 |
} |
| 44 |
|
| 45 |
void CustomStimEditor::createContextMenu() |
| 46 |
{ |
| 47 |
// Menu widgets (is not packed, hence create a shared_ptr) |
| 48 |
_contextMenu.menu.reset(new wxMenu); |
| 49 |
|
| 50 |
_contextMenu.add = |
| 51 |
_contextMenu.menu->Append(new wxutil::StockIconTextMenuItem(_("Add"), wxART_PLUS)); |
| 52 |
_contextMenu.remove = |
| 53 |
_contextMenu.menu->Append(new wxutil::StockIconTextMenuItem(_("Delete"), wxART_MINUS)); |
| 54 |
|
| 55 |
// Connect up the signals |
| 56 |
_contextMenu.menu->Connect(_contextMenu.remove->GetId(), wxEVT_MENU, |
| 57 |
wxCommandEventHandler(CustomStimEditor::onContextMenuDelete), NULL, this); |
| 58 |
_contextMenu.menu->Connect(_contextMenu.add->GetId(), wxEVT_MENU, |
| 59 |
wxCommandEventHandler(CustomStimEditor::onContextMenuAdd), NULL, this); |
| 60 |
} |
| 61 |
|
| 62 |
void CustomStimEditor::populatePage(wxWindow* parent) |
| 63 |
{ |
| 64 |
wxBoxSizer* hbox = new wxBoxSizer(wxHORIZONTAL); |
| 65 |
parent->GetSizer()->Add(hbox, 1, wxEXPAND | wxALL, 6); |
| 66 |
|
| 67 |
// Setup a treemodel filter to display the custom stims only |
| 68 |
_customStimStore = new wxutil::TreeModelFilter(_stimTypes.getListStore()); |
| 69 |
|
| 70 |
_customStimStore->SetFilterColumn(_stimTypes.getColumns().isCustom); |
| 71 |
|
| 72 |
_list = wxutil::TreeView::Create(parent); |
| 73 |
_list->AssociateModel(_customStimStore.get()); |
| 74 |
_list->SetMinClientSize(wxSize(TREE_VIEW_WIDTH, TREE_VIEW_HEIGHT)); |
| 75 |
|
| 76 |
// Connect the signals |
| 77 |
_list->Connect(wxEVT_DATAVIEW_SELECTION_CHANGED, |
| 78 |
wxDataViewEventHandler(CustomStimEditor::onSelectionChange), NULL, this); |
| 79 |
_list->Connect(wxEVT_DATAVIEW_ITEM_CONTEXT_MENU, |
| 80 |
wxDataViewEventHandler(CustomStimEditor::onContextMenu), NULL, this); |
| 81 |
|
| 82 |
// Add the columns to the treeview |
| 83 |
// ID number |
| 84 |
_list->AppendTextColumn("ID", _stimTypes.getColumns().id.getColumnIndex(), |
| 85 |
wxDATAVIEW_CELL_INERT, wxCOL_WIDTH_AUTOSIZE, wxALIGN_NOT); |
| 86 |
|
| 87 |
// The Type |
| 88 |
_list->AppendIconTextColumn(_("Type"), _stimTypes.getColumns().caption.getColumnIndex(), |
| 89 |
wxDATAVIEW_CELL_INERT, wxCOL_WIDTH_AUTOSIZE, wxALIGN_NOT); |
| 90 |
|
| 91 |
wxBoxSizer* listVBox = new wxBoxSizer(wxVERTICAL); |
| 92 |
listVBox->Add(_list, 1, wxEXPAND | wxBOTTOM, 6); |
| 93 |
listVBox->Add(createListButtons(parent), 0, wxEXPAND); |
| 94 |
|
| 95 |
_propertyWidgets.vbox = new wxPanel(parent, wxID_ANY); |
| 96 |
_propertyWidgets.vbox->SetSizer(new wxBoxSizer(wxVERTICAL)); |
| 97 |
|
| 98 |
hbox->Add(listVBox, 0, wxEXPAND | wxRIGHT, 12); |
| 99 |
hbox->Add(_propertyWidgets.vbox, 1, wxEXPAND); |
| 100 |
|
| 101 |
// The name widgets |
| 102 |
wxBoxSizer* nameHBox = new wxBoxSizer(wxHORIZONTAL); |
| 103 |
_propertyWidgets.nameLabel = new wxStaticText(_propertyWidgets.vbox, wxID_ANY, _("Name:")); |
| 104 |
_propertyWidgets.nameEntry = new wxTextCtrl(_propertyWidgets.vbox, wxID_ANY); |
| 105 |
|
| 106 |
nameHBox->Add(_propertyWidgets.nameLabel, 0, wxRIGHT | wxALIGN_CENTER_VERTICAL, 6); |
| 107 |
nameHBox->Add(_propertyWidgets.nameEntry, 1, wxEXPAND); |
| 108 |
|
| 109 |
// Connect the entry field |
| 110 |
_propertyWidgets.nameEntry->Connect(wxEVT_TEXT, |
| 111 |
wxCommandEventHandler(CustomStimEditor::onEntryChanged), NULL, this); |
| 112 |
|
| 113 |
wxStaticText* infoText = new wxStaticText(_propertyWidgets.vbox, wxID_ANY, |
| 114 |
_("Note: Please beware that deleting custom stims may\n" |
| 115 |
"affect other entities as well. So check before you delete.") |
| 116 |
); |
| 117 |
|
| 118 |
_propertyWidgets.vbox->GetSizer()->Add(nameHBox, 0, wxEXPAND | wxBOTTOM, 12); |
| 119 |
_propertyWidgets.vbox->GetSizer()->Add(infoText, 0); |
| 120 |
} |
| 121 |
|
| 122 |
wxBoxSizer* CustomStimEditor::createListButtons(wxWindow* parent) |
| 123 |
{ |
| 124 |
wxBoxSizer* hbox = new wxBoxSizer(wxHORIZONTAL); |
| 125 |
|
| 126 |
_listButtons.add = new wxButton(parent, wxID_ANY, _("Add Stim Type")); |
| 127 |
_listButtons.remove = new wxButton(parent, wxID_ANY, _("Remove Stim Type")); |
| 128 |
|
| 129 |
hbox->Add(_listButtons.add, 1, wxRIGHT, 6); |
| 130 |
hbox->Add(_listButtons.remove, 1); |
| 131 |
|
| 132 |
_listButtons.add->Connect(wxEVT_BUTTON, wxCommandEventHandler(CustomStimEditor::onAddStimType), NULL, this); |
| 133 |
_listButtons.remove->Connect(wxEVT_BUTTON, wxCommandEventHandler(CustomStimEditor::onRemoveStimType), NULL, this); |
| 134 |
|
| 135 |
return hbox; |
| 136 |
} |
| 137 |
|
| 138 |
void CustomStimEditor::update() |
| 139 |
{ |
| 140 |
_updatesDisabled = true; |
| 141 |
|
| 142 |
int id = getIdFromSelection(); |
| 143 |
|
| 144 |
if (id > 0) |
| 145 |
{ |
| 146 |
_propertyWidgets.vbox->Enable(true); |
| 147 |
|
| 148 |
StimType stimType = _stimTypes.get(id); |
| 149 |
_propertyWidgets.nameEntry->SetValue(stimType.caption); |
| 150 |
|
| 151 |
_contextMenu.menu->Enable(_contextMenu.remove->GetId(), true); |
| 152 |
} |
| 153 |
else |
| 154 |
{ |
| 155 |
_propertyWidgets.vbox->Enable(false); |
| 156 |
_contextMenu.menu->Enable(_contextMenu.remove->GetId(), false); |
| 157 |
} |
| 158 |
|
| 159 |
_updatesDisabled = false; |
| 160 |
} |
| 161 |
|
| 162 |
void CustomStimEditor::selectId(int id) |
| 163 |
{ |
| 164 |
// Setup the selectionfinder to search for the id |
| 165 |
wxDataViewItem item = _customStimStore->FindInteger(id, _stimTypes.getColumns().id.getColumnIndex()); |
| 166 |
|
| 167 |
if (item) |
| 168 |
{ |
| 169 |
// Set the active row of the list to the given stim |
| 170 |
_list->Select(item); |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
void CustomStimEditor::addStimType() |
| 175 |
{ |
| 176 |
// Add a new stim type with the lowest free custom id |
| 177 |
int id = _stimTypes.getFreeCustomStimId(); |
| 178 |
|
| 179 |
_stimTypes.add(id, |
| 180 |
string::to_string(id), |
| 181 |
"CustomStimType", |
| 182 |
_("Custom Stim"), |
| 183 |
ICON_CUSTOM_STIM, |
| 184 |
true); |
| 185 |
|
| 186 |
selectId(id); |
| 187 |
update(); |
| 188 |
} |
| 189 |
|
| 190 |
int CustomStimEditor::getIdFromSelection() |
| 191 |
{ |
| 192 |
wxDataViewItem item = _list->GetSelection(); |
| 193 |
|
| 194 |
if (item.IsOk()) |
| 195 |
{ |
| 196 |
wxutil::TreeModel::Row row(item, *_customStimStore); |
| 197 |
return row[_stimTypes.getColumns().id].getInteger(); |
| 198 |
} |
| 199 |
|
| 200 |
return -1; |
| 201 |
} |
| 202 |
|
| 203 |
void CustomStimEditor::removeStimType() |
| 204 |
{ |
| 205 |
IDialogPtr dialog = GlobalDialogManager().createMessageBox(_("Delete Custom Stim"), |
| 206 |
_("Beware that other entities might still be using this stim type.\n" |
| 207 |
"Do you really want to delete this custom stim?"), ui::IDialog::MESSAGE_ASK); |
| 208 |
|
| 209 |
if (dialog->run() == IDialog::RESULT_YES) |
| 210 |
{ |
| 211 |
_stimTypes.remove(getIdFromSelection()); |
| 212 |
} |
| 213 |
} |
| 214 |
|
| 215 |
void CustomStimEditor::onAddStimType(wxCommandEvent& ev) |
| 216 |
{ |
| 217 |
addStimType(); |
| 218 |
} |
| 219 |
|
| 220 |
void CustomStimEditor::onRemoveStimType(wxCommandEvent& ev) |
| 221 |
{ |
| 222 |
// Delete the selected stim type from the list |
| 223 |
removeStimType(); |
| 224 |
} |
| 225 |
|
| 226 |
void CustomStimEditor::onEntryChanged(wxCommandEvent& ev) |
| 227 |
{ |
| 228 |
if (_updatesDisabled) return; // Callback loop guard |
| 229 |
|
| 230 |
// Set the caption of the curently selected stim type |
| 231 |
std::string caption = _propertyWidgets.nameEntry->GetValue().ToStdString(); |
| 232 |
|
| 233 |
// Pass the call to the helper class |
| 234 |
_stimTypes.setStimTypeCaption(getIdFromSelection(), caption); |
| 235 |
|
| 236 |
if (_entity != NULL) |
| 237 |
{ |
| 238 |
_entity->updateListStores(); |
| 239 |
} |
| 240 |
} |
| 241 |
|
| 242 |
void CustomStimEditor::onSelectionChange(wxDataViewEvent& ev) |
| 243 |
{ |
| 244 |
update(); |
| 245 |
} |
| 246 |
|
| 247 |
// Delete context menu items activated |
| 248 |
void CustomStimEditor::onContextMenuDelete(wxCommandEvent& ev) |
| 249 |
{ |
| 250 |
// Delete the selected stim from the list |
| 251 |
removeStimType(); |
| 252 |
} |
| 253 |
|
| 254 |
// Delete context menu items activated |
| 255 |
void CustomStimEditor::onContextMenuAdd(wxCommandEvent& ev) |
| 256 |
{ |
| 257 |
addStimType(); |
| 258 |
} |
| 259 |
|
| 260 |
void CustomStimEditor::onContextMenu(wxDataViewEvent& ev) |
| 261 |
{ |
| 262 |
_list->PopupMenu(_contextMenu.menu.get()); |
| 263 |
} |
| 264 |
|
| 265 |
} // namespace ui |