Developer Documentation

QtGLGraphicsScene.cc
1 /*===========================================================================*\
2 * *
3 * OpenFlipper *
4 * Copyright (c) 2001-2015, RWTH-Aachen University *
5 * Department of Computer Graphics and Multimedia *
6 * All rights reserved. *
7 * www.openflipper.org *
8 * *
9 *---------------------------------------------------------------------------*
10 * This file is part of OpenFlipper. *
11 *---------------------------------------------------------------------------*
12 * *
13 * Redistribution and use in source and binary forms, with or without *
14 * modification, are permitted provided that the following conditions *
15 * are met: *
16 * *
17 * 1. Redistributions of source code must retain the above copyright notice, *
18 * this list of conditions and the following disclaimer. *
19 * *
20 * 2. Redistributions in binary form must reproduce the above copyright *
21 * notice, this list of conditions and the following disclaimer in the *
22 * documentation and/or other materials provided with the distribution. *
23 * *
24 * 3. Neither the name of the copyright holder nor the names of its *
25 * contributors may be used to endorse or promote products derived from *
26 * this software without specific prior written permission. *
27 * *
28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS *
29 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
30 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A *
31 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER *
32 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, *
33 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, *
34 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR *
35 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF *
36 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING *
37 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS *
38 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
39 * *
40 \*===========================================================================*/
41
42
43
44
45
46
47 //=============================================================================
48 //
49 // CLASS QtGLGraphicsScene - IMPLEMENTATION
50 //
51 //=============================================================================
52
53 //== INCLUDES =================================================================
54
55 #include <ACG/GL/acg_glew.hh>
56 #include <OpenFlipper/BasePlugin/PluginFunctions.hh>
57 #include <OpenFlipper/common/GlobalOptions.hh>
58 #include "QtGLGraphicsScene.hh"
59 #include "QtMultiViewLayout.hh"
60 #include <QApplication>
61 #include <QPainter>
62 #include <QPaintEngine>
63 #include <QGraphicsSceneMouseEvent>
64
65 //== NAMESPACES ===============================================================
66
67 //== IMPLEMENTATION ===========================================================
68
69 QtGLGraphicsScene::QtGLGraphicsScene(std::vector< glViewer *> *_views,
70 QtMultiViewLayout *_layout) :
71 QGraphicsScene (),
72 views_(_views),
73 layout_(_layout),
74 cursorPainter_(0)
75 {
76 }
77
78 //-----------------------------------------------------------------------------
79
80 void QtGLGraphicsScene::drawBackground(QPainter *_painter, const QRectF &_rect)
81 {
82 // Check for switch in qt4.6 to OpenGL2
83 if (_painter->paintEngine()->type() != QPaintEngine::OpenGL && _painter->paintEngine()->type() != QPaintEngine::OpenGL2 ) {
84 std::cerr << "QtGLGraphicsScene: drawBackground needs a QGLWidget to be set as viewport on the graphics view\n";
85 return;
86 }
87
88 // Initialize background first
89 _painter->setBackground(QApplication::palette().window());
90
91 #ifdef WIN32
92 if(!OpenFlipper::Options::coreProfile())
93 #endif
94 _painter->eraseRect(_rect);
95
96 // From now on we do OpenGL direct painting on the scene
97 // Tell Qt that we directly use OpenGL
98 _painter->beginNativePainting();
99
100 static bool initialized = false;
101 if (!initialized)
102 {
103 #ifndef __APPLE__
104 // we use GLEW to manage extensions
105 // initialize it first
106 glewInit();
107 #endif
108 for (unsigned int i = 0; i < views_->size (); i++)
109 {
110 views_->at(i)->initializeGL ();
111 }
112 if (cursorPainter_)
113 cursorPainter_->initializeGL ();
114 initialized = true;
115 }
116
117 // Update the cursor position in all viewers
118 if (cursorPainter_ && cursorPainter_->enabled())
119 {
120 // avoid projection matrix stack overflow
121 GLdouble mat[16];
122 glGetDoublev(GL_PROJECTION_MATRIX, mat);
123
124 glMatrixMode(GL_MODELVIEW);
125 glPushMatrix ();
126
127 glPushAttrib (GL_ALL_ATTRIB_BITS);
128 for (unsigned int i = 0; i < views_->size (); i++)
129 {
130 if (views_->at(i)->isVisible())
131 views_->at(i)->updateCursorPosition(cursorPainter_->cursorPosition ());
132 }
133 glPopAttrib ();
134
135 glMatrixMode(GL_PROJECTION);
136 glLoadMatrixd (mat);
137 glMatrixMode(GL_MODELVIEW);
138 glPopMatrix ();
139 }
140
141 // Clear the depth buffer (This is required since QT4.6 Otherwise we get an emtpty scene!
142 glClear(GL_DEPTH_BUFFER_BIT);
143
144 // Paint the viewers
145 for (unsigned int i = 0; i < views_->size (); i++)
146 {
147 if (views_->at(i)->isVisible())
148 views_->at(i)->paintGL();
149 }
150
151 // The rest is painting through QT again.
152 _painter->endNativePainting();
153
154 // Draw red box around active examiner
155 if (layout_->mode() != QtMultiViewLayout::SingleView)
156 {
157 glViewer *v = views_->at(PluginFunctions::activeExaminer());
158
159 QPen pen(Qt::red);
160 pen.setWidth (2);
161 _painter->setPen(pen);
162 _painter->drawLine(v->scenePos().x(), v->scenePos().y(),
163 v->scenePos().x(),
164 v->scenePos().y() + v->size().height() - 1);
165 _painter->drawLine(v->scenePos().x() + v->size().width(), v->scenePos().y(),
166 v->scenePos().x() + v->size().width(),
167 v->scenePos().y() + v->size().height() - 1);
168 _painter->drawLine(v->scenePos().x(), v->scenePos().y() - 1,
169 v->scenePos().x() + v->size().width(),
170 v->scenePos().y() - 1);
171 _painter->drawLine(v->scenePos().x(),
172 v->scenePos().y() + v->size().height() - 1,
173 v->scenePos().x() + v->size().width(),
174 v->scenePos().y() + v->size().height() - 1);
175 }
176 }
177
178 //-----------------------------------------------------------------------------
179
180 glViewer* QtGLGraphicsScene::findView (const QPointF &_p, bool _setActive)
181 {
182 for (unsigned int i = 0; i < views_->size (); i++)
183 {
184 if (views_->at(i)->contains(views_->at(i)->mapFromScene (_p)))
185 {
186 if (_setActive && PluginFunctions::activeExaminer() != i)
187 {
188 PluginFunctions::setActiveExaminer(i);
189 update();
190 }
191 return views_->at(i);
192 }
193 }
194 return NULL;
195 }
196
197 //-----------------------------------------------------------------------------
198
199 void QtGLGraphicsScene::mouseMoveEvent(QGraphicsSceneMouseEvent* _e)
200 {
201 QGraphicsScene::mouseMoveEvent(_e);
202 if (_e->isAccepted())
203 return;
204
205 glViewer *v = findView (_e->scenePos());
206 if (!v)
207 return;
208
209 v->mouseMoveEvent(_e);
210 }
211
212 //-----------------------------------------------------------------------------
213
214 void QtGLGraphicsScene::setCursorPainter(CursorPainter * _cursorPainter)
215 {
216 cursorPainter_ = _cursorPainter;
217 }
218
219 //-----------------------------------------------------------------------------
220
221 bool QtGLGraphicsScene::event(QEvent *_event)
222 {
223 if (cursorPainter_ && _event->type() == QEvent::Enter)
224 {
225 cursorPainter_->setMouseIn (true);
226 }
227 else if (cursorPainter_ && _event->type() == QEvent::Leave)
228 {
229 cursorPainter_->setMouseIn (false);
230 update ();
231 }
232 else if (cursorPainter_ && _event->type() == QEvent::GraphicsSceneMouseMove)
233 {
234 QGraphicsSceneMouseEvent *e = static_cast<QGraphicsSceneMouseEvent*>(_event);
235 cursorPainter_->updateCursorPosition (e->scenePos ());
236 update ();
237 }
238
239 return QGraphicsScene::event (_event);
240 }
241
242
243 //=============================================================================
244 //=============================================================================
245
CursorPainter
Definition: CursorPainter.hh:68
PluginFunctions::activeExaminer
unsigned int activeExaminer()
Get the id of the examiner which got the last mouse events.
Definition: PluginFunctions.cc:194
PluginFunctions.hh
glViewer
Definition: QtBaseViewer.hh:126
glViewer::mouseMoveEvent
virtual void mouseMoveEvent(QGraphicsSceneMouseEvent *_event)
handle mouse move events
Definition: QtBaseViewer.cc:1666
PluginFunctions::setActiveExaminer
void setActiveExaminer(const unsigned int _id)
Set the active id of the examiner which got the last mouse events.
Definition: PluginFunctions.cc:181
GlobalOptions.hh
QtMultiViewLayout
Definition: QtMultiViewLayout.hh:68