source: src/UIElements/Views/Qt4/Qt3D/GLWorldView.cpp@ 55736b

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 55736b was 5a9f4c, checked in by Frederik Heber <heber@…>, 11 years ago

Removed lots of commented-out code from GLWorldView.

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