diff --git a/Plugins/org.mitk.gui.qt.application/src/QmitkFileSaveAction.cpp b/Plugins/org.mitk.gui.qt.application/src/QmitkFileSaveAction.cpp index 01a02ec881..1fa0818eb9 100644 --- a/Plugins/org.mitk.gui.qt.application/src/QmitkFileSaveAction.cpp +++ b/Plugins/org.mitk.gui.qt.application/src/QmitkFileSaveAction.cpp @@ -1,209 +1,277 @@ /*=================================================================== 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 "QmitkFileSaveAction.h" #include "internal/org_mitk_gui_qt_application_Activator.h" #include #include +#include #include #include #include #include #include #include +namespace +{ + mitk::DataStorage::Pointer GetDataStorage() + { + auto context = mitk::org_mitk_gui_qt_application_Activator::GetContext(); + + if (nullptr == context) + return nullptr; + + auto dataStorageServiceReference = context->getServiceReference(); + + if (!dataStorageServiceReference) + return nullptr; + + auto dataStorageService = context->getService(dataStorageServiceReference); + + if (nullptr == dataStorageService) + return nullptr; + + auto dataStorageReference = dataStorageService->GetDataStorage(); + + if (dataStorageReference.IsNull()) + return nullptr; + + return dataStorageReference->GetDataStorage(); + } + + QString GetParentPath(mitk::DataNode::Pointer dataNode) + { + if (dataNode.IsNull()) + return ""; + + auto dataStorage = GetDataStorage(); + + if (dataStorage.IsNull()) + return ""; + + auto sources = dataStorage->GetSources(dataNode); + + if (sources.IsNull() || sources->empty()) + return ""; + + const auto &parentNode = sources->front(); + + if (parentNode.IsNull()) + return ""; + + auto data = parentNode->GetData(); + + if (nullptr != data) + { + auto pathProperty = data->GetConstProperty("path"); + + if (pathProperty.IsNotNull()) + return QFileInfo(QString::fromStdString(pathProperty->GetValueAsString())).canonicalPath(); + } + + return GetParentPath(parentNode); + } +} + class QmitkFileSaveActionPrivate { private: void HandleSelectionChanged(const berry::IWorkbenchPart::Pointer& /*part*/, const berry::ISelection::ConstPointer& selection) { this->setEnabled(selection); } QScopedPointer m_SelectionListener; public: QmitkFileSaveActionPrivate() : m_SelectionListener(new berry::NullSelectionChangedAdapter( this, &QmitkFileSaveActionPrivate::HandleSelectionChanged)) { } ~QmitkFileSaveActionPrivate() { if (!m_Window.Expired()) { m_Window.Lock()->GetSelectionService()->RemoveSelectionListener(m_SelectionListener.data()); } } void init ( berry::IWorkbenchWindow* window, QmitkFileSaveAction* action ) { m_Window = berry::IWorkbenchWindow::Pointer(window); m_Action = action; action->setText("&Save..."); action->setToolTip("Save data objects (images, surfaces,...)"); berry::ISelectionService* selectionService = m_Window.Lock()->GetSelectionService(); setEnabled(selectionService->GetSelection()); selectionService->AddSelectionListener(m_SelectionListener.data()); QObject::connect(action, SIGNAL(triggered(bool)), action, SLOT(Run())); } berry::IPreferences::Pointer GetPreferences() const { berry::IPreferencesService* prefService = mitk::PluginActivator::GetInstance()->GetPreferencesService(); if (prefService != nullptr) { return prefService->GetSystemPreferences()->Node("/General"); } return berry::IPreferences::Pointer(nullptr); } QString getLastFileSavePath() const { berry::IPreferences::Pointer prefs = GetPreferences(); if(prefs.IsNotNull()) { return prefs->Get("LastFileSavePath", ""); } return QString(); } void setLastFileSavePath(const QString& path) const { berry::IPreferences::Pointer prefs = GetPreferences(); if(prefs.IsNotNull()) { prefs->Put("LastFileSavePath", path); prefs->Flush(); } } void setEnabled(berry::ISelection::ConstPointer selection) { mitk::DataNodeSelection::ConstPointer nodeSelection = selection.Cast(); if (nodeSelection.IsNotNull() && !selection->IsEmpty()) { bool enable = false; std::list dataNodes = nodeSelection->GetSelectedDataNodes(); for (std::list::const_iterator nodeIter = dataNodes.begin(), nodeIterEnd = dataNodes.end(); nodeIter != nodeIterEnd; ++nodeIter) { if ((*nodeIter)->GetData() != nullptr) { enable = true; break; } } m_Action->setEnabled(enable); } else { m_Action->setEnabled(false); } } berry::IWorkbenchWindow::WeakPtr m_Window; QAction* m_Action; }; QmitkFileSaveAction::QmitkFileSaveAction(berry::IWorkbenchWindow::Pointer window) : QAction(nullptr), d(new QmitkFileSaveActionPrivate) { d->init(window.GetPointer(), this); } QmitkFileSaveAction::QmitkFileSaveAction(const QIcon & icon, berry::IWorkbenchWindow::Pointer window) : QAction(nullptr), d(new QmitkFileSaveActionPrivate) { d->init(window.GetPointer(), this); this->setIcon(icon); } QmitkFileSaveAction::QmitkFileSaveAction(const QIcon& icon, berry::IWorkbenchWindow* window) : QAction(nullptr), d(new QmitkFileSaveActionPrivate) { d->init(window, this); this->setIcon(icon); } QmitkFileSaveAction::~QmitkFileSaveAction() { } void QmitkFileSaveAction::Run() { // Get the list of selected base data objects mitk::DataNodeSelection::ConstPointer selection = d->m_Window.Lock()->GetSelectionService()->GetSelection().Cast(); if (selection.IsNull() || selection->IsEmpty()) { MITK_ERROR << "Assertion failed: data node selection is nullptr or empty"; return; } std::list dataNodes = selection->GetSelectedDataNodes(); std::vector data; QStringList names; for (std::list::const_iterator nodeIter = dataNodes.begin(), nodeIterEnd = dataNodes.end(); nodeIter != nodeIterEnd; ++nodeIter) { data.push_back((*nodeIter)->GetData()); std::string name; (*nodeIter)->GetStringProperty("name", name); names.push_back(QString::fromStdString(name)); } QString path; if (1 == data.size()) { - auto pathProperty = data[0]->GetConstProperty("path"); + if (nullptr != data[0]) + { + auto pathProperty = data[0]->GetConstProperty("path"); + + if (pathProperty.IsNotNull()) + path = QFileInfo(QString::fromStdString(pathProperty->GetValueAsString())).canonicalPath(); + } - if (pathProperty.IsNotNull()) - path = QFileInfo(QString::fromStdString(pathProperty->GetValueAsString())).canonicalPath(); + if (path.isEmpty()) + path = GetParentPath(dataNodes.front()); } if (path.isEmpty()) path = d->getLastFileSavePath(); try { QStringList fileNames = QmitkIOUtil::Save(data, names, path, d->m_Action->parentWidget()); if (!fileNames.empty()) { d->setLastFileSavePath(QFileInfo(fileNames.back()).absolutePath()); } } catch (const mitk::Exception& e) { MITK_INFO << e; return; } }