Medical Imaging Interaction Toolkit  2018.4.99-389bf124
Medical Imaging Interaction Toolkit
QmitkExtFileSaveProjectAction.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 (DKFZ)
6 All rights reserved.
7 
8 Use of this source code is governed by a 3-clause BSD license that can be
9 found in the LICENSE file.
10 
11 ============================================================================*/
12 
14 
15 #include "internal/QmitkCommonExtPlugin.h"
16 
17 #include <QFileDialog>
18 #include <QMessageBox>
19 #include <QApplication>
20 
21 #include <mitkSceneIO.h>
22 #include <mitkProgressBar.h>
23 #include <mitkNodePredicateNot.h>
25 #include <mitkProperties.h>
26 
27 #include <mitkCoreObjectFactory.h>
30 #include <berryIEditorPart.h>
31 #include <berryIWorkbenchPage.h>
32 #include <berryIWorkbenchWindow.h>
34 #include "berryPlatform.h"
35 
36 
38  : QAction(nullptr)
39  , m_Window(nullptr)
40 {
41  this->Init(window.GetPointer());
42 }
43 
45  : QAction(nullptr)
46  , m_Window(nullptr)
47 {
48  this->Init(window);
49 }
50 
51 void QmitkExtFileSaveProjectAction::Init(berry::IWorkbenchWindow* window)
52 {
53  m_Window = window;
54  this->setText("&Save Project...");
55  this->setToolTip("Save content of Data Manager as a .mitk project file");
56 
57  this->connect(this, SIGNAL(triggered(bool)), this, SLOT(Run()));
58 }
59 
60 
62 {
63  try
64  {
68  static QString m_LastPath;
69 
71 
72  {
73  ctkPluginContext* context = QmitkCommonExtPlugin::getContext();
74  mitk::IDataStorageService* dss = nullptr;
75  ctkServiceReference dsServiceRef = context->getServiceReference<mitk::IDataStorageService>();
76  if (dsServiceRef)
77  {
78  dss = context->getService<mitk::IDataStorageService>(dsServiceRef);
79  }
80 
81  if (!dss)
82  {
83  QString msg = "IDataStorageService service not available. Unable to open files.";
84  MITK_WARN << msg.toStdString();
85  QMessageBox::warning(QApplication::activeWindow(), "Unable to open files", msg);
86  return;
87  }
88 
89  // Get the active data storage (or the default one, if none is active)
90  dsRef = dss->GetDataStorage();
91  context->ungetService(dsServiceRef);
92  }
93 
94  mitk::DataStorage::Pointer storage = dsRef->GetDataStorage();
95 
96  QString dialogTitle = "Save MITK Scene (%1)";
97  QString fileName = QFileDialog::getSaveFileName(nullptr,
98  dialogTitle.arg(dsRef->GetLabel()),
99  m_LastPath,
100  "MITK scene files (*.mitk)",
101  nullptr );
102 
103  if (fileName.isEmpty() )
104  return;
105 
106  // remember the location
107  m_LastPath = fileName;
108 
109  if ( fileName.right(5) != ".mitk" )
110  fileName += ".mitk";
111 
112  mitk::SceneIO::Pointer sceneIO = mitk::SceneIO::New();
113 
115 
116  /* Build list of nodes that should be saved */
117  mitk::NodePredicateNot::Pointer isNotHelperObject =
119  mitk::DataStorage::SetOfObjects::ConstPointer nodesToBeSaved = storage->GetSubset(isNotHelperObject);
120 
121  if ( !sceneIO->SaveScene( nodesToBeSaved, storage, fileName.toStdString() ) )
122  {
123  QMessageBox::information(nullptr,
124  "Scene saving",
125  "Scene could not be written completely. Please check the log.",
126  QMessageBox::Ok);
127 
128  }
130 
131  mitk::SceneIO::FailedBaseDataListType::ConstPointer failedNodes = sceneIO->GetFailedNodes();
132  if (!failedNodes->empty())
133  {
134  std::stringstream ss;
135  ss << "The following nodes could not be serialized:" << std::endl;
136  for ( mitk::SceneIO::FailedBaseDataListType::const_iterator iter = failedNodes->begin();
137  iter != failedNodes->end();
138  ++iter )
139  {
140  ss << " - ";
141  if ( mitk::BaseData* data =(*iter)->GetData() )
142  {
143  ss << data->GetNameOfClass();
144  }
145  else
146  {
147  ss << "(nullptr)";
148  }
149 
150  ss << " contained in node '" << (*iter)->GetName() << "'" << std::endl;
151  }
152 
153  MITK_WARN << ss.str();
154  }
155 
156  mitk::PropertyList::ConstPointer failedProperties = sceneIO->GetFailedProperties();
157  if (!failedProperties->GetMap()->empty())
158  {
159  std::stringstream ss;
160  ss << "The following properties could not be serialized:" << std::endl;
161  const mitk::PropertyList::PropertyMap* propmap = failedProperties->GetMap();
162  for ( mitk::PropertyList::PropertyMap::const_iterator iter = propmap->begin();
163  iter != propmap->end();
164  ++iter )
165  {
166  ss << " - " << iter->second->GetNameOfClass() << " associated to key '" << iter->first << "'" << std::endl;
167  }
168 
169  MITK_WARN << ss.str();
170  }
171  }
172  catch (std::exception& e)
173  {
174  MITK_ERROR << "Exception caught during scene saving: " << e.what();
175  }
176 }
void Progress(unsigned int steps=1)
Sets the current amount of progress to current progress + steps.
QmitkExtFileSaveProjectAction(berry::SmartPointer< berry::IWorkbenchWindow > window)
Base of all data objects.
Definition: mitkBaseData.h:37
#define MITK_ERROR
Definition: mitkLogMacros.h:20
virtual IDataStorageReference::Pointer GetDataStorage() const =0
static ProgressBar * GetInstance()
static method to get the GUI dependent ProgressBar-instance so the methods for steps to do and progre...
static Pointer New()
static Pointer New()
#define MITK_WARN
Definition: mitkLogMacros.h:19
std::map< std::string, BaseProperty::Pointer > PropertyMap
static Pointer New(const mitk::NodePredicateBase *_arg)
static Pointer New(const char *_arg)
void AddStepsToDo(unsigned int steps)
Adds steps to totalSteps.
ObjectType * GetPointer() const