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