Medical Imaging Interaction Toolkit  2018.4.99-9a29ffc6
Medical Imaging Interaction Toolkit
QmitkFileOpenAction.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 
13 #include "QmitkFileOpenAction.h"
14 
15 #include "internal/org_mitk_gui_qt_application_Activator.h"
16 
19 #include <mitkWorkbenchUtil.h>
20 
21 #include <QmitkIOUtil.h>
22 
23 #include <berryIPreferences.h>
24 
25 #include <QFileDialog>
26 
27 namespace
28 {
29  mitk::DataStorage::Pointer GetDataStorage()
30  {
31  auto context = mitk::org_mitk_gui_qt_application_Activator::GetContext();
32 
33  if (nullptr == context)
34  return nullptr;
35 
36  auto dataStorageServiceReference = context->getServiceReference<mitk::IDataStorageService>();
37 
38  if (!dataStorageServiceReference)
39  return nullptr;
40 
41  auto dataStorageService = context->getService<mitk::IDataStorageService>(dataStorageServiceReference);
42 
43  if (nullptr == dataStorageService)
44  return nullptr;
45 
46  auto dataStorageReference = dataStorageService->GetDataStorage();
47 
48  if (dataStorageReference.IsNull())
49  return nullptr;
50 
51  return dataStorageReference->GetDataStorage();
52  }
53 
54  mitk::DataNode::Pointer GetFirstSelectedNode()
55  {
56  auto dataStorage = GetDataStorage();
57 
58  if (dataStorage.IsNull())
59  return nullptr;
60 
61  auto selectedNodes = dataStorage->GetSubset(mitk::NodePredicateProperty::New("selected", mitk::BoolProperty::New(true)));
62 
63  if (selectedNodes->empty())
64  return nullptr;
65 
66  return selectedNodes->front();
67  }
68 
69  QString GetPathOfFirstSelectedNode()
70  {
71  auto firstSelectedNode = GetFirstSelectedNode();
72 
73  if (firstSelectedNode.IsNull())
74  return "";
75 
76  auto data = firstSelectedNode->GetData();
77 
78  if (nullptr == data)
79  return "";
80 
81  auto pathProperty = data->GetConstProperty("path");
82 
83  if (pathProperty.IsNull())
84  return "";
85 
86  return QFileInfo(QString::fromStdString(pathProperty->GetValueAsString())).canonicalPath();
87  }
88 }
89 
90 class QmitkFileOpenActionPrivate
91 {
92 public:
93 
94  void Init(berry::IWorkbenchWindow* window, QmitkFileOpenAction* action)
95  {
96  m_Window = window;
97  action->setText("&Open File...");
98  action->setToolTip("Open data files (images, surfaces,...)");
99 
100  QObject::connect(action, SIGNAL(triggered(bool)), action, SLOT(Run()));
101  }
102 
103  berry::IPreferences::Pointer GetPreferences() const
104  {
105  berry::IPreferencesService* prefService = mitk::PluginActivator::GetInstance()->GetPreferencesService();
106  if (prefService != nullptr)
107  {
108  return prefService->GetSystemPreferences()->Node("/General");
109  }
110  return berry::IPreferences::Pointer(nullptr);
111  }
112 
113  QString GetLastFileOpenPath() const
114  {
115  berry::IPreferences::Pointer prefs = GetPreferences();
116  if (prefs.IsNotNull())
117  {
118  return prefs->Get("LastFileOpenPath", "");
119  }
120  return QString();
121  }
122 
123  void SetLastFileOpenPath(const QString& path) const
124  {
125  berry::IPreferences::Pointer prefs = GetPreferences();
126  if (prefs.IsNotNull())
127  {
128  prefs->Put("LastFileOpenPath", path);
129  prefs->Flush();
130  }
131  }
132 
133  bool GetOpenEditor() const
134  {
135  berry::IPreferences::Pointer prefs = GetPreferences();
136  if (prefs.IsNotNull())
137  {
138  return prefs->GetBool("OpenEditor", true);
139  }
140  return true;
141  }
142 
143  berry::IWorkbenchWindow* m_Window;
144 };
145 
147  : QAction(nullptr)
148  , d(new QmitkFileOpenActionPrivate)
149 {
150  d->Init(window.GetPointer(), this);
151 }
152 
154  : QAction(nullptr)
155  , d(new QmitkFileOpenActionPrivate)
156 {
157  d->Init(window.GetPointer(), this);
158  setIcon(icon);
159 }
160 
162  : QAction(nullptr), d(new QmitkFileOpenActionPrivate)
163 {
164  d->Init(window, this);
165  setIcon(icon);
166 }
167 
169 {
170 }
171 
173 {
174  auto path = GetPathOfFirstSelectedNode();
175 
176  if (path.isEmpty())
177  path = d->GetLastFileOpenPath();
178 
179  // Ask the user for a list of files to open
180  QStringList fileNames = QFileDialog::getOpenFileNames(nullptr, "Open",
181  path,
183 
184  if (fileNames.empty())
185  {
186  return;
187  }
188 
189  d->SetLastFileOpenPath(fileNames.front());
190  mitk::WorkbenchUtil::LoadFiles(fileNames, berry::IWorkbenchWindow::Pointer(d->m_Window), d->GetOpenEditor());
191 }
virtual IDataStorageReference::Pointer GetDataStorage() const =0
virtual SmartPointer< IPreferences > GetSystemPreferences()=0
static Pointer New()
static void LoadFiles(const QStringList &fileNames, berry::IWorkbenchWindow::Pointer wnd, bool openEditor=true)
static QString GetFileOpenFilterString()
GetFilterString.
static Pointer New(const char *_arg)
void Run(berry::IWorkbenchPartSite::Pointer workbenchPartSite, mitk::DataStorage::Pointer dataStorage)
berry::SmartPointer< Self > Pointer
QmitkFileOpenAction(berry::IWorkbenchWindow::Pointer window)
ObjectType * GetPointer() const