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