Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
mitkWorkbenchUtil.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 
17 
18 #include "mitkWorkbenchUtil.h"
19 
20 #include <berryPlatform.h>
21 #include <berryPlatformUI.h>
22 #include <berryIEditorRegistry.h>
23 #include <berryCoreException.h>
25 #include <berryIPreferences.h>
26 
29 #include "mitkRenderingManager.h"
30 #include "mitkIRenderWindowPart.h"
31 #include "mitkIRenderingManager.h"
32 
33 #include "mitkProperties.h"
34 #include "mitkNodePredicateData.h"
35 #include "mitkNodePredicateNot.h"
37 #include "mitkCoreObjectFactory.h"
38 
39 #include "QmitkIOUtil.h"
40 
41 #include <QMessageBox>
42 #include <QApplication>
43 #include <QDateTime>
44 
45 #include "internal/org_mitk_gui_common_Activator.h"
46 
47 namespace mitk {
48 
49 struct WorkbenchUtilPrivate {
50 
65  static berry::IEditorDescriptor::Pointer GetEditorDescriptor(const QString& name,
66  berry::IEditorRegistry* editorReg,
67  berry::IEditorDescriptor::Pointer defaultDescriptor)
68  {
69 
70  if (defaultDescriptor.IsNotNull())
71  {
72  return defaultDescriptor;
73  }
74 
75  berry::IEditorDescriptor::Pointer editorDesc = defaultDescriptor;
76 
77  // next check the OS for in-place editor (OLE on Win32)
78  if (editorReg->IsSystemInPlaceEditorAvailable(name))
79  {
81  }
82 
83  // next check with the OS for an external editor
84  if (editorDesc.IsNull() && editorReg->IsSystemExternalEditorAvailable(name))
85  {
87  }
88 
89  // if no valid editor found, bail out
90  if (editorDesc.IsNull())
91  {
92  throw berry::PartInitException("No editor found");
93  }
94 
95  return editorDesc;
96  }
97 };
98 // //! [UtilLoadFiles]
99 void WorkbenchUtil::LoadFiles(const QStringList &fileNames, berry::IWorkbenchWindow::Pointer window, bool openEditor)
100 // //! [UtilLoadFiles]
101 {
102  if (fileNames.empty())
103  return;
104 
106 
107  {
108  ctkPluginContext* context = mitk::PluginActivator::GetContext();
109  mitk::IDataStorageService* dss = 0;
110  ctkServiceReference dsRef = context->getServiceReference<mitk::IDataStorageService>();
111  if (dsRef)
112  {
113  dss = context->getService<mitk::IDataStorageService>(dsRef);
114  }
115 
116  if (!dss)
117  {
118  QString msg = "IDataStorageService service not available. Unable to open files.";
119  MITK_WARN << msg.toStdString();
120  QMessageBox::warning(QApplication::activeWindow(), "Unable to open files", msg);
121  return;
122  }
123 
124  // Get the active data storage (or the default one, if none is active)
125  dataStorageRef = dss->GetDataStorage();
126  context->ungetService(dsRef);
127  }
128 
129  mitk::DataStorage::Pointer dataStorage = dataStorageRef->GetDataStorage();
130 
131  // Do the actual work of loading the data into the data storage
132 
133  // Turn off ASSERT
134  #if defined(_MSC_VER) && !defined(NDEBUG) && defined(_DEBUG) && defined(_CRT_ERROR)
135  int lastCrtReportType = _CrtSetReportMode( _CRT_ASSERT, _CRTDBG_MODE_DEBUG );
136  #endif
137 
139  try
140  {
141  data = QmitkIOUtil::Load(fileNames, *dataStorage);
142  }
143  catch (const mitk::Exception& e)
144  {
145  MITK_INFO << e;
146  return;
147  }
148  const bool dsmodified = !data->empty();
149 
150  // Set ASSERT status back to previous status.
151  #if defined(_MSC_VER) && !defined(NDEBUG) && defined(_DEBUG) && defined(_CRT_ERROR)
152  if (lastCrtReportType)
153  _CrtSetReportMode( _CRT_ASSERT, lastCrtReportType );
154  #endif
155 
156  // Check if there is an open perspective. If not, open the default perspective.
157  if (window->GetActivePage().IsNull())
158  {
159  QString defaultPerspId = window->GetWorkbench()->GetPerspectiveRegistry()->GetDefaultPerspective();
160  window->GetWorkbench()->ShowPerspective(defaultPerspId, window);
161  }
162 
163  bool globalReinitOnNodeAdded = true;
165  if (prefService != nullptr)
166  {
168  = prefService->GetSystemPreferences()->Node("org.mitk.views.datamanager");
169  if(prefs.IsNotNull())
170  {
171  globalReinitOnNodeAdded = prefs->GetBool("Call global reinit if node is added", true);
172  }
173  }
174 
175  if (openEditor && globalReinitOnNodeAdded)
176  {
177  try
178  {
179  // Activate the editor using the same data storage or open the default editor
181  berry::IEditorPart::Pointer editor = mitk::WorkbenchUtil::OpenEditor(window->GetActivePage(), input, true);
182  mitk::IRenderWindowPart* renderEditor = dynamic_cast<mitk::IRenderWindowPart*>(editor.GetPointer());
183  mitk::IRenderingManager* renderingManager = renderEditor == 0 ? 0 : renderEditor->GetRenderingManager();
184 
185  if(dsmodified && renderingManager)
186  {
188  }
189  }
190  catch (const berry::PartInitException& e)
191  {
192  QString msg = "An error occurred when displaying the file(s): %1";
193  QMessageBox::warning(QApplication::activeWindow(), "Error displaying file",
194  msg.arg(e.message()));
195  }
196  }
197 }
198 
201  const QString &editorId, bool activate)
202 {
203  // sanity checks
204  if (page.IsNull())
205  {
206  throw std::invalid_argument("page argument must not be NULL");
207  }
208 
209  // open the editor on the input
210  return page->OpenEditor(input, editorId, activate);
211 }
212 
215  bool activate,
216  bool determineContentType)
217 {
218  // sanity checks
219  if (page.IsNull())
220  {
221  throw std::invalid_argument("page argument must not be NULL");
222  }
223 
224  // open the editor on the data storage
225  QString name = input->GetName() + ".mitk";
227  WorkbenchUtilPrivate::GetEditorDescriptor(name,
228  berry::PlatformUI::GetWorkbench()->GetEditorRegistry(),
229  GetDefaultEditor(name, determineContentType));
230  return page->OpenEditor(input, editorDesc->GetId(), activate);
231 }
232 
234  const QString& name, bool /*inferContentType*/)
235 {
236  if (name.isEmpty())
237  {
238  throw std::invalid_argument("name argument must not be empty");
239  }
240 
241  // no used for now
242  //IContentType contentType = inferContentType ? Platform
243  // .getContentTypeManager().findContentTypeFor(name) : null;
244 
246 
247  return WorkbenchUtilPrivate::GetEditorDescriptor(name, editorReg,
248  editorReg->GetDefaultEditor(name /*, contentType*/));
249 }
250 
252  bool /*determineContentType*/)
253 {
254  // Try file specific editor.
256  try
257  {
258  QString editorID; // = file.getPersistentProperty(EDITOR_KEY);
259  if (!editorID.isEmpty())
260  {
261  berry::IEditorDescriptor::Pointer desc = editorReg->FindEditor(editorID);
262  if (desc.IsNotNull())
263  {
264  return desc;
265  }
266  }
267  }
268  catch (const berry::CoreException&)
269  {
270  // do nothing
271  }
272 
273 // IContentType contentType = null;
274 // if (determineContentType)
275 // {
276 // contentType = getContentType(file);
277 // }
278 
279  // Try lookup with filename
280  return editorReg->GetDefaultEditor(name); //, contentType);
281 }
282 
283 bool WorkbenchUtil::SetDepartmentLogoPreference(const QString &logoResource, ctkPluginContext *context)
284 {
285  if (context == nullptr)
286  {
287  BERRY_WARN << "Plugin context invalid, unable to set custom logo.";
288  return false;
289  }
290 
291  // The logo must be available in the local filesystem. We check if we have not already extracted the
292  // logo from the plug-in or if this plug-ins timestamp is newer then the already extracted logo timestamp.
293  // If one of the conditions is true, extract it and write it to the plug-in specific storage location.
294  const QString logoFileName = logoResource.mid(logoResource.lastIndexOf('/')+1);
295 
296  if (logoFileName.isEmpty())
297  {
298  BERRY_WARN << "Logo file name empty, unable to set custom logo.";
299  return false;
300  }
301 
302  const QString logoPath = context->getDataFile("").absoluteFilePath();
303 
304  bool extractLogo = true;
305  QFileInfo logoFileInfo(logoPath + "/" + logoFileName);
306 
307  if (logoFileInfo.exists())
308  {
309  // The logo has been extracted previously. Check if the plugin timestamp is newer, which
310  // means it might contain an updated logo.
311  QString pluginLocation = QUrl(context->getPlugin()->getLocation()).toLocalFile();
312  if (!pluginLocation.isEmpty())
313  {
314  QFileInfo pluginFileInfo(pluginLocation);
315  if (logoFileInfo.lastModified() > pluginFileInfo.lastModified())
316  {
317  extractLogo = false;
318  }
319  }
320  }
321 
322  if (extractLogo)
323  {
324  // Extract the logo from the shared library and write it to disk.
325  QFile logo(logoResource);
326 
327  if (!logo.exists())
328  {
329  BERRY_WARN << "Custom logo '" << logoResource << "' does not exist.";
330  return false;
331  }
332 
333  if (logo.open(QIODevice::ReadOnly))
334  {
335  QFile localLogo(logoPath + "/" + logoFileName);
336 
337  if (localLogo.open(QIODevice::WriteOnly))
338  {
339  localLogo.write(logo.readAll());
340  localLogo.flush();
341  }
342  }
343  }
344 
345  logoFileInfo.refresh();
346 
347  if (logoFileInfo.exists())
348  {
349  // Get the preferences service
350  ctkServiceReference prefServiceRef = context->getServiceReference<berry::IPreferencesService>();
351  berry::IPreferencesService* prefService = NULL;
352  if (prefServiceRef)
353  {
354  prefService = context->getService<berry::IPreferencesService>(prefServiceRef);
355  }
356 
357  if (prefService)
358  {
359  prefService->GetSystemPreferences()->Put("DepartmentLogo", qPrintable(logoFileInfo.absoluteFilePath()));
360  }
361  else
362  {
363  BERRY_WARN << "Preferences service not available, unable to set custom logo.";
364  return false;
365  }
366  }
367  else
368  {
369  BERRY_WARN << "Custom logo at '" << logoFileInfo.absoluteFilePath().toStdString() << "' does not exist.";
370  return false;
371  }
372 
373  return true;
374 }
375 
376 } // namespace mitk
static const QString SYSTEM_INPLACE_EDITOR_ID
static berry::IEditorDescriptor::Pointer GetDefaultEditor(const QString &file, bool determineContentType)
itk::SmartPointer< Self > Pointer
virtual IEditorDescriptor::Pointer FindEditor(const QString &editorId)=0
#define MITK_INFO
Definition: mitkLogMacros.h:22
static berry::IEditorDescriptor::Pointer GetEditorDescriptor(const QString &name, bool inferContentType=true)
virtual IDataStorageReference::Pointer GetDataStorage() const =0
Interface for a MITK Workbench Part providing a render window.
DataCollection - Class to facilitate loading/accessing structured data.
virtual SmartPointer< IPreferences > GetSystemPreferences()=0
An editor input based on a mitk::DataStorage.
static bool SetDepartmentLogoPreference(const QString &logoResource, ctkPluginContext *context)
static void LoadFiles(const QStringList &fileNames, berry::IWorkbenchWindow::Pointer wnd, bool openEditor=true)
#define MITK_WARN
Definition: mitkLogMacros.h:23
An object of this class represents an exception of MITK. Please don't instantiate exceptions manually...
Definition: mitkException.h:49
static RenderingManager * GetInstance()
static IWorkbench * GetWorkbench()
virtual IEditorDescriptor::Pointer GetDefaultEditor()=0
static QList< mitk::BaseData::Pointer > Load(const QStringList &paths, QWidget *parent=NULL)
Loads the specified files.
#define BERRY_WARN
Definition: berryLog.h:25
ObjectType * GetPointer() const
virtual bool IsSystemInPlaceEditorAvailable(const QString &filename)=0
virtual bool IsSystemExternalEditorAvailable(const QString &filename)=0
static berry::IEditorPart::Pointer OpenEditor(berry::IWorkbenchPage::Pointer page, berry::IEditorInput::Pointer input, const QString &editorId, bool activate=false)
static const QString SYSTEM_EXTERNAL_EDITOR_ID
virtual void InitializeViewsByBoundingObjects(const DataStorage *)
Initializes the renderwindows by the aggregated geometry of all objects that are held in the data sto...
An interface for accessing a mitk::RenderingManager instance.
virtual IEditorRegistry * GetEditorRegistry() const =0
static IPreferencesService * GetPreferencesService()