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
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),
[c742bb1]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 &)));
[704d59]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)));
[57608e]95 connect(worldscene, SIGNAL(hoverEnd()), this, SLOT(triggerUpdateGL()));
96 connect(worldscene, SIGNAL(updated()), this, SLOT(triggerUpdateGL()));
[98ad30]97 connect(this, SIGNAL(updated(const bool)), this, SLOT(triggerUpdateGL(const bool)));
[57608e]98 connect(worldscene, SIGNAL(sceneChanged()), this, SLOT(sceneChangeSignalled()));
[f91ef6]99 connect(this, SIGNAL(moleculesVisibilityChanged(ObservedValue_Index_t,bool)),
100 worldscene, SLOT(moleculesVisibilityChanged(ObservedValue_Index_t,bool)));
[7188b1]101
[c742bb1]102 // sign on to changes in the shape registry
[284551]103 ShapeRegistry::getInstance().signOn(this);
[85c36d]104 ShapeRegistry::getInstance().signOn(this, ShapeRegistry::ShapeInserted);
[284551]105 ShapeRegistry::getInstance().signOn(this, ShapeRegistry::ShapeRemoved);
106 ShapeRegistry::getInstance().signOn(this, ShapeRegistry::SelectionChanged);
107
[585f78]108 redrawTimer = new QTimer(this);
[d238e7]109}
110
[04f017]111GLWorldView::~GLWorldView()
112{
[592d42]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
[284551]120 ShapeRegistry::getInstance().signOff(this);
[85c36d]121 ShapeRegistry::getInstance().signOff(this, ShapeRegistry::ShapeInserted);
[284551]122 ShapeRegistry::getInstance().signOff(this, ShapeRegistry::ShapeRemoved);
123 ShapeRegistry::getInstance().signOff(this, ShapeRegistry::SelectionChanged);
[04f017]124 delete worldscene;
[e8c636]125
126 delete(domainBoxMaterial);
127 for (int i=0;i<3;i++)
128 delete(dreiBeinMaterial[i]);
[04f017]129}
130
[a2c7c3]131GLWorldScene::SelectionModeType GLWorldView::getSelectionMode() const
132{
133 return worldscene->getSelectionMode();
134}
[0e5d14]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{
[f31b34]143 // camera control mode
[0e5d14]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);
[8e7dd9]154
[f31b34]155 // stereo mode
[8e7dd9]156 QToolButton *stereoButton = new QToolButton(toolbar);
157 QMenu *stereoMenu = new QMenu();
[a8fc70]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);
[8e7dd9]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);
[5b991a]180 stereoButton->setIcon(QIcon(QPixmap(":/icon_view_stereo.png")));
[8e7dd9]181 stereoButton->setPopupMode(QToolButton::InstantPopup);
182 toolbar->addWidget(stereoButton);
[f31b34]183
184 // selection mode
185 toolbar->addSeparator();
[5b991a]186 QAction *selAtomAction = new QAction(QIcon(QPixmap(":/icon_select_atom.png")), tr("select atom by clicking"), this);
[f31b34]187 connect(selAtomAction, SIGNAL(triggered()), worldscene, SLOT(setSelectionModeAtom()));
188 toolbar->addAction(selAtomAction);
[5b991a]189 QAction *selMolAction = new QAction(QIcon(QPixmap(":/icon_select_molecule.png")), tr("select molecule by clicking"), this);
[f31b34]190 connect(selMolAction, SIGNAL(triggered()), worldscene, SLOT(setSelectionModeMolecule()));
191 toolbar->addAction(selMolAction);
[592d42]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);
[0e5d14]201}
202
[26ed25]203void GLWorldView::createDomainBox()
204{
[c7e000]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);
[592d42]213 const bool status = settings.value("domainBoxEnabled").toBool();
[c7e000]214 settings.endGroup();
215
[02b2d3]216 domainBoxMaterial = new QGLMaterial;
217 domainBoxMaterial->setAmbientColor(QColor(0,0,0,255));
218 domainBoxMaterial->setDiffuseColor(QColor(0,0,0,255));
[c7e000]219 domainBoxMaterial->setEmittedLight(colorFrame);
[02b2d3]220
[26ed25]221
[02b2d3]222 QGLMaterial *material = new QGLMaterial;
[c7e000]223 material->setAmbientColor(colorAmbient);
224 material->setDiffuseColor(colorDiffuse);
[02b2d3]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);
[592d42]234
235 setDomainStatus( status );
[26ed25]236}
237
238void GLWorldView::createDreiBein()
239{
[c7e000]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);
[592d42]248 const bool status = settings.value("dreiBeinEnabled").toBool();
[c7e000]249 settings.endGroup();
250
[02b2d3]251 // Create 3 color for the 3 axes.
[26ed25]252 dreiBeinMaterial[0] = new QGLMaterial;
[c7e000]253 dreiBeinMaterial[0]->setColor(colorX);
[26ed25]254 dreiBeinMaterial[1] = new QGLMaterial;
[c7e000]255 dreiBeinMaterial[1]->setColor(colorY);
[26ed25]256 dreiBeinMaterial[2] = new QGLMaterial;
[c7e000]257 dreiBeinMaterial[2]->setColor(colorZ);
[26ed25]258
[02b2d3]259 // Create the basic meshes (cylinder and cone).
[26ed25]260 QGLBuilder builderCyl;
[37e910]261 builderCyl << QGLCylinder(.15,.15,1.6,16);
[26ed25]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
[02b2d3]273 // Create a scene node from the 3 axes.
[26ed25]274 meshDreiBein = new QGLSceneNode(this);
275
276 // X-direction
277 QGLSceneNode *node = new QGLSceneNode(meshDreiBein);
278 node->setMaterial(dreiBeinMaterial[0]);
279 node->addNode(cyl);
[37e910]280 node->setPosition(QVector3D(.8f, 0.f, 0.f));
[26ed25]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 }
[37e910]298 node->setPosition(QVector3D(0.f, .8f, 0.f));
[26ed25]299
300 // Z-direction
301 node = new QGLSceneNode(meshDreiBein);
302 node->setMaterial(dreiBeinMaterial[2]);
303 node->addNode(cyl);
304 node->addNode(cone);
[37e910]305 node->setPosition(QVector3D(0.f, 0.f, .8f));
[592d42]306
307 setdreiBeinStatus( status );
[26ed25]308}
309
[7188b1]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{
[98ad30]316// emit updated(true);
[7188b1]317}
318
319/**
320 * The observable can tell when it dies.
321 */
[9c259e]322void GLWorldView::subjectKilled(Observable *publisher)
323{
[0f7ffe]324 // world never dies
[9c259e]325}
[7188b1]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{
[c742bb1]334 if (static_cast<ShapeRegistry*>(publisher) == ShapeRegistry::getPointer()) {
[284551]335 switch (notification->getChannelNo()) {
[85c36d]336 case ShapeRegistry::ShapeInserted:
337 {
[8819d2]338 needsRedraw = false;
[07136a]339 emit ShapeAdded(ShapeRegistry::getInstance().lastChanged()->getName());
[85c36d]340 break;
341 }
[284551]342 case ShapeRegistry::ShapeRemoved:
343 {
[8819d2]344 needsRedraw = false;
[07136a]345 emit ShapeRemoved(ShapeRegistry::getInstance().lastChanged()->getName());
[284551]346 break;
347 }
348 case ShapeRegistry::SelectionChanged:
349 {
[8819d2]350 needsRedraw = false;
[85c36d]351 worldscene->updateSelectedShapes();
[284551]352 break;
353 }
354 default:
[89b992]355 ASSERT(0, "GLWorldView::recieveNotification() - we cannot get here for ShapeRegistry.");
[284551]356 break;
357 }
[a9161d]358 }
[7188b1]359}
[04f017]360
[592d42]361void GLWorldView::changeDreiBein()
362{
363 // invert to new status
364 const bool status = ((meshDreiBein->options() & QGLSceneNode::HideNode) == 0);
365 setdreiBeinStatus(!status);
366 // realize
[98ad30]367 emit updated(true);
[592d42]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
[98ad30]384 emit updated(true);
[592d42]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
[98ad30]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()
[585f78]406{
[98ad30]407 needsRedraw = false;
408 QGLView::updateGL();
[585f78]409}
410
[d238e7]411void GLWorldView::initializeGL(QGLPainter *painter)
412{
[3f5b0a]413 boost::recursive_mutex::scoped_lock lock(changed_mutex);
[65487f]414 worldscene->initialize(this, painter);
415 changesPresent = false;
[d238e7]416}
417
418void GLWorldView::paintGL(QGLPainter *painter)
419{
[3f5b0a]420 {
421 boost::recursive_mutex::scoped_lock lock(changed_mutex);
422 if (changesPresent)
423 initializeGL(painter);
424 }
[72a4c1]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);
[e8c636]430
431 drawDreiBein(painter);
[02b2d3]432
433 // Domain box has to be last because of its transparency.
434 drawDomainBox(painter);
[907636]435}
[d238e7]436
[57608e]437void GLWorldView::sceneChangeSignalled()
438{
439 boost::recursive_mutex::scoped_lock lock(changed_mutex);
440 changesPresent = true;
441}
442
[907636]443void GLWorldView::keyPressEvent(QKeyEvent *e)
444{
[b6d92e]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
[4e124d]452 if (e->key() == Qt::Key_Insert) {
[65487f]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();
[b6d92e]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);
[65487f]480 }
481 QGLView::keyPressEvent(e);
482}
483
[0e5d14]484/**
485 * Set the current camera control mode.
486 */
[e13b34]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
[0e5d14]502/**
503 * Returns the current camera control mode.
504 * This needs to be invertable (rotation - translation), if the shift key is pressed.
505 */
[e13b34]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
[8880c9]518/**
519 * Set the camera so it can oversee the whole domain.
520 */
[e13b34]521void GLWorldView::fitCameraToDomain()
522{
[8880c9]523 // Move the camera focus point to the center of the domain box.
[3f48c2]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
[8880c9]527 // Guess some eye distance.
[3f48c2]528 double dist = v.Norm() * 3;
529 camera()->setEye(QVector3D(v[0], v[1], v[2] + dist));
530 camera()->setUpVector(QVector3D(0, 1, 0));
[e13b34]531}
532
[8e7dd9]533void GLWorldView::setCameraStereoModeDisable()
534{
535 setStereoType(QGLView::Hardware);
536 camera()->setEyeSeparation(0.0);
[98ad30]537 emit updated(true);
[8e7dd9]538}
539
540void GLWorldView::setCameraStereoModeHardware()
541{
542 setStereoType(QGLView::Hardware);
543 camera()->setEyeSeparation(defaultEyeSeparation);
[98ad30]544 emit updated(true);
[8e7dd9]545}
546
547void GLWorldView::setCameraStereoModeLeftRight()
548{
549 setStereoType(QGLView::LeftRight);
550 camera()->setEyeSeparation(defaultEyeSeparation);
[98ad30]551 emit updated(true);
[8e7dd9]552}
553
554void GLWorldView::setCameraStereoModeRightLeft()
555{
556 setStereoType(QGLView::RightLeft);
557 camera()->setEyeSeparation(defaultEyeSeparation);
[98ad30]558 emit updated(true);
[8e7dd9]559}
560
561void GLWorldView::setCameraStereoModeTopBottom()
562{
563 setStereoType(QGLView::TopBottom);
564 camera()->setEyeSeparation(defaultEyeSeparation);
[98ad30]565 emit updated(true);
[8e7dd9]566}
567
568void GLWorldView::setCameraStereoModeBottomTop()
569{
570 setStereoType(QGLView::BottomTop);
571 camera()->setEyeSeparation(defaultEyeSeparation);
[98ad30]572 emit updated(true);
[8e7dd9]573}
574
575void GLWorldView::setCameraStereoModeAnaglyph()
576{
577 setStereoType(QGLView::RedCyanAnaglyph);
578 camera()->setEyeSeparation(defaultEyeSeparation);
[98ad30]579 emit updated(true);
[8e7dd9]580}
581
[8880c9]582void GLWorldView::mousePressEvent(QMouseEvent *event)
583{
[9cdab3]584 // check for right mouse button
585 if (event->button() == Qt::RightButton) {
[71423c]586 // NOTE(FH): simply setting setContextMenuPolicy(Qt::CustomContextMenu) did not emit the signal ...
[9cdab3]587 emit customContextMenuRequested(event->pos());
[71423c]588 event->accept();
589 return;
[9cdab3]590 }
591
[8880c9]592 // Reset the saved mouse position.
593 lastMousePos = event->posF();
[511f02]594
[4e124d]595 QGLView::mousePressEvent(event);
[8880c9]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 }
[511f02]623 QGLView::mouseMoveEvent(event);
[8880c9]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);
[511f02]642
643 QGLView::wheelEvent(event);
[8880c9]644}
645
[02b2d3]646/**
647 * Draw a transparent cube representing the domain.
648 */
[e8c636]649void GLWorldView::drawDomainBox(QGLPainter *painter) const
650{
[02b2d3]651 // Apply the domain matrix.
[e8c636]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
[02b2d3]659 // Draw the transparent cube.
[073a2ff]660 painter->setStandardEffect(QGL::LitMaterial);
[02b2d3]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
[592d42]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 }
[02b2d3]695
[e8c636]696 painter->modelViewMatrix().pop();
697}
698
699void GLWorldView::drawDreiBein(QGLPainter *painter)
700{
701 painter->modelViewMatrix().push();
702 painter->modelViewMatrix().translate(camera()->center());
[073a2ff]703 painter->setStandardEffect(QGL::LitMaterial);
[02b2d3]704 painter->setFaceMaterial(QGL::FrontFaces, NULL);
[26ed25]705 meshDreiBein->draw(painter);
[e8c636]706 painter->modelViewMatrix().pop();
707}
708
[704d59]709void GLWorldView::sceneHoverSignalled(const atomId_t _id)
[407638e]710{
[704d59]711 emit hoverChanged(_id);
[98ad30]712 emit updated(true);
[407638e]713}
[2b596f]714
[704d59]715void GLWorldView::sceneHoverSignalled(const moleculeId_t _id, int _i)
[2b596f]716{
[704d59]717 emit hoverChanged(_id, _i);
[98ad30]718 emit updated(true);
[c0e35d]719}
Note: See TracBrowser for help on using the repository browser.