source: src/UIElements/Views/Qt4/Qt3D/GLWorldScene.cpp@ 35ab62

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 35ab62 was 4a187d, checked in by Frederik Heber <heber@…>, 10 years ago

GLWorldScene also takes note of empty molecule.

  • this is for the case when a molecule is signalled empty before it signals ready for removal. This may happen cause the empty and the removal signal come from different sources, empty from GLMoleculeObject_molecule and removal from the board.
  • Property mode set to 100644
File size: 30.1 KB
RevLine 
[907636]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/>.
[907636]22 */
23
24/*
25 * GLWorldScene.cpp
26 *
27 * This is based on the Qt3D example "teaservice", specifically parts of teaservice.cpp.
28 *
29 * Created on: Aug 17, 2011
30 * Author: heber
31 */
32
33// include config.h
34#ifdef HAVE_CONFIG_H
35#include <config.h>
36#endif
37
38#include "GLWorldScene.hpp"
[d1196d]39#include <Qt3D/qglview.h>
[bca99d]40#include <Qt3D/qglbuilder.h>
41#include <Qt3D/qglscenenode.h>
42#include <Qt3D/qglsphere.h>
43#include <Qt3D/qglcylinder.h>
[907636]44
[015f8c]45#include "UIElements/Views/Qt4/Qt3D/GLMoleculeObject.hpp"
46#include "UIElements/Views/Qt4/Qt3D/GLMoleculeObject_atom.hpp"
47#include "UIElements/Views/Qt4/Qt3D/GLMoleculeObject_bond.hpp"
48#include "UIElements/Views/Qt4/Qt3D/GLMoleculeObject_molecule.hpp"
49#include "UIElements/Views/Qt4/Qt3D/GLMoleculeObject_shape.hpp"
50
[2f7988]51#include "UIElements/Qt4/InstanceBoard/QtObservedInstanceBoard.hpp"
[907636]52
53#include "CodePatterns/MemDebug.hpp"
54
[7188b1]55#include "CodePatterns/Log.hpp"
56
[0e9ffe]57#include "Actions/SelectionAction/Atoms/AtomByIdAction.hpp"
[89643d]58#include "Actions/SelectionAction/Atoms/NotAtomByIdAction.hpp"
[6966b7]59#include "Actions/SelectionAction/Molecules/MoleculeByIdAction.hpp"
60#include "Actions/SelectionAction/Molecules/NotMoleculeByIdAction.hpp"
[6f0841]61#include "Atom/atom.hpp"
[7188b1]62#include "Bond/bond.hpp"
[89643d]63#include "Descriptors/AtomIdDescriptor.hpp"
[8c001a]64#include "Descriptors/MoleculeIdDescriptor.hpp"
[37b2575]65#include "Helpers/helpers.hpp"
[85c36d]66#include "Shapes/ShapeRegistry.hpp"
[907636]67#include "molecule.hpp"
68#include "World.hpp"
69
[2ad1ec]70#include <iostream>
71
[ce7fdc]72using namespace MoleCuilder;
[907636]73
[15c8a9]74GLWorldScene::GLWorldScene(
[2f7988]75 QtObservedInstanceBoard * _board,
[15c8a9]76 QObject *parent) :
77 QObject(parent),
78 selectionMode(SelectAtom),
79 board(_board)
[907636]80{
[72a4c1]81 int sphereDetails[] = {5, 3, 2, 0};
82 int cylinderDetails[] = {16, 8, 6, 3};
83 for (int i=0;i<GLMoleculeObject::DETAILTYPES_MAX;i++){
84 QGLBuilder emptyBuilder;
[8c001a]85 GLMoleculeObject::meshEmpty[i] = emptyBuilder.finalizedSceneNode();
[72a4c1]86 QGLBuilder sphereBuilder;
87 sphereBuilder << QGLSphere(2.0, sphereDetails[i]);
[8c001a]88 GLMoleculeObject::meshSphere[i] = sphereBuilder.finalizedSceneNode();
89 GLMoleculeObject::meshSphere[i]->setOption(QGLSceneNode::CullBoundingBox, true);
[72a4c1]90 QGLBuilder cylinderBuilder;
91 cylinderBuilder << QGLCylinder(.25,.25,1.0,cylinderDetails[i]);
[8c001a]92 GLMoleculeObject::meshCylinder[i] = cylinderBuilder.finalizedSceneNode();
93 GLMoleculeObject::meshCylinder[i]->setOption(QGLSceneNode::CullBoundingBox, true);
[72a4c1]94 }
[1b07b1]95 connect(board, SIGNAL(moleculeInserted(QtObservedMolecule::ptr)),
[59f1bc]96 this, SLOT(moleculeSignOn(QtObservedMolecule::ptr)), Qt::DirectConnection);
[15c8a9]97 connect(board, SIGNAL(moleculeRemoved(const moleculeId_t)),
[59f1bc]98 this, SLOT(moleculeSignOff(const moleculeId_t)), Qt::DirectConnection);
[15c8a9]99 connect(board, SIGNAL(moleculeIndexChanged(const moleculeId_t, const moleculeId_t)),
100 this, SLOT(moleculeIndexChanged(const moleculeId_t, const moleculeId_t)));
[59f1bc]101 connect(board, SIGNAL(atomInserted(QtObservedAtom::ptr)),
102 this, SLOT(atomInserted(QtObservedAtom::ptr)));
103 connect(board, SIGNAL(atomRemoved(const atomId_t)),
104 this, SLOT(atomRemoved(const atomId_t)));
[8281cc]105 connect(this, SIGNAL(insertMolecule(QtObservedMolecule::ptr)),
106 this, SLOT(moleculeInserted(QtObservedMolecule::ptr)) );
[e29427]107 connect(this, SIGNAL(removeMolecule(QtObservedMolecule*)),
108 this, SLOT(moleculeRemoved(QtObservedMolecule*)) );
[0a6ff9]109
[f714763]110// connect(this, SIGNAL(updated()), this, SLOT(update()));
[907636]111}
112
113GLWorldScene::~GLWorldScene()
[7188b1]114{
115 // remove all elements
116 GLMoleculeObject::cleanMaterialMap();
117}
[907636]118
[8c001a]119void GLWorldScene::atomClicked(atomId_t no)
120{
121 LOG(3, "INFO: GLMoleculeObject_molecule - atom " << no << " has been clicked.");
[f01769]122 const atom * const Walker = const_cast<const World &>(World::getInstance()).
123 getAtom(AtomById(no));
[369cb1]124 ASSERT( Walker != NULL,
125 "GLWorldScene::atomClicked() - clicked atom has disappeared.");
[8c001a]126 if (selectionMode == SelectAtom){
127 if (!World::getInstance().isSelected(Walker))
128 SelectionAtomById(std::vector<atomId_t>(1,no));
129 else
130 SelectionNotAtomById(std::vector<atomId_t>(1,no));
131 }else if (selectionMode == SelectMolecule){
132 const molecule *mol = Walker->getMolecule();
133 ASSERT(mol, "Atom without molecule has been clicked.");
[d7cad1]134 molids_t ids(1, mol->getId());
[8c001a]135 if (!World::getInstance().isSelected(mol))
[d7cad1]136 SelectionMoleculeById(ids);
[8c001a]137 else
[d7cad1]138 SelectionNotMoleculeById(ids);
[8c001a]139 }
140 emit clicked(no);
141}
142
[9a7ef9]143void GLWorldScene::moleculeClicked(moleculeId_t no)
144{
145 LOG(3, "INFO: GLMoleculeObject_molecule - mol " << no << " has been clicked.");
[63fb7a]146 const molecule * const mol= const_cast<const World &>(World::getInstance()).
147 getMolecule(MoleculeById(no));
[9a7ef9]148 ASSERT(mol, "Atom without molecule has been clicked.");
[d7cad1]149 molids_t ids(1, mol->getId());
[9a7ef9]150 if (!World::getInstance().isSelected(mol))
[d7cad1]151 SelectionMoleculeById(ids);
[9a7ef9]152 else
[d7cad1]153 SelectionNotMoleculeById(ids);
[9a7ef9]154 emit clicked(no);
155}
156
[59f1bc]157/** Prepares insertion of a general atom.
[9c259e]158 *
[59f1bc]159 * This is called before the insertion into a molecule and thus before the
160 * insertion into the scene.
161 *
162 * @param _atom atom to insert
[9c259e]163 */
[1b07b1]164void GLWorldScene::atomInserted(QtObservedAtom::ptr _atom)
[9c259e]165{
[59f1bc]166 const atomId_t atomid = _atom->getAtomIndex();
167 ASSERT( QtObservedAtomMap.find(atomid) == QtObservedAtomMap.end(),
168 "GLWorldScene::AtomInserted() - atom with id "+toString(atomid)
169 +" is already present in QtObservedAtomMap.");
170 QtObservedAtomMap[atomid] = _atom;
171 connect(_atom.get(), SIGNAL(indexChanged(const atomId_t,const atomId_t)),
172 this, SLOT(atomIndexChanged(const atomId_t,const atomId_t)) );
173}
174
175/** Removes an general atom.
176 *
177 * This is called when the atom has been removed from the molecule.
178 *
179 * @param _atom atom to remove
180 */
181void GLWorldScene::atomRemoved(const atomId_t _atomid)
182{
183 const QtObservedAtomMap_t::iterator eraseiter = QtObservedAtomMap.find(_atomid);
184 ASSERT( eraseiter != QtObservedAtomMap.end(),
185 "GLWorldScene::AtomRemoved() - atom with id "+toString(_atomid)
186 +" is not present in QtObservedAtomMap.");
187 disconnect(eraseiter->second.get(), SIGNAL(indexChanged(const atomId_t,const atomId_t)),
188 this, SLOT(atomIndexChanged(const atomId_t,const atomId_t)) );
189 QtObservedAtomMap.erase(eraseiter);
190}
191
192/** Inserts an atom into the scene when molecule is present.
193 *
194 * @param _atom atom to insert
195 */
[96e145]196void GLWorldScene::moleculesAtomInserted(QtObservedAtom::ptr _atom, QtObservedMolecule * _mol)
[59f1bc]197{
[1b07b1]198 const atomId_t atomid = _atom->getAtomIndex();
[1187c5]199 QtObservedMolecule* const atommol = _atom->getAtomMolecule();
200 // this ASSERT cannot work: The same molecule may get reassigned to a different
201 // molecule, when e.g. the time step changes (which triggers an update-molecules).
202 // The GUI is slow and may lack behind and thus get to execute the event
203 // moleculeAtomInserted when the change has been already been done.
204// ASSERT( (atommol == NULL) || (_mol == atommol),
205// "GLWorldScene::moleculesAtomInserted() - atom "+toString(atomid)
206// +" claims to belong to QtObservedMolecule "+toString(atommol)
207// +" but signal came from QtObservedMolecule "+toString(_mol)+".");
[1b07b1]208 LOG(3, "INFO: GLWorldScene: Received signal atomInserted for atom "+toString(atomid)+".");
[1187c5]209 const moleculeId_t molid = _mol->getMolIndex();
[7c7c4a]210
[9c259e]211 // check of molecule is already present
[59f1bc]212 boost::recursive_mutex::scoped_lock lock(MoleculeinSceneMap_mutex);
[96e145]213 const MoleculeNodeMap::iterator moliter = MoleculesinSceneMap.find(molid);
[8d5fbf1]214 if (moliter != MoleculesinSceneMap.end()) {
[4a187d]215 const RemovedMoleculesMap_t::iterator emptyiter = EmptyMolecules.find(_mol);
216 if (emptyiter != EmptyMolecules.end())
217 EmptyMolecules.erase(emptyiter);
[96e145]218 // check that it is the right molecule
219 QtObservedMolecule::ptr &checkmol = moliter->second->ObservedMolecule;
220 if (checkmol.get() == _mol) {
221 LOG(3, "INFO: GLWorldScene: Sending signal moleculesAtomInserted for atom "+toString(atomid)+".");
222 QMetaObject::invokeMethod(moliter->second, // pointer to a QObject
223 "atomInserted", // member name (no parameters here)
224 Qt::QueuedConnection, // connection type
225 Q_ARG(QtObservedAtom::ptr, _atom)); // parameters
226 } else {
227 // relay atomRemoved to GLMoleculeObject_molecule in RemovedMolecules
228// LOG(3, "INFO: GLWorldScene: Sending signal moleculesAtomInserted for atom "+toString(_atomid)
229// +" to molecule in RemovedMolecules.");
[e29427]230// const RemovedMoleculesMap_t::iterator removedmoliter = RemovedMolecules.find(molid);
[96e145]231// ASSERT( removedmoliter != RemovedMolecules.end(),
232// "GLWorldScene::moleculesAtomInserted() - signal from old mol "
233// +toString(molid)+", but not present in RemovedMolecules");
234// QMetaObject::invokeMethod(removedmoliter->second, // pointer to a QObject
235// "atomInserted ", // member name (no parameters here)
236// Qt::QueuedConnection, // connection type
237// Q_ARG(QtObservedAtom::ptr, _atom)); // parameters
238 ASSERT( 0,
239 "GLWorldScene::moleculesAtomInserted() - would need to send atomInserted to already removed molecule.");
240 }
[9c259e]241 } else {
[e29427]242 const RemovedMoleculesMap_t::iterator removedmoliter = RemovedMolecules.find(_mol);
243 if (removedmoliter != RemovedMolecules.end()) {
244 ASSERT( 0,
245 "GLWorldScene::moleculesAtomInserted() - would need to send atomInserted to already removed molecule.");
246 } else {
247 boost::recursive_mutex::scoped_lock lock(MoleculeMissedStateMap_mutex);
248 // only record missed state for molecule if (still) present but not instantiated
249 if (QtObservedMoleculeMap.count(molid)) {
250 // store signal for when it is instantiated
251 if (MoleculeMissedStateMap.count(molid) == 0)
252 MoleculeMissedStateMap.insert( std::make_pair(molid ,StateChangeMap_t()) );
253 MoleculeMissedStateMap[molid].insert( std::make_pair(atomid, atomInsertedState) );
254 ASSERT( QtObservedAtomMap[atomid] == _atom,
255 "GLWorldScene::moleculesAtomInserted() - atom "+toString(atomid)
256 +" inserted in molecule "+toString(molid)
257 +" which does not match atom in QtObservedAtomMap.");
258 LOG(3, "INFO: GLWorldScene: Placing atomInserted for atom " << atomid
259 << " and molecule " << molid << " into missed state map.");
260 }
[59f1bc]261 }
[9c259e]262 }
263}
264
265/** Removes an atom into the scene before molecule is present.
266 *
[59f1bc]267 * @param _atomid atom to remove
[9c259e]268 */
[96e145]269void GLWorldScene::moleculesAtomRemoved(const atomId_t _atomid, QtObservedMolecule * _mol)
[9c259e]270{
[8281cc]271 LOG(3, "INFO: GLWorldScene: Received signal atomRemoved for atom "+toString(_atomid)+".");
272
[805fdc]273 const moleculeId_t molid = _mol->getMolIndex();
274 // check of molecule is already present
275 boost::recursive_mutex::scoped_lock lock(MoleculeinSceneMap_mutex);
276 const MoleculeNodeMap::iterator moliter = MoleculesinSceneMap.find(molid);
277 if (moliter != MoleculesinSceneMap.end()) {
278 const QtObservedMolecule::ptr &checkmol = moliter->second->ObservedMolecule;
279 if (checkmol.get() == _mol) {
280 LOG(3, "INFO: GLWorldScene: Sending signal moleculesAtomRemoved for atom "+toString(_atomid)+".");
281 QMetaObject::invokeMethod(moliter->second, // pointer to a QObject
282 "atomRemoved", // member name (no parameters here)
283 Qt::QueuedConnection, // connection type
284 Q_ARG(const atomId_t, _atomid)); // parameters
285 } else {
286 // relay atomRemoved to GLMoleculeObject_molecule in RemovedMolecules
287 LOG(3, "INFO: GLWorldScene: Sending signal moleculesAtomRemoved for atom "+toString(_atomid)
288 +" to molecule in RemovedMolecules.");
[e29427]289 const RemovedMoleculesMap_t::iterator removedmoliter = RemovedMolecules.find(_mol);
[805fdc]290 ASSERT( removedmoliter != RemovedMolecules.end(),
291 "GLWorldScene::moleculesAtomRemoved() - signal from old molecule "
292 +toString(molid)+", but not present in RemovedMolecules");
293#ifndef NDEBUG
294 const QtObservedMolecule::ptr &othercheckmol = removedmoliter->second->ObservedMolecule;
295#endif
296 ASSERT( othercheckmol.get() == _mol,
297 "GLWorldScene::moleculesAtomRemoved() - signal from old molecule "
298 +toString(molid)+", but different one "+toString(othercheckmol)
299 +" present in RemovedMolecules.");
300 QMetaObject::invokeMethod(removedmoliter->second, // pointer to a QObject
301 "atomRemoved", // member name (no parameters here)
302 Qt::QueuedConnection, // connection type
303 Q_ARG(const atomId_t, _atomid)); // parameters
304 }
305 } else {
[e29427]306 const RemovedMoleculesMap_t::iterator removedmoliter = RemovedMolecules.find(_mol);
[805fdc]307 if (removedmoliter != RemovedMolecules.end()) {
308 ASSERT( removedmoliter != RemovedMolecules.end(),
309 "GLWorldScene::moleculesAtomRemoved() - signal from old molecule "
310 +toString(molid)+", but not present in RemovedMolecules");
311#ifndef NDEBUG
312 const QtObservedMolecule::ptr &othercheckmol = removedmoliter->second->ObservedMolecule;
313#endif
314 ASSERT( othercheckmol.get() == _mol,
315 "GLWorldScene::moleculesAtomRemoved() - signal from old molecule "
316 +toString(molid)+", but different one "+toString(othercheckmol)
317 +" present in RemovedMolecules.");
318 QMetaObject::invokeMethod(removedmoliter->second, // pointer to a QObject
319 "atomRemoved", // member name (no parameters here)
320 Qt::QueuedConnection, // connection type
321 Q_ARG(const atomId_t, _atomid)); // parameters
[8281cc]322 } else {
[59f1bc]323 boost::recursive_mutex::scoped_lock lock(MoleculeMissedStateMap_mutex);
324 // only record missed state for molecule if (still) present but not instantiated
325 if (QtObservedMoleculeMap.count(molid)) {
326 // store signal for when it is instantiated
327 if (MoleculeMissedStateMap.count(molid) == 0)
328 MoleculeMissedStateMap.insert( std::make_pair(molid, StateChangeMap_t()) );
329 MoleculeMissedStateMap[molid].insert( std::make_pair(_atomid, atomRemovedState) );
330 LOG(3, "INFO: GLWorldScene: Placing atomRemoved for atom " << _atomid
331 << " and molecule " << molid << " into missed state map.");
332 }
[8281cc]333 }
[9c259e]334 }
335}
336
[8281cc]337void GLWorldScene::moleculeSignOn(QtObservedMolecule::ptr _mol)
338{
339 // sign on to QtObservedMolecule to get atomInserted/..Removed signals from same
340 // source as GLMoleculeObject_molecule would
[96e145]341 connect(_mol.get(), SIGNAL(atomInserted(QtObservedAtom::ptr, QtObservedMolecule *)),
342 this, SLOT(moleculesAtomInserted(QtObservedAtom::ptr, QtObservedMolecule *)) );
343 connect(_mol.get(), SIGNAL(atomRemoved(const atomId_t, QtObservedMolecule *)),
344 this, SLOT(moleculesAtomRemoved(const atomId_t, QtObservedMolecule *)) );
[8281cc]345 const moleculeId_t molid = _mol->getMolIndex();
[59f1bc]346 ASSERT( QtObservedMoleculeMap.find(molid) == QtObservedMoleculeMap.end(),
347 "GLWorldScene::moleculeSignOn() - molecule with id "+toString(molid)
348 +" is already present in QtObservedMoleculeMap.");
349 QtObservedMoleculeMap[molid] = _mol;
350 connect(_mol.get(), SIGNAL(indexChanged(const moleculeId_t,const moleculeId_t)),
351 this, SLOT(moleculeIndexChanged(const moleculeId_t,const moleculeId_t)) );
[8281cc]352
353 LOG(3, "INFO: GLWorldScene: Received signal moleculeSignOn for molecule "+toString(molid)+".");
354
355 emit insertMolecule(_mol);
356}
357
358void GLWorldScene::moleculeSignOff(const moleculeId_t _id)
359{
[59f1bc]360 const QtObservedMoleculeMap_t::iterator eraseiter = QtObservedMoleculeMap.find(_id);
361 ASSERT( eraseiter != QtObservedMoleculeMap.end(),
[8281cc]362 "GLWorldScene::moleculeSignOff() - cannot find id "+toString(_id)+" in map.");
[59f1bc]363 disconnect(eraseiter->second.get(), SIGNAL(indexChanged(const moleculeId_t,const moleculeId_t)),
364 this, SLOT(moleculeIndexChanged(const moleculeId_t,const moleculeId_t)) );
[e29427]365 QtObservedMolecule * mol = eraseiter->second.get();
[59f1bc]366 QtObservedMoleculeMap.erase(eraseiter);
[8281cc]367
368 LOG(3, "INFO: GLWorldScene: Received signal moleculeSignOff for molecule "+toString(_id)+".");
369
[e29427]370 emit removeMolecule(mol);
[8281cc]371}
372
[026bef]373/** Inserts a molecule into the scene.
[c67518]374 *
[8c001a]375 * @param _mol molecule to insert
[c67518]376 */
[1b07b1]377void GLWorldScene::moleculeInserted(QtObservedMolecule::ptr _mol)
[c67518]378{
[1b07b1]379 ASSERT (_mol,
380 "GLWorldScene::moleculeInserted() - received shared_ptr for molecule is empty?");
381 const moleculeId_t molid = _mol->getMolIndex();
382 LOG(3, "INFO: GLWorldScene: Received signal moleculeInserted for molecule "+toString(molid)+".");
[7cf0eb]383
384 boost::recursive_mutex::scoped_lock lock(MoleculeinSceneMap_mutex);
385
[1b07b1]386 MoleculeNodeMap::const_iterator iter = MoleculesinSceneMap.find(molid);
[8c001a]387 ASSERT( iter == MoleculesinSceneMap.end(),
[1b07b1]388 "GLWorldScene::moleculeInserted() - molecule's id "+toString(molid)+" already present.");
[8c001a]389
390 // add new object
[1b07b1]391 LOG(1, "DEBUG: Adding GLMoleculeObject_molecule to id " << molid);
392 GLMoleculeObject_molecule *molObject =
393 new GLMoleculeObject_molecule(
394 GLMoleculeObject::meshEmpty,
395 this,
396 *board,
397 _mol);
398 ASSERT( molObject != NULL,
399 "GLWorldScene::moleculeInserted - could not create molecule object for "+toString(molid));
400#ifndef NDEBUG
401 std::pair<MoleculeNodeMap::iterator, bool> inserter =
402#endif
403 MoleculesinSceneMap.insert( make_pair(molid, molObject) );
404 ASSERT(inserter.second,
405 "GLWorldScene::moleculeInserted() - molecule "+toString(molid)+" already present in scene.");
406
407 // now handle all state changes that came up before the instantiation
408 if (MoleculeMissedStateMap.count(molid) != 0) {
409// ASSERT( !MoleculeMissedStateMap[molid].empty(),
[4cac6c]410// "GLWorldScene::moleculeInserted() - we have an empty state change map for molecule with id "
[1b07b1]411// +toString(molid));
412 boost::recursive_mutex::scoped_lock lock(MoleculeMissedStateMap_mutex);
413 for (StateChangeMap_t::iterator iter = MoleculeMissedStateMap[molid].begin();
414 !MoleculeMissedStateMap[molid].empty();
415 iter = MoleculeMissedStateMap[molid].begin()) {
416 std::pair<StateChangeMap_t::iterator, StateChangeMap_t::iterator> rangeiter =
417 MoleculeMissedStateMap[molid].equal_range(iter->first);
418 const size_t StateCounts = std::distance(rangeiter.first, rangeiter.second);
419 if (StateCounts > 1) {
420 // more than one state change, have to combine
421 typedef std::map<StateChangeType, size_t> StateChangeAmounts_t;
422 StateChangeAmounts_t StateChangeAmounts;
423 for (StateChangeMap_t::const_iterator stateiter = rangeiter.first;
424 stateiter != rangeiter.second; ++stateiter)
425 ++StateChangeAmounts[stateiter->second];
426 ASSERT( StateChangeAmounts[atomInsertedState] >= StateChangeAmounts[atomRemovedState],
427 "GLWorldScene::moleculeInserted() - more atomRemoved states "
428 +toString(StateChangeAmounts[atomRemovedState])+" than atomInserted "
429 +toString(StateChangeAmounts[atomInsertedState])+" for atom "+toString(iter->first));
430 if (StateChangeAmounts[atomInsertedState] > StateChangeAmounts[atomRemovedState]) {
431 LOG(1, "INFO: invoking atomInserted for atom " << iter->first);
432 QMetaObject::invokeMethod(molObject, // pointer to a QObject
433 "atomInserted", // member name (no parameters here)
434 Qt::QueuedConnection, // connection type
435 Q_ARG(QtObservedAtom::ptr, QtObservedAtomMap[iter->first])); // parameters
436 } else {
437 LOG(1, "INFO: Atom " << iter->first << " has been inserted and removed already.");
438 }
439 // removed all state changes for this atom
440 MoleculeMissedStateMap[molid].erase(rangeiter.first, rangeiter.second);
441 } else {
442 // can only be an insertion
443 switch (rangeiter.first->second) {
444 case atomRemovedState:
445 ASSERT( 0,
446 "GLWorldScene::moleculeInserted() - atomRemoved state without atomInserted for atom "
447 +toString(iter->first));
448 break;
449 case atomInsertedState:
450 {
[73b13c]451 LOG(1, "INFO: invoking atomInserted for atom " << iter->first);
[9c259e]452 QMetaObject::invokeMethod(molObject, // pointer to a QObject
453 "atomInserted", // member name (no parameters here)
[8d5fbf1]454 Qt::QueuedConnection, // connection type
[1b07b1]455 Q_ARG(QtObservedAtom::ptr, QtObservedAtomMap[iter->first])); // parameters
456 break;
[7ded1c]457 }
[1b07b1]458 default:
459 ASSERT( 0,
460 "GLWorldScene::moleculeInserted() - there are unknown change states.");
461 break;
[9c259e]462 }
[7ded1c]463 // removed state changes for this atom
[1b07b1]464 MoleculeMissedStateMap[molid].erase(iter);
[9c259e]465 }
466 }
467 }
468
[1b07b1]469 connect (molObject, SIGNAL(changed()), this, SIGNAL(changed()));
470 connect (molObject, SIGNAL(changeOccured()), this, SIGNAL(changeOccured()));
471 connect (molObject, SIGNAL(atomClicked(atomId_t)), this, SLOT(atomClicked(atomId_t)));
472 connect (molObject, SIGNAL(moleculeClicked(moleculeId_t)), this, SLOT(moleculeClicked(moleculeId_t)));
[e29427]473 connect (molObject, SIGNAL(moleculeEmptied(QtObservedMolecule::ptr)), this, SLOT(moleculeEmpty(QtObservedMolecule::ptr)));
[1b07b1]474 connect (molObject, SIGNAL(selectionChanged()), this, SIGNAL(changed()));
475 connect (molObject, SIGNAL(selectionChanged()), this, SIGNAL(changed()));
476 connect (molObject, SIGNAL(hoverChanged(const atomId_t)), this, SIGNAL(hoverChanged(const atomId_t)));
477 connect (molObject, SIGNAL(hoverChanged(const moleculeId_t, int)), this, SIGNAL(hoverChanged(const moleculeId_t, int)));
478 connect (molObject, SIGNAL(hoverChanged(const moleculeId_t, int)), this, SIGNAL(hoverChanged(const moleculeId_t, int)));
479
480 emit changed();
481 emit changeOccured();
482
[7ded1c]483 // remove state change map for the molecule
484 {
485 boost::recursive_mutex::scoped_lock lock(MoleculeMissedStateMap_mutex);
[1b07b1]486 MoleculeMissedStateMap.erase(molid);
[7ded1c]487 }
[c67518]488}
489
[7cf0eb]490/** Removes a molecule from the scene.
491 *
[e29427]492 * @param _mol QtObservedMolecule instance of molecule to remove
[7cf0eb]493 */
[e29427]494void GLWorldScene::moleculeRemoved(QtObservedMolecule* _mol)
[7cf0eb]495{
[e29427]496 const moleculeId_t molid = _mol->getMolIndex();
497 LOG(3, "INFO: GLWorldScene: Received signal moleculeRemoved for molecule "+toString(molid)+".");
[7c7c4a]498
[7cf0eb]499 boost::recursive_mutex::scoped_lock lock(MoleculeinSceneMap_mutex);
500
[e29427]501 MoleculeNodeMap::iterator iter = MoleculesinSceneMap.find(molid);
[4a187d]502 ASSERT ( iter != MoleculesinSceneMap.end(),
503 "GLWorldScene::moleculeRemoved() - to be removed molecule "+toString(molid)
504 +" is already gone.");
505 // check if it's already empty
506 const RemovedMoleculesMap_t::iterator emptyiter = EmptyMolecules.find(_mol);
507 if (emptyiter != EmptyMolecules.end()) {
508 LOG(1, "DEBUG: Removing empty GLMoleculeObject_molecule to id " << molid << " from scene.");
509 // it's already empty, remove it
510 ASSERT( emptyiter->second == iter->second,
511 "GLWorldScene::moleculeRemoved() - empty molecule "
512 +toString(emptyiter->second)+" and removed molecule "
513 +toString(iter->second)+" don't match.");
514 LOG(1, "DEBUG: Deleting already empty GLMoleculeObject_molecule to id " << molid);
515 GLMoleculeObject_molecule *molObject = emptyiter->second;
516 EmptyMolecules.erase(emptyiter);
517 molObject->disconnect();
518 delete molObject;
519
520 emit changed();
521 emit changeOccured();
522 } else {
523 // otherwise note it as removal candidate
[e29427]524 RemovedMolecules.insert( std::make_pair(_mol, iter->second) );
[bcf9cd]525 }
[4a187d]526 MoleculesinSceneMap.erase(iter);
[bcf9cd]527
528 // remove any possible state changes left
529 {
530 boost::recursive_mutex::scoped_lock lock(MoleculeMissedStateMap_mutex);
[e29427]531 MoleculeMissedStateMap.erase(molid);
[bcf9cd]532 }
533}
534
[e29427]535void GLWorldScene::moleculeEmpty(QtObservedMolecule::ptr _mol)
[bcf9cd]536{
[e29427]537 const moleculeId_t molid = _mol->getMolIndex();
538 LOG(3, "INFO: GLWorldScene: Received signal moleculeEmpty for molecule "+toString(molid)+".");
[bcf9cd]539
540 boost::recursive_mutex::scoped_lock lock(MoleculeinSceneMap_mutex);
541
[4a187d]542 MoleculeNodeMap::iterator iter = MoleculesinSceneMap.find(molid);
543 if ( iter == MoleculesinSceneMap.end()) {
544 RemovedMoleculesMap_t::iterator removeiter = RemovedMolecules.find(_mol.get());
545 ASSERT ( removeiter != RemovedMolecules.end(),
546 "GLWorldScene::moleculeEmpty() - to be removed molecule "+toString(molid)
547 +" is neither in MoleculesinSceneMap, nor in RemovedMolecules.");
548 // it's noted for removal already, remove it
549 LOG(1, "DEBUG: Deleting empty GLMoleculeObject_molecule to id " << molid);
550 GLMoleculeObject_molecule *molObject = removeiter->second;
551 RemovedMolecules.erase(removeiter);
[7c7c4a]552 molObject->disconnect();
553 delete molObject;
[9c259e]554
[4a187d]555 emit changed();
556 emit changeOccured();
557 } else {
558 // otherwise just note it as empty
559 EmptyMolecules.insert( std::make_pair(_mol.get(), iter->second) );
[73b13c]560 }
[9c259e]561
[4a187d]562// // remove any possible state changes left
563// {
564// boost::recursive_mutex::scoped_lock lock(MoleculeMissedStateMap_mutex);
565// MoleculeMissedStateMap.erase(molid);
566// }
[ce4126]567}
568
[8281cc]569void GLWorldScene::atomIndexChanged(const atomId_t _oldid, const atomId_t _newid)
570{
571 if (QtObservedAtomMap.count(_oldid)) {
572 const QtObservedAtom::ptr atom = QtObservedAtomMap[_oldid];
573 QtObservedAtomMap.erase(_oldid);
574 QtObservedAtomMap.insert( std::make_pair(_newid, atom) );
575 }
576}
577
[15c8a9]578void GLWorldScene::moleculeIndexChanged(const moleculeId_t _oldid, const moleculeId_t _newid)
579{
[59f1bc]580 {
581 QtObservedMoleculeMap_t::iterator const olditer = QtObservedMoleculeMap.find(_oldid);
582 ASSERT ( olditer != QtObservedMoleculeMap.end(),
583 "GLWorldScene::moleculeIndexChanged() - molecule "
584 +toString(_oldid)+" not present in QtObservedMoleculeMap.");
585#ifndef NDEBUG
586 QtObservedMoleculeMap_t::iterator const newiter = QtObservedMoleculeMap.find(_newid);
587 ASSERT ( newiter == QtObservedMoleculeMap.end(),
588 "GLWorldScene::moleculeIndexChanged() - molecule "
589 +toString(_newid)+" already present in QtObservedMoleculeMap.");
590#endif
591 const QtObservedMolecule::ptr mol = olditer->second;
592 QtObservedMoleculeMap.erase(olditer);
593 QtObservedMoleculeMap[_newid] = mol;
594 }
595 {
596 MoleculeNodeMap::iterator const olditer = MoleculesinSceneMap.find(_oldid);
597 if ( olditer != MoleculesinSceneMap.end()) {
598#ifndef NDEBUG
599 MoleculeNodeMap::iterator const newiter = MoleculesinSceneMap.find(_newid);
600 ASSERT ( newiter == MoleculesinSceneMap.end(),
601 "GLWorldScene::moleculeIndexChanged() - molecule "
602 +toString(_newid)+" already present in MoleculesinSceneMap.");
603#endif
[bcf9cd]604 LOG(1, "DEBUG: Changing GLMoleculeObject_molecule in MoleculesinSceneMapfrom "
605 << _oldid << " to id " << _newid);
[59f1bc]606 GLMoleculeObject_molecule* const molObject = olditer->second;
607 MoleculesinSceneMap.erase(olditer);
608 MoleculesinSceneMap[_newid] = molObject;
[8281cc]609 }
[15c8a9]610 }
611}
612
[739ee9]613void GLWorldScene::moleculesVisibilityChanged(const moleculeId_t _id, bool _visible)
614{
[7c7c4a]615 boost::recursive_mutex::scoped_lock lock(MoleculeinSceneMap_mutex);
[739ee9]616 MoleculeNodeMap::iterator iter = MoleculesinSceneMap.find(_id);
617 ASSERT( iter != MoleculesinSceneMap.end(),
618 "GLWorldScene::moleculeInserted() - molecule's id "+toString(_id)+" is unknown.");
619
620 GLMoleculeObject_molecule *molObject = iter->second;
621 molObject->setVisible(_visible);
622
623 emit changed();
624 emit changeOccured();
625}
626
[f75491]627/** Adds a shape to the scene.
[4d6662]628 *
[f75491]629 */
[07136a]630void GLWorldScene::addShape(const std::string &_name)
[f75491]631{
[07136a]632 Shape * const shape = ShapeRegistry::getInstance().getByName(_name);
633 if (shape != NULL) {
634 GLMoleculeObject_shape *shapeObject = new GLMoleculeObject_shape(*shape, this);
635 ShapeNodeMap::iterator iter = ShapesinSceneMap.find(_name);
636 ASSERT(iter == ShapesinSceneMap.end(),
637 "GLWorldScene::addShape() - same shape "+_name+" added again.");
638 ShapesinSceneMap.insert( make_pair(_name, shapeObject) );
639 } else
640 ELOG(2, "GLWorldScene::addShape() - shape disappeared before we could draw it.");
641
642 emit changed();
[284551]643}
644
[07136a]645void GLWorldScene::removeShape(const std::string &_name)
[85c36d]646{
[07136a]647 ShapeNodeMap::iterator iter = ShapesinSceneMap.find(_name);
[ba6b5c]648 ASSERT(iter != ShapesinSceneMap.end(),
[07136a]649 "GLWorldScene::removeShape() - shape "+_name+" not in scene.");
[148dde0]650 ShapesinSceneMap.erase(iter);
[85c36d]651 delete(iter->second);
[07136a]652
653 emit changed();
[85c36d]654}
655
656void GLWorldScene::updateSelectedShapes()
[284551]657{
658 foreach (QObject *obj, children()) {
659 GLMoleculeObject_shape *shapeobj = qobject_cast<GLMoleculeObject_shape *>(obj);
[85c36d]660 if (shapeobj){
661 shapeobj->enable(ShapeRegistry::getInstance().isSelected(shapeobj->getShape()));
662 }
[284551]663 }
[07136a]664
665 emit changed();
[f75491]666}
667
[7188b1]668void GLWorldScene::initialize(QGLView *view, QGLPainter *painter) const
669{
670 // Initialize all of the mesh objects that we have as children.
671 foreach (QObject *obj, children()) {
672 GLMoleculeObject *meshobj = qobject_cast<GLMoleculeObject *>(obj);
673 if (meshobj)
674 meshobj->initialize(view, painter);
675 }
676}
677
[72a4c1]678void GLWorldScene::draw(QGLPainter *painter, const QVector4D &cameraPlane) const
[7188b1]679{
680 // Draw all of the mesh objects that we have as children.
681 foreach (QObject *obj, children()) {
682 GLMoleculeObject *meshobj = qobject_cast<GLMoleculeObject *>(obj);
683 if (meshobj)
[72a4c1]684 meshobj->draw(painter, cameraPlane);
[7188b1]685 }
[907636]686}
[06ebf5]687
[6966b7]688void GLWorldScene::setSelectionMode(SelectionModeType mode)
689{
690 selectionMode = mode;
691 // TODO send update to toolbar
692}
693
694void GLWorldScene::setSelectionModeAtom()
695{
696 setSelectionMode(SelectAtom);
697}
698
699void GLWorldScene::setSelectionModeMolecule()
700{
701 setSelectionMode(SelectMolecule);
702}
[20f9b5]703
Note: See TracBrowser for help on using the repository browser.