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
QmitkFunctionality.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 #include "QmitkFunctionality.h"
18 #include "internal/QmitkFunctionalityUtil.h"
19 #include "internal/QmitkCommonLegacyActivator.h"
20 
21 // other includes
22 #include <mitkLogMacros.h>
23 
24 // mitk Includes
27 
28 // berry Includes
29 #include <berryIWorkbenchPage.h>
30 #include <berryIBerryPreferences.h>
31 #include <berryIEditorPart.h>
32 #include <berryPlatform.h>
33 
34 // Qmitk Includes
36 
37 // Qt Includes
38 #include <QMessageBox>
39 #include <QScrollArea>
40 #include <QVBoxLayout>
41 #include <QApplication>
42 
44  : m_Parent(0)
45  , m_Active(false)
46  , m_Visible(false)
47  , m_SelectionProvider(0)
48  , m_DataStorageServiceTracker(QmitkCommonLegacyActivator::GetContext())
49  , m_HandlesMultipleDataStorages(false)
50  , m_InDataStorageChanged(false)
51 {
52  m_DataStorageServiceTracker.open();
53 }
54 
56 {
57  m_HandlesMultipleDataStorages = multiple;
58 }
59 
61 {
62  return m_HandlesMultipleDataStorages;
63 }
64 
67 {
68  mitk::IDataStorageService* service = m_DataStorageServiceTracker.getService();
69 
70  if (service != 0)
71  {
72  if(m_HandlesMultipleDataStorages)
73  return service->GetActiveDataStorage()->GetDataStorage();
74  else
75  return service->GetDefaultDataStorage()->GetDataStorage();
76  }
77 
78  return 0;
79 }
80 
82 {
83  mitk::IDataStorageService* service = m_DataStorageServiceTracker.getService();
84 
85  if (service != 0)
86  {
87  return service->GetDefaultDataStorage()->GetDataStorage();
88  }
89 
90  return 0;
91 }
92 
94 {
95  mitk::IDataStorageService* dsService = m_DataStorageServiceTracker.getService();
96 
97  if (dsService != 0)
98  {
99  return dsService->GetDataStorage();
100  }
101 
103 }
104 
106 {
107 
108  // scrollArea
109  QScrollArea* scrollArea = new QScrollArea;
110  //QVBoxLayout* scrollAreaLayout = new QVBoxLayout(scrollArea);
111  scrollArea->setFrameShadow(QFrame::Plain);
112  scrollArea->setFrameShape(QFrame::NoFrame);
113  scrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded);
114  scrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
115 
116  // m_Parent
117  m_Parent = new QWidget;
118  //m_Parent->setSizePolicy(QSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding));
120 
121  //scrollAreaLayout->addWidget(m_Parent);
122  //scrollArea->setLayout(scrollAreaLayout);
123 
124  // set the widget now
125  scrollArea->setWidgetResizable(true);
126  scrollArea->setWidget(m_Parent);
127 
128  // add the scroll area to the real parent (the view tabbar)
129  QWidget* parentQWidget = static_cast<QWidget*>(parent);
130  QVBoxLayout* parentLayout = new QVBoxLayout(parentQWidget);
131  parentLayout->setMargin(0);
132  parentLayout->setSpacing(0);
133  parentLayout->addWidget(scrollArea);
134 
135  // finally set the layout containing the scroll area to the parent widget (= show it)
136  parentQWidget->setLayout(parentLayout);
137 
138  this->AfterCreateQtPartControl();
139 }
140 
142 {
143  // REGISTER DATASTORAGE LISTENER
150 
151  // REGISTER PREFERENCES LISTENER
153  if(prefs.IsNotNull())
154  prefs->OnChanged.AddListener(berry::MessageDelegate1<QmitkFunctionality
156 
157  // REGISTER FOR WORKBENCH SELECTION EVENTS
158  m_BlueBerrySelectionListener.reset(new berry::SelectionChangedAdapter<QmitkFunctionality>(
159  this,
161  );
162  this->GetSite()->GetWorkbenchWindow()->GetSelectionService()->AddPostSelectionListener(
163  /*"org.mitk.views.datamanager",*/ m_BlueBerrySelectionListener.data());
164 
165  // REGISTER A SELECTION PROVIDER
167  new QmitkFunctionalitySelectionProvider(this));
168  m_SelectionProvider = _SelectionProvider.GetPointer();
169  this->GetSite()->SetSelectionProvider(berry::ISelectionProvider::Pointer(m_SelectionProvider));
170 
171  // EMULATE INITIAL SELECTION EVENTS
172 
173  // by default a a multi widget is always available
175 
176  // send datamanager selection
178 
179  // send preferences changed event
180  this->OnPreferencesChanged(this->GetPreferences().Cast<berry::IBerryPreferences>().GetPointer());
181 }
182 
184 {
185 
186 }
187 
189 {
196 
198  if(prefs.IsNotNull())
199  {
200  prefs->OnChanged.RemoveListener(berry::MessageDelegate1<QmitkFunctionality
202  // flush the preferences here (disabled, everyone should flush them by themselves at the right moment)
203  // prefs->Flush();
204  }
205 
206  // REMOVE SELECTION PROVIDER
207  this->GetSite()->SetSelectionProvider(berry::ISelectionProvider::Pointer(NULL));
208 
209  berry::ISelectionService* s = GetSite()->GetWorkbenchWindow()->GetSelectionService();
210  if(s)
211  {
212  s->RemovePostSelectionListener(m_BlueBerrySelectionListener.data());
213  }
214 
215  this->ClosePart();
216 }
217 
219 {
220  this->Register();
221  this->ClosePartProxy();
222 
223  this->UnRegister(false);
224 
225  m_DataStorageServiceTracker.close();
226 }
227 
229 {
230 }
231 
233  const berry::ISelection::ConstPointer& selection)
234 {
235  if(sourcepart.IsNull() || sourcepart->GetSite()->GetId() != "org.mitk.views.datamanager")
236  return;
237 
238  mitk::DataNodeSelection::ConstPointer _DataNodeSelection
239  = selection.Cast<const mitk::DataNodeSelection>();
240  this->OnSelectionChanged(this->DataNodeSelectionToVector(_DataNodeSelection));
241 }
242 
244 {
245  return m_Visible;
246 }
247 
249 {
250 }
251 
253 {
254 }
255 
257 {
258 }
259 
261 {
262 }
264 {
265 }
266 
268 {
269 
270 }
271 
273 {
274  QmitkStdMultiWidget* activeStdMultiWidget = 0;
275 
277  this->GetSite()->GetPage()->GetActiveEditor();
278 
279  if (reCreateWidget
280  || editor.Cast<QmitkStdMultiWidgetEditor>().IsNull()
281  )
282  {
285  // open a new multi-widget editor, but do not give it the focus
286  berry::IEditorPart::Pointer editor = this->GetSite()->GetPage()->OpenEditor(editorInput, QmitkStdMultiWidgetEditor::EDITOR_ID, false, berry::IWorkbenchPage::MATCH_ID);
287  activeStdMultiWidget = editor.Cast<QmitkStdMultiWidgetEditor>()->GetStdMultiWidget();
288  }
289  else if (editor.Cast<QmitkStdMultiWidgetEditor>().IsNotNull())
290  {
291  activeStdMultiWidget = editor.Cast<QmitkStdMultiWidgetEditor>()->GetStdMultiWidget();
292  }
293 
294  return activeStdMultiWidget;
295 }
296 
297 void QmitkFunctionality::HandleException( const char* str, QWidget* parent, bool showDialog ) const
298 {
299  //itkGenericOutputMacro( << "Exception caught: " << str );
300  MITK_ERROR << str;
301  if ( showDialog )
302  {
303  QMessageBox::critical ( parent, "Exception caught!", str );
304  }
305 }
306 
307 void QmitkFunctionality::HandleException( std::exception& e, QWidget* parent, bool showDialog ) const
308 {
309  HandleException( e.what(), parent, showDialog );
310 }
311 
313 {
314 
315 }
316 
318 {
319  QApplication::setOverrideCursor( QCursor(Qt::WaitCursor) );
320 }
321 
323 {
324  QApplication::setOverrideCursor( QCursor(Qt::BusyCursor) );
325 }
326 
328 {
329  this->RestoreOverrideCursor();
330 }
331 
333 {
334  this->RestoreOverrideCursor();
335 }
336 
338 {
339  QApplication::restoreOverrideCursor();
340 }
341 
343 {
345  // const_cast workaround for bad programming: const uncorrectness this->GetViewSite() should be const
346  QString id = "/" + (const_cast<QmitkFunctionality*>(this))->GetViewSite()->GetId();
347  return prefService != nullptr ? prefService->GetSystemPreferences()->Node(id): berry::IPreferences::Pointer(0);
348 }
349 
351 {
352 
353 }
354 
356 {
357 
358 }
359 
361 {
362  return true;
363 }
364 
365 void QmitkFunctionality::SetVisible( bool visible )
366 {
367  m_Visible = visible;
368 }
369 
370 void QmitkFunctionality::SetActivated( bool activated )
371 {
372  m_Active = activated;
373 }
374 
376 {
377  return m_Active;
378 }
379 
virtual void RemovePostSelectionListener(ISelectionListener *listener)=0
void Register() const
virtual IDataStorageReference::Pointer GetDefaultDataStorage() const =0
itk::SmartPointer< Self > Pointer
std::vector< mitk::DataNode * > GetDataManagerSelection() const
virtual IDataStorageReference::Pointer GetActiveDataStorage() const =0
void NodeRemovedProxy(const mitk::DataNode *node)
mitk::IDataStorageReference::Pointer GetDataStorageReference() const
#define MITK_ERROR
Definition: mitkLogMacros.h:24
void UnRegister(bool del=true) const
void SetActivated(bool activated)
virtual IDataStorageReference::Pointer GetDataStorage() const =0
void CreatePartControl(QWidget *parent) override
virtual SmartPointer< IPreferences > GetSystemPreferences()=0
QmitkStdMultiWidget * GetActiveStdMultiWidget(bool reCreateWidget=true)
virtual void StdMultiWidgetClosed(QmitkStdMultiWidget &stdMultiWidget)
berry::IPreferences::Pointer GetPreferences() const
An editor input based on a mitk::DataStorage.
void SetVisible(bool visible)
virtual void Deactivated()
virtual void OnPreferencesChanged(const berry::IBerryPreferences *)
mitk::DataStorage::Pointer GetDefaultDataStorage() const
berry::SmartPointer< Self > Pointer
Definition: berryObject.h:88
void NodeAddedProxy(const mitk::DataNode *node)
virtual void CreateQtPartControl(QWidget *parent)=0
mitk::DataStorage::Pointer GetDataStorage() const
bool HandlesMultipleDataStorages() const
virtual bool IsExclusiveFunctionality() const
void BlueBerrySelectionChanged(const berry::IWorkbenchPart::Pointer &sourcepart, const berry::ISelection::ConstPointer &selection)
virtual void StdMultiWidgetNotAvailable()
void SetFocus() override
The base class of all MITK related blueberry views (~ in the old version of MITK, this was called "Fu...
virtual void DataStorageChanged()
void NodeChangedProxy(const mitk::DataNode *node)
void SetHandleMultipleDataStorages(bool multiple)
SmartPointer< Other > Cast() const
virtual void StdMultiWidgetAvailable(QmitkStdMultiWidget &stdMultiWidget)
std::vector< mitk::DataNode * > DataNodeSelectionToVector(mitk::DataNodeSelection::ConstPointer currentSelection) const
void HandleException(std::exception &e, QWidget *parent=NULL, bool showDialog=true) const
berry::SmartPointer< Self > Pointer
IWorkbenchPartSite::Pointer GetSite() const override
virtual void OnSelectionChanged(std::vector< mitk::DataNode * >)
static IPreferencesService * GetPreferencesService()