1 /**************************************************************************\
2  * Copyright (c) Kongsberg Oil & Gas Technologies AS
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are
7  * met:
8  *
9  * Redistributions of source code must retain the above copyright notice,
10  * this list of conditions and the following disclaimer.
11  *
12  * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  *
16  * Neither the name of the copyright holder nor the names of its
17  * contributors may be used to endorse or promote products derived from
18  * this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24  * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 \**************************************************************************/
32 
33 // This class' purpose is to overload the Qt OpenGL widget, so we can
34 // do our own initialization and event handling on resizes and expose
35 // events.
36 
37 #ifdef HAVE_CONFIG_H
38 #include <config.h>
39 #endif // HAVE_CONFIG_H
40 #include <qt-config.h>
41 
42 #include <assert.h>
43 
44 #include <qmetaobject.h>
45 #include <qnamespace.h>
46 #include <qevent.h>
47 
48 #include <Inventor/errors/SoDebugError.h>
49 
50 #include <Inventor/Qt/widgets/SoQtGLArea.h>
51 #include <Inventor/Qt/widgets/moc_SoQtGLArea.icc>
52 #include <soqtdefs.h>
53 
54 // ************************************************************************
55 
56 // Take care of namespace incompatibilities between Qt 3 and Qt 4.
57 
58 #if QT_VERSION < 0x040000 // pre Qt 4
59 #define QTWIDGET_STRONGFOCUS QWidget::StrongFocus
60 #else // Qt 4.0.0+
61 #define QTWIDGET_STRONGFOCUS Qt::StrongFocus
62 #endif // Qt 4.0.0+
63 
64 // *************************************************************************
65 
66 #if SOQT_DEBUG && 0 // switch 0<->1 to toggle debugging info on/off
67 
68 #define SOQT_GLAREA_DEBUG_INFO(_funcname_, _infostr_) \
69   do { \
70     SbString s("SoQtGLArea::"); \
71     s += SO__QUOTE(_funcname_); \
72     SoDebugError::postInfo(s.getString(), _infostr_); \
73   } while (0)
74 
75 #define SOQT_GLAREA_DEBUG_START(_funcname_)  SOQT_GLAREA_DEBUG_INFO(_funcname_, "start")
76 #define SOQT_GLAREA_DEBUG_DONE(_funcname_)  SOQT_GLAREA_DEBUG_INFO(_funcname_, "done")
77 
78 #else // !debug
79 
80 #define SOQT_GLAREA_DEBUG_START(_funcname_)
81 #define SOQT_GLAREA_DEBUG_DONE(_funcname_)
82 
83 #endif // !debug
84 
85 // *************************************************************************
86 
SoQtGLArea(QGLFormat * const format,QWidget * const parent,const QGLWidget * sharewidget,const char * const name)87 SoQtGLArea::SoQtGLArea(QGLFormat * const format,
88                        QWidget * const parent,
89                        const QGLWidget * sharewidget,
90                        const char * const name)
91 #if QT_VERSION >= 0x040000
92    : inherited(*format, parent, sharewidget)
93 #else
94    : inherited(*format, parent, NULL, sharewidget, Qt::WResizeNoErase)
95 #endif
96 {
97 #if QT_VERSION >= 0x040000
98   this->setObjectName(name);
99 #else
100   this->setName(name);
101 #endif
102 #if HAVE_QGLWIDGET_SETAUTOBUFFERSWAP
103   // We'll handle the OpenGL buffer swapping ourselves, to support the
104   // different combinations of rendering options (doublebuffer with
105   // the "DrawToFront" flag is for instance hard to do within the
106   // QGLWidget model).
107   this->setAutoBufferSwap(FALSE);
108 #endif // HAVE_QGLWIDGET_SETAUTOBUFFERSWAP
109 
110   this->keycb = NULL;
111   this->setFocusPolicy(QTWIDGET_STRONGFOCUS);
112 }
113 
~SoQtGLArea()114 SoQtGLArea::~SoQtGLArea()
115 {
116 }
117 
118 // Overridden from QGLWidget to emit a signal.
119 void
initializeGL(void)120 SoQtGLArea::initializeGL(void)
121 {
122   SOQT_GLAREA_DEBUG_START(initializeGL);
123   emit this->init_sig();
124   SOQT_GLAREA_DEBUG_DONE(initializeGL);
125 }
126 
127 // Overridden from QtGLWidget to emit a signal.
128 void
resizeGL(int width,int height)129 SoQtGLArea::resizeGL(int width, int height)
130 {
131   SOQT_GLAREA_DEBUG_START(resizeGL);
132   emit this->reshape_sig(width, height);
133   SOQT_GLAREA_DEBUG_DONE(resizeGL);
134 }
135 
136 // Overridden from QtGLWidget. Emit a signal whenever we need to
137 // repaint because of an expose event.
138 void
paintGL(void)139 SoQtGLArea::paintGL(void)
140 {
141   SOQT_GLAREA_DEBUG_START(paintGL);
142   emit this->expose_sig();
143   SOQT_GLAREA_DEBUG_DONE(paintGL);
144 }
145 
146 // Overridden from QWidget to avoid update() being called when we
147 // enable another focuspolicy than QWidget::NoFocus.
148 void
focusInEvent(QFocusEvent * e)149 SoQtGLArea::focusInEvent(QFocusEvent * e)
150 {
151   // Here's what the QWidget implementation of this method does:
152 
153 //     if ( focusPolicy() != NoFocus || !isTopLevel() ) {
154 //        update();
155 //        if ( testWState(WState_AutoMask) )
156 //            updateMask();
157 //        setMicroFocusHint(width()/2, 0, 1, height(), FALSE);
158 //     }
159 
160   // QWidget::update() calls repaint(), which causes paintGL() to be
161   // invoked on all focus-in and focus-out events.
162 }
163 
164 // See doc on focusInEvent() above.
165 void
focusOutEvent(QFocusEvent * e)166 SoQtGLArea::focusOutEvent(QFocusEvent * e)
167 {
168 }
169 
170 // *************************************************************************
171 
172 bool
event(QEvent * e)173 SoQtGLArea::event(QEvent * e)
174 {
175   if ((e->type() == QEvent::KeyPress) || (e->type() == QEvent::KeyRelease)) {
176     if (this->keycb) {
177       QKeyEvent * ke = (QKeyEvent *)e;
178       this->keycb(ke, this->keycbuserdata);
179       ke->accept();
180       return TRUE;
181     }
182   }
183 
184   // The following is a workaround for what may be a Qt bug (or at
185   // least very peculiar behavior).
186   //
187   // In a Qt MDI application, with multiple windows, each containing a
188   // SoQtExaminerViewer, use of the mousewheel over a viewer that
189   // doesn't have the focus causes wheel events to end up in both the
190   // viewer under the mouse and the viewer that has the focus.
191   //
192   // Our workaround is thus to ignore a wheel event when the widget
193   // doesn't have the focus.
194   //
195   // Problem found with Win2000 and Qt 3.3.2.
196   if (e->type() == QEvent::Wheel && ! this->hasFocus()) { return FALSE; }
197 
198   return QGLWidget::event(e);
199 }
200 
201 // *************************************************************************
202