forked from SpineML/SpineCreator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SC_component_rootcomponentitem.cpp
536 lines (471 loc) · 20.6 KB
/
SC_component_rootcomponentitem.cpp
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
/***************************************************************************
** **
** This file is part of SpineCreator, an easy to use GUI for **
** describing spiking neural network models. **
** Copyright (C) 2013-2014 Alex Cope, Paul Richmond, Seb James **
** **
** This program is free software: you can redistribute it and/or modify **
** it under the terms of the GNU General Public License as published by **
** the Free Software Foundation, either version 3 of the License, or **
** (at your option) any later version. **
** **
** This program is distributed in the hope that it will be useful, **
** but WITHOUT ANY WARRANTY; without even the implied warranty of **
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the **
** GNU General Public License for more details. **
** **
** You should have received a copy of the GNU General Public License **
** along with this program. If not, see http://www.gnu.org/licenses/. **
** **
****************************************************************************
** Author: Paul Richmond **
** Website/Contact: http://bimpa.group.shef.ac.uk/ **
****************************************************************************/
#include <algorithm>
#include <typeinfo>
#include "SC_component_rootcomponentitem.h"
#include "SC_component_graphicsitems.h"
#include "SC_component_propertiesmanager.h"
#include "SC_component_scene.h"
#include "SC_network_layer_rootdata.h"
#include "SC_undocommands.h"
RootComponentItem::RootComponentItem(MainWindow *main,Ui::MainWindow *ui, QFile *file)
{
this->ui =ui;
this->main = main;
init();
al = QSharedPointer<Component>(new Component());
//load file if one is provided (otherwise create blank doc)
if (file != NULL)
{
QDomDocument doc("SpineML");
doc.setContent(file);
al->load(&doc);
}
else
{
al->name = "New Component";
al->type = "neuron_body";
}
alPtr.clear();
//initialise the scene
scene->initialiseScene(al);
}
RootComponentItem::RootComponentItem(MainWindow *main,Ui::MainWindow *ui, QSharedPointer<Component>component)
{
this->ui =ui;
this->main = main;
init();
al = QSharedPointer<Component>(new Component(component));
alPtr.clear();
//initialise the scene
scene->initialiseScene(al);
}
RootComponentItem::~RootComponentItem()
{
//any left over data
delete properties;
properties = NULL;
delete scene;
scene = NULL;
delete gvlayout;
gvlayout = NULL;
al.clear();
alPtr.clear();
delete actionSelectMode;
actionSelectMode = NULL;
delete actionAddOnCondition;
actionAddOnCondition = NULL;
delete actionAddOnEvent;
actionAddOnEvent = NULL;
delete actionLayout;
actionAddOnImpulse = NULL;
delete actionAddOnImpulse;
actionLayout = NULL;
delete actionDeleteItems;
actionDeleteItems = NULL;
delete actionMove_Up;
actionMove_Up = NULL;
delete actionMove_Down;
actionMove_Down = NULL;
delete actionShowHidePorts;
actionShowHidePorts = NULL;
delete actionShowHideParams;
actionShowHideParams = NULL;
delete actionZoomIn;
actionZoomIn = NULL;
delete actionZoonOut;
actionZoonOut = NULL;
delete actionAddTimeDerivative;
actionAddTimeDerivative = NULL;
delete actionAddParameter;
actionAddParameter = NULL;
delete actionAddStateVariable;
actionAddStateVariable = NULL;
delete actionAddAlias;
actionAddAlias = NULL;
delete actionAddAnalogePort;
actionAddAnalogePort = NULL;
delete actionAddEventPort;
actionAddEventPort = NULL;
delete actionAddEventOut;
actionAddEventOut = NULL;
delete actionAddStateAssignment;
actionAddStateAssignment = NULL;
delete actionAddRegime;
actionAddRegime = NULL;
toolbar->deleteLater();
addItemsToolbar->deleteLater();
}
void RootComponentItem::setSelectionMode(ALSceneMode mode)
{
scene->clearSelection();
switch(mode){
case(ModeSelect):{
clearSelection();
actionSelectMode->setChecked(true);
actionDeleteItems->setChecked(false);
actionAddOnCondition->setChecked(false);
actionAddOnEvent->setChecked(false);
actionAddOnImpulse->setChecked(false);
main->viewCL.display->setCursor(Qt::ArrowCursor);
break;
}
case(ModeInsertOnCondition):{
actionAddOnCondition->setChecked(true);
actionAddOnImpulse->setChecked(false);
actionDeleteItems->setChecked(false);
actionSelectMode->setChecked(false);
actionAddOnEvent->setChecked(false);
main->viewCL.display->setCursor(Qt::ArrowCursor);
break;
}
case(ModeInsertOnEvent):{
actionAddOnEvent->setChecked(true);
actionAddOnImpulse->setChecked(false);
actionDeleteItems->setChecked(false);
actionSelectMode->setChecked(false);
actionAddOnCondition->setChecked(false);
main->viewCL.display->setCursor(Qt::ArrowCursor);
break;
}
case(ModeInsertOnImpulse):{
actionAddOnImpulse->setChecked(true);
actionAddOnEvent->setChecked(false);
actionDeleteItems->setChecked(false);
actionSelectMode->setChecked(false);
actionAddOnCondition->setChecked(false);
main->viewCL.display->setCursor(Qt::ArrowCursor);
break;
}
}
scene->setMode(mode);
}
void RootComponentItem::clearSelection()
{
properties->clear();
scene->clearSelection();
}
void RootComponentItem::notifyDataChange()
{
// Update the component in the component library in root data with this line:
if (this->alPtr != NULL && this->al != NULL) {
this->alPtr->updateFrom (this->al);
}
emit unsavedChange(true);
}
void RootComponentItem::requestLayoutUpdate()
{
gvlayout->updateLayout();
}
void RootComponentItem::setComponentClassName(QString name)
{
QSharedPointer<Component> oldComponent = QSharedPointer<Component>(new Component(al));
// find which catalog we are saving to
QVector<QSharedPointer<Component> >* curr_lib = (QVector<QSharedPointer<Component> >*)0;
if (al->type == "neuron_body")
curr_lib = &main->data.catalogNrn;
if (al->type == "weight_update")
curr_lib = &main->data.catalogWU;
if (al->type == "postsynapse")
curr_lib = &main->data.catalogPS;
if (al->type == "generic_component")
curr_lib = &main->data.catalogUnsorted;
bool unique = true;
// check if name is unique
for (int i = 0; i < curr_lib->size(); ++i) {
if ((*curr_lib)[i]->name == name) {
qDebug() << name << " is not unique in the component catalog.";
unique = false;
}
}
// find unique name
int val = -1;
if (!unique) {
val = 1;
// see if there is a component with the same name
while (!unique) {
unique = true;
for (int i = 0; i < curr_lib->size(); ++i) {
if ((*curr_lib)[i]->name == name + QString::number(float(val))) {
unique = false;
}
}
if (!unique) {
++val;
}
}
}
if (val != -1) {
qDebug() << "Adding extra suffix to name to make it unique";
name += " " + QString::number(val);
}
// Update the name in the current component.
al->name = name;
notifyDataChange();
if (qobject_cast < QLineEdit *> (sender())) {
alPtr->undoStack.push(new changeComponent(this, oldComponent, "Set Component name"));
} else {
oldComponent.clear();
}
main->updateTitle();
}
void RootComponentItem::setComponentClassType(QString type)
{
QSharedPointer<Component> oldComponent = QSharedPointer<Component>(new Component(al));
QString oldType = al->type;
al->type = type;
QVector <QSharedPointer<Component> > * old_lib = (QVector <QSharedPointer<Component> > *)0;
QVector <QSharedPointer<Component> > * new_lib = (QVector <QSharedPointer<Component> > *)0;
// pointer to source catalog
if (oldType == "neuron_body")
old_lib = &main->data.catalogNrn;
if (oldType == "weight_update")
old_lib = &main->data.catalogWU;
if (oldType == "postsynapse")
old_lib = &main->data.catalogPS;
if (oldType == "generic_component")
old_lib = &main->data.catalogUnsorted;
// pointer to dest catalog
if (type == "neuron_body")
new_lib = &main->data.catalogNrn;
if (type == "weight_update")
new_lib = &main->data.catalogWU;
if (type == "postsynapse")
new_lib = &main->data.catalogPS;
if (type == "generic_component")
new_lib = &main->data.catalogUnsorted;
notifyDataChange();
if (qobject_cast < QComboBox *> (sender()))
alPtr->undoStack.push(new changeComponentType(this, old_lib, new_lib, alPtr, "Set Component class type"));
else
oldComponent.clear();
}
void RootComponentItem::setLearningState(bool b)
{
al->islearning = b;
}
void RootComponentItem::setInitialRegime(QString regime)
{
QSharedPointer<Component> oldComponent = QSharedPointer<Component> (new Component(al));
for (int i=0; i< al->RegimeList.size(); i++){
if (al->RegimeList[i]->name == regime){
al->initial_regime = al->RegimeList[i];
al->initial_regime_name = regime;
}
}
if (al->initial_regime) {
emit initialRegimeChanged();
}
notifyDataChange();
if (qobject_cast < QComboBox *> (sender())) {
alPtr->undoStack.push(new changeComponent(this, oldComponent, "Set Component Initial Regime"));
} else {
oldComponent.clear();
}
}
void RootComponentItem::setPath(QString component_path)
{
QSharedPointer<Component> oldComponent = QSharedPointer<Component> (new Component(al));
al->path = component_path;
if (qobject_cast < QComboBox *> (sender()))
alPtr->undoStack.push(new changeComponent(this, oldComponent, "Set Component path"));
else
oldComponent.clear();
}
void RootComponentItem::validateAndStore()
{
emit validateAndStoreSignal();
properties->clear();
properties->createEmptySelectionProperties();
}
void RootComponentItem::deleteComponent()
{
if (alPtr == NULL)
return;
// check if component is referenced:
if (rootDataPtr->isComponentInUse(alPtr)) {
QMessageBox msgBox;
msgBox.setIcon(QMessageBox::Warning);
if (al->path == "temp" || al->path == "model")
msgBox.setText("Cannot close: component is in use in network");
else
msgBox.setText("Cannot delete: component is in use in network");
msgBox.exec();
} else {
if (alPtr->type == "neuron_body") {
for (int i = 0; i < rootDataPtr->catalogNrn.size(); ++i)
if (rootDataPtr->catalogNrn[i] == alPtr)
rootDataPtr->catalogNrn.erase(rootDataPtr->catalogNrn.begin()+i);
} else if (alPtr->type == "weight_update") {
for (int i = 0; i < rootDataPtr->catalogWU.size(); ++i)
if (rootDataPtr->catalogWU[i] == alPtr)
rootDataPtr->catalogWU.erase(rootDataPtr->catalogWU.begin()+i);
} else if (alPtr->type == "postsynapse") {
for (int i = 0; i < rootDataPtr->catalogPS.size(); ++i)
if (rootDataPtr->catalogPS[i] == alPtr)
rootDataPtr->catalogPS.erase(rootDataPtr->catalogPS.begin()+i);
} else //generic_component
for (int i = 0; i < rootDataPtr->catalogUnsorted.size(); ++i)
if (rootDataPtr->catalogUnsorted[i] == alPtr)
rootDataPtr->catalogUnsorted.erase(rootDataPtr->catalogUnsorted.begin()+i);
/*delete scene;
delete gvlayout;
delete al;
alPtr = NULL;
gvlayout = new GVLayout();
scene = new NineMLALScene(this);*/
//initialise the scene
/*scene->initialiseScene();
this->main->viewCL.display->setScene(scene);
gvlayout->updateLayout();
properties->clear();
properties->createEmptySelectionProperties();*/
}
// update the file list
main->viewCL.fileList->disconnect();
main->addComponentsToFileList();
connect(main->viewCL.fileList, SIGNAL(currentItemChanged(QListWidgetItem*,QListWidgetItem*)), main, SLOT(fileListItemChanged(QListWidgetItem*,QListWidgetItem*)));
// update list in NL editor
main->viewNL.layout->updatePanel(&main->data);
// go down
main->viewCL.root = NULL;
delete this;
}
void RootComponentItem::init()
{
// add actions and toolbars:
toolbar = new QToolBar("Component main toolbar");
main->viewCL.subWin->addToolBar(Qt::TopToolBarArea,toolbar);
#ifndef Q_OS_MAC
toolbar->setStyleSheet(main->toolbarStyleSheet);
#endif
toolbar->setMaximumHeight(32);
toolbar->layout()->setMargin(0);
addItemsToolbar = new QToolBar("Component additional toolbar");
main->viewCL.subWin->addToolBar( Qt::TopToolBarArea, addItemsToolbar);
#ifndef Q_OS_MAC
addItemsToolbar->setStyleSheet(main->toolbarStyleSheet);
#endif
addItemsToolbar->setMaximumHeight(32);
addItemsToolbar->layout()->setMargin(0);
properties = new PropertiesManager(this);
gvlayout = new GVLayout();
scene = new NineMLALScene(this);
actionSelectMode = new QAction(QIcon(":/icons/toolbar/images/cursor.png"),"",main);
actionSelectMode->setCheckable(true);
actionSelectMode->setChecked(true);
actionSelectMode->setToolTip("Select Mode: Select items in the viewport");
actionAddOnCondition = new QAction(QIcon(":/icons/toolbar/images/add_oncond.png"), "",main);
actionAddOnCondition->setCheckable(true);
actionAddOnCondition->setChecked(false);
actionAddOnCondition->setToolTip("OnCondition Add Mode: Drag between Regimes to add OnCondition");
actionAddOnEvent = new QAction(QIcon(":/icons/toolbar/images/add_onev.png"), "",main);
actionAddOnEvent->setCheckable(true);
actionAddOnEvent->setChecked(false);
actionAddOnEvent->setToolTip("OnEvent Add Mode: Drag between Regimes to add OnEvent");
actionAddOnImpulse = new QAction(QIcon(":/icons/toolbar/images/add_onimp.png"), "",main);
actionAddOnImpulse->setCheckable(true);
actionAddOnImpulse->setChecked(false);
actionAddOnImpulse->setToolTip("OnImpulse Add Mode: Drag between Regimes to add OnImpulse");
actionLayout = new QAction(QIcon(":/icons/toolbar/images/layout-icon.png"), "",main);
actionDeleteItems = new QAction(QIcon(":/icons/toolbar/delShad.png"), "",main);
actionDeleteItems->setToolTip("Delete selected items");
actionMove_Up = new QAction(QIcon(":/icons/toolbar/up.png"),"",main);
actionMove_Up->setToolTip("Move selected item up in list");
actionMove_Down = new QAction(QIcon(":/icons/toolbar/down.png"),"",main);
actionMove_Down->setToolTip("Move selected item down in list");
actionShowHidePorts = new QAction(QIcon(":/icons/toolbar/images/sh-ports-icon.png"),"",main);
actionShowHidePorts->setCheckable(true);
actionShowHidePorts->setChecked(true);
actionShowHidePorts->setToolTip("Show/hide Ports box");
actionShowHideParams = new QAction(QIcon(":/icons/toolbar/images/sh-param-icon.png"),"",main);
actionShowHideParams->setCheckable(true);
actionShowHideParams->setChecked(true);
actionShowHideParams->setToolTip("Show/hide Params, Vars & Alias box");
actionZoomIn = new QAction(QIcon(":/icons/toolbar/images/zoom_in.png"),"",main);
actionZoomIn->setToolTip("Zoom viewport in");
actionZoonOut = new QAction(QIcon(":/icons/toolbar/images/zoom_out.png"),"",main);
actionZoonOut->setToolTip("Zoom viewport out");
connect(actionSelectMode, SIGNAL(triggered()), main, SLOT(actionSelectMode_triggered()));
connect(actionAddOnCondition, SIGNAL(triggered()), main, SLOT(actionAddOnCondition_triggered()));
connect(actionAddOnEvent, SIGNAL(triggered()), main, SLOT(actionAddOnEvent_triggered()));
connect(actionAddOnImpulse, SIGNAL(triggered()), main, SLOT(actionAddOnImpulse_triggered()));
//connect(actionLayout, SIGNAL(triggered()), main, SLOT(requestLayoutUpdate()));
connect(actionDeleteItems, SIGNAL(triggered()), main, SLOT(actionDeleteItems_triggered()));
connect(actionMove_Up, SIGNAL(triggered()), main, SLOT(actionMove_Up_triggered()));
connect(actionMove_Down, SIGNAL(triggered()), main, SLOT(actionMove_Down_triggered()));
connect(actionShowHidePorts, SIGNAL(toggled(bool)), main, SLOT(actionShowHidePorts_triggered(bool)));
connect(actionShowHideParams, SIGNAL(toggled(bool)), main, SLOT(actionShowHideParams_triggered(bool)));
connect(actionZoomIn, SIGNAL(triggered()), main, SLOT(actionZoomIn_triggered()));
connect(actionZoonOut, SIGNAL(triggered()), main, SLOT(actionZoomOut_triggered()));
toolbar->addAction(actionSelectMode);
toolbar->addAction(actionAddOnCondition);
toolbar->addAction(actionAddOnEvent);
toolbar->addAction(actionAddOnImpulse);
toolbar->addSeparator();
//toolbar->addAction(actionLayout);
toolbar->addAction(actionDeleteItems);
toolbar->addAction(actionMove_Up);
toolbar->addAction(actionMove_Down);
toolbar->addSeparator();
toolbar->addAction(actionShowHidePorts);
toolbar->addAction(actionShowHideParams);
toolbar->addSeparator();
toolbar->addAction(actionZoomIn);
toolbar->addAction(actionZoonOut);
toolbar->addSeparator();
actionAddTimeDerivative = new QAction(QIcon(":/icons/toolbar/images/derivative-icon.png"),"",main);
actionAddTimeDerivative->setToolTip("Add a TimeDerivative to the selected Regime");
actionAddParameter = new QAction(QIcon(":/icons/toolbar/images/param-icon.png"),"",main);
actionAddParameter->setToolTip("Add a Parameter");
actionAddStateVariable = new QAction(QIcon(":/icons/toolbar/images/statevar-icon.png"),"",main);
actionAddStateVariable->setToolTip("Add a State Variable");
actionAddAlias = new QAction(QIcon(":/icons/toolbar/images/alias-icon.png"),"",main);
actionAddAlias->setToolTip("Add an Alias");
actionAddAnalogePort = new QAction(QIcon(":/icons/toolbar/images/analogport-icon.png"),"",main);
actionAddAnalogePort->setToolTip("Add an Analog Port");
actionAddEventPort = new QAction(QIcon(":/icons/toolbar/images/eventport-icon.png"),"",main);
actionAddEventPort->setToolTip("Add an Event Port");
actionAddImpulsePort = new QAction(QIcon(":/icons/toolbar/images/impulseport-icon.png"),"",main);
actionAddImpulsePort->setToolTip("Add an Impulse Port");
actionAddEventOut = new QAction(QIcon(":/icons/toolbar/images/emitevent-icon.png"),"",main);
actionAddEventOut->setToolTip("Add an EventOut to the current Transition");
actionAddImpulseOut = new QAction(QIcon(":/icons/toolbar/images/emitimpulse-icon.png"),"",main);
actionAddImpulseOut->setToolTip("Add an ImpulseOut to the current Transition");
actionAddStateAssignment = new QAction(QIcon(":/icons/toolbar/images/stateassignment-icon.png"),"",main);
actionAddStateAssignment->setToolTip("Add an State Assignment to the current Transition");
actionAddRegime = new QAction(QIcon(":/icons/toolbar/images/regime-icon.png"),"",main);
actionAddRegime->setToolTip("Add a Regime to the Component");
connect(actionAddTimeDerivative, SIGNAL(triggered()), main, SLOT(actionAddTimeDerivative_triggered()));
connect(actionAddParameter, SIGNAL(triggered()), main, SLOT(actionAddParamater_triggered()));
connect(actionAddStateVariable, SIGNAL(triggered()), main, SLOT(actionAddStateVariable_triggered()));
connect(actionAddAlias, SIGNAL(triggered()), main, SLOT(actionAddAlias_triggered()));
connect(actionAddAnalogePort, SIGNAL(triggered()), main, SLOT(actionAddAnalogePort_triggered()));
connect(actionAddEventPort, SIGNAL(triggered()), main, SLOT(actionAddEventPort_triggered()));
connect(actionAddImpulsePort, SIGNAL(triggered()), main, SLOT(actionAddImpulsePort_triggered()));
connect(actionAddEventOut, SIGNAL(triggered()), main, SLOT(actionAddEventOut_triggered()));
connect(actionAddImpulseOut, SIGNAL(triggered()), main, SLOT(actionAddImpulseOut_triggered()));
connect(actionAddStateAssignment, SIGNAL(triggered()), main, SLOT(actionAddStateAssignment_triggered()));
connect(actionAddRegime, SIGNAL(triggered()), main, SLOT(actionAddRegime_triggered()));
}