QtGLGraphicsScene.cc 9.1 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/*===========================================================================*\
 *                                                                           *
 *                              OpenFlipper                                  *
 *      Copyright (C) 2001-2009 by Computer Graphics Group, RWTH Aachen      *
 *                           www.openflipper.org                             *
 *                                                                           *
 *---------------------------------------------------------------------------*
 *  This file is part of OpenFlipper.                                        *
 *                                                                           *
 *  OpenFlipper is free software: you can redistribute it and/or modify      *
 *  it under the terms of the GNU Lesser General Public License as           *
 *  published by the Free Software Foundation, either version 3 of           *
 *  the License, or (at your option) any later version with the              *
 *  following exceptions:                                                    *
 *                                                                           *
 *  If other files instantiate templates or use macros                       *
 *  or inline functions from this file, or you compile this file and         *
 *  link it with other files to produce an executable, this file does        *
 *  not by itself cause the resulting executable to be covered by the        *
 *  GNU Lesser General Public License. This exception does not however       *
 *  invalidate any other reasons why the executable file might be            *
 *  covered by the GNU Lesser General Public License.                        *
 *                                                                           *
 *  OpenFlipper is distributed in the hope that it will be useful,           *
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of           *
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            *
 *  GNU Lesser General Public License for more details.                      *
 *                                                                           *
 *  You should have received a copy of the GNU LesserGeneral Public          *
 *  License along with OpenFlipper. If not,                                  *
 *  see <http://www.gnu.org/licenses/>.                                      *
 *                                                                           *
\*===========================================================================*/

/*===========================================================================*\
 *                                                                           *
 *   $Revision$                                                         *
 *   $Author$                                                      *
 *   $Date$                   *
 *                                                                           *
\*===========================================================================*/
Jan Möbius's avatar
 
Jan Möbius committed
42
43
44
45
46
47
48
49
50
51
52
53




//=============================================================================
//
//  CLASS QtGLGraphicsScene - IMPLEMENTATION
//
//=============================================================================

//== INCLUDES =================================================================

54
#include <OpenFlipper/BasePlugin/PluginFunctions.hh>
Jan Möbius's avatar
 
Jan Möbius committed
55
#include "QtGLGraphicsScene.hh"
56
#include "QtMultiViewLayout.hh"
Jan Möbius's avatar
   
Jan Möbius committed
57
#include <OpenFlipper/widgets/glWidget/QtBaseViewer.hh>
58
#include <QApplication>
Jan Möbius's avatar
 
Jan Möbius committed
59
60
61
62
63
64
65
66
67
#include <QPainter>
#include <QPaintEngine>
#include <QGraphicsSceneMouseEvent>
#include <QGraphicsSceneDragDropEvent>

//== NAMESPACES ===============================================================

//== IMPLEMENTATION ===========================================================

68
69
QtGLGraphicsScene::QtGLGraphicsScene(std::vector< glViewer *> *_views,
				     QtMultiViewLayout *_layout) :
Jan Möbius's avatar
 
Jan Möbius committed
70
  QGraphicsScene (),
71
  views_(_views),
Jan Möbius's avatar
Dennis:    
Jan Möbius committed
72
73
  layout_(_layout),
  cursorPainter_(0)
Jan Möbius's avatar
 
Jan Möbius committed
74
75
76
{
}

Jan Möbius's avatar
Dennis:    
Jan Möbius committed
77
//-----------------------------------------------------------------------------
Jan Möbius's avatar
Jan Möbius committed
78

79
void QtGLGraphicsScene::drawBackground(QPainter *_painter, const QRectF &_rect)
Jan Möbius's avatar
 
