source: src/UIElements/Views/Qt4/Qt3D/GLWorldView.cpp@ 73b13c

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 Candidate_v1.7.0 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 73b13c was f759e5, checked in by Frederik Heber <heber@…>, 10 years ago

FIX: GLWorldView must not listen to moleculeRemoved but to its subjectKilled.

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