Developer Documentation
simpleGLGraphicsScene.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 SimpleGLGraphicsScene - IMPLEMENTATION
50//
51//=============================================================================
52
53//== INCLUDES =================================================================
54
55#include <ACG/GL/acg_glew.hh>
58#include <OpenFlipper/widgets/glWidget/QtBaseViewer.hh>
59#include <QApplication>
60#include <QPainter>
61#include <QPaintEngine>
62#include <QGraphicsSceneMouseEvent>
63
64#include "simpleGLGraphicsScene.hh"
65
66//== NAMESPACES ===============================================================
67
68//== IMPLEMENTATION ===========================================================
69
70SimpleGLGraphicsScene::SimpleGLGraphicsScene () :
71 QGraphicsScene (),
72 view_(),
73 initialized_(false)
74{
75 cursorPainter_ = new CursorPainter (this);
76 cursorPainter_->setEnabled( OpenFlipperSettings().value("Core/Gui/glViewer/nativeMouse",false).toBool() );
77}
78
79
80void SimpleGLGraphicsScene::drawBackground(QPainter *_painter, const QRectF &_rect)
81{
82
83 // Check for switch in qt4.6 to OpenGL2
84 if (_painter->paintEngine()->type() != QPaintEngine::OpenGL && _painter->paintEngine()->type() != QPaintEngine::OpenGL2 ) {
85 std::cerr << "QtGLGraphicsScene: drawBackground needs a QGLWidget to be set as viewport on the graphics view\n";
86 return;
87 }
88
89 // Initialize background first
90 _painter->setBackground(QApplication::palette().window());
91 _painter->eraseRect(_rect);
92
93 if (!view_)
94 return;
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 if (!initialized_)
101 {
102 // we use GLEW to manage extensions
103 // initialize it first
104 #ifndef __APPLE__
105 glewInit();
106 #endif
107 view_->initializeGL();
108 cursorPainter_->initializeGL ();
109 initialized_ = true;
110 }
111
112 if (cursorPainter_->enabled())
113 {
114 // avoid projection matrix stack overflow
115 GLdouble mat[16];
116 glGetDoublev(GL_PROJECTION_MATRIX, mat);
117
118 glMatrixMode(GL_MODELVIEW);
119 glPushMatrix ();
120
121 glPushAttrib (GL_ALL_ATTRIB_BITS);
122 view_->updateCursorPosition(cursorPainter_->cursorPosition ());
123 glPopAttrib ();
124
125 glMatrixMode(GL_PROJECTION);
126 glLoadMatrixd (mat);
127 glMatrixMode(GL_MODELVIEW);
128 glPopMatrix ();
129 glClear(GL_DEPTH_BUFFER_BIT);
130 }
131
132 // Clear the depth buffer (This is required since QT4.6 Otherwise we get an emtpty scene!
133 glClear(GL_DEPTH_BUFFER_BIT);
134
135 view_->paintGL();
136
137 // The rest is painting through QT again.
138 _painter->endNativePainting();
139}
140
141
142void SimpleGLGraphicsScene::mouseMoveEvent(QGraphicsSceneMouseEvent* _e)
143{
144 QGraphicsScene::mouseMoveEvent(_e);
145 if (_e->isAccepted())
146 return;
147
148 if (view_)
149 view_->mouseMoveEvent(_e);
150}
151
152void SimpleGLGraphicsScene::setView(glViewer * _view)
153{
154 view_ = _view;
155 cursorPainter_->registerViewer (view_);
156}
157
158//-----------------------------------------------------------------------------
159
160bool SimpleGLGraphicsScene::event(QEvent *_event)
161{
162 if (_event->type() == QEvent::Enter)
163 {
164 cursorPainter_->setMouseIn (true);
165 }
166 else if (_event->type() == QEvent::Leave)
167 {
168 cursorPainter_->setMouseIn (false);
169 update ();
170 }
171 else if (cursorPainter_ && _event->type() == QEvent::GraphicsSceneMouseMove)
172 {
173 QGraphicsSceneMouseEvent *e = static_cast<QGraphicsSceneMouseEvent*>(_event);
174 cursorPainter_->updateCursorPosition (e->scenePos ());
175 update ();
176 }
177 return QGraphicsScene::event (_event);
178}
179
180//=============================================================================
181//=============================================================================
182
183
DLLEXPORT OpenFlipperQSettings & OpenFlipperSettings()
QSettings object containing all program settings of OpenFlipper.
void initializeGL()
Needs to be called after the gl context has been set up to initialize internal values.
void updateCursorPosition(QPointF _scenePos)
Sets the current cursor position.
bool enabled()
Returns true if cursor painting is enabled and compatible cursor is set.
void setMouseIn(bool _in)
Inform the cursor painter about mouse enter / leave.
QPointF cursorPosition()
Return the current cursor position.
void registerViewer(glViewer *_viewer)
Add a glViewer that will use this CursorPainter.
virtual void mouseMoveEvent(QGraphicsSceneMouseEvent *_event)
handle mouse move events
virtual void paintGL(double _aspect=0.0)
draw the scene. Triggered by updateGL().
virtual void initializeGL()
Return a resonable size hint.
void updateCursorPosition(QPointF _scenePos)
will be called from CursorPainter to inform the viewer that the cursor position changed