Medical Imaging Interaction Toolkit  2018.4.99-389bf124
Medical Imaging Interaction Toolkit
QmitkAbstractMultiWidget.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 // mitk qt widgets module
15 #include "QmitkLevelWindowWidget.h"
18 
19 // mitk core
20 #include <mitkDataStorage.h>
22 
23 // qt
24 #include <QMouseEvent>
25 
26 // c++
27 #include <iomanip>
28 
29 struct QmitkAbstractMultiWidget::Impl final
30 {
31  Impl(QmitkAbstractMultiWidget* multiWidget,
32  const QString& multiWidgetName);
33 
34  void SetDataStorage(mitk::DataStorage* dataStorage)
35  {
36  if (dataStorage == m_DataStorage)
37  {
38  return;
39  }
40 
41  m_DataStorage = dataStorage;
42  // set new data storage for the render window widgets
43  for (const auto& renderWindowWidget : m_RenderWindowWidgets)
44  {
45  renderWindowWidget.second->SetDataStorage(m_DataStorage);
46  }
47  }
48 
49  void InitializeDisplayActionEventHandling()
50  {
51  m_DisplayActionEventBroadcast = mitk::DisplayActionEventBroadcast::New();
52  m_DisplayActionEventBroadcast->LoadStateMachine("DisplayInteraction.xml");
53  m_DisplayActionEventBroadcast->SetEventConfig("DisplayConfigPACS.xml");
54  }
55 
57 
58  QString m_MultiWidgetName;
59 
60  RenderWindowWidgetMap m_RenderWindowWidgets;
61  RenderWindowWidgetPointer m_ActiveRenderWindowWidget;
62 
63  int m_MultiWidgetRows;
64  int m_MultiWidgetColumns;
65 
66  // interaction
67  mitk::DisplayActionEventBroadcast::Pointer m_DisplayActionEventBroadcast;
68  std::unique_ptr<mitk::DisplayActionEventHandler> m_DisplayActionEventHandler;
69  QmitkMultiWidgetLayoutManager* m_LayoutManager;
70 };
71 
72 QmitkAbstractMultiWidget::Impl::Impl(QmitkAbstractMultiWidget* multiWidget,
73  const QString& multiWidgetName)
74  : m_DataStorage(nullptr)
75  , m_MultiWidgetName(multiWidgetName)
76  , m_MultiWidgetRows(0)
77  , m_MultiWidgetColumns(0)
78  , m_DisplayActionEventBroadcast(nullptr)
79  , m_DisplayActionEventHandler(nullptr)
80  , m_LayoutManager(new QmitkMultiWidgetLayoutManager(multiWidget))
81 {
82  InitializeDisplayActionEventHandling();
83 }
84 
86  Qt::WindowFlags f/* = 0*/,
87  const QString& multiWidgetName/* = "multiwidget"*/)
88  : QWidget(parent, f)
89  , m_Impl(std::make_unique<Impl>(this, multiWidgetName))
90 {
91  // nothing here
92 }
93 
95 
97 {
98  m_Impl->SetDataStorage(dataStorage);
99 }
100 
102 {
103  return m_Impl->m_DataStorage;
104 }
105 
107 {
108  return m_Impl->m_MultiWidgetRows;
109 }
110 
112 {
113  return m_Impl->m_MultiWidgetColumns;
114 }
115 
116 void QmitkAbstractMultiWidget::SetLayout(int row, int column)
117 {
118  m_Impl->m_MultiWidgetRows = row;
119  m_Impl->m_MultiWidgetColumns = column;
120  SetLayoutImpl();
121 }
122 
124 {
125  auto interactionSchemeSwitcher = mitk::InteractionSchemeSwitcher::New();
126  auto interactionEventHandler = GetInteractionEventHandler();
127  try
128  {
129  interactionSchemeSwitcher->SetInteractionScheme(interactionEventHandler, scheme);
130  }
131  catch (const mitk::Exception&)
132  {
133  return;
134  }
135 
136  SetInteractionSchemeImpl();
137 }
138 
140 {
141  return m_Impl->m_DisplayActionEventBroadcast.GetPointer();
142 }
143 
144 void QmitkAbstractMultiWidget::SetDisplayActionEventHandler(std::unique_ptr<mitk::DisplayActionEventHandler> displayActionEventHandler)
145 {
146  m_Impl->m_DisplayActionEventHandler = std::move(displayActionEventHandler);
147  m_Impl->m_DisplayActionEventHandler->SetObservableBroadcast(m_Impl->m_DisplayActionEventBroadcast);
148 }
149 
151 {
152  return m_Impl->m_DisplayActionEventHandler.get();
153 }
154 
156 {
157  return m_Impl->m_RenderWindowWidgets;
158 }
159 
161 {
162  RenderWindowWidgetMap renderWindowWidgets2D;
163 
164  auto renderWindowWidgets = GetRenderWindowWidgets();
165  for (const auto& renderWindowWidget : renderWindowWidgets)
166  {
167  auto renderWindow = renderWindowWidget.second->GetRenderWindow();
168  if(mitk::BaseRenderer::Standard2D == mitk::BaseRenderer::GetInstance(renderWindow->GetVtkRenderWindow())->GetMapperID())
169  {
170  renderWindowWidgets2D.insert(std::make_pair(renderWindowWidget.first, renderWindowWidget.second));
171  }
172  }
173 
174  return renderWindowWidgets2D;
175 }
176 
178 {
179  RenderWindowWidgetMap renderWindowWidgets3D;
180 
181  auto renderWindowWidgets = GetRenderWindowWidgets();
182  for (const auto& renderWindowWidget : renderWindowWidgets)
183  {
184  auto renderWindow = renderWindowWidget.second->GetRenderWindow();
185  if (mitk::BaseRenderer::Standard3D == mitk::BaseRenderer::GetInstance(renderWindow->GetVtkRenderWindow())->GetMapperID())
186  {
187  renderWindowWidgets3D.insert(std::make_pair(renderWindowWidget.first, renderWindowWidget.second));
188  }
189  }
190 
191  return renderWindowWidgets3D;
192 }
193 
195 {
196  return GetRenderWindowWidget(GetNameFromIndex(row, column));
197 }
198 
200 {
201  RenderWindowWidgetMap::const_iterator it = m_Impl->m_RenderWindowWidgets.find(widgetName);
202  if (it != m_Impl->m_RenderWindowWidgets.end())
203  {
204  return it->second;
205  }
206 
207  return nullptr;
208 }
209 
211 {
212  auto renderWindowWidgets = GetRenderWindowWidgets();
213  for (const auto& renderWindowWidget : renderWindowWidgets)
214  {
215  if (renderWindowWidget.second->GetRenderWindow() == renderWindow)
216  {
217  return renderWindowWidget.second;
218  }
219  }
220 
221  return nullptr;
222 }
223 
225 {
226  RenderWindowHash result;
227  // create QHash on demand
228  auto renderWindowWidgets = GetRenderWindowWidgets();
229  for (const auto& renderWindowWidget : renderWindowWidgets)
230  {
231  result.insert(renderWindowWidget.first, renderWindowWidget.second->GetRenderWindow());
232  }
233 
234  return result;
235 }
236 
238 {
239  return GetRenderWindow(GetNameFromIndex(row, column));
240 }
241 
243 {
244  RenderWindowWidgetPointer renderWindowWidget = GetRenderWindowWidget(widgetName);
245  if (nullptr != renderWindowWidget)
246  {
247  return renderWindowWidget->GetRenderWindow();
248  }
249 
250  return nullptr;
251 }
252 
254 {
255  m_Impl->m_ActiveRenderWindowWidget = activeRenderWindowWidget;
257 }
258 
260 {
261  return m_Impl->m_ActiveRenderWindowWidget;
262 }
263 
265 {
266  if (!m_Impl->m_RenderWindowWidgets.empty())
267  {
268  return m_Impl->m_RenderWindowWidgets.begin()->second;
269  }
270  else
271  {
272  return nullptr;
273  }
274 }
275 
277 {
278  if (!m_Impl->m_RenderWindowWidgets.empty())
279  {
280  return m_Impl->m_RenderWindowWidgets.rbegin()->second;
281  }
282  else
283  {
284  return nullptr;
285  }
286 }
287 
288 QString QmitkAbstractMultiWidget::GetNameFromIndex(int row, int column) const
289 {
290  if (0 <= row && m_Impl->m_MultiWidgetRows > row && 0 <= column && m_Impl->m_MultiWidgetColumns > column)
291  {
292  return GetNameFromIndex(row * m_Impl->m_MultiWidgetColumns + column);
293  }
294 
295  return QString();
296 }
297 
299 {
300  if (index <= m_Impl->m_RenderWindowWidgets.size())
301  {
302  return m_Impl->m_MultiWidgetName + ".widget" + QString::number(index);
303  }
304 
305  return QString();
306 }
307 
309 {
310  return m_Impl->m_RenderWindowWidgets.size();
311 }
312 
313 void QmitkAbstractMultiWidget::RequestUpdate(const QString& widgetName)
314 {
315  RenderWindowWidgetPointer renderWindowWidget = GetRenderWindowWidget(widgetName);
316  if (nullptr != renderWindowWidget)
317  {
318  return renderWindowWidget->RequestUpdate();
319  }
320 }
321 
323 {
324  for (const auto& renderWindowWidget : m_Impl->m_RenderWindowWidgets)
325  {
326  renderWindowWidget.second->RequestUpdate();
327  }
328 }
329 
330 void QmitkAbstractMultiWidget::ForceImmediateUpdate(const QString& widgetName)
331 {
332  RenderWindowWidgetPointer renderWindowWidget = GetRenderWindowWidget(widgetName);
333  if (nullptr != renderWindowWidget)
334  {
335  renderWindowWidget->ForceImmediateUpdate();
336  }
337 }
338 
340 {
341  for (const auto& renderWindowWidget : m_Impl->m_RenderWindowWidgets)
342  {
343  renderWindowWidget.second->ForceImmediateUpdate();
344  }
345 }
346 
348 {
349  for (const auto& renderWindowWidget : m_Impl->m_RenderWindowWidgets)
350  {
351  auto renderWindow = renderWindowWidget.second->GetRenderWindow();
352  renderWindow->ActivateMenuWidget(state);
353  }
354 }
355 
357 {
358  return m_Impl->m_ActiveRenderWindowWidget->GetRenderWindow()->GetActivateMenuWidgetFlag();
359 }
360 
362 {
363  return m_Impl->m_LayoutManager;
364 }
365 
366 void QmitkAbstractMultiWidget::AddRenderWindowWidget(const QString& widgetName, RenderWindowWidgetPointer renderWindowWidget)
367 {
368  m_Impl->m_RenderWindowWidgets.insert(std::make_pair(widgetName, renderWindowWidget));
369 }
370 
372 {
373  auto iterator = m_Impl->m_RenderWindowWidgets.find(GetNameFromIndex(GetRenderWindowWidgets().size() - 1));
374  if (iterator == m_Impl->m_RenderWindowWidgets.end())
375  {
376  return;
377  }
378 
379  // disconnect each signal of this render window widget
380  RenderWindowWidgetPointer renderWindowWidgetToRemove = iterator->second;
381  disconnect(renderWindowWidgetToRemove.get(), 0, 0, 0);
382 
383  // erase the render window from the map
384  m_Impl->m_RenderWindowWidgets.erase(iterator);
385 }
QmitkRenderWindow * GetRenderWindow(int row, int column) const
virtual void ActivateMenuWidget(bool state)
virtual void AddRenderWindowWidget(const QString &widgetName, RenderWindowWidgetPointer renderWindowWidget)
Data management class that handles &#39;was created by&#39; relations.
QmitkMultiWidgetLayoutManager * GetMultiWidgetLayoutManager() const
RenderWindowWidgetMap GetRenderWindowWidgets() const
static BaseRenderer * GetInstance(vtkRenderWindow *renWin)
RenderWindowWidgetPointer GetRenderWindowWidget(int row, int column) const
QmitkAbstractMultiWidget(QWidget *parent=0, Qt::WindowFlags f=0, const QString &multiWidgetName="multiwidget")
STL namespace.
unsigned int GetNumberOfRenderWindowWidgets() const
RenderWindowHash GetRenderWindows() const
RenderWindowWidgetMap Get2DRenderWindowWidgets() const
RenderWindowWidgetMap Get3DRenderWindowWidgets() const
This class simplifies the process of adding an itkEventObject-itkCommand pair as an observer of a Dis...
std::shared_ptr< QmitkRenderWindowWidget > RenderWindowWidgetPointer
RenderWindowWidgetPointer GetActiveRenderWindowWidget() const
mitk::DataStorage::Pointer m_DataStorage
mitk::InteractionEventHandler * GetInteractionEventHandler()
An object of this class represents an exception of MITK. Please don&#39;t instantiate exceptions manually...
Definition: mitkException.h:45
void SetDisplayActionEventHandler(std::unique_ptr< mitk::DisplayActionEventHandler > displayActionEventHandler)
virtual void SetLayout(int row, int column)
virtual bool IsMenuWidgetEnabled() const
MITK implementation of the QVTKWidget.
void RequestUpdate(const QString &widgetName)
mitk::DisplayActionEventHandler * GetDisplayActionEventHandler()
void ForceImmediateUpdate(const QString &widgetName)
QHash< QString, QmitkRenderWindow * > RenderWindowHash
virtual void SetInteractionScheme(mitk::InteractionSchemeSwitcher::InteractionScheme scheme)
std::map< QString, std::shared_ptr< QmitkRenderWindowWidget > > RenderWindowWidgetMap
virtual QString GetNameFromIndex(int row, int column) const
virtual void SetActiveRenderWindowWidget(RenderWindowWidgetPointer activeRenderWindowWidget)
virtual void SetDataStorage(mitk::DataStorage *dataStorage)
mitk::DataStorage * GetDataStorage() const
The &#39;QmitkAbstractMultiWidget&#39; is a &#39;QWidget&#39; that can be subclassed to display multiple render windo...
RenderWindowWidgetPointer GetLastRenderWindowWidget() const
RenderWindowWidgetPointer GetFirstRenderWindowWidget() const
The layout manager provides different layout-functions that can modify the layout of an QmitkAbstract...