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