source: src/UIElements/Views/Qt4/Qt3D/GLWorldScene.cpp@ 96f14a

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

Modified all GLMoleculeObjects and GLWorldScene to the new QtObservedBond.

  • GLWorldScene transmits bondInserted/bondRemoved similarly to those for the atoms to the molecule if aLready present and otherwise into the MissedStateMap.
  • Property mode set to 100644
File size: 39.7 KB
Line 
1/*
2 * Project: MoleCuilder
3 * Description: creates and alters molecular systems
4 * Copyright (C) 2010-2012 University of Bonn. All rights reserved.
5 * Copyright (C) 2013 Frederik Heber. All rights reserved.
6 *
7 *
8 * This file is part of MoleCuilder.
9 *
10 * MoleCuilder is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation, either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * MoleCuilder is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with MoleCuilder. If not, see <http://www.gnu.org/licenses/>.
22 */
23
24/*
25 * 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"
39#include <Qt3D/qglview.h>
40#include <Qt3D/qglbuilder.h>
41#include <Qt3D/qglscenenode.h>
42#include <Qt3D/qglsphere.h>
43#include <Qt3D/qglcylinder.h>
44
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
51#include "UIElements/Qt4/InstanceBoard/QtObservedInstanceBoard.hpp"
52
53#include "CodePatterns/MemDebug.hpp"
54
55#include "CodePatterns/Log.hpp"
56
57#include "Actions/SelectionAction/Atoms/AtomByIdAction.hpp"
58#include "Actions/SelectionAction/Atoms/NotAtomByIdAction.hpp"
59#include "Actions/SelectionAction/Molecules/MoleculeByIdAction.hpp"
60#include "Actions/SelectionAction/Molecules/NotMoleculeByIdAction.hpp"
61#include "Atom/atom.hpp"
62#include "Bond/bond.hpp"
63#include "Descriptors/AtomIdDescriptor.hpp"
64#include "Descriptors/MoleculeIdDescriptor.hpp"
65#include "Helpers/helpers.hpp"
66#include "Shapes/ShapeRegistry.hpp"
67#include "molecule.hpp"
68#include "World.hpp"
69
70#include <iostream>
71
72using namespace MoleCuilder;
73
74GLWorldScene::GLWorldScene(
75 QtObservedInstanceBoard * _board,
76 QObject *parent) :
77 QObject(parent),
78 selectionMode(SelectAtom),
79 board(_board)
80{
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;
85 GLMoleculeObject::meshEmpty[i] = emptyBuilder.finalizedSceneNode();
86 QGLBuilder sphereBuilder;
87 sphereBuilder << QGLSphere(2.0, sphereDetails[i]);
88 GLMoleculeObject::meshSphere[i] = sphereBuilder.finalizedSceneNode();
89 GLMoleculeObject::meshSphere[i]->setOption(QGLSceneNode::CullBoundingBox, true);
90 QGLBuilder cylinderBuilder;
91 cylinderBuilder << QGLCylinder(.25,.25,1.0,cylinderDetails[i]);
92 GLMoleculeObject::meshCylinder[i] = cylinderBuilder.finalizedSceneNode();
93 GLMoleculeObject::meshCylinder[i]->setOption(QGLSceneNode::CullBoundingBox, true);
94 }
95 connect(board, SIGNAL(moleculeInserted(QtObservedMolecule::ptr)),
96 this, SLOT(moleculeSignOn(QtObservedMolecule::ptr)), Qt::DirectConnection);
97 connect(board, SIGNAL(moleculeRemoved(ObservedValue_Index_t)),
98 this, SLOT(moleculeSignOff(ObservedValue_Index_t)), Qt::DirectConnection);
99 connect(board, SIGNAL(atomInserted(QtObservedAtom::ptr)),
100 this, SLOT(atomInserted(QtObservedAtom::ptr)));
101 connect(board, SIGNAL(atomRemoved(ObservedValue_Index_t)),
102 this, SLOT(atomRemoved(ObservedValue_Index_t)));
103 connect(board, SIGNAL(bondInserted(QtObservedBond::ptr)),
104 this, SLOT(bondInserted(QtObservedBond::ptr)));
105 connect(board, SIGNAL(bondRemoved(ObservedValue_Index_t)),
106 this, SLOT(bondRemoved(ObservedValue_Index_t)));
107 connect(this, SIGNAL(insertMolecule(QtObservedMolecule::ptr)),
108 this, SLOT(moleculeInserted(QtObservedMolecule::ptr)) );
109 connect(this, SIGNAL(removeMolecule(QtObservedMolecule*)),
110 this, SLOT(moleculeRemoved(QtObservedMolecule*)) );
111
112// connect(this, SIGNAL(updated()), this, SLOT(update()));
113}
114
115GLWorldScene::~GLWorldScene()
116{
117 // remove all elements
118 GLMoleculeObject::cleanMaterialMap();
119}
120
121void GLWorldScene::atomClicked(atomId_t no)
122{
123 LOG(3, "INFO: GLMoleculeObject_molecule - atom " << no << " has been clicked.");
124 const atom * const Walker = const_cast<const World &>(World::getInstance()).
125 getAtom(AtomById(no));
126 ASSERT( Walker != NULL,
127 "GLWorldScene::atomClicked() - clicked atom has disappeared.");
128 if (selectionMode == SelectAtom){
129 if (!World::getInstance().isSelected(Walker))
130 SelectionAtomById(std::vector<atomId_t>(1,no));
131 else
132 SelectionNotAtomById(std::vector<atomId_t>(1,no));
133 }else if (selectionMode == SelectMolecule){
134 const molecule *mol = Walker->getMolecule();
135 ASSERT(mol, "Atom without molecule has been clicked.");
136 molids_t ids(1, mol->getId());
137 if (!World::getInstance().isSelected(mol))
138 SelectionMoleculeById(ids);
139 else
140 SelectionNotMoleculeById(ids);
141 }
142 emit clicked(no);
143}
144
145void GLWorldScene::moleculeClicked(moleculeId_t no)
146{
147 LOG(3, "INFO: GLMoleculeObject_molecule - mol " << no << " has been clicked.");
148 const molecule * const mol= const_cast<const World &>(World::getInstance()).
149 getMolecule(MoleculeById(no));
150 ASSERT(mol, "Atom without molecule has been clicked.");
151 molids_t ids(1, mol->getId());
152 if (!World::getInstance().isSelected(mol))
153 SelectionMoleculeById(ids);
154 else
155 SelectionNotMoleculeById(ids);
156 emit clicked(no);
157}
158
159/** Prepares insertion of a general atom.
160 *
161 * This is called before the insertion into a molecule and thus before the
162 * insertion into the scene.
163 *
164 * @param _atom atom to insert
165 */
166void GLWorldScene::atomInserted(QtObservedAtom::ptr _atom)
167{
168 const ObservedValue_Index_t atomid = _atom->getIndex();
169 ASSERT( QtObservedAtomMap.find(atomid) == QtObservedAtomMap.end(),
170 "GLWorldScene::AtomInserted() - atom with id "+toString(_atom->getAtomIndex())
171 +" is already present in QtObservedAtomMap.");
172 QtObservedAtomMap[atomid] = _atom;
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(ObservedValue_Index_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 QtObservedAtomMap.erase(eraseiter);
188}
189
190/** Prepares insertion of a bond.
191 *
192 * This is called before the insertion into a molecule and thus before the
193 * insertion into the scene.
194 *
195 * @param _bond bond to insert
196 */
197void GLWorldScene::bondInserted(QtObservedBond::ptr _bond)
198{
199 const ObservedValue_Index_t bondid = _bond->getIndex();
200 ASSERT( QtObservedBondMap.find(bondid) == QtObservedBondMap.end(),
201 "GLWorldScene::BondInserted() - bond with id "+toString(_bond->getBondIndex())
202 +" is already present in QtObservedBondMap.");
203 QtObservedBondMap[bondid] = _bond;
204
205 // assign to its molecule if present
206 const moleculeId_t molid = _bond->getMoleculeIndex();
207 if (molid != (moleculeId_t)-1) {
208 QtObservedMolecule::ptr mol = board->getObservedMolecule(molid);
209 emit moleculesBondInserted(_bond, mol.get());
210 } else {
211 // watch bond till it has a molecule
212 connect( _bond.get(), SIGNAL(moleculeIndexChanged(moleculeId_t,moleculeId_t)),
213 this, SLOT(bondsMoleculeChanged(moleculeId_t, moleculeId_t)));
214 }
215}
216
217/** Handle change of molecule from initial none.
218 *
219 */
220void GLWorldScene::bondsMoleculeChanged(moleculeId_t _oldid, moleculeId_t _newid)
221{
222 ASSERT( _oldid == (moleculeId_t)-1,
223 "GLWorldScene::bondsMoleculeChanged() - got true index change from "
224 +toString(_oldid)+" to "+toString(_newid)+" and not just added to mol.");
225 QtObservedBond* bondref = static_cast<QtObservedBond*>(sender());
226
227 // disconnect from further molecule changes
228 disconnect( bondref, SIGNAL(moleculeIndexChanged(moleculeId_t,moleculeId_t)),
229 this, SLOT(bondsMoleculeChanged(moleculeId_t, moleculeId_t)));
230
231 // add it to its molecule
232 QtObservedMolecule::ptr mol = board->getObservedMolecule(_newid);
233 emit moleculesBondInserted(bondref->getRef(), mol.get());
234}
235
236
237/** Removes an general bond.
238 *
239 * This is called when the bond has been removed from the molecule.
240 *
241 * @param _bond bond to remove
242 */
243void GLWorldScene::bondRemoved(ObservedValue_Index_t _bondid)
244{
245 const QtObservedBondMap_t::iterator eraseiter = QtObservedBondMap.find(_bondid);
246 ASSERT( eraseiter != QtObservedBondMap.end(),
247 "GLWorldScene::BondRemoved() - bond with id "+toString(_bondid)
248 +" is not present in QtObservedBondMap.");
249 QtObservedBond::ptr bondref = eraseiter->second;
250
251 // tell its assigned molecule if present
252 const moleculeId_t molid = bondref->getMoleculeIndex();
253 if (molid != (moleculeId_t)-1) {
254 QtObservedMolecule::ptr mol = board->getObservedMolecule(molid);
255 emit moleculesBondRemoved(_bondid, mol.get());
256 } else {
257 // it might have still been waiting for a molecule assignment
258 disconnect( bondref.get(), SIGNAL(moleculeIndexChanged(moleculeId_t,moleculeId_t)),
259 this, SLOT(bondsMoleculeChanged(moleculeId_t, moleculeId_t)));
260 }
261 QtObservedBondMap.erase(eraseiter);
262}
263
264/** Inserts an atom into the scene when molecule is present.
265 *
266 * @param _atom atom to insert
267 */
268void GLWorldScene::moleculesAtomInserted(QtObservedAtom::ptr _atom, QtObservedMolecule * _mol)
269{
270 const ObservedValue_Index_t atomid = _atom->getIndex();
271 // this ASSERT cannot work: The same molecule may get reassigned to a different
272 // molecule, when e.g. the time step changes (which triggers an update-molecules).
273 // The GUI is slow and may lack behind and thus get to execute the event
274 // moleculeAtomInserted when the change has been already been done.
275// ASSERT( (atommol == NULL) || (_mol == atommol),
276// "GLWorldScene::moleculesAtomInserted() - atom "+toString(atomid)
277// +" claims to belong to QtObservedMolecule "+toString(atommol)
278// +" but signal came from QtObservedMolecule "+toString(_mol)+".");
279 LOG(3, "INFO: GLWorldScene: Received signal atomInserted for atom " << _atom->getAtomIndex());
280 const ObservedValue_Index_t molid = _mol->getIndex();
281
282 // check of molecule is already present
283 boost::recursive_mutex::scoped_lock lock(MoleculeinSceneMap_mutex);
284 const MoleculeNodeMap::iterator moliter = MoleculesinSceneMap.find(molid);
285 if (moliter != MoleculesinSceneMap.end()) {
286 const RemovedMoleculesMap_t::iterator emptyiter = EmptyMolecules.find(_mol);
287 if (emptyiter != EmptyMolecules.end())
288 EmptyMolecules.erase(emptyiter);
289 // check that it is the right molecule
290 QtObservedMolecule::ptr &checkmol = moliter->second->ObservedMolecule;
291 if (checkmol.get() == _mol) {
292 LOG(3, "INFO: GLWorldScene: Sending signal moleculesAtomInserted for atom "
293 << _atom->getAtomIndex());
294 QMetaObject::invokeMethod(moliter->second, // pointer to a QObject
295 "atomInserted", // member name (no parameters here)
296 Qt::QueuedConnection, // connection type
297 Q_ARG(QtObservedAtom::ptr, _atom)); // parameters
298 } else {
299 // relay atomRemoved to GLMoleculeObject_molecule in RemovedMolecules
300// LOG(3, "INFO: GLWorldScene: Sending signal moleculesAtomInserted for atom "+toString(_atomid)
301// +" to molecule in RemovedMolecules.");
302// const RemovedMoleculesMap_t::iterator removedmoliter = RemovedMolecules.find(molid);
303// ASSERT( removedmoliter != RemovedMolecules.end(),
304// "GLWorldScene::moleculesAtomInserted() - signal from old mol "
305// +toString(molid)+", but not present in RemovedMolecules");
306// QMetaObject::invokeMethod(removedmoliter->second, // pointer to a QObject
307// "atomInserted ", // member name (no parameters here)
308// Qt::QueuedConnection, // connection type
309// Q_ARG(QtObservedAtom::ptr, _atom)); // parameters
310 ASSERT( 0,
311 "GLWorldScene::moleculesAtomInserted() - would need to send atomInserted to already removed molecule.");
312 }
313 } else {
314 const RemovedMoleculesMap_t::iterator removedmoliter = RemovedMolecules.find(_mol);
315 if (removedmoliter != RemovedMolecules.end()) {
316 ASSERT( 0,
317 "GLWorldScene::moleculesAtomInserted() - would need to send atomInserted to already removed molecule.");
318 } else {
319 boost::recursive_mutex::scoped_lock lock(MoleculeMissedStateMap_mutex);
320 // only record missed state for molecule if (still) present but not instantiated
321 if (QtObservedMoleculeMap.count(molid)) {
322 // store signal for when it is instantiated
323 if (MoleculeMissedStateMap.count(molid) == 0)
324 MoleculeMissedStateMap.insert( std::make_pair(molid ,StateChangeMap_t()) );
325 MoleculeMissedStateMap[molid].insert( std::make_pair(atomid, atomInsertedState) );
326 ASSERT( QtObservedAtomMap[atomid] == _atom,
327 "GLWorldScene::moleculesAtomInserted() - atom "+toString(atomid)
328 +" inserted in molecule "+toString(_mol->getMolIndex())
329 +" which does not match atom in QtObservedAtomMap.");
330 LOG(3, "INFO: GLWorldScene: Placing atomInserted for atom " << _atom->getAtomIndex()
331 << " and molecule " << _mol->getMolIndex() << " into missed state map.");
332 }
333 }
334 }
335}
336
337/** Removes an atom into the scene before molecule is present.
338 *
339 * @param _atomid atom to remove
340 */
341void GLWorldScene::moleculesAtomRemoved(ObservedValue_Index_t _atomid, QtObservedMolecule * _mol)
342{
343 LOG(3, "INFO: GLWorldScene: Received signal atomRemoved for atom "+toString(_atomid)+".");
344
345 const ObservedValue_Index_t molid = _mol->getIndex();
346 // check of molecule is already present
347 boost::recursive_mutex::scoped_lock lock(MoleculeinSceneMap_mutex);
348 const MoleculeNodeMap::iterator moliter = MoleculesinSceneMap.find(molid);
349 if (moliter != MoleculesinSceneMap.end()) {
350 const QtObservedMolecule::ptr &checkmol = moliter->second->ObservedMolecule;
351 if (checkmol.get() == _mol) {
352 LOG(3, "INFO: GLWorldScene: Sending signal moleculesAtomRemoved for atom "+toString(_atomid)+".");
353 QMetaObject::invokeMethod(moliter->second, // pointer to a QObject
354 "atomRemoved", // member name (no parameters here)
355 Qt::QueuedConnection, // connection type
356 Q_ARG(ObservedValue_Index_t, _atomid)); // parameters
357 } else {
358 // relay atomRemoved to GLMoleculeObject_molecule in RemovedMolecules
359 LOG(3, "INFO: GLWorldScene: Sending signal moleculesAtomRemoved for atom "+toString(_atomid)
360 +" to molecule in RemovedMolecules.");
361 const RemovedMoleculesMap_t::iterator removedmoliter = RemovedMolecules.find(_mol);
362 ASSERT( removedmoliter != RemovedMolecules.end(),
363 "GLWorldScene::moleculesAtomRemoved() - signal from old molecule "
364 +toString(molid)+", but not present in RemovedMolecules");
365#ifndef NDEBUG
366 const QtObservedMolecule::ptr &othercheckmol = removedmoliter->second->ObservedMolecule;
367#endif
368 ASSERT( othercheckmol.get() == _mol,
369 "GLWorldScene::moleculesAtomRemoved() - signal from old molecule "
370 +toString(molid)+", but different one "+toString(othercheckmol)
371 +" present in RemovedMolecules.");
372 QMetaObject::invokeMethod(removedmoliter->second, // pointer to a QObject
373 "atomRemoved", // member name (no parameters here)
374 Qt::QueuedConnection, // connection type
375 Q_ARG(ObservedValue_Index_t, _atomid)); // parameters
376 }
377 } else {
378 const RemovedMoleculesMap_t::iterator removedmoliter = RemovedMolecules.find(_mol);
379 if (removedmoliter != RemovedMolecules.end()) {
380 ASSERT( removedmoliter != RemovedMolecules.end(),
381 "GLWorldScene::moleculesAtomRemoved() - signal from old molecule "
382 +toString(molid)+", but not present in RemovedMolecules");
383#ifndef NDEBUG
384 const QtObservedMolecule::ptr &othercheckmol = removedmoliter->second->ObservedMolecule;
385#endif
386 ASSERT( othercheckmol.get() == _mol,
387 "GLWorldScene::moleculesAtomRemoved() - signal from old molecule "
388 +toString(molid)+", but different one "+toString(othercheckmol)
389 +" present in RemovedMolecules.");
390 QMetaObject::invokeMethod(removedmoliter->second, // pointer to a QObject
391 "atomRemoved", // member name (no parameters here)
392 Qt::QueuedConnection, // connection type
393 Q_ARG(ObservedValue_Index_t, _atomid)); // parameters
394 } else {
395 boost::recursive_mutex::scoped_lock lock(MoleculeMissedStateMap_mutex);
396 // only record missed state for molecule if (still) present but not instantiated
397 if (QtObservedMoleculeMap.count(molid)) {
398 // store signal for when it is instantiated
399 if (MoleculeMissedStateMap.count(molid) == 0)
400 MoleculeMissedStateMap.insert( std::make_pair(molid, StateChangeMap_t()) );
401 MoleculeMissedStateMap[molid].insert( std::make_pair(_atomid, atomRemovedState) );
402 LOG(3, "INFO: GLWorldScene: Placing atomRemoved for atom " << _atomid
403 << " and molecule " << molid << " into missed state map.");
404 }
405 }
406 }
407}
408
409/** Inserts an bond into the scene when molecule is present.
410 *
411 * @param _bond bond to insert
412 */
413void GLWorldScene::moleculesBondInserted(QtObservedBond::ptr _bond, QtObservedMolecule * _mol)
414{
415 const ObservedValue_Index_t bondid = _bond->getIndex();
416 LOG(3, "INFO: GLWorldScene: Received signal bondInserted for bond " << _bond->getBondIndex());
417 const ObservedValue_Index_t molid = _mol->getIndex();
418
419 // check of molecule is already present
420 boost::recursive_mutex::scoped_lock lock(MoleculeinSceneMap_mutex);
421 const MoleculeNodeMap::iterator moliter = MoleculesinSceneMap.find(molid);
422 if (moliter != MoleculesinSceneMap.end()) {
423 // check that it is the right molecule
424 QtObservedMolecule::ptr &checkmol = moliter->second->ObservedMolecule;
425 ASSERT( checkmol.get() == _mol,
426 "GLWorldScene::moleculesBondInserted() - claimed and present molecule differ.");
427 LOG(3, "INFO: GLWorldScene: Sending signal moleculesBondInserted for bond "
428 << _bond->getBondIndex());
429 QMetaObject::invokeMethod(moliter->second, // pointer to a QObject
430 "bondInserted", // member name (no parameters here)
431 Qt::QueuedConnection, // connection type
432 Q_ARG(QtObservedBond::ptr, _bond)); // parameters
433 } else {
434#ifndef NDEBUG
435 const RemovedMoleculesMap_t::iterator removedmoliter = RemovedMolecules.find(_mol);
436 ASSERT( removedmoliter != RemovedMolecules.end(),
437 "GLWorldScene::moleculesBondInserted() - would need to send bondInserted to already removed molecule.");
438#endif
439 boost::recursive_mutex::scoped_lock lock(MoleculeMissedStateMap_mutex);
440 // only record missed state for molecule if (still) present but not instantiated
441 if (QtObservedMoleculeMap.count(molid)) {
442 // store signal for when it is instantiated
443 if (MoleculeMissedStateMap.count(molid) == 0)
444 MoleculeMissedStateMap.insert( std::make_pair(molid ,StateChangeMap_t()) );
445 MoleculeMissedStateMap[molid].insert( std::make_pair(bondid, bondInsertedState) );
446 ASSERT( QtObservedBondMap[bondid] == _bond,
447 "GLWorldScene::moleculesBondInserted() - bond "+toString(bondid)
448 +" inserted in molecule "+toString(_mol->getMolIndex())
449 +" which does not match bond in QtObservedBondMap.");
450 LOG(3, "INFO: GLWorldScene: Placing bondInserted for bond " << _bond->getBondIndex()
451 << " and molecule " << _mol->getMolIndex() << " into missed state map.");
452 }
453 }
454}
455
456/** Removes an bond into the scene before molecule is present.
457 *
458 * @param _bondid bond to remove
459 */
460void GLWorldScene::moleculesBondRemoved(ObservedValue_Index_t _bondid, QtObservedMolecule * _mol)
461{
462 LOG(3, "INFO: GLWorldScene: Received signal bondRemoved for bond "+toString(_bondid)+".");
463
464 const ObservedValue_Index_t molid = _mol->getIndex();
465 // check of molecule is already present
466 boost::recursive_mutex::scoped_lock lock(MoleculeinSceneMap_mutex);
467 const MoleculeNodeMap::iterator moliter = MoleculesinSceneMap.find(molid);
468 if (moliter != MoleculesinSceneMap.end()) {
469 const QtObservedMolecule::ptr &checkmol = moliter->second->ObservedMolecule;
470 if (checkmol.get() == _mol) {
471 LOG(3, "INFO: GLWorldScene: Sending signal moleculesBondRemoved for bond "+toString(_bondid)+".");
472 QMetaObject::invokeMethod(moliter->second, // pointer to a QObject
473 "bondRemoved", // member name (no parameters here)
474 Qt::QueuedConnection, // connection type
475 Q_ARG(ObservedValue_Index_t, _bondid)); // parameters
476 } else {
477 // relay bondRemoved to GLMoleculeObject_molecule in RemovedMolecules
478 LOG(3, "INFO: GLWorldScene: Sending signal moleculesBondRemoved for bond "+toString(_bondid)
479 +" to molecule in RemovedMolecules.");
480 const RemovedMoleculesMap_t::iterator removedmoliter = RemovedMolecules.find(_mol);
481 ASSERT( removedmoliter != RemovedMolecules.end(),
482 "GLWorldScene::moleculesBondRemoved() - signal from old molecule "
483 +toString(molid)+", but not present in RemovedMolecules");
484#ifndef NDEBUG
485 const QtObservedMolecule::ptr &othercheckmol = removedmoliter->second->ObservedMolecule;
486 ASSERT( othercheckmol.get() == _mol,
487 "GLWorldScene::moleculesBondRemoved() - signal from old molecule "
488 +toString(molid)+", but different one "+toString(othercheckmol)
489 +" present in RemovedMolecules.");
490#endif
491 QMetaObject::invokeMethod(removedmoliter->second, // pointer to a QObject
492 "bondRemoved", // member name (no parameters here)
493 Qt::QueuedConnection, // connection type
494 Q_ARG(ObservedValue_Index_t, _bondid)); // parameters
495 }
496 } else {
497 const RemovedMoleculesMap_t::iterator removedmoliter = RemovedMolecules.find(_mol);
498 if (removedmoliter != RemovedMolecules.end()) {
499 ASSERT( removedmoliter != RemovedMolecules.end(),
500 "GLWorldScene::moleculesBondRemoved() - signal from old molecule "
501 +toString(molid)+", but not present in RemovedMolecules");
502#ifndef NDEBUG
503 const QtObservedMolecule::ptr &othercheckmol = removedmoliter->second->ObservedMolecule;
504 ASSERT( othercheckmol.get() == _mol,
505 "GLWorldScene::moleculesBondRemoved() - signal from old molecule "
506 +toString(molid)+", but different one "+toString(othercheckmol)
507 +" present in RemovedMolecules.");
508#endif
509 QMetaObject::invokeMethod(removedmoliter->second, // pointer to a QObject
510 "bondRemoved", // member name (no parameters here)
511 Qt::QueuedConnection, // connection type
512 Q_ARG(ObservedValue_Index_t, _bondid)); // parameters
513 } else {
514 boost::recursive_mutex::scoped_lock lock(MoleculeMissedStateMap_mutex);
515 // only record missed state for molecule if (still) present but not instantiated
516 if (QtObservedMoleculeMap.count(molid)) {
517 // store signal for when it is instantiated
518 if (MoleculeMissedStateMap.count(molid) == 0)
519 MoleculeMissedStateMap.insert( std::make_pair(molid, StateChangeMap_t()) );
520 MoleculeMissedStateMap[molid].insert( std::make_pair(_bondid, bondRemovedState) );
521 LOG(3, "INFO: GLWorldScene: Placing bondRemoved for bond " << _bondid
522 << " and molecule " << molid << " into missed state map.");
523 }
524 }
525 }
526}
527
528void GLWorldScene::moleculeSignOn(QtObservedMolecule::ptr _mol)
529{
530 // sign on to QtObservedMolecule to get atomInserted/..Removed signals from same
531 // source as GLMoleculeObject_molecule would
532 connect(_mol.get(), SIGNAL(atomInserted(QtObservedAtom::ptr, QtObservedMolecule *)),
533 this, SLOT(moleculesAtomInserted(QtObservedAtom::ptr, QtObservedMolecule *)) );
534 connect(_mol.get(), SIGNAL(atomRemoved(ObservedValue_Index_t, QtObservedMolecule *)),
535 this, SLOT(moleculesAtomRemoved(ObservedValue_Index_t, QtObservedMolecule *)) );
536 const ObservedValue_Index_t molid = _mol->getIndex();
537 ASSERT( QtObservedMoleculeMap.find(molid) == QtObservedMoleculeMap.end(),
538 "GLWorldScene::moleculeSignOn() - molecule with id "+toString(_mol->getMolIndex())
539 +" is already present in QtObservedMoleculeMap.");
540 QtObservedMoleculeMap[molid] = _mol;
541
542 LOG(3, "INFO: GLWorldScene: Received signal moleculeSignOn for molecule " << _mol->getMolIndex());
543
544 emit insertMolecule(_mol);
545}
546
547void GLWorldScene::moleculeSignOff(ObservedValue_Index_t _id)
548{
549 const QtObservedMoleculeMap_t::iterator eraseiter = QtObservedMoleculeMap.find(_id);
550 ASSERT( eraseiter != QtObservedMoleculeMap.end(),
551 "GLWorldScene::moleculeSignOff() - cannot find id "+toString(_id)+" in map.");
552 QtObservedMolecule * mol = eraseiter->second.get();
553 QtObservedMoleculeMap.erase(eraseiter);
554
555 LOG(3, "INFO: GLWorldScene: Received signal moleculeSignOff for molecule " << mol->getMolIndex());
556
557 emit removeMolecule(mol);
558}
559
560/** Inserts a molecule into the scene.
561 *
562 * @param _mol molecule to insert
563 */
564void GLWorldScene::moleculeInserted(QtObservedMolecule::ptr _mol)
565{
566 ASSERT (_mol,
567 "GLWorldScene::moleculeInserted() - received shared_ptr for molecule is empty?");
568 const ObservedValue_Index_t molid = _mol->getIndex();
569 LOG(3, "INFO: GLWorldScene: Received signal moleculeInserted for molecule "
570 << _mol->getMolIndex());
571
572 boost::recursive_mutex::scoped_lock lock(MoleculeinSceneMap_mutex);
573
574 MoleculeNodeMap::const_iterator iter = MoleculesinSceneMap.find(molid);
575 ASSERT( iter == MoleculesinSceneMap.end(),
576 "GLWorldScene::moleculeInserted() - molecule's id "+toString(_mol->getMolIndex())
577 +" already present.");
578
579 // add new object
580 LOG(1, "DEBUG: Adding GLMoleculeObject_molecule to id " << _mol->getMolIndex());
581 GLMoleculeObject_molecule *molObject =
582 new GLMoleculeObject_molecule(
583 GLMoleculeObject::meshEmpty,
584 this,
585 *board,
586 _mol);
587 ASSERT( molObject != NULL,
588 "GLWorldScene::moleculeInserted - could not create molecule object for "
589 +toString(_mol->getMolIndex()));
590#ifndef NDEBUG
591 std::pair<MoleculeNodeMap::iterator, bool> inserter =
592#endif
593 MoleculesinSceneMap.insert( make_pair(molid, molObject) );
594 ASSERT(inserter.second,
595 "GLWorldScene::moleculeInserted() - molecule "+toString(_mol->getMolIndex())
596 +" already present in scene.");
597
598 // now handle all state changes that came up before the instantiation
599 if (MoleculeMissedStateMap.count(molid) != 0) {
600// ASSERT( !MoleculeMissedStateMap[molid].empty(),
601// "GLWorldScene::moleculeInserted() - we have an empty state change map for molecule with id "
602// +toString(molid));
603 boost::recursive_mutex::scoped_lock lock(MoleculeMissedStateMap_mutex);
604 for (StateChangeMap_t::iterator iter = MoleculeMissedStateMap[molid].begin();
605 !MoleculeMissedStateMap[molid].empty();
606 iter = MoleculeMissedStateMap[molid].begin()) {
607 std::pair<StateChangeMap_t::iterator, StateChangeMap_t::iterator> rangeiter =
608 MoleculeMissedStateMap[molid].equal_range(iter->first);
609 const size_t StateCounts = std::distance(rangeiter.first, rangeiter.second);
610 if (StateCounts > 1) {
611 // more than one state change, have to combine
612 typedef std::map<StateChangeType, size_t> StateChangeAmounts_t;
613 StateChangeAmounts_t StateChangeAmounts;
614 for (StateChangeMap_t::const_iterator stateiter = rangeiter.first;
615 stateiter != rangeiter.second; ++stateiter)
616 ++StateChangeAmounts[stateiter->second];
617 // is it an atom?
618 if ((StateChangeAmounts[atomInsertedState] + StateChangeAmounts[atomRemovedState]) != 0) {
619 ASSERT( StateChangeAmounts[atomInsertedState] >= StateChangeAmounts[atomRemovedState],
620 "GLWorldScene::moleculeInserted() - more atomRemoved states "
621 +toString(StateChangeAmounts[atomRemovedState])+" than atomInserted "
622 +toString(StateChangeAmounts[atomInsertedState])+" for atom "+toString(iter->first));
623 if (StateChangeAmounts[atomInsertedState] > StateChangeAmounts[atomRemovedState]) {
624 LOG(1, "INFO: invoking atomInserted for atom " << iter->first);
625 QMetaObject::invokeMethod(molObject, // pointer to a QObject
626 "atomInserted", // member name (no parameters here)
627 Qt::QueuedConnection, // connection type
628 Q_ARG(QtObservedAtom::ptr, QtObservedAtomMap[iter->first])); // parameters
629 } else {
630 LOG(1, "INFO: Atom " << iter->first << " has been inserted and removed already.");
631 }
632 }
633 // or is it a bond?
634 if ((StateChangeAmounts[bondInsertedState] + StateChangeAmounts[bondInsertedState]) != 0) {
635 ASSERT( StateChangeAmounts[bondInsertedState] >= StateChangeAmounts[bondRemovedState],
636 "GLWorldScene::moleculeInserted() - more bondRemoved states "
637 +toString(StateChangeAmounts[bondRemovedState])+" than bondInserted "
638 +toString(StateChangeAmounts[bondInsertedState])+" for bond "+toString(iter->first));
639 if (StateChangeAmounts[bondInsertedState] > StateChangeAmounts[bondRemovedState]) {
640 LOG(1, "INFO: invoking bondInserted for bond " << iter->first);
641 QMetaObject::invokeMethod(molObject, // pointer to a QObject
642 "bondInserted", // member name (no parameters here)
643 Qt::QueuedConnection, // connection type
644 Q_ARG(QtObservedBond::ptr, QtObservedBondMap[iter->first])); // parameters
645 } else {
646 LOG(1, "INFO: Bond " << iter->first << " has been inserted and removed already.");
647 }
648 }
649 ASSERT( (StateChangeAmounts[bondInsertedState] + StateChangeAmounts[bondInsertedState]
650 + StateChangeAmounts[atomInsertedState] + StateChangeAmounts[atomRemovedState]) != 0,
651 "GLWorldScene::moleculeInserted() - state with no changes for "+toString(iter->first));
652 // removed all state changes for this atom/bond
653 MoleculeMissedStateMap[molid].erase(rangeiter.first, rangeiter.second);
654 } else {
655 // can only be an insertion
656 switch (rangeiter.first->second) {
657 case atomRemovedState:
658 ASSERT( 0,
659 "GLWorldScene::moleculeInserted() - atomRemoved state without atomInserted for atom "
660 +toString(iter->first));
661 break;
662 case atomInsertedState:
663 {
664 LOG(1, "INFO: invoking atomInserted for atom " << iter->first);
665 QMetaObject::invokeMethod(molObject, // pointer to a QObject
666 "atomInserted", // member name (no parameters here)
667 Qt::QueuedConnection, // connection type
668 Q_ARG(QtObservedAtom::ptr, QtObservedAtomMap[iter->first])); // parameters
669 break;
670 }
671 case bondRemovedState:
672 ASSERT( 0,
673 "GLWorldScene::moleculeInserted() - bondRemoved state without bondInserted for bond "
674 +toString(iter->first));
675 break;
676 case bondInsertedState:
677 {
678 LOG(1, "INFO: invoking bondInserted for bond " << iter->first);
679 QMetaObject::invokeMethod(molObject, // pointer to a QObject
680 "bondInserted", // member name (no parameters here)
681 Qt::QueuedConnection, // connection type
682 Q_ARG(QtObservedBond::ptr, QtObservedBondMap[iter->first])); // parameters
683 break;
684 }
685 default:
686 ASSERT( 0,
687 "GLWorldScene::moleculeInserted() - there are unknown change states.");
688 break;
689 }
690 // removed state changes for this atom/bond
691 MoleculeMissedStateMap[molid].erase(iter);
692 }
693 }
694 }
695
696 connect (molObject, SIGNAL(changed()), this, SIGNAL(changed()));
697 connect (molObject, SIGNAL(changeOccured()), this, SIGNAL(changeOccured()));
698 connect (molObject, SIGNAL(atomClicked(atomId_t)), this, SLOT(atomClicked(atomId_t)));
699 connect (molObject, SIGNAL(moleculeClicked(moleculeId_t)), this, SLOT(moleculeClicked(moleculeId_t)));
700 connect (molObject, SIGNAL(moleculeEmptied(QtObservedMolecule::ptr)), this, SLOT(moleculeEmpty(QtObservedMolecule::ptr)));
701 connect (molObject, SIGNAL(selectionChanged()), this, SIGNAL(changed()));
702 connect (molObject, SIGNAL(selectionChanged()), this, SIGNAL(changed()));
703 connect (molObject, SIGNAL(hoverChanged(const atomId_t)), this, SIGNAL(hoverChanged(const atomId_t)));
704 connect (molObject, SIGNAL(hoverChanged(const moleculeId_t, int)), this, SIGNAL(hoverChanged(const moleculeId_t, int)));
705 connect (molObject, SIGNAL(hoverChanged(const moleculeId_t, int)), this, SIGNAL(hoverChanged(const moleculeId_t, int)));
706
707 emit changed();
708 emit changeOccured();
709
710 // remove state change map for the molecule
711 {
712 boost::recursive_mutex::scoped_lock lock(MoleculeMissedStateMap_mutex);
713 MoleculeMissedStateMap.erase(molid);
714 }
715}
716
717/** Removes a molecule from the scene.
718 *
719 * @param _mol QtObservedMolecule instance of molecule to remove
720 */
721void GLWorldScene::moleculeRemoved(QtObservedMolecule* _mol)
722{
723 const ObservedValue_Index_t molid = _mol->getIndex();
724 LOG(3, "INFO: GLWorldScene: Received signal moleculeRemoved for molecule "+toString(molid)+".");
725
726 boost::recursive_mutex::scoped_lock lock(MoleculeinSceneMap_mutex);
727
728 MoleculeNodeMap::iterator iter = MoleculesinSceneMap.find(molid);
729 ASSERT ( iter != MoleculesinSceneMap.end(),
730 "GLWorldScene::moleculeRemoved() - to be removed molecule "+toString(_mol->getMolIndex())
731 +" is already gone.");
732 // check if it's already empty
733 const RemovedMoleculesMap_t::iterator emptyiter = EmptyMolecules.find(_mol);
734 if (emptyiter != EmptyMolecules.end()) {
735 LOG(1, "DEBUG: Removing empty GLMoleculeObject_molecule to id " << _mol->getMolIndex()
736 << " from scene.");
737 // it's already empty, remove it
738 ASSERT( emptyiter->second == iter->second,
739 "GLWorldScene::moleculeRemoved() - empty molecule "
740 +toString(emptyiter->second)+" and removed molecule "
741 +toString(iter->second)+" don't match.");
742 LOG(1, "DEBUG: Deleting already empty GLMoleculeObject_molecule to id " << _mol->getMolIndex());
743 GLMoleculeObject_molecule *molObject = emptyiter->second;
744 EmptyMolecules.erase(emptyiter);
745 molObject->disconnect();
746 delete molObject;
747
748 emit changed();
749 emit changeOccured();
750 } else {
751 // otherwise note it as removal candidate
752 RemovedMolecules.insert( std::make_pair(_mol, iter->second) );
753 }
754 MoleculesinSceneMap.erase(iter);
755
756 // remove any possible state changes left
757 {
758 boost::recursive_mutex::scoped_lock lock(MoleculeMissedStateMap_mutex);
759 MoleculeMissedStateMap.erase(molid);
760 }
761}
762
763void GLWorldScene::moleculeEmpty(QtObservedMolecule::ptr _mol)
764{
765 LOG(3, "INFO: GLWorldScene: Received signal moleculeEmpty for molecule "
766 +toString(_mol->getMolIndex())+".");
767
768 boost::recursive_mutex::scoped_lock lock(MoleculeinSceneMap_mutex);
769
770 const ObservedValue_Index_t molid = _mol->getIndex();
771 MoleculeNodeMap::iterator iter = MoleculesinSceneMap.find(molid);
772 if ( iter == MoleculesinSceneMap.end()) {
773 RemovedMoleculesMap_t::iterator removeiter = RemovedMolecules.find(_mol.get());
774 ASSERT ( removeiter != RemovedMolecules.end(),
775 "GLWorldScene::moleculeEmpty() - to be removed molecule "+toString(_mol->getMolIndex())
776 +" is neither in MoleculesinSceneMap, nor in RemovedMolecules.");
777 // it's noted for removal already, remove it
778 LOG(1, "DEBUG: Deleting empty GLMoleculeObject_molecule to id " << _mol->getMolIndex());
779 GLMoleculeObject_molecule *molObject = removeiter->second;
780 RemovedMolecules.erase(removeiter);
781 molObject->disconnect();
782 delete molObject;
783
784 emit changed();
785 emit changeOccured();
786 } else {
787 // otherwise just note it as empty
788 EmptyMolecules.insert( std::make_pair(_mol.get(), iter->second) );
789 }
790
791// // remove any possible state changes left
792// {
793// boost::recursive_mutex::scoped_lock lock(MoleculeMissedStateMap_mutex);
794// MoleculeMissedStateMap.erase(molid);
795// }
796}
797
798void GLWorldScene::moleculesVisibilityChanged(ObservedValue_Index_t _id, bool _visible)
799{
800 boost::recursive_mutex::scoped_lock lock(MoleculeinSceneMap_mutex);
801 MoleculeNodeMap::iterator iter = MoleculesinSceneMap.find(_id);
802 ASSERT( iter != MoleculesinSceneMap.end(),
803 "GLWorldScene::moleculeInserted() - molecule's id "
804 +toString(board->getMoleculeIdToIndex(_id))+" is unknown.");
805
806 GLMoleculeObject_molecule *molObject = iter->second;
807 molObject->setVisible(_visible);
808
809 emit changed();
810 emit changeOccured();
811}
812
813/** Adds a shape to the scene.
814 *
815 */
816void GLWorldScene::addShape(const std::string &_name)
817{
818 Shape * const shape = ShapeRegistry::getInstance().getByName(_name);
819 if (shape != NULL) {
820 GLMoleculeObject_shape *shapeObject = new GLMoleculeObject_shape(*shape, this);
821 ShapeNodeMap::iterator iter = ShapesinSceneMap.find(_name);
822 ASSERT(iter == ShapesinSceneMap.end(),
823 "GLWorldScene::addShape() - same shape "+_name+" added again.");
824 ShapesinSceneMap.insert( make_pair(_name, shapeObject) );
825 } else
826 ELOG(2, "GLWorldScene::addShape() - shape disappeared before we could draw it.");
827
828 emit changed();
829}
830
831void GLWorldScene::removeShape(const std::string &_name)
832{
833 ShapeNodeMap::iterator iter = ShapesinSceneMap.find(_name);
834 ASSERT(iter != ShapesinSceneMap.end(),
835 "GLWorldScene::removeShape() - shape "+_name+" not in scene.");
836 ShapesinSceneMap.erase(iter);
837 delete(iter->second);
838
839 emit changed();
840}
841
842void GLWorldScene::updateSelectedShapes()
843{
844 foreach (QObject *obj, children()) {
845 GLMoleculeObject_shape *shapeobj = qobject_cast<GLMoleculeObject_shape *>(obj);
846 if (shapeobj){
847 shapeobj->enable(ShapeRegistry::getInstance().isSelected(shapeobj->getShape()));
848 }
849 }
850
851 emit changed();
852}
853
854void GLWorldScene::initialize(QGLView *view, QGLPainter *painter) const
855{
856 // Initialize all of the mesh objects that we have as children.
857 foreach (QObject *obj, children()) {
858 GLMoleculeObject *meshobj = qobject_cast<GLMoleculeObject *>(obj);
859 if (meshobj)
860 meshobj->initialize(view, painter);
861 }
862}
863
864void GLWorldScene::draw(QGLPainter *painter, const QVector4D &cameraPlane) const
865{
866 // Draw all of the mesh objects that we have as children.
867 foreach (QObject *obj, children()) {
868 GLMoleculeObject *meshobj = qobject_cast<GLMoleculeObject *>(obj);
869 if (meshobj)
870 meshobj->draw(painter, cameraPlane);
871 }
872}
873
874void GLWorldScene::setSelectionMode(SelectionModeType mode)
875{
876 selectionMode = mode;
877 // TODO send update to toolbar
878}
879
880void GLWorldScene::setSelectionModeAtom()
881{
882 setSelectionMode(SelectAtom);
883}
884
885void GLWorldScene::setSelectionModeMolecule()
886{
887 setSelectionMode(SelectMolecule);
888}
889
Note: See TracBrowser for help on using the repository browser.