QtGLGraphicsScene.cc 6.51 KB
Newer Older
Jan Möbius's avatar
 
Jan Möbius committed
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
42
//=============================================================================
//
//                               OpenFlipper
//        Copyright (C) 2008 by Computer Graphics Group, RWTH Aachen
//                           www.openflipper.org
//
//-----------------------------------------------------------------------------
//
//                                License
//
//  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.
//
//  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 Lesser General Public License
//  along with OpenFlipper.  If not, see <http://www.gnu.org/licenses/>.
//
//-----------------------------------------------------------------------------
//
//   $Revision: $
//   $Author: $
//   $Date: $
//
//=============================================================================




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

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

43
#include <OpenFlipper/BasePlugin/PluginFunctions.hh>
Jan Möbius's avatar
 
Jan Möbius committed
44
#include "QtGLGraphicsScene.hh"
45
#include "QtMultiViewLayout.hh"
Jan Möbius's avatar
   
Jan Möbius committed
46
#include <OpenFlipper/widgets/glWidget/QtBaseViewer.hh>
47
#include <QApplication>
Jan Möbius's avatar
 
Jan Möbius committed
48
49
50
51
52
53
54
55
56
#include <QPainter>
#include <QPaintEngine>
#include <QGraphicsSceneMouseEvent>
#include <QGraphicsSceneDragDropEvent>

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

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

57
58
QtGLGraphicsScene::QtGLGraphicsScene(std::vector< glViewer *> *_views,
				     QtMultiViewLayout *_layout) :
Jan Möbius's avatar
 
Jan Möbius committed
59
  QGraphicsScene (),
60
61
  views_(_views),
  layout_(_layout)
Jan Möbius's avatar
 
Jan Möbius committed
62
63
64
{
}

Jan Möbius's avatar
Jan Möbius committed
65

66
void QtGLGraphicsScene::drawBackground(QPainter *_painter, const QRectF &_rect)
Jan Möbius's avatar
 
Jan Möbius committed
67
{
Jan Möbius's avatar
Jan Möbius committed
68
69
70
71
  if (_painter->paintEngine()->type() != QPaintEngine::OpenGL) {
    std::cerr << "QtGLGraphicsScene: drawBackground needs a QGLWidget to be set as viewport on the graphics view\n";
    return;
  }
Jan Möbius's avatar
 
Jan Möbius committed
72

Jan Möbius's avatar
Jan Möbius committed
73
74
75
76
77
78
79
  static bool initialized = false;
  if (!initialized)
  {
    // we use GLEW to manage extensions
    // initialize it first
    glewInit();
  }
Jan Möbius's avatar
 
Jan Möbius committed
80

81
82
83
  _painter->setBackground(QApplication::palette().window());
  _painter->eraseRect(_rect);

Jan Möbius's avatar
Jan Möbius committed
84
85
86
87
88
  for (unsigned int i = 0; i < views_->size (); i++)
  {
    if (views_->at(i)->isVisible())
      views_->at(i)->paintGL();
  }
89

90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111

  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
112
113
}

114
glViewer* QtGLGraphicsScene::findView (const QPointF &_p, bool _setActive)
Jan Möbius's avatar
   
Jan Möbius committed
115
116
117
{
  for (unsigned int i = 0; i < views_->size (); i++)
  {
118
119
120
121
122
123
124
125
126
    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
127
128
129
  }
  return NULL;
}
Jan Möbius's avatar
 
Jan Möbius committed
130
131
132
133
134
135

void QtGLGraphicsScene::mousePressEvent(QGraphicsSceneMouseEvent* _e)
{
  QGraphicsScene::mousePressEvent(_e);
  if (_e->isAccepted())
    return;
136
  glViewer *v = findView (_e->scenePos(), true);
Jan Möbius's avatar
   
Jan Möbius committed
137
138
  if (!v)
    return;
Jan Möbius's avatar
 
Jan Möbius committed
139
140
141
142

  QPoint p (_e->scenePos().x(), _e->scenePos().y());
  QMouseEvent me(QEvent::MouseButtonPress ,p, _e->screenPos(), _e->button(),
	         _e->buttons(), _e->modifiers());
Jan Möbius's avatar
   
Jan Möbius committed
143
  v->glMousePressEvent(&me);
Jan Möbius's avatar
 
Jan Möbius committed
144
145
146
147
148
149
150
151
  _e->accept();
}

