source: src/UIElements/Views/Qt4/Qt3D/GLWorldView.cpp@ 015f8c

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 015f8c was 015f8c, checked in by Frederik Heber <heber@…>, 9 years ago

QtMainWindow uses QtSelectionChangedAgent.

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