14 #include <QTreeWidgetItem>
16 #include <QFileDialog>
17 #include <QPushButton>
19 #include <QTableWidget>
20 #include <QHeaderView>
21 #include <QApplication>
22 #include <QDesktopWidget>
23 #include <QStackedWidget>
47 mSelectedIndexInTable(0),
48 mFileManager(filemanager),
49 mVisServices(services)
52 mTableWidget =
new QTableWidget();
53 mTableWidget->setRowCount(0);
54 mTableWidget->setColumnCount(3);
55 mTableHeader<<
""<<
"Status"<<
"Filename";
56 mTableWidget->setHorizontalHeaderLabels(mTableHeader);
57 mTableWidget->horizontalHeader()->setStretchLastSection(
true);
58 mTableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);
59 mTableWidget->verticalHeader()->setVisible(
false);
60 mTableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);
61 mTableWidget->setSelectionBehavior(QAbstractItemView::SelectRows);
62 mTableWidget->setSelectionMode(QAbstractItemView::SingleSelection);
63 mTableWidget->setShowGrid(
false);
64 mTableWidget->setStyleSheet(
"QTableView {selection-background-color: #ACCEF7;}");
65 mTableWidget->setGeometry(QApplication::desktop()->screenGeometry());
66 connect(mTableWidget, &QTableWidget::currentCellChanged,
this, &ImportWidget::tableItemSelected);
68 mStackedWidget =
new QStackedWidget;
70 mTopLayout =
new QVBoxLayout();
71 this->setLayout(mTopLayout);
72 QPushButton *addMoreFilesButton =
new QPushButton(
"Add more files...");
73 QHBoxLayout *buttonLayout =
new QHBoxLayout();
74 QPushButton *importButton =
new QPushButton(
"Import");
75 QPushButton *cancelButton =
new QPushButton(
"Cancel");
76 buttonLayout->addWidget(importButton);
77 buttonLayout->addWidget(cancelButton);
78 buttonLayout->addStretch();
79 mTopLayout->addWidget(addMoreFilesButton);
80 mTopLayout->addWidget(
new QLabel(
"Supports: "+this->generateFileTypeFilter()));
81 mTopLayout->addWidget(mTableWidget);
82 mTopLayout->addWidget(mStackedWidget);
83 mTopLayout->addStretch();
84 mTopLayout->addLayout(buttonLayout);
86 connect(addMoreFilesButton, &QPushButton::clicked,
this, &ImportWidget::addMoreFilesButtonClicked);
87 connect(importButton, &QPushButton::clicked,
this, &ImportWidget::importButtonClicked);
88 connect(cancelButton, &QPushButton::clicked,
this, &ImportWidget::cancelButtonClicked);
91 QAction* addMoreFilesButtonClickedAction =
new QAction(
"AddMoreFilesButtonClickedAction",
this);
92 this->addAction(addMoreFilesButtonClickedAction);
93 connect(addMoreFilesButtonClickedAction, &QAction::triggered,
this, &ImportWidget::addMoreFilesButtonClicked);
95 QAction* importButtonClickedAction =
new QAction(
"ImportButtonClickedAction",
this);
96 this->addAction(importButtonClickedAction);
97 connect(importButtonClickedAction, &QAction::triggered,
this, &ImportWidget::importButtonClicked);
100 int ImportWidget::insertDataIntoTable(QString fullfilename, std::vector<DataPtr> data)
102 int newRowIndex = mTableWidget->rowCount();
103 mTableWidget->setRowCount(newRowIndex+1);
104 mTableWidget->selectRow(mSelectedIndexInTable);
106 bool allDataOk =
true;
107 for(
unsigned i=0; i<data.size(); ++i)
112 status = allDataOk ?
"ok" :
"error";
113 QFileInfo fileInfo(fullfilename);
114 QString filename = fileInfo.fileName();
116 QIcon trashcan(
":/icons/open_icon_library/edit-delete-2.png");
117 QPushButton *removeButton =
new QPushButton(trashcan,
"");
118 connect(removeButton, &QPushButton::clicked,
this, &ImportWidget::removeRowFromTableAndRemoveFilenameFromImportList);
120 mTableWidget->setCellWidget(newRowIndex, 0, removeButton);
121 mTableWidget->setItem(newRowIndex, 1,
new QTableWidgetItem(status));
122 QTableWidgetItem *filenameItem =
new QTableWidgetItem(filename);
123 filenameItem->setData(Qt::ToolTipRole, fullfilename);
124 mTableWidget->setItem(newRowIndex, 2, filenameItem);
129 int ImportWidget::findRowIndexContainingButton(QPushButton *button)
const
132 for(
int i=0; i<mTableWidget->rowCount(); ++i)
134 int buttonColoumn = 0;
135 QTableWidgetItem *item = mTableWidget->item(i,buttonColoumn);
136 QWidget *cellWidget = mTableWidget->cellWidget(i,buttonColoumn);
137 if(button == cellWidget)
143 void ImportWidget::addMoreFilesButtonClicked()
145 QStringList filenames = this->openFileBrowserForSelectingFiles();
147 bool addedDICOM =
false;
149 ImportDataTypeWidget *widget = NULL;
150 for(
int i = 0; i < filenames.size(); ++i)
152 QString filename = filenames[i];
153 QString fileType = mFileManager->getFileReaderName(filename);
154 if(fileType ==
"DICOMReader")
162 CX_LOG_WARNING() <<
"Import of multiple DICOM series at once is not supported. Skipping series based on file: " << filename;
168 mFileNames.push_back(filename);
170 std::vector<DataPtr> newData = mFileManager->read(filename);
171 if(newData.size() > 0)
173 int index = this->insertDataIntoTable(filename, newData);
174 widget =
new ImportDataTypeWidget(
this, mVisServices, newData, mParentCandidates, filename);
175 mStackedWidget->insertWidget(index, widget);
176 mNotImportedData.insert(mNotImportedData.end(), newData.begin(), newData.end());
180 this->generateParentCandidates();
183 void ImportWidget::importButtonClicked()
189 mVisServices->session()->save();
192 void ImportWidget::cancelButtonClicked()
198 void ImportWidget::clearData()
200 mParentCandidates.clear();
201 mNotImportedData.clear();
204 void ImportWidget::removeWidget(QWidget *widget)
206 mTopLayout->removeWidget(widget);
207 widget->deleteLater();
210 void ImportWidget::removeRowFromTableAndRemoveFilenameFromImportList()
212 QPushButton *button = qobject_cast<QPushButton*>(QObject::sender());
213 int rowindex = this->findRowIndexContainingButton(button);
214 int filenamecoloumn = 2;
215 QString fullfilename = mTableWidget->item(rowindex, filenamecoloumn)->data(Qt::ToolTipRole).toString();
217 mTableWidget->removeRow(rowindex);
218 int numberOfRemovedEntries = mFileNames.removeAll(fullfilename);
220 QWidget *widgetToRemove = mStackedWidget->widget(rowindex);
221 mStackedWidget->removeWidget(widgetToRemove);
224 void ImportWidget::tableItemSelected(
int currentRow,
int currentColumn,
int previousRow,
int previousColumn)
226 if(currentRow == mSelectedIndexInTable)
229 mStackedWidget->setCurrentIndex(currentRow);
230 mSelectedIndexInTable = currentRow;
233 void ImportWidget::cleanUpAfterImport()
240 mTableWidget->clear();
241 mTableWidget->setRowCount(0);
242 mTableWidget->setHorizontalHeaderLabels(mTableHeader);
244 for(
int i=mStackedWidget->count()-1; i>=0; --i)
246 QWidget *widgetToRemove = mStackedWidget->widget(i);
247 mStackedWidget->removeWidget(widgetToRemove);
248 delete widgetToRemove;
252 QStringList ImportWidget::openFileBrowserForSelectingFiles()
254 QString file_type_filter = generateFileTypeFilter();
256 QStringList fileName = QFileDialog::getOpenFileNames(this->parentWidget(), QString(tr(
"Select data file(s) for import")),
profile()->getSessionRootFolder(), tr(file_type_filter.toStdString().c_str()));
257 if (fileName.empty())
259 report(
"Import canceled");
266 QString ImportWidget::generateFileTypeFilter()
const
268 QString file_type_filter;
269 std::vector<FileReaderWriterServicePtr> mesh_readers = mVisServices->file()->getImportersForDataType(
Mesh::getTypeName());
270 std::vector<FileReaderWriterServicePtr> image_readers = mVisServices->file()->getImportersForDataType(
Image::getTypeName());
271 std::vector<FileReaderWriterServicePtr> point_metric_readers = mVisServices->file()->getImportersForDataType(
PointMetric::getTypeName());
272 std::vector<FileReaderWriterServicePtr> readers;
273 readers.insert( readers.end(), mesh_readers.begin(), mesh_readers.end() );
274 readers.insert( readers.end(), image_readers.begin(), image_readers.end() );
275 readers.insert( readers.end(), point_metric_readers.begin(), point_metric_readers.end() );
277 file_type_filter =
"Image/Mesh/PointMetrics (";
278 for(
unsigned i=0; i<readers.size(); ++i)
280 QString suffix = readers[i]->getFileSuffix();
281 if(!suffix.isEmpty())
282 file_type_filter.append(
"*."+suffix+
" ");
284 while(file_type_filter.endsWith(
' ' ))
285 file_type_filter.chop(1);
286 file_type_filter.append(
")");
288 return file_type_filter;
291 void ImportWidget::generateParentCandidates()
293 for(
unsigned i=0; i<mNotImportedData.size(); ++i)
295 if(!mNotImportedData[i])
297 CX_LOG_WARNING() <<
"ImportWidget::generateParentCandidates(): No data";
301 mParentCandidates.push_back(mNotImportedData[i]);
303 std::map<QString, DataPtr> loadedData = mVisServices->patient()->getDatas();
304 std::map<QString, DataPtr>::iterator it = loadedData.begin();
305 for(; it!=loadedData.end(); ++it)
307 mParentCandidates.push_back(it->second);