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