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