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