source: src/UIElements/Views/Qt4/Qt3D/GLWorldView.cpp@ 98ad30

Candidate_v1.7.1 stable
Last change on this file since 98ad30 was 98ad30, checked in by Frederik Heber <frederik.heber@…>, 4 weeks ago

QtView: SingleShort timer fires every 10ms.

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