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