QtGLGraphicsScene.cc 9.13 KB
Newer Older
1
/*===========================================================================*\
Jan Möbius's avatar
Jan Möbius committed
2 3
*                                                                            *
*                              OpenFlipper                                   *
Jan Möbius's avatar
Jan Möbius committed
4
*      Copyright (C) 2001-2011 by Computer Graphics Group, RWTH Aachen       *
Jan Möbius's avatar
Jan Möbius committed
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
*                           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/>.                                       *
*                                                                            *
33 34 35
\*===========================================================================*/

/*===========================================================================*\
Jan Möbius's avatar
Jan Möbius committed
36 37 38 39 40
*                                                                            *
*   $Revision$                                                       *
*   $LastChangedBy$                                                *
*   $Date$                     *
*                                                                            *
41
\*===========================================================================*/
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
  static bool initialized = false;
  if (!initialized)
  {
    // we use GLEW to manage extensions
    // initialize it first
109
    #ifndef __APPLE__
Jan Möbius's avatar
Jan Möbius committed
110
    glewInit();
111
    #endif
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
112 113 114 115
    for (unsigned int i = 0; i < views_->size (); i++)
    {
      views_->at(i)->initializeGL ();
    }
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
116 117
    if (cursorPainter_)
      cursorPainter_->initializeGL ();
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
118
    initialized = true;
Jan Möbius's avatar
Jan Möbius committed
119
  }
Jan Möbius's avatar
 
Jan Möbius committed
120

121
  // Update the cursor position in all viewers
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
  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 ();
  }

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

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

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

160
  // Draw red box around active examiner
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
  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
182 183
}

Jan Möbius's avatar
Dennis:  
Jan Möbius committed
184
//-----------------------------------------------------------------------------
185

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

Jan Möbius's avatar
Dennis:  
Jan Möbius committed
203 204
//-----------------------------------------------------------------------------

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

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

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

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

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
238 239 240 241 242 243 244
  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
245
  return QGraphicsScene::event (_event);
Jan Möbius's avatar
 
Jan Möbius committed
246 247 248 249 250
}


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