void QtGLGraphicsScene::mouseDoubleClickEvent(QGraphicsSceneMouseEvent* _e)
{
  QGraphicsScene::mouseDoubleClickEvent(_e);
  if (_e->isAccepted())
    return;
152
  glViewer *v = findView (_e->scenePos(), true);
Jan Möbius's avatar
   
Jan Möbius committed
153
154
  if (!v)
    return;
Jan Möbius's avatar
 
Jan Möbius committed
155
156
157
158

  QPoint p (_e->scenePos().x(), _e->scenePos().y());
  QMouseEvent me(QEvent::MouseButtonDblClick ,p, _e->screenPos(), _e->button(),
	         _e->buttons(), _e->modifiers());
Jan Möbius's avatar
   
Jan Möbius committed
159
  v->glMouseDoubleClickEvent(&me);
Jan Möbius's avatar
 
Jan Möbius committed
160
161
162
163
164
165
166
167
  _e->accept();
}

void QtGLGraphicsScene::mouseReleaseEvent(QGraphicsSceneMouseEvent* _e)
{
  QGraphicsScene::mouseReleaseEvent(_e);
  if (_e->isAccepted())
    return;
Jan Möbius's avatar
   
Jan Möbius committed
168
169
170
  glViewer *v = findView (_e->scenePos());
  if (!v)
    return;
Jan Möbius's avatar
 
Jan Möbius committed
171

Jan Möbius's avatar
   
Jan Möbius committed
172
  QPointF f (v->mapFromScene (_e->scenePos()));
Jan Möbius's avatar
 
Jan Möbius committed
173
174
175
  QPoint p (_e->scenePos().x(), _e->scenePos().y());
  QMouseEvent me(QEvent::MouseButtonRelease ,p, _e->screenPos(), _e->button(),
	         _e->buttons(), _e->modifiers());
Jan Möbius's avatar
   
Jan Möbius committed
176
  v->glMouseReleaseEvent(&me);
Jan Möbius's avatar
 
Jan Möbius committed
177
178
179
180
181
182
183
184
  _e->accept();
}

void QtGLGraphicsScene::mouseMoveEvent(QGraphicsSceneMouseEvent* _e)
{
  QGraphicsScene::mouseMoveEvent(_e);
  if (_e->isAccepted())
    return;
Jan Möbius's avatar
   
Jan Möbius committed
185
186
187
  glViewer *v = findView (_e->scenePos());
  if (!v)
    return;
Jan Möbius's avatar
 
Jan Möbius committed
188
189
190
191

  QPoint p (_e->scenePos().x(), _e->scenePos().y());
  QMouseEvent me(QEvent::MouseMove ,p, _e->screenPos(), _e->button(),
	         _e->buttons(), _e->modifiers());
Jan Möbius's avatar
   
Jan Möbius committed
192
  v->glMouseMoveEvent(&me);
Jan Möbius's avatar
 
Jan Möbius committed
193
194
195
196
197
198
199
200
201
  _e->accept();
}

void QtGLGraphicsScene::wheelEvent(QGraphicsSceneWheelEvent* _e)
{
  QGraphicsScene::wheelEvent(_e);
  if (_e->isAccepted())
    return;

202
  glViewer *v = findView (_e->scenePos(), true);
Jan Möbius's avatar
   
Jan Möbius committed
203
  if (!v)
Jan Möbius's avatar
 
Jan Möbius committed
204
205
206
    return;

  QPoint p (_e->scenePos().x(), _e->scenePos().y());
Jan Möbius's avatar
   
Jan Möbius committed
207
208
209
  QWheelEvent we(p, _e->screenPos(), _e->delta(), _e->buttons(),
		 _e->modifiers(), _e->orientation());
  v->glMouseWheelEvent(&we);
Jan Möbius's avatar
 
Jan Möbius committed
210
211
212
213
214
215
  _e->accept();
}


//=============================================================================
//=============================================================================