forked from SpineML/SpineCreator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SC_systemmodel.cpp
326 lines (243 loc) · 10.3 KB
/
SC_systemmodel.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
/***************************************************************************
** **
** 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: Alex Cope **
** Website/Contact: http://bimpa.group.shef.ac.uk/ **
****************************************************************************/
#include "SC_systemmodel.h"
#include "QCheckBox"
#include "SC_network_layer_rootdata.h"
systemmodel::systemmodel(nl_rootdata * dataPtr, QObject *parent) :
QAbstractItemModel(parent)
{
this->dataPtr = dataPtr;
QList<QVariant> rootData;
rootData << "Model";
rootItem = new TreeItem(rootData, NULL);
setupModelData(rootItem);
}
QModelIndexList systemmodel::getPersistentIndexList()
{
return this->persistentIndexList();
}
int systemmodel::rowCount(const QModelIndex & parent) const
{
TreeItem *parentItem;
if (parent.column() > 0)
return 0;
if (!parent.isValid())
parentItem = rootItem;
else
parentItem = static_cast<TreeItem*>(parent.internalPointer());
return parentItem->childCount();
}
int systemmodel::columnCount(const QModelIndex & parent) const
{
if (parent.isValid())
return static_cast<TreeItem*>(parent.internalPointer())->columnCount();
else
return rootItem->columnCount();
}
QVariant systemmodel::data(const QModelIndex &index, int role) const
{
if (!index.isValid())
return QVariant();
TreeItem *item = static_cast<TreeItem*>(index.internalPointer());
if ( role == Qt::CheckStateRole && item->type != projectionObject )
return static_cast< int >( item->isChecked() ? Qt::Checked : Qt::Unchecked );
if (role != Qt::DisplayRole)
return QVariant();
return item->data(index.column());
}
bool systemmodel::setData ( const QModelIndex & index, const QVariant & value, int role )
{
if (!index.isValid())
return false;
TreeItem *item = static_cast<TreeItem*>(index.internalPointer());
if ( role == Qt::CheckStateRole && index.column() == 0 ) {
item->setChecked(value.toBool());
// find and set status of item in system:
for (int i = 0; i < dataPtr->populations.size(); ++i) {
QSharedPointer <population> currPop = (QSharedPointer <population>) dataPtr->populations[i];
// populations
if (currPop->getName() == item->name) {
currPop->isVisualised = value.toBool();
}
// projections
for (int j = 0; j < currPop->projections.size(); ++j) {
QSharedPointer <projection> currProj = (QSharedPointer <projection>) currPop->projections[j];
// synapses
for (int k = 0; k < currProj->synapses.size(); ++k) {
QSharedPointer <synapse> currTarg = (QSharedPointer <synapse>) currProj->synapses[k];
if (currProj->getName() + ": Synapse " + QString::number(k) == item->name) {
currTarg->isVisualised = value.toBool();
}
}
}
}
emit dataChanged(index, index);
return true;
}
return false;
}
QModelIndex systemmodel::parent( const QModelIndex & index ) const
{
if (!index.isValid())
return QModelIndex();
TreeItem *childItem = static_cast<TreeItem*>(index.internalPointer());
TreeItem *parentItem = childItem->parent();
if (parentItem == rootItem)
return QModelIndex();
return createIndex(parentItem->row(), 0, parentItem);
}
QModelIndex systemmodel::index(int row, int column, const QModelIndex &parent) const
{
if (!hasIndex(row, column, parent))
return QModelIndex();
TreeItem *parentItem;
if (!parent.isValid())
parentItem = rootItem;
else
parentItem = static_cast<TreeItem*>(parent.internalPointer());
TreeItem *childItem = parentItem->child(row);
if (childItem)
return createIndex(row, column, childItem);
else
return QModelIndex();
}
QVariant systemmodel::headerData(int section, Qt::Orientation orientation, int role) const
{
if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
return rootItem->data(section);
return QVariant();
}
void systemmodel::emitDataChanged() {
QModelIndex index = this->createIndex(0,0);
emit dataChanged(index, index);
}
systemmodel::~systemmodel()
{
delete rootItem;
}
Qt::ItemFlags systemmodel::flags(const QModelIndex &index) const
{
if (!index.isValid())
return 0;
Qt::ItemFlags flags = Qt::ItemIsEnabled | Qt::ItemIsSelectable;
TreeItem * item = static_cast<TreeItem*>(index.internalPointer());
if (!(item->type == projectionObject))
flags |= Qt::ItemIsUserCheckable;
return flags;
}
void systemmodel::setupModelData(TreeItem *parent)
{
QList<TreeItem*> parents;
parents << parent;
for (int pop = 0; pop < dataPtr->populations.size();++pop) {
// add population
QSharedPointer <population> currPop = (QSharedPointer <population>) dataPtr->populations[pop];
QList<QVariant> columnDataPop;
columnDataPop << currPop->getName();
// add as child of current parent and set this as the new parent
parents.last()->appendChild(new TreeItem(columnDataPop, &(currPop->isVisualised), parents.last()));
parents.last()->child(parents.last()->childCount()-1)->type = currPop->type;
parents << parents.last()->child(parents.last()->childCount()-1);
// add generic inputs for Populations
for (int output = 0; output < dataPtr->populations[pop]->neuronType->outputs.size(); ++output) {
// add output
QSharedPointer<genericInput> currOutput = dataPtr->populations[pop]->neuronType->outputs[output];
// really we can only currently display pop -> pop inputs sensibly...
if (!currOutput->projInput) {
if (currOutput->source->type == populationObject && currOutput->destination->type == populationObject) {
QList<QVariant> columnDataInput;
columnDataInput << "Output from " + currOutput->source->getName() + " to " + currOutput->destination->getName() + " port " + currOutput->dstPort + " " + QString::number(output);
// add as child of current parent
parents.last()->appendChild(new TreeItem(columnDataInput, &(currOutput->isVisualised), parents.last()));
parents.last()->child(parents.last()->childCount()-1)->type = currOutput->type;
}
}
}
// add projections
for (int proj = 0; proj < currPop->projections.size(); ++proj) {
// add projection
QSharedPointer <projection> currProj = (QSharedPointer <projection>) currPop->projections[proj];
QList<QVariant> columnDataProj;
columnDataProj << currProj->getName();
// add as child of current parent and set this as the new parent
parents.last()->appendChild(new TreeItem(columnDataProj, NULL, parents.last()));
parents.last()->child(parents.last()->childCount()-1)->type = currProj->type;
parents << parents.last()->child(parents.last()->childCount()-1);
for (int targ = 0; targ < dataPtr->populations[pop]->projections[proj]->synapses.size(); ++targ) {
// add Synapse
//QSharedPointer <projection> currTarg = (QSharedPointer <projection>) currProj->synapses[targ];
QList<QVariant> columnDataTarg;
columnDataTarg << currProj->getName() + ": Synapse " + QString::number(targ);
// add as child of current parent
parents.last()->appendChild(new TreeItem(columnDataTarg, &(currProj->synapses[targ]->isVisualised), parents.last()));
parents.last()->child(parents.last()->childCount()-1)->type = nullObject;
}
// go back a level of parents
parents.pop_back();
}
// go back a level of parents
parents.pop_back();
}
}
TreeItem::TreeItem(const QList<QVariant> &data, bool * check, TreeItem *parent)
{
parentItem = parent;
itemData = data;
checked = check;
name = itemData[0].toString();
}
TreeItem::~TreeItem()
{
qDeleteAll(childItems);
}
void TreeItem::appendChild(TreeItem *item)
{
childItems.append(item);
}
TreeItem *TreeItem::child(int row)
{
return childItems.value(row);
}
int TreeItem::childCount() const
{
return childItems.count();
}
int TreeItem::row() const
{
if (parentItem)
return parentItem->childItems.indexOf(const_cast<TreeItem*>(this));
return 0;
}
int TreeItem::columnCount() const
{
return itemData.count();
}
QVariant TreeItem::data(int column) const
{
return itemData.value(column);
}
TreeItem *TreeItem::parent()
{
return parentItem;
}