]> Creatis software - clitk.git/blob - vv/QVTKWidget.h
Initial revision
[clitk.git] / vv / QVTKWidget.h
1 /*=========================================================================\r
2 \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
9 \r
10 =========================================================================*/\r
11 \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
16 \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
24  !!! license.\r
25 =========================================================================*/\r
26 \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
30 \r
31 #ifndef Q_VTK_WIDGET_H\r
32 #define Q_VTK_WIDGET_H\r
33 \r
34 #include <qwidget.h>\r
35 class QPaintEngine;\r
36 \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
43 \r
44 #if defined(Q_WS_MAC) && QT_VERSION >= 0x040000\r
45 #include <Carbon/Carbon.h>    // Event handling for dirty region\r
46 #endif\r
47 \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
51 #else\r
52 #define QVTK_EXPORT __declspec( dllimport )\r
53 #endif\r
54 #define QVTK_EXPORT\r
55 #else\r
56 #define QVTK_EXPORT\r
57 #endif\r
58 \r
59 //! QVTKWidget displays a VTK window in a Qt window.\r
60 class QVTK_EXPORT QVTKWidget : public QWidget\r
61 {\r
62     Q_OBJECT\r
63 \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
71 \r
72 public:\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
76 #else\r
77     //! constructor for Qt 4\r
78     QVTKWidget(QWidget* parent = NULL, Qt::WFlags f = 0);\r
79 #endif\r
80 \r
81     QString text() const {\r
82         return "";\r
83     }\r
84 \r
85     //! destructor\r
86     virtual ~QVTKWidget();\r
87 \r
88     // Description:\r
89     // Set the vtk render window, if you wish to use your own vtkRenderWindow\r
90     virtual void SetRenderWindow(vtkRenderWindow*);\r
91 \r
92     // Description:\r
93     // Get the vtk render window.\r
94     virtual vtkRenderWindow* GetRenderWindow();\r
95 \r
96     // Description:\r
97     // Get the Qt/vtk interactor that was either created by default or set by the user\r
98     virtual QVTKInteractor* GetInteractor();\r
99 \r
100     // Description:\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
104     {\r
105         ContextMenuEvent = vtkCommand::UserEvent + 100,\r
106         DragEnterEvent,\r
107         DragMoveEvent,\r
108         DragLeaveEvent,\r
109         DropEvent\r
110     };\r
111 \r
112     // Description:\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
117 \r
118     // Description:\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
128 \r
129     // Description:\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
133 \r
134 #if QT_VERSION < 0x040000\r
135     // Description:\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
138 #endif\r
139 \r
140     // Description:\r
141     // Handle showing of the Widget\r
142     virtual void showEvent(QShowEvent*);\r
143 \r
144     virtual QPaintEngine* paintEngine() const;\r
145 \r
146 signals:\r
147     // Description:\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
151 \r
152     // Description:\r
153     // This signal will be emitted whenever the cached image goes from clean\r
154     // to dirty.\r
155     void cachedImageDirty();\r
156 \r
157     // Description:\r
158     // This signal will be emitted whenever the cached image is refreshed.\r
159     void cachedImageClean();\r
160 \r
161 public slots:\r
162     // Description:\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
169 \r
170     // Description:\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
174 \r
175     void setText(const QString &) {};\r
176 \r
177 protected:\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
184 \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
202 #endif\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
209 \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
220 \r
221     // the vtk render window\r
222     vtkRenderWindow* mRenWin;\r
223 \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
227 \r
228 #if defined(Q_WS_MAC) && QT_VERSION < 0x040000\r
229     void macFixRect();\r
230     virtual void setRegionDirty(bool);\r
231     virtual void macWidgetChangedWindow();\r
232 #endif\r
233 \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
238 #endif\r
239 \r
240 private slots:\r
241     void internalMacFixRect();\r
242 \r
243 protected:\r
244 \r
245     vtkUnsignedCharArray* mCachedImage;\r
246     bool cachedImageCleanFlag;\r
247     bool automaticImageCache;\r
248     double maxImageCacheRenderRate;\r
249 \r
250 private:\r
251     //! unimplemented operator=\r
252     QVTKWidget const& operator=(QVTKWidget const&);\r
253     //! unimplemented copy\r
254     QVTKWidget(const QVTKWidget&);\r
255 \r
256 };\r
257 \r
258 class QVTKInteractorInternal;\r
259 \r
260 // .NAME QVTKInteractor - An interactor for the QVTKWidget.\r
261 // .SECTION Description\r
262 // QVTKInteractor is an interactor for a QVTKWiget.\r
263 \r
264 class QVTK_EXPORT QVTKInteractor : public QObject, public vtkRenderWindowInteractor\r
265 {\r
266     Q_OBJECT\r
267 public:\r
268     static QVTKInteractor* New();\r
269     vtkTypeMacro(QVTKInteractor,vtkRenderWindowInteractor);\r
270 \r
271     // Description:\r
272     // Overloaded terminiate app, which does nothing in Qt.\r
273     // Use qApp->exit() instead.\r
274     virtual void TerminateApp();\r
275 \r
276     // Description:\r
277     // Overloaded start method does nothing.\r
278     // Use qApp->exec() instead.\r
279     virtual void Start();\r
280 \r
281 public slots:\r
282 // timer event slot\r
283     virtual void TimerEvent(int timerId);\r
284 \r
285 protected:\r
286     // constructor\r
287     QVTKInteractor();\r
288     // destructor\r
289     ~QVTKInteractor();\r
290 \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
295 \r
296 private:\r
297 \r
298     QVTKInteractorInternal* Internal;\r
299 \r
300     // unimplemented copy\r
301     QVTKInteractor(const QVTKInteractor&);\r
302     // unimplemented operator=\r
303     void operator=(const QVTKInteractor&);\r
304 \r
305 };\r
306 \r
307 \r
308 #endif\r
309 \r
310 \r