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

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 9a7ef9 was 2b596f, checked in by Frederik Heber <heber@…>, 10 years ago

Hovering over molecules is working including update of QtInfoBox.

  • Property mode set to 100644
File size: 24.7 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<atomId_t>("atomId_t");
81 qRegisterMetaType<moleculeId_t>("moleculeId_t");
82
83 connect(this, SIGNAL(ShapeAdded()), worldscene, SLOT(addShape()));
84 connect(this, SIGNAL(ShapeRemoved()), worldscene, SLOT(removeShape()));
85 connect(this, SIGNAL(TimeChanged()), worldscene, SIGNAL(updated()));
86 connect(worldscene, SIGNAL(changeOccured()), this, SLOT(changeSignalled()));
87 connect(worldscene, SIGNAL(changed()), this, SIGNAL(changed()));
88 connect(worldscene, SIGNAL(hoverChanged(const atom &)), this, SLOT(sceneHoverSignalled(const atom &)));
89 connect(worldscene, SIGNAL(hoverChanged(const molecule &, int)), this, SLOT(sceneHoverSignalled(const molecule &, int)));
90 connect(this, SIGNAL(worldSelectionChanged()), worldscene, SLOT(worldSelectionChanged()));
91 connect(this, SIGNAL(moleculeRemoved(const moleculeId_t)), worldscene, SLOT(moleculeRemoved(const moleculeId_t)));
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 connect(this, SIGNAL(moleculesVisibilityChanged(const moleculeId_t,bool)), worldscene, SLOT(moleculesVisibilityChanged(const moleculeId_t,bool)));
96
97 // sign on to changes in the world
98 World::getInstance().signOn(this);
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::SelectionChanged:
340 {
341 #ifdef LOG_OBSERVER
342 observerLog().addMessage() << "++ Observer " << observerLog().getName(static_cast<Observer *>(this)) << " received notification that selection has changed.";
343 #endif
344 emit worldSelectionChanged();
345 break;
346 }
347 case World::MoleculeInserted:
348 {
349 const molecule * _mol = World::getInstance().lastChanged<molecule>();
350 #ifdef LOG_OBSERVER
351 observerLog().addMessage() << "++ Observer " << observerLog().getName(static_cast<Observer *>(this)) << " received notification that molecule "+toString(_mol->getId())+" has been removed.";
352 #endif
353 emit moleculeInserted(_mol);
354 break;
355 }
356 case World::MoleculeRemoved:
357 {
358 const moleculeId_t _id = World::getInstance().lastChanged<molecule>()->getId();
359 #ifdef LOG_OBSERVER
360 observerLog().addMessage() << "++ Observer " << observerLog().getName(static_cast<Observer *>(this)) << " received notification that molecule "+toString(_id)+" has been removed.";
361 #endif
362 emit moleculeRemoved(_id);
363 break;
364 }
365 default:
366 ASSERT(0, "GLWorldView::recieveNotification() - we cannot get here for World.");
367 break;
368 }
369 } else if (static_cast<WorldTime *>(publisher) == WorldTime::getPointer()) {
370 switch (notification->getChannelNo()) {
371 case WorldTime::TimeChanged:
372 {
373#ifdef LOG_OBSERVER
374 observerLog().addMessage() << "++ Observer " << observerLog().getName(static_cast<Observer *>(this)) << " received notification that WorldTime's time has changed.";
375#endif
376 emit changed();
377 emit TimeChanged();
378 break;
379 }
380 default:
381 ASSERT(0, "GLWorldView::recieveNotification() - we cannot get here for WorldTime.");
382 break;
383 }
384} else if (dynamic_cast<AtomObservable *>(publisher) != NULL) {
385 switch (notification->getChannelNo()) {
386 case AtomObservable::PositionChanged:
387 {
388 #ifdef LOG_OBSERVER
389 const atom *_atom = dynamic_cast<const atom *>(publisher);
390 observerLog().addMessage() << "++ Observer " << observerLog().getName(static_cast<Observer *>(this)) << " received notification that atom "+toString(_atom->getId())+" has changed its position.";
391 #endif
392 emit changed();
393 break;
394 }
395 default:
396 ASSERT(0, "GLWorldView::recieveNotification() - we cannot get here for AtomObservable.");
397 break;
398 }
399 } else if (static_cast<ShapeRegistry*>(publisher) == ShapeRegistry::getPointer()) {
400 switch (notification->getChannelNo()) {
401 case ShapeRegistry::ShapeInserted:
402 {
403 emit ShapeAdded();
404 break;
405 }
406 case ShapeRegistry::ShapeRemoved:
407 {
408 emit ShapeRemoved();
409 break;
410 }
411 case ShapeRegistry::SelectionChanged:
412 {
413 worldscene->updateSelectedShapes();
414 break;
415 }
416 default:
417 ASSERT(0, "GLWorldView::recieveNotification() - we cannot get here for ShapeRegistry.");
418 break;
419 }
420 } else
421 ASSERT(0, "GLWorldView::recieveNotification() - received notification from unknown source.");
422}
423
424void GLWorldView::checkChanges()
425{
426 updateGL();
427 needsRedraw = false;
428}
429
430void GLWorldView::changeDreiBein()
431{
432 // invert to new status
433 const bool status = ((meshDreiBein->options() & QGLSceneNode::HideNode) == 0);
434 setdreiBeinStatus(!status);
435 // realize
436 updateGL();
437 needsRedraw = true;
438}
439
440void GLWorldView::setdreiBeinStatus(const bool status)
441{
442 if (status)
443 meshDreiBein->setOptions( meshDreiBein->options() & (255-QGLSceneNode::HideNode) );
444 else
445 meshDreiBein->setOptions( meshDreiBein->options() | QGLSceneNode::HideNode );
446}
447
448void GLWorldView::changeDomain()
449{
450 // invert to new status
451 const bool status = ((meshDomainBox->options() & QGLSceneNode::HideNode) == 0);
452 setDomainStatus(!status);
453 // realize
454 updateGL();
455 needsRedraw = true;
456}
457
458void GLWorldView::setDomainStatus(const bool status)
459{
460 if (status)
461 meshDomainBox->setOptions( meshDomainBox->options() & (255-QGLSceneNode::HideNode) );
462 else
463 meshDomainBox->setOptions( meshDomainBox->options() | QGLSceneNode::HideNode );
464}
465
466void GLWorldView::sceneChangeSignalled()
467{
468 if (!needsRedraw){
469 redrawTimer->singleShot(0, this, SLOT(checkChanges()));
470 needsRedraw = true;
471 redrawTimer->start();
472 }
473}
474
475void GLWorldView::initializeGL(QGLPainter *painter)
476{
477 worldscene->initialize(this, painter);
478 changesPresent = false;
479}
480
481void GLWorldView::paintGL(QGLPainter *painter)
482{
483 if (changesPresent) {
484 initializeGL(painter);
485 changesPresent = false;
486 }
487
488 QVector3D cameraDir = camera()->center() - camera()->eye();
489 cameraDir.normalize();
490 QVector4D cameraPlane(cameraDir, QVector3D::dotProduct(cameraDir, camera()->eye()));
491 worldscene->draw(painter, cameraPlane);
492
493 drawDreiBein(painter);
494
495 // Domain box has to be last because of its transparency.
496 drawDomainBox(painter);
497}
498
499void GLWorldView::keyPressEvent(QKeyEvent *e)
500{
501 if (e->key() == Qt::Key_Tab) {
502 // The Tab key turns the ShowPicking option on and off,
503 // which helps show what the pick buffer looks like.
504 setOption(QGLView::ShowPicking, ((options() & QGLView::ShowPicking) == 0));
505 updateGL();
506 }
507 QGLView::keyPressEvent(e);
508}
509
510void GLWorldView::changeSignalled()
511{
512 changesPresent = true;
513}
514
515
516/**
517 * Set the current camera control mode.
518 */
519void GLWorldView::setCameraControlMode(GLWorldView::CameraControlModeType mode)
520{
521 cameraControlMode = mode;
522}
523
524void GLWorldView::setCameraControlModeRotation()
525{
526 setCameraControlMode(Rotate);
527}
528
529void GLWorldView::setCameraControlModeTranslation()
530{
531 setCameraControlMode(Translate);
532}
533
534/**
535 * Returns the current camera control mode.
536 * This needs to be invertable (rotation - translation), if the shift key is pressed.
537 */
538GLWorldView::CameraControlModeType GLWorldView::getCameraControlMode(bool inverted)
539{
540 if (inverted){
541 if (cameraControlMode == Rotate)
542 return Translate;
543 if (cameraControlMode == Translate)
544 return Rotate;
545 return Rotate;
546 }else
547 return cameraControlMode;
548}
549
550/**
551 * Set the camera so it can oversee the whole domain.
552 */
553void GLWorldView::fitCameraToDomain()
554{
555 // Move the camera focus point to the center of the domain box.
556 Vector v = World::getInstance().getDomain().translateIn(Vector(0.5, 0.5, 0.5));
557 camera()->setCenter(QVector3D(v[0], v[1], v[2]));
558
559 // Guess some eye distance.
560 double dist = v.Norm() * 3;
561 camera()->setEye(QVector3D(v[0], v[1], v[2] + dist));
562 camera()->setUpVector(QVector3D(0, 1, 0));
563}
564
565void GLWorldView::setCameraStereoModeDisable()
566{
567 setStereoType(QGLView::Hardware);
568 camera()->setEyeSeparation(0.0);
569 updateGL();
570}
571
572void GLWorldView::setCameraStereoModeHardware()
573{
574 setStereoType(QGLView::Hardware);
575 camera()->setEyeSeparation(defaultEyeSeparation);
576 updateGL();
577}
578
579void GLWorldView::setCameraStereoModeLeftRight()
580{
581 setStereoType(QGLView::LeftRight);
582 camera()->setEyeSeparation(defaultEyeSeparation);
583 updateGL();
584}
585
586void GLWorldView::setCameraStereoModeRightLeft()
587{
588 setStereoType(QGLView::RightLeft);
589 camera()->setEyeSeparation(defaultEyeSeparation);
590 updateGL();
591}
592
593void GLWorldView::setCameraStereoModeTopBottom()
594{
595 setStereoType(QGLView::TopBottom);
596 camera()->setEyeSeparation(defaultEyeSeparation);
597 updateGL();
598}
599
600void GLWorldView::setCameraStereoModeBottomTop()
601{
602 setStereoType(QGLView::BottomTop);
603 camera()->setEyeSeparation(defaultEyeSeparation);
604 updateGL();
605}
606
607void GLWorldView::setCameraStereoModeAnaglyph()
608{
609 setStereoType(QGLView::RedCyanAnaglyph);
610 camera()->setEyeSeparation(defaultEyeSeparation);
611 updateGL();
612}
613
614void GLWorldView::mousePressEvent(QMouseEvent *event)
615{
616 QGLView::mousePressEvent(event);
617
618 // Reset the saved mouse position.
619 lastMousePos = event->posF();
620}
621
622/**
623 * Handle a mouse move event.
624 * This is used to control the camera (rotation and translation) when the left button is being pressed.
625 */
626void GLWorldView::mouseMoveEvent(QMouseEvent *event)
627{
628 if (event->buttons() & Qt::LeftButton){
629 // Find the mouse distance since the last event.
630 QPointF d = event->posF() - lastMousePos;
631 lastMousePos = event->posF();
632
633 // Rotate or translate? (inverted by shift key)
634 CameraControlModeType mode = getCameraControlMode(event->modifiers() & Qt::ShiftModifier);
635
636 if (mode == Rotate){
637 // Rotate the camera.
638 d *= 0.3;
639 camera()->tiltPanRollCenter(- d.y(), - d.x(), 0);
640 }else if (mode == Translate){
641 // Translate the camera.
642 d *= 0.02;
643 camera()->translateCenter(- d.x(), d.y(), 0);
644 camera()->translateEye(- d.x(), d.y(), 0);
645 }
646 }else{
647 // Without this Qt would not test for hover events (i.e. mouse over an atom).
648 QGLView::mouseMoveEvent(event);
649 }
650}
651
652/**
653 * When the mouse wheel is used, zoom in or out.
654 */
655void GLWorldView::wheelEvent(QWheelEvent *event)
656{
657 // Find the distance between the eye and focus point.
658 QVector3D d = camera()->eye() - camera()->center();
659
660 // Scale the distance.
661 if (event->delta() < 0)
662 d *= 1.2;
663 else if (event->delta() > 0)
664 d /= 1.2;
665
666 // Set new eye position.
667 camera()->setEye(camera()->center() + d);
668}
669
670/**
671 * Draw a transparent cube representing the domain.
672 */
673void GLWorldView::drawDomainBox(QGLPainter *painter) const
674{
675 // Apply the domain matrix.
676 RealSpaceMatrix m = World::getInstance().getDomain().getM();
677 painter->modelViewMatrix().push();
678 painter->modelViewMatrix() *= QMatrix4x4(m.at(0,0), m.at(0,1), m.at(0,2), 0.0,
679 m.at(1,0), m.at(1,1), m.at(1,2), 0.0,
680 m.at(2,0), m.at(2,1), m.at(2,2), 0.0,
681 0.0, 0.0, 0.0, 1.0);
682
683 // Draw the transparent cube.
684 painter->setStandardEffect(QGL::LitMaterial);
685 glCullFace(GL_BACK);
686 glEnable(GL_CULL_FACE);
687 glEnable(GL_BLEND);
688 glDepthMask(0);
689 //glDisable(GL_DEPTH_TEST);
690 meshDomainBox->draw(painter);
691 //glEnable(GL_DEPTH_TEST);
692 glDepthMask(1);
693 glDisable(GL_BLEND);
694 glDisable(GL_CULL_FACE);
695
696 // Draw the outlines (if we have drawn the box itself)
697 if ((meshDomainBox->options() & QGLSceneNode::HideNode) == 0) {
698 painter->setFaceMaterial(QGL::AllFaces, domainBoxMaterial);
699 //glEnable(GL_LINE_SMOOTH);
700 QVector3DArray array;
701 array.append(0, 0, 0); array.append(1, 0, 0);
702 array.append(1, 0, 0); array.append(1, 1, 0);
703 array.append(1, 1, 0); array.append(0, 1, 0);
704 array.append(0, 1, 0); array.append(0, 0, 0);
705
706 array.append(0, 0, 1); array.append(1, 0, 1);
707 array.append(1, 0, 1); array.append(1, 1, 1);
708 array.append(1, 1, 1); array.append(0, 1, 1);
709 array.append(0, 1, 1); array.append(0, 0, 1);
710
711 array.append(0, 0, 0); array.append(0, 0, 1);
712 array.append(1, 0, 0); array.append(1, 0, 1);
713 array.append(0, 1, 0); array.append(0, 1, 1);
714 array.append(1, 1, 0); array.append(1, 1, 1);
715 painter->clearAttributes();
716 painter->setVertexAttribute(QGL::Position, array);
717 painter->draw(QGL::Lines, 24);
718 }
719
720 painter->modelViewMatrix().pop();
721}
722
723void GLWorldView::drawDreiBein(QGLPainter *painter)
724{
725 painter->modelViewMatrix().push();
726 painter->modelViewMatrix().translate(camera()->center());
727 painter->setStandardEffect(QGL::LitMaterial);
728 painter->setFaceMaterial(QGL::FrontFaces, NULL);
729 meshDreiBein->draw(painter);
730 painter->modelViewMatrix().pop();
731}
732
733void GLWorldView::sceneHoverSignalled(const atom &_atom)
734{
735 emit hoverChanged(_atom);
736}
737
738void GLWorldView::sceneHoverSignalled(const molecule &_mol, int _i)
739{
740 emit hoverChanged(_mol, _i);
741}
Note: See TracBrowser for help on using the repository browser.