13 #include <QVBoxLayout>
14 #include <QHBoxLayout>
16 #include <QTableWidget>
17 #include <QHeaderView>
18 #include <QApplication>
19 #include <QDesktopWidget>
41 BaseWidget(parent,
"ImportDataTypeWidget",
"Import"),
42 mImportWidget(parent),
46 mParentCandidates(parentCandidates),
47 mSelectedIndexInTable(0),
48 mImageTypeCombo(NULL),
52 mAnatomicalCoordinateSystems =
new QComboBox();
53 mAnatomicalCoordinateSystems->addItem(
"LPS");
54 mAnatomicalCoordinateSystems->addItem(
"RAS");
56 if(isInputFileInNiftiFormat())
57 mAnatomicalCoordinateSystems->setCurrentText(
"RAS");
59 mShouldImportParentTransform =
new QComboBox();
60 mShouldImportParentTransform->addItem(
"No");
61 mShouldImportParentTransform->addItem(
"Yes");
63 mParentCandidatesCB =
new QComboBox();
65 mShouldConvertDataToUnsigned =
new QCheckBox();
66 mShouldConvertDataToUnsigned->setCheckState(Qt::Unchecked);
68 mTableWidget =
new QTableWidget();
69 mTableWidget->setRowCount(0);
70 mTableWidget->setColumnCount(4);
71 mTableHeader<<
"#"<<
"Type"<<
"Name"<<
"Space";
72 mTableWidget->setHorizontalHeaderLabels(mTableHeader);
73 mTableWidget->horizontalHeader()->setStretchLastSection(
true);
74 mTableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);
75 mTableWidget->verticalHeader()->setVisible(
false);
76 mTableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);
77 mTableWidget->setSelectionBehavior(QAbstractItemView::SelectRows);
78 mTableWidget->setSelectionMode(QAbstractItemView::SingleSelection);
79 mTableWidget->setShowGrid(
false);
80 mTableWidget->setStyleSheet(
"QTableView {selection-background-color: #ACCEF7;}");
81 mTableWidget->setGeometry(QApplication::desktop()->screenGeometry());
84 for(
unsigned i=0; i<mData.size(); ++i)
88 CX_LOG_WARNING() <<
"ImportDataTypeWidget::ImportDataTypeWidget: No data";
91 type = mData[i]->getType();
92 name = mData[i]->getName();
93 QString space = mData[i]->getSpace();
97 space = boost::dynamic_pointer_cast<PointMetric>(mData[i])->getSpace().toString();
98 (mPointMetricGroups[space]).push_back(mData[i]);
103 int newRowIndex = mTableWidget->rowCount();
104 mTableWidget->setRowCount(newRowIndex+1);
105 mTableWidget->setItem(newRowIndex, 0,
new QTableWidgetItem(
"1"));
106 mTableWidget->setItem(newRowIndex, 1,
new QTableWidgetItem(name));
107 mTableWidget->setItem(newRowIndex, 2,
new QTableWidgetItem(type));
108 mTableWidget->setItem(newRowIndex, 3,
new QTableWidgetItem(space));
110 this->createDataSpecificGui(mData[i]);
112 this->addPointMetricGroupsToTable();
115 QVBoxLayout *topLayout =
new QVBoxLayout(
this);
116 this->setLayout(topLayout);
118 QFileInfo fileInfo(filename);
119 QString title = fileInfo.fileName();
121 QGroupBox *groupBox =
new QGroupBox(title);
123 QGridLayout *gridLayout =
new QGridLayout();
124 gridLayout->addWidget(
new QLabel(
"For all data in the file: "), 0, 0, 1, 2);
125 gridLayout->addWidget(
new QLabel(
"Specify anatomical coordinate system"), 1, 0);
126 gridLayout->addWidget(mAnatomicalCoordinateSystems, 1, 1);
127 gridLayout->addWidget(
new QLabel(
"Import parents transform?"), 2, 0);
128 gridLayout->addWidget(mShouldImportParentTransform, 2, 1);
129 gridLayout->addWidget(
new QLabel(
"Set parent"), 3, 0);
130 gridLayout->addWidget(mParentCandidatesCB, 3, 1);
131 gridLayout->addWidget(
new QLabel(
"Convert data to unsigned?"), 4, 0);
132 gridLayout->addWidget(mShouldConvertDataToUnsigned, 4,1);
133 gridLayout->addWidget(mTableWidget, 5, 0, 1, 2);
135 gridLayout->addWidget(mModalityCombo);
137 gridLayout->addWidget(mImageTypeCombo);
139 groupBox->setLayout(gridLayout);
140 topLayout->addWidget(groupBox);
153 void ImportDataTypeWidget::createDataSpecificGui(
DataPtr data)
155 ImagePtr image = boost::dynamic_pointer_cast<Image>(data);
161 mModalityAdapter->setData(image);
165 mImageTypeAdapter->setData(image);
167 if(isInputFileInNiftiFormat())
175 void ImportDataTypeWidget::updateImageType()
179 mImageTypeAdapter->setValue(
enum2string(imageSubType));
182 std::map<QString, QString> ImportDataTypeWidget::getParentCandidateList()
184 std::map<QString, QString> parentCandidates;
185 for(
unsigned i=0; i<mParentCandidates.size(); ++i)
187 parentCandidates[mParentCandidates[i]->getName()] = mParentCandidates[i]->getUid();
190 return parentCandidates;
193 void ImportDataTypeWidget::updateSpaceComboBox(QComboBox *box, QString pointMetricGroupId)
196 std::map<QString, QString> parentCandidates = this->getParentCandidateList();
197 std::map<QString, QString>::iterator it;
198 for(it= parentCandidates.begin(); it != parentCandidates.end(); ++it)
200 QVariant id(it->second);
201 box->addItem(it->first,
id);
203 std::vector<DataPtr> pointMetricGroup = mPointMetricGroups[pointMetricGroupId];
204 QString currentSpace = pointMetricGroup[0]->getSpace();
205 box->setCurrentText(currentSpace);
208 void ImportDataTypeWidget::updateParentCandidatesComboBox()
211 QString selectedParentId = (mParentCandidatesCB->itemData(mParentCandidatesCB->currentIndex()).toString());
213 mParentCandidatesCB->clear();
214 std::map<QString, QString> parentCandidates = this->getParentCandidateList();
215 std::map<QString, QString>::iterator it;
216 QVariant emptyId(
"");
217 mParentCandidatesCB->addItem(
"", emptyId);
218 int selectedIndex = 0;
219 for(it= parentCandidates.begin(); it != parentCandidates.end(); ++it)
221 QString idString = it->second;
222 QVariant id(idString);
223 mParentCandidatesCB->addItem(it->first,
id);
224 if(selectedParentId.compare(idString, Qt::CaseInsensitive) == 0)
225 selectedIndex = mParentCandidatesCB->count()-1;
228 if(selectedIndex != 0)
229 mParentCandidatesCB->setCurrentIndex(selectedIndex);
233 QString parentGuess = this->getInitialGuessForParentFrame();
235 mParentCandidatesCB->setCurrentText(parentGuess);
241 void ImportDataTypeWidget::importAllData()
243 for(
unsigned i=0; i<mData.size(); ++i)
247 QString parentId = (mParentCandidatesCB->itemData(mParentCandidatesCB->currentIndex()).toString());
248 mData[i]->get_rMd_History()->setParentSpace(parentId);
250 mServices->patient()->insertData(mData[i]);
251 mServices->view()->autoShowData(mData[i]);
256 void ImportDataTypeWidget::applyParentTransformImport()
260 QString parentId = (mParentCandidatesCB->itemData(mParentCandidatesCB->currentIndex()).toString());
261 DataPtr parent = mServices->patient()->getData(parentId);
265 CX_LOG_ERROR() <<
"Could not find parent data with uid: " << parentId;
269 std::vector<DataPtr>::iterator it = mData.begin();
270 for(;it!=mData.end(); ++it)
273 data->get_rMd_History()->setRegistration(parent->get_rMd());
274 report(
"Assigned rMd from data [" + parent->getName() +
"] to data [" + data->getName() +
"]");
290 void ImportDataTypeWidget::applyConversionLPS()
294 std::vector<DataPtr>::iterator it = mData.begin();
295 for(;it!=mData.end(); ++it)
301 data->get_rMd_History()->setRegistration(rMd);
302 report(
"Nifti import: Converted data " + data->getName() +
" from LPS to RAS coordinates.");
320 void ImportDataTypeWidget::applyConversionToUnsigned()
324 std::vector<DataPtr>::iterator it = mData.begin();
325 for(;it!=mData.end(); ++it)
329 ImagePtr image = boost::dynamic_pointer_cast<Image>(data);
335 image->setVtkImageData(converted->getBaseVtkImageData());
337 ImageTF3DPtr TF3D = converted->getTransferFunctions3D()->createCopy();
338 ImageLUT2DPtr LUT2D = converted->getLookupTable2D()->createCopy();
339 image->setLookupTable2D(LUT2D);
340 image->setTransferFunctions3D(TF3D);
343 DataPtr convertedData = boost::dynamic_pointer_cast<Data>(image);
344 (*it) = convertedData;
369 this->updateParentCandidatesComboBox();
371 std::map<QString, QComboBox *>::iterator it;
372 for(it=mSpaceCBs.begin(); it != mSpaceCBs.end(); ++it)
374 QString
id = it->first;
375 QComboBox *box = it->second;
376 this->updateSpaceComboBox(box,
id);
382 if(mShouldConvertDataToUnsigned->isChecked())
383 this->applyConversionToUnsigned();
385 this->importAllData();
387 if(mShouldImportParentTransform->currentText() ==
"Yes")
388 this->applyParentTransformImport();
389 if(mAnatomicalCoordinateSystems->currentText() !=
"LPS")
390 this->applyConversionLPS();
393 void ImportDataTypeWidget::showEvent(QShowEvent *event)
399 void ImportDataTypeWidget::pointMetricGroupSpaceChanged(
int index)
401 QComboBox *box = qobject_cast<QComboBox*>(QObject::sender());
402 QString newSpace = box->currentData().toString();
404 QString pointMetricsGroupId;
405 std::map<QString, QComboBox *>::iterator it;
406 for(it = mSpaceCBs.begin(); it != mSpaceCBs.end(); ++it)
408 if(it->second == box)
409 pointMetricsGroupId = it->first;
411 std::vector<DataPtr> pointMetricGroup = mPointMetricGroups[pointMetricsGroupId];
412 for(
unsigned i=0; i<pointMetricGroup.size(); ++i)
414 CoordinateSystem cs(
csDATA, newSpace);
415 boost::dynamic_pointer_cast<PointMetric>(pointMetricGroup[i])->setSpace(cs);
419 QString ImportDataTypeWidget::getInitialGuessForParentFrame()
421 int candidateScore = 0;
422 QString bestCandidate;
424 for(
unsigned i=0; i < mData.size(); ++i)
427 std::map<QString, QString> parentCandidates = this->getParentCandidateList();
429 std::map<QString, QString>::iterator iter;
430 for(iter= parentCandidates.begin(); iter != parentCandidates.end(); ++iter)
432 int similarity = similatiryMeasure(data->getUid(), iter->second);
433 if(similarity > candidateScore && !isSegmentation(iter->second))
435 candidateScore = similarity;
436 bestCandidate = iter->first;
441 return bestCandidate;
444 int ImportDataTypeWidget::similatiryMeasure(QString current, QString candidate)
446 QStringList currentList = splitStringIntoSeparateParts(current);
447 QStringList candidateList = splitStringIntoSeparateParts(candidate);
448 return countEqualListElements(currentList, candidateList);
451 QStringList ImportDataTypeWidget::splitStringIntoSeparateParts(QString current)
453 current = removeParenthesis(current);
455 QStringList list = current.split(
".", QString::SkipEmptyParts);
457 for (
int i = 0; i < list.size(); ++i)
459 list2 << list[i].split(
"_", QString::SkipEmptyParts);
461 QStringList currentParts;
462 for (
int i = 0; i < list2.size(); ++i)
464 currentParts << list2[i].split(
"-", QString::SkipEmptyParts);
469 QString ImportDataTypeWidget::removeParenthesis(QString current)
471 int startParenthesis;
474 startParenthesis = current.indexOf(
"{");
475 int endParenthesis = current.indexOf(
"}");
476 current.replace(startParenthesis, endParenthesis-startParenthesis+1,
"");
477 }
while (startParenthesis != -1);
481 int ImportDataTypeWidget::countEqualListElements(QStringList first, QStringList second)
484 int numComparedElements = 0;
485 for (
int i = 0; i < first.size(); ++i)
487 if(excludeElement(first[i]))
489 ++numComparedElements;
490 for (
int j = 0; j < second.size(); ++j)
492 if(first[i].compare(second[j]) == 0)
500 if (retval == numComparedElements)
505 bool ImportDataTypeWidget::excludeElement(QString element)
507 if (isSegmentation(element))
512 void ImportDataTypeWidget::addPointMetricGroupsToTable()
516 std::map<QString, std::vector<DataPtr> >::iterator it = mPointMetricGroups.begin();
517 for(; it != mPointMetricGroups.end(); ++it)
521 QString space = it->first;
522 std::vector<DataPtr> datas = it->second;
524 if(datas.empty() || !data)
529 QComboBox *spaceCB =
new QComboBox();
530 mSpaceCBs[space] = spaceCB;
531 connect(spaceCB, SIGNAL(currentIndexChanged(
int)),
this, SLOT(pointMetricGroupSpaceChanged(
int)));
532 this->updateSpaceComboBox(spaceCB, space);
534 type = data->getType();
535 name =
"Point metric group "+QString::number(groupnr);
537 int newRowIndex = mTableWidget->rowCount();
538 mTableWidget->setRowCount(newRowIndex+1);
539 mTableWidget->setItem(newRowIndex, 0,
new QTableWidgetItem(QString::number(datas.size())));
540 mTableWidget->setItem(newRowIndex, 1,
new QTableWidgetItem(name));
541 mTableWidget->setItem(newRowIndex, 2,
new QTableWidgetItem(type));
542 mTableWidget->setCellWidget(newRowIndex, 3, spaceCB);
546 bool ImportDataTypeWidget::isInputFileInNiftiFormat()
548 if(mFilename.endsWith(
".nii", Qt::CaseInsensitive))
553 bool ImportDataTypeWidget::isSegmentation(QString filename)
555 if(filename.contains(
"label", Qt::CaseInsensitive))
557 if(filename.contains(
"seg", Qt::CaseInsensitive))