source: src/UIElements/Views/Qt4/Qt3D/GLWorldView.cpp@ 1b6cec

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 1b6cec was 7f1b51, checked in by Frederik Heber <heber@…>, 10 years ago

Merge branch 'GUI_Fixes' into stable

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