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

Action_Thermostats Add_AtomRandomPerturbation Add_RotateAroundBondAction Add_SelectAtomByNameAction Adding_Graph_to_ChangeBondActions Adding_MD_integration_tests Adding_StructOpt_integration_tests AutomationFragmentation_failures Candidate_v1.6.1 ChangeBugEmailaddress ChangingTestPorts ChemicalSpaceEvaluator Disabling_MemDebug Docu_Python_wait EmpiricalPotential_contain_HomologyGraph_documentation Enhance_userguide Enhanced_StructuralOptimization Enhanced_StructuralOptimization_continued Example_ManyWaysToTranslateAtom Exclude_Hydrogens_annealWithBondGraph Fix_ChronosMutex Fix_StatusMsg Fix_StepWorldTime_single_argument Fix_Verbose_Codepatterns ForceAnnealing_goodresults ForceAnnealing_oldresults ForceAnnealing_tocheck ForceAnnealing_with_BondGraph ForceAnnealing_with_BondGraph_continued ForceAnnealing_with_BondGraph_continued_betteresults ForceAnnealing_with_BondGraph_contraction-expansion GeometryObjects Gui_displays_atomic_force_velocity IndependentFragmentGrids_IntegrationTest JobMarket_RobustOnKillsSegFaults JobMarket_StableWorkerPool PythonUI_with_named_parameters QtGui_reactivate_TimeChanged_changes Recreated_GuiChecks RotateToPrincipalAxisSystem_UndoRedo StoppableMakroAction TremoloParser_IncreasedPrecision TremoloParser_MultipleTimesteps Ubuntu_1604_changes
Last change on this file since 9eb71b3 was 9eb71b3, checked in by Frederik Heber <frederik.heber@…>, 8 years ago

Commented out MemDebug include and Memory::ignore.

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