Medical Imaging Interaction Toolkit  2018.4.99-3e3f1a6e
Medical Imaging Interaction Toolkit
QmitkRenderWindow.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 "QmitkRenderWindow.h"
14 
16 #include "mitkInternalEvent.h"
18 #include "mitkMouseMoveEvent.h"
19 #include "mitkMousePressEvent.h"
20 #include "mitkMouseReleaseEvent.h"
21 #include "mitkMouseWheelEvent.h"
22 #include <QCursor>
23 #include <QDragEnterEvent>
24 #include <QDropEvent>
25 #include <QKeyEvent>
26 #include <QMouseEvent>
27 #include <QResizeEvent>
28 #include <QSurfaceFormat>
29 #include <QTimer>
30 #include <QWheelEvent>
31 #include <QWindow>
32 
33 #include "QmitkMimeTypes.h"
34 #include "QmitkRenderWindowMenu.h"
35 
36 QmitkRenderWindow::QmitkRenderWindow(QWidget *parent, const QString &name, mitk::VtkPropRenderer *)
37  : QVTKOpenGLWidget(parent)
38  , m_ResendQtEvents(true)
39  , m_MenuWidget(nullptr)
40  , m_MenuWidgetActivated(false)
41  , m_LayoutIndex(QmitkRenderWindowMenu::LayoutIndex::AXIAL)
42 {
43  m_InternalRenderWindow = vtkSmartPointer<vtkGenericOpenGLRenderWindow>::New();
44  m_InternalRenderWindow->SetMultiSamples(0);
45  m_InternalRenderWindow->SetAlphaBitPlanes(0);
46 
47  SetRenderWindow(m_InternalRenderWindow);
48 
49  Initialize(name.toStdString().c_str());
50 
51  setFocusPolicy(Qt::StrongFocus);
52  setMouseTracking(true);
53  QSizePolicy sizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
54  setSizePolicy(sizePolicy);
55 }
56 
58 {
59  Destroy(); // Destroy mitkRenderWindowBase
60 }
61 
63 {
64  m_ResendQtEvents = resend;
65 }
66 
68 {
69  m_LayoutIndex = layoutIndex;
70  if (nullptr != m_MenuWidget)
71  {
72  m_MenuWidget->SetLayoutIndex(layoutIndex);
73  }
74 }
75 
77 {
78  if (nullptr != m_MenuWidget)
79  {
80  return m_MenuWidget->GetLayoutIndex();
81  }
82  else
83  {
85  }
86 }
87 
89 {
90  if (nullptr != m_MenuWidget)
91  {
92  m_MenuWidget->UpdateLayoutDesignList(layoutDesign);
93  }
94 }
95 
97 {
98  if (nullptr == m_MenuWidget)
99  {
100  m_MenuWidget = new QmitkRenderWindowMenu(this, nullptr, m_Renderer);
101  m_MenuWidget->SetLayoutIndex(m_LayoutIndex);
102  }
103 
104  m_MenuWidgetActivated = state;
105 
106  if (m_MenuWidgetActivated)
107  {
109  connect(m_MenuWidget, &QmitkRenderWindowMenu::ResetView, this, &QmitkRenderWindow::ResetView);
112  }
113  else
114  {
116  disconnect(m_MenuWidget, &QmitkRenderWindowMenu::ResetView, this, &QmitkRenderWindow::ResetView);
119 
120  m_MenuWidget->hide();
121  }
122 }
123 
124 void QmitkRenderWindow::moveEvent(QMoveEvent *event)
125 {
126  QVTKOpenGLWidget::moveEvent(event);
127 
128  // after a move the overlays need to be positioned
129  emit moved();
130 }
131 
132 void QmitkRenderWindow::showEvent(QShowEvent *event)
133 {
134  QVTKOpenGLWidget::showEvent(event);
135 
136  // this singleshot is necessary to have the overlays positioned correctly after initial show
137  // simple call of moved() is no use here!!
138  QTimer::singleShot(0, this, SIGNAL(moved()));
139 }
140 
142 {
143  // Get mouse position in vtk display coordinate system. me contains qt display infos...
144  mitk::Point2D displayPos = GetMousePosition(me);
145 
146  mitk::MousePressEvent::Pointer mPressEvent =
147  mitk::MousePressEvent::New(m_Renderer, displayPos, GetButtonState(me), GetModifiers(me), GetEventButton(me));
148 
149  if (!this->HandleEvent(mPressEvent.GetPointer()))
150  {
151  QVTKOpenGLWidget::mousePressEvent(me);
152  }
153 
154  if (m_ResendQtEvents)
155  {
156  me->ignore();
157  }
158 }
159 
161 {
162  mitk::Point2D displayPos = GetMousePosition(me);
164  mitk::MouseDoubleClickEvent::New(m_Renderer, displayPos, GetButtonState(me), GetModifiers(me), GetEventButton(me));
165 
166  if (!this->HandleEvent(mPressEvent.GetPointer()))
167  {
168  QVTKOpenGLWidget::mousePressEvent(me);
169  }
170 
171  if (m_ResendQtEvents)
172  {
173  me->ignore();
174  }
175 }
176 
178 {
179  mitk::Point2D displayPos = GetMousePosition(me);
180  mitk::MouseReleaseEvent::Pointer mReleaseEvent =
181  mitk::MouseReleaseEvent::New(m_Renderer, displayPos, GetButtonState(me), GetModifiers(me), GetEventButton(me));
182 
183  if (!this->HandleEvent(mReleaseEvent.GetPointer()))
184  {
185  QVTKOpenGLWidget::mouseReleaseEvent(me);
186  }
187 
188  if (m_ResendQtEvents)
189  {
190  me->ignore();
191  }
192 }
193 
195 {
196  mitk::Point2D displayPos = GetMousePosition(me);
197 
199 
200  mitk::MouseMoveEvent::Pointer mMoveEvent =
201  mitk::MouseMoveEvent::New(m_Renderer, displayPos, GetButtonState(me), GetModifiers(me));
202 
203  if (!this->HandleEvent(mMoveEvent.GetPointer()))
204  {
205  QVTKOpenGLWidget::mouseMoveEvent(me);
206  }
207 }
208 
209 void QmitkRenderWindow::wheelEvent(QWheelEvent *we)
210 {
211  mitk::Point2D displayPos = GetMousePosition(we);
212  mitk::MouseWheelEvent::Pointer mWheelEvent =
213  mitk::MouseWheelEvent::New(m_Renderer, displayPos, GetButtonState(we), GetModifiers(we), GetDelta(we));
214 
215  if (!this->HandleEvent(mWheelEvent.GetPointer()))
216  {
217  QVTKOpenGLWidget::wheelEvent(we);
218  }
219 
220  if (m_ResendQtEvents)
221  {
222  we->ignore();
223  }
224 }
225 
227 {
228  mitk::InteractionEvent::ModifierKeys modifiers = GetModifiers(ke);
229  std::string key = GetKeyLetter(ke);
230 
232  if (!this->HandleEvent(keyEvent.GetPointer()))
233  {
234  QVTKOpenGLWidget::keyPressEvent(ke);
235  }
236 
237  if (m_ResendQtEvents)
238  {
239  ke->ignore();
240  }
241 }
242 
244 {
245  // TODO implement new event
246  QVTKOpenGLWidget::enterEvent(e);
247 }
248 
250 {
251  mitk::InternalEvent::Pointer internalEvent = mitk::InternalEvent::New(this->m_Renderer, nullptr, "LeaveRenderWindow");
252 
253  this->HandleEvent(internalEvent.GetPointer());
254 
255  if (nullptr != m_MenuWidget)
256  {
257  m_MenuWidget->smoothHide();
258  }
259 
260  QVTKOpenGLWidget::leaveEvent(e);
261 }
262 
263 void QmitkRenderWindow::resizeGL(int w, int h)
264 {
265  QVTKOpenGLWidget::resizeGL(w, h);
267 }
268 
269 void QmitkRenderWindow::dragEnterEvent(QDragEnterEvent *event)
270 {
271  if (event->mimeData()->hasFormat("application/x-mitk-datanodes"))
272  {
273  event->accept();
274  }
275 }
276 
277 void QmitkRenderWindow::dropEvent(QDropEvent *event)
278 {
279  QList<mitk::DataNode *> dataNodeList = QmitkMimeTypes::ToDataNodePtrList(event->mimeData());
280  if (!dataNodeList.empty())
281  {
282  emit NodesDropped(this, dataNodeList.toVector().toStdVector());
283  }
284 }
285 
287 {
288  if (nullptr != m_MenuWidget)
289  {
290  m_MenuWidget->ShowMenu();
291  m_MenuWidget->MoveWidgetToCorrectPos(1.0f);
292  }
293 }
294 
295 void QmitkRenderWindow::DeferredHideMenu()
296 {
297  MITK_DEBUG << "QmitkRenderWindow::DeferredHideMenu";
298 
299  if (nullptr != m_MenuWidget)
300  {
301  m_MenuWidget->HideMenu();
302  }
303 }
304 
305 mitk::Point2D QmitkRenderWindow::GetMousePosition(QMouseEvent *me) const
306 {
307  mitk::Point2D point;
308  point[0] = me->x();
309  // We need to convert the y component, as the display and vtk have other definitions for the y direction
310  point[1] = m_Renderer->GetSizeY() - me->y();
311  return point;
312 }
313 
314 mitk::Point2D QmitkRenderWindow::GetMousePosition(QWheelEvent *we) const
315 {
316  mitk::Point2D point;
317  point[0] = we->x();
318  // We need to convert the y component, as the display and vtk have other definitions for the y direction
319  point[1] = m_Renderer->GetSizeY() - we->y();
320  return point;
321 }
322 
323 mitk::InteractionEvent::MouseButtons QmitkRenderWindow::GetEventButton(QMouseEvent *me) const
324 {
326  switch (me->button())
327  {
328  case Qt::LeftButton:
330  break;
331  case Qt::RightButton:
333  break;
334  case Qt::MidButton:
336  break;
337  default:
338  eventButton = mitk::InteractionEvent::NoButton;
339  break;
340  }
341  return eventButton;
342 }
343 
344 mitk::InteractionEvent::MouseButtons QmitkRenderWindow::GetButtonState(QMouseEvent *me) const
345 {
347 
348  if (me->buttons() & Qt::LeftButton)
349  {
350  buttonState = buttonState | mitk::InteractionEvent::LeftMouseButton;
351  }
352  if (me->buttons() & Qt::RightButton)
353  {
354  buttonState = buttonState | mitk::InteractionEvent::RightMouseButton;
355  }
356  if (me->buttons() & Qt::MidButton)
357  {
358  buttonState = buttonState | mitk::InteractionEvent::MiddleMouseButton;
359  }
360  return buttonState;
361 }
362 
363 mitk::InteractionEvent::ModifierKeys QmitkRenderWindow::GetModifiers(QInputEvent *me) const
364 {
366 
367  if (me->modifiers() & Qt::ALT)
368  {
369  modifiers = modifiers | mitk::InteractionEvent::AltKey;
370  }
371  if (me->modifiers() & Qt::CTRL)
372  {
373  modifiers = modifiers | mitk::InteractionEvent::ControlKey;
374  }
375  if (me->modifiers() & Qt::SHIFT)
376  {
377  modifiers = modifiers | mitk::InteractionEvent::ShiftKey;
378  }
379  return modifiers;
380 }
381 
382 mitk::InteractionEvent::MouseButtons QmitkRenderWindow::GetButtonState(QWheelEvent *we) const
383 {
385 
386  if (we->buttons() & Qt::LeftButton)
387  {
388  buttonState = buttonState | mitk::InteractionEvent::LeftMouseButton;
389  }
390  if (we->buttons() & Qt::RightButton)
391  {
392  buttonState = buttonState | mitk::InteractionEvent::RightMouseButton;
393  }
394  if (we->buttons() & Qt::MidButton)
395  {
396  buttonState = buttonState | mitk::InteractionEvent::MiddleMouseButton;
397  }
398  return buttonState;
399 }
400 
401 std::string QmitkRenderWindow::GetKeyLetter(QKeyEvent *ke) const
402 {
403  // Converting Qt Key Event to string element.
404  std::string key = "";
405  int tkey = ke->key();
406  if (tkey < 128)
407  { // standard ascii letter
408  key = (char)toupper(tkey);
409  }
410  else
411  { // special keys
412  switch (tkey)
413  {
414  case Qt::Key_Return:
416  break;
417  case Qt::Key_Enter:
419  break;
420  case Qt::Key_Escape:
422  break;
423  case Qt::Key_Delete:
425  break;
426  case Qt::Key_Up:
428  break;
429  case Qt::Key_Down:
431  break;
432  case Qt::Key_Left:
434  break;
435  case Qt::Key_Right:
437  break;
438 
439  case Qt::Key_F1:
441  break;
442  case Qt::Key_F2:
444  break;
445  case Qt::Key_F3:
447  break;
448  case Qt::Key_F4:
450  break;
451  case Qt::Key_F5:
453  break;
454  case Qt::Key_F6:
456  break;
457  case Qt::Key_F7:
459  break;
460  case Qt::Key_F8:
462  break;
463  case Qt::Key_F9:
465  break;
466  case Qt::Key_F10:
468  break;
469  case Qt::Key_F11:
471  break;
472  case Qt::Key_F12:
474  break;
475 
476  case Qt::Key_End:
478  break;
479  case Qt::Key_Home:
481  break;
482  case Qt::Key_Insert:
484  break;
485  case Qt::Key_PageDown:
487  break;
488  case Qt::Key_PageUp:
490  break;
491  case Qt::Key_Space:
493  break;
494  }
495  }
496  return key;
497 }
498 
499 int QmitkRenderWindow::GetDelta(QWheelEvent *we) const
500 {
501  return we->delta();
502 }
static const std::string KeyF1
void LayoutDesignChanged(QmitkRenderWindowMenu::LayoutDesign)
static const std::string KeyF12
virtual bool HandleEvent(InteractionEvent *interactionEvent)
static const std::string KeyF9
void NodesDropped(QmitkRenderWindow *thisWindow, std::vector< mitk::DataNode *> nodes)
Emits a signal to say that this window has had the following nodes dropped on it. ...
void dropEvent(QDropEvent *event) override
If the dropped type is application/x-mitk-datanodes we process the request by converting to mitk::Dat...
static const std::string KeyF4
The QmitkRenderWindowMenu is a popup Widget which shows up when the mouse cursor enter a QmitkRenderW...
static Pointer New(BaseRenderer *_arga, const Point2D &_argb, MouseButtons _argc, ModifierKeys _argd, MouseButtons _arge)
static const std::string KeyPos1
void dragEnterEvent(QDragEnterEvent *event) override
Simply says we accept the event type.
static const std::string KeyF10
static const std::string KeyEsc
static const std::string KeyArrowLeft
static const std::string KeySpace
static Pointer New(BaseRenderer *_arga, const Point2D &_argb, MouseButtons _argc, ModifierKeys _argd, MouseButtons _arge)
QmitkRenderWindowMenu::LayoutIndex GetLayoutIndex()
void UpdateLayoutDesignList(LayoutDesign layoutDesign)
#define MITK_DEBUG
Definition: mitkLogMacros.h:22
static QList< mitk::DataNode * > ToDataNodePtrList(const QByteArray &ba)
static const std::string KeyEnd
void CrosshairVisibilityChanged(bool)
void mouseReleaseEvent(QMouseEvent *event) override
void ActivateMenuWidget(bool state)
void LayoutDesignChanged(LayoutDesign layoutDesign)
void leaveEvent(QEvent *) override
static const std::string KeyArrowRight
static const std::string KeyEnter
void showEvent(QShowEvent *event) override
static const std::string KeyF7
static Pointer New(BaseRenderer *_arga, const Point2D &_argb, MouseButtons _argc, ModifierKeys _argd, int _arge)
static const std::string KeyArrowDown
static const std::string KeyF2
static const std::string KeyF11
static Pointer New(BaseRenderer *_arga, const std::string &_argb, ModifierKeys _argc)
void enterEvent(QEvent *) override
mitk::VtkPropRenderer::Pointer m_Renderer
static RenderingManager * GetInstance()
QmitkRenderWindow(QWidget *parent=nullptr, const QString &name="unnamed renderwindow", mitk::VtkPropRenderer *renderer=nullptr)
static const std::string KeyInsert
void wheelEvent(QWheelEvent *) override
void CrosshairRotationModeChanged(int)
void LayoutDesignListChanged(QmitkRenderWindowMenu::LayoutDesign layoutDesign)
void SetLayoutIndex(LayoutIndex layoutIndex)
void Initialize(const char *name="unnamed renderer")
virtual void SetResendQtEvents(bool resend)
Whether Qt events should be passed to parent (default: true)
void mouseDoubleClickEvent(QMouseEvent *event) override
static Pointer New(BaseRenderer *_arga, const Point2D &_argb, MouseButtons _argc, ModifierKeys _argd)
~QmitkRenderWindow() override
void CrosshairVisibilityChanged(bool)
void keyPressEvent(QKeyEvent *event) override
void ForceImmediateUpdate(vtkRenderWindow *renderWindow)
static const std::string KeyDelete
static const std::string KeyF5
static Pointer New(BaseRenderer *_arga, const Point2D &_argb, MouseButtons _argc, ModifierKeys _argd, MouseButtons _arge)
void moveEvent(QMoveEvent *event) override
void mouseMoveEvent(QMouseEvent *event) override
ViewDirection
Possible view directions for render windows.
void resizeGL(int w, int h) override
static Pointer New(BaseRenderer *_arga, DataInteractor *_argb, const std::string &_argc)
static const std::string KeyF6
void mousePressEvent(QMouseEvent *event) override
static const std::string KeyF3
static const std::string KeyF8
static const std::string KeyPageDown
static const std::string KeyReturn
void SetLayoutIndex(QmitkRenderWindowMenu::LayoutIndex layoutIndex)
void CrosshairRotationModeChanged(int)
static const std::string KeyArrowUp
void AdjustRenderWindowMenuVisibility(const QPoint &pos)
static const std::string KeyPageUp