1 /*=========================================================================
\r
3 Copyright 2004 Sandia Corporation.
\r
4 Under the terms of Contract DE-AC04-94AL85000, there is a non-exclusive
\r
5 license for use of this work by or on behalf of the
\r
6 U.S. Government. Redistribution and use in source and binary forms, with
\r
7 or without modification, are permitted provided that this Notice and any
\r
8 statement of authorship are reproduced on all copies.
\r
10 =========================================================================*/
\r
12 /*========================================================================
\r
13 For general information about using VTK and Qt, see:
\r
14 http://www.trolltech.com/products/3rdparty/vtksupport.html
\r
15 =========================================================================*/
\r
17 /*========================================================================
\r
18 !!! WARNING for those who want to contribute code to this file.
\r
19 !!! If you use a commercial edition of Qt, you can modify this code.
\r
20 !!! If you use an open source version of Qt, you are free to modify
\r
21 !!! and use this code within the guidelines of the GPL license.
\r
22 !!! Unfortunately, you cannot contribute the changes back into this
\r
23 !!! file. Doing so creates a conflict between the GPL and BSD-like VTK
\r
25 =========================================================================*/
\r
27 // .NAME QVTKWidget - Display a vtkRenderWindow in a Qt's QWidget.
\r
28 // .SECTION Description
\r
29 // QVTKWidget provides a way to display VTK data in a Qt widget.
\r
31 #ifndef Q_VTK_WIDGET_H
\r
32 #define Q_VTK_WIDGET_H
\r
34 #include <qwidget.h>
\r
37 class vtkRenderWindow;
\r
38 class QVTKInteractor;
\r
39 #include <vtkRenderWindowInteractor.h>
\r
40 #include <vtkCommand.h>
\r
41 #include <vtkConfigure.h>
\r
42 class vtkUnsignedCharArray;
\r
44 #if defined(Q_WS_MAC) && QT_VERSION >= 0x040000
\r
45 #include <Carbon/Carbon.h> // Event handling for dirty region
\r
48 #if defined(WIN32) && defined(VTK_BUILD_SHARED_LIBS)
\r
49 #if defined(QVTK_EXPORTS) || defined(QVTKWidgetPlugin_EXPORTS)
\r
50 #define QVTK_EXPORT __declspec( dllexport )
\r
52 #define QVTK_EXPORT __declspec( dllimport )
\r
59 //! QVTKWidget displays a VTK window in a Qt window.
\r
60 class QVTK_EXPORT QVTKWidget : public QWidget
\r
64 Q_PROPERTY(bool automaticImageCacheEnabled
\r
65 READ isAutomaticImageCacheEnabled
\r
66 WRITE setAutomaticImageCacheEnabled)
\r
67 Q_PROPERTY(double maxRenderRateForImageCache
\r
68 READ maxRenderRateForImageCache
\r
69 WRITE setMaxRenderRateForImageCache)
\r
70 Q_PROPERTY(QString text READ text WRITE setText)
\r
73 #if QT_VERSION < 0x040000
\r
74 //! constructor for Qt 3
\r
75 QVTKWidget(QWidget* parent = NULL, const char* name = NULL, Qt::WFlags f = 0);
\r
77 //! constructor for Qt 4
\r
78 QVTKWidget(QWidget* parent = NULL, Qt::WFlags f = 0);
\r
81 QString text() const {
\r
86 virtual ~QVTKWidget();
\r
89 // Set the vtk render window, if you wish to use your own vtkRenderWindow
\r
90 virtual void SetRenderWindow(vtkRenderWindow*);
\r
93 // Get the vtk render window.
\r
94 virtual vtkRenderWindow* GetRenderWindow();
\r
97 // Get the Qt/vtk interactor that was either created by default or set by the user
\r
98 virtual QVTKInteractor* GetInteractor();
\r
101 // Enum for additional event types supported.
\r
102 // These events can be picked up by command observers on the interactor
\r
103 enum vtkCustomEvents
\r
105 ContextMenuEvent = vtkCommand::UserEvent + 100,
\r
113 // Enables/disables automatic image caching. If disabled (the default),
\r
114 // QVTKWidget will not call saveImageToCache() on its own.
\r
115 virtual void setAutomaticImageCacheEnabled(bool flag);
\r
116 virtual bool isAutomaticImageCacheEnabled() const;
\r
119 // If automatic image caching is enabled, then the image will be cached
\r
120 // after every render with a DesiredUpdateRate that is greater than
\r
121 // this parameter. By default, the vtkRenderWindowInteractor will
\r
122 // change the desired render rate depending on the user's
\r
123 // interactions. (See vtkRenderWindow::DesiredUpdateRate,
\r
124 // vtkRenderWindowInteractor::DesiredUpdateRate and
\r
125 // vtkRenderWindowInteractor::StillUpdateRate for more details.)
\r
126 virtual void setMaxRenderRateForImageCache(double rate);
\r
127 virtual double maxRenderRateForImageCache() const;
\r
130 // Returns the current image in the window. If the image cache is up
\r
131 // to date, that is returned to avoid grabbing other windows.
\r
132 virtual vtkUnsignedCharArray* cachedImage();
\r
134 #if QT_VERSION < 0x040000
\r
136 // Handle reparenting of this widget in Qt 3.x
\r
137 virtual void reparent(QWidget* parent, Qt::WFlags f, const QPoint& p, bool showit);
\r
141 // Handle showing of the Widget
\r
142 virtual void showEvent(QShowEvent*);
\r
144 virtual QPaintEngine* paintEngine() const;
\r
148 // This signal will be emitted whenever a mouse event occurs
\r
149 // within the QVTK window
\r
150 void mouseEvent(QMouseEvent* event);
\r
153 // This signal will be emitted whenever the cached image goes from clean
\r
155 void cachedImageDirty();
\r
158 // This signal will be emitted whenever the cached image is refreshed.
\r
159 void cachedImageClean();
\r
163 // This will mark the cached image as dirty. This slot is automatically
\r
164 // invoked whenever the render window has a render event or the widget is
\r
165 // resized. Your application should invoke this slot whenever the image in
\r
166 // the render window is changed by some other means. If the image goes
\r
167 // from clean to dirty, the cachedImageDirty() signal is emitted.
\r
168 void markCachedImageAsDirty();
\r
171 // If the cached image is dirty, it is updated with the current image in
\r
172 // the render window and the cachedImageClean() signal is emitted.
\r
173 void saveImageToCache();
\r
175 void setText(const QString &) {};
\r
178 // overloaded resize handler
\r
179 virtual void resizeEvent(QResizeEvent* event);
\r
180 // overloaded move handler
\r
181 virtual void moveEvent(QMoveEvent* event);
\r
182 // overloaded paint handler
\r
183 virtual void paintEvent(QPaintEvent* event);
\r
185 // overloaded mouse press handler
\r
186 virtual void mousePressEvent(QMouseEvent* event);
\r
187 // overloaded mouse move handler
\r
188 virtual void mouseMoveEvent(QMouseEvent* event);
\r
189 // overloaded mouse release handler
\r
190 virtual void mouseReleaseEvent(QMouseEvent* event);
\r
191 // overloaded key press handler
\r
192 virtual void keyPressEvent(QKeyEvent* event);
\r
193 // overloaded key release handler
\r
194 virtual void keyReleaseEvent(QKeyEvent* event);
\r
195 // overloaded enter event
\r
196 virtual void enterEvent(QEvent*);
\r
197 // overloaded leave event
\r
198 virtual void leaveEvent(QEvent*);
\r
199 #ifndef QT_NO_WHEELEVENT
\r
200 // overload wheel mouse event
\r
201 virtual void wheelEvent(QWheelEvent*);
\r
203 // overload focus event
\r
204 virtual void focusInEvent(QFocusEvent*);
\r
205 // overload focus event
\r
206 virtual void focusOutEvent(QFocusEvent*);
\r
207 // overload Qt's event() to capture more keys
\r
208 bool event( QEvent* e );
\r
210 // overload context menu event
\r
211 virtual void contextMenuEvent(QContextMenuEvent*);
\r
212 // overload drag enter event
\r
213 virtual void dragEnterEvent(QDragEnterEvent*);
\r
214 // overload drag move event
\r
215 virtual void dragMoveEvent(QDragMoveEvent*);
\r
216 // overload drag leave event
\r
217 virtual void dragLeaveEvent(QDragLeaveEvent*);
\r
218 // overload drop event
\r
219 virtual void dropEvent(QDropEvent*);
\r
221 // the vtk render window
\r
222 vtkRenderWindow* mRenWin;
\r
224 // set up an X11 window based on a visual and colormap
\r
225 // that VTK chooses
\r
226 void x11_setup_window();
\r
228 #if defined(Q_WS_MAC) && QT_VERSION < 0x040000
\r
230 virtual void setRegionDirty(bool);
\r
231 virtual void macWidgetChangedWindow();
\r
234 #if defined(Q_WS_MAC) && QT_VERSION >= 0x040000
\r
235 EventHandlerUPP DirtyRegionHandlerUPP;
\r
236 EventHandlerRef DirtyRegionHandler;
\r
237 static OSStatus DirtyRegionProcessor(EventHandlerCallRef er, EventRef event, void*);
\r
241 void internalMacFixRect();
\r
245 vtkUnsignedCharArray* mCachedImage;
\r
246 bool cachedImageCleanFlag;
\r
247 bool automaticImageCache;
\r
248 double maxImageCacheRenderRate;
\r
251 //! unimplemented operator=
\r
252 QVTKWidget const& operator=(QVTKWidget const&);
\r
253 //! unimplemented copy
\r
254 QVTKWidget(const QVTKWidget&);
\r
258 class QVTKInteractorInternal;
\r
260 // .NAME QVTKInteractor - An interactor for the QVTKWidget.
\r
261 // .SECTION Description
\r
262 // QVTKInteractor is an interactor for a QVTKWiget.
\r
264 class QVTK_EXPORT QVTKInteractor : public QObject, public vtkRenderWindowInteractor
\r
268 static QVTKInteractor* New();
\r
269 vtkTypeMacro(QVTKInteractor,vtkRenderWindowInteractor);
\r
272 // Overloaded terminiate app, which does nothing in Qt.
\r
273 // Use qApp->exit() instead.
\r
274 virtual void TerminateApp();
\r
277 // Overloaded start method does nothing.
\r
278 // Use qApp->exec() instead.
\r
279 virtual void Start();
\r
282 // timer event slot
\r
283 virtual void TimerEvent(int timerId);
\r
291 // create a Qt Timer
\r
292 virtual int InternalCreateTimer(int timerId, int timerType, unsigned long duration);
\r
293 // destroy a Qt Timer
\r
294 virtual int InternalDestroyTimer(int platformTimerId);
\r
298 QVTKInteractorInternal* Internal;
\r
300 // unimplemented copy
\r
301 QVTKInteractor(const QVTKInteractor&);
\r
302 // unimplemented operator=
\r
303 void operator=(const QVTKInteractor&);
\r