source: src/UIElements/Views/Qt4/Qt3D/GLWorldView.cpp@ 0f7ffe

Action_Thermostats Add_AtomRandomPerturbation Add_FitFragmentPartialChargesAction Add_RotateAroundBondAction Add_SelectAtomByNameAction Added_ParseSaveFragmentResults AddingActions_SaveParseParticleParameters Adding_Graph_to_ChangeBondActions Adding_MD_integration_tests Adding_ParticleName_to_Atom Adding_StructOpt_integration_tests AtomFragments Automaking_mpqc_open AutomationFragmentation_failures Candidate_v1.5.4 Candidate_v1.6.0 Candidate_v1.6.1 ChangeBugEmailaddress ChangingTestPorts ChemicalSpaceEvaluator CombiningParticlePotentialParsing Combining_Subpackages Debian_Package_split Debian_package_split_molecuildergui_only Disabling_MemDebug Docu_Python_wait EmpiricalPotential_contain_HomologyGraph EmpiricalPotential_contain_HomologyGraph_documentation Enable_parallel_make_install Enhance_userguide Enhanced_StructuralOptimization Enhanced_StructuralOptimization_continued Example_ManyWaysToTranslateAtom Exclude_Hydrogens_annealWithBondGraph FitPartialCharges_GlobalError Fix_BoundInBox_CenterInBox_MoleculeActions Fix_ChargeSampling_PBC Fix_ChronosMutex Fix_FitPartialCharges Fix_FitPotential_needs_atomicnumbers Fix_ForceAnnealing Fix_IndependentFragmentGrids Fix_ParseParticles Fix_ParseParticles_split_forward_backward_Actions Fix_PopActions Fix_QtFragmentList_sorted_selection Fix_Restrictedkeyset_FragmentMolecule Fix_StatusMsg Fix_StepWorldTime_single_argument Fix_Verbose_Codepatterns Fix_fitting_potentials Fixes ForceAnnealing_goodresults ForceAnnealing_oldresults ForceAnnealing_tocheck ForceAnnealing_with_BondGraph ForceAnnealing_with_BondGraph_continued ForceAnnealing_with_BondGraph_continued_betteresults ForceAnnealing_with_BondGraph_contraction-expansion FragmentAction_writes_AtomFragments FragmentMolecule_checks_bonddegrees GeometryObjects Gui_Fixes Gui_displays_atomic_force_velocity ImplicitCharges IndependentFragmentGrids IndependentFragmentGrids_IndividualZeroInstances IndependentFragmentGrids_IntegrationTest IndependentFragmentGrids_Sole_NN_Calculation JobMarket_RobustOnKillsSegFaults JobMarket_StableWorkerPool JobMarket_unresolvable_hostname_fix MoreRobust_FragmentAutomation ODR_violation_mpqc_open PartialCharges_OrthogonalSummation PdbParser_setsAtomName PythonUI_with_named_parameters QtGui_reactivate_TimeChanged_changes Recreated_GuiChecks Rewrite_FitPartialCharges RotateToPrincipalAxisSystem_UndoRedo SaturateAtoms_findBestMatching SaturateAtoms_singleDegree StoppableMakroAction Subpackage_CodePatterns Subpackage_JobMarket Subpackage_LinearAlgebra Subpackage_levmar Subpackage_mpqc_open Subpackage_vmg Switchable_LogView ThirdParty_MPQC_rebuilt_buildsystem TrajectoryDependenant_MaxOrder TremoloParser_IncreasedPrecision TremoloParser_MultipleTimesteps TremoloParser_setsAtomName Ubuntu_1604_changes stable
Last change on this file since 0f7ffe was 0f7ffe, checked in by Frederik Heber <heber@…>, 9 years ago

Changed connections between GLWorldScene, GLWorldView and GLMoleculeObject_molecule.

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