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