| 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 |  | 
|---|
| 74 | using namespace MoleCuilder; | 
|---|
| 75 |  | 
|---|
| 76 | // static functions | 
|---|
| 77 |  | 
|---|
| 78 | static void eraseBondNodeParents( | 
|---|
| 79 | std::vector<GLWorldScene::BondNodeParentMap_t> &_BondNodeParentMaps, | 
|---|
| 80 | const ObservedValue_Index_t &_bondid) | 
|---|
| 81 | { | 
|---|
| 82 | const size_t left_erased = _BondNodeParentMaps[0].left.erase(_bondid); | 
|---|
| 83 | const size_t right_erased = _BondNodeParentMaps[1].left.erase(_bondid); | 
|---|
| 84 | ASSERT( (left_erased == 1) && (right_erased == 1), | 
|---|
| 85 | "eraseBondNodeParents() - could not erase both parents of "+toString(_bondid)+"?"); | 
|---|
| 86 | } | 
|---|
| 87 |  | 
|---|
| 88 | /** Checks whether both parents of the bond are set to GLWorldScene and the | 
|---|
| 89 | * bondRemoved() signals was received for this bond. | 
|---|
| 90 | * | 
|---|
| 91 | * \param _BondNodeParent_Maps - map containing parents | 
|---|
| 92 | * \param _ToBeRemovedNodes - set with all bonds that are to be removed | 
|---|
| 93 | * \param _bondid id of bond | 
|---|
| 94 | * \return true - both parents are assigned to GLWorldScene, false - else | 
|---|
| 95 | */ | 
|---|
| 96 | static bool checkBondRemoval( | 
|---|
| 97 | const std::vector<GLWorldScene::BondNodeParentMap_t> &_BondNodeParentMaps, | 
|---|
| 98 | const GLWorldScene::ToBeRemovedNodes_t &_ToBeRemovedNodes, | 
|---|
| 99 | const ObservedValue_Index_t &_bondid) | 
|---|
| 100 | { | 
|---|
| 101 | GLWorldScene::BondNodeParentMap_t::left_const_iterator leftiter = | 
|---|
| 102 | _BondNodeParentMaps[0].left.find(_bondid); | 
|---|
| 103 | GLWorldScene::BondNodeParentMap_t::left_const_iterator rightiter = | 
|---|
| 104 | _BondNodeParentMaps[1].left.find(_bondid); | 
|---|
| 105 | ASSERT( leftiter != _BondNodeParentMaps[0].left.end(), | 
|---|
| 106 | "checkBondRemoval() - left parent to index "+toString(_bondid)+" missing?"); | 
|---|
| 107 | ASSERT( rightiter != _BondNodeParentMaps[1].left.end(), | 
|---|
| 108 | "checkBondRemoval() - right parent to index "+toString(_bondid)+" missing?"); | 
|---|
| 109 | return ((leftiter->second == (ObservedValue_Index_t)NULL) | 
|---|
| 110 | && (rightiter->second == (ObservedValue_Index_t)NULL) | 
|---|
| 111 | && (_ToBeRemovedNodes.count(_bondid))); | 
|---|
| 112 | } | 
|---|
| 113 |  | 
|---|
| 114 | void removeBondFromScene( | 
|---|
| 115 | GLWorldScene::BondNodeMap &_BondsinSceneMap, | 
|---|
| 116 | const ObservedValue_Index_t &_bondid) | 
|---|
| 117 | { | 
|---|
| 118 | std::pair<GLWorldScene::BondNodeMap::iterator, GLWorldScene::BondNodeMap::iterator> iters = | 
|---|
| 119 | _BondsinSceneMap.equal_range(_bondid); | 
|---|
| 120 | ASSERT( iters.first != iters.second, | 
|---|
| 121 | "removeBondFromScene() - could not find bond to id "+toString(_bondid)); | 
|---|
| 122 | for (GLWorldScene::BondNodeMap::iterator iter = iters.first; iter != iters.second; ++iter) { | 
|---|
| 123 | GLMoleculeObject_bond *bondObject = iter->second; | 
|---|
| 124 | delete bondObject; // is done by signal from bond itself | 
|---|
| 125 | //LOG(4, "INFO: Still present bonds " << BondsinSceneMap << "."); | 
|---|
| 126 | } | 
|---|
| 127 | _BondsinSceneMap.erase(_bondid); | 
|---|
| 128 | } | 
|---|
| 129 |  | 
|---|
| 130 | static bool checkAtomRemoval( | 
|---|
| 131 | const GLWorldScene::AtomNodeParentMap_t &_AtomNodeParentMap, | 
|---|
| 132 | const GLWorldScene::ToBeRemovedNodes_t &_ToBeRemovedNodes, | 
|---|
| 133 | const ObservedValue_Index_t &_atomid) | 
|---|
| 134 | { | 
|---|
| 135 | GLWorldScene::AtomNodeParentMap_t::left_const_iterator iter = _AtomNodeParentMap.left.find(_atomid); | 
|---|
| 136 | ASSERT( iter != _AtomNodeParentMap.left.end(), | 
|---|
| 137 | "checkAtomRemoval() - missing parent entry for "+toString(_atomid)); | 
|---|
| 138 | return ((iter->second == (ObservedValue_Index_t)NULL) && (_ToBeRemovedNodes.count(_atomid))); | 
|---|
| 139 | } | 
|---|
| 140 |  | 
|---|
| 141 | // class definitions | 
|---|
| 142 |  | 
|---|
| 143 | GLWorldScene::GLWorldScene( | 
|---|
| 144 | QtObservedInstanceBoard * _board, | 
|---|
| 145 | QObject *parent) : | 
|---|
| 146 | QObject(parent), | 
|---|
| 147 | BondNodeParentMaps(std::vector<BondNodeParentMap_t>(2)), | 
|---|
| 148 | selectionMode(SelectAtom), | 
|---|
| 149 | board(_board) | 
|---|
| 150 | { | 
|---|
| 151 | qRegisterMetaType<atomId_t>("atomId_t"); | 
|---|
| 152 | qRegisterMetaType<GLMoleculeObject_bond::SideOfBond>("GLMoleculeObject_bond::SideOfBond"); | 
|---|
| 153 |  | 
|---|
| 154 | int sphereDetails[] = {5, 3, 2, 0}; | 
|---|
| 155 | int cylinderDetails[] = {16, 8, 6, 3}; | 
|---|
| 156 | int arrowDetails[] = {8, 5, 3, 0}; | 
|---|
| 157 | for (int i=0;i<GLMoleculeObject::DETAILTYPES_MAX;i++){ | 
|---|
| 158 | QGLBuilder emptyBuilder; | 
|---|
| 159 | GLMoleculeObject::meshEmpty[i] = emptyBuilder.finalizedSceneNode(); | 
|---|
| 160 | QGLBuilder sphereBuilder; | 
|---|
| 161 | sphereBuilder << QGLSphere(2.0, sphereDetails[i]); | 
|---|
| 162 | GLMoleculeObject::meshSphere[i] = sphereBuilder.finalizedSceneNode(); | 
|---|
| 163 | GLMoleculeObject::meshSphere[i]->setOption(QGLSceneNode::CullBoundingBox, true); | 
|---|
| 164 | QGLBuilder cylinderBuilder; | 
|---|
| 165 | cylinderBuilder << QGLCylinder(.25,.25,1.0,cylinderDetails[i]); | 
|---|
| 166 | GLMoleculeObject::meshCylinder[i] = cylinderBuilder.finalizedSceneNode(); | 
|---|
| 167 | GLMoleculeObject::meshCylinder[i]->setOption(QGLSceneNode::CullBoundingBox, true); | 
|---|
| 168 | { | 
|---|
| 169 | QGLBuilder builderCyl; | 
|---|
| 170 | builderCyl << QGLCylinder(.15,.15,1.6,arrowDetails[i]); | 
|---|
| 171 | QGLSceneNode *cyl = builderCyl.finalizedSceneNode(); | 
|---|
| 172 | QGLBuilder builderCone; | 
|---|
| 173 | builderCone << QGLCylinder(0,.4,0.4,arrowDetails[i]); | 
|---|
| 174 | QGLSceneNode *cone = builderCone.finalizedSceneNode(); | 
|---|
| 175 | { | 
|---|
| 176 | QMatrix4x4 mat; | 
|---|
| 177 | mat.translate(0.0f, 0.0f, 1.0f); | 
|---|
| 178 | cone->setLocalTransform(mat); | 
|---|
| 179 | } | 
|---|
| 180 | GLMoleculeObject::meshArrow[i] = new QGLSceneNode(this); | 
|---|
| 181 | GLMoleculeObject::meshArrow[i]->addNode(cyl); | 
|---|
| 182 | GLMoleculeObject::meshArrow[i]->addNode(cone); | 
|---|
| 183 | } | 
|---|
| 184 | GLMoleculeObject::meshArrow[i]->setOption(QGLSceneNode::CullBoundingBox, true); | 
|---|
| 185 | } | 
|---|
| 186 | connect(board, SIGNAL(moleculeInserted(QtObservedMolecule::ptr)), | 
|---|
| 187 | this, SLOT(insertMolecule(QtObservedMolecule::ptr))); | 
|---|
| 188 | connect(board, SIGNAL(moleculeRemoved(ObservedValue_Index_t)), | 
|---|
| 189 | this, SLOT(removeMolecule(ObservedValue_Index_t))); | 
|---|
| 190 |  | 
|---|
| 191 | connect(board, SIGNAL(atomInserted(QtObservedAtom::ptr)), | 
|---|
| 192 | this, SLOT(connectAtom(QtObservedAtom::ptr)), Qt::DirectConnection); | 
|---|
| 193 | connect(this, SIGNAL(atomConnected(QtObservedAtom::ptr)), | 
|---|
| 194 | this, SLOT(insertAtom(QtObservedAtom::ptr))); | 
|---|
| 195 | connect(board, SIGNAL(atomRemoved(ObservedValue_Index_t)), | 
|---|
| 196 | this, SLOT(removeAtom(ObservedValue_Index_t))); | 
|---|
| 197 |  | 
|---|
| 198 | connect(board, SIGNAL(bondInserted(QtObservedBond::ptr)), | 
|---|
| 199 | this, SLOT(connectBond(QtObservedBond::ptr)), Qt::DirectConnection); | 
|---|
| 200 | connect(this, SIGNAL(bondConnected(QtObservedBond::ptr)), | 
|---|
| 201 | this, SLOT(insertBond(QtObservedBond::ptr))); | 
|---|
| 202 | connect(board, SIGNAL(bondRemoved(ObservedValue_Index_t)), | 
|---|
| 203 | this, SLOT(removeBond(ObservedValue_Index_t))); | 
|---|
| 204 |  | 
|---|
| 205 | //  connect(this, SIGNAL(updated()), this, SLOT(update())); | 
|---|
| 206 | } | 
|---|
| 207 |  | 
|---|
| 208 | GLWorldScene::~GLWorldScene() | 
|---|
| 209 | { | 
|---|
| 210 | // remove all elements | 
|---|
| 211 | GLMoleculeObject::cleanMaterialMap(); | 
|---|
| 212 | } | 
|---|
| 213 |  | 
|---|
| 214 | void GLWorldScene::clickAtom(atomId_t no) | 
|---|
| 215 | { | 
|---|
| 216 | LOG(3, "INFO: GLWorldScene - atom " << no << " has been clicked."); | 
|---|
| 217 | const atom * const Walker = const_cast<const World &>(World::getInstance()). | 
|---|
| 218 | getAtom(AtomById(no)); | 
|---|
| 219 | ASSERT( Walker != NULL, | 
|---|
| 220 | "GLWorldScene::clickAtom() - clicked atom has disappeared."); | 
|---|
| 221 | if (selectionMode == SelectAtom){ | 
|---|
| 222 | if (!World::getInstance().isSelected(Walker)) | 
|---|
| 223 | SelectionAtomById(std::vector<atomId_t>(1,no)); | 
|---|
| 224 | else | 
|---|
| 225 | SelectionNotAtomById(std::vector<atomId_t>(1,no)); | 
|---|
| 226 | }else if (selectionMode == SelectMolecule){ | 
|---|
| 227 | const molecule *mol = Walker->getMolecule(); | 
|---|
| 228 | ASSERT(mol, "Atom without molecule has been clicked."); | 
|---|
| 229 | molids_t ids(1, mol->getId()); | 
|---|
| 230 | if (!World::getInstance().isSelected(mol)) | 
|---|
| 231 | SelectionMoleculeById(ids); | 
|---|
| 232 | else | 
|---|
| 233 | SelectionNotMoleculeById(ids); | 
|---|
| 234 | } | 
|---|
| 235 | emit clicked(no); | 
|---|
| 236 | } | 
|---|
| 237 |  | 
|---|
| 238 | /** Prepares adding an atom to the scene | 
|---|
| 239 | * | 
|---|
| 240 | * We need to listen to moleculeChanged() in order to properly re-parent() | 
|---|
| 241 | * this QObject | 
|---|
| 242 | * | 
|---|
| 243 | * @param _atom atom to connect | 
|---|
| 244 | */ | 
|---|
| 245 | void GLWorldScene::connectAtom(QtObservedAtom::ptr _atom) | 
|---|
| 246 | { | 
|---|
| 247 | boost::recursive_mutex::scoped_lock lock(Atom_mutex); | 
|---|
| 248 |  | 
|---|
| 249 | connect(_atom.get(), SIGNAL(moleculeChanged()), this, SLOT(reparentAtom())); | 
|---|
| 250 |  | 
|---|
| 251 | // store the object, as we need it on reparenting | 
|---|
| 252 | const ObservedValue_Index_t atomid = _atom->getIndex(); | 
|---|
| 253 | #ifndef NDEBUG | 
|---|
| 254 | std::pair< ObservedAtoms_t::iterator, bool > inserter = | 
|---|
| 255 | #endif | 
|---|
| 256 | ObservedAtoms.insert( std::make_pair(_atom.get(), _atom) ); | 
|---|
| 257 | ASSERT( inserter.second, | 
|---|
| 258 | "GLWorldScene::connectAtom() - observed atom "+toString(_atom)+" already stored?"); | 
|---|
| 259 | #ifndef NDEBUG | 
|---|
| 260 | std::pair< ObservedValueIndexLookup_t::iterator, bool > indexinserter = | 
|---|
| 261 | #endif | 
|---|
| 262 | ObservedAtomIndexLookup.insert( std::make_pair(atomid, _atom.get()) ); | 
|---|
| 263 | ASSERT( indexinserter.second, | 
|---|
| 264 | "GLWorldScene::connectAtom() - observed atom's index " | 
|---|
| 265 | +toString(atomid)+" already stored?"); | 
|---|
| 266 |  | 
|---|
| 267 | // and create entry in the parent map | 
|---|
| 268 | AtomNodeParentMap.left.insert( std::make_pair(atomid, (ObservedValue_Index_t)NULL) ); | 
|---|
| 269 |  | 
|---|
| 270 | emit atomConnected(_atom); | 
|---|
| 271 | } | 
|---|
| 272 |  | 
|---|
| 273 | /** Adds an atom to the scene. | 
|---|
| 274 | * | 
|---|
| 275 | * @param _atom atom to add | 
|---|
| 276 | */ | 
|---|
| 277 | void GLWorldScene::insertAtom(QtObservedAtom::ptr _atom) | 
|---|
| 278 | { | 
|---|
| 279 | LOG(3, "INFO: GLWorldScene: Received signal atomInserted for atom " | 
|---|
| 280 | << _atom->getAtomIndex()); | 
|---|
| 281 | boost::recursive_mutex::scoped_lock lock(Atom_mutex); | 
|---|
| 282 |  | 
|---|
| 283 | const ObservedValue_Index_t atomid = _atom->getIndex(); | 
|---|
| 284 | QObject *parent = static_cast<QObject *>(this); | 
|---|
| 285 | GLMoleculeObject_atom *atomObject = NULL; | 
|---|
| 286 | { | 
|---|
| 287 | AtomNodeParentMap_t::left_iterator parentiter = AtomNodeParentMap.left.find(atomid); | 
|---|
| 288 | ASSERT (parentiter != AtomNodeParentMap.left.end(), | 
|---|
| 289 | "GLWorldScene::insertAtom() - parent to atom id "+toString(atomid)+" unknown?"); | 
|---|
| 290 | const ObservedValue_Index_t parentindex = parentiter->second; | 
|---|
| 291 | if (parentindex != (ObservedValue_Index_t)NULL) { | 
|---|
| 292 | const MoleculeNodeMap::iterator moliter = MoleculesinSceneMap.find(parentindex); | 
|---|
| 293 | if (moliter != MoleculesinSceneMap.end()) | 
|---|
| 294 | parent = moliter->second; | 
|---|
| 295 | } | 
|---|
| 296 |  | 
|---|
| 297 | atomObject = new GLMoleculeObject_atom( | 
|---|
| 298 | GLMoleculeObject::meshSphere, | 
|---|
| 299 | GLMoleculeObject::meshArrow, | 
|---|
| 300 | parent, | 
|---|
| 301 | _atom); | 
|---|
| 302 | ASSERT( atomObject != NULL, | 
|---|
| 303 | "GLWorldScene::atomInserted - could not create atom object for " | 
|---|
| 304 | +toString(_atom->getAtomIndex())); | 
|---|
| 305 | } | 
|---|
| 306 | #ifndef NDEBUG | 
|---|
| 307 | const AtomNodeMap::iterator iter = AtomsinSceneMap.find(atomid); | 
|---|
| 308 | #endif | 
|---|
| 309 | ASSERT(iter == AtomsinSceneMap.end(), | 
|---|
| 310 | "GLWorldScene::insertAtom - same atom with id " | 
|---|
| 311 | +toString(_atom->getAtomIndex())+" added again."); | 
|---|
| 312 | AtomsinSceneMap.insert( make_pair(atomid, atomObject) ); | 
|---|
| 313 |  | 
|---|
| 314 | connect (atomObject, SIGNAL(clicked(atomId_t)), this, SLOT(clickAtom(atomId_t))); | 
|---|
| 315 | connect (atomObject, SIGNAL(changed()), this, SIGNAL(changed())); | 
|---|
| 316 | connect (atomObject, SIGNAL(hoverChanged(GLMoleculeObject *)), this, SIGNAL(changed())); | 
|---|
| 317 | connect (atomObject, SIGNAL(hoverChanged(GLMoleculeObject *)), this, SLOT(hoverChangedSignalled(GLMoleculeObject *))); | 
|---|
| 318 |  | 
|---|
| 319 | emit changed(); | 
|---|
| 320 | emit changeOccured(); | 
|---|
| 321 | } | 
|---|
| 322 |  | 
|---|
| 323 | template <class T> | 
|---|
| 324 | void removeStoredObservedValue( | 
|---|
| 325 | GLWorldScene::ObservedValueIndexLookup_t &_ObservedValueIndexLookup, | 
|---|
| 326 | T &_ObservedValues, | 
|---|
| 327 | const ObservedValue_Index_t &_id) | 
|---|
| 328 | { | 
|---|
| 329 | // get QObject* from lookup | 
|---|
| 330 | const GLWorldScene::ObservedValueIndexLookup_t::iterator iter = | 
|---|
| 331 | _ObservedValueIndexLookup.find(_id); | 
|---|
| 332 | ASSERT( iter != _ObservedValueIndexLookup.end(), | 
|---|
| 333 | "removeStoredObservedValue() - could not find index "+toString(_id) | 
|---|
| 334 | +" to stored observed value."); | 
|---|
| 335 | QObject * sender = iter->second; | 
|---|
| 336 | const size_t erased = _ObservedValues.erase(sender); | 
|---|
| 337 | ASSERT( erased == 1, | 
|---|
| 338 | "removeStoredObservedValue() - could not erase stored observed value " | 
|---|
| 339 | +toString(_id)); | 
|---|
| 340 | _ObservedValueIndexLookup.erase(iter); | 
|---|
| 341 | } | 
|---|
| 342 |  | 
|---|
| 343 | void removeAtomFromScene( | 
|---|
| 344 | GLWorldScene::AtomNodeMap &_AtomsinSceneMap, | 
|---|
| 345 | const ObservedValue_Index_t &_atomid) | 
|---|
| 346 | { | 
|---|
| 347 | GLWorldScene::AtomNodeMap::iterator iter = _AtomsinSceneMap.find(_atomid); | 
|---|
| 348 | ASSERT(iter != _AtomsinSceneMap.end(), | 
|---|
| 349 | "removeAtomFromScene() - atom "+toString(_atomid)+" not on display."); | 
|---|
| 350 | GLMoleculeObject_atom *atomObject = iter->second; | 
|---|
| 351 | _AtomsinSceneMap.erase(iter); | 
|---|
| 352 | delete atomObject; | 
|---|
| 353 | } | 
|---|
| 354 |  | 
|---|
| 355 | void GLWorldScene::checkAndRemoveAtom(const ObservedValue_Index_t &_atomid) | 
|---|
| 356 | { | 
|---|
| 357 | boost::recursive_mutex::scoped_lock lock(Atom_mutex); | 
|---|
| 358 |  | 
|---|
| 359 | if (checkAtomRemoval(AtomNodeParentMap, ToBeRemovedNodes, _atomid)) { | 
|---|
| 360 | LOG(4, "DEBUG: Found parent of to be removed atom as GLWorldScene, removing."); | 
|---|
| 361 | removeAtomFromScene(AtomsinSceneMap, _atomid); | 
|---|
| 362 | AtomNodeParentMap.left.erase(_atomid); | 
|---|
| 363 | ToBeRemovedNodes.erase(_atomid); | 
|---|
| 364 | removeStoredObservedValue(ObservedAtomIndexLookup, ObservedAtoms, _atomid); | 
|---|
| 365 | } | 
|---|
| 366 | } | 
|---|
| 367 |  | 
|---|
| 368 | /** Removes an atom. | 
|---|
| 369 | * | 
|---|
| 370 | * @param _atomid index of the atom that is removed | 
|---|
| 371 | */ | 
|---|
| 372 | void GLWorldScene::removeAtom(ObservedValue_Index_t _atomid) | 
|---|
| 373 | { | 
|---|
| 374 | LOG(3, "INFO: GLWorldScene: Received signal atomRemoved for atom "+toString(_atomid)+"."); | 
|---|
| 375 | // bonds are removed by signal coming from ~bond | 
|---|
| 376 | boost::recursive_mutex::scoped_lock lock(Atom_mutex); | 
|---|
| 377 |  | 
|---|
| 378 | ASSERT( ToBeRemovedNodes.count(_atomid) == 0, | 
|---|
| 379 | "GLWorldScene::removeAtom() - removedAtom signal for id "+toString(_atomid) | 
|---|
| 380 | +" came in twice?"); | 
|---|
| 381 | ToBeRemovedNodes.insert( _atomid ); | 
|---|
| 382 |  | 
|---|
| 383 | // remove atoms | 
|---|
| 384 | checkAndRemoveAtom( _atomid ); | 
|---|
| 385 |  | 
|---|
| 386 | emit changed(); | 
|---|
| 387 | emit changeOccured(); | 
|---|
| 388 | } | 
|---|
| 389 |  | 
|---|
| 390 | /** Prepares adding a bond to the scene | 
|---|
| 391 | * | 
|---|
| 392 | * We need to listen to moleculeChanged() in order to properly re-parent() | 
|---|
| 393 | * this QObject | 
|---|
| 394 | * | 
|---|
| 395 | * @param _bond bond to connect | 
|---|
| 396 | */ | 
|---|
| 397 | void GLWorldScene::connectBond(QtObservedBond::ptr _bond) | 
|---|
| 398 | { | 
|---|
| 399 | LOG(3, "INFO: GLWorldScene::connectBond() - connecting to bonds " << _bond->getBondIndex()); | 
|---|
| 400 |  | 
|---|
| 401 | connect(_bond.get(), SIGNAL(leftmoleculeChanged()), this, SLOT(reparentBondLeft()), Qt::QueuedConnection); | 
|---|
| 402 | connect(_bond.get(), SIGNAL(rightmoleculeChanged()), this, SLOT(reparentBondRight()), Qt::QueuedConnection); | 
|---|
| 403 |  | 
|---|
| 404 | boost::recursive_mutex::scoped_lock lock(Bond_mutex); | 
|---|
| 405 |  | 
|---|
| 406 | // store the object, as we need it on reparenting | 
|---|
| 407 | const ObservedValue_Index_t bondid = _bond->getIndex(); | 
|---|
| 408 | { | 
|---|
| 409 | #ifndef NDEBUG | 
|---|
| 410 | std::pair< ObservedBonds_t::iterator, bool > inserter = | 
|---|
| 411 | #endif | 
|---|
| 412 | ObservedBonds.insert( std::make_pair(_bond.get(), _bond) ); | 
|---|
| 413 | ASSERT( inserter.second, | 
|---|
| 414 | "GLWorldScene::connectBond() - observed bond "+toString(_bond)+" already stored?"); | 
|---|
| 415 | #ifndef NDEBUG | 
|---|
| 416 | std::pair< ObservedValueIndexLookup_t::iterator, bool > indexinserter = | 
|---|
| 417 | #endif | 
|---|
| 418 | ObservedBondIndexLookup.insert( std::make_pair(bondid, _bond.get()) ); | 
|---|
| 419 | ASSERT( indexinserter.second, | 
|---|
| 420 | "GLWorldScene::connectBond() - observed bond's index " | 
|---|
| 421 | +toString(bondid)+" already stored?"); | 
|---|
| 422 | } | 
|---|
| 423 | { | 
|---|
| 424 | if ((_bond->getLeftAtom() != NULL) && (_bond->getLeftAtom()->getMoleculeRef() != NULL)) | 
|---|
| 425 | BondNodeParentMaps[0].left.insert( | 
|---|
| 426 | std::make_pair(bondid, _bond->getLeftAtom()->getMoleculeRef()->getIndex()) ); | 
|---|
| 427 | else | 
|---|
| 428 | BondNodeParentMaps[0].left.insert( | 
|---|
| 429 | std::make_pair(bondid, (ObservedValue_Index_t)NULL) ); | 
|---|
| 430 | if ((_bond->getRightAtom() != NULL) && (_bond->getRightAtom()->getMoleculeRef() != NULL)) | 
|---|
| 431 | BondNodeParentMaps[1].left.insert( | 
|---|
| 432 | std::make_pair(bondid, _bond->getRightAtom()->getMoleculeRef()->getIndex()) ); | 
|---|
| 433 | else | 
|---|
| 434 | BondNodeParentMaps[1].left.insert( | 
|---|
| 435 | std::make_pair(bondid, (ObservedValue_Index_t)NULL) ); | 
|---|
| 436 | } | 
|---|
| 437 |  | 
|---|
| 438 | emit bondConnected(_bond); | 
|---|
| 439 | } | 
|---|
| 440 |  | 
|---|
| 441 | /** Adds a bond to the scene. | 
|---|
| 442 | * | 
|---|
| 443 | * @param _bond bond to add | 
|---|
| 444 | */ | 
|---|
| 445 | void GLWorldScene::insertBond(QtObservedBond::ptr _bond) | 
|---|
| 446 | { | 
|---|
| 447 | boost::recursive_mutex::scoped_lock lock(Bond_mutex); | 
|---|
| 448 |  | 
|---|
| 449 | const std::vector< GLMoleculeObject_bond::SideOfBond > bondsides = | 
|---|
| 450 | boost::assign::list_of<GLMoleculeObject_bond::SideOfBond> | 
|---|
| 451 | (GLMoleculeObject_bond::left) | 
|---|
| 452 | (GLMoleculeObject_bond::right); | 
|---|
| 453 | LOG(3, "INFO: GLWorldScene::insertBond() - Adding bonds " << _bond->getBondIndex()); | 
|---|
| 454 |  | 
|---|
| 455 | const ObservedValue_Index_t bondid = _bond->getIndex(); | 
|---|
| 456 | #ifdef NDEBUG | 
|---|
| 457 | BondNodeMap::iterator iter = BondsinSceneMap.find(bondid); | 
|---|
| 458 | ASSERT( iter == BondsinSceneMap.end(), | 
|---|
| 459 | "GLWorldScene::insertBond() - bond "+toString(bondid)+" is already known."); | 
|---|
| 460 | #endif | 
|---|
| 461 | { | 
|---|
| 462 | for (size_t i=0;i<2;++i) { | 
|---|
| 463 | BondNodeParentMap_t::left_iterator parentiter = BondNodeParentMaps[i].left.find(bondid); | 
|---|
| 464 | ASSERT (parentiter != BondNodeParentMaps[i].left.end(), | 
|---|
| 465 | "GLWorldScene::insertBond() - parent to bond id "+toString(bondid)+" unknown?"); | 
|---|
| 466 | QObject *parent = this; | 
|---|
| 467 | if (parentiter->second != (ObservedValue_Index_t)NULL) { | 
|---|
| 468 | const MoleculeNodeMap::iterator moliter = MoleculesinSceneMap.find(parentiter->second); | 
|---|
| 469 | if (moliter != MoleculesinSceneMap.end()) | 
|---|
| 470 | parent = moliter->second; | 
|---|
| 471 | } | 
|---|
| 472 |  | 
|---|
| 473 | GLMoleculeObject_bond *bondObject = new GLMoleculeObject_bond( | 
|---|
| 474 | GLMoleculeObject::meshCylinder, | 
|---|
| 475 | parent, | 
|---|
| 476 | _bond, | 
|---|
| 477 | bondsides[i]); | 
|---|
| 478 | connect (bondObject, SIGNAL(changed()), this, SIGNAL(changed())); | 
|---|
| 479 | BondsinSceneMap.insert( std::make_pair(bondid, bondObject) ); | 
|---|
| 480 | } | 
|---|
| 481 | } | 
|---|
| 482 |  | 
|---|
| 483 | emit changed(); | 
|---|
| 484 | emit changeOccured(); | 
|---|
| 485 | } | 
|---|
| 486 |  | 
|---|
| 487 | void GLWorldScene::checkAndRemoveBond(const ObservedValue_Index_t &_bondid) | 
|---|
| 488 | { | 
|---|
| 489 | boost::recursive_mutex::scoped_lock lock(Bond_mutex); | 
|---|
| 490 |  | 
|---|
| 491 | if (checkBondRemoval(BondNodeParentMaps, ToBeRemovedNodes, _bondid)) { | 
|---|
| 492 | LOG(3, "DEBUG: Found both parents of to be removed bond " << _bondid << " as GLWorldScene, removing."); | 
|---|
| 493 | removeBondFromScene(BondsinSceneMap, _bondid); | 
|---|
| 494 | removeStoredObservedValue(ObservedBondIndexLookup, ObservedBonds, _bondid); | 
|---|
| 495 | eraseBondNodeParents(BondNodeParentMaps, _bondid); | 
|---|
| 496 | ToBeRemovedNodes.erase(_bondid); | 
|---|
| 497 | } | 
|---|
| 498 | } | 
|---|
| 499 |  | 
|---|
| 500 | /** Removes a bond. | 
|---|
| 501 | * | 
|---|
| 502 | * @param _bondid id of bond to remove | 
|---|
| 503 | */ | 
|---|
| 504 | void GLWorldScene::removeBond(ObservedValue_Index_t _bondid) | 
|---|
| 505 | { | 
|---|
| 506 | boost::recursive_mutex::scoped_lock lock(Bond_mutex); | 
|---|
| 507 | LOG(3, "INFO: GLWorldScene::removedBond() - got bondRemoved signal from board for id " << _bondid); | 
|---|
| 508 |  | 
|---|
| 509 | // check current state of associated molecules: We won't receive any | 
|---|
| 510 | // moleculeChanged() here anymore, as this QtObservedBond is already | 
|---|
| 511 | // disconnected from its bond object | 
|---|
| 512 | { | 
|---|
| 513 | const ObservedValueIndexLookup_t::const_iterator objiter = ObservedBondIndexLookup.find(_bondid); | 
|---|
| 514 | ASSERT( objiter != ObservedBondIndexLookup.end(), | 
|---|
| 515 | "GLWorldScene::removeBond() - bond's index "+toString(_bondid) | 
|---|
| 516 | +" to be removed not stored yet?"); | 
|---|
| 517 | QObject *obj = objiter->second; | 
|---|
| 518 | const ObservedBonds_t::const_iterator observediter = ObservedBonds.find(obj); | 
|---|
| 519 | ASSERT( observediter != ObservedBonds.end(), | 
|---|
| 520 | "GLWorldScene::removeBond() - bond "+toString(_bondid) | 
|---|
| 521 | +" to be removed not stored yet??"); | 
|---|
| 522 | const QtObservedBond::ptr &_bond = observediter->second; | 
|---|
| 523 | if (_bond->getLeftMoleculeIndex() != (moleculeId_t)-1) { | 
|---|
| 524 | BondNodeParentMaps[0].left.erase(_bondid); | 
|---|
| 525 | BondNodeParentMaps[0].left.insert( std::make_pair( _bondid, (ObservedValue_Index_t)NULL) ); | 
|---|
| 526 | resetParent(getBondInScene(_bondid, GLMoleculeObject_bond::left), NULL); | 
|---|
| 527 | } | 
|---|
| 528 | if (_bond->getRightMoleculeIndex() != (moleculeId_t)-1) { | 
|---|
| 529 | BondNodeParentMaps[1].left.erase(_bondid); | 
|---|
| 530 | BondNodeParentMaps[1].left.insert( std::make_pair( _bondid, (ObservedValue_Index_t)NULL) ); | 
|---|
| 531 | resetParent(getBondInScene(_bondid, GLMoleculeObject_bond::right), NULL); | 
|---|
| 532 | } | 
|---|
| 533 | } | 
|---|
| 534 |  | 
|---|
| 535 | ASSERT( ToBeRemovedNodes.find(_bondid) == ToBeRemovedNodes.end(), | 
|---|
| 536 | "GLWorldScene::removeBond() - bondRemoved for "+toString(_bondid)+" obtained twice?"); | 
|---|
| 537 | ToBeRemovedNodes.insert( _bondid ); | 
|---|
| 538 |  | 
|---|
| 539 | // check whether node has still non-NULL parents, otherwise remove completely | 
|---|
| 540 | checkAndRemoveBond( _bondid ); | 
|---|
| 541 |  | 
|---|
| 542 | emit changed(); | 
|---|
| 543 | emit changeOccured(); | 
|---|
| 544 | } | 
|---|
| 545 |  | 
|---|
| 546 | void GLWorldScene::hoverChangedSignalled(GLMoleculeObject *ob) | 
|---|
| 547 | { | 
|---|
| 548 | boost::recursive_mutex::scoped_lock lock(Atom_mutex); | 
|---|
| 549 | // Find the atom, ob corresponds to. | 
|---|
| 550 | hoverAtomId = -1; | 
|---|
| 551 | GLMoleculeObject_atom *atomObject = dynamic_cast<GLMoleculeObject_atom *>(ob); | 
|---|
| 552 | if (atomObject){ | 
|---|
| 553 | for (AtomNodeMap::iterator iter = AtomsinSceneMap.begin();iter != AtomsinSceneMap.end(); ++ iter){ | 
|---|
| 554 | if (iter->second == atomObject) | 
|---|
| 555 | hoverAtomId = iter->second->objectId(); | 
|---|
| 556 | } | 
|---|
| 557 |  | 
|---|
| 558 | // Propagate signal. | 
|---|
| 559 | emit hoverChanged(hoverAtomId); | 
|---|
| 560 | } else { | 
|---|
| 561 | // Find the atom, ob corresponds to. | 
|---|
| 562 | GLMoleculeObject_molecule *moleculeObject = dynamic_cast<GLMoleculeObject_molecule *>(ob); | 
|---|
| 563 | if (moleculeObject){ | 
|---|
| 564 | // Propagate signal. | 
|---|
| 565 | emit hoverChanged(moleculeObject->objectId(), 0); | 
|---|
| 566 | } | 
|---|
| 567 | } | 
|---|
| 568 | } | 
|---|
| 569 |  | 
|---|
| 570 | void GLWorldScene::clickMolecule(moleculeId_t no) | 
|---|
| 571 | { | 
|---|
| 572 | LOG(3, "INFO: GLMoleculeObject_molecule - mol " << no << " has been clicked."); | 
|---|
| 573 | const molecule * const mol= const_cast<const World &>(World::getInstance()). | 
|---|
| 574 | getMolecule(MoleculeById(no)); | 
|---|
| 575 | ASSERT(mol, "Atom without molecule has been clicked."); | 
|---|
| 576 | molids_t ids(1, mol->getId()); | 
|---|
| 577 | if (!World::getInstance().isSelected(mol)) | 
|---|
| 578 | SelectionMoleculeById(ids); | 
|---|
| 579 | else | 
|---|
| 580 | SelectionNotMoleculeById(ids); | 
|---|
| 581 | emit clicked(no); | 
|---|
| 582 | } | 
|---|
| 583 |  | 
|---|
| 584 | /** Inserts a molecule into the scene. | 
|---|
| 585 | * | 
|---|
| 586 | * @param _mol molecule to insert | 
|---|
| 587 | */ | 
|---|
| 588 | void GLWorldScene::insertMolecule(QtObservedMolecule::ptr _mol) | 
|---|
| 589 | { | 
|---|
| 590 | const ObservedValue_Index_t molid = _mol->getIndex(); | 
|---|
| 591 | LOG(3, "INFO: GLWorldScene: Received signal moleculeInserted for molecule " | 
|---|
| 592 | << _mol->getMolIndex()); | 
|---|
| 593 |  | 
|---|
| 594 | MoleculeNodeMap::const_iterator iter = MoleculesinSceneMap.find(molid); | 
|---|
| 595 | ASSERT( iter == MoleculesinSceneMap.end(), | 
|---|
| 596 | "GLWorldScene::insertMolecule() - molecule's id "+toString(molid) | 
|---|
| 597 | +" already present."); | 
|---|
| 598 |  | 
|---|
| 599 | // add new object | 
|---|
| 600 | LOG(1, "DEBUG: Adding GLMoleculeObject_molecule to id " << molid); | 
|---|
| 601 | GLMoleculeObject_molecule *molObject = | 
|---|
| 602 | new GLMoleculeObject_molecule( | 
|---|
| 603 | GLMoleculeObject::meshEmpty, | 
|---|
| 604 | this, | 
|---|
| 605 | _mol); | 
|---|
| 606 | ASSERT( molObject != NULL, | 
|---|
| 607 | "GLWorldScene::insertMolecule - could not create molecule object for " | 
|---|
| 608 | +toString(molid)); | 
|---|
| 609 | #ifndef NDEBUG | 
|---|
| 610 | foreach (QObject *obj, molObject->children()) { | 
|---|
| 611 | GLMoleculeObject *meshobj = qobject_cast<GLMoleculeObject *>(obj); | 
|---|
| 612 | ASSERT( meshobj == NULL, | 
|---|
| 613 | "GLWorldScene::insertMolecule() - there are already atoms or bonds attached to a to molecule."); | 
|---|
| 614 | } | 
|---|
| 615 | #endif | 
|---|
| 616 |  | 
|---|
| 617 | // check all atoms for not yet assigned parents | 
|---|
| 618 | { | 
|---|
| 619 | boost::recursive_mutex::scoped_lock lock(Atom_mutex); | 
|---|
| 620 | std::pair<AtomNodeParentMap_t::right_const_iterator, AtomNodeParentMap_t::right_const_iterator> iters = | 
|---|
| 621 | AtomNodeParentMap.right.equal_range(molid); | 
|---|
| 622 | for (AtomNodeParentMap_t::right_const_iterator iter = iters.first; | 
|---|
| 623 | iter != iters.second; ++iter) { | 
|---|
| 624 | AtomNodeMap::const_iterator atomiter = AtomsinSceneMap.find(iter->second); | 
|---|
| 625 | if (atomiter != AtomsinSceneMap.end()) | 
|---|
| 626 | resetParent(atomiter->second, molObject); | 
|---|
| 627 | } | 
|---|
| 628 | } | 
|---|
| 629 | // check all bonds for not yet assigned parents | 
|---|
| 630 | { | 
|---|
| 631 | boost::recursive_mutex::scoped_lock lock(Bond_mutex); | 
|---|
| 632 | for (size_t i=0;i<2;++i) { | 
|---|
| 633 | std::pair<BondNodeParentMap_t::right_const_iterator, BondNodeParentMap_t::right_const_iterator> iters = | 
|---|
| 634 | BondNodeParentMaps[i].right.equal_range(molid); | 
|---|
| 635 | for (BondNodeParentMap_t::right_const_iterator iter = iters.first; | 
|---|
| 636 | iter != iters.second; ++iter) { | 
|---|
| 637 | BondNodeMap::const_iterator bonditer = BondsinSceneMap.find(iter->second); | 
|---|
| 638 | if (bonditer != BondsinSceneMap.end()) | 
|---|
| 639 | resetParent(bonditer->second, molObject); | 
|---|
| 640 | } | 
|---|
| 641 | } | 
|---|
| 642 | } | 
|---|
| 643 |  | 
|---|
| 644 | #ifndef NDEBUG | 
|---|
| 645 | std::pair<MoleculeNodeMap::iterator, bool> inserter = | 
|---|
| 646 | #endif | 
|---|
| 647 | MoleculesinSceneMap.insert( make_pair(molid, molObject) ); | 
|---|
| 648 | ASSERT(inserter.second, | 
|---|
| 649 | "GLWorldScene::insertMolecule() - molecule "+toString(_mol->getMolIndex()) | 
|---|
| 650 | +" already present in scene."); | 
|---|
| 651 |  | 
|---|
| 652 | connect (molObject, SIGNAL(changed()), this, SIGNAL(changed())); | 
|---|
| 653 | connect (molObject, SIGNAL(changeOccured()), this, SIGNAL(changeOccured())); | 
|---|
| 654 |  | 
|---|
| 655 | emit changed(); | 
|---|
| 656 | emit changeOccured(); | 
|---|
| 657 | } | 
|---|
| 658 |  | 
|---|
| 659 | /** Removes a molecule from the scene. | 
|---|
| 660 | * | 
|---|
| 661 | * @param _molid index of the molecule to remove | 
|---|
| 662 | */ | 
|---|
| 663 | void GLWorldScene::removeMolecule(ObservedValue_Index_t _molid) | 
|---|
| 664 | { | 
|---|
| 665 | LOG(3, "INFO: GLWorldScene: Received signal moleculeRemoved for molecule "+toString(_molid)+"."); | 
|---|
| 666 |  | 
|---|
| 667 | MoleculeNodeMap::iterator iter = MoleculesinSceneMap.find(_molid); | 
|---|
| 668 | ASSERT ( iter != MoleculesinSceneMap.end(), | 
|---|
| 669 | "GLWorldScene::removeMolecule() - to be removed molecule "+toString(_molid) | 
|---|
| 670 | +" is already gone."); | 
|---|
| 671 | GLMoleculeObject_molecule *molObject = iter->second; | 
|---|
| 672 | // check for any atoms and bonds still attached to it | 
|---|
| 673 | #ifndef NDEBUG | 
|---|
| 674 | foreach (QObject *obj, molObject->children()) { | 
|---|
| 675 | GLMoleculeObject *meshobj = qobject_cast<GLMoleculeObject *>(obj); | 
|---|
| 676 | ASSERT( meshobj == NULL, | 
|---|
| 677 | "GLWorldScene::removeMolecule() - there are still atoms or bonds attached to a to molecule."); | 
|---|
| 678 | } | 
|---|
| 679 | #endif | 
|---|
| 680 | // finally, remove molecule | 
|---|
| 681 | delete molObject; | 
|---|
| 682 | MoleculesinSceneMap.erase(iter); | 
|---|
| 683 |  | 
|---|
| 684 | emit changed(); | 
|---|
| 685 | emit changeOccured(); | 
|---|
| 686 | } | 
|---|
| 687 |  | 
|---|
| 688 | void GLWorldScene::moleculesVisibilityChanged(ObservedValue_Index_t _id, bool _visible) | 
|---|
| 689 | { | 
|---|
| 690 | MoleculeNodeMap::iterator iter = MoleculesinSceneMap.find(_id); | 
|---|
| 691 | ASSERT( iter != MoleculesinSceneMap.end(), | 
|---|
| 692 | "GLWorldScene::moleculeInserted() - molecule's id " | 
|---|
| 693 | +toString(board->getMoleculeIdToIndex(_id))+" is unknown."); | 
|---|
| 694 |  | 
|---|
| 695 | GLMoleculeObject_molecule *molObject = iter->second; | 
|---|
| 696 | molObject->setVisible(_visible); | 
|---|
| 697 |  | 
|---|
| 698 | emit changed(); | 
|---|
| 699 | emit changeOccured(); | 
|---|
| 700 | } | 
|---|
| 701 |  | 
|---|
| 702 | /** This converts safely index into a GLMoleculeObject_molecule. | 
|---|
| 703 | * | 
|---|
| 704 | * \param _MoleculesinSceneMap all present molecules | 
|---|
| 705 | * \param _molid index to look for | 
|---|
| 706 | * \return MolObject or NULL when not found | 
|---|
| 707 | */ | 
|---|
| 708 | GLMoleculeObject_molecule *GLWorldScene::getMoleculeObject( | 
|---|
| 709 | const ObservedValue_Index_t _molid) const | 
|---|
| 710 | { | 
|---|
| 711 | const MoleculeNodeMap::const_iterator moliter = MoleculesinSceneMap.find(_molid); | 
|---|
| 712 | if (moliter != MoleculesinSceneMap.end()) | 
|---|
| 713 | return moliter->second; | 
|---|
| 714 | else | 
|---|
| 715 | return NULL; | 
|---|
| 716 | } | 
|---|
| 717 |  | 
|---|
| 718 | /** Changes the parent of an object in the scene. | 
|---|
| 719 | * | 
|---|
| 720 | * \param _id index of the object whose parent to change | 
|---|
| 721 | * \param _ob new parent | 
|---|
| 722 | */ | 
|---|
| 723 | void GLWorldScene::reparentAtom() | 
|---|
| 724 | { | 
|---|
| 725 | boost::recursive_mutex::scoped_lock lock(Atom_mutex); | 
|---|
| 726 | QObject * origin = sender(); | 
|---|
| 727 | if (origin == NULL) { | 
|---|
| 728 | ELOG(1, "Got reparentAtom() with sender being NULL."); | 
|---|
| 729 | return; | 
|---|
| 730 | } | 
|---|
| 731 | ObservedAtoms_t::const_iterator iter = ObservedAtoms.find(origin); | 
|---|
| 732 | ASSERT( iter != ObservedAtoms.end(), | 
|---|
| 733 | "GLWorldScene::reparentAtom() - atom's "+toString(origin)+" is no longer stored?"); | 
|---|
| 734 | QtObservedAtom::ptr walker = iter->second; | 
|---|
| 735 | const ObservedValue_Index_t walkerid = walker->getIndex(); | 
|---|
| 736 | LOG(4, "DEBUG: GLWorldScene: Received signal moleculeChanged for atom "+toString(walkerid)+"."); | 
|---|
| 737 | AtomNodeParentMap_t::left_iterator parentiter = AtomNodeParentMap.left.find(walkerid); | 
|---|
| 738 | ASSERT( parentiter != AtomNodeParentMap.left.end(), | 
|---|
| 739 | "GLWorldScene::reparentAtom() - could not find object to id "+toString(walkerid)); | 
|---|
| 740 |  | 
|---|
| 741 | // change parent entry | 
|---|
| 742 | AtomNodeParentMap.left.erase(parentiter); | 
|---|
| 743 | if (walker->getMoleculeRef() != NULL) | 
|---|
| 744 | AtomNodeParentMap.left.insert( std::make_pair(walkerid, walker->getMoleculeRef()->getIndex()) ); | 
|---|
| 745 | else | 
|---|
| 746 | AtomNodeParentMap.left.insert( std::make_pair(walkerid, (ObservedValue_Index_t)NULL) ); | 
|---|
| 747 | parentiter = AtomNodeParentMap.left.find(walkerid); | 
|---|
| 748 |  | 
|---|
| 749 | const AtomNodeMap::iterator atomiter = AtomsinSceneMap.find(walkerid); | 
|---|
| 750 | if (atomiter != AtomsinSceneMap.end()) | 
|---|
| 751 | resetParent(atomiter->second, getMoleculeObject(parentiter->second)); | 
|---|
| 752 | // else atom does not yet exist | 
|---|
| 753 |  | 
|---|
| 754 | // check whether node is still shown, otherwise remove completely | 
|---|
| 755 | checkAndRemoveAtom( walkerid ); | 
|---|
| 756 | } | 
|---|
| 757 |  | 
|---|
| 758 | /** Changes the parent of an left-side bond in the scene. | 
|---|
| 759 | * | 
|---|
| 760 | */ | 
|---|
| 761 | void GLWorldScene::reparentBondLeft() | 
|---|
| 762 | { | 
|---|
| 763 | boost::recursive_mutex::scoped_lock lock(Bond_mutex); | 
|---|
| 764 | QObject * origin = sender(); | 
|---|
| 765 | if (origin == NULL) { | 
|---|
| 766 | ELOG(1, "Got reparentBondLeft() with sender being NULL."); | 
|---|
| 767 | return; | 
|---|
| 768 | } | 
|---|
| 769 | ObservedBonds_t::const_iterator iter = ObservedBonds.find(origin); | 
|---|
| 770 | ASSERT( iter != ObservedBonds.end(), | 
|---|
| 771 | "GLWorldScene::reparentBondLeft() - bond "+toString(origin)+" is no longer stored?"); | 
|---|
| 772 | QtObservedBond::ptr bond = iter->second; | 
|---|
| 773 | LOG(3, "INFO: GLWorldScene::reparentBondLeft() - Reparenting left side of bond " | 
|---|
| 774 | << bond->getBondIndex() << "."); | 
|---|
| 775 | reparentBond(bond, bond->getLeftAtom(), GLMoleculeObject_bond::left); | 
|---|
| 776 |  | 
|---|
| 777 | // check whether node is still shown, otherwise remove completely | 
|---|
| 778 | checkAndRemoveBond( bond->getIndex() ); | 
|---|
| 779 | } | 
|---|
| 780 |  | 
|---|
| 781 | /** Changes the parent of an right-side bond in the scene. | 
|---|
| 782 | * | 
|---|
| 783 | */ | 
|---|
| 784 | void GLWorldScene::reparentBondRight() | 
|---|
| 785 | { | 
|---|
| 786 | boost::recursive_mutex::scoped_lock lock(Bond_mutex); | 
|---|
| 787 | QObject * origin = sender(); | 
|---|
| 788 | if (origin == NULL) { | 
|---|
| 789 | ELOG(1, "Got reparentBondRight() with sender being NULL."); | 
|---|
| 790 | return; | 
|---|
| 791 | } | 
|---|
| 792 | ObservedBonds_t::const_iterator iter = ObservedBonds.find(origin); | 
|---|
| 793 | ASSERT( iter != ObservedBonds.end(), | 
|---|
| 794 | "GLWorldScene::reparentBondRight() - bond "+toString(origin)+" is no longer stored?"); | 
|---|
| 795 | QtObservedBond::ptr bond = iter->second; | 
|---|
| 796 | LOG(3, "INFO: GLWorldScene::reparentBondRight() - Reparenting right side of bond " | 
|---|
| 797 | << bond->getBondIndex() << "."); | 
|---|
| 798 | reparentBond(bond, bond->getRightAtom(), GLMoleculeObject_bond::right); | 
|---|
| 799 |  | 
|---|
| 800 | // check whether node is still shown, otherwise remove completely | 
|---|
| 801 | checkAndRemoveBond( bond->getIndex() ); | 
|---|
| 802 | } | 
|---|
| 803 |  | 
|---|
| 804 | GLMoleculeObject_bond *GLWorldScene::getBondInScene( | 
|---|
| 805 | const ObservedValue_Index_t _bondid, | 
|---|
| 806 | GLMoleculeObject_bond::SideOfBond _side) const | 
|---|
| 807 | { | 
|---|
| 808 | std::pair<GLWorldScene::BondNodeMap::const_iterator, GLWorldScene::BondNodeMap::const_iterator> iters = | 
|---|
| 809 | BondsinSceneMap.equal_range(_bondid); | 
|---|
| 810 | ASSERT( std::distance(iters.first, iters.second) >= 1, | 
|---|
| 811 | "GLWorldScene::getBondInScene() - not at least one bond of id " | 
|---|
| 812 | +toString(_bondid)+" present in scene."); | 
|---|
| 813 | for (GLWorldScene::BondNodeMap::const_iterator bonditer = iters.first; | 
|---|
| 814 | bonditer != iters.second; ++bonditer) { | 
|---|
| 815 | if (bonditer->second->BondSide == _side) | 
|---|
| 816 | return bonditer->second; | 
|---|
| 817 | } | 
|---|
| 818 | return NULL; | 
|---|
| 819 | } | 
|---|
| 820 |  | 
|---|
| 821 | /** Changes the parent of an object in the scene. | 
|---|
| 822 | * | 
|---|
| 823 | * \param _atom atom of bond whose molecule we are associated to | 
|---|
| 824 | * \param _side side of bond | 
|---|
| 825 | */ | 
|---|
| 826 | void GLWorldScene::reparentBond( | 
|---|
| 827 | const QtObservedBond::ptr _bond, | 
|---|
| 828 | const QtObservedAtom::ptr _atom, | 
|---|
| 829 | const GLMoleculeObject_bond::SideOfBond _side) | 
|---|
| 830 | { | 
|---|
| 831 | boost::recursive_mutex::scoped_lock lock(Bond_mutex); | 
|---|
| 832 | const size_t dim = (_side == GLMoleculeObject_bond::left) ? 0 : 1; | 
|---|
| 833 | const ObservedValue_Index_t bondid = _bond->getIndex(); | 
|---|
| 834 | BondNodeParentMap_t::left_iterator parentiter = BondNodeParentMaps[dim].left.find(bondid); | 
|---|
| 835 | ASSERT( parentiter != BondNodeParentMaps[dim].left.end(), | 
|---|
| 836 | "GLWorldScene::reparentBond() - could not find object to id "+toString(bondid)); | 
|---|
| 837 |  | 
|---|
| 838 | // change parent entry | 
|---|
| 839 | BondNodeParentMaps[dim].left.erase(bondid); | 
|---|
| 840 | if ((_atom != NULL) && (_atom->getMoleculeRef() != NULL)) | 
|---|
| 841 | BondNodeParentMaps[dim].left.insert( std::make_pair( bondid, _atom->getMoleculeRef()->getIndex())); | 
|---|
| 842 | else | 
|---|
| 843 | BondNodeParentMaps[dim].left.insert( std::make_pair( bondid, (ObservedValue_Index_t)NULL) ); | 
|---|
| 844 | parentiter = BondNodeParentMaps[dim].left.find(bondid); | 
|---|
| 845 | LOG(3, "INFO: GLWorldScene::reparentBond() - Reparented bond " | 
|---|
| 846 | << _bond->getBondIndex() << " to " << parentiter->second); | 
|---|
| 847 |  | 
|---|
| 848 | // reset parent | 
|---|
| 849 | resetParent(getBondInScene(bondid, _side), getMoleculeObject(parentiter->second)); | 
|---|
| 850 | } | 
|---|
| 851 |  | 
|---|
| 852 | /** Resets the parent of an GLMoleculeObject. | 
|---|
| 853 | * | 
|---|
| 854 | * \param _obj object to reparent | 
|---|
| 855 | * \param _molid index of parent molecule | 
|---|
| 856 | */ | 
|---|
| 857 | void GLWorldScene::resetParent( | 
|---|
| 858 | GLMoleculeObject *_obj, | 
|---|
| 859 | GLMoleculeObject_molecule *_molobj) | 
|---|
| 860 | { | 
|---|
| 861 | if (_obj != NULL) { | 
|---|
| 862 | if (_molobj != NULL) { | 
|---|
| 863 | LOG(5, "DEBUG: Resetting parent of " << _obj << " to " << _molobj); | 
|---|
| 864 | _obj->setParent(_molobj); | 
|---|
| 865 | ASSERT( _obj->parent() == _molobj, | 
|---|
| 866 | "GLWorldScene::resetParent() - new parent "+toString(_molobj)+" was not set."); | 
|---|
| 867 | } else { | 
|---|
| 868 | LOG(5, "DEBUG: Resetting parent of " << _obj << " to " << this); | 
|---|
| 869 | _obj->setParent(this); | 
|---|
| 870 | ASSERT( _obj->parent() == this, | 
|---|
| 871 | "GLWorldScene::resetParent() - new parent "+toString(this)+" was not set."); | 
|---|
| 872 | } | 
|---|
| 873 | } else | 
|---|
| 874 | ELOG(1, "Object to reparent was NULL."); | 
|---|
| 875 | // else object does not yet exist | 
|---|
| 876 | } | 
|---|
| 877 |  | 
|---|
| 878 | /** Adds a shape to the scene. | 
|---|
| 879 | * | 
|---|
| 880 | */ | 
|---|
| 881 | void GLWorldScene::addShape(const std::string &_name) | 
|---|
| 882 | { | 
|---|
| 883 | Shape * const shape = ShapeRegistry::getInstance().getByName(_name); | 
|---|
| 884 | if (shape != NULL) { | 
|---|
| 885 | GLMoleculeObject_shape *shapeObject = new GLMoleculeObject_shape(*shape, this); | 
|---|
| 886 | ShapeNodeMap::iterator iter = ShapesinSceneMap.find(_name); | 
|---|
| 887 | ASSERT(iter == ShapesinSceneMap.end(), | 
|---|
| 888 | "GLWorldScene::addShape() - same shape "+_name+" added again."); | 
|---|
| 889 | ShapesinSceneMap.insert( make_pair(_name, shapeObject) ); | 
|---|
| 890 | } else | 
|---|
| 891 | ELOG(2, "GLWorldScene::addShape() - shape disappeared before we could draw it."); | 
|---|
| 892 |  | 
|---|
| 893 | emit changed(); | 
|---|
| 894 | } | 
|---|
| 895 |  | 
|---|
| 896 | void GLWorldScene::removeShape(const std::string &_name) | 
|---|
| 897 | { | 
|---|
| 898 | ShapeNodeMap::iterator iter = ShapesinSceneMap.find(_name); | 
|---|
| 899 | ASSERT(iter != ShapesinSceneMap.end(), | 
|---|
| 900 | "GLWorldScene::removeShape() - shape "+_name+" not in scene."); | 
|---|
| 901 | delete(iter->second); | 
|---|
| 902 | ShapesinSceneMap.erase(iter); | 
|---|
| 903 |  | 
|---|
| 904 | emit changed(); | 
|---|
| 905 | } | 
|---|
| 906 |  | 
|---|
| 907 | void GLWorldScene::updateSelectedShapes() | 
|---|
| 908 | { | 
|---|
| 909 | foreach (QObject *obj, children()) { | 
|---|
| 910 | GLMoleculeObject_shape *shapeobj = qobject_cast<GLMoleculeObject_shape *>(obj); | 
|---|
| 911 | if (shapeobj){ | 
|---|
| 912 | shapeobj->enable(ShapeRegistry::getInstance().isSelected(shapeobj->getShape())); | 
|---|
| 913 | } | 
|---|
| 914 | } | 
|---|
| 915 |  | 
|---|
| 916 | emit changed(); | 
|---|
| 917 | } | 
|---|
| 918 |  | 
|---|
| 919 | void GLWorldScene::initialize(QGLView *view, QGLPainter *painter) const | 
|---|
| 920 | { | 
|---|
| 921 | // Initialize all of the mesh objects that we have as children. | 
|---|
| 922 | foreach (QObject *obj, children()) { | 
|---|
| 923 | GLMoleculeObject *meshobj = qobject_cast<GLMoleculeObject *>(obj); | 
|---|
| 924 | if (meshobj) | 
|---|
| 925 | meshobj->initialize(view, painter); | 
|---|
| 926 | } | 
|---|
| 927 | } | 
|---|
| 928 |  | 
|---|
| 929 | void GLWorldScene::draw(QGLPainter *painter, const QVector4D &cameraPlane) const | 
|---|
| 930 | { | 
|---|
| 931 | // Draw all of the mesh objects that we have as children. | 
|---|
| 932 | foreach (QObject *obj, children()) { | 
|---|
| 933 | GLMoleculeObject *meshobj = qobject_cast<GLMoleculeObject *>(obj); | 
|---|
| 934 | if (meshobj) | 
|---|
| 935 | meshobj->draw(painter, cameraPlane); | 
|---|
| 936 | } | 
|---|
| 937 | } | 
|---|
| 938 |  | 
|---|
| 939 | void GLWorldScene::setSelectionMode(SelectionModeType mode) | 
|---|
| 940 | { | 
|---|
| 941 | selectionMode = mode; | 
|---|
| 942 | // TODO send update to toolbar | 
|---|
| 943 | } | 
|---|
| 944 |  | 
|---|
| 945 | void GLWorldScene::setSelectionModeAtom() | 
|---|
| 946 | { | 
|---|
| 947 | setSelectionMode(SelectAtom); | 
|---|
| 948 | } | 
|---|
| 949 |  | 
|---|
| 950 | void GLWorldScene::setSelectionModeMolecule() | 
|---|
| 951 | { | 
|---|
| 952 | setSelectionMode(SelectMolecule); | 
|---|
| 953 | } | 
|---|
| 954 |  | 
|---|