Fraxinus  2023.01.05-dev+develop.0da12
An IGT application
QVTKWidget3.cpp
Go to the documentation of this file.
1 #include "qvtkwidget3.h"
2 
3 #include "vtkRenderWindowInteractor.h"
4 #include "vtkInteractorStyle.h"
5 #include "vtkInteractorStyleTrackballCamera.h"
6 
7 #include <QResizeEvent>
8 
9 QVTKWidget3::QVTKWidget3(QWidget *parent, Qt::WindowFlags f, QSurfaceFormat format)
10  : QOpenGLWidget(parent, f)
11  , m_renWin(nullptr)
12 {
13  // VTK requires a compatibility profile
14  format.setProfile(QSurfaceFormat::CompatibilityProfile);
15  setFormat(format);
16 
17  // Initialize interactors
18  m_irenAdapter = new QVTKInteractorAdapter(this);
19  m_connect = vtkSmartPointer<vtkEventQtSlotConnect>::New();
20 }
21 
22 // Destructor
24 {
25  // Following line is not needed.
26  // get rid of the VTK window
27  // this->SetRenderWindow(NULL);
28 }
29 
30 // GetRenderWindow
31 vtkGenericOpenGLRenderWindow* QVTKWidget3::GetRenderWindow()
32 {
33  if (this->m_renWin == nullptr)
34  {
35  // create a default vtk window
36  vtkGenericOpenGLRenderWindow* win = vtkGenericOpenGLRenderWindow::New();
37  this->SetRenderWindow(win);
38  }
39 
40  return this->m_renWin;
41 }
42 
43 // SetRenderWindow
44 void QVTKWidget3::SetRenderWindow(vtkGenericOpenGLRenderWindow* w)
45 {
46  // do nothing if we don't have to
47  if(w == this->m_renWin) {
48  return;
49  }
50 
51  // unregister previous window
52  if(this->m_renWin != nullptr) {
53  this->m_renWin->Finalize();
54  this->m_renWin->SetMapped(0);
55 
56  m_connect->Disconnect(m_renWin, vtkCommand::WindowMakeCurrentEvent, this, SLOT(MakeCurrent()));
57  m_connect->Disconnect(m_renWin, vtkCommand::WindowIsCurrentEvent, this, SLOT(IsCurrent(vtkObject*, unsigned long, void*, void*)));
58  m_connect->Disconnect(m_renWin, vtkCommand::WindowFrameEvent, this, SLOT(Frame()));
59  m_connect->Disconnect(m_renWin, vtkCommand::StartEvent, this, SLOT(Start()));
60  m_connect->Disconnect(m_renWin, vtkCommand::EndEvent, this, SLOT(End()));
61  m_connect->Disconnect(m_renWin, vtkCommand::WindowIsDirectEvent, this, SLOT(IsDirect(vtkObject*, unsigned long, void*, void*)));
62  m_connect->Disconnect(m_renWin, vtkCommand::WindowSupportsOpenGLEvent, this, SLOT(SupportsOpenGL(vtkObject*, unsigned long, void*, void*)));
63  }
64 
65  // now set the window
66  this->m_renWin = w;
67 
68  if(this->m_renWin != nullptr) {
69  // if it is mapped somewhere else, unmap it
70  this->m_renWin->Finalize();
71  this->m_renWin->SetMapped(1);
72 
73  // tell the vtk window what the size of this window is
74  this->m_renWin->SetSize(this->width(), this->height());
75  this->m_renWin->SetPosition(this->x(), this->y());
76 
77  // if an interactor wasn't provided, we'll make one by default
78  if(this->m_renWin->GetInteractor() == NULL)
79  {
80  // create a default interactor
81  QVTKInteractor* iren = QVTKInteractor::New();
82  iren->SetUseTDx(false);
83  this->m_renWin->SetInteractor(iren);
84  iren->Initialize();
85 
86  // now set the default style
87  vtkInteractorStyle* s = vtkInteractorStyleTrackballCamera::New();
88  iren->SetInteractorStyle(s);
89 
90  iren->Delete();
91  s->Delete();
92  }
93 
94  // tell the interactor the size of this window
95  this->m_renWin->GetInteractor()->SetSize(this->width(), this->height());
96 
97  m_connect->Connect(m_renWin, vtkCommand::WindowMakeCurrentEvent, this, SLOT(MakeCurrent()));
98  m_connect->Connect(m_renWin, vtkCommand::WindowIsCurrentEvent, this, SLOT(IsCurrent(vtkObject*, unsigned long, void*, void*)));
99  m_connect->Connect(m_renWin, vtkCommand::WindowFrameEvent, this, SLOT(Frame()));
100  m_connect->Connect(m_renWin, vtkCommand::StartEvent, this, SLOT(Start()));
101  m_connect->Connect(m_renWin, vtkCommand::EndEvent, this, SLOT(End()));
102  m_connect->Connect(m_renWin, vtkCommand::WindowIsDirectEvent, this, SLOT(IsDirect(vtkObject*, unsigned long, void*, void*)));
103  m_connect->Connect(m_renWin, vtkCommand::WindowSupportsOpenGLEvent, this, SLOT(SupportsOpenGL(vtkObject*, unsigned long, void*, void*)));
104  }
105 }
106 
107 // GetInteractor
108 QVTKInteractor* QVTKWidget3::GetInteractor()
109 {
110  return QVTKInteractor::SafeDownCast(this->GetRenderWindow()->GetInteractor());
111 }
112 
113 // Initialize
115 {
116  if(this->m_renWin == nullptr) {
117  return;
118  }
119 
120  this->m_renWin->OpenGLInitContext();
121 }
122 
123 // Paint
125 {
126  vtkRenderWindowInteractor* iren = nullptr;
127  if(this->m_renWin != nullptr) {
128  iren = this->m_renWin->GetInteractor();
129  }
130 
131  if(iren == nullptr || !iren->GetEnabled()) {
132  return;
133  }
134 
135  iren->Render();
136 }
137 
138 // Resize
139 void QVTKWidget3::resizeGL(int w, int h)
140 {
141  if(this->m_renWin == nullptr) {
142  return;
143  }
144 
145  this->m_renWin->SetSize(w,h);
146 
147  // and update the interactor
148  if(this->m_renWin->GetInteractor() != NULL) {
149  QResizeEvent e(QSize(w,h), QSize());
150  m_irenAdapter->ProcessEvent(&e, this->m_renWin->GetInteractor());
151  }
152 }
153 
154 // Move
155 void QVTKWidget3::moveEvent(QMoveEvent* e)
156 {
157  QWidget::moveEvent(e);
158 
159  if(this->m_renWin == nullptr) {
160  return;
161  }
162 
163  this->m_renWin->SetPosition(this->x(), this->y());
164 }
165 
166 
167 // --------
168 // Slots
169 // --------
170 
172 {
173  makeCurrent();
174  m_renWin->PushState();
175  m_renWin->OpenGLInitState();
176 }
177 
179 {
180  m_renWin->PopState();
181 }
182 
184 {
185  return;
186  // Automaticly handled by QOpenGLWidget
187  // this->makeCurrent();
188 }
189 
190 void QVTKWidget3::IsCurrent(vtkObject*, unsigned long, void*, void* call_data)
191 {
192  bool* ptr = reinterpret_cast<bool*>(call_data);
193  *ptr = (int)true;
194 }
195 
196 void QVTKWidget3::IsDirect(vtkObject*, unsigned long, void*, void* call_data)
197 {
198  int* ptr = reinterpret_cast<int*>(call_data);
199  *ptr = (int)true;
200 }
201 
202 void QVTKWidget3::SupportsOpenGL(vtkObject*, unsigned long, void*, void* call_data)
203 {
204  int* ptr = reinterpret_cast<int*>(call_data);
205  *ptr = (int)true;
206 }
207 
209 {
210  if(m_renWin->GetSwapBuffers()) {
211  this->update();
212  }
213 
214  // This callback will call swapBuffers() for us
215  // because sometimes VTK does a render without coming through this paintGL()
216 
217  // FOLLOWING NOT TESTED FOR QOPENGLWIDGET
218  // if you want paintGL to always be called for each time VTK renders
219  // 1. turn off EnableRender on the interactor,
220  // 2. turn off SwapBuffers on the render window,
221  // 3. add an observer for the RenderEvent coming from the interactor
222  // 4. implement the callback on the observer to call updateGL() on this widget
223  // 5. overload QVTKWidget3::paintGL() to call m_renWin->Render() instead iren->Render()
224 }
225 
226 // ----------------------
227 // Interaction handlers
228 // ----------------------
229 
232 void QVTKWidget3::mousePressEvent(QMouseEvent* e)
233 {
234  if(this->m_renWin)
235  {
236  m_irenAdapter->ProcessEvent(e, this->m_renWin->GetInteractor());
237  }
238 
239 }
240 
243 void QVTKWidget3::mouseMoveEvent(QMouseEvent* e)
244 {
245  if(this->m_renWin)
246  {
247  m_irenAdapter->ProcessEvent(e, this->m_renWin->GetInteractor());
248  }
249 }
250 
253 void QVTKWidget3::enterEvent(QEvent* e)
254 {
255  if(this->m_renWin)
256  {
257  m_irenAdapter->ProcessEvent(e, this->m_renWin->GetInteractor());
258  }
259 }
260 
263 void QVTKWidget3::leaveEvent(QEvent* e)
264 {
265  if(this->m_renWin)
266  {
267  m_irenAdapter->ProcessEvent(e, this->m_renWin->GetInteractor());
268  }
269 }
270 
273 void QVTKWidget3::mouseReleaseEvent(QMouseEvent* e)
274 {
275  if(this->m_renWin)
276  {
277  m_irenAdapter->ProcessEvent(e, this->m_renWin->GetInteractor());
278  }
279 }
280 
283 void QVTKWidget3::keyPressEvent(QKeyEvent* e)
284 {
285  if(this->m_renWin)
286  {
287  m_irenAdapter->ProcessEvent(e, this->m_renWin->GetInteractor());
288  }
289 }
290 
294 {
295  if(this->m_renWin)
296  {
297  m_irenAdapter->ProcessEvent(e, this->m_renWin->GetInteractor());
298  }
299 }
300 
301 void QVTKWidget3::wheelEvent(QWheelEvent* e)
302 {
303  if(this->m_renWin)
304  {
305  m_irenAdapter->ProcessEvent(e, this->m_renWin->GetInteractor());
306  }
307 }
308 
309 void QVTKWidget3::contextMenuEvent(QContextMenuEvent* e)
310 {
311  if(this->m_renWin)
312  {
313  m_irenAdapter->ProcessEvent(e, this->m_renWin->GetInteractor());
314  }
315 }
316 
317 void QVTKWidget3::dragEnterEvent(QDragEnterEvent* e)
318 {
319  if(this->m_renWin)
320  {
321  m_irenAdapter->ProcessEvent(e, this->m_renWin->GetInteractor());
322  }
323 }
324 
325 void QVTKWidget3::dragMoveEvent(QDragMoveEvent* e)
326 {
327  if(this->m_renWin)
328  {
329  m_irenAdapter->ProcessEvent(e, this->m_renWin->GetInteractor());
330  }
331 }
332 
333 void QVTKWidget3::dragLeaveEvent(QDragLeaveEvent* e)
334 {
335  if(this->m_renWin)
336  {
337  m_irenAdapter->ProcessEvent(e, this->m_renWin->GetInteractor());
338  }
339 }
340 
341 void QVTKWidget3::dropEvent(QDropEvent* e)
342 {
343  if(this->m_renWin)
344  {
345  m_irenAdapter->ProcessEvent(e, this->m_renWin->GetInteractor());
346  }
347 }
348 
350 {
351  return false;
352 }
virtual void End()
Slot called when vtk wants to end the render.
virtual void MakeCurrent()
Slot to make this vtk render window current.
virtual void wheelEvent(QWheelEvent *)
virtual void SupportsOpenGL(vtkObject *caller, unsigned long vtk_event, void *client_data, void *call_data)
Slot called when vtk wants to know if a window supports OpenGL.
virtual void IsDirect(vtkObject *caller, unsigned long vtk_event, void *client_data, void *call_data)
Slot called when vtk wants to know if a window is direct.
virtual void leaveEvent(QEvent *)
virtual void dropEvent(QDropEvent *)
virtual void mouseReleaseEvent(QMouseEvent *event)
virtual void keyReleaseEvent(QKeyEvent *event)
vtkGenericOpenGLRenderWindow * m_renWin
Definition: QVTKWidget3.h:74
virtual vtkGenericOpenGLRenderWindow * GetRenderWindow()
Returns the curren render window (creates one if none exists)
Definition: QVTKWidget3.cpp:31
virtual void paintGL()
Paint handler.
virtual void Frame()
Slot called when vtk wants to frame the window.
virtual void IsCurrent(vtkObject *caller, unsigned long vtk_event, void *client_data, void *call_data)
Slot called when vtk wants to know if the context is current.
virtual void mouseMoveEvent(QMouseEvent *event)
QVTKWidget3(QWidget *parent=NULL, Qt::WindowFlags f=0, QSurfaceFormat format=QSurfaceFormat::defaultFormat())
Definition: QVTKWidget3.cpp:9
virtual void enterEvent(QEvent *)
virtual void contextMenuEvent(QContextMenuEvent *)
virtual void keyPressEvent(QKeyEvent *event)
virtual void dragLeaveEvent(QDragLeaveEvent *)
virtual void dragEnterEvent(QDragEnterEvent *)
virtual void initializeGL()
Initialize handler.
virtual void mousePressEvent(QMouseEvent *event)
virtual void SetRenderWindow(vtkGenericOpenGLRenderWindow *)
Set a custom render window.
Definition: QVTKWidget3.cpp:44
vtkSmartPointer< vtkEventQtSlotConnect > m_connect
Definition: QVTKWidget3.h:76
virtual QVTKInteractor * GetInteractor()
Returns interactor of the current render window.
virtual void moveEvent(QMoveEvent *event)
Move handler.
QVTKInteractorAdapter * m_irenAdapter
Definition: QVTKWidget3.h:75
virtual void Start()
Slot called when vtk wants to start the render.
virtual void resizeGL(int, int)
Resize handler.
virtual void dragMoveEvent(QDragMoveEvent *)
virtual bool focusNextPrevChild(bool)
virtual ~QVTKWidget3()
Definition: QVTKWidget3.cpp:23