MNE-CPP  0.1.9
A Framework for Electrophysiology
bidsview.cpp
Go to the documentation of this file.
1 //=============================================================================================================
38 //=============================================================================================================
39 // INCLUDES
40 //=============================================================================================================
41 
42 #include "bidsview.h"
43 #include "ui_bidsview.h"
44 
45 #include "helpers/bidsviewmodel.h"
46 
47 //=============================================================================================================
48 // QT INCLUDES
49 //=============================================================================================================
50 
51 #include <QTreeView>
52 #include <QStandardItemModel>
53 #include <QDebug>
54 #include <QMenu>
55 #include <QKeyEvent>
56 #include <QMessageBox>
57 #include <QInputDialog>
58 
59 //=============================================================================================================
60 // USED NAMESPACES
61 //=============================================================================================================
62 
63 using namespace DISPLIB;
64 
65 //=============================================================================================================
66 // DEFINE MEMBER METHODS
67 //=============================================================================================================
68 
69 BidsView::BidsView(QWidget *parent)
70 : AbstractView(parent)
71 , m_pUi(new Ui::BidsViewWidget)
72 {
73  m_pUi->setupUi(this);
74  m_pUi->m_pTreeView->setContextMenuPolicy(Qt::CustomContextMenu);
75  connect(m_pUi->m_pTreeView, &QTreeView::customContextMenuRequested,
76  this, &BidsView::customMenuRequested, Qt::UniqueConnection);
77 }
78 
79 //=============================================================================================================
80 
82 {
83  delete m_pUi;
84 }
85 
86 //=============================================================================================================
87 
88 void BidsView::setModel(QAbstractItemModel *pModel)
89 {
90  m_pUi->m_pTreeView->setModel(pModel);
91 
92  connect(m_pUi->m_pTreeView->selectionModel(), &QItemSelectionModel::selectionChanged,
93  this, &BidsView::onCurrentItemChanged, Qt::UniqueConnection);
94 // connect(static_cast<ANSHAREDLIB::AnalyzeDataModel*>(pModel), &ANSHAREDLIB::AnalyzeDataModel::newFileAdded,
95 // this, &DataManagerControlView::onNewFileLoaded);
96  connect(static_cast<DISPLIB::BidsViewModel*>(pModel), &DISPLIB::BidsViewModel::newItemIndex,
97  this, &BidsView::onNewItemIndex);
98 
99  //Connect BIDS Model Functions
100  DISPLIB::BidsViewModel *pBidsModel = qobject_cast<DISPLIB::BidsViewModel *>(pModel);
101 
102  //Adding
103  connect(this, &BidsView::onAddSubject,
104  pBidsModel, &BidsViewModel::addSubject);
105  connect(this, SIGNAL(onAddSession(QModelIndex, const QString&)),
106  pBidsModel, SLOT(addSessionToSubject(QModelIndex, const QString&)));
107 
108  //Moving
109  connect(this, &BidsView::onMoveSession,
111  connect(this, &BidsView::onMoveData,
112  pBidsModel, &BidsViewModel::moveDataToSession);
113 
114  //Updating
115  connect(pBidsModel, &BidsViewModel::modelReset,
116  this, &BidsView::onModelReset);
117 }
118 
119 //=============================================================================================================
120 
121 void BidsView::customMenuRequested(QPoint pos)
122 {
123  QString sToolTip = m_pUi->m_pTreeView->model()->data(m_pUi->m_pTreeView->indexAt(pos), Qt::ToolTipRole).toString();
124 
125  DISPLIB::BidsViewModel *pModel = qobject_cast<DISPLIB::BidsViewModel *>(m_pUi->m_pTreeView->model());
126 
127  QAction* pRemoveAction;
128 
129  if(m_pUi->m_pTreeView->indexAt(pos).isValid()){
130  QStandardItem* pItem = pModel->itemFromIndex(m_pUi->m_pTreeView->indexAt(pos));
131 
132  switch (pItem->data(BIDS_ITEM_TYPE).value<int>()){
133  case BIDS_SUBJECT: {
134  QMenu *menu = new QMenu(this);
135 
136  QAction* pAddSessionAction = new QAction("Add Session", this);
137  connect(pAddSessionAction, &QAction::triggered, [=]() {
138  bool ok;
139  QString text = QInputDialog::getText(this, tr("Adding Session"),
140  tr("Please name new session:"), QLineEdit::Normal,
141  "", &ok);
142  if (ok && !text.isEmpty()){
143  emit onAddSession(pItem->index(), text);
144  }
145  });
146 
147  pRemoveAction = new QAction("Remove Subject", this);
148 
149  menu->addAction(pAddSessionAction);
150  menu->addAction(pRemoveAction);
151  menu->popup(m_pUi->m_pTreeView->viewport()->mapToGlobal(pos));
152  break;
153  }
154  case BIDS_SESSION: {
155  QMenu *menu = new QMenu(this);
156 
157  pRemoveAction = new QAction("Remove Session", this);
158 
159  QMenu* pMoveMenu = new QMenu("Move Session to ...");
160 
161  //Find all available subjects
162  for(int i = 0; i < pModel->rowCount(); i++){
163  if(pModel->item(i)->index() != pItem->data(BIDS_ITEM_SUBJECT).value<QModelIndex>()){
164  qDebug() << "Relative model index" << pModel->item(i)->index();
165  qDebug() << "Relative item index" << pItem->data(BIDS_ITEM_SUBJECT).value<QModelIndex>();
166  QAction* pTargetAction = new QAction(pModel->item(i)->text());
167  connect(pTargetAction, &QAction::triggered, [=] () {
168  emit onMoveSession(pModel->item(i)->index(), pItem->index());
169  });
170  pMoveMenu->addAction(pTargetAction);
171  }
172  }
173 
174  if (!pMoveMenu->isEmpty()){
175  menu->addMenu(pMoveMenu);
176  }
177  menu->addAction(pRemoveAction);
178  menu->popup(m_pUi->m_pTreeView->viewport()->mapToGlobal(pos));
179  break;
180  }
181  case BIDS_BEHAVIORALDATA:
182  case BIDS_ANATOMICALDATA:
183  case BIDS_FUNCTIONALDATA: {
184  QMenu *menu = new QMenu(this);
185 
186  pRemoveAction = new QAction("Remove Data", this);
187 
188  QMenu* pMoveMenu = new QMenu("Move Data to ...");
189 
190  //Find all available sessions
191  for(int i = 0; i < pModel->rowCount(); i++){
192  QMenu* pSubjectMenu = new QMenu(pModel->item(i)->text());
193  for (int j = 0; j < pModel->item(i)->rowCount(); j++){
194  if(pModel->item(i)->child(j)->index() != pItem->data(BIDS_ITEM_SESSION).value<QModelIndex>()){
195  QAction* pTargetAction = new QAction(pModel->item(i)->child(j)->text());
196  connect(pTargetAction, &QAction::triggered, [=] {
197  emit onMoveData(pModel->item(i)->child(j)->index(), pItem->index());
198  });
199  pSubjectMenu->addAction(pTargetAction);
200  }
201  }
202  if(!pSubjectMenu->isEmpty()){
203  pMoveMenu->addMenu(pSubjectMenu);
204  }
205  }
206 
207  if (!pMoveMenu->isEmpty()){
208  menu->addMenu(pMoveMenu);
209  }
210  menu->addAction(pRemoveAction);
211  menu->popup(m_pUi->m_pTreeView->viewport()->mapToGlobal(pos));
212  break;
213  }
214  default:{
215  qDebug() << "DataManagerControlView::customMenuRequested - default";
216  QMenu *menu = new QMenu(this);
217 
218  pRemoveAction = new QAction("Remove", this);
219  menu->addAction(pRemoveAction);
220  menu->popup(m_pUi->m_pTreeView->viewport()->mapToGlobal(pos));
221  break;
222  }
223  }
224  connect(pRemoveAction, &QAction::triggered, [=]() {
225  QMessageBox msgBox;
226 
227  msgBox.setText("Are you sure you want to remove " + pItem->text() + "?");
228  msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
229  msgBox.setDefaultButton(QMessageBox::No);
230 
231  int ret = msgBox.exec();
232 
233  if(ret == QMessageBox::Yes) {
234  emit removeItem(m_pUi->m_pTreeView->indexAt(pos));
235  }
236  });
237  } else {
238  QMenu *menu = new QMenu(this);
239 
240  QAction* pAddSubjectAction = new QAction("Add Subject", this);
241  connect(pAddSubjectAction, &QAction::triggered, [=]() {
242  bool ok;
243  QString text = QInputDialog::getText(this, tr("Adding Subject"),
244  tr("Please name new subject:"), QLineEdit::Normal,
245  "", &ok);
246  if (ok && !text.isEmpty()){
247  emit onAddSubject(text);
248  }
249  });
250 
251  menu->addAction(pAddSubjectAction);
252  menu->popup(m_pUi->m_pTreeView->viewport()->mapToGlobal(pos));
253  }
254 }
255 
256 //=============================================================================================================
257 
258 void BidsView::onCurrentItemChanged(const QItemSelection &selected,
259  const QItemSelection &deselected)
260 {
261  Q_UNUSED(deselected)
262  if(selected.indexes().empty()) {
263  return;
264  }
265 
266  emit selectedItemChanged(selected.indexes().first());
267 
268  if(QStandardItemModel *pModel = qobject_cast<QStandardItemModel *>(m_pUi->m_pTreeView->model())) {
269  if(QStandardItem* pItem = pModel->itemFromIndex(selected.indexes().first())) {
270  if(!pItem->data().isNull()) {
271  emit selectedModelChanged(pItem->data());
272  }
273  }
274  }
275 }
276 
277 //=============================================================================================================
278 
279 void BidsView::onNewFileLoaded(int iSubject,
280  int iModel)
281 {
282  m_pUi->m_pTreeView->selectionModel()->select(m_pUi->m_pTreeView->model()->index(iModel, 0, m_pUi->m_pTreeView->model()->index(iSubject, 0)),
283  QItemSelectionModel::ClearAndSelect);
284 }
285 
286 //=============================================================================================================
287 
288 void BidsView::keyPressEvent(QKeyEvent *event)
289 {
290  switch (event->key()) {
291  case Qt::Key_Delete:
292  if(m_pUi->m_pTreeView->model()->data(m_pUi->m_pTreeView->currentIndex(), Qt::ToolTipRole).toString() != "Subject item") {
293  emit removeItem(m_pUi->m_pTreeView->currentIndex());
294  }
295  break;
296 
297  default:
298  QWidget::keyPressEvent(event);
299  }
300 }
301 
302 //=============================================================================================================
303 
304 void BidsView::onNewItemIndex(QModelIndex itemIndex)
305 {
306  m_pUi->m_pTreeView->selectionModel()->select(itemIndex, QItemSelectionModel::ClearAndSelect);
307  m_pUi->m_pTreeView->expand(itemIndex.parent());
308 // m_pUi->m_pTreeView->expandRecursively(itemIndex);
309  m_pUi->m_pTreeView->expandAll();
310 }
311 
312 //=============================================================================================================
313 
315 {
316 
317 }
318 
319 //=============================================================================================================
320 
322 {
323 
324 }
325 
326 //=============================================================================================================
327 
328 void BidsView::updateGuiMode(GuiMode mode)
329 {
330  switch(mode) {
331  case GuiMode::Clinical:
332  break;
333  default: // default is research mode
334  break;
335  }
336 }
337 
338 //=============================================================================================================
339 
340 void BidsView::updateProcessingMode(ProcessingMode mode)
341 {
342  switch(mode) {
343  case ProcessingMode::Offline:
344  break;
345  default: // default is realtime mode
346  break;
347  }
348 }
349 
350 //=============================================================================================================
351 
352 void BidsView::onModelReset()
353 {
354  m_pUi->m_pTreeView->expandAll();
355 }
356 
357 //=============================================================================================================
358 
360 {
361 
362 }
DISPLIB::AbstractView
The AbstractView class provides the base calss for all Disp viewers.
Definition: abstractview.h:75
DISPLIB::BidsView::updateGuiMode
virtual void updateGuiMode(GuiMode mode)
Definition: bidsview.cpp:328
DISPLIB::BidsView::selectedItemChanged
void selectedItemChanged(const QModelIndex &pIndex)
DISPLIB::BidsView::selectedModelChanged
void selectedModelChanged(const QVariant &data)
bidsview.h
Contains the declaration of the BidsView class.
DISPLIB::BidsView::loadSettings
virtual void loadSettings()
Definition: bidsview.cpp:321
DISPLIB::BidsView::clearView
void clearView()
Definition: bidsview.cpp:359
DISPLIB::BidsView::~BidsView
virtual ~BidsView()
Definition: bidsview.cpp:81
DISPLIB::BidsViewModel::addSubject
QModelIndex addSubject(const QString &sSubjectName)
Definition: bidsviewmodel.cpp:134
DISPLIB::BidsViewModel::moveSessionToSubject
QModelIndex moveSessionToSubject(QModelIndex subjectIndex, QModelIndex sessionIndex)
Definition: bidsviewmodel.cpp:299
DISPLIB::BidsView::BidsView
BidsView(QWidget *parent=0)
Definition: bidsview.cpp:69
DISPLIB::BidsView::updateProcessingMode
virtual void updateProcessingMode(ProcessingMode mode)
Definition: bidsview.cpp:340
DISPLIB::BidsView::onMoveSession
void onMoveSession(QModelIndex subjectIndex, QModelIndex sessionIndex)
DISPLIB::BidsView::setModel
void setModel(QAbstractItemModel *pModel)
Definition: bidsview.cpp:88
DISPLIB::BidsViewModel::moveDataToSession
QModelIndex moveDataToSession(QModelIndex sessionIndex, QModelIndex dataIndex)
Definition: bidsviewmodel.cpp:328
bidsviewmodel.h
Contains declaration of BidsViewModel Container class.
DISPLIB::BidsViewModel::newItemIndex
void newItemIndex(QModelIndex itemIndex)
DISPLIB::BidsView::removeItem
void removeItem(const QModelIndex &pIndex)
DISPLIB::BidsViewModel
The BidsViewModel class is the base data container.
Definition: bidsviewmodel.h:104
DISPLIB::BidsView::onAddSession
void onAddSession(QModelIndex subjectIndex, const QString &sSessionName)
DISPLIB::BidsView::onAddSubject
void onAddSubject(const QString &sSubjectName)
DISPLIB::BidsView::saveSettings
virtual void saveSettings()
Definition: bidsview.cpp:314
DISPLIB::BidsView::onMoveData
void onMoveData(QModelIndex sessionIndex, QModelIndex dataIndex)