VTK
QVTKWidget.h
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: Visualization Toolkit
4  Module: QVTKWidget.h
5 
6  Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
7  All rights reserved.
8  See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
9 
10  This software is distributed WITHOUT ANY WARRANTY; without even
11  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12  PURPOSE. See the above copyright notice for more information.
13 
14 =========================================================================*/
15 
16 /*=========================================================================
17 
18  Copyright 2004 Sandia Corporation.
19  Under the terms of Contract DE-AC04-94AL85000, there is a non-exclusive
20  license for use of this work by or on behalf of the
21  U.S. Government. Redistribution and use in source and binary forms, with
22  or without modification, are permitted provided that this Notice and any
23  statement of authorship are reproduced on all copies.
24 
25 =========================================================================*/
26 
35 #ifndef Q_VTK_WIDGET_H
36 #define Q_VTK_WIDGET_H
37 
38 #include "vtkGUISupportQtModule.h" // For export macro
39 #include "QVTKInteractor.h"
40 #include <QWidget>
41 #include <QTimer>
42 
44 
45 class vtkRenderWindow;
46 #include <vtkConfigure.h>
47 #include <vtkToolkits.h>
48 class vtkImageData;
49 
50 #include "vtkTDxConfigure.h" // defines VTK_USE_TDX
51 #ifdef VTK_USE_TDX
52 class vtkTDxDevice;
53 #endif
54 
55 #if defined(Q_WS_MAC)
56 # if defined(QT_MAC_USE_COCOA) && defined(VTK_USE_COCOA)
57 # define QVTK_USE_COCOA
58 # elif defined(VTK_USE_COCOA)
59 # error "VTK configured to use Cocoa, but Qt configured to use Carbon"
60 # endif
61 #endif
62 
63 
64 #include "QVTKWin32Header.h"
65 
66 class VTKGUISUPPORTQT_EXPORT QVTKWidget : public QWidget
67 {
68  Q_OBJECT
69 
70  Q_PROPERTY(bool automaticImageCacheEnabled
71  READ isAutomaticImageCacheEnabled
72  WRITE setAutomaticImageCacheEnabled)
73  Q_PROPERTY(double maxRenderRateForImageCache
74  READ maxRenderRateForImageCache
75  WRITE setMaxRenderRateForImageCache)
76  Q_PROPERTY(bool deferRenderInPaintEvent
77  READ deferRenderInPaintEvent
78  WRITE setDeferRenderInPaintEvent)
79 
80 public:
82  VTK_LEGACY(QVTKWidget(QWidget *parent = nullptr, Qt::WindowFlags f = 0));
84  ~QVTKWidget() override;
85 
86  // Description:
87  // Set the vtk render window, if you wish to use your own vtkRenderWindow
88  virtual void SetRenderWindow(vtkRenderWindow*);
89 
90  // Description:
91  // Get the vtk render window.
92  virtual vtkRenderWindow* GetRenderWindow();
93 
94  // Description:
95  // Get the Qt/vtk interactor that was either created by default or set by the user
96  virtual QVTKInteractor* GetInteractor();
97 
98  // Description:
99  // Enum for additional event types supported.
100  // These events can be picked up by command observers on the interactor
102  {
108  };
109 
110  // Description:
111  // Enables/disables automatic image caching. If disabled (the default),
112  // QVTKWidget will not call saveImageToCache() on its own.
113  virtual void setAutomaticImageCacheEnabled(bool flag);
114  virtual bool isAutomaticImageCacheEnabled() const;
115 
116  // Description:
117  // If automatic image caching is enabled, then the image will be cached
118  // after every render with a DesiredUpdateRate that is less than
119  // this parameter. By default, the vtkRenderWindowInteractor will
120  // change the desired render rate depending on the user's
121  // interactions. (See vtkRenderWindow::DesiredUpdateRate,
122  // vtkRenderWindowInteractor::DesiredUpdateRate and
123  // vtkRenderWindowInteractor::StillUpdateRate for more details.)
124  virtual void setMaxRenderRateForImageCache(double rate);
125  virtual double maxRenderRateForImageCache() const;
126 
127  // Description:
128  // Returns the current image in the window. If the image cache is up
129  // to date, that is returned to avoid grabbing other windows.
130  virtual vtkImageData* cachedImage();
131 
132  // Description:
133  // Handle showing of the Widget
134  void showEvent(QShowEvent*) override;
135 
136  QPaintEngine* paintEngine() const override;
137 
138  // Description:
139  // Use a 3DConnexion device. Initial value is false.
140  // If VTK is not build with the TDx option, this is no-op.
141  // If VTK is build with the TDx option, and a device is not connected,
142  // a warning is emitted.
143  // It is must be called before the first Render to be effective, otherwise
144  // it is ignored.
145  void SetUseTDx(bool useTDx);
146  bool GetUseTDx() const;
147 
148  // Description:
149  // When set to true (default is false), paintEvent() will never directly trigger
150  // a render on the vtkRenderWindow (via vtkRenderWindowInteractor::Render()).
151  // Instead, it starts a timer that then triggers the render on idle. This, in
152  // general is a good strategy for cases where Render may take a while with
153  // applications wanting to report progress and consequently trigger paint
154  // events on other widgets like progress bars, etc.
155  // There is one caveat: when paintEvent() is called using a redirected paint device,
156  // then this flag is ignored and the paintEvent() will trigger
157  // vtkRenderWindowInteractor::Render(), if needed.
158  void setDeferRenderInPaintEvent(bool val);
159  bool deferRenderInPaintEvent() const;
160 
161 Q_SIGNALS:
162  // Description:
163  // This signal will be emitted whenever a mouse event occurs
164  // within the QVTK window
165  void mouseEvent(QMouseEvent* event);
166 
167  // Description:
168  // This signal will be emitted whenever the cached image goes from clean
169  // to dirty.
170  void cachedImageDirty();
171 
172  // Description:
173  // This signal will be emitted whenever the cached image is refreshed.
174  void cachedImageClean();
175 
176 public Q_SLOTS:
177  // Description:
178  // This will mark the cached image as dirty. This slot is automatically
179  // invoked whenever the render window has a render event or the widget is
180  // resized. Your application should invoke this slot whenever the image in
181  // the render window is changed by some other means. If the image goes
182  // from clean to dirty, the cachedImageDirty() signal is emitted.
183  void markCachedImageAsDirty();
184 
185  // Description:
186  // If the cached image is dirty, it is updated with the current image in
187  // the render window and the cachedImageClean() signal is emitted.
188  void saveImageToCache();
189 
190  // Description:
191  // Receive notification of the creation of the TDxDevice.
192  // Only relevant for Unix.
193 #ifdef VTK_USE_TDX
194  void setDevice(vtkTDxDevice *device);
195 #endif
196 
197 protected Q_SLOTS:
198  // Description:
199  // Request to defer a render call i.e. start the mDeferedRenderTimer. When the
200  // timer times out, it will call doDeferredRender() to do the actual
201  // rendering.
202  virtual void deferRender();
203 
204  // Description:
205  // Called when the mDeferedRenderTimer times out to do the rendering.
206  virtual void doDeferredRender();
207 
208 protected:
209  // overloaded resize handler
210  void resizeEvent(QResizeEvent* event) override;
211  // overloaded move handler
212  void moveEvent(QMoveEvent* event) override;
213  // overloaded paint handler
214  void paintEvent(QPaintEvent* event) override;
215 
216  // overloaded mouse press handler
217  void mousePressEvent(QMouseEvent* event) override;
218  // overloaded mouse move handler
219  void mouseMoveEvent(QMouseEvent* event) override;
220  // overloaded mouse release handler
221  void mouseReleaseEvent(QMouseEvent* event) override;
222  // overloaded key press handler
223  void keyPressEvent(QKeyEvent* event) override;
224  // overloaded key release handler
225  void keyReleaseEvent(QKeyEvent* event) override;
226  // overloaded enter event
227  void enterEvent(QEvent*) override;
228  // overloaded leave event
229  void leaveEvent(QEvent*) override;
230 #ifndef QT_NO_WHEELEVENT
231  // overload wheel mouse event
232  void wheelEvent(QWheelEvent*) override;
233 #endif
234  // overload focus event
235  void focusInEvent(QFocusEvent*) override;
236  // overload focus event
237  void focusOutEvent(QFocusEvent*) override;
238  // overload Qt's event() to capture more keys
239  bool event( QEvent* e ) override;
240 
241  // overload context menu event
242  void contextMenuEvent(QContextMenuEvent*) override;
243  // overload drag enter event
244  void dragEnterEvent(QDragEnterEvent*) override;
245  // overload drag move event
246  void dragMoveEvent(QDragMoveEvent*) override;
247  // overload drag leave event
248  void dragLeaveEvent(QDragLeaveEvent*) override;
249  // overload drop event
250  void dropEvent(QDropEvent*) override;
251 
252  // method called in paintEvent() to render the image cache on to the device.
253  // return false, if cache couldn;t be used for painting. In that case, the
254  // paintEvent() method will continue with the default painting code.
255  virtual bool paintCachedImage();
256 
257  // the vtk render window
259  bool UseTDx;
260 
261  // the paint engine
262  QPaintEngine* mPaintEngine;
263 
264  // set up an X11 window based on a visual and colormap
265  // that VTK chooses
266  void x11_setup_window();
267 
268 #if defined(Q_OS_WIN)
269  bool winEvent(MSG* msg, long* result);
270 
271  bool nativeEvent(const QByteArray& eventType, void* message, long* result);
272 
273 #endif
274 
275 protected:
282 
283 
284 private:
285  QVTKWidget & operator=(QVTKWidget const&) = delete;
286  QVTKWidget(const QVTKWidget&) = delete;
287 
288  unsigned long renderEventCallbackObserverId;
289 
290  // Description:
291  // Callback called on every vtkCommand::RenderEvent fired by the
292  // vtkRenderWindow.
293  void renderEventCallback();
294  QTimer mDeferedRenderTimer;
295 };
296 
297 #endif
double maxImageCacheRenderRate
Definition: QVTKWidget.h:279
QPaintEngine * mPaintEngine
Definition: QVTKWidget.h:262
vtkRenderWindow * mRenWin
Definition: QVTKWidget.h:258
bool mDeferRenderInPaintEvent
Definition: QVTKWidget.h:281
vtkImageData * mCachedImage
Definition: QVTKWidget.h:276
an interactor for QVTKOpenGLNativeWidget (and QVTKWiget).
topologically and geometrically regular array of data
Definition: vtkImageData.h:45
QVTKInteractorAdapter * mIrenAdapter
Definition: QVTKWidget.h:280
create a window for renderers to draw into
bool cachedImageCleanFlag
Definition: QVTKWidget.h:277
bool UseTDx
Definition: QVTKWidget.h:259
display a vtkRenderWindow in a Qt&#39;s QWidget.
bool automaticImageCache
Definition: QVTKWidget.h:278