Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
QmitkFileSaveAction.cpp
Go to the documentation of this file.
1 /*===================================================================
2 
3 The Medical Imaging Interaction Toolkit (MITK)
4 
5 Copyright (c) German Cancer Research Center,
6 Division of Medical and Biological Informatics.
7 All rights reserved.
8 
9 This software is distributed WITHOUT ANY WARRANTY; without
10 even the implied warranty of MERCHANTABILITY or FITNESS FOR
11 A PARTICULAR PURPOSE.
12 
13 See LICENSE.txt or http://www.mitk.org for details.
14 
15 ===================================================================*/
16 
17 #include "QmitkFileSaveAction.h"
18 
19 #include "internal/org_mitk_gui_qt_application_Activator.h"
20 
21 #include <mitkWorkbenchUtil.h>
22 #include <mitkDataNodeSelection.h>
23 
24 #include <berryISelectionService.h>
26 #include <berryIPreferences.h>
27 
28 #include <QmitkIOUtil.h>
29 
30 #include <QFileDialog>
31 #include <QMessageBox>
32 
33 class QmitkFileSaveActionPrivate
34 {
35 private:
36 
37  void HandleSelectionChanged(const berry::IWorkbenchPart::Pointer& /*part*/,
38  const berry::ISelection::ConstPointer& selection)
39  {
40  this->setEnabled(selection);
41  }
42 
43  QScopedPointer<berry::ISelectionListener> m_SelectionListener;
44 
45 public:
46 
47  QmitkFileSaveActionPrivate()
48  : m_SelectionListener(new berry::NullSelectionChangedAdapter<QmitkFileSaveActionPrivate>(
49  this, &QmitkFileSaveActionPrivate::HandleSelectionChanged))
50  {
51  }
52 
53  ~QmitkFileSaveActionPrivate()
54  {
55  if (!m_Window.Expired())
56  {
57  m_Window.Lock()->GetSelectionService()->RemoveSelectionListener(m_SelectionListener.data());
58  }
59  }
60 
61  void init ( berry::IWorkbenchWindow* window, QmitkFileSaveAction* action )
62  {
63  m_Window = berry::IWorkbenchWindow::Pointer(window);
64  m_Action = action;
65 
66  action->setText("&Save...");
67  action->setToolTip("Save data objects (images, surfaces,...)");
68 
69  berry::ISelectionService* selectionService = m_Window.Lock()->GetSelectionService();
70  setEnabled(selectionService->GetSelection());
71 
72  selectionService->AddSelectionListener(m_SelectionListener.data());
73 
74  QObject::connect(action, SIGNAL(triggered(bool)), action, SLOT(Run()));
75  }
76 
77  berry::IPreferences::Pointer GetPreferences() const
78  {
79  berry::IPreferencesService* prefService = mitk::PluginActivator::GetInstance()->GetPreferencesService();
80  if (prefService != nullptr)
81  {
82  return prefService->GetSystemPreferences()->Node("/General");
83  }
85  }
86 
87  QString getLastFileSavePath() const
88  {
89  berry::IPreferences::Pointer prefs = GetPreferences();
90  if(prefs.IsNotNull())
91  {
92  return prefs->Get("LastFileSavePath", "");
93  }
94  return QString();
95  }
96 
97  void setLastFileSavePath(const QString& path) const
98  {
99  berry::IPreferences::Pointer prefs = GetPreferences();
100  if(prefs.IsNotNull())
101  {
102  prefs->Put("LastFileSavePath", path);
103  prefs->Flush();
104  }
105  }
106 
107  void setEnabled(berry::ISelection::ConstPointer selection)
108  {
109  mitk::DataNodeSelection::ConstPointer nodeSelection = selection.Cast<const mitk::DataNodeSelection>();
110  if (nodeSelection.IsNotNull() && !selection->IsEmpty())
111  {
112  bool enable = false;
113  std::list<mitk::DataNode::Pointer> dataNodes = nodeSelection->GetSelectedDataNodes();
114  for (std::list<mitk::DataNode::Pointer>::const_iterator nodeIter = dataNodes.begin(),
115  nodeIterEnd = dataNodes.end(); nodeIter != nodeIterEnd; ++nodeIter)
116  {
117  if ((*nodeIter)->GetData() != NULL)
118  {
119  enable = true;
120  break;
121  }
122  }
123  m_Action->setEnabled(enable);
124  }
125  else
126  {
127  m_Action->setEnabled(false);
128  }
129  }
130 
132  QAction* m_Action;
133 };
134 
136  : QAction(0), d(new QmitkFileSaveActionPrivate)
137 {
138  d->init(window.GetPointer(), this);
139 }
140 
142  : QAction(0), d(new QmitkFileSaveActionPrivate)
143 {
144  d->init(window.GetPointer(), this);
145  this->setIcon(icon);
146 }
147 
149  : QAction(0), d(new QmitkFileSaveActionPrivate)
150 {
151  d->init(window, this);
152  this->setIcon(icon);
153 }
154 
156 {
157 }
158 
160 {
161  // Get the list of selected base data objects
162  mitk::DataNodeSelection::ConstPointer selection = d->m_Window.Lock()->GetSelectionService()->GetSelection().Cast<const mitk::DataNodeSelection>();
163  if (selection.IsNull() || selection->IsEmpty())
164  {
165  MITK_ERROR << "Assertion failed: data node selection is NULL or empty";
166  return;
167  }
168 
169  std::list<mitk::DataNode::Pointer> dataNodes = selection->GetSelectedDataNodes();
170 
171  std::vector<const mitk::BaseData*> data;
172  QStringList names;
173  for (std::list<mitk::DataNode::Pointer>::const_iterator nodeIter = dataNodes.begin(),
174  nodeIterEnd = dataNodes.end(); nodeIter != nodeIterEnd; ++nodeIter)
175  {
176  data.push_back((*nodeIter)->GetData());
177  std::string name;
178  (*nodeIter)->GetStringProperty("name", name);
179  names.push_back(QString::fromStdString(name));
180  }
181 
182  try
183  {
184  QStringList fileNames = QmitkIOUtil::Save(data, names, d->getLastFileSavePath(),
185  d->m_Action->parentWidget());
186  if (!fileNames.empty())
187  {
188  d->setLastFileSavePath(QFileInfo(fileNames.back()).absolutePath());
189  }
190  }
191  catch (const mitk::Exception& e)
192  {
193  MITK_INFO << e;
194  return;
195  }
196 }
#define MITK_INFO
Definition: mitkLogMacros.h:22
#define MITK_ERROR
Definition: mitkLogMacros.h:24
virtual SmartPointer< IPreferences > GetSystemPreferences()=0
virtual ISelection::ConstPointer GetSelection() const =0
berry::SmartPointer< Self > Pointer
Definition: berryObject.h:88
QmitkFileSaveAction(berry::IWorkbenchWindow::Pointer window)
An object of this class represents an exception of MITK. Please don't instantiate exceptions manually...
Definition: mitkException.h:49
virtual void AddSelectionListener(ISelectionListener *listener)=0
ObjectType * GetPointer() const
static QString Save(const mitk::BaseData *data, const QString &defaultBaseName, const QString &defaultPath=QString(), QWidget *parent=NULL)
SmartPointer< Other > Cast() const
berry::SmartPointer< Self > Pointer