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
QmitkCloseProjectAction.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 #include "internal/org_mitk_gui_qt_application_Activator.h"
19 
22 #include <mitkNodePredicateNot.h>
24 #include <mitkProperties.h>
25 
26 #include <berryIEditorReference.h>
27 #include <berryIWorkbenchPage.h>
28 #include <berryIWorkbenchWindow.h>
29 
30 #include <QMessageBox>
31 
33  : QAction(0)
34  , m_Window(nullptr)
35 {
36  this->init(window.GetPointer());
37 }
38 
40  : QAction(0)
41  , m_Window(nullptr)
42 {
43  this->setIcon(icon);
44  this->init(window.GetPointer());
45 }
46 
48  : QAction(0)
49  , m_Window(nullptr)
50 {
51  this->init(window);
52 }
53 
55  : QAction(0)
56  , m_Window(nullptr)
57 {
58  this->setIcon(icon);
59  this->init(window);
60 }
61 
62 void QmitkCloseProjectAction::init(berry::IWorkbenchWindow* window)
63 {
64  m_Window = window;
65  this->setText("&Close Project...");
66  this->setToolTip("Close Project will remove all data objects from the application. This will free up the memory that is used by the data.");
67  this->connect(this, SIGNAL(triggered(bool)), this, SLOT(Run()));
68 }
69 
71 {
72  try
73  {
74  ctkPluginContext* context = mitk::PluginActivator::GetContext();
76  ctkServiceReference dsRef = context->getServiceReference<mitk::IDataStorageService>();
77  if (dsRef)
78  {
79  dss = context->getService<mitk::IDataStorageService>(dsRef);
80  }
81 
82  if (!dss)
83  {
84  MITK_WARN << "IDataStorageService service not available. Unable to close project.";
85  context->ungetService(dsRef);
86  return;
87  }
88 
90  if (dataStorageRef.IsNull())
91  {
92  // No active data storage set (i.e. not editor with a DataStorageEditorInput is active).
93  dataStorageRef = dss->GetDefaultDataStorage();
94  }
95 
96  mitk::DataStorage::Pointer dataStorage = dataStorageRef->GetDataStorage();
97  if (dataStorage.IsNull())
98  {
99  MITK_WARN << "No data storage available. Cannot close project.";
100  return;
101  }
102 
103  //check if we got the default datastorage and if there is anything else then helper object in the storage
104  if(dataStorageRef->IsDefault() &&
105  dataStorage->GetSubset(mitk::NodePredicateNot::New(mitk::NodePredicateProperty::New("helper object", mitk::BoolProperty::New(true))))->empty())
106  {
107  return;
108  }
109 
110  /* Ask, if the user is sure about that */
111  QString msg = "Are you sure that you want to close the current project (%1)?\nThis will remove all data objects.";
112  if (QMessageBox::question(NULL, "Remove all data?", msg.arg(dataStorageRef->GetLabel()),
113  QMessageBox::Yes | QMessageBox::No) != QMessageBox::Yes)
114  {
115  return;
116  }
117 
118  /* Remove everything */
119  mitk::DataStorage::SetOfObjects::ConstPointer nodesToRemove = dataStorage->GetAll();
120  dataStorage->Remove(nodesToRemove);
121 
122  // Remove the datastorage from the data storage service
123  dss->RemoveDataStorageReference(dataStorageRef);
124 
125  // Close all editors with this data storage as input
127  QList<berry::IEditorReference::Pointer> dsEditors =
128  m_Window->GetActivePage()->FindEditors(dsInput, QString(), berry::IWorkbenchPage::MATCH_INPUT);
129 
130  if (!dsEditors.empty())
131  {
132  QList<berry::IEditorReference::Pointer> editorsToClose = dsEditors;
133  m_Window->GetActivePage()->CloseEditors(editorsToClose, false);
134  }
135  }
136  catch (std::exception& e)
137  {
138  MITK_ERROR << "Exception caught during closing project: " << e.what();
139  QMessageBox::warning(NULL, "Error", QString("An error occurred during Close Project: %1").arg(e.what()));
140  }
141 }
virtual IDataStorageReference::Pointer GetDefaultDataStorage() const =0
virtual IDataStorageReference::Pointer GetActiveDataStorage() const =0
#define MITK_ERROR
Definition: mitkLogMacros.h:24
static const int MATCH_INPUT
virtual SmartPointer< IWorkbenchPage > GetActivePage() const override=0
An editor input based on a mitk::DataStorage.
itk::SmartPointer< const Self > ConstPointer
static Pointer New()
#define MITK_WARN
Definition: mitkLogMacros.h:23
static Pointer New(const mitk::NodePredicateBase *_arg)
static Pointer New(const char *_arg)
QmitkCloseProjectAction(berry::SmartPointer< berry::IWorkbenchWindow > window)
ObjectType * GetPointer() const
virtual bool RemoveDataStorageReference(IDataStorageReference::Pointer dataStorageRef)=0