15 #include "internal/org_mitk_gui_qt_application_Activator.h" 27 #include <QFileDialog> 28 #include <QMessageBox> 34 auto context = mitk::org_mitk_gui_qt_application_Activator::GetContext();
36 if (
nullptr == context)
41 if (!dataStorageServiceReference)
46 if (
nullptr == dataStorageService)
51 if (dataStorageReference.IsNull())
54 return dataStorageReference->GetDataStorage();
59 if (dataNode.IsNull())
62 auto dataStorage = GetDataStorage();
64 if (dataStorage.IsNull())
67 auto sources = dataStorage->GetSources(dataNode);
69 if (sources.IsNull() || sources->empty())
72 const auto &parentNode = sources->front();
74 if (parentNode.IsNull())
77 auto data = parentNode->GetData();
81 auto pathProperty = data->GetConstProperty(
"path");
83 if (pathProperty.IsNotNull())
84 return QFileInfo(QString::fromStdString(pathProperty->GetValueAsString())).canonicalPath();
87 return GetParentPath(parentNode);
91 class QmitkFileSaveActionPrivate
98 this->SetEnabled(selection);
101 QScopedPointer<berry::ISelectionListener> m_SelectionListener;
105 QmitkFileSaveActionPrivate()
106 : m_SelectionListener(new
berry::NullSelectionChangedAdapter<QmitkFileSaveActionPrivate>(
107 this, &QmitkFileSaveActionPrivate::HandleSelectionChanged))
111 ~QmitkFileSaveActionPrivate()
113 if (!m_Window.Expired())
115 m_Window.Lock()->GetSelectionService()->RemoveSelectionListener(m_SelectionListener.data());
124 m_Action->setText(
"&Save...");
125 m_Action->setToolTip(
"Save data objects (images, surfaces,...)");
132 QObject::connect(m_Action, SIGNAL(triggered(
bool)), m_Action, SLOT(
Run()));
138 if (prefService !=
nullptr)
145 QString GetLastFileSavePath()
const 150 return prefs->Get(
"LastFileSavePath",
"");
155 void SetLastFileSavePath(
const QString& path)
const 160 prefs->Put(
"LastFileSavePath", path);
168 if (nodeSelection.
IsNotNull() && !selection->IsEmpty())
171 std::list<mitk::DataNode::Pointer> dataNodes = nodeSelection->GetSelectedDataNodes();
172 for (std::list<mitk::DataNode::Pointer>::const_iterator nodeIter = dataNodes.begin(), nodeIterEnd = dataNodes.end(); nodeIter != nodeIterEnd; ++nodeIter)
174 if ((*nodeIter)->GetData() !=
nullptr)
180 m_Action->setEnabled(enable);
184 m_Action->setEnabled(
false);
193 : QAction(tr(
"Save..."))
194 , d(new QmitkFileSaveActionPrivate)
200 : QAction(tr(
"Save..."))
201 , d(new QmitkFileSaveActionPrivate)
208 : QAction(tr(
"Save..."))
209 , d(new QmitkFileSaveActionPrivate)
211 d->Init(window,
this);
223 if (selection.
IsNull() || selection->IsEmpty())
225 MITK_ERROR <<
"Assertion failed: data node selection is nullptr or empty";
229 std::list<mitk::DataNode::Pointer> dataNodes = selection->GetSelectedDataNodes();
231 std::vector<const mitk::BaseData*> data;
233 for (std::list<mitk::DataNode::Pointer>::const_iterator nodeIter = dataNodes.begin(), nodeIterEnd = dataNodes.end(); nodeIter != nodeIterEnd; ++nodeIter)
235 data.push_back((*nodeIter)->GetData());
237 (*nodeIter)->GetStringProperty(
"name", name);
238 names.push_back(QString::fromStdString(name));
243 if (1 == data.size())
245 if (
nullptr != data[0])
247 auto pathProperty = data[0]->GetConstProperty(
"path");
249 if (pathProperty.IsNotNull())
250 path = QFileInfo(QString::fromStdString(pathProperty->GetValueAsString())).canonicalPath();
254 path = GetParentPath(dataNodes.front());
258 path = d->GetLastFileSavePath();
262 auto setPathProperty =
true;
263 auto fileNames =
QmitkIOUtil::Save(data, names, path, d->m_Action->parentWidget(), setPathProperty);
265 if (!fileNames.empty())
266 d->SetLastFileSavePath(QFileInfo(fileNames.back()).absolutePath());
SmartPointer< Other > Cast() const
virtual IDataStorageReference::Pointer GetDataStorage() const =0
virtual SmartPointer< IPreferences > GetSystemPreferences()=0
virtual ISelection::ConstPointer GetSelection() const =0
berry::SmartPointer< Self > Pointer
QmitkFileSaveAction(berry::IWorkbenchWindow::Pointer window)
An object of this class represents an exception of MITK. Please don't instantiate exceptions manually...
virtual void AddSelectionListener(ISelectionListener *listener)=0
static QString Save(const mitk::BaseData *data, const QString &defaultBaseName, const QString &defaultPath=QString(), QWidget *parent=nullptr, bool setPathProperty=false)
~QmitkFileSaveAction() override
void Run(berry::IWorkbenchPartSite::Pointer workbenchPartSite, mitk::DataStorage::Pointer dataStorage)
berry::SmartPointer< Self > Pointer
ObjectType * GetPointer() const