Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
QVTKQuickItem.cxx
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 // original copyright below
18 
19 /*========================================================================
20  OpenView -- http://openview.kitware.com
21 
22  Copyright 2012 Kitware, Inc.
23 
24  Licensed under the BSD license. See LICENSE file for details.
25  ========================================================================*/
26 
27 #include "QVTKQuickItem.h"
28 
29 #include <QOpenGLContext>
30 #include <QQuickWindow>
31 #include <QThread>
32 #include <QSGSimpleRectNode>
33 #include <QSGSimpleTextureNode>
34 
35 #include "QVTKInteractor.h"
37 #include "vtkGenericOpenGLRenderWindow.h"
38 #include "vtkEventQtSlotConnect.h"
39 #include "vtkgl.h"
40 #include "vtkOpenGLExtensionManager.h"
41 #include "vtkRenderer.h"
42 
43 #include "vtkCubeSource.h"
44 #include "vtkPolyDataMapper.h"
45 #include "vtkProperty.h"
46 
47 #include <vtkFrameBufferObject2.h>
48 
51 
52 QVTKQuickItem::QVTKQuickItem(QQuickItem* parent)
53 : QQuickFramebufferObject(parent)
54 {
55  setAcceptHoverEvents(true);
56  setAcceptedMouseButtons(Qt::LeftButton | Qt::MiddleButton | Qt::RightButton);
57 
58  m_interactor = vtkSmartPointer<QVTKInteractor>::New();
59  m_interactorAdapter = new QVTKMitkInteractorAdapter(this);
61 
63  m_interactor->SetRenderWindow(m_win);
64 
65  m_connect->Connect(m_win, vtkCommand::WindowIsCurrentEvent, this, SLOT(IsCurrent(vtkObject*, unsigned long, void*, void*)), NULL, 0.0, Qt::DirectConnection);
66  m_connect->Connect(m_win, vtkCommand::WindowIsDirectEvent, this, SLOT(IsDirect(vtkObject*, unsigned long, void*, void*)), NULL, 0.0, Qt::DirectConnection);
67  m_connect->Connect(m_win, vtkCommand::WindowSupportsOpenGLEvent, this, SLOT(SupportsOpenGL(vtkObject*, unsigned long, void*, void*)), NULL, 0.0, Qt::DirectConnection);
68 
69  connect(this, SIGNAL(textureFollowsItemSizeChanged(bool)),
70  this, SLOT(onTextureFollowsItemSizeChanged(bool)));
71 }
72 
74 {
75  if(m_win)
76  {
77  m_connect->Disconnect(m_win, vtkCommand::WindowIsCurrentEvent, this, SLOT(IsCurrent(vtkObject*, unsigned long, void*, void*)));
78  m_connect->Disconnect(m_win, vtkCommand::WindowIsDirectEvent, this, SLOT(IsDirect(vtkObject*, unsigned long, void*, void*)));
79  m_connect->Disconnect(m_win, vtkCommand::WindowSupportsOpenGLEvent, this, SLOT(SupportsOpenGL(vtkObject*, unsigned long, void*, void*)));
80  m_win->Delete();
81  }
82 }
83 
84 QSGNode* QVTKQuickItem::updatePaintNode(QSGNode *node, QQuickItem::UpdatePaintNodeData *nodeData)
85 {
86  if (!node) {
87  node = QQuickFramebufferObject::updatePaintNode(node, nodeData);
88  QSGSimpleTextureNode *n = static_cast<QSGSimpleTextureNode *>(node);
89  if (n)
90  n->setTextureCoordinatesTransform(QSGSimpleTextureNode::MirrorVertically);
91  return node;
92  }
93  return QQuickFramebufferObject::updatePaintNode(node, nodeData);
94 }
95 
96 QQuickFramebufferObject::Renderer *QVTKQuickItem::createRenderer() const
97 {
98  return new QVTKFramebufferObjectRenderer(static_cast<vtkInternalOpenGLRenderWindow*>(m_win));
99 }
100 
101 vtkOpenGLRenderWindow* QVTKQuickItem::GetRenderWindow() const
102 {
103  return m_win;
104 }
105 
106 QVTKInteractor* QVTKQuickItem::GetInteractor() const
107 {
108  return m_interactor;
109 }
110 
111 void QVTKQuickItem::IsCurrent(vtkObject*, unsigned long, void*, void* call_data)
112 {
113  bool* ptr = reinterpret_cast<bool*>(call_data);
114  *ptr = QOpenGLContext::currentContext() == this->window()->openglContext();
115 }
116 
117 void QVTKQuickItem::IsDirect(vtkObject*, unsigned long, void*, void* call_data)
118 {
119  int* ptr = reinterpret_cast<int*>(call_data);
120  *ptr = 1;
121 }
122 
123 void QVTKQuickItem::SupportsOpenGL(vtkObject*, unsigned long, void*, void* call_data)
124 {
125  int* ptr = reinterpret_cast<int*>(call_data);
126  *ptr = 1;
127 }
128 
130 {
131  if (!follows)
132  {
133  qWarning("QVTKQuickItem: Mouse interaction is not (yet) supported when textureFollowsItemSize==false");
134  }
135 }
136 
137 void QVTKQuickItem::geometryChanged(const QRectF & newGeometry, const QRectF & oldGeometry)
138 {
139  QQuickFramebufferObject::geometryChanged(newGeometry, oldGeometry);
140  QSize oldSize(oldGeometry.width(), oldGeometry.height());
141  QSize newSize(newGeometry.width(), newGeometry.height());
142  QResizeEvent e(newSize, oldSize);
143  if (m_interactorAdapter)
144  {
145  this->m_viewLock.lock();
146  m_interactorAdapter->ProcessEvent(&e, m_interactor);
147  this->m_viewLock.unlock();
148  }
149 }
150 
152 {
153  e->accept();
154  this->m_viewLock.lock();
155  m_interactorAdapter->ProcessEvent(e, m_interactor);
156  this->m_viewLock.unlock();
157  update();
158 }
159 
161 {
162  e->accept();
163  this->m_viewLock.lock();
164  m_interactorAdapter->ProcessEvent(e, m_interactor);
165  this->m_viewLock.unlock();
166  update();
167 }
168 
169 void QVTKQuickItem::mousePressEvent(QMouseEvent* e)
170 {
171  e->accept();
172  this->m_viewLock.lock();
173  m_interactorAdapter->ProcessEvent(e, m_interactor);
174  this->m_viewLock.unlock();
175  update();
176 }
177 
179 {
180  e->accept();
181  this->m_viewLock.lock();
182  m_interactorAdapter->ProcessEvent(e, m_interactor);
183  this->m_viewLock.unlock();
184  update();
185 }
186 
188 {
189  e->accept();
190  this->m_viewLock.lock();
191  m_interactorAdapter->ProcessEvent(e, m_interactor);
192  this->m_viewLock.unlock();
193  update();
194 }
195 
196 void QVTKQuickItem::mouseMoveEvent(QMouseEvent* e)
197 {
198  e->accept();
199  this->m_viewLock.lock();
200  m_interactorAdapter->ProcessEvent(e, m_interactor);
201  this->m_viewLock.unlock();
202  update();
203 }
204 
205 void QVTKQuickItem::wheelEvent(QWheelEvent* e)
206 {
207  e->accept();
208  this->m_viewLock.lock();
209  m_interactorAdapter->ProcessEvent(e, m_interactor);
210  this->m_viewLock.unlock();
211  update();
212 }
213 
214 void QVTKQuickItem::hoverEnterEvent(QHoverEvent* e)
215 {
216  e->accept();
217  QEvent e2(QEvent::Enter);
218  this->m_viewLock.lock();
219  m_interactorAdapter->ProcessEvent(&e2, m_interactor);
220  this->m_viewLock.unlock();
221  update();
222 }
223 
224 void QVTKQuickItem::hoverLeaveEvent(QHoverEvent* e)
225 {
226  e->accept();
227  QEvent e2(QEvent::Leave);
228  this->m_viewLock.lock();
229  m_interactorAdapter->ProcessEvent(&e2, m_interactor);
230  this->m_viewLock.unlock();
231  update();
232 }
233 
234 void QVTKQuickItem::hoverMoveEvent(QHoverEvent* e)
235 {
236  e->accept();
237  QMouseEvent e2(QEvent::MouseMove, e->pos(), Qt::NoButton, Qt::NoButton, e->modifiers());
238  this->m_viewLock.lock();
239  m_interactorAdapter->ProcessEvent(&e2, m_interactor);
240  this->m_viewLock.unlock();
241  update();
242 }
243 
245 {
246  m_win->OpenGLInitContext();
247  m_win->GetExtensionManager()->LoadExtension("GL_VERSION_1_4");
248  m_win->GetExtensionManager()->LoadExtension("GL_VERSION_2_0");
249 }
250 
252 {
253  return true;
254 }
255 
257 {
258 }
virtual void mouseDoubleClickEvent(QMouseEvent *e)
QMutex m_viewLock
Definition: QVTKQuickItem.h:72
virtual void cleanupAfterRender()
virtual bool prepareForRender()
virtual void mouseMoveEvent(QMouseEvent *e)
virtual void hoverLeaveEvent(QHoverEvent *e)
virtual void IsCurrent(vtkObject *caller, unsigned long vtk_event, void *client_data, void *call_data)
virtual void SupportsOpenGL(vtkObject *caller, unsigned long vtk_event, void *client_data, void *call_data)
QVTKInteractor * GetInteractor() const
Renderer * createRenderer() const
virtual void keyReleaseEvent(QKeyEvent *e)
virtual void hoverEnterEvent(QHoverEvent *e)
virtual void wheelEvent(QWheelEvent *e)
virtual void mouseReleaseEvent(QMouseEvent *e)
virtual void IsDirect(vtkObject *caller, unsigned long vtk_event, void *client_data, void *call_data)
virtual void keyPressEvent(QKeyEvent *e)
virtual void mousePressEvent(QMouseEvent *e)
bool ProcessEvent(QEvent *e, vtkRenderWindowInteractor *iren)
friend class QVTKFramebufferObjectRenderer
virtual void hoverMoveEvent(QHoverEvent *e)
virtual void geometryChanged(const QRectF &newGeometry, const QRectF &oldGeometry)
static vtkInternalOpenGLRenderWindow * New()
QSGNode * updatePaintNode(QSGNode *node, QQuickItem::UpdatePaintNodeData *nodeData)
void onTextureFollowsItemSizeChanged(bool follows)
virtual void init()
QVTKQuickItem(QQuickItem *parent=0)
vtkOpenGLRenderWindow * GetRenderWindow() const
static itkEventMacro(BoundingShapeInteractionEvent, itk::AnyEvent) class MITKBOUNDINGSHAPE_EXPORT BoundingShapeInteractor Pointer New()
Basic interaction methods for mitk::GeometryData.