source: src/UIElements/Views/Qt4/Qt3D/GLWorldView.cpp

Candidate_v1.7.1 stable v1.7.1
Last change on this file was c742bb1, checked in by Frederik Heber <frederik.heber@…>, 4 months ago

Removes some Observer::signOn's from GLWorldView.

  • Property mode set to 100644
File size: 23.3 KB
Line 
1/*
2 * Project: MoleCuilder
3 * Description: creates and alters molecular systems
4 * Copyright (C) 2010-2012 University of Bonn. All rights reserved.
5 * Copyright (C) 2013 Frederik Heber. All rights reserved.
6 *
7 *
8 * This file is part of MoleCuilder.
9 *
10 * MoleCuilder is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation, either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * MoleCuilder is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with MoleCuilder. If not, see <http://www.gnu.org/licenses/>.
22 */
23
24/*
25 * GLWorldView.cpp
26 *
27 * Created on: Aug 1, 2010
28 * Author: heber
29 */
30
31// include config.h
32#ifdef HAVE_CONFIG_H
33#include <config.h>
34#endif
35
36#include "GLWorldView.hpp"
37
38#include <Qt/qevent.h>
39#include <Qt/qaction.h>
40#include <QtGui/QMenu>
41#include <QtGui/QToolBar>
42#include <QtGui/QToolButton>
43#include <Qt/qtimer.h>
44#include <Qt/qsettings.h>
45#include <Qt3D/qglbuilder.h>
46#include <Qt3D/qglscenenode.h>
47#include <Qt3D/qglsphere.h>
48#include <Qt3D/qglcylinder.h>
49#include <Qt3D/qglcube.h>
50
51#include "GLWorldScene.hpp"
52
53//#include "CodePatterns/MemDebug.hpp"
54
55#include "Atom/AtomObserver.hpp"
56#include "Atom/atom_observable.hpp"
57#include "Box.hpp"
58#include "CodePatterns/Log.hpp"
59#include "CodePatterns/Observer/Notification.hpp"
60#include "CodePatterns/Observer/ObserverLog.hpp"
61#include "Descriptors/MoleculeIdDescriptor.hpp"
62#include "molecule.hpp"
63#include "Shapes/ShapeRegistry.hpp"
64#include "World.hpp"
65#include "WorldTime.hpp"
66
67GLWorldView::GLWorldView(
68 QtObservedInstanceBoard * _board,
69 QWidget *parent) :
70 QGLView(parent),
71 Observer("GLWorldView"),
72 worldscene(NULL),
73 changesPresent(false),
74 needsRedraw(false)
75{
76 worldscene = new GLWorldScene(_board, this);
77
78 setOption(QGLView::ObjectPicking, true);
79 setOption(QGLView::CameraNavigation, false);
80 setFocusPolicy(Qt::StrongFocus);
81 setCameraControlMode(Rotate);
82 defaultEyeSeparation = 4.0;
83
84 createDomainBox();
85 createDreiBein();
86 //changeMaterials(false);
87
88 qRegisterMetaType<atomId_t>("atomId_t");
89 qRegisterMetaType<moleculeId_t>("moleculeId_t");
90
91 connect(this, SIGNAL(ShapeAdded(const std::string &)), worldscene, SLOT(addShape(const std::string &)));
92 connect(this, SIGNAL(ShapeRemoved(const std::string &)), worldscene, SLOT(removeShape(const std::string &)));
93 connect(worldscene, SIGNAL(hoverChanged(const atomId_t)), this, SLOT(sceneHoverSignalled(const atomId_t)));
94 connect(worldscene, SIGNAL(hoverChanged(const moleculeId_t, int)), this, SLOT(sceneHoverSignalled(const moleculeId_t, int)));
95 connect(worldscene, SIGNAL(hoverEnd()), this, SLOT(triggerUpdateGL()));
96 connect(worldscene, SIGNAL(updated()), this, SLOT(triggerUpdateGL()));
97 connect(this, SIGNAL(updated(const bool)), this, SLOT(triggerUpdateGL(const bool)));
98 connect(worldscene, SIGNAL(sceneChanged()), this, SLOT(sceneChangeSignalled()));
99 connect(this, SIGNAL(moleculesVisibilityChanged(ObservedValue_Index_t,bool)),
100 worldscene, SLOT(moleculesVisibilityChanged(ObservedValue_Index_t,bool)));
101
102 // sign on to changes in the shape registry
103 ShapeRegistry::getInstance().signOn(this);
104 ShapeRegistry::getInstance().signOn(this, ShapeRegistry::ShapeInserted);
105 ShapeRegistry::getInstance().signOn(this, ShapeRegistry::ShapeRemoved);
106 ShapeRegistry::getInstance().signOn(this, ShapeRegistry::SelectionChanged);
107
108 redrawTimer = new QTimer(this);
109}
110
111GLWorldView::~GLWorldView()
112{
113 QSettings settings;
114 settings.beginGroup("WorldView");
115 settings.setValue("domainBoxEnabled", (meshDomainBox->options() & QGLSceneNode::HideNode) == 0);
116 settings.setValue("dreiBeinEnabled", (meshDreiBein->options() & QGLSceneNode::HideNode) == 0);
117 settings.endGroup();
118
119
120 ShapeRegistry::getInstance().signOff(this);
121 ShapeRegistry::getInstance().signOff(this, ShapeRegistry::ShapeInserted);
122 ShapeRegistry::getInstance().signOff(this, ShapeRegistry::ShapeRemoved);
123 ShapeRegistry::getInstance().signOff(this, ShapeRegistry::SelectionChanged);
124 delete worldscene;
125
126 delete(domainBoxMaterial);
127 for (int i=0;i<3;i++)
128 delete(dreiBeinMaterial[i]);
129}
130
131GLWorldScene::SelectionModeType GLWorldView::getSelectionMode() const
132{
133 return worldscene->getSelectionMode();
134}
135
136/**
137 * Add some widget specific actions to the toolbar:
138 * - camera rotation/translation mode
139 * - camera fit to domain
140 */
141void GLWorldView::addToolBarActions(QToolBar *toolbar)
142{
143 // camera control mode
144 toolbar->addSeparator();
145 QAction *transAction = new QAction(QIcon::fromTheme("forward"), tr("camera translation mode"), this);
146 connect(transAction, SIGNAL(triggered()), this, SLOT(setCameraControlModeTranslation()));
147 toolbar->addAction(transAction);
148 QAction *rotAction = new QAction(QIcon::fromTheme("object-rotate-left"), tr("camera rotation mode"), this);
149 connect(rotAction, SIGNAL(triggered()), this, SLOT(setCameraControlModeRotation()));
150 toolbar->addAction(rotAction);
151 QAction *fitAction = new QAction(QIcon::fromTheme("zoom-best-fit"), tr("camera fit to domain"), this);
152 connect(fitAction, SIGNAL(triggered()), this, SLOT(fitCameraToDomain()));
153 toolbar->addAction(fitAction);
154
155 // stereo mode
156 QToolButton *stereoButton = new QToolButton(toolbar);
157 QMenu *stereoMenu = new QMenu();
158 QAction *stereoDisableAction = new QAction(QIcon::fromTheme("zoom-best-fit"), tr("disable"), this);
159 connect(stereoDisableAction, SIGNAL(triggered()), this, SLOT(setCameraStereoModeDisable()));
160 stereoMenu->addAction(stereoDisableAction);
161 QAction *stereoHardwareAction = new QAction(QIcon::fromTheme("zoom-best-fit"), tr("hardware"), this);
162 connect(stereoHardwareAction, SIGNAL(triggered()), this, SLOT(setCameraStereoModeHardware()));
163 stereoMenu->addAction(stereoHardwareAction);
164 QAction *stereoLeftRightAction = new QAction(QIcon::fromTheme("zoom-best-fit"), tr("left right"), this);
165 connect(stereoLeftRightAction, SIGNAL(triggered()), this, SLOT(setCameraStereoModeLeftRight()));
166 stereoMenu->addAction(stereoLeftRightAction);
167 QAction *stereoRightLeftAction = new QAction(QIcon::fromTheme("zoom-best-fit"), tr("right left"), this);
168 connect(stereoRightLeftAction, SIGNAL(triggered()), this, SLOT(setCameraStereoModeRightLeft()));
169 stereoMenu->addAction(stereoRightLeftAction);
170 QAction *stereoTopBottomAction = new QAction(QIcon::fromTheme("zoom-best-fit"), tr("top bottom"), this);
171 connect(stereoTopBottomAction, SIGNAL(triggered()), this, SLOT(setCameraStereoModeTopBottom()));
172 stereoMenu->addAction(stereoTopBottomAction);
173 QAction *stereoBottomTopAction = new QAction(QIcon::fromTheme("zoom-best-fit"), tr("bottom top"), this);
174 connect(stereoBottomTopAction, SIGNAL(triggered()), this, SLOT(setCameraStereoModeBottomTop()));
175 stereoMenu->addAction(stereoBottomTopAction);
176 QAction *stereoAnaglyphAction = new QAction(QIcon::fromTheme("zoom-best-fit"), tr("anaglyph"), this);
177 connect(stereoAnaglyphAction, SIGNAL(triggered()), this, SLOT(setCameraStereoModeAnaglyph()));
178 stereoMenu->addAction(stereoAnaglyphAction);
179 stereoButton->setMenu(stereoMenu);
180 stereoButton->setIcon(QIcon(QPixmap(":/icon_view_stereo.png")));
181 stereoButton->setPopupMode(QToolButton::InstantPopup);
182 toolbar->addWidget(stereoButton);
183
184 // selection mode
185 toolbar->addSeparator();
186 QAction *selAtomAction = new QAction(QIcon(QPixmap(":/icon_select_atom.png")), tr("select atom by clicking"), this);
187 connect(selAtomAction, SIGNAL(triggered()), worldscene, SLOT(setSelectionModeAtom()));
188 toolbar->addAction(selAtomAction);
189 QAction *selMolAction = new QAction(QIcon(QPixmap(":/icon_select_molecule.png")), tr("select molecule by clicking"), this);
190 connect(selMolAction, SIGNAL(triggered()), worldscene, SLOT(setSelectionModeMolecule()));
191 toolbar->addAction(selMolAction);
192
193 // dreiBein/domain enabler
194 toolbar->addSeparator();
195 QAction *seldreiBein = new QAction(QIcon(QPixmap(":/icon_dreiBein.png")), tr("enable/disable dreiBein"), this);
196 connect(seldreiBein, SIGNAL(triggered()), this, SLOT(changeDreiBein()));
197 toolbar->addAction(seldreiBein);
198 QAction *seldomain = new QAction(QIcon(QPixmap(":/icon_domain.png")), tr("enable/disable domain box"), this);
199 connect(seldomain, SIGNAL(triggered()), this, SLOT(changeDomain()));
200 toolbar->addAction(seldomain);
201}
202
203void GLWorldView::createDomainBox()
204{
205 QSettings settings;
206 settings.beginGroup("WorldView");
207 QColor colorFrame = settings.value("domainBoxColorFrame", QColor(150,160,200,255)).value<QColor>();
208 QColor colorAmbient = settings.value("domainBoxColorAmbient", QColor(50,60,100,255)).value<QColor>();
209 QColor colorDiffuse = settings.value("domainBoxColorDiffuse", QColor(150,160,200,180)).value<QColor>();
210 settings.setValue("domainBoxColorFrame", colorFrame);
211 settings.setValue("domainBoxColorAmbient", colorAmbient);
212 settings.setValue("domainBoxColorDiffuse", colorDiffuse);
213 const bool status = settings.value("domainBoxEnabled").toBool();
214 settings.endGroup();
215
216 domainBoxMaterial = new QGLMaterial;
217 domainBoxMaterial->setAmbientColor(QColor(0,0,0,255));
218 domainBoxMaterial->setDiffuseColor(QColor(0,0,0,255));
219 domainBoxMaterial->setEmittedLight(colorFrame);
220
221
222 QGLMaterial *material = new QGLMaterial;
223 material->setAmbientColor(colorAmbient);
224 material->setDiffuseColor(colorDiffuse);
225
226 QGLBuilder builder;
227 builder << QGL::Faceted;
228 builder << QGLCube(-1.0); // "inverted" => inside faces are used as front.
229 meshDomainBox = builder.finalizedSceneNode();
230 QMatrix4x4 mat;
231 mat.translate(0.5f, 0.5f, 0.5f);
232 meshDomainBox->setLocalTransform(mat);
233 meshDomainBox->setMaterial(material);
234
235 setDomainStatus( status );
236}
237
238void GLWorldView::createDreiBein()
239{
240 QSettings settings;
241 settings.beginGroup("WorldView");
242 QColor colorX = settings.value("dreiBeinColorX", QColor(255,50,50,255)).value<QColor>();
243 QColor colorY = settings.value("dreiBeinColorY", QColor(50,255,50,255)).value<QColor>();
244 QColor colorZ = settings.value("dreiBeinColorZ", QColor(50,50,255,255)).value<QColor>();
245 settings.setValue("dreiBeinColorX", colorX);
246 settings.setValue("dreiBeinColorY", colorY);
247 settings.setValue("dreiBeinColorZ", colorZ);
248 const bool status = settings.value("dreiBeinEnabled").toBool();
249 settings.endGroup();
250
251 // Create 3 color for the 3 axes.
252 dreiBeinMaterial[0] = new QGLMaterial;
253 dreiBeinMaterial[0]->setColor(colorX);
254 dreiBeinMaterial[1] = new QGLMaterial;
255 dreiBeinMaterial[1]->setColor(colorY);
256 dreiBeinMaterial[2] = new QGLMaterial;
257 dreiBeinMaterial[2]->setColor(colorZ);
258
259 // Create the basic meshes (cylinder and cone).
260 QGLBuilder builderCyl;
261 builderCyl << QGLCylinder(.15,.15,1.6,16);
262 QGLSceneNode *cyl = builderCyl.finalizedSceneNode();
263
264 QGLBuilder builderCone;
265 builderCone << QGLCylinder(0,.4,0.4,16);
266 QGLSceneNode *cone = builderCone.finalizedSceneNode();
267 {
268 QMatrix4x4 mat;
269 mat.translate(0.0f, 0.0f, 1.0f);
270 cone->setLocalTransform(mat);
271 }
272
273 // Create a scene node from the 3 axes.
274 meshDreiBein = new QGLSceneNode(this);
275
276 // X-direction
277 QGLSceneNode *node = new QGLSceneNode(meshDreiBein);
278 node->setMaterial(dreiBeinMaterial[0]);
279 node->addNode(cyl);
280 node->setPosition(QVector3D(.8f, 0.f, 0.f));
281 node->addNode(cone);
282 {
283 QMatrix4x4 mat;
284 mat.rotate(90, 0.0f, 1.0f, 0.0f);
285 node->setLocalTransform(mat);
286 }
287
288 // Y-direction
289 node = new QGLSceneNode(meshDreiBein);
290 node->setMaterial(dreiBeinMaterial[1]);
291 node->addNode(cyl);
292 node->addNode(cone);
293 {
294 QMatrix4x4 mat;
295 mat.rotate(-90, 1.0f, 0.0f, 0.0f);
296 node->setLocalTransform(mat);
297 }
298 node->setPosition(QVector3D(0.f, .8f, 0.f));
299
300 // Z-direction
301 node = new QGLSceneNode(meshDreiBein);
302 node->setMaterial(dreiBeinMaterial[2]);
303 node->addNode(cyl);
304 node->addNode(cone);
305 node->setPosition(QVector3D(0.f, 0.f, .8f));
306
307 setdreiBeinStatus( status );
308}
309
310/**
311 * Update operation which can be invoked by the observable (which should be the
312 * change tracker here).
313 */
314void GLWorldView::update(Observable *publisher)
315{
316// emit updated(true);
317}
318
319/**
320 * The observable can tell when it dies.
321 */
322void GLWorldView::subjectKilled(Observable *publisher)
323{
324 // world never dies
325}
326
327/** Listen to specific changes to the world.
328 *
329 * @param publisher ref to observable.
330 * @param notification type of notification
331 */
332void GLWorldView::recieveNotification(Observable *publisher, Notification_ptr notification)
333{
334 if (static_cast<ShapeRegistry*>(publisher) == ShapeRegistry::getPointer()) {
335 switch (notification->getChannelNo()) {
336 case ShapeRegistry::ShapeInserted:
337 {
338 needsRedraw = false;
339 emit ShapeAdded(ShapeRegistry::getInstance().lastChanged()->getName());
340 break;
341 }
342 case ShapeRegistry::ShapeRemoved:
343 {
344 needsRedraw = false;
345 emit ShapeRemoved(ShapeRegistry::getInstance().lastChanged()->getName());
346 break;
347 }
348 case ShapeRegistry::SelectionChanged:
349 {
350 needsRedraw = false;
351 worldscene->updateSelectedShapes();
352 break;
353 }
354 default:
355 ASSERT(0, "GLWorldView::recieveNotification() - we cannot get here for ShapeRegistry.");
356 break;
357 }
358 }
359}
360
361void GLWorldView::changeDreiBein()
362{
363 // invert to new status
364 const bool status = ((meshDreiBein->options() & QGLSceneNode::HideNode) == 0);
365 setdreiBeinStatus(!status);
366 // realize
367 emit updated(true);
368}
369
370void GLWorldView::setdreiBeinStatus(const bool status)
371{
372 if (status)
373 meshDreiBein->setOptions( meshDreiBein->options() & (255-QGLSceneNode::HideNode) );
374 else
375 meshDreiBein->setOptions( meshDreiBein->options() | QGLSceneNode::HideNode );
376}
377
378void GLWorldView::changeDomain()
379{
380 // invert to new status
381 const bool status = ((meshDomainBox->options() & QGLSceneNode::HideNode) == 0);
382 setDomainStatus(!status);
383 // realize
384 emit updated(true);
385}
386
387void GLWorldView::setDomainStatus(const bool status)
388{
389 if (status)
390 meshDomainBox->setOptions( meshDomainBox->options() & (255-QGLSceneNode::HideNode) );
391 else
392 meshDomainBox->setOptions( meshDomainBox->options() | QGLSceneNode::HideNode );
393}
394
395void GLWorldView::triggerUpdateGL(const bool _needsRedraw)
396{
397 if ((!needsRedraw) && (_needsRedraw)) {
398 // accumulate updates over 10ms
399 needsRedraw = true;
400 redrawTimer->singleShot(10, this, SLOT(updateGL()));
401 redrawTimer->start();
402 }
403}
404
405void GLWorldView::updateGL()
406{
407 needsRedraw = false;
408 QGLView::updateGL();
409}
410
411void GLWorldView::initializeGL(QGLPainter *painter)
412{
413 boost::recursive_mutex::scoped_lock lock(changed_mutex);
414 worldscene->initialize(this, painter);
415 changesPresent = false;
416}
417
418void GLWorldView::paintGL(QGLPainter *painter)
419{
420 {
421 boost::recursive_mutex::scoped_lock lock(changed_mutex);
422 if (changesPresent)
423 initializeGL(painter);
424 }
425
426 QVector3D cameraDir = camera()->center() - camera()->eye();
427 cameraDir.normalize();
428 QVector4D cameraPlane(cameraDir, QVector3D::dotProduct(cameraDir, camera()->eye()));
429 worldscene->draw(painter, cameraPlane);
430
431 drawDreiBein(painter);
432
433 // Domain box has to be last because of its transparency.
434 drawDomainBox(painter);
435}
436
437void GLWorldView::sceneChangeSignalled()
438{
439 boost::recursive_mutex::scoped_lock lock(changed_mutex);
440 changesPresent = true;
441}
442
443void GLWorldView::keyPressEvent(QKeyEvent *e)
444{
445 // Find the distance between the eye and focus point.
446 QVector3D d = camera()->eye() - camera()->center();
447// LOG(1, "Distance vector eye and center is "
448// << d.x() << "," << d.y() << "," << d.z());
449 // scale the move unit by the eye <-> domain center distance
450 const double key_move_unit = 0.04*(d.length()/50.);
451
452 if (e->key() == Qt::Key_Insert) {
453 // The Tab key turns the ShowPicking option on and off,
454 // which helps show what the pick buffer looks like.
455 setOption(QGLView::ShowPicking, ((options() & QGLView::ShowPicking) == 0));
456 updateGL();
457 } else if ((e->key() == Qt::Key_Minus) || (e->key() == Qt::Key_Plus)) {
458 // Scale the distance.
459 if (e->key() == Qt::Key_Minus)
460 d *= 1.2;
461 else if (e->key() == Qt::Key_Plus)
462 d /= 1.2;
463 // Set new eye position.
464 camera()->setEye(camera()->center() + d);
465 } else if ((e->key() == Qt::Key_Left) || (e->key() == Qt::Key_Right)) {
466 // Translate the camera.
467 const double d = (e->key() == Qt::Key_Left) ? -key_move_unit : key_move_unit;
468 camera()->translateCenter( d, 0., 0);
469 camera()->translateEye( d, 0., 0);
470 } else if ((e->key() == Qt::Key_Up) || (e->key() == Qt::Key_Down)) {
471 // Translate the camera.
472 const double d = (e->key() == Qt::Key_Up) ? -key_move_unit : key_move_unit;
473 camera()->translateCenter( 0., d, 0);
474 camera()->translateEye( 0., d, 0);
475 } else if ((e->key() == Qt::Key_PageUp) || (e->key() == Qt::Key_PageDown)) {
476 // Translate the camera.
477 const double d = (e->key() == Qt::Key_PageUp) ? -key_move_unit : key_move_unit;
478 camera()->translateCenter( 0., 0., d);
479 camera()->translateEye( 0., 0., d);
480 }
481 QGLView::keyPressEvent(e);
482}
483
484/**
485 * Set the current camera control mode.
486 */
487void GLWorldView::setCameraControlMode(GLWorldView::CameraControlModeType mode)
488{
489 cameraControlMode = mode;
490}
491
492void GLWorldView::setCameraControlModeRotation()
493{
494 setCameraControlMode(Rotate);
495}
496
497void GLWorldView::setCameraControlModeTranslation()
498{
499 setCameraControlMode(Translate);
500}
501
502/**
503 * Returns the current camera control mode.
504 * This needs to be invertable (rotation - translation), if the shift key is pressed.
505 */
506GLWorldView::CameraControlModeType GLWorldView::getCameraControlMode(bool inverted)
507{
508 if (inverted){
509 if (cameraControlMode == Rotate)
510 return Translate;
511 if (cameraControlMode == Translate)
512 return Rotate;
513 return Rotate;
514 }else
515 return cameraControlMode;
516}
517
518/**
519 * Set the camera so it can oversee the whole domain.
520 */
521void GLWorldView::fitCameraToDomain()
522{
523 // Move the camera focus point to the center of the domain box.
524 Vector v = World::getInstance().getDomain().translateIn(Vector(0.5, 0.5, 0.5));
525 camera()->setCenter(QVector3D(v[0], v[1], v[2]));
526
527 // Guess some eye distance.
528 double dist = v.Norm() * 3;
529 camera()->setEye(QVector3D(v[0], v[1], v[2] + dist));
530 camera()->setUpVector(QVector3D(0, 1, 0));
531}
532
533void GLWorldView::setCameraStereoModeDisable()
534{
535 setStereoType(QGLView::Hardware);
536 camera()->setEyeSeparation(0.0);
537 emit updated(true);
538}
539
540void GLWorldView::setCameraStereoModeHardware()
541{
542 setStereoType(QGLView::Hardware);
543 camera()->setEyeSeparation(defaultEyeSeparation);
544 emit updated(true);
545}
546
547void GLWorldView::setCameraStereoModeLeftRight()
548{
549 setStereoType(QGLView::LeftRight);
550 camera()->setEyeSeparation(defaultEyeSeparation);
551 emit updated(true);
552}
553
554void GLWorldView::setCameraStereoModeRightLeft()
555{
556 setStereoType(QGLView::RightLeft);
557 camera()->setEyeSeparation(defaultEyeSeparation);
558 emit updated(true);
559}
560
561void GLWorldView::setCameraStereoModeTopBottom()
562{
563 setStereoType(QGLView::TopBottom);
564 camera()->setEyeSeparation(defaultEyeSeparation);
565 emit updated(true);
566}
567
568void GLWorldView::setCameraStereoModeBottomTop()
569{
570 setStereoType(QGLView::BottomTop);
571 camera()->setEyeSeparation(defaultEyeSeparation);
572 emit updated(true);
573}
574
575void GLWorldView::setCameraStereoModeAnaglyph()
576{
577 setStereoType(QGLView::RedCyanAnaglyph);
578 camera()->setEyeSeparation(defaultEyeSeparation);
579 emit updated(true);
580}
581
582void GLWorldView::mousePressEvent(QMouseEvent *event)
583{
584 // check for right mouse button
585 if (event->button() == Qt::RightButton) {
586 // NOTE(FH): simply setting setContextMenuPolicy(Qt::CustomContextMenu) did not emit the signal ...
587 emit customContextMenuRequested(event->pos());
588 event->accept();
589 return;
590 }
591
592 // Reset the saved mouse position.
593 lastMousePos = event->posF();
594
595 QGLView::mousePressEvent(event);
596}
597
598/**
599 * Handle a mouse move event.
600 * This is used to control the camera (rotation and translation) when the left button is being pressed.
601 */
602void GLWorldView::mouseMoveEvent(QMouseEvent *event)
603{
604 if (event->buttons() & Qt::LeftButton){
605 // Find the mouse distance since the last event.
606 QPointF d = event->posF() - lastMousePos;
607 lastMousePos = event->posF();
608
609 // Rotate or translate? (inverted by shift key)
610 CameraControlModeType mode = getCameraControlMode(event->modifiers() & Qt::ShiftModifier);
611
612 if (mode == Rotate){
613 // Rotate the camera.
614 d *= 0.3;
615 camera()->tiltPanRollCenter(- d.y(), - d.x(), 0);
616 }else if (mode == Translate){
617 // Translate the camera.
618 d *= 0.02;
619 camera()->translateCenter(- d.x(), d.y(), 0);
620 camera()->translateEye(- d.x(), d.y(), 0);
621 }
622 }
623 QGLView::mouseMoveEvent(event);
624}
625
626/**
627 * When the mouse wheel is used, zoom in or out.
628 */
629void GLWorldView::wheelEvent(QWheelEvent *event)
630{
631 // Find the distance between the eye and focus point.
632 QVector3D d = camera()->eye() - camera()->center();
633
634 // Scale the distance.
635 if (event->delta() < 0)
636 d *= 1.2;
637 else if (event->delta() > 0)
638 d /= 1.2;
639
640 // Set new eye position.
641 camera()->setEye(camera()->center() + d);
642
643 QGLView::wheelEvent(event);
644}
645
646/**
647 * Draw a transparent cube representing the domain.
648 */
649void GLWorldView::drawDomainBox(QGLPainter *painter) const
650{
651 // Apply the domain matrix.
652 RealSpaceMatrix m = World::getInstance().getDomain().getM();
653 painter->modelViewMatrix().push();
654 painter->modelViewMatrix() *= QMatrix4x4(m.at(0,0), m.at(0,1), m.at(0,2), 0.0,
655 m.at(1,0), m.at(1,1), m.at(1,2), 0.0,
656 m.at(2,0), m.at(2,1), m.at(2,2), 0.0,
657 0.0, 0.0, 0.0, 1.0);
658
659 // Draw the transparent cube.
660 painter->setStandardEffect(QGL::LitMaterial);
661 glCullFace(GL_BACK);
662 glEnable(GL_CULL_FACE);
663 glEnable(GL_BLEND);
664 glDepthMask(0);
665 //glDisable(GL_DEPTH_TEST);
666 meshDomainBox->draw(painter);
667 //glEnable(GL_DEPTH_TEST);
668 glDepthMask(1);
669 glDisable(GL_BLEND);
670 glDisable(GL_CULL_FACE);
671
672 // Draw the outlines (if we have drawn the box itself)
673 if ((meshDomainBox->options() & QGLSceneNode::HideNode) == 0) {
674 painter->setFaceMaterial(QGL::AllFaces, domainBoxMaterial);
675 //glEnable(GL_LINE_SMOOTH);
676 QVector3DArray array;
677 array.append(0, 0, 0); array.append(1, 0, 0);
678 array.append(1, 0, 0); array.append(1, 1, 0);
679 array.append(1, 1, 0); array.append(0, 1, 0);
680 array.append(0, 1, 0); array.append(0, 0, 0);
681
682 array.append(0, 0, 1); array.append(1, 0, 1);
683 array.append(1, 0, 1); array.append(1, 1, 1);
684 array.append(1, 1, 1); array.append(0, 1, 1);
685 array.append(0, 1, 1); array.append(0, 0, 1);
686
687 array.append(0, 0, 0); array.append(0, 0, 1);
688 array.append(1, 0, 0); array.append(1, 0, 1);
689 array.append(0, 1, 0); array.append(0, 1, 1);
690 array.append(1, 1, 0); array.append(1, 1, 1);
691 painter->clearAttributes();
692 painter->setVertexAttribute(QGL::Position, array);
693 painter->draw(QGL::Lines, 24);
694 }
695
696 painter->modelViewMatrix().pop();
697}
698
699void GLWorldView::drawDreiBein(QGLPainter *painter)
700{
701 painter->modelViewMatrix().push();
702 painter->modelViewMatrix().translate(camera()->center());
703 painter->setStandardEffect(QGL::LitMaterial);
704 painter->setFaceMaterial(QGL::FrontFaces, NULL);
705 meshDreiBein->draw(painter);
706 painter->modelViewMatrix().pop();
707}
708
709void GLWorldView::sceneHoverSignalled(const atomId_t _id)
710{
711 emit hoverChanged(_id);
712 emit updated(true);
713}
714
715void GLWorldView::sceneHoverSignalled(const moleculeId_t _id, int _i)
716{
717 emit hoverChanged(_id, _i);
718 emit updated(true);
719}
Note: See TracBrowser for help on using the repository browser.