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