Jan Möbius committed
80
{
Jan Möbius's avatar
Jan Möbius committed
81
82
83
84
85
86
87
88
89
90
91
92
93
  // Check for switch in qt4.6 to OpenGL2
  #if QT_VERSION >= 0x040600
    if (_painter->paintEngine()->type() != QPaintEngine::OpenGL && _painter->paintEngine()->type() != QPaintEngine::OpenGL2 ) {
      std::cerr << "QtGLGraphicsScene: drawBackground needs a QGLWidget to be set as viewport on the graphics view\n";
      return;
    }
  #else
    if (_painter->paintEngine()->type() != QPaintEngine::OpenGL ) {
      std::cerr << "QtGLGraphicsScene: drawBackground needs a QGLWidget to be set as viewport on the graphics view\n";
      return;
    }
  #endif

94
95
96
97
98
  // Initialize background first
  _painter->setBackground(QApplication::palette().window());
  _painter->eraseRect(_rect);

  // From now on we do OpenGL direct painting on the scene
Jan Möbius's avatar
Jan Möbius committed
99
  #if QT_VERSION >= 0x040600
100
101
    // Tell Qt that we directly use OpenGL
    _painter->beginNativePainting();
Jan Möbius's avatar
Jan Möbius committed
102
  #endif
Jan Möbius's avatar
 
Jan Möbius committed
103

Jan Möbius's avatar
Jan Möbius committed
104
105
106
107
108
109
  static bool initialized = false;
  if (!initialized)
  {
    // we use GLEW to manage extensions
    // initialize it first
    glewInit();
Jan Möbius's avatar
Dennis:    
Jan Möbius committed
110
111
112
113
    for (unsigned int i = 0; i < views_->size (); i++)
    {
      views_->at(i)->initializeGL ();
    }
Jan Möbius's avatar
Dennis:    
Jan Möbius committed
114
115
    if (cursorPainter_)
      cursorPainter_->initializeGL ();
Jan Möbius's avatar
Dennis:    
Jan Möbius committed
116
    initialized = true;
Jan Möbius's avatar
Jan Möbius committed
117
  }
Jan Möbius's avatar
 
Jan Möbius committed
118

119
  // Update the cursor position in all viewers
Jan Möbius's avatar
Dennis:    
Jan Möbius committed
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
  if (cursorPainter_ && cursorPainter_->enabled())
  {
    // avoid projection matrix stack overflow
    GLdouble mat[16];
    glGetDoublev(GL_PROJECTION_MATRIX, mat);

    glMatrixMode(GL_MODELVIEW);
    glPushMatrix ();

    glPushAttrib (GL_ALL_ATTRIB_BITS);
    for (unsigned int i = 0; i < views_->size (); i++)
    {
      if (views_->at(i)->isVisible())
        views_->at(i)->updateCursorPosition(cursorPainter_->cursorPosition ());
    }
    glPopAttrib ();

    glMatrixMode(GL_PROJECTION);
    glLoadMatrixd (mat);
    glMatrixMode(GL_MODELVIEW);
    glPopMatrix ();
  }

143
144
  // Clear the depth buffer (This is required since QT4.6 Otherwise we get an emtpty scene!
  glClear(GL_DEPTH_BUFFER_BIT);
145

146
  // Paint the viewers
Jan Möbius's avatar
Jan Möbius committed
147
148
149
150
151
  for (unsigned int i = 0; i < views_->size (); i++)
  {
    if (views_->at(i)->isVisible())
      views_->at(i)->paintGL();
  }
152

Jan Möbius's avatar
Jan Möbius committed
153
  #if QT_VERSION >= 0x040600
154
155
    // The rest is painting through QT again. 
    _painter->endNativePainting();
Jan Möbius's avatar
Jan Möbius committed
156
  #endif
157

158
  // Draw red box around active examiner
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
  if (layout_->mode() != QtMultiViewLayout::SingleView)
  {
    glViewer *v = views_->at(PluginFunctions::activeExaminer());

    QPen pen(Qt::red);
    pen.setWidth (2);
    _painter->setPen(pen);
    _painter->drawLine(v->scenePos().x(), v->scenePos().y(),
                       v->scenePos().x(),
                       v->scenePos().y() + v->size().height() - 1);
    _painter->drawLine(v->scenePos().x() + v->size().width(), v->scenePos().y(),
                       v->scenePos().x() + v->size().width(),
                       v->scenePos().y() + v->size().height() - 1);
    _painter->drawLine(v->scenePos().x(), v->scenePos().y() - 1,
                       v->scenePos().x() + v->size().width(),
                       v->scenePos().y() - 1);
    _painter->drawLine(v->scenePos().x(),
                       v->scenePos().y() + v->size().height() - 1,
                       v->scenePos().x() + v->size().width(),
                       v->scenePos().y() + v->size().height() - 1);
  }
Jan Möbius's avatar
 
Jan Möbius committed
180
181
}

Jan Möbius's avatar
Dennis:    
Jan Möbius committed
182
//-----------------------------------------------------------------------------
183

184
glViewer* QtGLGraphicsScene::findView (const QPointF &_p, bool _setActive)
Jan Möbius's avatar
   
Jan Möbius committed
185
186
187
{
  for (unsigned int i = 0; i < views_->size (); i++)
  {
188
189
190
191
192
193
194
195
196
    if (views_->at(i)->contains(views_->at(i)->mapFromScene (_p)))
    {
      if (_setActive && PluginFunctions::activeExaminer() != i)
      {
        PluginFunctions::setActiveExaminer (i);
	update();
      }
      return views_->at(i);
    }
Jan Möbius's avatar
   
Jan Möbius committed
197
198
199
  }
  return NULL;
}
Jan Möbius's avatar
 
Jan Möbius committed
200

Jan Möbius's avatar
Dennis:    
Jan Möbius committed
201
202
//-----------------------------------------------------------------------------

Jan Möbius's avatar
 
Jan Möbius committed
203
204
205
206
207
208
void QtGLGraphicsScene::mouseMoveEvent(QGraphicsSceneMouseEvent* _e)
{
  QGraphicsScene::mouseMoveEvent(_e);
  if (_e->isAccepted())
    return;

209
  glViewer *v = findView (_e->scenePos());
Jan Möbius's avatar
   
Jan Möbius committed
210
  if (!v)
Jan Möbius's avatar
 
Jan Möbius committed
211
212
    return;

213
  v->mouseMoveEvent(_e);
Jan Möbius's avatar
Dennis:    
Jan Möbius committed
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
}

//-----------------------------------------------------------------------------

void QtGLGraphicsScene::setCursorPainter(CursorPainter * _cursorPainter)
{
  cursorPainter_ = _cursorPainter;
}

//-----------------------------------------------------------------------------

bool QtGLGraphicsScene::event(QEvent *_event)
{
  if (cursorPainter_ && _event->type() == QEvent::Enter)
  {
    cursorPainter_->setMouseIn (true);
  }
  else if (cursorPainter_ && _event->type() == QEvent::Leave)
  {
    cursorPainter_->setMouseIn (false);
    update ();
  }
Jan Möbius's avatar
Dennis:    
Jan Möbius committed
236
237
238
239
240
241
242
  else if (cursorPainter_ && _event->type() == QEvent::GraphicsSceneMouseMove)
  {
    QGraphicsSceneMouseEvent *e = static_cast<QGraphicsSceneMouseEvent*>(_event);
    cursorPainter_->updateCursorPosition (e->scenePos ());
    update ();
  }

Jan Möbius's avatar
Dennis:    
Jan Möbius committed
243
  return QGraphicsScene::event (_event);
Jan Möbius's avatar
 
Jan Möbius committed
244
245
246
247
248
}


//=============================================================================
//=============================================================================
Jan Möbius's avatar
Dennis:    
Jan Möbius committed
249