source: src/UIElements/Views/Qt4/Qt3D/GLWorldScene.cpp@ 50246c

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

GLWorldScene now properly reassigns parents of GLMoleculeObjects.

  • this is important for atoms and bonds such that they disappear when their associated molecule is visualized as a tesselated surface.
  • Property mode set to 100644
File size: 22.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 <boost/assign.hpp>
58
59#include "Actions/SelectionAction/Atoms/AtomByIdAction.hpp"
60#include "Actions/SelectionAction/Atoms/NotAtomByIdAction.hpp"
61#include "Actions/SelectionAction/Molecules/MoleculeByIdAction.hpp"
62#include "Actions/SelectionAction/Molecules/NotMoleculeByIdAction.hpp"
63#include "Atom/atom.hpp"
64#include "Bond/bond.hpp"
65#include "Descriptors/AtomIdDescriptor.hpp"
66#include "Descriptors/MoleculeIdDescriptor.hpp"
67#include "Helpers/helpers.hpp"
68#include "Shapes/ShapeRegistry.hpp"
69#include "molecule.hpp"
70#include "World.hpp"
71
72#include <iostream>
73
74using namespace MoleCuilder;
75
76GLWorldScene::GLWorldScene(
77 QtObservedInstanceBoard * _board,
78 QObject *parent) :
79 QObject(parent),
80 selectionMode(SelectAtom),
81 board(_board)
82{
83 qRegisterMetaType<atomId_t>("atomId_t");
84 qRegisterMetaType<GLMoleculeObject_bond::SideOfBond>("GLMoleculeObject_bond::SideOfBond");
85
86 int sphereDetails[] = {5, 3, 2, 0};
87 int cylinderDetails[] = {16, 8, 6, 3};
88 for (int i=0;i<GLMoleculeObject::DETAILTYPES_MAX;i++){
89 QGLBuilder emptyBuilder;
90 GLMoleculeObject::meshEmpty[i] = emptyBuilder.finalizedSceneNode();
91 QGLBuilder sphereBuilder;
92 sphereBuilder << QGLSphere(2.0, sphereDetails[i]);
93 GLMoleculeObject::meshSphere[i] = sphereBuilder.finalizedSceneNode();
94 GLMoleculeObject::meshSphere[i]->setOption(QGLSceneNode::CullBoundingBox, true);
95 QGLBuilder cylinderBuilder;
96 cylinderBuilder << QGLCylinder(.25,.25,1.0,cylinderDetails[i]);
97 GLMoleculeObject::meshCylinder[i] = cylinderBuilder.finalizedSceneNode();
98 GLMoleculeObject::meshCylinder[i]->setOption(QGLSceneNode::CullBoundingBox, true);
99 }
100 connect(board, SIGNAL(moleculeInserted(QtObservedMolecule::ptr)),
101 this, SLOT(insertMolecule(QtObservedMolecule::ptr)));
102 connect(board, SIGNAL(moleculeRemoved(ObservedValue_Index_t)),
103 this, SLOT(removeMolecule(ObservedValue_Index_t)));
104
105 connect(board, SIGNAL(atomInserted(QtObservedAtom::ptr)),
106 this, SLOT(connectAtom(QtObservedAtom::ptr)), Qt::DirectConnection);
107 connect(this, SIGNAL(atomConnected(QtObservedAtom::ptr)),
108 this, SLOT(insertAtom(QtObservedAtom::ptr)));
109 connect(board, SIGNAL(atomRemoved(ObservedValue_Index_t)),
110 this, SLOT(removeAtom(ObservedValue_Index_t)));
111
112 connect(board, SIGNAL(bondInserted(QtObservedBond::ptr)),
113 this, SLOT(connectBond(QtObservedBond::ptr)), Qt::DirectConnection);
114 connect(this, SIGNAL(bondConnected(QtObservedBond::ptr)),
115 this, SLOT(insertBond(QtObservedBond::ptr)));
116 connect(board, SIGNAL(bondRemoved(ObservedValue_Index_t)),
117 this, SLOT(removeBond(ObservedValue_Index_t)));
118
119// connect(this, SIGNAL(updated()), this, SLOT(update()));
120}
121
122GLWorldScene::~GLWorldScene()
123{
124 // remove all elements
125 GLMoleculeObject::cleanMaterialMap();
126}
127
128void GLWorldScene::clickAtom(atomId_t no)
129{
130 LOG(3, "INFO: GLWorldScene - atom " << no << " has been clicked.");
131 const atom * const Walker = const_cast<const World &>(World::getInstance()).
132 getAtom(AtomById(no));
133 ASSERT( Walker != NULL,
134 "GLWorldScene::clickAtom() - clicked atom has disappeared.");
135 if (selectionMode == SelectAtom){
136 if (!World::getInstance().isSelected(Walker))
137 SelectionAtomById(std::vector<atomId_t>(1,no));
138 else
139 SelectionNotAtomById(std::vector<atomId_t>(1,no));
140 }else if (selectionMode == SelectMolecule){
141 const molecule *mol = Walker->getMolecule();
142 ASSERT(mol, "Atom without molecule has been clicked.");
143 molids_t ids(1, mol->getId());
144 if (!World::getInstance().isSelected(mol))
145 SelectionMoleculeById(ids);
146 else
147 SelectionNotMoleculeById(ids);
148 }
149 emit clicked(no);
150}
151
152/** Prepares adding an atom to the scene
153 *
154 * We need to listen to moleculeChanged() in order to properly re-parent()
155 * this QObject
156 *
157 * @param _atom atom to connect
158 */
159void GLWorldScene::connectAtom(QtObservedAtom::ptr _atom)
160{
161 connect(_atom.get(), SIGNAL(moleculeChanged()), this, SLOT(reparentAtom()));
162
163 {
164 const ObservedValue_Index_t atomid = _atom->getIndex();
165 boost::recursive_mutex::scoped_lock lock(AtomNodeParentMap_mutex);
166 AtomNodeParentMap[atomid] = static_cast<QObject *>(this);
167 }
168
169 emit atomConnected(_atom);
170}
171
172/** Adds an atom to the scene.
173 *
174 * @param _atom atom to add
175 */
176void GLWorldScene::insertAtom(QtObservedAtom::ptr _atom)
177{
178 LOG(3, "INFO: GLWorldScene: Received signal atomInserted for atom "
179 << _atom->getAtomIndex());
180
181 const ObservedValue_Index_t atomid = _atom->getIndex();
182 QObject *parent = static_cast<QObject *>(this);
183 GLMoleculeObject_atom *atomObject = NULL;
184 {
185 boost::recursive_mutex::scoped_lock lock(AtomNodeParentMap_mutex);
186 AtomNodeParentMap_t::iterator parentiter = AtomNodeParentMap.find(atomid);
187 if (parentiter != AtomNodeParentMap.end())
188 parent = parentiter->second;
189 else
190 AtomNodeParentMap[atomid] = parent;
191
192 atomObject = new GLMoleculeObject_atom(
193 GLMoleculeObject::meshSphere,
194 parent,
195 _atom);
196 ASSERT( atomObject != NULL,
197 "GLWorldScene::atomInserted - could not create atom object for "
198 +toString(_atom->getAtomIndex()));
199 }
200 AtomNodeMap::iterator iter = AtomsinSceneMap.find(atomid);
201 ASSERT(iter == AtomsinSceneMap.end(),
202 "GLWorldScene::insertAtom - same atom with id "
203 +toString(_atom->getAtomIndex())+" added again.");
204 AtomsinSceneMap.insert( make_pair(atomid, atomObject) );
205
206 connect (atomObject, SIGNAL(clicked(atomId_t)), this, SLOT(clickAtom(atomId_t)));
207 connect (atomObject, SIGNAL(changed()), this, SIGNAL(changed()));
208 connect (atomObject, SIGNAL(hoverChanged(GLMoleculeObject *)), this, SIGNAL(changed()));
209 connect (atomObject, SIGNAL(hoverChanged(GLMoleculeObject *)), this, SLOT(hoverChangedSignalled(GLMoleculeObject *)));
210
211 emit changed();
212 emit changeOccured();
213}
214
215/** Removes an atom.
216 *
217 * @param _atomid index of the atom that is removed
218 */
219void GLWorldScene::removeAtom(ObservedValue_Index_t _atomid)
220{
221 LOG(3, "INFO: GLWorldScene: Received signal atomRemoved for atom "+toString(_atomid)+".");
222 // bonds are removed by signal coming from ~bond
223
224 // remove atoms
225 AtomNodeMap::iterator iter = AtomsinSceneMap.find(_atomid);
226 ASSERT(iter != AtomsinSceneMap.end(),
227 "GLWorldScene::removeAtom() - atom "+toString(_atomid)+" not on display.");
228 GLMoleculeObject_atom *atomObject = iter->second;
229 AtomsinSceneMap.erase(iter);
230 delete atomObject;
231 {
232 boost::recursive_mutex::scoped_lock lock(AtomNodeParentMap_mutex);
233 AtomNodeParentMap.erase(_atomid);
234 }
235 emit changed();
236 emit changeOccured();
237}
238
239/** Prepares adding a bond to the scene
240 *
241 * We need to listen to moleculeChanged() in order to properly re-parent()
242 * this QObject
243 *
244 * @param _bond bond to connect
245 */
246void GLWorldScene::connectBond(QtObservedBond::ptr _bond)
247{
248 connect(_bond.get(), SIGNAL(leftmoleculeChanged()), this, SLOT(reparentBondLeft()));
249 connect(_bond.get(), SIGNAL(rightmoleculeChanged()), this, SLOT(reparentBondRight()));
250
251 {
252 const ObservedValue_Index_t bondid = _bond->getIndex();
253 boost::recursive_mutex::scoped_lock lock(BondNodeParentMap_mutex);
254 std::vector<QObject *> parents(2, static_cast<QObject *>(this));
255 if ((_bond->getLeftAtom() != NULL) && (_bond->getLeftAtom()->getMoleculeRef() != NULL)) {
256 const ObservedValue_Index_t molid = _bond->getLeftAtom()->getMoleculeRef()->getIndex();
257 const MoleculeNodeMap::iterator leftiter = MoleculesinSceneMap.find(molid);
258 if (leftiter != MoleculesinSceneMap.end())
259 parents[0] = leftiter->second;
260 }
261 if ((_bond->getRightAtom() != NULL) && (_bond->getRightAtom()->getMoleculeRef() != NULL)) {
262 const ObservedValue_Index_t molid = _bond->getRightAtom()->getMoleculeRef()->getIndex();
263 const MoleculeNodeMap::iterator leftiter = MoleculesinSceneMap.find(molid);
264 if (leftiter != MoleculesinSceneMap.end())
265 parents[1] = leftiter->second;
266 }
267 BondNodeParentMap.insert( std::make_pair(bondid, parents) );
268 }
269
270 emit bondConnected(_bond);
271}
272
273/** Adds a bond to the scene.
274 *
275 * @param _bond bond to add
276 */
277void GLWorldScene::insertBond(QtObservedBond::ptr _bond)
278{
279 static const std::vector< GLMoleculeObject_bond::SideOfBond > bondsides =
280 boost::assign::list_of<GLMoleculeObject_bond::SideOfBond>
281 (GLMoleculeObject_bond::left)
282 (GLMoleculeObject_bond::right);
283 LOG(3, "INFO: GLWorldScene::insertBond() - Adding bonds " << _bond->getBondIndex());
284
285 const ObservedValue_Index_t bondid = _bond->getIndex();
286 std::vector<QObject *> parents(2, static_cast<QObject *>(this));
287 {
288 boost::recursive_mutex::scoped_lock lock(BondNodeParentMap_mutex);
289 BondNodeParentMap_t::iterator parentiter = BondNodeParentMap.find(bondid);
290 if (parentiter != BondNodeParentMap.end()) {
291 parents = parentiter->second;
292 } else
293 BondNodeParentMap.insert( std::make_pair(bondid, parents) );
294
295 BondNodeMap::iterator iter = BondsinSceneMap.find(bondid);
296 ASSERT( iter == BondsinSceneMap.end(),
297 "GLWorldScene::insertBond() - bond "+toString(bondid)+" is already known.");
298 for (size_t i=0;i<2;++i) {
299 GLMoleculeObject_bond *bondObject = new GLMoleculeObject_bond(
300 GLMoleculeObject::meshCylinder,
301 parents[i],
302 _bond,
303 bondsides[i]);
304 connect (bondObject, SIGNAL(changed()), this, SIGNAL(changed()));
305 BondsinSceneMap.insert( std::make_pair(bondid, bondObject) );
306 }
307 }
308
309 emit changed();
310 emit changeOccured();
311}
312
313/** Removes a bond.
314 *
315 * @param _bondid id of bond to remove
316 */
317void GLWorldScene::removeBond(ObservedValue_Index_t _bondid)
318{
319 LOG(3, "INFO: GLWorldScene::removedBond() - Removing bond to id " << _bondid);
320
321 // left bond
322 std::pair<BondNodeMap::iterator, BondNodeMap::iterator> iters =
323 BondsinSceneMap.equal_range(_bondid);
324 ASSERT( iters.first != iters.second,
325 "GLWorldScene::removedBond() - could not find bond to id "+toString(_bondid));
326 for (BondNodeMap::iterator iter = iters.first; iter != iters.second; ++iter) {
327 GLMoleculeObject_bond *bondObject = iter->second;
328 delete bondObject; // is done by signal from bond itself
329 //LOG(4, "INFO: Still present bonds " << BondsinSceneMap << ".");
330 }
331 BondsinSceneMap.erase(_bondid);
332 {
333 boost::recursive_mutex::scoped_lock lock(BondNodeParentMap_mutex);
334 BondNodeParentMap.erase(_bondid);
335 }
336
337 emit changed();
338 emit changeOccured();
339}
340
341void GLWorldScene::hoverChangedSignalled(GLMoleculeObject *ob)
342{
343 // Find the atom, ob corresponds to.
344 hoverAtomId = -1;
345 GLMoleculeObject_atom *atomObject = dynamic_cast<GLMoleculeObject_atom *>(ob);
346 if (atomObject){
347 for (AtomNodeMap::iterator iter = AtomsinSceneMap.begin();iter != AtomsinSceneMap.end(); ++ iter){
348 if (iter->second == atomObject)
349 hoverAtomId = iter->second->objectId();
350 }
351
352 // Propagate signal.
353 emit hoverChanged(hoverAtomId);
354 } else {
355 // Find the atom, ob corresponds to.
356 GLMoleculeObject_molecule *moleculeObject = dynamic_cast<GLMoleculeObject_molecule *>(ob);
357 if (moleculeObject){
358 // Propagate signal.
359 emit hoverChanged(moleculeObject->objectId(), 0);
360 }
361 }
362}
363
364void GLWorldScene::clickMolecule(moleculeId_t no)
365{
366 LOG(3, "INFO: GLMoleculeObject_molecule - mol " << no << " has been clicked.");
367 const molecule * const mol= const_cast<const World &>(World::getInstance()).
368 getMolecule(MoleculeById(no));
369 ASSERT(mol, "Atom without molecule has been clicked.");
370 molids_t ids(1, mol->getId());
371 if (!World::getInstance().isSelected(mol))
372 SelectionMoleculeById(ids);
373 else
374 SelectionNotMoleculeById(ids);
375 emit clicked(no);
376}
377
378/** Inserts a molecule into the scene.
379 *
380 * @param _mol molecule to insert
381 */
382void GLWorldScene::insertMolecule(QtObservedMolecule::ptr _mol)
383{
384 const ObservedValue_Index_t molid = _mol->getIndex();
385 LOG(3, "INFO: GLWorldScene: Received signal moleculeInserted for molecule "
386 << _mol->getMolIndex());
387
388 MoleculeNodeMap::const_iterator iter = MoleculesinSceneMap.find(molid);
389 ASSERT( iter == MoleculesinSceneMap.end(),
390 "GLWorldScene::insertMolecule() - molecule's id "+toString(_mol->getMolIndex())
391 +" already present.");
392
393 // add new object
394 LOG(1, "DEBUG: Adding GLMoleculeObject_molecule to id " << _mol->getMolIndex());
395 GLMoleculeObject_molecule *molObject =
396 new GLMoleculeObject_molecule(
397 GLMoleculeObject::meshEmpty,
398 this,
399 _mol);
400 ASSERT( molObject != NULL,
401 "GLWorldScene::insertMolecule - could not create molecule object for "
402 +toString(_mol->getMolIndex()));
403#ifndef NDEBUG
404 std::pair<MoleculeNodeMap::iterator, bool> inserter =
405#endif
406 MoleculesinSceneMap.insert( make_pair(molid, molObject) );
407 ASSERT(inserter.second,
408 "GLWorldScene::insertMolecule() - molecule "+toString(_mol->getMolIndex())
409 +" already present in scene.");
410
411 connect (molObject, SIGNAL(changed()), this, SIGNAL(changed()));
412 connect (molObject, SIGNAL(changeOccured()), this, SIGNAL(changeOccured()));
413
414 emit changed();
415 emit changeOccured();
416}
417
418/** Removes a molecule from the scene.
419 *
420 * @param _molid index of the molecule to remove
421 */
422void GLWorldScene::removeMolecule(ObservedValue_Index_t _molid)
423{
424 LOG(3, "INFO: GLWorldScene: Received signal moleculeRemoved for molecule "+toString(_molid)+".");
425
426 MoleculeNodeMap::iterator iter = MoleculesinSceneMap.find(_molid);
427 ASSERT ( iter != MoleculesinSceneMap.end(),
428 "GLWorldScene::removeMolecule() - to be removed molecule "+toString(_molid)
429 +" is already gone.");
430 // check if it's already empty
431 GLMoleculeObject_molecule *molObject = iter->second;
432 delete molObject;
433 MoleculesinSceneMap.erase(iter);
434
435 emit changed();
436 emit changeOccured();
437}
438
439void GLWorldScene::moleculesVisibilityChanged(ObservedValue_Index_t _id, bool _visible)
440{
441 MoleculeNodeMap::iterator iter = MoleculesinSceneMap.find(_id);
442 ASSERT( iter != MoleculesinSceneMap.end(),
443 "GLWorldScene::moleculeInserted() - molecule's id "
444 +toString(board->getMoleculeIdToIndex(_id))+" is unknown.");
445
446 GLMoleculeObject_molecule *molObject = iter->second;
447 molObject->setVisible(_visible);
448
449 emit changed();
450 emit changeOccured();
451}
452
453/** Changes the parent of an object in the scene.
454 *
455 * \param _id index of the object whose parent to change
456 * \param _ob new parent
457 */
458void GLWorldScene::reparentAtom()
459{
460 QtObservedAtom *walker = dynamic_cast<QtObservedAtom *>(sender());
461 boost::recursive_mutex::scoped_lock lock(AtomNodeParentMap_mutex);
462 const ObservedValue_Index_t walkerid = walker->getIndex();
463 AtomNodeParentMap_t::iterator parentiter = AtomNodeParentMap.find(walkerid);
464 ASSERT( parentiter != AtomNodeParentMap.end(),
465 "GLWorldScene::reparentAtom() - could not find object to id "+toString(walkerid));
466 AtomNodeMap::iterator atomiter = AtomsinSceneMap.find(walkerid);
467 ASSERT( atomiter != AtomsinSceneMap.end(),
468 "GLWorldScene::reparentAtom() - could not find GLMoleculeObject to id "+toString(walkerid));
469 if (walker->getMoleculeRef() != NULL) {
470 const ObservedValue_Index_t molid = walker->getMoleculeRef()->getIndex();
471 MoleculeNodeMap::iterator moliter = MoleculesinSceneMap.find(molid);
472 if (moliter != MoleculesinSceneMap.end()) {
473 parentiter->second = moliter->second;
474 atomiter->second->setParent(moliter->second);
475 } else {
476 // associated molecule has not yet been instantiated, keep in mind
477 ASSERT(0,
478 "GLWorldScene::reparentAtom() - new molecule not present yet?");
479 }
480 } else {
481 parentiter->second = static_cast<QObject*>(this);
482 atomiter->second->setParent(this);
483 }
484}
485
486/** Changes the parent of an object in the scene.
487 *
488 * \param _id index of the object whose parent to change
489 * \param _ob new parent
490 */
491void GLWorldScene::reparentBondLeft()
492{
493 QtObservedBond *bond = dynamic_cast<QtObservedBond *>(sender());
494 boost::recursive_mutex::scoped_lock lock(BondNodeParentMap_mutex);
495 const ObservedValue_Index_t bondid = bond->getIndex();
496 BondNodeParentMap_t::iterator parentiter = BondNodeParentMap.find(bondid);
497 ASSERT( parentiter != BondNodeParentMap.end(),
498 "GLWorldScene::reparentBondLeft() - could not find object to id "+toString(bondid));
499 BondNodeMap::iterator bonditer = BondsinSceneMap.find(bondid);
500 ASSERT( bonditer != BondsinSceneMap.end(),
501 "GLWorldScene::reparentBondLeft() - could not find GLMoleculeObject to id "+toString(bondid));
502 if (bonditer->second->BondSide != GLMoleculeObject_bond::left)
503 ++bonditer;
504 ASSERT( (bonditer->first == bondid)
505 && (bonditer->second->BondSide == GLMoleculeObject_bond::left),
506 "GLWorldScene::reparentBondLeft() - could not find left-side bond.");
507 if ((bond->getLeftAtom() != NULL) && (bond->getLeftAtom()->getMoleculeRef() != NULL)) {
508 const ObservedValue_Index_t molid = bond->getLeftAtom()->getMoleculeRef()->getIndex();
509 MoleculeNodeMap::iterator moliter = MoleculesinSceneMap.find(molid);
510 if (moliter != MoleculesinSceneMap.end()) {
511 parentiter->second[0] = moliter->second;
512 bonditer->second->setParent(moliter->second);
513 } else {
514 // associated molecule has not yet been instantiated, keep in mind
515 ASSERT(0,
516 "GLWorldScene::reparentBondLeft() - new molecule not present yet?");
517 }
518 } else {
519 parentiter->second[0] = static_cast<QObject*>(this);
520 bonditer->second->setParent(this);
521 }
522}
523
524/** Changes the parent of an object in the scene.
525 *
526 * \param _id index of the object whose parent to change
527 * \param _ob new parent
528 */
529void GLWorldScene::reparentBondRight()
530{
531 QtObservedBond *bond = dynamic_cast<QtObservedBond *>(sender());
532 boost::recursive_mutex::scoped_lock lock(BondNodeParentMap_mutex);
533 const ObservedValue_Index_t bondid = bond->getIndex();
534 BondNodeParentMap_t::iterator parentiter = BondNodeParentMap.find(bondid);
535 ASSERT( parentiter != BondNodeParentMap.end(),
536 "GLWorldScene::reparentBondLeft() - could not find object to id "+toString(bondid));
537 BondNodeMap::iterator bonditer = BondsinSceneMap.find(bondid);
538 ASSERT( bonditer != BondsinSceneMap.end(),
539 "GLWorldScene::reparentBondLeft() - could not find GLMoleculeObject to id "+toString(bondid));
540 if (bonditer->second->BondSide != GLMoleculeObject_bond::right)
541 ++bonditer;
542 ASSERT( (bonditer->first == bondid)
543 && (bonditer->second->BondSide == GLMoleculeObject_bond::right),
544 "GLWorldScene::reparentBondLeft() - could not find right-side bond.");
545 if ((bond->getRightAtom() != NULL) && (bond->getRightAtom()->getMoleculeRef() != NULL)) {
546 const ObservedValue_Index_t molid = bond->getRightAtom()->getMoleculeRef()->getIndex();
547 MoleculeNodeMap::iterator moliter = MoleculesinSceneMap.find(molid);
548 if (moliter != MoleculesinSceneMap.end()) {
549 parentiter->second[1] = moliter->second;
550 bonditer->second->setParent(moliter->second);
551 } else {
552 // associated molecule has not yet been instantiated, keep in mind
553 ASSERT(0,
554 "GLWorldScene::reparentBondRight() - new molecule not present yet?");
555 }
556 } else {
557 parentiter->second[1] = static_cast<QObject*>(this);
558 bonditer->second->setParent(this);
559 }
560}
561
562/** Adds a shape to the scene.
563 *
564 */
565void GLWorldScene::addShape(const std::string &_name)
566{
567 Shape * const shape = ShapeRegistry::getInstance().getByName(_name);
568 if (shape != NULL) {
569 GLMoleculeObject_shape *shapeObject = new GLMoleculeObject_shape(*shape, this);
570 ShapeNodeMap::iterator iter = ShapesinSceneMap.find(_name);
571 ASSERT(iter == ShapesinSceneMap.end(),
572 "GLWorldScene::addShape() - same shape "+_name+" added again.");
573 ShapesinSceneMap.insert( make_pair(_name, shapeObject) );
574 } else
575 ELOG(2, "GLWorldScene::addShape() - shape disappeared before we could draw it.");
576
577 emit changed();
578}
579
580void GLWorldScene::removeShape(const std::string &_name)
581{
582 ShapeNodeMap::iterator iter = ShapesinSceneMap.find(_name);
583 ASSERT(iter != ShapesinSceneMap.end(),
584 "GLWorldScene::removeShape() - shape "+_name+" not in scene.");
585 ShapesinSceneMap.erase(iter);
586 delete(iter->second);
587
588 emit changed();
589}
590
591void GLWorldScene::updateSelectedShapes()
592{
593 foreach (QObject *obj, children()) {
594 GLMoleculeObject_shape *shapeobj = qobject_cast<GLMoleculeObject_shape *>(obj);
595 if (shapeobj){
596 shapeobj->enable(ShapeRegistry::getInstance().isSelected(shapeobj->getShape()));
597 }
598 }
599
600 emit changed();
601}
602
603void GLWorldScene::initialize(QGLView *view, QGLPainter *painter) const
604{
605 // Initialize all of the mesh objects that we have as children.
606 foreach (QObject *obj, children()) {
607 GLMoleculeObject *meshobj = qobject_cast<GLMoleculeObject *>(obj);
608 if (meshobj)
609 meshobj->initialize(view, painter);
610 }
611}
612
613void GLWorldScene::draw(QGLPainter *painter, const QVector4D &cameraPlane) const
614{
615 // Draw all of the mesh objects that we have as children.
616 foreach (QObject *obj, children()) {
617 GLMoleculeObject *meshobj = qobject_cast<GLMoleculeObject *>(obj);
618 if (meshobj)
619 meshobj->draw(painter, cameraPlane);
620 }
621}
622
623void GLWorldScene::setSelectionMode(SelectionModeType mode)
624{
625 selectionMode = mode;
626 // TODO send update to toolbar
627}
628
629void GLWorldScene::setSelectionModeAtom()
630{
631 setSelectionMode(SelectAtom);
632}
633
634void GLWorldScene::setSelectionModeMolecule()
635{
636 setSelectionMode(SelectMolecule);
637}
638
Note: See TracBrowser for help on using the repository browser.