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 // CLASS QtGLGraphicsScene - IMPLEMENTATION
49 //
50 //=============================================================================
51 
52 //== INCLUDES =================================================================
53 
54 #include "QtGLGraphicsScene.hh"
55 #include <QGraphicsSceneMouseEvent>
56 #include <QApplication>
57 #include <QPainter>
58 #include <QPaintEngine>
59 
60 //== NAMESPACES ===============================================================
61 
62 namespace ACG {
63 namespace QtWidgets {
64 
65 
66 //== IMPLEMENTATION ===========================================================
67 
68 QtGLGraphicsScene::QtGLGraphicsScene(QtBaseViewer* _w) :
69  QGraphicsScene (),
70  w_(_w)
71 {
72 }
73 
74 
75 void QtGLGraphicsScene::drawBackground(QPainter *_painter, const QRectF &_rect)
76 {
77  // Check for switch in qt4.6 to OpenGL2
78  if (_painter->paintEngine()->type() != QPaintEngine::OpenGL && _painter->paintEngine()->type() != QPaintEngine::OpenGL2 ) {
79  std::cerr << "QtGLGraphicsScene: drawBackground needs a QGLWidget to be set as viewport on the graphics view\n";
80  return;
81  }
82 
83  // Initialize background first
84  _painter->setBackground(QApplication::palette().window());
85  _painter->eraseRect(_rect);
86 
87  // From now on we do OpenGL direct painting on the scene
88  // Tell Qt that we directly use OpenGL
89  _painter->beginNativePainting();
90 
91  // Clear the depth buffer (This is required since QT4.6 Otherwise we get an emtpty scene!
92  glClear(GL_DEPTH_BUFFER_BIT);
93 
94  w_->paintGL();
95 
96  // The rest is painting through QT again.
97  _painter->endNativePainting();
98 }
99 
100 
101 void QtGLGraphicsScene::mousePressEvent(QGraphicsSceneMouseEvent* _e)
102 {
103  QGraphicsScene::mousePressEvent(_e);
104  if (_e->isAccepted())
105  return;
106 
107  QPoint p (_e->scenePos().x(), _e->scenePos().y());
108  QMouseEvent me(QEvent::MouseButtonPress ,p, _e->screenPos(), _e->button(),
109  _e->buttons(), _e->modifiers());
110  w_->glMousePressEvent(&me);
111  _e->accept();
112 }
113 
114 void QtGLGraphicsScene::mouseDoubleClickEvent(QGraphicsSceneMouseEvent* _e)
115 {
116  QGraphicsScene::mouseDoubleClickEvent(_e);
117  if (_e->isAccepted())
118  return;
119 
120  QPoint p (_e->scenePos().x(), _e->scenePos().y());
121  QMouseEvent me(QEvent::MouseButtonDblClick ,p, _e->screenPos(), _e->button(),
122  _e->buttons(), _e->modifiers());
123  w_->glMouseDoubleClickEvent(&me);
124  _e->accept();
125 }
126 
127 void QtGLGraphicsScene::mouseReleaseEvent(QGraphicsSceneMouseEvent* _e)
128 {
129  QGraphicsScene::mouseReleaseEvent(_e);
130  if (_e->isAccepted())
131  return;
132 
133  QPoint p (_e->scenePos().x(), _e->scenePos().y());
134  QMouseEvent me(QEvent::MouseButtonRelease ,p, _e->screenPos(), _e->button(),
135  _e->buttons(), _e->modifiers());
136  w_->glMouseReleaseEvent(&me);
137  _e->accept();
138 }
139 
140 void QtGLGraphicsScene::mouseMoveEvent(QGraphicsSceneMouseEvent* _e)
141 {
142  QGraphicsScene::mouseMoveEvent(_e);
143  if (_e->isAccepted())
144  return;
145 
146  QPoint p (_e->scenePos().x(), _e->scenePos().y());
147  QMouseEvent me(QEvent::MouseMove ,p, _e->screenPos(), _e->button(),
148  _e->buttons(), _e->modifiers());
149  w_->glMouseMoveEvent(&me);
150  _e->accept();
151 }
152 
153 void QtGLGraphicsScene::wheelEvent(QGraphicsSceneWheelEvent* _e)
154 {
155  QGraphicsScene::wheelEvent(_e);
156  if (_e->isAccepted())
157  return;
158  QPoint p (_e->scenePos().x(), _e->scenePos().y());
159  QWheelEvent we(p, _e->screenPos(), _e->delta(), _e->buttons(),
160  _e->modifiers(), _e->orientation());
161  w_->glMouseWheelEvent(&we);
162  _e->accept();
163 }
164 
165 void QtGLGraphicsScene::keyPressEvent(QKeyEvent* _e)
166 {
167  QGraphicsScene::keyPressEvent(_e);
168  if (_e->isAccepted())
169  return;
170  w_->glKeyPressEvent (_e);
171 }
172 
173 void QtGLGraphicsScene::keyReleaseEvent(QKeyEvent* _e)
174 {
175  QGraphicsScene::keyReleaseEvent(_e);
176  if (_e->isAccepted())
177  return;
178  w_->glKeyReleaseEvent (_e);
179 }
180 
181 void QtGLGraphicsScene::contextMenuEvent(QGraphicsSceneContextMenuEvent* _e)
182 {
183  QGraphicsScene::contextMenuEvent(_e);
184  if (_e->isAccepted())
185  return;
186 
187  QPoint p (_e->scenePos().x(), _e->scenePos().y());
188  QContextMenuEvent ce(static_cast<QContextMenuEvent::Reason> (_e->reason()),
189  p, _e->screenPos(), _e->modifiers());
190  w_->glContextMenuEvent(&ce);
191  _e->accept();
192 }
193 
194 void QtGLGraphicsScene::dragEnterEvent(QGraphicsSceneDragDropEvent* _e)
195 {
196  QGraphicsScene::dragEnterEvent(_e);
197  if (_e->isAccepted())
198  return;
199 
200  QPoint p (_e->scenePos().x(), _e->scenePos().y());
201  QDragEnterEvent de(p, _e->possibleActions(), _e->mimeData(), _e->buttons(),
202  _e->modifiers ());
203  w_->glDragEnterEvent(&de);
204  _e->accept();
205 }
206 
207 void QtGLGraphicsScene::dropEvent(QGraphicsSceneDragDropEvent* _e)
208 {
209  QGraphicsScene::dropEvent(_e);
210  if (_e->isAccepted())
211  return;
212  QPoint p (_e->scenePos().x(), _e->scenePos().y());
213  QDropEvent de(p, _e->possibleActions(), _e->mimeData(), _e->buttons(),
214  _e->modifiers ());
215  w_->glDropEvent(&de);
216  _e->accept();
217 }
218 
219 //=============================================================================
220 } // namespace QtWidgets
221 } // namespace ACG
222 //=============================================================================
Namespace providing different geometric functions concerning angles.