42 #include "ui_channelselectionview.h"
59 #include <QFileDialog>
60 #include <QListWidgetItem>
62 #include <QApplication>
73 using namespace DISPLIB;
74 using namespace FIFFLIB;
75 using namespace UTILSLIB;
86 , m_pUi(new Ui::ChannelSelectionViewWidget)
87 , m_pChannelInfoModel(pChannelInfoModel)
90 m_sSettingsPath = sSettingsPath;
95 initSelectionSceneView();
118 void ChannelSelectionView::initListWidgets()
121 m_pUi->m_listWidget_userDefined->installEventFilter(
this);
122 m_pUi->m_listWidget_selectionGroups->installEventFilter(
this);
125 connect(m_pUi->m_listWidget_selectionGroups, &QListWidget::currentItemChanged,
126 this, &ChannelSelectionView::updateSelectionGroupsList);
130 connect(m_pUi->m_listWidget_userDefined->model(), &QAbstractTableModel::dataChanged,
136 void ChannelSelectionView::initSelectionSceneView()
139 m_pSelectionScene =
new SelectionScene(m_pUi->m_graphicsView_layoutPlot);
140 m_pUi->m_graphicsView_layoutPlot->setScene(m_pSelectionScene);
142 connect(m_pSelectionScene, &QGraphicsScene::selectionChanged,
143 this, &ChannelSelectionView::updateUserDefinedChannelsList);
148 void ChannelSelectionView::initComboBoxes()
150 m_pUi->m_comboBox_layoutFile->clear();
151 m_pUi->m_comboBox_layoutFile->insertItems(0, QStringList()
152 <<
"babymeg-mag-inner-layer.lout"
153 <<
"babymeg-mag-outer-layer.lout"
155 <<
"Vectorview-grad.lout"
156 <<
"Vectorview-all.lout"
157 <<
"Vectorview-mag.lout"
158 <<
"standard_waveguard64_duke.lout"
163 connect(m_pUi->m_comboBox_layoutFile, &QComboBox::currentTextChanged,
164 this, &ChannelSelectionView::onComboBoxLayoutChanged);
167 QString selectionName(
"Vectorview-all.lout");
172 selectionName = QString(
"mne_browse_raw_vv.sel");
173 loadSelectionGroups(QCoreApplication::applicationDirPath() + selectionName.prepend(
"/../resources/general/selectionGroups/"));
178 void ChannelSelectionView::initButtons()
180 connect(m_pUi->m_pushButton_saveSelection, &QPushButton::clicked,
181 this, &ChannelSelectionView::onBtnSaveUserSelection);
183 connect(m_pUi->m_pushButton_loadSelection, &QPushButton::clicked,
184 this, &ChannelSelectionView::onBtnLoadUserSelection);
186 connect(m_pUi->m_pushButton_addToSelectionGroups, &QPushButton::clicked,
187 this, &ChannelSelectionView::onBtnAddToSelectionGroups);
192 void ChannelSelectionView::initCheckBoxes()
194 connect(m_pUi->m_checkBox_showBadChannelsAsRed, &QCheckBox::clicked,
202 m_currentlyLoadedFiffChannels = mappedLayoutChNames;
205 m_pUi->m_listWidget_visibleChannels->clear();
208 m_pUi->m_listWidget_selectionGroups->clear();
211 auto it = m_selectionGroupsMap.find(
"All");
212 if (it != m_selectionGroupsMap.end()) {
213 m_selectionGroupsMap.erase(it);
215 m_selectionGroupsMap.insert(
"All", m_currentlyLoadedFiffChannels);
218 for(
auto i = m_selectionGroupsMap.constBegin(); i != m_selectionGroupsMap.constEnd(); i++) {
219 m_pUi->m_listWidget_selectionGroups->insertItem(m_pUi->m_listWidget_selectionGroups->count(), i.key());
223 m_pUi->m_listWidget_selectionGroups->setCurrentItem(
getItemForChName(m_pUi->m_listWidget_selectionGroups,
"All"), QItemSelectionModel::Select);
226 updateSelectionGroupsList(
getItemForChName(m_pUi->m_listWidget_selectionGroups,
"All"),
new QListWidgetItem());
233 QStringList channelList;
234 for(
int i = 0; i < channelIndexList.size(); i++) {
235 QModelIndex nameIndex = m_pChannelInfoModel->index(channelIndexList.at(i).row(),3);
236 channelList<<m_pChannelInfoModel->data(nameIndex, ChannelInfoModelRoles::GetMappedLayoutChName).toString();
239 QList<QGraphicsItem *> allSceneItems = m_pSelectionScene->items();
241 for(
int i = 0; i < allSceneItems.size(); i++) {
249 m_pSelectionScene->update();
256 QList<QGraphicsItem *> allSceneItems = m_pSelectionScene->items();
258 for(
int i = 0; i < allSceneItems.size(); i++) {
261 item->setSelected(
true);
263 item->setSelected(
false);
266 m_pSelectionScene->update();
274 QListWidget* targetListWidget;
275 if(m_pUi->m_listWidget_userDefined->count()>0)
276 targetListWidget = m_pUi->m_listWidget_userDefined;
278 targetListWidget = m_pUi->m_listWidget_visibleChannels;
281 QStringList selectedChannels;
283 for(
int i = 0; i < targetListWidget->count(); i++) {
284 QListWidgetItem* item = targetListWidget->item(i);
285 selectedChannels << item->text();
288 return selectedChannels;
294 const QString &channelName)
296 for(
int i=0; i < listWidget->count(); i++)
297 if(listWidget->item(i)->text() == channelName)
298 return listWidget->item(i);
300 return new QListWidgetItem();
316 loadLayout(m_pUi->m_comboBox_layoutFile->currentText());
323 return m_pUi->m_comboBox_layoutFile->currentText();
330 return m_pUi->m_lineEdit_loadedFile->text();
337 qDebug() <<
"setCurrentLayoutFile:" << currentLayoutFile;
338 m_pUi->m_comboBox_layoutFile->setCurrentText(currentLayoutFile);
347 QStringList badChannelMappedNames;
348 QStringList badChannelList = m_pChannelInfoModel->getBadChannelList();
350 if(m_pUi->m_checkBox_showBadChannelsAsRed->isChecked()) {
351 for(
int i = 0; i < m_pChannelInfoModel->rowCount(); i++) {
352 QModelIndex digIndex = m_pChannelInfoModel->index(i,3);
353 QString mappedChName = m_pChannelInfoModel->data(digIndex,ChannelInfoModelRoles::GetMappedLayoutChName).toString();
355 digIndex = m_pChannelInfoModel->index(i,1);
356 QString origChName = m_pChannelInfoModel->data(digIndex,ChannelInfoModelRoles::GetOrigChName).toString();
358 if(badChannelList.contains(origChName)) {
359 badChannelMappedNames << mappedChName;
364 m_pSelectionScene->
repaintItems(m_layoutMap, badChannelMappedNames);
365 m_pSelectionScene->update();
376 QListWidget* targetListWidget;
377 if(m_pUi->m_listWidget_userDefined->count()>0)
378 targetListWidget = m_pUi->m_listWidget_userDefined;
380 targetListWidget = m_pUi->m_listWidget_visibleChannels;
383 QStringList selectedChannels;
385 for(
int i = 0; i < targetListWidget->count(); i++) {
386 QListWidgetItem* item = targetListWidget->item(i);
387 int indexTemp = m_pChannelInfoModel->getIndexFromMappedChName(item->text());
389 if(indexTemp != -1) {
390 QModelIndex mappedNameIndex = m_pChannelInfoModel->index(indexTemp,1);
391 QString origChName = m_pChannelInfoModel->data(mappedNameIndex,ChannelInfoModelRoles::GetOrigChName).toString();
393 selectedChannels << origChName;
396 selectedChannels << item->text();
402 if(!m_pSelectionScene->selectedItems().empty()) {
406 QList<QGraphicsItem*> visibleItemList = m_pSelectionScene->items();
407 QMutableListIterator<QGraphicsItem*> i(visibleItemList);
408 while (i.hasNext()) {
409 if(!i.next()->isVisible()){
421 if(m_sSettingsPath.isEmpty()) {
425 QSettings settings(
"MNECPP");
429 settings.setValue(m_sSettingsPath + QString(
"/ChannelSelectionView/selectedLayoutFile"),
getCurrentLayoutFile());
430 settings.setValue(m_sSettingsPath + QString(
"/ChannelSelectionView/channelSelectionViewPos"), this->pos());
431 settings.setValue(m_sSettingsPath + QString(
"/ChannelSelectionView/selectedGroupFile"),
getCurrentGroupFile());
438 if(m_sSettingsPath.isEmpty()) {
442 QSettings settings(
"MNECPP");
444 setCurrentLayoutFile(settings.value(m_sSettingsPath + QString(
"/ChannelSelectionView/selectedLayoutFile"),
"Vectorview-all.lout").toString());
445 loadSelectionGroups(QCoreApplication::applicationDirPath() + settings.value(m_sSettingsPath + QString(
"/ChannelSelectionView/selectedGroupFile"),
"mne_browse_raw_vv.sel").toString().prepend(
"/../resources/general/selectionGroups/"));
449 QPoint pos = settings.value(m_sSettingsPath + QString(
"/ChannelSelectionView/channelSelectionViewPos"), QPoint(100,100)).toPoint();
451 QList<QScreen*> screensList = QGuiApplication::screens();
452 if(screensList.isEmpty())
454 move(QPoint(100,100));
465 case GuiMode::Clinical:
477 case ProcessingMode::Offline:
486 bool ChannelSelectionView::loadLayout(QString path)
488 qDebug() <<
"loadLayout:" << path;
489 bool state = LayoutLoader::readMNELoutFile(path, m_layoutMap);
492 QList<QVector<float> > inputPoints;
493 QList<QVector<float> > outputPoints;
495 QFile out(
"manualLayout.lout");
497 for(
int i = 0; i < m_pChannelInfoModel->rowCount(); i++) {
498 QModelIndex digIndex = m_pChannelInfoModel->index(i,1);
499 QString chName = m_pChannelInfoModel->data(digIndex,ChannelInfoModelRoles::GetOrigChName).toString();
501 digIndex = m_pChannelInfoModel->index(i,8);
502 QVector3D channelDig = m_pChannelInfoModel->data(digIndex,ChannelInfoModelRoles::GetChDigitizer).value<QVector3D>();
504 digIndex = m_pChannelInfoModel->index(i,4);
505 int kind = m_pChannelInfoModel->data(digIndex,ChannelInfoModelRoles::GetChKind).toInt();
507 if(kind == FIFFV_EEG_CH) {
509 temp.append(channelDig.x());
510 temp.append(channelDig.y());
511 temp.append(-channelDig.z());
512 inputPoints.append(temp);
523 if(inputPoints.size() > 0) {
524 while(numberTries < 10) {
525 if(!LayoutMaker::makeLayout(inputPoints,
544 for(
int i = 0; i < outputPoints.size(); i++) {
545 if(!m_layoutMap.contains(names.at(i))) {
546 m_layoutMap[names.at(i)] = QPointF(outputPoints.at(i)[0],outputPoints.at(i)[1]);
552 m_pSelectionScene->update();
556 m_pUi->m_graphicsView_layoutPlot->fitInView(m_pSelectionScene->itemsBoundingRect(), Qt::KeepAspectRatio);
570 bool ChannelSelectionView::loadSelectionGroups(QString path)
574 m_pUi->m_listWidget_visibleChannels->clear();
577 m_pUi->m_listWidget_selectionGroups->clear();
580 QString newPath = path;
582 m_selectionGroupsMap.clear();
585 if(!path.isEmpty()) {
586 if(path.contains(
".sel"))
587 state = SelectionIO::readMNESelFile(newPath, m_selectionGroupsMap);
588 if(path.contains(
".mon"))
589 state = SelectionIO::readBrainstormMonFile(newPath, m_selectionGroupsMap);
593 auto it = m_selectionGroupsMap.find(
"All");
594 if (it != m_selectionGroupsMap.end()) {
595 m_selectionGroupsMap.erase(it);
597 m_selectionGroupsMap.insert(
"All", m_currentlyLoadedFiffChannels);
600 for(
int i = 0; i < m_pChannelInfoModel->rowCount(); i++) {
601 QModelIndex digIndex = m_pChannelInfoModel->index(i,1);
602 QString chName = m_pChannelInfoModel->data(digIndex,ChannelInfoModelRoles::GetOrigChName).toString();
604 digIndex = m_pChannelInfoModel->index(i,4);
605 int kind = m_pChannelInfoModel->data(digIndex,ChannelInfoModelRoles::GetChKind).toInt();
607 if(kind == FIFFV_EEG_CH)
612 it = m_selectionGroupsMap.find(
"All EEG");
613 if (it != m_selectionGroupsMap.end()) {
614 m_selectionGroupsMap.erase(it);
616 m_selectionGroupsMap.insert(
"All EEG", names);
620 for(
auto i = m_selectionGroupsMap.constBegin(); i != m_selectionGroupsMap.constEnd(); i++) {
621 m_pUi->m_listWidget_selectionGroups->insertItem(m_pUi->m_listWidget_selectionGroups->count(), i.key());
625 updateSelectionGroupsList(
getItemForChName(m_pUi->m_listWidget_selectionGroups,
"All"),
new QListWidgetItem());
628 m_pUi->m_listWidget_selectionGroups->setCurrentItem(
getItemForChName(m_pUi->m_listWidget_selectionGroups,
"All"), QItemSelectionModel::Select);
634 m_pUi->m_lineEdit_loadedFile->setText(QFileInfo(path).fileName());
645 void ChannelSelectionView::cleanUpMEGChannels()
648 for(
auto i = m_selectionGroupsMap.constBegin(); i != m_selectionGroupsMap.constEnd(); i++) {
649 QStringList channelList = i.value();
652 QMutableStringListIterator stringListIndex(channelList);
653 while (stringListIndex.hasNext()) {
654 stringListIndex.next();
656 if(!m_layoutMap.contains(stringListIndex.value()) && stringListIndex.value().contains(
"MEG"))
657 stringListIndex.remove();
661 m_selectionGroupsMap.insert(i.key(), channelList);
667 void ChannelSelectionView::updateSelectionGroupsList(QListWidgetItem* current, QListWidgetItem* previous)
674 if(current->text().contains(
"EEG"))
680 m_pUi->m_listWidget_visibleChannels->clear();
681 auto items = m_selectionGroupsMap.find(current->text());
682 m_pUi->m_listWidget_visibleChannels->addItems(*items);
695 void ChannelSelectionView::updateSceneItems()
697 QStringList visibleItems;
699 for(
int i = 0; i < m_pUi->m_listWidget_visibleChannels->count(); i++)
700 visibleItems << m_pUi->m_listWidget_visibleChannels->item(i)->text();
702 m_pSelectionScene->
hideItems(visibleItems);
707 void ChannelSelectionView::updateUserDefinedChannelsList()
709 QList<QGraphicsItem*> itemList = m_pSelectionScene->selectedItems();
710 QStringList userDefinedChannels;
712 for(
int i = 0; i < itemList.size(); i++) {
717 m_pUi->m_listWidget_userDefined->clear();
718 m_pUi->m_listWidget_userDefined->addItems(userDefinedChannels);
725 void ChannelSelectionView::onBtnLoadUserSelection()
727 QString path = QFileDialog::getOpenFileName(
this,
728 QString(
"Open selection file"),
729 QString(
"../resources/general/selectionGroups/"),
730 tr(
"Selection files (*.sel *.mon)"));
735 loadSelectionGroups(path);
736 m_pUi->m_lineEdit_loadedFile->setText(QFileInfo(path).fileName());
741 void ChannelSelectionView::onBtnSaveUserSelection()
744 QString path = QFileDialog::getSaveFileName(
this,
745 "Save user channel selection",
746 QString(
"../resources/general/selectionGroups/%1_%2_%3_UserSelection").arg(date.currentDate().year()).arg(date.currentDate().month()).arg(date.currentDate().day()),
747 tr(
"MNE selection file(*.sel);; Brainstorm montage file(*.mon)"));
749 QMultiMap<QString, QStringList> tempMap = m_selectionGroupsMap;
750 tempMap.remove(
"All");
751 tempMap.remove(
"All EEG");
753 if(!path.isEmpty()) {
754 if(path.contains(
".sel"))
755 SelectionIO::writeMNESelFile(path, tempMap);
756 if(path.contains(
".mon"))
757 SelectionIO::writeBrainstormMonFiles(path, tempMap);
763 void ChannelSelectionView::onBtnAddToSelectionGroups()
766 for(
int i = 0; i < m_pUi->m_listWidget_userDefined->count(); i++)
767 temp<<m_pUi->m_listWidget_userDefined->item(i)->text();
769 m_selectionGroupsMap.insert(m_pUi->m_lineEdit_selectionGroupName->text(), temp);
770 m_pUi->m_listWidget_selectionGroups->insertItem(m_pUi->m_listWidget_selectionGroups->count(), m_pUi->m_lineEdit_selectionGroupName->text());
775 void ChannelSelectionView::onComboBoxLayoutChanged()
777 QString selectionName(m_pUi->m_comboBox_layoutFile->currentText());
778 loadLayout(QCoreApplication::applicationDirPath() + selectionName.prepend(
"/../resources/general/2DLayouts/"));
784 void ChannelSelectionView::resizeEvent(QResizeEvent* event)
794 bool ChannelSelectionView::eventFilter(QObject *obj, QEvent *event)
797 if (obj == m_pUi->m_listWidget_userDefined && event->type() == QEvent::KeyRelease) {
798 QKeyEvent *keyEvent =
static_cast<QKeyEvent*
>(event);
800 if(keyEvent->key() == Qt::Key_Delete) {
801 qDeleteAll(m_pUi->m_listWidget_userDefined->selectedItems());
809 if (obj == m_pUi->m_listWidget_selectionGroups && event->type() == QEvent::KeyRelease) {
810 QKeyEvent *keyEvent =
static_cast<QKeyEvent*
>(event);
812 if(keyEvent->key() == Qt::Key_Delete) {
813 QList<QListWidgetItem *> tempSelectedList;
815 for(
int i = 0; i < m_pUi->m_listWidget_selectionGroups->selectedItems().size(); i++) {
816 if(m_pUi->m_listWidget_selectionGroups->selectedItems().at(i)->text() !=
"All" &&
817 m_pUi->m_listWidget_selectionGroups->selectedItems().at(i)->text() !=
"All EEG") {
818 tempSelectedList.append(m_pUi->m_listWidget_selectionGroups->selectedItems().at(i));
819 m_selectionGroupsMap.remove(m_pUi->m_listWidget_selectionGroups->selectedItems().at(i)->text());
823 qDeleteAll(tempSelectedList);
833 return QWidget::eventFilter(obj, event);
840 return m_pUi->viewWidget;
847 return m_pUi->controlWidget;
854 return ((m_pUi->m_listWidget_selectionGroups->currentItem()->text() ==
"All") && (m_pUi->m_listWidget_userDefined->count() == 0));
861 m_pSelectionScene->clear();