source: src/UIElements/Views/Qt4/Qt3D/GLWorldView.cpp@ 9cdab3

Candidate_v1.7.0 stable
Last change on this file since 9cdab3 was 9cdab3, checked in by Frederik Heber <frederik.heber@…>, 2 months ago

Adds context menu "atom" to GLWorldView.

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