source: src/UIElements/Views/Qt4/Qt3D/GLMoleculeObject_molecule.cpp@ 2b596f

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 2b596f was 2b596f, checked in by Frederik Heber <heber@…>, 10 years ago

Hovering over molecules is working including update of QtInfoBox.

  • Property mode set to 100644
File size: 22.3 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 * GLMoleculeObject_molecule.cpp
26 *
27 * Created on: Mar 30, 2012
28 * Author: ankele
29 */
30
31
32
33
34
35// include config.h
36#ifdef HAVE_CONFIG_H
37#include <config.h>
38#endif
39
40#include "GLMoleculeObject_molecule.hpp"
41
42#include <Qt3D/qglscenenode.h>
43#include <Qt3D/qglbuilder.h>
44
45#include "CodePatterns/MemDebug.hpp"
46
47#include "CodePatterns/Assert.hpp"
48#include "CodePatterns/Log.hpp"
49#include "CodePatterns/Observer/Notification.hpp"
50#include "CodePatterns/Observer/ObserverLog.hpp"
51
52#include "Atom/atom.hpp"
53#include "molecule.hpp"
54#include "Descriptors/AtomIdDescriptor.hpp"
55#include "Element/element.hpp"
56#include "LinearAlgebra/Vector.hpp"
57#include "LinkedCell/PointCloudAdaptor.hpp"
58#include "LinkedCell/linkedcell.hpp"
59#include "Tesselation/tesselation.hpp"
60#include "Tesselation/BoundaryLineSet.hpp"
61#include "Tesselation/BoundaryTriangleSet.hpp"
62#include "Tesselation/CandidateForTesselation.hpp"
63#include "Atom/TesselPoint.hpp"
64#include "World.hpp"
65
66#include "GLMoleculeObject_atom.hpp"
67
68static QGLSceneNode *createMoleculeMesh(const molecule *molref, QObject *parent)
69{
70// Shape shape = molref->getBoundingSphere();
71 double minradius = 2.; // TODO: set to maximum bond length value
72 LOG(3, "DEBUG: Molecule fits into sphere of radius " << minradius);
73 if (minradius < 1.)
74 minradius = 1.;
75
76 QGeometryData geo;
77 // we need at least three points for tesselation
78 if (molref->getAtomCount() >= 3) {
79 // Tesselate the points.
80 Tesselation T;
81 PointCloudAdaptor<molecule> cloud(const_cast<molecule *>(molref), molref->getName());
82 T(cloud, minradius);
83
84 // Fill the points into a Qt geometry.
85 LinkedCell_deprecated LinkedList(cloud, minradius);
86 std::map<int, int> indices;
87 std::map<int, Vector> normals;
88 int index = 0;
89 for (PointMap::const_iterator piter = T.PointsOnBoundary.begin();
90 piter != T.PointsOnBoundary.end(); ++piter) {
91 const Vector &point = piter->second->getPosition();
92 // add data to the primitive
93 geo.appendVertex(QVector3D(point[0], point[1], point[2]));
94 Vector normalvector;
95 for (LineMap::const_iterator lineiter = piter->second->lines.begin();
96 lineiter != piter->second->lines.end(); ++lineiter)
97 for (TriangleMap::const_iterator triangleiter = lineiter->second->triangles.begin();
98 triangleiter != lineiter->second->triangles.end(); ++triangleiter)
99 normalvector +=
100 triangleiter->second->NormalVector;
101 normalvector.Normalize();
102 geo.appendNormal(QVector3D(normalvector[0], normalvector[1], normalvector[2]));
103 geo.appendColor(QColor(1, 1, 1, 1));
104 geo.appendTexCoord(QVector2D(0, 0));
105 indices.insert( std::make_pair( piter->second->getNr(), index++));
106 }
107
108 // Fill the tesselated triangles into the geometry.
109 for (TriangleMap::const_iterator runner = T.TrianglesOnBoundary.begin();
110 runner != T.TrianglesOnBoundary.end(); runner++) {
111 int v[3];
112 for (size_t i=0; i<3; ++i)
113 v[i] = runner->second->endpoints[i]->getNr();
114
115 // Sort the vertices so the triangle is clockwise (relative to the normal vector).
116 Vector cross = T.PointsOnBoundary[v[1]]->getPosition() - T.PointsOnBoundary[v[0]]->getPosition();
117 cross.VectorProduct(T.PointsOnBoundary[v[2]]->getPosition() - T.PointsOnBoundary[v[0]]->getPosition());
118 if (cross.ScalarProduct(runner->second->NormalVector) > 0)
119 geo.appendIndices(indices[v[0]], indices[v[1]], indices[v[2]]);
120 else
121 geo.appendIndices(indices[v[0]], indices[v[2]], indices[v[1]]);
122 }
123 }
124
125 // Build a mesh from the geometry.
126 QGLBuilder builder;
127 builder.addTriangles(geo);
128 QGLSceneNode *mesh = builder.finalizedSceneNode();
129 return mesh;
130}
131
132GLMoleculeObject_molecule::GLMoleculeObject_molecule(QObject *parent, const molecule *molref) :
133 GLMoleculeObject(createMoleculeMesh(molref, parent), parent),
134 Observer(std::string("GLMoleculeObject_molecule")+toString(molref->getId())),
135 isBoundingBoxUptodate(true),
136 isSignedOn(false),
137 _molecule(molref),
138 TesselationHullUptodate(true),
139 hoverAtom(NULL)
140{
141 // sign on as observer (obtain non-const instance before)
142 _molecule->signOn(this, molecule::AtomInserted);
143 _molecule->signOn(this, molecule::AtomRemoved);
144 _molecule->signOn(this, molecule::AtomMoved);
145 isSignedOn = true;
146 /*molref->signOn(this, AtomObservable::IndexChanged);
147 molref->signOn(this, AtomObservable::PositionChanged);
148 molref->signOn(this, AtomObservable::ElementChanged);
149 molref->signOn(this, AtomObservable::BondsAdded);*/
150 setMaterial(getMaterial(1));
151 World::getInstance().signOn(this, World::SelectionChanged);
152 updateBoundingBox();
153
154 // initially, atoms and bonds should be visible
155 m_visible = false;
156
157 init();
158
159 connect (this, SIGNAL(hoverChanged(GLMoleculeObject *)), this, SLOT(hoverChangedSignalled(GLMoleculeObject *)));
160 connect (this, SIGNAL(hoverChanged(GLMoleculeObject *)), this, SIGNAL(changed()));
161}
162
163GLMoleculeObject_molecule::GLMoleculeObject_molecule(QGLSceneNode *mesh[], QObject *parent, const molecule *molref) :
164 GLMoleculeObject(mesh, parent),
165 Observer(std::string("GLMoleculeObject_molecule")+toString(molref->getId())),
166 isBoundingBoxUptodate(true),
167 isSignedOn(false),
168 _molecule(molref),
169 TesselationHullUptodate(true),
170 hoverAtom(NULL)
171{
172 // sign on as observer (obtain non-const instance before)
173 _molecule->signOn(this, molecule::AtomInserted);
174 _molecule->signOn(this, molecule::AtomRemoved);
175 _molecule->signOn(this, molecule::AtomMoved);
176 isSignedOn = true;
177 /*molref->signOn(this, AtomObservable::IndexChanged);
178 molref->signOn(this, AtomObservable::PositionChanged);
179 molref->signOn(this, AtomObservable::ElementChanged);
180 molref->signOn(this, AtomObservable::BondsAdded);*/
181 setMaterial(getMaterial(1));
182 World::getInstance().signOn(this, World::SelectionChanged);
183 updateBoundingBox();
184
185 // initially, atoms and bonds should be visible
186 m_visible = false;
187
188 init();
189
190 connect (this, SIGNAL(hoverChanged(GLMoleculeObject *)), this, SLOT(hoverChangedSignalled(GLMoleculeObject *)));
191 connect (this, SIGNAL(hoverChanged(GLMoleculeObject *)), this, SIGNAL(changed()));
192}
193
194GLMoleculeObject_molecule::~GLMoleculeObject_molecule()
195{
196 if (isSignedOn) {
197 _molecule->signOff(this, molecule::AtomInserted);
198 _molecule->signOff(this, molecule::AtomRemoved);
199 _molecule->signOff(this, molecule::AtomMoved);
200 }
201 /*_atom->signOff(this, AtomObservable::IndexChanged);
202 _atom->signOff(this, AtomObservable::PositionChanged);
203 _atom->signOff(this, AtomObservable::ElementChanged);
204 _atom->signOff(this, AtomObservable::BondsAdded);*/
205 World::getInstance().signOff(this, World::SelectionChanged);
206}
207
208/** Initialise the WorldScene with molecules and atoms from World.
209 *
210 */
211void GLMoleculeObject_molecule::init()
212{
213 if (_molecule->begin() != _molecule->end()) {
214 int atomicid = -1;
215 for (molecule::const_iterator atomiter = _molecule->begin();
216 atomiter != _molecule->end();
217 atomiter++) {
218 // create atom objects in scene
219 atomicid = (*atomiter)->getId();
220 atomInserted(atomicid);
221
222 // create bond objects in scene
223 const BondList &bondlist = (*atomiter)->getListOfBonds();
224 for (BondList::const_iterator bonditer = bondlist.begin();
225 bonditer != bondlist.end();
226 ++bonditer) {
227 const bond::ptr _bond = *bonditer;
228 const GLMoleculeObject_bond::SideOfBond side = (_bond->leftatom == *atomiter) ?
229 GLMoleculeObject_bond::left : GLMoleculeObject_bond::right;
230 bondInserted(_bond, side);
231 }
232 }
233 // set id to one of the atom's as either mol or atoms is present at the same time
234 setObjectId(atomicid);
235 }
236}
237
238void GLMoleculeObject_molecule::addAtomBonds(
239 const bond::ptr &_bond,
240 const GLMoleculeObject_bond::SideOfBond _side
241 )
242{
243 bool bond_present = false;
244 const BondIds ids = getBondIds(_bond, _side);
245 // check whether bond is not present already
246 bond_present = BondsinSceneMap.count(ids);
247 if (!bond_present)
248 bondInserted(_bond, _side);
249 else {
250 BondsinSceneMap[ids]->resetPosition();
251 BondsinSceneMap[ids]->resetWidth();
252 }
253}
254
255void GLMoleculeObject_molecule::addAtomBonds(
256 const atom *_atom)
257{
258 const bool atom_present = AtomsinSceneMap.count(_atom->getId());
259 const BondList &bondlist = _atom->getListOfBonds();
260 for (BondList::const_iterator bonditer = bondlist.begin();
261 (bonditer != bondlist.end()) && atom_present;
262 ++bonditer) {
263 const bond::ptr _bond = *bonditer;
264 // check if OtherAtom's sphere is already present
265 const atom *OtherAtom = _bond->GetOtherAtom(_atom);
266 const bool otheratom_present = AtomsinSceneMap.count(OtherAtom->getId());
267 if (otheratom_present && atom_present) {
268 const GLMoleculeObject_bond::SideOfBond side = (_bond->leftatom == _atom) ?
269 GLMoleculeObject_bond::left : GLMoleculeObject_bond::right;
270 const GLMoleculeObject_bond::SideOfBond otherside = (_bond->leftatom == _atom) ?
271 GLMoleculeObject_bond::right : GLMoleculeObject_bond::left;
272 addAtomBonds(_bond, side);
273 addAtomBonds(_bond, otherside);
274 }
275 }
276}
277
278void GLMoleculeObject_molecule::reinit()
279{
280 if (_molecule->getAtomCount() > 0) {
281 for (molecule::const_iterator atomiter = _molecule->begin();
282 atomiter != _molecule->end();
283 atomiter++) {
284 // check whether atom already exists
285 const atomId_t atomid = (*atomiter)->getId();
286 const bool atom_present = AtomsinSceneMap.count(atomid);
287 if (!atom_present)
288 atomInserted((*atomiter)->getId());
289 else
290 AtomsinSceneMap[atomid]->resetPosition();
291
292
293 // create bond objects in scene
294 addAtomBonds(*atomiter);
295 }
296 }
297}
298
299void GLMoleculeObject_molecule::updateBoundingBox()
300{
301 isBoundingBoxUptodate = true;
302 Shape shape = _molecule->getBoundingSphere();
303 Vector v = shape.getCenter();
304 setPosition(QVector3D(v[0], v[1], v[2]));
305 setScale(shape.getRadius() + 0.3); // getBoundingShape() only sees atoms as points, so make the box a bit bigger
306}
307
308void GLMoleculeObject_molecule::update(Observable *publisher)
309{
310#ifdef LOG_OBSERVER
311 const molecule *_mol = static_cast<molecule *>(publisher);
312 observerLog().addMessage() << "++ Update of Observer " << observerLog().getName(static_cast<Observer *>(this)) << " from molecule "+toString(_mol->getId())+".";
313#endif
314}
315
316void GLMoleculeObject_molecule::subjectKilled(Observable *publisher)
317{
318 isSignedOn = false;
319}
320
321void GLMoleculeObject_molecule::recieveNotification(Observable *publisher, Notification_ptr notification)
322{
323 if (publisher == dynamic_cast<const Observable*>(_molecule)){
324 // notofication from atom
325#ifdef LOG_OBSERVER
326 observerLog().addMessage() << "++ Update of Observer "<< observerLog().getName(static_cast<Observer *>(this))
327 << " received notification from molecule " << _molecule->getId() << " for channel "
328 << notification->getChannelNo() << ".";
329#endif
330 switch (notification->getChannelNo()) {
331 case molecule::AtomInserted:
332 {
333 const atomId_t _id = _molecule->lastChanged()->getId();
334 #ifdef LOG_OBSERVER
335 observerLog().addMessage() << "++ Observer " << observerLog().getName(static_cast<Observer *>(this)) << " received notification that atom "+toString(_id)+" has been inserted.";
336 #endif
337 TesselationHullUptodate = false;
338 isBoundingBoxUptodate = false;
339 atomInserted(_id);
340 break;
341 }
342 case World::AtomRemoved:
343 {
344 const atomId_t _id = _molecule->lastChanged()->getId();
345 #ifdef LOG_OBSERVER
346 observerLog().addMessage() << "++ Observer " << observerLog().getName(static_cast<Observer *>(this)) << " received notification that atom "+toString(_id)+" has been removed.";
347 #endif
348 TesselationHullUptodate = false;
349 isBoundingBoxUptodate = false;
350 atomRemoved(_id);
351 break;
352 }
353 case molecule::AtomMoved:
354 {
355 #ifdef LOG_OBSERVER
356 const atomId_t _id = _molecule->lastChanged()->getId();
357 observerLog().addMessage() << "++ Observer " << observerLog().getName(static_cast<Observer *>(this)) << " received notification that atom "+toString(_id)+" has been inserted.";
358 #endif
359 TesselationHullUptodate = false;
360 isBoundingBoxUptodate = false;
361 break;
362 }
363 default:
364 break;
365 }
366 }else{
367 // notification from world
368#ifdef LOG_OBSERVER
369 observerLog().addMessage() << "++ Update of Observer "<< observerLog().getName(static_cast<Observer *>(this))
370 << " received notification from world for channel "
371 << notification->getChannelNo() << ".";
372#endif
373 switch (notification->getChannelNo()) {
374 case World::SelectionChanged:
375 setSelected(World::getInstance().isSelected(_molecule));
376 break;
377 default:
378 break;
379 }
380 }
381}
382
383void GLMoleculeObject_molecule::initialize(QGLView *view, QGLPainter *painter)
384{
385 // Initialize all of the mesh objects that we have as children.
386 if (m_visible) {
387 GLMoleculeObject::initialize(view, painter);
388 } else {
389 foreach (QObject *obj, children()) {
390 GLMoleculeObject *meshobj = qobject_cast<GLMoleculeObject *>(obj);
391 if (meshobj)
392 meshobj->initialize(view, painter);
393 }
394 }
395}
396
397void GLMoleculeObject_molecule::draw(QGLPainter *painter, const QVector4D &cameraPlane)
398{
399 // draw either molecule's mesh or all atoms and bonds
400 if (m_visible) {
401 updateTesselationHull();
402
403 painter->modelViewMatrix().push();
404
405 // Apply the material and effect to the painter.
406 QGLMaterial *material;
407 if (m_hovering)
408 material = m_hoverMaterial;
409 else if (m_selected)
410 material = m_selectionMaterial;
411 else
412 material = m_material;
413
414 ASSERT(material, "GLMoleculeObject::draw: chosen material is NULL");
415
416 painter->setColor(material->diffuseColor());
417 painter->setFaceMaterial(QGL::AllFaces, material);
418 if (m_effect)
419 painter->setUserEffect(m_effect);
420 else
421 painter->setStandardEffect(QGL::LitMaterial);
422
423 // Mark the object for object picking purposes.
424 int prevObjectId = painter->objectPickId();
425 if (m_objectId != -1)
426 painter->setObjectPickId(m_objectId);
427
428 m_mesh[0]->draw(painter);
429
430 // Turn off the user effect, if present.
431 if (m_effect)
432 painter->setStandardEffect(QGL::LitMaterial);
433
434 // Revert to the previous object identifier.
435 painter->setObjectPickId(prevObjectId);
436
437 // Restore the modelview matrix.
438 painter->modelViewMatrix().pop();
439
440 // GLMoleculeObject::draw(painter, cameraPlane);
441 } else {
442 // Draw all of the mesh objects that we have as children.
443 foreach (QObject *obj, children()) {
444 GLMoleculeObject *meshobj = qobject_cast<GLMoleculeObject *>(obj);
445 if (meshobj)
446 meshobj->draw(painter, cameraPlane);
447 }
448
449 // update bounding box prior to selection
450 if (!isBoundingBoxUptodate)
451 updateBoundingBox();
452
453 painter->modelViewMatrix().push();
454 painter->modelViewMatrix().translate(m_position);
455 if ((m_scaleX != 1.0f) || (m_scaleY != 1.0f) || (m_scaleZ != 1.0f))
456 painter->modelViewMatrix().scale(m_scaleX, m_scaleY, m_scaleZ);
457 if (m_rotationAngle != 0.0f)
458 painter->modelViewMatrix().rotate(m_rotationAngle, m_rotationVector);
459
460 // Draw a box around the mesh, if selected.
461 if (m_selected)
462 drawSelectionBox(painter);
463
464 // Restore the modelview matrix.
465 painter->modelViewMatrix().pop();
466 }
467}
468
469/** Adds an atom of this molecule to the scene.
470 *
471 * @param _atom atom to add
472 */
473void GLMoleculeObject_molecule::atomInserted(const atomicNumber_t _id)
474{
475 LOG(3, "INFO: GLWorldScene: Received signal atomInserted for atom "+toString(_id)+".");
476 GLMoleculeObject_atom *atomObject = new GLMoleculeObject_atom(GLMoleculeObject::meshSphere, this, _id);
477 AtomNodeMap::iterator iter = AtomsinSceneMap.find(_id);
478 ASSERT(iter == AtomsinSceneMap.end(),
479 "GLWorldScene::atomAdded() - same atom with id "+toString(_id)+" added again.");
480 AtomsinSceneMap.insert( make_pair(_id, atomObject) );
481
482 qRegisterMetaType<atomId_t>("atomId_t");
483 qRegisterMetaType<bond::ptr>("bond::ptr");
484 qRegisterMetaType<GLMoleculeObject_bond::SideOfBond>("GLMoleculeObject_bond::SideOfBond");
485 connect (atomObject, SIGNAL(clicked(atomId_t)), this, SIGNAL(atomClicked(atomId_t)));
486 connect (atomObject, SIGNAL(changed()), this, SIGNAL(changed()));
487 connect (atomObject, SIGNAL(hoverChanged(GLMoleculeObject *)), this, SIGNAL(changed()));
488 connect (atomObject, SIGNAL(hoverChanged(GLMoleculeObject *)), this, SLOT(hoverChangedSignalled(GLMoleculeObject *)));
489 connect (atomObject, SIGNAL(selectionChanged()), this, SIGNAL(changed()));
490 connect (atomObject, SIGNAL(BondsInserted(const bond::ptr , const GLMoleculeObject_bond::SideOfBond)), this, SLOT(bondInserted(const bond::ptr , const GLMoleculeObject_bond::SideOfBond)));
491 connect (atomObject, SIGNAL(indexChanged(GLMoleculeObject_atom*, int, int)), this, SIGNAL(changeAtomId(GLMoleculeObject_atom*, int, int)));
492
493 isBoundingBoxUptodate = false;
494
495 if (m_objectId == -1)
496 setObjectId(_id);
497
498 //bondsChanged(_atom);
499 emit changeOccured();
500}
501
502/** Removes an atom of this molecule from the scene.
503 *
504 * We just the id as the atom might have already been destroyed.
505 *
506 * @param _id id of atom to remove
507 */
508void GLMoleculeObject_molecule::atomRemoved(const atomicNumber_t _id)
509{
510 LOG(3, "INFO: GLWorldScene: Received signal atomRemoved for atom "+toString(_id)+".");
511 // bonds are removed by signal coming from ~bond
512
513 if (m_objectId == _id)
514 setObjectId(-1);
515
516 // remove atoms
517 AtomNodeMap::iterator iter = AtomsinSceneMap.find(_id);
518 ASSERT(iter != AtomsinSceneMap.end(),
519 "GLWorldScene::atomRemoved() - atom "+toString(_id)+" not on display.");
520 GLMoleculeObject_atom *atomObject = iter->second;
521 atomObject->disconnect();
522 AtomsinSceneMap.erase(iter);
523 delete atomObject;
524
525 isBoundingBoxUptodate = false;
526
527 emit changeOccured();
528}
529
530void GLMoleculeObject_molecule::hoverChangedSignalled(GLMoleculeObject *ob)
531{
532 // Find the atom, ob corresponds to.
533 hoverAtom = NULL;
534 GLMoleculeObject_atom *atomObject = dynamic_cast<GLMoleculeObject_atom *>(ob);
535 if (atomObject){
536 for (AtomNodeMap::iterator iter = AtomsinSceneMap.begin();iter != AtomsinSceneMap.end(); ++ iter){
537 if (iter->second == atomObject)
538 hoverAtom = World::getInstance().getAtom(AtomById(iter->first));
539 }
540
541 // Propagate signal.
542 emit hoverChanged(*hoverAtom);
543 } else {
544 // Find the atom, ob corresponds to.
545 GLMoleculeObject_molecule *moleculeObject = dynamic_cast<GLMoleculeObject_molecule *>(ob);
546 if (moleculeObject == this){
547 // Propagate signal.
548 emit hoverChanged(*_molecule, 0);
549 }
550 }
551}
552
553
554/** Helper function to get bond ids in the correct order for BondNodeMap.
555 *
556 * \return pair of ids in correct order.
557 */
558GLMoleculeObject_molecule::BondIds GLMoleculeObject_molecule::getBondIds(
559 const bond::ptr _bond,
560 const enum GLMoleculeObject_bond::SideOfBond _side)
561{
562 BondIds ids;
563 switch (_side) {
564 case GLMoleculeObject_bond::left:
565 ids = std::make_pair(_bond->leftatom->getId(), _bond->rightatom->getId());
566 break;
567 case GLMoleculeObject_bond::right:
568 ids = std::make_pair(_bond->rightatom->getId(), _bond->leftatom->getId());
569 break;
570 }
571 return ids;
572}
573
574/** Adds a bond to the scene.
575 *
576 * @param _bond bond to add
577 * @param side which side of the bond (left or right)
578 */
579void GLMoleculeObject_molecule::bondInserted(const bond::ptr _bond, const enum GLMoleculeObject_bond::SideOfBond _side)
580{
581 LOG(3, "INFO: GLWorldScene::bondInserted() - Adding bond "+toString(*_bond)+".");
582 //LOG(4, "INFO: Currently present bonds " << BondsinSceneMap << ".");
583
584 const BondIds ids = getBondIds(_bond, _side);
585 BondNodeMap::iterator iter = BondsinSceneMap.find(ids);
586 if (iter == BondsinSceneMap.end()) {
587 GLMoleculeObject_bond * bondObject =
588 new GLMoleculeObject_bond(GLMoleculeObject::meshCylinder, this, _bond, _side);
589 connect (
590 bondObject, SIGNAL(BondRemoved(const atomId_t, const atomId_t)),
591 this, SLOT(bondRemoved(const atomId_t, const atomId_t)));
592 connect (bondObject, SIGNAL(changed()), this, SIGNAL(changed()));
593 BondsinSceneMap.insert( make_pair(ids, bondObject) );
594 // BondIdsinSceneMap.insert( Leftids );
595 } else {
596 iter->second->resetPosition();
597 iter->second->resetWidth();
598 }
599 emit changeOccured();
600}
601
602/** Removes a bond from the scene.
603 *
604 * @param _bond bond to remove
605 */
606void GLMoleculeObject_molecule::bondRemoved(const atomId_t leftnr, const atomId_t rightnr)
607{
608 LOG(3, "INFO: GLWorldScene::bondRemoved() - Removing bond between "+toString(leftnr)+" and "+toString(rightnr)+".");
609 {
610 // left bond
611 const BondIds Leftids( make_pair(leftnr, rightnr) );
612 BondNodeMap::iterator leftiter = BondsinSceneMap.find( Leftids );
613 ASSERT(leftiter != BondsinSceneMap.end(),
614 "GLWorldScene::bondRemoved() - bond "+toString(leftnr)+"-"
615 +toString(rightnr)+" not on display.");
616 GLMoleculeObject_bond *bondObject = leftiter->second;
617 bondObject->disconnect();
618 BondsinSceneMap.erase(leftiter);
619 delete bondObject; // is done by signal from bond itself
620 //LOG(4, "INFO: Still present bonds " << BondsinSceneMap << ".");
621 }
622
623 emit changeOccured();
624}
625
626void GLMoleculeObject_molecule::setVisible(bool value)
627{
628 // first update the mesh if we are going to be visible now
629 if (value)
630 updateTesselationHull();
631 // then emit onward
632 GLMoleculeObject::setVisible(value);
633}
634
635void GLMoleculeObject_molecule::updateTesselationHull()
636{
637 if (!TesselationHullUptodate) {
638 updateMesh(createMoleculeMesh(_molecule, parent()));
639 TesselationHullUptodate = true;
640 }
641}
642
643std::ostream &operator<<(std::ostream &ost, const GLMoleculeObject_molecule::BondIds &t)
644{
645 ost << t.first << "," << t.second;
646 return ost;
647}
648
Note: See TracBrowser for help on using the repository browser.