diff --git a/Modules/Qmitk/QmitkNativeFileOpenDialog.cpp b/Modules/Qmitk/QmitkNativeFileOpenDialog.cpp new file mode 100644 index 0000000000..a63fe6a9cc --- /dev/null +++ b/Modules/Qmitk/QmitkNativeFileOpenDialog.cpp @@ -0,0 +1,110 @@ +/*=================================================================== + +The Medical Imaging Interaction Toolkit (MITK) + +Copyright (c) German Cancer Research Center, +Division of Medical and Biological Informatics. +All rights reserved. + +This software is distributed WITHOUT ANY WARRANTY; without +even the implied warranty of MERCHANTABILITY or FITNESS FOR +A PARTICULAR PURPOSE. + +See LICENSE.txt or http://www.mitk.org for details. + +===================================================================*/ + +#include "QmitkNativeFileOpenDialog.h" + +#include "QmitkIOUtil.h" + +#include "mitkCoreServices.h" +#include "mitkIMimeTypeProvider.h" + +#include + +class QmitkNativeFileOpenDialogPrivate +{ + +public: + + QWidget* parent; + QString caption; + QString dir; + QFileDialog::Options options; + QString filter; + QStringList selectedFiles; + + QString GetFilter() + { + QString filters; + + mitk::CoreServicePointer mimeTypeProvider(mitk::CoreServices::GetMimeTypeProvider()); + std::vector categories = mimeTypeProvider->GetCategories(); + std::sort(categories.begin(), categories.end()); + for (std::vector::iterator cat = categories.begin(); cat != categories.end(); ++cat) + { + QSet filterExtensions; + std::vector mimeTypes = mimeTypeProvider->GetMimeTypesForCategory(*cat); + for (std::vector::iterator mt = mimeTypes.begin(); mt != mimeTypes.end(); ++mt) + { + std::vector extensions = mimeTypeProvider->GetExtensions(*mt); + for (std::vector::iterator ext = extensions.begin(); ext != extensions.end(); ++ext) + { + filterExtensions << QString::fromStdString(*ext); + } + } + + QString filter = QString::fromStdString(*cat) + " ("; + foreach(const QString& extension, filterExtensions) + { + filter += "*." + extension + " "; + } + filter = filter.replace(filter.size()-1, 1, ')'); + filters += ";;" + filter; + } + filters.prepend("All (*.*)"); + return filters; + } +}; + +QmitkNativeFileOpenDialog::QmitkNativeFileOpenDialog(QWidget* parent) + : d(new QmitkNativeFileOpenDialogPrivate) +{ + d->parent = parent; + d->filter = d->GetFilter(); +} + +QmitkNativeFileOpenDialog::~QmitkNativeFileOpenDialog() +{ +} + +void QmitkNativeFileOpenDialog::SetCaption(const QString& caption) +{ + d->caption = caption; +} + +void QmitkNativeFileOpenDialog::SetDir(const QString& dir) +{ + d->dir = dir; +} + +void QmitkNativeFileOpenDialog::SetOptions(QFileDialog::Options options) +{ + d->options = options; +} + +void QmitkNativeFileOpenDialog::Exec() +{ + d->selectedFiles = QFileDialog::getOpenFileNames(d->parent, d->caption, d->dir, d->filter, NULL, d->options); +} + +QStringList QmitkNativeFileOpenDialog::GetSelectedFiles() const +{ + return d->selectedFiles; +} + +QList QmitkNativeFileOpenDialog::ReadBaseData(mitk::DataStorage* ds) const +{ + return QmitkIOUtil::LoadFiles(d->selectedFiles, ds); +} diff --git a/Modules/Qmitk/QmitkNativeFileOpenDialog.h b/Modules/Qmitk/QmitkNativeFileOpenDialog.h new file mode 100644 index 0000000000..4896d869f7 --- /dev/null +++ b/Modules/Qmitk/QmitkNativeFileOpenDialog.h @@ -0,0 +1,58 @@ +/*=================================================================== + +The Medical Imaging Interaction Toolkit (MITK) + +Copyright (c) German Cancer Research Center, +Division of Medical and Biological Informatics. +All rights reserved. + +This software is distributed WITHOUT ANY WARRANTY; without +even the implied warranty of MERCHANTABILITY or FITNESS FOR +A PARTICULAR PURPOSE. + +See LICENSE.txt or http://www.mitk.org for details. + +===================================================================*/ + +#ifndef QMITKNATIVEFILEOPENDIALOG_H +#define QMITKNATIVEFILEOPENDIALOG_H + +#include + +#include + +#include +#include + +namespace mitk { +class DataStorage; +} + +class QmitkNativeFileOpenDialogPrivate; + +class QMITK_EXPORT QmitkNativeFileOpenDialog +{ + +public: + + QmitkNativeFileOpenDialog(QWidget* parent = NULL); + ~QmitkNativeFileOpenDialog(); + + void SetCaption(const QString& caption); + void SetDir(const QString& dir); + void SetOptions(QFileDialog::Options options); + + void Exec(); + + QStringList GetSelectedFiles() const; + + QList ReadBaseData(mitk::DataStorage* ds = NULL) const; + +private: + + Q_DISABLE_COPY(QmitkNativeFileOpenDialog) + + QScopedPointer d; +}; + +#endif // QMITKNATIVEFILEOPENDIALOG_H diff --git a/Modules/Qmitk/files.cmake b/Modules/Qmitk/files.cmake index a9f3a319f4..95dd1150a4 100644 --- a/Modules/Qmitk/files.cmake +++ b/Modules/Qmitk/files.cmake @@ -1,73 +1,74 @@ set(CPP_FILES QmitkApplicationCursor.cpp QmitkEnums.h QmitkCustomVariants.h QmitkDataStorageComboBox.cpp QmitkDataStorageListModel.cpp QmitkDataStorageTableModel.cpp QmitkDataStorageTreeModel.cpp QmitkEventAdapter.cpp QmitkFileDialog.cpp QmitkFileOpenDialog.cpp QmitkFileSaveDialog.cpp QmitkIOUtil.cpp QmitkLevelWindowPresetDefinitionDialog.cpp QmitkLevelWindowRangeChangeDialog.cpp QmitkLevelWindowWidgetContextMenu.cpp QmitkLevelWindowWidget.cpp QmitkLineEditLevelWindowWidget.cpp QmitkMemoryUsageIndicatorView.cpp +QmitkNativeFileOpenDialog.cpp QmitkNodeDescriptor.cpp QmitkNodeDescriptorManager.cpp QmitkRenderWindowMenu.cpp QmitkProgressBar.cpp QmitkPropertiesTableEditor.cpp QmitkPropertiesTableModel.cpp QmitkPropertyDelegate.cpp QmitkRegisterClasses.cpp QmitkRenderingManager.cpp QmitkRenderingManagerFactory.cpp QmitkRenderWindow.cpp QmitkServiceListWidget.cpp QmitkSliderLevelWindowWidget.cpp QmitkStdMultiWidget.cpp QmitkMouseModeSwitcher.cpp ) set(MOC_H_FILES QmitkDataStorageComboBox.h QmitkDataStorageTableModel.h QmitkFileDialog.h QmitkFileOpenDialog.h QmitkFileSaveDialog.h QmitkLevelWindowPresetDefinitionDialog.h QmitkLevelWindowRangeChangeDialog.h QmitkLevelWindowWidgetContextMenu.h QmitkLevelWindowWidget.h QmitkLineEditLevelWindowWidget.h QmitkMemoryUsageIndicatorView.h QmitkNodeDescriptor.h QmitkNodeDescriptorManager.h QmitkRenderWindowMenu.h QmitkProgressBar.h QmitkPropertiesTableEditor.h QmitkPropertyDelegate.h QmitkRenderingManager.h QmitkRenderWindow.h QmitkServiceListWidget.h QmitkSliderLevelWindowWidget.h QmitkStdMultiWidget.h QmitkMouseModeSwitcher.h ) set(UI_FILES QmitkLevelWindowPresetDefinition.ui QmitkLevelWindowWidget.ui QmitkLevelWindowRangeChange.ui QmitkMemoryUsageIndicator.ui QmitkServiceListWidgetControls.ui ) set(QRC_FILES Qmitk.qrc ) diff --git a/Plugins/org.mitk.gui.common/CMakeLists.txt b/Plugins/org.mitk.gui.common/CMakeLists.txt index 2e7de4bb9c..d4f785db05 100644 --- a/Plugins/org.mitk.gui.common/CMakeLists.txt +++ b/Plugins/org.mitk.gui.common/CMakeLists.txt @@ -1,8 +1,9 @@ project(org_mitk_gui_common) MACRO_CREATE_MITK_CTK_PLUGIN( EXPORT_DIRECTIVE MITK_GUI_COMMON_PLUGIN EXPORTED_INCLUDE_SUFFIXES src + MODULE_DEPENDENCIES Qmitk SUBPROJECTS MITK-CoreUI ) diff --git a/Plugins/org.mitk.gui.common/src/mitkWorkbenchUtil.cpp b/Plugins/org.mitk.gui.common/src/mitkWorkbenchUtil.cpp index 956788b312..70c2b8e74d 100644 --- a/Plugins/org.mitk.gui.common/src/mitkWorkbenchUtil.cpp +++ b/Plugins/org.mitk.gui.common/src/mitkWorkbenchUtil.cpp @@ -1,347 +1,350 @@ /*=================================================================== The Medical Imaging Interaction Toolkit (MITK) Copyright (c) German Cancer Research Center, Division of Medical and Biological Informatics. All rights reserved. This software is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See LICENSE.txt or http://www.mitk.org for details. ===================================================================*/ #include "mitkWorkbenchUtil.h" #include #include #include #include #include "mitkIDataStorageService.h" #include "mitkDataStorageEditorInput.h" #include "mitkRenderingManager.h" #include "mitkIRenderWindowPart.h" #include "mitkIRenderingManager.h" #include "mitkProperties.h" #include "mitkNodePredicateData.h" #include "mitkNodePredicateNot.h" #include "mitkNodePredicateProperty.h" -#include "mitkIOUtil.h" -#include "mitkWorkbenchUtil.h" +#include "mitkCoreObjectFactory.h" -#include +#include "QmitkIOUtil.h" #include #include #include #include "internal/org_mitk_gui_common_Activator.h" namespace mitk { struct WorkbenchUtilPrivate { /** * Get the editor descriptor for a given name using the editorDescriptor * passed in as a default as a starting point. * * @param name * The name of the element to open. * @param editorReg * The editor registry to do the lookups from. * @param defaultDescriptor * IEditorDescriptor or null * @return IEditorDescriptor * @throws PartInitException * if no valid editor can be found */ static berry::IEditorDescriptor::Pointer GetEditorDescriptor(const QString& name, berry::IEditorRegistry* editorReg, berry::IEditorDescriptor::Pointer defaultDescriptor) { if (defaultDescriptor.IsNotNull()) { return defaultDescriptor; } berry::IEditorDescriptor::Pointer editorDesc = defaultDescriptor; // next check the OS for in-place editor (OLE on Win32) if (editorReg->IsSystemInPlaceEditorAvailable(name.toStdString())) { editorDesc = editorReg->FindEditor(berry::IEditorRegistry::SYSTEM_INPLACE_EDITOR_ID); } // next check with the OS for an external editor if (editorDesc.IsNull() && editorReg->IsSystemExternalEditorAvailable(name.toStdString())) { editorDesc = editorReg->FindEditor(berry::IEditorRegistry::SYSTEM_EXTERNAL_EDITOR_ID); } // if no valid editor found, bail out if (editorDesc.IsNull()) { throw berry::PartInitException("No editor found"); } return editorDesc; } }; + // //! [UtilLoadFiles] void WorkbenchUtil::LoadFiles(const QStringList &fileNames, berry::IWorkbenchWindow::Pointer window, bool openEditor) // //! [UtilLoadFiles] { if (fileNames.empty()) return; mitk::IDataStorageReference::Pointer dataStorageRef; { ctkPluginContext* context = mitk::PluginActivator::GetContext(); mitk::IDataStorageService* dss = 0; ctkServiceReference dsRef = context->getServiceReference(); if (dsRef) { dss = context->getService(dsRef); } if (!dss) { QString msg = "IDataStorageService service not available. Unable to open files."; MITK_WARN << msg.toStdString(); QMessageBox::warning(QApplication::activeWindow(), "Unable to open files", msg); return; } // Get the active data storage (or the default one, if none is active) dataStorageRef = dss->GetDataStorage(); context->ungetService(dsRef); } mitk::DataStorage::Pointer dataStorage = dataStorageRef->GetDataStorage(); - // Do the actual work of loading the data into the data storage - std::vector fileNames2; - - // Correct conversion for File names.(BUG 12252) - fileNames2.resize(fileNames.size()); - for (int i = 0; i< fileNames.size(); i++) - fileNames2[i] = std::string(QFile::encodeName(fileNames[i]).data()); - - // Old conversion which returns wrong encoded Non-Latin-Characters. - //ctk::qListToSTLVector(fileNames, fileNames2); - // Turn off ASSERT #if defined(_MSC_VER) && !defined(NDEBUG) && defined(_DEBUG) && defined(_CRT_ERROR) int lastCrtReportType = _CrtSetReportMode( _CRT_ASSERT, _CRTDBG_MODE_DEBUG ); #endif - const bool dsmodified = mitk::IOUtil::LoadFiles(fileNames2, *dataStorage); + unsigned int oldSize = dataStorage->GetAll()->Size(); + QList data = QmitkIOUtil::LoadFiles(fileNames, dataStorage.GetPointer()); + foreach(mitk::BaseData::Pointer baseData, data) + { + mitk::DataNode::Pointer node = mitk::DataNode::New(); + node->SetData(baseData); + mitk::StringProperty::Pointer pathProp = dynamic_cast(baseData->GetProperty("path").GetPointer()); + if (pathProp.IsNotNull()) + { + node->SetName(pathProp->GetValue()); + } + mitk::CoreObjectFactory::GetInstance()->SetDefaultProperties(node); + dataStorage->Add(node); + } + const bool dsmodified = !data.empty() || oldSize < dataStorage->GetAll()->Size(); // Set ASSERT status back to previous status. #if defined(_MSC_VER) && !defined(NDEBUG) && defined(_DEBUG) && defined(_CRT_ERROR) if (lastCrtReportType) _CrtSetReportMode( _CRT_ASSERT, lastCrtReportType ); #endif // Check if there is an open perspective. If not, open the default perspective. if (window->GetActivePage().IsNull()) { std::string defaultPerspId = window->GetWorkbench()->GetPerspectiveRegistry()->GetDefaultPerspective(); window->GetWorkbench()->ShowPerspective(defaultPerspId, window); } if (openEditor) { try { // Activate the editor using the same data storage or open the default editor mitk::DataStorageEditorInput::Pointer input(new mitk::DataStorageEditorInput(dataStorageRef)); berry::IEditorPart::Pointer editor = mitk::WorkbenchUtil::OpenEditor(window->GetActivePage(), input, true); mitk::IRenderWindowPart* renderEditor = dynamic_cast(editor.GetPointer()); mitk::IRenderingManager* renderingManager = renderEditor == 0 ? 0 : renderEditor->GetRenderingManager(); if(dsmodified && renderingManager) { // get all nodes that have not set "includeInBoundingBox" to false mitk::NodePredicateNot::Pointer pred = mitk::NodePredicateNot::New(mitk::NodePredicateProperty::New("includeInBoundingBox" , mitk::BoolProperty::New(false))); mitk::DataStorage::SetOfObjects::ConstPointer rs = dataStorage->GetSubset(pred); // calculate bounding geometry of these nodes mitk::TimeSlicedGeometry::Pointer bounds = dataStorage->ComputeBoundingGeometry3D(rs); // initialize the views to the bounding geometry renderingManager->InitializeViews(bounds); } } catch (const berry::PartInitException& e) { QString msg = "An error occurred when displaying the file(s): %1"; QMessageBox::warning(QApplication::activeWindow(), "Error displaying file", msg.arg(QString::fromStdString(e.message()))); } } } berry::IEditorPart::Pointer WorkbenchUtil::OpenEditor(berry::IWorkbenchPage::Pointer page, berry::IEditorInput::Pointer input, const QString &editorId, bool activate) { // sanity checks if (page.IsNull()) { throw std::invalid_argument("page argument must not be NULL"); } // open the editor on the input return page->OpenEditor(input, editorId.toStdString(), activate); } berry::IEditorPart::Pointer WorkbenchUtil::OpenEditor(berry::IWorkbenchPage::Pointer page, mitk::DataStorageEditorInput::Pointer input, bool activate, bool determineContentType) { // sanity checks if (page.IsNull()) { throw std::invalid_argument("page argument must not be NULL"); } // open the editor on the data storage QString name = QString::fromStdString(input->GetName()) + ".mitk"; berry::IEditorDescriptor::Pointer editorDesc = WorkbenchUtilPrivate::GetEditorDescriptor(name, berry::PlatformUI::GetWorkbench()->GetEditorRegistry(), GetDefaultEditor(name, determineContentType)); return page->OpenEditor(input, editorDesc->GetId(), activate); } berry::IEditorDescriptor::Pointer WorkbenchUtil::GetEditorDescriptor( const QString& name, bool /*inferContentType*/) { if (name.isEmpty()) { throw std::invalid_argument("name argument must not be empty"); } // no used for now //IContentType contentType = inferContentType ? Platform // .getContentTypeManager().findContentTypeFor(name) : null; berry::IEditorRegistry* editorReg = berry::PlatformUI::GetWorkbench()->GetEditorRegistry(); return WorkbenchUtilPrivate::GetEditorDescriptor(name, editorReg, editorReg->GetDefaultEditor(name.toStdString() /*, contentType*/)); } berry::IEditorDescriptor::Pointer WorkbenchUtil::GetDefaultEditor(const QString& name, bool /*determineContentType*/) { // Try file specific editor. berry::IEditorRegistry* editorReg = berry::PlatformUI::GetWorkbench()->GetEditorRegistry(); try { QString editorID; // = file.getPersistentProperty(EDITOR_KEY); if (!editorID.isEmpty()) { berry::IEditorDescriptor::Pointer desc = editorReg->FindEditor(editorID.toStdString()); if (desc.IsNotNull()) { return desc; } } } catch (const berry::CoreException&) { // do nothing } // IContentType contentType = null; // if (determineContentType) // { // contentType = getContentType(file); // } // Try lookup with filename return editorReg->GetDefaultEditor(name.toStdString()); //, contentType); } bool WorkbenchUtil::SetDepartmentLogoPreference(const QString &logoResource, ctkPluginContext *context) { // The logo must be available in the local filesystem. We check if we have not already extracted the // logo from the plug-in or if this plug-ins timestamp is newer then the already extracted logo timestamp. // If one of the conditions is true, extract it and write it to the plug-in specific storage location. const QString logoFileName = logoResource.mid(logoResource.lastIndexOf('/')+1); const QString logoPath = context->getDataFile("").absoluteFilePath(); bool extractLogo = true; QFileInfo logoFileInfo(logoPath + "/" + logoFileName); if (logoFileInfo.exists()) { // The logo has been extracted previously. Check if the plugin timestamp is newer, which // means it might contain an updated logo. QString pluginLocation = QUrl(context->getPlugin()->getLocation()).toLocalFile(); if (!pluginLocation.isEmpty()) { QFileInfo pluginFileInfo(pluginLocation); if (logoFileInfo.lastModified() > pluginFileInfo.lastModified()) { extractLogo = false; } } } if (extractLogo) { // Extract the logo from the shared library and write it to disk. QFile logo(logoResource); if (logo.open(QIODevice::ReadOnly)) { QFile localLogo(logoPath + "/" + logoFileName); if (localLogo.open(QIODevice::WriteOnly)) { localLogo.write(logo.readAll()); } } } logoFileInfo.refresh(); if (logoFileInfo.exists()) { // Get the preferences service ctkServiceReference prefServiceRef = context->getServiceReference(); berry::IPreferencesService* prefService = NULL; if (prefServiceRef) { prefService = context->getService(prefServiceRef); } if (prefService) { prefService->GetSystemPreferences()->Put("DepartmentLogo", qPrintable(logoFileInfo.absoluteFilePath())); } else { BERRY_WARN << "Preferences service not available, unable to set custom logo."; return false; } } else { BERRY_WARN << "Custom logo at " << logoFileInfo.absoluteFilePath().toStdString() << " does not exist"; return false; } return true; } } // namespace mitk diff --git a/Plugins/org.mitk.gui.qt.application/src/QmitkFileOpenAction.cpp b/Plugins/org.mitk.gui.qt.application/src/QmitkFileOpenAction.cpp index cf4d6b084a..8d452aa8e1 100644 --- a/Plugins/org.mitk.gui.qt.application/src/QmitkFileOpenAction.cpp +++ b/Plugins/org.mitk.gui.qt.application/src/QmitkFileOpenAction.cpp @@ -1,130 +1,132 @@ /*=================================================================== The Medical Imaging Interaction Toolkit (MITK) Copyright (c) German Cancer Research Center, Division of Medical and Biological Informatics. All rights reserved. This software is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See LICENSE.txt or http://www.mitk.org for details. ===================================================================*/ #include "QmitkFileOpenAction.h" #include "internal/org_mitk_gui_qt_application_Activator.h" #include #include #include #include #include +#include #include #include #include #include #include #include #include class QmitkFileOpenActionPrivate { public: QmitkFileOpenActionPrivate() : m_PrefServiceTracker(mitk::PluginActivator::GetContext()) {} void init ( berry::IWorkbenchWindow::Pointer window, QmitkFileOpenAction* action ) { m_PrefServiceTracker.open(); m_Window = window; action->setParent(static_cast(m_Window.Lock()->GetShell()->GetControl())); action->setText("&Open..."); action->setToolTip("Open data files (images, surfaces,...)"); QObject::connect(action, SIGNAL(triggered(bool)), action, SLOT(Run())); } berry::IPreferences::Pointer GetPreferences() const { berry::IPreferencesService* prefService = m_PrefServiceTracker.getService(); if (prefService) { return prefService->GetSystemPreferences()->Node("/General"); } return berry::IPreferences::Pointer(0); } QString getLastFileOpenPath() const { berry::IPreferences::Pointer prefs = GetPreferences(); if(prefs.IsNotNull()) { return QString::fromStdString(prefs->Get("LastFileOpenPath", "")); } return QString(); } void setLastFileOpenPath(const QString& path) const { berry::IPreferences::Pointer prefs = GetPreferences(); if(prefs.IsNotNull()) { prefs->Put("LastFileOpenPath", path.toStdString()); prefs->Flush(); } } berry::IWorkbenchWindow::WeakPtr m_Window; ctkServiceTracker m_PrefServiceTracker; }; QmitkFileOpenAction::QmitkFileOpenAction(berry::IWorkbenchWindow::Pointer window) : QAction(0), d(new QmitkFileOpenActionPrivate) { d->init(window, this); } QmitkFileOpenAction::QmitkFileOpenAction(const QIcon & icon, berry::IWorkbenchWindow::Pointer window) : QAction(0), d(new QmitkFileOpenActionPrivate) { d->init(window, this); this->setIcon(icon); } QmitkFileOpenAction::~QmitkFileOpenAction() { } void QmitkFileOpenAction::Run() { // Ask the user for a list of files to open - QmitkFileOpenDialog dialog; + //QmitkFileOpenDialog dialog; //QmitkFileDialog dialog; - dialog.setFilter(mitk::CoreObjectFactory::GetInstance()->GetFileExtensions()); + QmitkNativeFileOpenDialog dialog; + //dialog.setFilter(mitk::CoreObjectFactory::GetInstance()->GetFileExtensions()); //dialog.show(); - dialog.exec(); + dialog.Exec(); - // QStringList fileNames = dialog.getOpenFileName(); + QStringList fileNames = dialog.GetSelectedFiles(); //QStringList fileNames = QmitkFileDialog::getOpenFileNames(NULL, "Open", //d->getLastFileOpenPath(), // mitk::CoreObjectFactory::GetInstance()->GetFileExtensions()); - //if (fileNames.empty()) - return; + if (fileNames.empty()) + return; - //d->setLastFileOpenPath(fileNames.front()); - //mitk::WorkbenchUtil::LoadFiles(fileNames, d->m_Window.Lock()); + d->setLastFileOpenPath(fileNames.front()); + mitk::WorkbenchUtil::LoadFiles(fileNames, d->m_Window.Lock()); }