v2.0.0
Loading...
Searching...
No Matches
bidsviewmodel.cpp
Go to the documentation of this file.
1//=============================================================================================================
13
14//=============================================================================================================
15// INCLUDES
16//=============================================================================================================
17
18#include "bidsviewmodel.h"
19#include <iostream>
20
21//=============================================================================================================
22// QT INCLUDES
23//=============================================================================================================
24
25#include <QtDebug>
26
27//=============================================================================================================
28// USED NAMESPACES
29//=============================================================================================================
30
31using namespace DISPLIB;
32
33//=============================================================================================================
34// DEFINE MEMBER METHODS
35//=============================================================================================================
36
38: QStandardItemModel(pParent)
39{
40}
41
42//=============================================================================================================
43
47
48//=============================================================================================================
49
50void BidsViewModel::addData(QModelIndex selectedItem,
51 QStandardItem* pNewItem,
52 int iDataType)
53{
54 switch (iDataType) {
58 if (!selectedItem.isValid()) {
59 addDataToSession(addSessionToSubject(addSubject("sub-01"), "ses-01"), pNewItem, iDataType);
60 } else {
61 if (itemFromIndex(selectedItem)->data(BIDS_ITEM_TYPE).value<int>() != BIDS_SUBJECT) {
62 addDataToSession(itemFromIndex(selectedItem)->data(BIDS_ITEM_SESSION).value<QModelIndex>(),
63 pNewItem,
64 iDataType);
65 } else {
66 qDebug() << "[BidsViewModel::addData] Prompt user to select a session";
67 }
68 }
69 break;
70 }
71 case BIDS_EVENT:
72 case BIDS_AVERAGE:
73 case BIDS_DIPOLE: {
74 if (!selectedItem.isValid()) {
75 QStandardItem* pItem = new QStandardItem("Unknown");
76 pItem->setEditable(false);
77 pItem->setDragEnabled(true);
78
79 addDataToSession(addSessionToSubject(addSubject("sub-01"), "ses-01"), pItem, BIDS_UNKNOWN);
80 addToData(pNewItem,
81 indexFromItem(pItem),
82 iDataType);
83 } else {
84 addToData(pNewItem,
85 selectedItem,
86 iDataType);
87 }
88 break;
89 }
90 }
91}
92
93//=============================================================================================================
94
95void BidsViewModel::addToData(QStandardItem* pNewAvgItem,
96 const QModelIndex& parentIndex,
97 int iDataType)
98{
99 QStandardItem* selectedData = itemFromIndex(parentIndex);
100 selectedData->setChild(selectedData->rowCount(),
101 pNewAvgItem);
102
103 pNewAvgItem->setData(itemFromIndex(parentIndex)->data(BIDS_ITEM_SUBJECT), BIDS_ITEM_SUBJECT);
104 pNewAvgItem->setData(itemFromIndex(parentIndex)->data(BIDS_ITEM_SESSION), BIDS_ITEM_SESSION);
105 pNewAvgItem->setData(QVariant::fromValue(iDataType), BIDS_ITEM_TYPE);
106
107 emit newItemIndex(pNewAvgItem->index());
108}
109
110//=============================================================================================================
111
112QModelIndex BidsViewModel::addSubject(const QString& sSubjectName)
113{
114 //Ensure subject name follow BIDS format
115 QString sNewSubjectName;
116
117 if (!sSubjectName.startsWith("sub-")) {
118 sNewSubjectName = "sub-" + sSubjectName;
119 } else {
120 sNewSubjectName = sSubjectName;
121 }
122
123 sNewSubjectName.remove(" ");
124
125 //Add subject to model
126 QStandardItem* pSubjectItem = new QStandardItem(sNewSubjectName);
127 appendRow(pSubjectItem);
128
129 pSubjectItem->setData(QVariant::fromValue(BIDS_SUBJECT), BIDS_ITEM_TYPE);
130 pSubjectItem->setData(QVariant::fromValue(pSubjectItem->index()), BIDS_ITEM_SUBJECT);
131
132 emit newItemIndex(pSubjectItem->index());
133
134 return pSubjectItem->index();
135}
136
137//=============================================================================================================
138
139QModelIndex BidsViewModel::addSessionToSubject(const QString& sSubjectName,
140 const QString& sSessionName)
141{
142 //Ensure session name follow BIDS format
143 QString sNewSessionName;
144
145 if (!sSessionName.startsWith("ses-")) {
146 sNewSessionName = "ses-" + sSessionName;
147 } else {
148 sNewSessionName = sSessionName;
149 }
150
151 sNewSessionName.remove(" ");
152
153 QList<QStandardItem*> listItems = findItems(sSubjectName);
154 bool bRepeat = false;
155
156 //Check for repeated names, no names
157 if (listItems.size() > 1) {
158 qWarning() << "[BidsViewModel::addSessionToSubject] Multiple subjects with same name";
159 bRepeat = true;
160 } else if (listItems.size() == 0) {
161 qWarning() << "[BidsViewModel::addSessionToSubject] No Subject found with name:" << sSubjectName;
162 return QModelIndex();
163 }
164
165 // Stays null when the loop body never runs, which the return below guards
166 // against; dereferencing it unconditionally would be undefined.
167 QStandardItem* pNewSessionItem = nullptr;
168
169 //Add session to subjects with mathcing names. Renames them if multiple.
170 for (int i = 0; i < listItems.size(); i++) {
171 QStandardItem* pSubjectItem = listItems.at(i);
172
173 if (bRepeat) {
174 pSubjectItem->setText(pSubjectItem->text() + QString::number(i + 1));
175 }
176
177 pNewSessionItem = new QStandardItem(sNewSessionName);
178 pSubjectItem->setChild(pSubjectItem->rowCount(),
179 pNewSessionItem);
180
181 pNewSessionItem->setData(QVariant::fromValue(BIDS_SESSION), BIDS_ITEM_TYPE);
182 pNewSessionItem->setData(QVariant::fromValue(pSubjectItem->index()), BIDS_ITEM_SUBJECT);
183 pNewSessionItem->setData(QVariant::fromValue(pNewSessionItem->index()), BIDS_ITEM_SESSION);
184
185 emit newItemIndex(pNewSessionItem->index());
186 }
187
188 if (!pNewSessionItem) {
189 return QModelIndex();
190 }
191
192 return pNewSessionItem->index();
193}
194
195//=============================================================================================================
196
197QModelIndex BidsViewModel::addSessionToSubject(QModelIndex subjectIndex,
198 const QString& sSessionName)
199{
200 //Ensure session name follow BIDS format
201 QString sNewSessionName;
202
203 if (!sSessionName.startsWith("ses-")) {
204 sNewSessionName = "ses-" + sSessionName;
205 } else {
206 sNewSessionName = sSessionName;
207 }
208
209 sNewSessionName.remove(" ");
210
211 QStandardItem* pSubjectItem = itemFromIndex(subjectIndex);
212 QStandardItem* pNewSessionItem = new QStandardItem(sNewSessionName);
213 pSubjectItem->setChild(pSubjectItem->rowCount(),
214 pNewSessionItem);
215
216 pNewSessionItem->setData(QVariant::fromValue(BIDS_SESSION), BIDS_ITEM_TYPE);
217 pNewSessionItem->setData(QVariant::fromValue(subjectIndex), BIDS_ITEM_SUBJECT);
218 pNewSessionItem->setData(QVariant::fromValue(pNewSessionItem->index()), BIDS_ITEM_SESSION);
219
220 emit newItemIndex(pNewSessionItem->index());
221
222 return pNewSessionItem->index();
223}
224
225//=============================================================================================================
226
227QModelIndex BidsViewModel::addDataToSession(QModelIndex sessionIndex,
228 QStandardItem* pNewItem,
229 int iDataType)
230{
231 QStandardItem* pSessionItem = itemFromIndex(sessionIndex);
232 bool bFolder = false;
233 int iFolder;
234
235 QString sFolderName;
236
237 switch (iDataType) {
239 sFolderName = "func";
240 break;
242 sFolderName = "anat";
243 break;
245 sFolderName = "beh";
246 break;
247 default:
248 sFolderName = "unknown";
249 }
250
251 for (iFolder = 0; iFolder < pSessionItem->rowCount(); iFolder++) {
252 if (pSessionItem->child(iFolder)->text() == sFolderName) {
253 bFolder = true;
254 break;
255 }
256 }
257
258 if (!bFolder) {
259 QStandardItem* pFunctionalItem = new QStandardItem(sFolderName);
260 pFunctionalItem->setData(QVariant::fromValue(BIDS_FOLDER), BIDS_ITEM_TYPE);
261 pFunctionalItem->setData(QVariant::fromValue(sessionIndex), BIDS_ITEM_SESSION);
262 pFunctionalItem->setData(itemFromIndex(sessionIndex)->data(BIDS_ITEM_SUBJECT), BIDS_ITEM_SUBJECT);
263
264 pSessionItem->setChild(pSessionItem->rowCount(),
265 pFunctionalItem);
266 pFunctionalItem->setChild(pFunctionalItem->rowCount(),
267 pNewItem);
268 } else {
269 pSessionItem->child(iFolder)->setChild(pSessionItem->child(iFolder)->rowCount(),
270 pNewItem);
271 }
272
273 pNewItem->setData(QVariant::fromValue(iDataType), BIDS_ITEM_TYPE);
274 pNewItem->setData(QVariant::fromValue(sessionIndex), BIDS_ITEM_SESSION);
275 pNewItem->setData(itemFromIndex(sessionIndex)->data(BIDS_ITEM_SUBJECT), BIDS_ITEM_SUBJECT);
276
277 emit newItemIndex(pNewItem->index());
278
279 return pNewItem->index();
280}
281
282//=============================================================================================================
283
284QModelIndex BidsViewModel::moveSessionToSubject(QModelIndex subjectIndex,
285 QModelIndex sessionIndex)
286{
287 beginResetModel();
288
289 QStandardItem* subjectItem = itemFromIndex(subjectIndex);
290 QStandardItem* sessionItem = itemFromIndex(sessionIndex);
291
292 sessionItem->parent()->takeRow(sessionItem->row());
293
294 subjectItem->setChild(subjectItem->rowCount(), sessionItem);
295 subjectItem->setData(subjectIndex, BIDS_ITEM_SUBJECT);
296
297 for (int i = 0; i < sessionItem->rowCount(); i++) {
298 sessionItem->child(i)->setData(subjectIndex, BIDS_ITEM_SUBJECT);
299 for (int j = 0; j < sessionItem->child(i)->rowCount(); j++) {
300 sessionItem->child(i)->child(j)->setData(subjectIndex, BIDS_ITEM_SUBJECT);
301 }
302 }
303
304 endResetModel();
305
306 emit newItemIndex(sessionItem->index());
307
308 return sessionItem->index();
309}
310
311//=============================================================================================================
312
313QModelIndex BidsViewModel::moveDataToSession(QModelIndex sessionIndex,
314 QModelIndex dataIndex)
315{
316 beginResetModel();
317
318 QStandardItem* dataItem = itemFromIndex(dataIndex);
319
320 if (dataItem->parent()->rowCount() < 2) {
321 QStandardItem* parent = dataItem->parent();
322
323 dataItem->parent()->takeRow(dataItem->row()).first();
324 parent->parent()->takeRow(parent->row()).first();
325 } else {
326 dataItem->parent()->takeRow(dataItem->row()).first();
327 }
328
329 QModelIndex newIndex;
330 // switch(dataItem->data(ITEM_TYPE).value<int>()){
331 // case FUNCTIONALDATA:{
332 // newIndex = addDataToSession(sessionIndex,
333 // dataItem,
334 // FUNCTIONALDATA);
335 // break;
336 // }
337 // default:{
338 // qWarning() << "[BidsViewModel::moveDataToSession] Move not supported for this type of data";
339 // }
340 // }
341
342 newIndex = addDataToSession(sessionIndex,
343 dataItem,
344 dataItem->data(BIDS_ITEM_TYPE).value<int>());
345
346 endResetModel();
347
348 emit newItemIndex(sessionIndex);
349 emit newItemIndex(newIndex);
350
351 return newIndex;
352}
353
354//=============================================================================================================
355
356bool BidsViewModel::removeItem(QModelIndex itemIndex)
357{
358 if (!itemIndex.isValid()) {
359 return false;
360 }
361
362 beginResetModel();
363
364 QStandardItem* pItem = itemFromIndex(itemIndex);
365
366 qInfo() << "Deleting" << pItem->text();
367
368 switch (pItem->data(BIDS_ITEM_TYPE).value<int>()) {
369 case BIDS_SUBJECT:
370 if (removeRows(itemIndex.row(), 1, itemIndex.parent())) {
371 endResetModel();
372 }
373 return true;
374 case BIDS_SESSION:
375 if (removeRows(itemIndex.row(), 1, itemIndex.parent())) {
376 endResetModel();
377 }
378 return true;
382 if (removeRows(itemIndex.row(), 1, itemIndex.parent())) {
383 endResetModel();
384 }
385 return true;
386 case BIDS_AVERAGE:
387 case BIDS_EVENT:
388 case BIDS_DIPOLE:
389 if (removeRows(itemIndex.row(), 1, itemIndex.parent())) {
390 endResetModel();
391 }
392 return true;
393 default:
394 if (removeRows(itemIndex.row(), 1, itemIndex.parent())) {
395 endResetModel();
396 }
397 return true;
398 }
399}
QStandardItemModel mirroring the BIDS dataset hierarchy (subjects / sessions / runs / derivatives).
#define BIDS_ITEM_SESSION
#define BIDS_ITEM_SUBJECT
#define BIDS_ITEM_TYPE
constexpr auto BIDS_FUNCTIONALDATA
constexpr auto BIDS_UNKNOWN
constexpr auto BIDS_ANATOMICALDATA
constexpr auto BIDS_DIPOLE
constexpr auto BIDS_AVERAGE
constexpr auto BIDS_SUBJECT
constexpr auto BIDS_BEHAVIORALDATA
constexpr auto BIDS_EVENT
constexpr auto BIDS_FOLDER
constexpr auto BIDS_SESSION
2-D display widgets and visualisation helpers (charts, topography, colour maps).
QModelIndex addDataToSession(QModelIndex sessionIndex, QStandardItem *pNewItem, int iDataType)
bool removeItem(QModelIndex itemIndex)
BidsViewModel(QObject *pParent=nullptr)
QModelIndex moveSessionToSubject(QModelIndex subjectIndex, QModelIndex sessionIndex)
void newItemIndex(QModelIndex itemIndex)
QModelIndex moveDataToSession(QModelIndex sessionIndex, QModelIndex dataIndex)
QModelIndex addSubject(const QString &sSubjectName)
void addToData(QStandardItem *pNewItem, const QModelIndex &parentIndex, int iDataType)
QModelIndex addSessionToSubject(const QString &sSubjectName, const QString &sSessionName)
void addData(QModelIndex selectedItem, QStandardItem *pNewItem, int iDataType)