| 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(moleculeInserted(QtObservedMolecule::ptr)));
|
|---|
| 97 | connect(board, SIGNAL(moleculeRemoved(const moleculeId_t)),
|
|---|
| 98 | this, SLOT(moleculeRemoved(const moleculeId_t)));
|
|---|
| 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 moleculeId_t, const atomId_t)),
|
|---|
| 104 | this, SLOT(atomRemoved(const moleculeId_t, const atomId_t)));
|
|---|
| 105 |
|
|---|
| 106 | // connect(this, SIGNAL(updated()), this, SLOT(update()));
|
|---|
| 107 | }
|
|---|
| 108 |
|
|---|
| 109 | GLWorldScene::~GLWorldScene()
|
|---|
| 110 | {
|
|---|
| 111 | // remove all elements
|
|---|
| 112 | GLMoleculeObject::cleanMaterialMap();
|
|---|
| 113 | }
|
|---|
| 114 |
|
|---|
| 115 | void GLWorldScene::atomClicked(atomId_t no)
|
|---|
| 116 | {
|
|---|
| 117 | LOG(3, "INFO: GLMoleculeObject_molecule - atom " << no << " has been clicked.");
|
|---|
| 118 | const atom * const Walker = const_cast<const World &>(World::getInstance()).
|
|---|
| 119 | getAtom(AtomById(no));
|
|---|
| 120 | ASSERT( Walker != NULL,
|
|---|
| 121 | "GLWorldScene::atomClicked() - clicked atom has disappeared.");
|
|---|
| 122 | if (selectionMode == SelectAtom){
|
|---|
| 123 | if (!World::getInstance().isSelected(Walker))
|
|---|
| 124 | SelectionAtomById(std::vector<atomId_t>(1,no));
|
|---|
| 125 | else
|
|---|
| 126 | SelectionNotAtomById(std::vector<atomId_t>(1,no));
|
|---|
| 127 | }else if (selectionMode == SelectMolecule){
|
|---|
| 128 | const molecule *mol = Walker->getMolecule();
|
|---|
| 129 | ASSERT(mol, "Atom without molecule has been clicked.");
|
|---|
| 130 | molids_t ids(1, mol->getId());
|
|---|
| 131 | if (!World::getInstance().isSelected(mol))
|
|---|
| 132 | SelectionMoleculeById(ids);
|
|---|
| 133 | else
|
|---|
| 134 | SelectionNotMoleculeById(ids);
|
|---|
| 135 | }
|
|---|
| 136 | emit clicked(no);
|
|---|
| 137 | }
|
|---|
| 138 |
|
|---|
| 139 | void GLWorldScene::moleculeClicked(moleculeId_t no)
|
|---|
| 140 | {
|
|---|
| 141 | LOG(3, "INFO: GLMoleculeObject_molecule - mol " << no << " has been clicked.");
|
|---|
| 142 | const molecule * const mol= const_cast<const World &>(World::getInstance()).
|
|---|
| 143 | getMolecule(MoleculeById(no));
|
|---|
| 144 | ASSERT(mol, "Atom without molecule has been clicked.");
|
|---|
| 145 | molids_t ids(1, mol->getId());
|
|---|
| 146 | if (!World::getInstance().isSelected(mol))
|
|---|
| 147 | SelectionMoleculeById(ids);
|
|---|
| 148 | else
|
|---|
| 149 | SelectionNotMoleculeById(ids);
|
|---|
| 150 | emit clicked(no);
|
|---|
| 151 | }
|
|---|
| 152 |
|
|---|
| 153 | /** Inserts an atom into the scene before molecule is present.
|
|---|
| 154 | *
|
|---|
| 155 | * @param _molid molecule to insert atom for
|
|---|
| 156 | * @param _atomid atom to insert
|
|---|
| 157 | */
|
|---|
| 158 | void GLWorldScene::atomInserted(QtObservedAtom::ptr _atom)
|
|---|
| 159 | {
|
|---|
| 160 | ASSERT (_atom,
|
|---|
| 161 | "GLWorldScene::moleculeInserted() - received shared_ptr for atom is empty?");
|
|---|
| 162 | const atomId_t atomid = _atom->getAtomIndex();
|
|---|
| 163 | const atomId_t molid = _atom->getAtomMoleculeIndex();
|
|---|
| 164 | LOG(3, "INFO: GLWorldScene: Received signal atomInserted for atom "+toString(atomid)+".");
|
|---|
| 165 |
|
|---|
| 166 | // check of molecule is already present
|
|---|
| 167 | MoleculeNodeMap::iterator moliter = MoleculesinSceneMap.find(molid);
|
|---|
| 168 | if (moliter != MoleculesinSceneMap.end()) {
|
|---|
| 169 | // no action, is also caught via QtObservedMolecule by GLMoleculeObject_molecule
|
|---|
| 170 | } else {
|
|---|
| 171 | // store signal for when it is instantiated
|
|---|
| 172 | if (MoleculeMissedStateMap.count(molid) == 0)
|
|---|
| 173 | MoleculeMissedStateMap.insert( std::make_pair(molid ,StateChangeMap_t()) );
|
|---|
| 174 | MoleculeMissedStateMap[molid].insert( std::make_pair(atomid, atomInsertedState) );
|
|---|
| 175 | QtObservedAtomMap[atomid] = _atom;
|
|---|
| 176 | LOG(3, "INFO: GLWorldScene: Placing atomInserted for atom " << atomid
|
|---|
| 177 | << " and molecule " << molid << " into missed state map.");
|
|---|
| 178 | }
|
|---|
| 179 | }
|
|---|
| 180 |
|
|---|
| 181 | /** Removes an atom into the scene before molecule is present.
|
|---|
| 182 | *
|
|---|
| 183 | * @param _molid molecule to insert atom for
|
|---|
| 184 | * @param _atomid atom to insert
|
|---|
| 185 | */
|
|---|
| 186 | void GLWorldScene::atomRemoved(const moleculeId_t _molid, const atomId_t _atomid)
|
|---|
| 187 | {
|
|---|
| 188 | LOG(3, "INFO: GLWorldScene: Received signal atomRemoved for atom "+toString(_atomid)+".");
|
|---|
| 189 |
|
|---|
| 190 | boost::recursive_mutex::scoped_lock lock(MoleculeMissedStateMap_mutex);
|
|---|
| 191 |
|
|---|
| 192 | // check of molecule is already present
|
|---|
| 193 | MoleculeNodeMap::iterator moliter = MoleculesinSceneMap.find(_molid);
|
|---|
| 194 | if (moliter != MoleculesinSceneMap.end()) {
|
|---|
| 195 | // no action, is also caught via QtObservedMolecule by GLMoleculeObject_molecule
|
|---|
| 196 | } else {
|
|---|
| 197 | // store signal for when it is instantiated
|
|---|
| 198 | if (MoleculeMissedStateMap.count(_molid) == 0)
|
|---|
| 199 | MoleculeMissedStateMap.insert( std::make_pair(_molid ,StateChangeMap_t()) );
|
|---|
| 200 | MoleculeMissedStateMap[_molid].insert( std::make_pair(_atomid, atomRemovedState) );
|
|---|
| 201 | LOG(3, "INFO: GLWorldScene: Placing atomRemoved for atom " << _atomid
|
|---|
| 202 | << " and molecule " << _molid << " into missed state map.");
|
|---|
| 203 | }
|
|---|
| 204 | }
|
|---|
| 205 |
|
|---|
| 206 | /** Inserts a molecule into the scene.
|
|---|
| 207 | *
|
|---|
| 208 | * @param _mol molecule to insert
|
|---|
| 209 | */
|
|---|
| 210 | void GLWorldScene::moleculeInserted(QtObservedMolecule::ptr _mol)
|
|---|
| 211 | {
|
|---|
| 212 | ASSERT (_mol,
|
|---|
| 213 | "GLWorldScene::moleculeInserted() - received shared_ptr for molecule is empty?");
|
|---|
| 214 | const moleculeId_t molid = _mol->getMolIndex();
|
|---|
| 215 | LOG(3, "INFO: GLWorldScene: Received signal moleculeInserted for molecule "+toString(molid)+".");
|
|---|
| 216 |
|
|---|
| 217 | boost::recursive_mutex::scoped_lock lock(MoleculeinSceneMap_mutex);
|
|---|
| 218 |
|
|---|
| 219 | MoleculeNodeMap::const_iterator iter = MoleculesinSceneMap.find(molid);
|
|---|
| 220 | ASSERT( iter == MoleculesinSceneMap.end(),
|
|---|
| 221 | "GLWorldScene::moleculeInserted() - molecule's id "+toString(molid)+" already present.");
|
|---|
| 222 |
|
|---|
| 223 | // check whether molecule is still present
|
|---|
| 224 | if (RemovalMolecules.count(molid) != 0) {
|
|---|
| 225 | RemovalMolecules.erase(molid);
|
|---|
| 226 | return;
|
|---|
| 227 | }
|
|---|
| 228 |
|
|---|
| 229 | // add new object
|
|---|
| 230 | LOG(1, "DEBUG: Adding GLMoleculeObject_molecule to id " << molid);
|
|---|
| 231 | GLMoleculeObject_molecule *molObject =
|
|---|
| 232 | new GLMoleculeObject_molecule(
|
|---|
| 233 | GLMoleculeObject::meshEmpty,
|
|---|
| 234 | this,
|
|---|
| 235 | *board,
|
|---|
| 236 | _mol);
|
|---|
| 237 | ASSERT( molObject != NULL,
|
|---|
| 238 | "GLWorldScene::moleculeInserted - could not create molecule object for "+toString(molid));
|
|---|
| 239 | #ifndef NDEBUG
|
|---|
| 240 | std::pair<MoleculeNodeMap::iterator, bool> inserter =
|
|---|
| 241 | #endif
|
|---|
| 242 | MoleculesinSceneMap.insert( make_pair(molid, molObject) );
|
|---|
| 243 | ASSERT(inserter.second,
|
|---|
| 244 | "GLWorldScene::moleculeInserted() - molecule "+toString(molid)+" already present in scene.");
|
|---|
| 245 |
|
|---|
| 246 | // now handle all state changes that came up before the instantiation
|
|---|
| 247 | if (MoleculeMissedStateMap.count(molid) != 0) {
|
|---|
| 248 | // ASSERT( !MoleculeMissedStateMap[molid].empty(),
|
|---|
| 249 | // "GLWorldScene::moleculeInserted() - we have an empty state change map for molecule with id "
|
|---|
| 250 | // +toString(molid));
|
|---|
| 251 | boost::recursive_mutex::scoped_lock lock(MoleculeMissedStateMap_mutex);
|
|---|
| 252 | for (StateChangeMap_t::iterator iter = MoleculeMissedStateMap[molid].begin();
|
|---|
| 253 | !MoleculeMissedStateMap[molid].empty();
|
|---|
| 254 | iter = MoleculeMissedStateMap[molid].begin()) {
|
|---|
| 255 | std::pair<StateChangeMap_t::iterator, StateChangeMap_t::iterator> rangeiter =
|
|---|
| 256 | MoleculeMissedStateMap[molid].equal_range(iter->first);
|
|---|
| 257 | const size_t StateCounts = std::distance(rangeiter.first, rangeiter.second);
|
|---|
| 258 | if (StateCounts > 1) {
|
|---|
| 259 | // more than one state change, have to combine
|
|---|
| 260 | typedef std::map<StateChangeType, size_t> StateChangeAmounts_t;
|
|---|
| 261 | StateChangeAmounts_t StateChangeAmounts;
|
|---|
| 262 | for (StateChangeMap_t::const_iterator stateiter = rangeiter.first;
|
|---|
| 263 | stateiter != rangeiter.second; ++stateiter)
|
|---|
| 264 | ++StateChangeAmounts[stateiter->second];
|
|---|
| 265 | ASSERT( StateChangeAmounts[atomInsertedState] >= StateChangeAmounts[atomRemovedState],
|
|---|
| 266 | "GLWorldScene::moleculeInserted() - more atomRemoved states "
|
|---|
| 267 | +toString(StateChangeAmounts[atomRemovedState])+" than atomInserted "
|
|---|
| 268 | +toString(StateChangeAmounts[atomInsertedState])+" for atom "+toString(iter->first));
|
|---|
| 269 | if (StateChangeAmounts[atomInsertedState] > StateChangeAmounts[atomRemovedState]) {
|
|---|
| 270 | LOG(1, "INFO: invoking atomInserted for atom " << iter->first);
|
|---|
| 271 | QMetaObject::invokeMethod(molObject, // pointer to a QObject
|
|---|
| 272 | "atomInserted", // member name (no parameters here)
|
|---|
| 273 | Qt::QueuedConnection, // connection type
|
|---|
| 274 | Q_ARG(QtObservedAtom::ptr, QtObservedAtomMap[iter->first])); // parameters
|
|---|
| 275 | } else {
|
|---|
| 276 | LOG(1, "INFO: Atom " << iter->first << " has been inserted and removed already.");
|
|---|
| 277 | }
|
|---|
| 278 | // removed all state changes for this atom
|
|---|
| 279 | MoleculeMissedStateMap[molid].erase(rangeiter.first, rangeiter.second);
|
|---|
| 280 | } else {
|
|---|
| 281 | // can only be an insertion
|
|---|
| 282 | switch (rangeiter.first->second) {
|
|---|
| 283 | case atomRemovedState:
|
|---|
| 284 | ASSERT( 0,
|
|---|
| 285 | "GLWorldScene::moleculeInserted() - atomRemoved state without atomInserted for atom "
|
|---|
| 286 | +toString(iter->first));
|
|---|
| 287 | break;
|
|---|
| 288 | case atomInsertedState:
|
|---|
| 289 | {
|
|---|
| 290 | LOG(1, "INFO: invoking atomInserted for atom " << iter->first);
|
|---|
| 291 | QMetaObject::invokeMethod(molObject, // pointer to a QObject
|
|---|
| 292 | "atomInserted", // member name (no parameters here)
|
|---|
| 293 | Qt::QueuedConnection, // connection type
|
|---|
| 294 | Q_ARG(QtObservedAtom::ptr, QtObservedAtomMap[iter->first])); // parameters
|
|---|
| 295 | break;
|
|---|
| 296 | }
|
|---|
| 297 | default:
|
|---|
| 298 | ASSERT( 0,
|
|---|
| 299 | "GLWorldScene::moleculeInserted() - there are unknown change states.");
|
|---|
| 300 | break;
|
|---|
| 301 | }
|
|---|
| 302 | // removed state changes for this atom
|
|---|
| 303 | MoleculeMissedStateMap[molid].erase(iter);
|
|---|
| 304 | }
|
|---|
| 305 | }
|
|---|
| 306 | }
|
|---|
| 307 |
|
|---|
| 308 | connect (molObject, SIGNAL(changed()), this, SIGNAL(changed()));
|
|---|
| 309 | connect (molObject, SIGNAL(changeOccured()), this, SIGNAL(changeOccured()));
|
|---|
| 310 | connect (molObject, SIGNAL(atomClicked(atomId_t)), this, SLOT(atomClicked(atomId_t)));
|
|---|
| 311 | connect (molObject, SIGNAL(moleculeClicked(moleculeId_t)), this, SLOT(moleculeClicked(moleculeId_t)));
|
|---|
| 312 | connect (molObject, SIGNAL(selectionChanged()), this, SIGNAL(changed()));
|
|---|
| 313 | connect (molObject, SIGNAL(selectionChanged()), this, SIGNAL(changed()));
|
|---|
| 314 | connect (molObject, SIGNAL(hoverChanged(const atomId_t)), this, SIGNAL(hoverChanged(const atomId_t)));
|
|---|
| 315 | connect (molObject, SIGNAL(hoverChanged(const moleculeId_t, int)), this, SIGNAL(hoverChanged(const moleculeId_t, int)));
|
|---|
| 316 | connect (molObject, SIGNAL(hoverChanged(const moleculeId_t, int)), this, SIGNAL(hoverChanged(const moleculeId_t, int)));
|
|---|
| 317 |
|
|---|
| 318 | emit changed();
|
|---|
| 319 | emit changeOccured();
|
|---|
| 320 |
|
|---|
| 321 | // remove state change map for the molecule
|
|---|
| 322 | {
|
|---|
| 323 | boost::recursive_mutex::scoped_lock lock(MoleculeMissedStateMap_mutex);
|
|---|
| 324 | MoleculeMissedStateMap.erase(molid);
|
|---|
| 325 | }
|
|---|
| 326 | }
|
|---|
| 327 |
|
|---|
| 328 | /** Removes a molecule from the scene.
|
|---|
| 329 | *
|
|---|
| 330 | * @param _id id of molecule to remove
|
|---|
| 331 | */
|
|---|
| 332 | void GLWorldScene::moleculeRemoved(const moleculeId_t _id)
|
|---|
| 333 | {
|
|---|
| 334 | LOG(3, "INFO: GLWorldScene: Received signal moleculeRemoved for molecule "+toString(_id)+".");
|
|---|
| 335 |
|
|---|
| 336 | boost::recursive_mutex::scoped_lock lock(MoleculeinSceneMap_mutex);
|
|---|
| 337 |
|
|---|
| 338 | MoleculeNodeMap::iterator iter = MoleculesinSceneMap.find(_id);
|
|---|
| 339 | if ( iter == MoleculesinSceneMap.end()) {
|
|---|
| 340 | RemovalMolecules.insert(_id);
|
|---|
| 341 | } else {
|
|---|
| 342 | LOG(1, "DEBUG: Removing GLMoleculeObject_molecule to id " << _id);
|
|---|
| 343 | GLMoleculeObject_molecule *molObject = iter->second;
|
|---|
| 344 | molObject->disconnect();
|
|---|
| 345 | MoleculesinSceneMap.erase(iter);
|
|---|
| 346 | delete molObject;
|
|---|
| 347 | }
|
|---|
| 348 |
|
|---|
| 349 | // remove any possible state changes left
|
|---|
| 350 | {
|
|---|
| 351 | boost::recursive_mutex::scoped_lock lock(MoleculeMissedStateMap_mutex);
|
|---|
| 352 | MoleculeMissedStateMap.erase(_id);
|
|---|
| 353 | }
|
|---|
| 354 |
|
|---|
| 355 | emit changed();
|
|---|
| 356 | emit changeOccured();
|
|---|
| 357 | }
|
|---|
| 358 |
|
|---|
| 359 | void GLWorldScene::moleculeIndexChanged(const moleculeId_t _oldid, const moleculeId_t _newid)
|
|---|
| 360 | {
|
|---|
| 361 | MoleculeNodeMap::iterator iter = MoleculesinSceneMap.find(_oldid);
|
|---|
| 362 | if ( iter == MoleculesinSceneMap.end()) {
|
|---|
| 363 | RemovalMolecule_t::iterator removeiter =
|
|---|
| 364 | RemovalMolecules.find(_oldid);
|
|---|
| 365 | ASSERT( removeiter != RemovalMolecules.end(),
|
|---|
| 366 | "GLWorldScene::moleculeIndexChanged() - cannot find id "+toString(_oldid)+" in any map.");
|
|---|
| 367 | RemovalMolecules.erase(removeiter);
|
|---|
| 368 | RemovalMolecules.insert(_newid);
|
|---|
| 369 | } else {
|
|---|
| 370 | LOG(1, "DEBUG: Changing GLMoleculeObject_molecule from " << _oldid << " to id " << _newid);
|
|---|
| 371 | GLMoleculeObject_molecule* molObject = iter->second;
|
|---|
| 372 | MoleculesinSceneMap.erase(iter);
|
|---|
| 373 | MoleculesinSceneMap.insert(
|
|---|
| 374 | std::make_pair( _newid, molObject) );
|
|---|
| 375 | }
|
|---|
| 376 | }
|
|---|
| 377 |
|
|---|
| 378 | void GLWorldScene::moleculesVisibilityChanged(const moleculeId_t _id, bool _visible)
|
|---|
| 379 | {
|
|---|
| 380 | boost::recursive_mutex::scoped_lock lock(MoleculeinSceneMap_mutex);
|
|---|
| 381 | MoleculeNodeMap::iterator iter = MoleculesinSceneMap.find(_id);
|
|---|
| 382 | ASSERT( iter != MoleculesinSceneMap.end(),
|
|---|
| 383 | "GLWorldScene::moleculeInserted() - molecule's id "+toString(_id)+" is unknown.");
|
|---|
| 384 |
|
|---|
| 385 | GLMoleculeObject_molecule *molObject = iter->second;
|
|---|
| 386 | molObject->setVisible(_visible);
|
|---|
| 387 |
|
|---|
| 388 | emit changed();
|
|---|
| 389 | emit changeOccured();
|
|---|
| 390 | }
|
|---|
| 391 |
|
|---|
| 392 | /** Adds a shape to the scene.
|
|---|
| 393 | *
|
|---|
| 394 | */
|
|---|
| 395 | void GLWorldScene::addShape(const std::string &_name)
|
|---|
| 396 | {
|
|---|
| 397 | Shape * const shape = ShapeRegistry::getInstance().getByName(_name);
|
|---|
| 398 | if (shape != NULL) {
|
|---|
| 399 | GLMoleculeObject_shape *shapeObject = new GLMoleculeObject_shape(*shape, this);
|
|---|
| 400 | ShapeNodeMap::iterator iter = ShapesinSceneMap.find(_name);
|
|---|
| 401 | ASSERT(iter == ShapesinSceneMap.end(),
|
|---|
| 402 | "GLWorldScene::addShape() - same shape "+_name+" added again.");
|
|---|
| 403 | ShapesinSceneMap.insert( make_pair(_name, shapeObject) );
|
|---|
| 404 | } else
|
|---|
| 405 | ELOG(2, "GLWorldScene::addShape() - shape disappeared before we could draw it.");
|
|---|
| 406 |
|
|---|
| 407 | emit changed();
|
|---|
| 408 | }
|
|---|
| 409 |
|
|---|
| 410 | void GLWorldScene::removeShape(const std::string &_name)
|
|---|
| 411 | {
|
|---|
| 412 | ShapeNodeMap::iterator iter = ShapesinSceneMap.find(_name);
|
|---|
| 413 | ASSERT(iter != ShapesinSceneMap.end(),
|
|---|
| 414 | "GLWorldScene::removeShape() - shape "+_name+" not in scene.");
|
|---|
| 415 | ShapesinSceneMap.erase(iter);
|
|---|
| 416 | delete(iter->second);
|
|---|
| 417 |
|
|---|
| 418 | emit changed();
|
|---|
| 419 | }
|
|---|
| 420 |
|
|---|
| 421 | void GLWorldScene::updateSelectedShapes()
|
|---|
| 422 | {
|
|---|
| 423 | foreach (QObject *obj, children()) {
|
|---|
| 424 | GLMoleculeObject_shape *shapeobj = qobject_cast<GLMoleculeObject_shape *>(obj);
|
|---|
| 425 | if (shapeobj){
|
|---|
| 426 | shapeobj->enable(ShapeRegistry::getInstance().isSelected(shapeobj->getShape()));
|
|---|
| 427 | }
|
|---|
| 428 | }
|
|---|
| 429 |
|
|---|
| 430 | emit changed();
|
|---|
| 431 | }
|
|---|
| 432 |
|
|---|
| 433 | void GLWorldScene::initialize(QGLView *view, QGLPainter *painter) const
|
|---|
| 434 | {
|
|---|
| 435 | // Initialize all of the mesh objects that we have as children.
|
|---|
| 436 | foreach (QObject *obj, children()) {
|
|---|
| 437 | GLMoleculeObject *meshobj = qobject_cast<GLMoleculeObject *>(obj);
|
|---|
| 438 | if (meshobj)
|
|---|
| 439 | meshobj->initialize(view, painter);
|
|---|
| 440 | }
|
|---|
| 441 | }
|
|---|
| 442 |
|
|---|
| 443 | void GLWorldScene::draw(QGLPainter *painter, const QVector4D &cameraPlane) const
|
|---|
| 444 | {
|
|---|
| 445 | // Draw all of the mesh objects that we have as children.
|
|---|
| 446 | foreach (QObject *obj, children()) {
|
|---|
| 447 | GLMoleculeObject *meshobj = qobject_cast<GLMoleculeObject *>(obj);
|
|---|
| 448 | if (meshobj)
|
|---|
| 449 | meshobj->draw(painter, cameraPlane);
|
|---|
| 450 | }
|
|---|
| 451 | }
|
|---|
| 452 |
|
|---|
| 453 | void GLWorldScene::setSelectionMode(SelectionModeType mode)
|
|---|
| 454 | {
|
|---|
| 455 | selectionMode = mode;
|
|---|
| 456 | // TODO send update to toolbar
|
|---|
| 457 | }
|
|---|
| 458 |
|
|---|
| 459 | void GLWorldScene::setSelectionModeAtom()
|
|---|
| 460 | {
|
|---|
| 461 | setSelectionMode(SelectAtom);
|
|---|
| 462 | }
|
|---|
| 463 |
|
|---|
| 464 | void GLWorldScene::setSelectionModeMolecule()
|
|---|
| 465 | {
|
|---|
| 466 | setSelectionMode(SelectMolecule);
|
|---|
| 467 | }
|
|---|
| 468 |
|
|---|
| 469 | void GLWorldScene::changeMoleculeId(
|
|---|
| 470 | GLMoleculeObject_molecule *ob,
|
|---|
| 471 | const moleculeId_t oldId,
|
|---|
| 472 | const moleculeId_t newId)
|
|---|
| 473 | {
|
|---|
| 474 | LOG(3, "INFO: GLWorldScene - change molecule id " << oldId << " to " << newId << ".");
|
|---|
| 475 |
|
|---|
| 476 | {
|
|---|
| 477 | boost::recursive_mutex::scoped_lock lock(MoleculeinSceneMap_mutex);
|
|---|
| 478 | // Remove from map.
|
|---|
| 479 | MoleculeNodeMap::iterator iter = MoleculesinSceneMap.find(oldId);
|
|---|
| 480 | ASSERT(iter != MoleculesinSceneMap.end(),
|
|---|
| 481 | "GLWorldScene::changeMoleculeId() - molecule with old id "+toString(oldId)+" not on display.");
|
|---|
| 482 | ASSERT(iter->second == ob,
|
|---|
| 483 | "GLWorldScene::changeMoleculeId() - molecule with id "
|
|---|
| 484 | +toString(oldId)+" does not match with object in MoleculesinSceneMap.");
|
|---|
| 485 | MoleculesinSceneMap.erase(iter);
|
|---|
| 486 |
|
|---|
| 487 | // Reinsert with new id.
|
|---|
| 488 | {
|
|---|
| 489 | MoleculeNodeMap::iterator iter = MoleculesinSceneMap.find(newId);
|
|---|
| 490 | ASSERT(iter == MoleculesinSceneMap.end(),
|
|---|
| 491 | "GLWorldScene::changeMoleculeId() - moleculewith new id "+toString(newId)+" already known.");
|
|---|
| 492 | }
|
|---|
| 493 | MoleculesinSceneMap.insert( make_pair(newId, ob) );
|
|---|
| 494 | }
|
|---|
| 495 |
|
|---|
| 496 | {
|
|---|
| 497 | boost::recursive_mutex::scoped_lock lock(MoleculeMissedStateMap_mutex);
|
|---|
| 498 | // Remove and re-insert from map if present.
|
|---|
| 499 | MoleculeMissedStateMap_t::iterator iter = MoleculeMissedStateMap.find(oldId);
|
|---|
| 500 | if (iter != MoleculeMissedStateMap.end()) {
|
|---|
| 501 | StateChangeMap_t changemap = iter->second;
|
|---|
| 502 | MoleculeMissedStateMap.erase(iter);
|
|---|
| 503 | MoleculeMissedStateMap.insert( std::make_pair(newId, changemap) );
|
|---|
| 504 | }
|
|---|
| 505 | }
|
|---|
| 506 | }
|
|---|
| 507 |
|
|---|