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

Candidate_v1.6.1
Last change on this file was 8819d2, checked in by Frederik Heber <frederik.heber@…>, 7 years ago

FIX: GLWorldView no longer caught changes after deactivating DreiBein.

  • the logic relies now in sceneChangeSignalled, which is the changed() signal from the worlscene, and ChangeSignalled, which is the changed() from the worldview itself.
  • both use updateGL() to trigger a repaint, and have two different state variables to check whether the redraw is needed.
  • Property mode set to 100644
File size: 24.0 KB
RevLine 
[d238e7]1/*
2 * Project: MoleCuilder
3 * Description: creates and alters molecular systems
[0aa122]4 * Copyright (C) 2010-2012 University of Bonn. All rights reserved.
[5aaa43]5 * Copyright (C) 2013 Frederik Heber. All rights reserved.
[94d5ac6]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/>.
[d238e7]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>
[0e5d14]39#include <Qt/qaction.h>
[8e7dd9]40#include <QtGui/QMenu>
[0e5d14]41#include <QtGui/QToolBar>
[8e7dd9]42#include <QtGui/QToolButton>
[585f78]43#include <Qt/qtimer.h>
[c7e000]44#include <Qt/qsettings.h>
[26ed25]45#include <Qt3D/qglbuilder.h>
46#include <Qt3D/qglscenenode.h>
47#include <Qt3D/qglsphere.h>
48#include <Qt3D/qglcylinder.h>
[02b2d3]49#include <Qt3D/qglcube.h>
[d238e7]50
[907636]51#include "GLWorldScene.hpp"
[d238e7]52
[9eb71b3]53//#include "CodePatterns/MemDebug.hpp"
[d238e7]54
[53059e]55#include "Atom/AtomObserver.hpp"
56#include "Atom/atom_observable.hpp"
[1d02c2d]57#include "Box.hpp"
[7188b1]58#include "CodePatterns/Log.hpp"
[02ce36]59#include "CodePatterns/Observer/Notification.hpp"
[856d05]60#include "CodePatterns/Observer/ObserverLog.hpp"
[9c259e]61#include "Descriptors/MoleculeIdDescriptor.hpp"
[1d02c2d]62#include "molecule.hpp"
[284551]63#include "Shapes/ShapeRegistry.hpp"
[7188b1]64#include "World.hpp"
[89b992]65#include "WorldTime.hpp"
[7188b1]66
[15c8a9]67GLWorldView::GLWorldView(
[2f7988]68 QtObservedInstanceBoard * _board,
[15c8a9]69 QWidget *parent) :
70 QGLView(parent),
71 Observer("GLWorldView"),
72 worldscene(NULL),
73 changesPresent(false),
74 needsRedraw(false)
[d238e7]75{
[15c8a9]76 worldscene = new GLWorldScene(_board, this);
[d238e7]77
[65487f]78 setOption(QGLView::ObjectPicking, true);
[8880c9]79 setOption(QGLView::CameraNavigation, false);
[b6d92e]80 setFocusPolicy(Qt::StrongFocus);
[8880c9]81 setCameraControlMode(Rotate);
[8e7dd9]82 defaultEyeSeparation = 4.0;
[d238e7]83
[26ed25]84 createDomainBox();
85 createDreiBein();
[e8c636]86 //changeMaterials(false);
87
[8c001a]88 qRegisterMetaType<atomId_t>("atomId_t");
89 qRegisterMetaType<moleculeId_t>("moleculeId_t");
[c48ac12]90
[07136a]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 &)));
[f714763]93// connect(this, SIGNAL(TimeChanged()), worldscene, SIGNAL(updated()));
[65487f]94 connect(worldscene, SIGNAL(changeOccured()), this, SLOT(changeSignalled()));
[8819d2]95 connect(this, SIGNAL(changed()), this, SLOT(changeSignalled()));
[704d59]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)));
[585f78]98 //connect(this, SIGNAL(changed()), this, SLOT(updateGL()));
[8819d2]99 connect(worldscene, SIGNAL(changed()), this, SLOT(sceneChangeSignalled()));
[f91ef6]100 connect(this, SIGNAL(moleculesVisibilityChanged(ObservedValue_Index_t,bool)),
101 worldscene, SLOT(moleculesVisibilityChanged(ObservedValue_Index_t,bool)));
[7188b1]102
[65487f]103 // sign on to changes in the world
[1d9b6dd]104 WorldTime::getInstance().signOn(this, WorldTime::TimeChanged);
[53059e]105 AtomObserver::getInstance().signOn(this, AtomObservable::PositionChanged);
[8819d2]106 AtomObserver::getInstance().signOn(this, AtomObservable::VelocityChanged);
107 AtomObserver::getInstance().signOn(this, AtomObservable::ForceChanged);
[585f78]108
[284551]109 ShapeRegistry::getInstance().signOn(this);
[85c36d]110 ShapeRegistry::getInstance().signOn(this, ShapeRegistry::ShapeInserted);
[284551]111 ShapeRegistry::getInstance().signOn(this, ShapeRegistry::ShapeRemoved);
112 ShapeRegistry::getInstance().signOn(this, ShapeRegistry::SelectionChanged);
113
[585f78]114 redrawTimer = new QTimer(this);
[d238e7]115}
116
[04f017]117GLWorldView::~GLWorldView()
118{
[592d42]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
[1d9b6dd]126 WorldTime::getInstance().signOff(this, WorldTime::TimeChanged);
[53059e]127 AtomObserver::getInstance().signOff(this, AtomObservable::PositionChanged);
[8819d2]128 AtomObserver::getInstance().signOff(this, AtomObservable::VelocityChanged);
129 AtomObserver::getInstance().signOff(this, AtomObservable::ForceChanged);
[284551]130 ShapeRegistry::getInstance().signOff(this);
[85c36d]131 ShapeRegistry::getInstance().signOff(this, ShapeRegistry::ShapeInserted);
[284551]132 ShapeRegistry::getInstance().signOff(this, ShapeRegistry::ShapeRemoved);
133 ShapeRegistry::getInstance().signOff(this, ShapeRegistry::SelectionChanged);
[04f017]134 delete worldscene;
[e8c636]135
136 delete(domainBoxMaterial);
137 for (int i=0;i<3;i++)
138 delete(dreiBeinMaterial[i]);
[04f017]139}
140
[0e5d14]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{
[f31b34]149 // camera control mode
[0e5d14]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);
[8e7dd9]160
[f31b34]161 // stereo mode
[8e7dd9]162 QToolButton *stereoButton = new QToolButton(toolbar);
163 QMenu *stereoMenu = new QMenu();
[a8fc70]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);
[8e7dd9]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);
[5b991a]186 stereoButton->setIcon(QIcon(QPixmap(":/icon_view_stereo.png")));
[8e7dd9]187 stereoButton->setPopupMode(QToolButton::InstantPopup);
188 toolbar->addWidget(stereoButton);
[f31b34]189
190 // selection mode
191 toolbar->addSeparator();
[5b991a]192 QAction *selAtomAction = new QAction(QIcon(QPixmap(":/icon_select_atom.png")), tr("select atom by clicking"), this);
[f31b34]193 connect(selAtomAction, SIGNAL(triggered()), worldscene, SLOT(setSelectionModeAtom()));
194 toolbar->addAction(selAtomAction);
[5b991a]195 QAction *selMolAction = new QAction(QIcon(QPixmap(":/icon_select_molecule.png")), tr("select molecule by clicking"), this);
[f31b34]196 connect(selMolAction, SIGNAL(triggered()), worldscene, SLOT(setSelectionModeMolecule()));
197 toolbar->addAction(selMolAction);
[592d42]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);
[0e5d14]207}
208
[26ed25]209void GLWorldView::createDomainBox()
210{
[c7e000]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);
[592d42]219 const bool status = settings.value("domainBoxEnabled").toBool();
[c7e000]220 settings.endGroup();
221
[02b2d3]222 domainBoxMaterial = new QGLMaterial;
223 domainBoxMaterial->setAmbientColor(QColor(0,0,0,255));
224 domainBoxMaterial->setDiffuseColor(QColor(0,0,0,255));
[c7e000]225 domainBoxMaterial->setEmittedLight(colorFrame);
[02b2d3]226
[26ed25]227
[02b2d3]228 QGLMaterial *material = new QGLMaterial;
[c7e000]229 material->setAmbientColor(colorAmbient);
230 material->setDiffuseColor(colorDiffuse);
[02b2d3]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);
[592d42]240
241 setDomainStatus( status );
[26ed25]242}
243
244void GLWorldView::createDreiBein()
245{
[c7e000]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);
[592d42]254 const bool status = settings.value("dreiBeinEnabled").toBool();
[c7e000]255 settings.endGroup();
256
[02b2d3]257 // Create 3 color for the 3 axes.
[26ed25]258 dreiBeinMaterial[0] = new QGLMaterial;
[c7e000]259 dreiBeinMaterial[0]->setColor(colorX);
[26ed25]260 dreiBeinMaterial[1] = new QGLMaterial;
[c7e000]261 dreiBeinMaterial[1]->setColor(colorY);
[26ed25]262 dreiBeinMaterial[2] = new QGLMaterial;
[c7e000]263 dreiBeinMaterial[2]->setColor(colorZ);
[26ed25]264
[02b2d3]265 // Create the basic meshes (cylinder and cone).
[26ed25]266 QGLBuilder builderCyl;
[37e910]267 builderCyl << QGLCylinder(.15,.15,1.6,16);
[26ed25]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
[02b2d3]279 // Create a scene node from the 3 axes.
[26ed25]280 meshDreiBein = new QGLSceneNode(this);
281
282 // X-direction
283 QGLSceneNode *node = new QGLSceneNode(meshDreiBein);
284 node->setMaterial(dreiBeinMaterial[0]);
285 node->addNode(cyl);
[37e910]286 node->setPosition(QVector3D(.8f, 0.f, 0.f));
[26ed25]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 }
[37e910]304 node->setPosition(QVector3D(0.f, .8f, 0.f));
[26ed25]305
306 // Z-direction
307 node = new QGLSceneNode(meshDreiBein);
308 node->setMaterial(dreiBeinMaterial[2]);
309 node->addNode(cyl);
310 node->addNode(cone);
[37e910]311 node->setPosition(QVector3D(0.f, 0.f, .8f));
[592d42]312
313 setdreiBeinStatus( status );
[26ed25]314}
315
[7188b1]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{
[3d5b5b]322// emit changed();
[7188b1]323}
324
325/**
326 * The observable can tell when it dies.
327 */
[9c259e]328void GLWorldView::subjectKilled(Observable *publisher)
329{
[0f7ffe]330 // world never dies
[9c259e]331}
[7188b1]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{
[1d9b6dd]340 if (static_cast<WorldTime *>(publisher) == WorldTime::getPointer()) {
[89b992]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
[8819d2]347 needsRedraw = false;
[89b992]348 emit changed();
[ce4126]349 emit TimeChanged();
[89b992]350 break;
351 }
352 default:
353 ASSERT(0, "GLWorldView::recieveNotification() - we cannot get here for WorldTime.");
354 break;
355 }
[284551]356 } else if (static_cast<ShapeRegistry*>(publisher) == ShapeRegistry::getPointer()) {
357 switch (notification->getChannelNo()) {
[85c36d]358 case ShapeRegistry::ShapeInserted:
359 {
[8819d2]360 needsRedraw = false;
[07136a]361 emit ShapeAdded(ShapeRegistry::getInstance().lastChanged()->getName());
[85c36d]362 break;
363 }
[284551]364 case ShapeRegistry::ShapeRemoved:
365 {
[8819d2]366 needsRedraw = false;
[07136a]367 emit ShapeRemoved(ShapeRegistry::getInstance().lastChanged()->getName());
[284551]368 break;
369 }
370 case ShapeRegistry::SelectionChanged:
371 {
[8819d2]372 needsRedraw = false;
[85c36d]373 worldscene->updateSelectedShapes();
[284551]374 break;
375 }
376 default:
[89b992]377 ASSERT(0, "GLWorldView::recieveNotification() - we cannot get here for ShapeRegistry.");
[284551]378 break;
379 }
[a9161d]380 }
[7188b1]381}
[04f017]382
[585f78]383void GLWorldView::checkChanges()
384{
385 needsRedraw = false;
[8819d2]386 updateGL();
[585f78]387}
388
[592d42]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;
[8819d2]396 emit changed();
[592d42]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;
[8819d2]414 emit changed();
[592d42]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
[585f78]425void GLWorldView::sceneChangeSignalled()
426{
[8819d2]427 if (needsRedraw){
[585f78]428 redrawTimer->singleShot(0, this, SLOT(checkChanges()));
429 needsRedraw = true;
430 redrawTimer->start();
431 }
432}
433
[d238e7]434void GLWorldView::initializeGL(QGLPainter *painter)
435{
[65487f]436 worldscene->initialize(this, painter);
437 changesPresent = false;
[d238e7]438}
439
440void GLWorldView::paintGL(QGLPainter *painter)
441{
[8819d2]442 if (changesPresent)
[65487f]443 initializeGL(painter);
[72a4c1]444
445 QVector3D cameraDir = camera()->center() - camera()->eye();
446 cameraDir.normalize();
447 QVector4D cameraPlane(cameraDir, QVector3D::dotProduct(cameraDir, camera()->eye()));
448 worldscene->draw(painter, cameraPlane);
[e8c636]449
450 drawDreiBein(painter);
[02b2d3]451
452 // Domain box has to be last because of its transparency.
453 drawDomainBox(painter);
[907636]454}
[d238e7]455
[907636]456void GLWorldView::keyPressEvent(QKeyEvent *e)
457{
[b6d92e]458 // Find the distance between the eye and focus point.
459 QVector3D d = camera()->eye() - camera()->center();
460// LOG(1, "Distance vector eye and center is "
461// << d.x() << "," << d.y() << "," << d.z());
462 // scale the move unit by the eye <-> domain center distance
463 const double key_move_unit = 0.04*(d.length()/50.);
464
[65487f]465 if (e->key() == Qt::Key_Tab) {
466 // The Tab key turns the ShowPicking option on and off,
467 // which helps show what the pick buffer looks like.
468 setOption(QGLView::ShowPicking, ((options() & QGLView::ShowPicking) == 0));
469 updateGL();
[b6d92e]470 } else if ((e->key() == Qt::Key_Minus) || (e->key() == Qt::Key_Plus)) {
471 // Scale the distance.
472 if (e->key() == Qt::Key_Minus)
473 d *= 1.2;
474 else if (e->key() == Qt::Key_Plus)
475 d /= 1.2;
476 // Set new eye position.
477 camera()->setEye(camera()->center() + d);
478 } else if ((e->key() == Qt::Key_Left) || (e->key() == Qt::Key_Right)) {
479 // Translate the camera.
480 const double d = (e->key() == Qt::Key_Left) ? -key_move_unit : key_move_unit;
481 camera()->translateCenter( d, 0., 0);
482 camera()->translateEye( d, 0., 0);
483 } else if ((e->key() == Qt::Key_Up) || (e->key() == Qt::Key_Down)) {
484 // Translate the camera.
485 const double d = (e->key() == Qt::Key_Up) ? -key_move_unit : key_move_unit;
486 camera()->translateCenter( 0., d, 0);
487 camera()->translateEye( 0., d, 0);
488 } else if ((e->key() == Qt::Key_PageUp) || (e->key() == Qt::Key_PageDown)) {
489 // Translate the camera.
490 const double d = (e->key() == Qt::Key_PageUp) ? -key_move_unit : key_move_unit;
491 camera()->translateCenter( 0., 0., d);
492 camera()->translateEye( 0., 0., d);
[65487f]493 }
494 QGLView::keyPressEvent(e);
495}
496
497void GLWorldView::changeSignalled()
498{
499 changesPresent = true;
[8819d2]500 updateGL();
[d238e7]501}
502
503
[0e5d14]504/**
505 * Set the current camera control mode.
506 */
[e13b34]507void GLWorldView::setCameraControlMode(GLWorldView::CameraControlModeType mode)
508{
509 cameraControlMode = mode;
510}
511
512void GLWorldView::setCameraControlModeRotation()
513{
514 setCameraControlMode(Rotate);
515}
516
517void GLWorldView::setCameraControlModeTranslation()
518{
519 setCameraControlMode(Translate);
520}
521
[0e5d14]522/**
523 * Returns the current camera control mode.
524 * This needs to be invertable (rotation - translation), if the shift key is pressed.
525 */
[e13b34]526GLWorldView::CameraControlModeType GLWorldView::getCameraControlMode(bool inverted)
527{
528 if (inverted){
529 if (cameraControlMode == Rotate)
530 return Translate;
531 if (cameraControlMode == Translate)
532 return Rotate;
533 return Rotate;
534 }else
535 return cameraControlMode;
536}
537
[8880c9]538/**
539 * Set the camera so it can oversee the whole domain.
540 */
[e13b34]541void GLWorldView::fitCameraToDomain()
542{
[8880c9]543 // Move the camera focus point to the center of the domain box.
[3f48c2]544 Vector v = World::getInstance().getDomain().translateIn(Vector(0.5, 0.5, 0.5));
545 camera()->setCenter(QVector3D(v[0], v[1], v[2]));
546
[8880c9]547 // Guess some eye distance.
[3f48c2]548 double dist = v.Norm() * 3;
549 camera()->setEye(QVector3D(v[0], v[1], v[2] + dist));
550 camera()->setUpVector(QVector3D(0, 1, 0));
[e13b34]551}
552
[8e7dd9]553void GLWorldView::setCameraStereoModeDisable()
554{
555 setStereoType(QGLView::Hardware);
556 camera()->setEyeSeparation(0.0);
[8819d2]557 emit changed();
[8e7dd9]558}
559
560void GLWorldView::setCameraStereoModeHardware()
561{
562 setStereoType(QGLView::Hardware);
563 camera()->setEyeSeparation(defaultEyeSeparation);
[8819d2]564 emit changed();
[8e7dd9]565}
566
567void GLWorldView::setCameraStereoModeLeftRight()
568{
569 setStereoType(QGLView::LeftRight);
570 camera()->setEyeSeparation(defaultEyeSeparation);
[8819d2]571 emit changed();
[8e7dd9]572}
573
574void GLWorldView::setCameraStereoModeRightLeft()
575{
576 setStereoType(QGLView::RightLeft);
577 camera()->setEyeSeparation(defaultEyeSeparation);
[8819d2]578 emit changed();
[8e7dd9]579}
580
581void GLWorldView::setCameraStereoModeTopBottom()
582{
583 setStereoType(QGLView::TopBottom);
584 camera()->setEyeSeparation(defaultEyeSeparation);
[8819d2]585 emit changed();
[8e7dd9]586}
587
588void GLWorldView::setCameraStereoModeBottomTop()
589{
590 setStereoType(QGLView::BottomTop);
591 camera()->setEyeSeparation(defaultEyeSeparation);
[8819d2]592 emit changed();
[8e7dd9]593}
594
595void GLWorldView::setCameraStereoModeAnaglyph()
596{
597 setStereoType(QGLView::RedCyanAnaglyph);
598 camera()->setEyeSeparation(defaultEyeSeparation);
[8819d2]599 emit changed();
[8e7dd9]600}
601
[8880c9]602void GLWorldView::mousePressEvent(QMouseEvent *event)
603{
604 QGLView::mousePressEvent(event);
605
606 // Reset the saved mouse position.
607 lastMousePos = event->posF();
608}
609
610/**
611 * Handle a mouse move event.
612 * This is used to control the camera (rotation and translation) when the left button is being pressed.
613 */
614void GLWorldView::mouseMoveEvent(QMouseEvent *event)
615{
616 if (event->buttons() & Qt::LeftButton){
617 // Find the mouse distance since the last event.
618 QPointF d = event->posF() - lastMousePos;
619 lastMousePos = event->posF();
620
621 // Rotate or translate? (inverted by shift key)
622 CameraControlModeType mode = getCameraControlMode(event->modifiers() & Qt::ShiftModifier);
623
624 if (mode == Rotate){
625 // Rotate the camera.
626 d *= 0.3;
627 camera()->tiltPanRollCenter(- d.y(), - d.x(), 0);
628 }else if (mode == Translate){
629 // Translate the camera.
630 d *= 0.02;
631 camera()->translateCenter(- d.x(), d.y(), 0);
632 camera()->translateEye(- d.x(), d.y(), 0);
633 }
634 }else{
635 // Without this Qt would not test for hover events (i.e. mouse over an atom).
636 QGLView::mouseMoveEvent(event);
637 }
638}
639
640/**
641 * When the mouse wheel is used, zoom in or out.
642 */
643void GLWorldView::wheelEvent(QWheelEvent *event)
644{
645 // Find the distance between the eye and focus point.
646 QVector3D d = camera()->eye() - camera()->center();
647
648 // Scale the distance.
649 if (event->delta() < 0)
650 d *= 1.2;
651 else if (event->delta() > 0)
652 d /= 1.2;
653
654 // Set new eye position.
655 camera()->setEye(camera()->center() + d);
656}
657
[02b2d3]658/**
659 * Draw a transparent cube representing the domain.
660 */
[e8c636]661void GLWorldView::drawDomainBox(QGLPainter *painter) const
662{
[02b2d3]663 // Apply the domain matrix.
[e8c636]664 RealSpaceMatrix m = World::getInstance().getDomain().getM();
665 painter->modelViewMatrix().push();
666 painter->modelViewMatrix() *= QMatrix4x4(m.at(0,0), m.at(0,1), m.at(0,2), 0.0,
667 m.at(1,0), m.at(1,1), m.at(1,2), 0.0,
668 m.at(2,0), m.at(2,1), m.at(2,2), 0.0,
669 0.0, 0.0, 0.0, 1.0);
670
[02b2d3]671 // Draw the transparent cube.
[073a2ff]672 painter->setStandardEffect(QGL::LitMaterial);
[02b2d3]673 glCullFace(GL_BACK);
674 glEnable(GL_CULL_FACE);
675 glEnable(GL_BLEND);
676 glDepthMask(0);
677 //glDisable(GL_DEPTH_TEST);
678 meshDomainBox->draw(painter);
679 //glEnable(GL_DEPTH_TEST);
680 glDepthMask(1);
681 glDisable(GL_BLEND);
682 glDisable(GL_CULL_FACE);
683
[592d42]684 // Draw the outlines (if we have drawn the box itself)
685 if ((meshDomainBox->options() & QGLSceneNode::HideNode) == 0) {
686 painter->setFaceMaterial(QGL::AllFaces, domainBoxMaterial);
687 //glEnable(GL_LINE_SMOOTH);
688 QVector3DArray array;
689 array.append(0, 0, 0); array.append(1, 0, 0);
690 array.append(1, 0, 0); array.append(1, 1, 0);
691 array.append(1, 1, 0); array.append(0, 1, 0);
692 array.append(0, 1, 0); array.append(0, 0, 0);
693
694 array.append(0, 0, 1); array.append(1, 0, 1);
695 array.append(1, 0, 1); array.append(1, 1, 1);
696 array.append(1, 1, 1); array.append(0, 1, 1);
697 array.append(0, 1, 1); array.append(0, 0, 1);
698
699 array.append(0, 0, 0); array.append(0, 0, 1);
700 array.append(1, 0, 0); array.append(1, 0, 1);
701 array.append(0, 1, 0); array.append(0, 1, 1);
702 array.append(1, 1, 0); array.append(1, 1, 1);
703 painter->clearAttributes();
704 painter->setVertexAttribute(QGL::Position, array);
705 painter->draw(QGL::Lines, 24);
706 }
[02b2d3]707
[e8c636]708 painter->modelViewMatrix().pop();
709}
710
711void GLWorldView::drawDreiBein(QGLPainter *painter)
712{
713 painter->modelViewMatrix().push();
714 painter->modelViewMatrix().translate(camera()->center());
[073a2ff]715 painter->setStandardEffect(QGL::LitMaterial);
[02b2d3]716 painter->setFaceMaterial(QGL::FrontFaces, NULL);
[26ed25]717 meshDreiBein->draw(painter);
[e8c636]718 painter->modelViewMatrix().pop();
719}
720
[704d59]721void GLWorldView::sceneHoverSignalled(const atomId_t _id)
[407638e]722{
[8819d2]723 needsRedraw = true;
724 emit changed();
[704d59]725 emit hoverChanged(_id);
[407638e]726}
[2b596f]727
[704d59]728void GLWorldView::sceneHoverSignalled(const moleculeId_t _id, int _i)
[2b596f]729{
[8819d2]730 needsRedraw = true;
731 emit changed();
[704d59]732 emit hoverChanged(_id, _i);
[2b596f]733}
Note: See TracBrowser for help on using the repository browser.