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);
96 int ImportWidget::insertDataIntoTable(QString fullfilename, std::vector<DataPtr> data)
98 int newRowIndex = mTableWidget->rowCount();
99 mTableWidget->setRowCount(newRowIndex+1);
100 mTableWidget->selectRow(mSelectedIndexInTable);
102 bool allDataOk =
true;
103 for(
unsigned i=0; i<data.size(); ++i)
108 status = allDataOk ?
"ok" :
"error";
109 QFileInfo fileInfo(fullfilename);
110 QString filename = fileInfo.fileName();
112 QIcon trashcan(
":/icons/open_icon_library/edit-delete-2.png");
113 QPushButton *removeButton =
new QPushButton(trashcan,
"");
114 connect(removeButton, &QPushButton::clicked,
this, &ImportWidget::removeRowFromTableAndRemoveFilenameFromImportList);
116 mTableWidget->setCellWidget(newRowIndex, 0, removeButton);
117 mTableWidget->setItem(newRowIndex, 1,
new QTableWidgetItem(status));
118 QTableWidgetItem *filenameItem =
new QTableWidgetItem(filename);
119 filenameItem->setData(Qt::ToolTipRole, fullfilename);
120 mTableWidget->setItem(newRowIndex, 2, filenameItem);
125 int ImportWidget::findRowIndexContainingButton(QPushButton *button)
const 128 for(
int i=0; i<mTableWidget->rowCount(); ++i)
130 int buttonColoumn = 0;
131 QTableWidgetItem *item = mTableWidget->item(i,buttonColoumn);
132 QWidget *cellWidget = mTableWidget->cellWidget(i,buttonColoumn);
133 if(button == cellWidget)
139 void ImportWidget::addMoreFilesButtonClicked()
141 QStringList filenames = this->openFileBrowserForSelectingFiles();
144 foreach (QString filename, filenames)
146 mFileNames.push_back(filename);
148 std::vector<DataPtr> newData = mFileManager->read(filename);
149 int index = this->insertDataIntoTable(filename, newData);
152 mStackedWidget->insertWidget(index, widget);
154 mNotImportedData.insert(mNotImportedData.end(), newData.begin(), newData.end());
157 this->generateParentCandidates();
160 void ImportWidget::importButtonClicked()
165 mVisServices->session()->save();
168 void ImportWidget::cancelButtonClicked()
173 void ImportWidget::removeWidget(QWidget *widget)
175 mTopLayout->removeWidget(widget);
176 widget->deleteLater();
179 void ImportWidget::removeRowFromTableAndRemoveFilenameFromImportList()
181 QPushButton *button = qobject_cast<QPushButton*>(QObject::sender());
182 int rowindex = this->findRowIndexContainingButton(button);
183 int filenamecoloumn = 2;
184 QString fullfilename = mTableWidget->item(rowindex, filenamecoloumn)->data(Qt::ToolTipRole).toString();
186 mTableWidget->removeRow(rowindex);
187 int numberOfRemovedEntries = mFileNames.removeAll(fullfilename);
189 QWidget *widgetToRemove = mStackedWidget->widget(rowindex);
190 mStackedWidget->removeWidget(widgetToRemove);
193 void ImportWidget::tableItemSelected(
int currentRow,
int currentColumn,
int previousRow,
int previousColumn)
195 if(currentRow == mSelectedIndexInTable)
198 mStackedWidget->setCurrentIndex(currentRow);
199 mSelectedIndexInTable = currentRow;
202 void ImportWidget::cleanUpAfterImport()
209 mTableWidget->clear();
210 mTableWidget->setRowCount(0);
211 mTableWidget->setHorizontalHeaderLabels(mTableHeader);
213 for(
int i=mStackedWidget->count()-1; i>=0; --i)
215 QWidget *widgetToRemove = mStackedWidget->widget(i);
216 mStackedWidget->removeWidget(widgetToRemove);
217 delete widgetToRemove;
221 QStringList ImportWidget::openFileBrowserForSelectingFiles()
223 QString file_type_filter = generateFileTypeFilter();
225 QStringList fileName = QFileDialog::getOpenFileNames(this->parentWidget(), QString(tr(
"Select data file(s) for import")),
profile()->getSessionRootFolder(), tr(file_type_filter.toStdString().c_str()));
226 if (fileName.empty())
228 report(
"Import canceled");
235 QString ImportWidget::generateFileTypeFilter()
const 237 QString file_type_filter;
238 std::vector<FileReaderWriterServicePtr> mesh_readers = mVisServices->file()->getImportersForDataType(
Mesh::getTypeName());
239 std::vector<FileReaderWriterServicePtr> image_readers = mVisServices->file()->getImportersForDataType(
Image::getTypeName());
240 std::vector<FileReaderWriterServicePtr> point_metric_readers = mVisServices->file()->getImportersForDataType(
PointMetric::getTypeName());
241 std::vector<FileReaderWriterServicePtr> readers;
242 readers.insert( readers.end(), mesh_readers.begin(), mesh_readers.end() );
243 readers.insert( readers.end(), image_readers.begin(), image_readers.end() );
244 readers.insert( readers.end(), point_metric_readers.begin(), point_metric_readers.end() );
246 file_type_filter =
"Image/Mesh/PointMetrics (";
247 for(
unsigned i=0; i<readers.size(); ++i)
249 QString suffix = readers[i]->getFileSuffix();
250 if(!suffix.isEmpty())
251 file_type_filter.append(
"*."+suffix+
" ");
253 while(file_type_filter.endsWith(
' ' ))
254 file_type_filter.chop(1);
255 file_type_filter.append(
")");
257 return file_type_filter;
260 void ImportWidget::generateParentCandidates()
262 for(
unsigned i=0; i<mNotImportedData.size(); ++i)
265 mParentCandidates.push_back(mNotImportedData[i]);
267 std::map<QString, DataPtr> loadedData = mVisServices->patient()->getDatas();
268 std::map<QString, DataPtr>::iterator it = loadedData.begin();
269 for(; it!=loadedData.end(); ++it)
271 mParentCandidates.push_back(it->second);
cxResource_EXPORT ProfilePtr profile()
boost::shared_ptr< class FileManagerService > FileManagerServicePtr
boost::shared_ptr< class VisServices > VisServicesPtr
static QString getTypeName()
static QString getTypeName()
static QString getTypeName()
Namespace for all CustusX production code.