14 #include <QTreeWidgetItem>
16 #include <QFileDialog>
17 #include <QPushButton>
19 #include <QTableWidget>
21 #include <QHeaderView>
22 #include <QApplication>
23 #include <QDesktopWidget>
24 #include <QStackedWidget>
25 #include <QProgressDialog>
50 mImportDataTypeWidget(widget),
53 this->setAttribute(Qt::WA_DeleteOnClose);
55 this->setWindowTitle(
"Select data for import (Click on a line)");
56 QVBoxLayout* layout =
new QVBoxLayout(
this);
59 layout->addWidget(tableWidget, 1);
60 connect(tableWidget, &QTableWidget::currentCellChanged,
this, &SimpleImportDataDialog::tableItemSelected);
62 QPushButton* cancelButton =
new QPushButton(
"Cancel",
this);
64 QHBoxLayout* buttonLayout =
new QHBoxLayout();
65 buttonLayout->addWidget(cancelButton);
66 layout->addLayout(buttonLayout);
68 connect(cancelButton, &QPushButton::clicked,
this, &SimpleImportDataDialog::cancelClicked);
69 cancelButton->setFocus();
72 void SimpleImportDataDialog::tableItemSelected(
int currentRow,
int currentColumn,
int previousRow,
int previousColumn)
74 int filenameColoumn = 1;
75 QString fullfilename = mImportDataTypeWidget->
getSimpleTableWidget()->item(currentRow, filenameColoumn)->text();
76 std::vector<DataPtr> datas = mImportDataTypeWidget->
getDatas();
78 for (std::vector<DataPtr>::iterator it = datas.begin(); it != datas.end(); ++it)
80 if((*it)->getName() == fullfilename)
85 datas = std::vector<DataPtr>();
86 datas.push_back(selected);
87 mImportDataTypeWidget->
setData(datas);
92 void SimpleImportDataDialog::cancelClicked()
94 mImportDataTypeWidget->
setData(std::vector<DataPtr>());
100 mSelectedIndexInTable(0),
101 mFileManager(filemanager),
102 mVisServices(services)
105 mTableWidget =
new QTableWidget();
106 mTableWidget->setRowCount(0);
107 mTableWidget->setColumnCount(3);
108 mTableHeader<<
""<<
"Status"<<
"Filename";
109 mTableWidget->setHorizontalHeaderLabels(mTableHeader);
110 mTableWidget->horizontalHeader()->setStretchLastSection(
true);
111 mTableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);
112 mTableWidget->verticalHeader()->setVisible(
false);
113 mTableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);
114 mTableWidget->setSelectionBehavior(QAbstractItemView::SelectRows);
115 mTableWidget->setSelectionMode(QAbstractItemView::SingleSelection);
116 mTableWidget->setShowGrid(
false);
117 mTableWidget->setStyleSheet(
"QTableView {selection-background-color: #ACCEF7;}");
118 mTableWidget->setGeometry(QApplication::desktop()->screenGeometry());
119 connect(mTableWidget, &QTableWidget::currentCellChanged,
this, &ImportWidget::tableItemSelected);
121 mStackedWidget =
new QStackedWidget;
123 mTopLayout =
new QVBoxLayout();
124 this->setLayout(mTopLayout);
125 QPushButton *addMoreFilesButton =
new QPushButton(
"Add more files...");
126 QHBoxLayout *buttonLayout =
new QHBoxLayout();
127 QPushButton *importButton =
new QPushButton(
"Import");
128 QPushButton *cancelButton =
new QPushButton(
"Cancel");
129 buttonLayout->addWidget(importButton);
130 buttonLayout->addWidget(cancelButton);
131 buttonLayout->addWidget(addMoreFilesButton);
132 buttonLayout->addStretch();
133 mTopLayout->addLayout(buttonLayout);
134 mTopLayout->addWidget(
new QLabel(
"Supports: "+this->generateFileTypeFilter()));
135 mTopLayout->addWidget(mTableWidget);
136 mTopLayout->addWidget(mStackedWidget);
137 mTopLayout->addStretch();
139 connect(addMoreFilesButton, &QPushButton::clicked,
this, &ImportWidget::addMoreFilesButtonClicked);
140 connect(importButton, &QPushButton::clicked,
this, &ImportWidget::importButtonClicked);
141 connect(cancelButton, &QPushButton::clicked,
this, &ImportWidget::cancelButtonClicked);
144 QAction* addMoreFilesButtonClickedAction =
new QAction(
"AddMoreFilesButtonClickedAction",
this);
145 this->addAction(addMoreFilesButtonClickedAction);
146 connect(addMoreFilesButtonClickedAction, &QAction::triggered,
this, &ImportWidget::addMoreFilesButtonClicked);
148 QAction* addFilesForImportWithDialogAction =
new QAction(
"AddFilesForImportWithDialogAction",
this);
149 this->addAction(addFilesForImportWithDialogAction);
150 connect(addFilesForImportWithDialogAction, &QAction::triggered,
this, &ImportWidget::addFilesForImportWithDialogTriggerend);
152 QAction* importButtonClickedAction =
new QAction(
"ImportButtonClickedAction",
this);
153 this->addAction(importButtonClickedAction);
154 connect(importButtonClickedAction, &QAction::triggered,
this, &ImportWidget::importButtonClicked);
157 int ImportWidget::insertDataIntoTable(QString fullfilename, std::vector<DataPtr> data)
159 int newRowIndex = mTableWidget->rowCount();
160 mTableWidget->setRowCount(newRowIndex+1);
161 mTableWidget->selectRow(mSelectedIndexInTable);
163 bool allDataOk =
true;
164 for(
unsigned i=0; i<data.size(); ++i)
169 status = allDataOk ?
"ok" :
"error";
170 QFileInfo fileInfo(fullfilename);
171 QString filename = fileInfo.fileName();
173 QIcon trashcan(
":/icons/open_icon_library/edit-delete-2.png");
174 QPushButton *removeButton =
new QPushButton(trashcan,
"");
175 connect(removeButton, &QPushButton::clicked,
this, &ImportWidget::removeRowFromTableAndRemoveFilenameFromImportList);
177 mTableWidget->setCellWidget(newRowIndex, 0, removeButton);
178 mTableWidget->setItem(newRowIndex, 1,
new QTableWidgetItem(status));
179 QTableWidgetItem *filenameItem =
new QTableWidgetItem(filename);
180 filenameItem->setData(Qt::ToolTipRole, fullfilename);
181 mTableWidget->setItem(newRowIndex, 2, filenameItem);
186 void ImportWidget::addFilesForImportWithDialogTriggerend()
188 ImportDataTypeWidget* widget = this->addMoreFilesButtonClicked();
189 if(widget && widget->getDatas().size() > 0)
191 SimpleImportDataDialog* dialog =
new SimpleImportDataDialog(widget,
this);
196 ImportDataTypeWidget* ImportWidget::addMoreFilesButtonClicked()
198 QStringList filenames = this->openFileBrowserForSelectingFiles();
200 bool addedDICOM =
false;
202 ImportDataTypeWidget *widget = NULL;
203 for(
int i = 0; i < filenames.size(); ++i)
205 QString filename = filenames[i];
206 QString fileType = mFileManager->getFileReaderName(filename);
207 if(fileType ==
"DICOMReader")
215 CX_LOG_WARNING() <<
"Import of multiple DICOM series at once is not supported. Skipping series based on file: " << filename;
221 mFileNames.push_back(filename);
223 std::vector<DataPtr> newData = mFileManager->read(filename);
224 if(newData.size() > 0)
226 int index = this->insertDataIntoTable(filename, newData);
227 widget =
new ImportDataTypeWidget(
this, mVisServices, newData, mParentCandidates, filename);
228 mStackedWidget->insertWidget(index, widget);
229 mNotImportedData.insert(mNotImportedData.end(), newData.begin(), newData.end());
233 this->generateParentCandidates();
237 void ImportWidget::showProgressDialog(QProgressDialog &progress)
239 progress.setWindowModality(Qt::WindowModal);
240 progress.setMinimumDuration(0);
243 for(
int i = 0; i < 100; ++i)
245 progress.setValue(i);
250 void ImportWidget::importButtonClicked()
256 mVisServices->session()->save();
259 void ImportWidget::cancelButtonClicked()
265 void ImportWidget::clearData()
267 mParentCandidates.clear();
268 mNotImportedData.clear();
271 void ImportWidget::removeWidget(QWidget *widget)
273 mTopLayout->removeWidget(widget);
274 widget->deleteLater();
277 void ImportWidget::removeRowFromTableAndRemoveFilenameFromImportList()
279 QPushButton *button = qobject_cast<QPushButton*>(QObject::sender());
281 int filenamecoloumn = 2;
282 QString fullfilename = mTableWidget->item(rowindex, filenamecoloumn)->data(Qt::ToolTipRole).toString();
284 mTableWidget->removeRow(rowindex);
285 int numberOfRemovedEntries = mFileNames.removeAll(fullfilename);
287 QWidget *widgetToRemove = mStackedWidget->widget(rowindex);
288 mStackedWidget->removeWidget(widgetToRemove);
291 void ImportWidget::tableItemSelected(
int currentRow,
int currentColumn,
int previousRow,
int previousColumn)
293 if(currentRow == mSelectedIndexInTable)
296 mStackedWidget->setCurrentIndex(currentRow);
297 mSelectedIndexInTable = currentRow;
300 void ImportWidget::cleanUpAfterImport()
307 mTableWidget->clear();
308 mTableWidget->setRowCount(0);
309 mTableWidget->setHorizontalHeaderLabels(mTableHeader);
311 for(
int i=mStackedWidget->count()-1; i>=0; --i)
313 QWidget *widgetToRemove = mStackedWidget->widget(i);
314 mStackedWidget->removeWidget(widgetToRemove);
315 delete widgetToRemove;
319 QStringList ImportWidget::openFileBrowserForSelectingFiles()
321 QString file_type_filter = generateFileTypeFilter();
323 QFileDialog dialog(this->parentWidget(), QString(tr(
"Select Medical Image file(s)/folder(s) for import")), QDir::homePath(), tr(file_type_filter.toStdString().c_str()));
324 dialog.setFileMode(QFileDialog::Directory);
328 dialog.setOption(QFileDialog::DontUseNativeDialog,
true);
329 QListView *l = dialog.findChild<QListView*>(
"listView");
331 l->setSelectionMode(QAbstractItemView::MultiSelection);
332 QTreeView *t = dialog.findChild<QTreeView*>();
334 t->setSelectionMode(QAbstractItemView::MultiSelection);
336 QStringList fileName;
338 fileName = dialog.selectedFiles();
339 fileName = removeDirIfSubdirIsIncluded(fileName);
341 if (fileName.empty())
343 report(
"Import canceled");
349 QStringList ImportWidget::removeDirIfSubdirIsIncluded(QStringList importFiles)
353 for(
int i = 0; i < importFiles.size(); ++i)
355 if(!QFileInfo(importFiles[i]).isDir())
356 retval << importFiles[i];
358 allDirs << importFiles[i];
361 QStringList removeDirs;
362 for(
int i = 0; i < importFiles.size(); ++i)
364 for(
int j = 0; j < allDirs.size(); ++j)
367 if(importFiles[i].contains(allDirs[j]))
368 removeDirs << allDirs[j];
371 for(
int i = 0; i < allDirs.size(); ++i)
374 for(
int j = 0; j < removeDirs.size(); ++j)
376 if(allDirs[i] == removeDirs[j])
380 retval << allDirs[i];
385 QString ImportWidget::generateFileTypeFilter()
const
387 QString file_type_filter;
388 std::vector<FileReaderWriterServicePtr> mesh_readers = mVisServices->file()->getImportersForDataType(
Mesh::getTypeName());
389 std::vector<FileReaderWriterServicePtr> image_readers = mVisServices->file()->getImportersForDataType(
Image::getTypeName());
390 std::vector<FileReaderWriterServicePtr> point_metric_readers = mVisServices->file()->getImportersForDataType(
PointMetric::getTypeName());
391 std::vector<FileReaderWriterServicePtr> readers;
392 readers.insert( readers.end(), mesh_readers.begin(), mesh_readers.end() );
393 readers.insert( readers.end(), image_readers.begin(), image_readers.end() );
394 readers.insert( readers.end(), point_metric_readers.begin(), point_metric_readers.end() );
396 file_type_filter =
"Image/Mesh/PointMetrics (";
397 for(
unsigned i=0; i<readers.size(); ++i)
399 QString suffix = readers[i]->getFileSuffix();
400 if(!suffix.isEmpty())
401 file_type_filter.append(
"*."+suffix+
" ");
403 while(file_type_filter.endsWith(
' ' ))
404 file_type_filter.chop(1);
405 file_type_filter.append(
")");
407 return file_type_filter;
410 void ImportWidget::generateParentCandidates()
412 for(
unsigned i=0; i<mNotImportedData.size(); ++i)
414 if(!mNotImportedData[i])
416 CX_LOG_WARNING() <<
"ImportWidget::generateParentCandidates(): No data";
420 mParentCandidates.push_back(mNotImportedData[i]);
422 std::map<QString, DataPtr> loadedData = mVisServices->patient()->getDatas();
423 std::map<QString, DataPtr>::iterator it = loadedData.begin();
424 for(; it!=loadedData.end(); ++it)
426 mParentCandidates.push_back(it->second);