File: CustomStimEditor.cpp

package info (click to toggle)
darkradiant 3.9.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 41,080 kB
  • sloc: cpp: 264,743; ansic: 10,659; python: 1,852; xml: 1,650; sh: 92; makefile: 21
file content (265 lines) | stat: -rw-r--r-- 7,338 bytes parent folder | download | duplicates (3)
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
#include "CustomStimEditor.h"

#include "ui/idialogmanager.h"

#include "string/convert.h"
#include "i18n.h"

#include "wxutil/menu/IconTextMenuItem.h"
#include <wx/menu.h>
#include <wx/textctrl.h>
#include <wx/stattext.h>
#include <wx/button.h>
#include <wx/sizer.h>
#include <wx/panel.h>

namespace ui
{

namespace
{
	const int TREE_VIEW_WIDTH = 250;
	const int TREE_VIEW_HEIGHT = 200;
}

CustomStimEditor::CustomStimEditor(wxWindow* parent, StimTypes& stimTypes) :
	_customStimStore(nullptr),
	_list(nullptr),
	_stimTypes(stimTypes),
	_updatesDisabled(false)
{
	populatePage(parent);

	// Setup the context menu items and connect them to the callbacks
	createContextMenu();

	// The list may be empty, update the sensitivity
	update();
}

void CustomStimEditor::setEntity(const SREntityPtr& entity)
{
	_entity = entity;
}

void CustomStimEditor::createContextMenu()
{
	// Menu widgets (is not packed, hence create a shared_ptr)
	_contextMenu.menu.reset(new wxMenu);

	_contextMenu.add = 
		_contextMenu.menu->Append(new wxutil::StockIconTextMenuItem(_("Add"), wxART_PLUS));
	_contextMenu.remove = 
		_contextMenu.menu->Append(new wxutil::StockIconTextMenuItem(_("Delete"), wxART_MINUS));

	// Connect up the signals
	_contextMenu.menu->Connect(_contextMenu.remove->GetId(), wxEVT_MENU, 
		wxCommandEventHandler(CustomStimEditor::onContextMenuDelete), NULL, this);
	_contextMenu.menu->Connect(_contextMenu.add->GetId(), wxEVT_MENU, 
		wxCommandEventHandler(CustomStimEditor::onContextMenuAdd), NULL, this);
}

void CustomStimEditor::populatePage(wxWindow* parent)
{
	wxBoxSizer* hbox = new wxBoxSizer(wxHORIZONTAL);
	parent->GetSizer()->Add(hbox, 1, wxEXPAND | wxALL, 6);

	// Setup a treemodel filter to display the custom stims only
	_customStimStore = new wxutil::TreeModelFilter(_stimTypes.getListStore());

	_customStimStore->SetFilterColumn(_stimTypes.getColumns().isCustom);

	_list = wxutil::TreeView::Create(parent);
	_list->AssociateModel(_customStimStore.get());
	_list->SetMinClientSize(wxSize(TREE_VIEW_WIDTH, TREE_VIEW_HEIGHT));

	// Connect the signals
	_list->Connect(wxEVT_DATAVIEW_SELECTION_CHANGED,
        wxDataViewEventHandler(CustomStimEditor::onSelectionChange), NULL, this);
	_list->Connect(wxEVT_DATAVIEW_ITEM_CONTEXT_MENU, 
		wxDataViewEventHandler(CustomStimEditor::onContextMenu), NULL, this);

	// Add the columns to the treeview
	// ID number
	_list->AppendTextColumn("ID", _stimTypes.getColumns().id.getColumnIndex(), 
		wxDATAVIEW_CELL_INERT, wxCOL_WIDTH_AUTOSIZE, wxALIGN_NOT);

	// The Type
	_list->AppendIconTextColumn(_("Type"), _stimTypes.getColumns().caption.getColumnIndex(), 
		wxDATAVIEW_CELL_INERT, wxCOL_WIDTH_AUTOSIZE, wxALIGN_NOT);

	wxBoxSizer* listVBox = new wxBoxSizer(wxVERTICAL);
	listVBox->Add(_list, 1, wxEXPAND | wxBOTTOM, 6);
	listVBox->Add(createListButtons(parent), 0, wxEXPAND);

	_propertyWidgets.vbox = new wxPanel(parent, wxID_ANY);
	_propertyWidgets.vbox->SetSizer(new wxBoxSizer(wxVERTICAL));

	hbox->Add(listVBox, 0, wxEXPAND | wxRIGHT, 12);
	hbox->Add(_propertyWidgets.vbox, 1, wxEXPAND);

	// The name widgets
	wxBoxSizer* nameHBox = new wxBoxSizer(wxHORIZONTAL);
	_propertyWidgets.nameLabel = new wxStaticText(_propertyWidgets.vbox, wxID_ANY, _("Name:"));
	_propertyWidgets.nameEntry = new wxTextCtrl(_propertyWidgets.vbox, wxID_ANY);

	nameHBox->Add(_propertyWidgets.nameLabel, 0, wxRIGHT | wxALIGN_CENTER_VERTICAL, 6);
	nameHBox->Add(_propertyWidgets.nameEntry, 1, wxEXPAND);

	// Connect the entry field
	_propertyWidgets.nameEntry->Connect(wxEVT_TEXT,
		wxCommandEventHandler(CustomStimEditor::onEntryChanged), NULL, this);

	wxStaticText* infoText = new wxStaticText(_propertyWidgets.vbox, wxID_ANY,
		_("Note: Please beware that deleting custom stims may\n"
		"affect other entities as well. So check before you delete.")
	);

	_propertyWidgets.vbox->GetSizer()->Add(nameHBox, 0, wxEXPAND | wxBOTTOM, 12);
	_propertyWidgets.vbox->GetSizer()->Add(infoText, 0);
}

wxBoxSizer* CustomStimEditor::createListButtons(wxWindow* parent)
{
	wxBoxSizer* hbox = new wxBoxSizer(wxHORIZONTAL);

	_listButtons.add = new wxButton(parent, wxID_ANY, _("Add Stim Type"));
	_listButtons.remove = new wxButton(parent, wxID_ANY, _("Remove Stim Type"));

	hbox->Add(_listButtons.add, 1, wxRIGHT, 6);
	hbox->Add(_listButtons.remove, 1);

	_listButtons.add->Connect(wxEVT_BUTTON, wxCommandEventHandler(CustomStimEditor::onAddStimType), NULL, this);
	_listButtons.remove->Connect(wxEVT_BUTTON, wxCommandEventHandler(CustomStimEditor::onRemoveStimType), NULL, this);
	
	return hbox;
}

void CustomStimEditor::update()
{
	_updatesDisabled = true;

	int id = getIdFromSelection();

	if (id > 0)
	{
		_propertyWidgets.vbox->Enable(true);

		StimType stimType = _stimTypes.get(id);
		_propertyWidgets.nameEntry->SetValue(stimType.caption);

		_contextMenu.menu->Enable(_contextMenu.remove->GetId(), true);
	}
	else
	{
		_propertyWidgets.vbox->Enable(false);
		_contextMenu.menu->Enable(_contextMenu.remove->GetId(), false);
	}

	_updatesDisabled = false;
}

void CustomStimEditor::selectId(int id)
{
	// Setup the selectionfinder to search for the id
	wxDataViewItem item = _customStimStore->FindInteger(id, _stimTypes.getColumns().id.getColumnIndex());

	if (item)
	{
		// Set the active row of the list to the given stim
		_list->Select(item);
	}
}

void CustomStimEditor::addStimType()
{
	// Add a new stim type with the lowest free custom id
	int id = _stimTypes.getFreeCustomStimId();

	_stimTypes.add(id,
				   string::to_string(id),
				   "CustomStimType",
				   _("Custom Stim"),
				   ICON_CUSTOM_STIM,
				   true);

	selectId(id);
	update();
}

int CustomStimEditor::getIdFromSelection()
{
	wxDataViewItem item = _list->GetSelection();

	if (item.IsOk()) 
	{
		wxutil::TreeModel::Row row(item, *_customStimStore);
		return row[_stimTypes.getColumns().id].getInteger();
	}
	
	return -1;
}

void CustomStimEditor::removeStimType()
{
	IDialogPtr dialog = GlobalDialogManager().createMessageBox(_("Delete Custom Stim"),
		_("Beware that other entities might still be using this stim type.\n"
		"Do you really want to delete this custom stim?"), ui::IDialog::MESSAGE_ASK);

	if (dialog->run() == IDialog::RESULT_YES)
	{
		_stimTypes.remove(getIdFromSelection());
	}
}

void CustomStimEditor::onAddStimType(wxCommandEvent& ev)
{
	addStimType();
}

void CustomStimEditor::onRemoveStimType(wxCommandEvent& ev)
{
	// Delete the selected stim type from the list
	removeStimType();
}

void CustomStimEditor::onEntryChanged(wxCommandEvent& ev)
{
	if (_updatesDisabled) return; // Callback loop guard

	// Set the caption of the curently selected stim type
	std::string caption = _propertyWidgets.nameEntry->GetValue().ToStdString();

	// Pass the call to the helper class
	_stimTypes.setStimTypeCaption(getIdFromSelection(), caption);

	if (_entity != NULL)
	{
		_entity->updateListStores();
	}
}

void CustomStimEditor::onSelectionChange(wxDataViewEvent& ev)
{
	update();
}

// Delete context menu items activated
void CustomStimEditor::onContextMenuDelete(wxCommandEvent& ev)
{
	// Delete the selected stim from the list
	removeStimType();
}

// Delete context menu items activated
void CustomStimEditor::onContextMenuAdd(wxCommandEvent& ev)
{
	addStimType();
}

void CustomStimEditor::onContextMenu(wxDataViewEvent& ev)
{
	_list->PopupMenu(_contextMenu.menu.get());
}

} // namespace ui