| 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 | #include "UIElements/Views/Qt4/QtSelectionChangedAgent.hpp"
|
|---|
| 53 |
|
|---|
| 54 | #include "CodePatterns/MemDebug.hpp"
|
|---|
| 55 |
|
|---|
| 56 | #include "CodePatterns/Log.hpp"
|
|---|
| 57 |
|
|---|
| 58 | #include "Actions/SelectionAction/Atoms/AtomByIdAction.hpp"
|
|---|
| 59 | #include "Actions/SelectionAction/Atoms/NotAtomByIdAction.hpp"
|
|---|
| 60 | #include "Actions/SelectionAction/Molecules/MoleculeByIdAction.hpp"
|
|---|
| 61 | #include "Actions/SelectionAction/Molecules/NotMoleculeByIdAction.hpp"
|
|---|
| 62 | #include "Atom/atom.hpp"
|
|---|
| 63 | #include "Bond/bond.hpp"
|
|---|
| 64 | #include "Descriptors/AtomIdDescriptor.hpp"
|
|---|
| 65 | #include "Descriptors/MoleculeIdDescriptor.hpp"
|
|---|
| 66 | #include "Helpers/helpers.hpp"
|
|---|
| 67 | #include "Shapes/ShapeRegistry.hpp"
|
|---|
| 68 | #include "molecule.hpp"
|
|---|
| 69 | #include "World.hpp"
|
|---|
| 70 |
|
|---|
| 71 | #include <iostream>
|
|---|
| 72 |
|
|---|
| 73 | using namespace MoleCuilder;
|
|---|
| 74 |
|
|---|
| 75 | GLWorldScene::GLWorldScene(
|
|---|
| 76 | QtObservedInstanceBoard * _board,
|
|---|
| 77 | QObject *parent) :
|
|---|
| 78 | QObject(parent),
|
|---|
| 79 | selectionMode(SelectAtom),
|
|---|
| 80 | board(_board)
|
|---|
| 81 | {
|
|---|
| 82 | int sphereDetails[] = {5, 3, 2, 0};
|
|---|
| 83 | int cylinderDetails[] = {16, 8, 6, 3};
|
|---|
| 84 | for (int i=0;i<GLMoleculeObject::DETAILTYPES_MAX;i++){
|
|---|
| 85 | QGLBuilder emptyBuilder;
|
|---|
| 86 | GLMoleculeObject::meshEmpty[i] = emptyBuilder.finalizedSceneNode();
|
|---|
| 87 | QGLBuilder sphereBuilder;
|
|---|
| 88 | sphereBuilder << QGLSphere(2.0, sphereDetails[i]);
|
|---|
| 89 | GLMoleculeObject::meshSphere[i] = sphereBuilder.finalizedSceneNode();
|
|---|
| 90 | GLMoleculeObject::meshSphere[i]->setOption(QGLSceneNode::CullBoundingBox, true);
|
|---|
| 91 | QGLBuilder cylinderBuilder;
|
|---|
| 92 | cylinderBuilder << QGLCylinder(.25,.25,1.0,cylinderDetails[i]);
|
|---|
| 93 | GLMoleculeObject::meshCylinder[i] = cylinderBuilder.finalizedSceneNode();
|
|---|
| 94 | GLMoleculeObject::meshCylinder[i]->setOption(QGLSceneNode::CullBoundingBox, true);
|
|---|
| 95 | }
|
|---|
| 96 | connect(board, SIGNAL(moleculeInserted(const moleculeId_t)),
|
|---|
| 97 | this, SLOT(moleculeInserted(const moleculeId_t)));
|
|---|
| 98 | connect(board, SIGNAL(moleculeRemoved(const moleculeId_t)),
|
|---|
| 99 | this, SLOT(moleculeRemoved(const moleculeId_t)));
|
|---|
| 100 | connect(board, SIGNAL(moleculeIndexChanged(const moleculeId_t, const moleculeId_t)),
|
|---|
| 101 | this, SLOT(moleculeIndexChanged(const moleculeId_t, const moleculeId_t)));
|
|---|
| 102 | connect(board, SIGNAL(atomInserted(const moleculeId_t, const atomId_t)),
|
|---|
| 103 | this, SLOT(atomInserted(const moleculeId_t, const atomId_t)), Qt::DirectConnection);
|
|---|
| 104 | connect(board, SIGNAL(atomRemoved(const moleculeId_t, const atomId_t)),
|
|---|
| 105 | this, SLOT(atomRemoved(const moleculeId_t, const atomId_t)), Qt::DirectConnection);
|
|---|
| 106 |
|
|---|
| 107 | // connect(this, SIGNAL(updated()), this, SLOT(update()));
|
|---|
| 108 | }
|
|---|
| 109 |
|
|---|
| 110 | GLWorldScene::~GLWorldScene()
|
|---|
| 111 | {
|
|---|
| 112 | // remove all elements
|
|---|
| 113 | GLMoleculeObject::cleanMaterialMap();
|
|---|
| 114 | }
|
|---|
| 115 |
|
|---|
| 116 | void GLWorldScene::atomClicked(atomId_t no)
|
|---|
| 117 | {
|
|---|
| 118 | LOG(3, "INFO: GLMoleculeObject_molecule - atom " << no << " has been clicked.");
|
|---|
| 119 | const atom * const Walker = const_cast<const World &>(World::getInstance()).
|
|---|
| 120 | getAtom(AtomById(no));
|
|---|
| 121 | ASSERT( Walker != NULL,
|
|---|
| 122 | "GLWorldScene::atomClicked() - clicked atom has disappeared.");
|
|---|
| 123 | if (selectionMode == SelectAtom){
|
|---|
| 124 | if (!World::getInstance().isSelected(Walker))
|
|---|
| 125 | SelectionAtomById(std::vector<atomId_t>(1,no));
|
|---|
| 126 | else
|
|---|
| 127 | SelectionNotAtomById(std::vector<atomId_t>(1,no));
|
|---|
| 128 | }else if (selectionMode == SelectMolecule){
|
|---|
| 129 | const molecule *mol = Walker->getMolecule();
|
|---|
| 130 | ASSERT(mol, "Atom without molecule has been clicked.");
|
|---|
| 131 | molids_t ids(1, mol->getId());
|
|---|
| 132 | if (!World::getInstance().isSelected(mol))
|
|---|
| 133 | SelectionMoleculeById(ids);
|
|---|
| 134 | else
|
|---|
| 135 | SelectionNotMoleculeById(ids);
|
|---|
| 136 | }
|
|---|
| 137 | emit clicked(no);
|
|---|
| 138 | }
|
|---|
| 139 |
|
|---|
| 140 | void GLWorldScene::moleculeClicked(moleculeId_t no)
|
|---|
| 141 | {
|
|---|
| 142 | LOG(3, "INFO: GLMoleculeObject_molecule - mol " << no << " has been clicked.");
|
|---|
| 143 | const molecule * const mol= const_cast<const World &>(World::getInstance()).
|
|---|
| 144 | getMolecule(MoleculeById(no));
|
|---|
| 145 | ASSERT(mol, "Atom without molecule has been clicked.");
|
|---|
| 146 | molids_t ids(1, mol->getId());
|
|---|
| 147 | if (!World::getInstance().isSelected(mol))
|
|---|
| 148 | SelectionMoleculeById(ids);
|
|---|
| 149 | else
|
|---|
| 150 | SelectionNotMoleculeById(ids);
|
|---|
| 151 | emit clicked(no);
|
|---|
| 152 | }
|
|---|
| 153 |
|
|---|
| 154 | /** Inserts an atom into the scene before molecule is present.
|
|---|
| 155 | *
|
|---|
| 156 | * @param _molid molecule to insert atom for
|
|---|
| 157 | * @param _atomid atom to insert
|
|---|
| 158 | */
|
|---|
| 159 | void GLWorldScene::atomInserted(const moleculeId_t _molid, const atomId_t _atomid)
|
|---|
| 160 | {
|
|---|
| 161 | boost::recursive_mutex::scoped_lock lock(MoleculeMissedStateMap_mutex);
|
|---|
| 162 |
|
|---|
| 163 | LOG(3, "INFO: GLWorldScene: Received signal atomInserted for atom "+toString(_atomid)+".");
|
|---|
| 164 |
|
|---|
| 165 | // check of molecule is already present
|
|---|
| 166 | MoleculeNodeMap::iterator moliter = MoleculesinSceneMap.find(_molid);
|
|---|
| 167 | if (moliter != MoleculesinSceneMap.end()) {
|
|---|
| 168 | // pass signal through
|
|---|
| 169 | GLMoleculeObject_molecule *molObject = moliter->second;
|
|---|
| 170 | QMetaObject::invokeMethod(molObject, // pointer to a QObject
|
|---|
| 171 | "atomInserted", // member name (no parameters here)
|
|---|
| 172 | Qt::QueuedConnection, // connection type
|
|---|
| 173 | Q_ARG(const atomId_t, _atomid)); // parameters
|
|---|
| 174 | } else {
|
|---|
| 175 | // store signal for when it is instantiated
|
|---|
| 176 | if (MoleculeMissedStateMap.count(_molid) == 0)
|
|---|
| 177 | MoleculeMissedStateMap.insert( std::make_pair(_molid ,StateChangeMap_t()) );
|
|---|
| 178 | MoleculeMissedStateMap[_molid].insert( std::make_pair(_atomid, atomInsertedState) );
|
|---|
| 179 | }
|
|---|
| 180 | }
|
|---|
| 181 |
|
|---|
| 182 | /** Removes an atom into the scene before molecule is present.
|
|---|
| 183 | *
|
|---|
| 184 | * @param _molid molecule to insert atom for
|
|---|
| 185 | * @param _atomid atom to insert
|
|---|
| 186 | */
|
|---|
| 187 | void GLWorldScene::atomRemoved(const moleculeId_t _molid, const atomId_t _atomid)
|
|---|
| 188 | {
|
|---|
| 189 | LOG(3, "INFO: GLWorldScene: Received signal atomRemoved for atom "+toString(_atomid)+".");
|
|---|
| 190 |
|
|---|
| 191 | boost::recursive_mutex::scoped_lock lock(MoleculeMissedStateMap_mutex);
|
|---|
| 192 |
|
|---|
| 193 | // check of molecule is already present
|
|---|
| 194 | MoleculeNodeMap::iterator moliter = MoleculesinSceneMap.find(_molid);
|
|---|
| 195 | if (moliter != MoleculesinSceneMap.end()) {
|
|---|
| 196 | // pass signal through
|
|---|
| 197 | GLMoleculeObject_molecule *molObject = moliter->second;
|
|---|
| 198 | QMetaObject::invokeMethod(molObject, // pointer to a QObject
|
|---|
| 199 | "atomRemoved", // member name (no parameters here)
|
|---|
| 200 | Qt::QueuedConnection, // connection type
|
|---|
| 201 | Q_ARG(const atomId_t, _atomid)); // parameters
|
|---|
| 202 | } else {
|
|---|
| 203 | // store signal for when it is instantiated
|
|---|
| 204 | if (MoleculeMissedStateMap.count(_molid) == 0)
|
|---|
| 205 | MoleculeMissedStateMap.insert( std::make_pair(_molid ,StateChangeMap_t()) );
|
|---|
| 206 | MoleculeMissedStateMap[_molid].insert( std::make_pair(_atomid, atomRemovedState) );
|
|---|
| 207 | }
|
|---|
| 208 | }
|
|---|
| 209 |
|
|---|
| 210 | /** Inserts a molecule into the scene.
|
|---|
| 211 | *
|
|---|
| 212 | * @param _mol molecule to insert
|
|---|
| 213 | */
|
|---|
| 214 | void GLWorldScene::moleculeInserted(const moleculeId_t _id)
|
|---|
| 215 | {
|
|---|
| 216 | LOG(3, "INFO: GLWorldScene: Received signal moleculeInserted for molecule "+toString(_id)+".");
|
|---|
| 217 |
|
|---|
| 218 | boost::recursive_mutex::scoped_lock lock(MoleculeinSceneMap_mutex);
|
|---|
| 219 |
|
|---|
| 220 | MoleculeNodeMap::const_iterator iter = MoleculesinSceneMap.find(_id);
|
|---|
| 221 | ASSERT( iter == MoleculesinSceneMap.end(),
|
|---|
| 222 | "GLWorldScene::moleculeInserted() - molecule's id "+toString(_id)+" already present.");
|
|---|
| 223 |
|
|---|
| 224 | // check whether molecule is still present
|
|---|
| 225 | if (RemovalMolecules.count(_id) != 0) {
|
|---|
| 226 | RemovalMolecules.erase(_id);
|
|---|
| 227 | return;
|
|---|
| 228 | }
|
|---|
| 229 | if (const_cast<const World &>(World::getInstance()).getMolecule(MoleculeById(_id)) == NULL) {
|
|---|
| 230 | ELOG(2, "Molecule with id " << _id << " has disappeared.");
|
|---|
| 231 | return;
|
|---|
| 232 | }
|
|---|
| 233 |
|
|---|
| 234 | // add new object
|
|---|
| 235 | LOG(1, "DEBUG: Adding GLMoleculeObject_molecule to id " << _id);
|
|---|
| 236 | GLMoleculeObject_molecule *molObject =
|
|---|
| 237 | new GLMoleculeObject_molecule(
|
|---|
| 238 | GLMoleculeObject::meshEmpty,
|
|---|
| 239 | this,
|
|---|
| 240 | _id,
|
|---|
| 241 | board,
|
|---|
| 242 | board->getMoleculeObservedValues(_id));
|
|---|
| 243 | ASSERT( molObject != NULL,
|
|---|
| 244 | "GLWorldScene::moleculeInserted - could not create molecule object for "+toString(_id));
|
|---|
| 245 | MoleculesinSceneMap.insert( make_pair(_id, molObject) );
|
|---|
| 246 |
|
|---|
| 247 | // now handle all state changes that came up before the instantiation
|
|---|
| 248 | while (MoleculeMissedStateMap.count(_id) != 0) {
|
|---|
| 249 | ASSERT( !MoleculeMissedStateMap[_id].empty(),
|
|---|
| 250 | "GLWorldScene::moleculeInserted() - we have an empty state change map for molecule with id "
|
|---|
| 251 | +toString(_id));
|
|---|
| 252 | boost::recursive_mutex::scoped_lock lock(MoleculeMissedStateMap_mutex);
|
|---|
| 253 | for (StateChangeMap_t::iterator iter = MoleculeMissedStateMap[_id].begin();
|
|---|
| 254 | !MoleculeMissedStateMap[_id].empty();
|
|---|
| 255 | iter = MoleculeMissedStateMap[_id].begin()) {
|
|---|
| 256 | std::pair<StateChangeMap_t::iterator, StateChangeMap_t::iterator> rangeiter =
|
|---|
| 257 | MoleculeMissedStateMap[_id].equal_range(iter->first);
|
|---|
| 258 | const size_t StateCounts = std::distance(rangeiter.first, rangeiter.second);
|
|---|
| 259 | if (StateCounts > 1) {
|
|---|
| 260 | // more than one state change, have to combine
|
|---|
| 261 | typedef std::map<StateChangeType, size_t> StateChangeAmounts_t;
|
|---|
| 262 | StateChangeAmounts_t StateChangeAmounts;
|
|---|
| 263 | for (StateChangeMap_t::const_iterator stateiter = rangeiter.first;
|
|---|
| 264 | stateiter != rangeiter.second; ++stateiter)
|
|---|
| 265 | ++StateChangeAmounts[stateiter->second];
|
|---|
| 266 | ASSERT( StateChangeAmounts[atomInsertedState] >= StateChangeAmounts[atomRemovedState],
|
|---|
| 267 | "GLWorldScene::moleculeInserted() - more atomRemoved states than atomInserted for atom "
|
|---|
| 268 | +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(const atomId_t, iter->first)); // parameters
|
|---|
| 275 | } else {
|
|---|
| 276 | LOG(1, "INFO: Atom " << iter->first << " has been inserted and removed already.");
|
|---|
| 277 | }
|
|---|
| 278 | } else {
|
|---|
| 279 | // can only be an insertion
|
|---|
| 280 | switch (rangeiter.first->second) {
|
|---|
| 281 | case atomRemovedState:
|
|---|
| 282 | ASSERT( 0,
|
|---|
| 283 | "GLWorldScene::moleculeInserted() - atomRemoved state without atomInserted for atom "
|
|---|
| 284 | +toString(iter->first));
|
|---|
| 285 | break;
|
|---|
| 286 | case atomInsertedState:
|
|---|
| 287 | LOG(1, "INFO: invoking atomInserted for atom " << iter->first);
|
|---|
| 288 | QMetaObject::invokeMethod(molObject, // pointer to a QObject
|
|---|
| 289 | "atomInserted", // member name (no parameters here)
|
|---|
| 290 | Qt::QueuedConnection, // connection type
|
|---|
| 291 | Q_ARG(const atomId_t, iter->first)); // parameters
|
|---|
| 292 | break;
|
|---|
| 293 | default:
|
|---|
| 294 | ASSERT( 0,
|
|---|
| 295 | "GLWorldScene::moleculeInserted() - there are unknown change states.");
|
|---|
| 296 | break;
|
|---|
| 297 | }
|
|---|
| 298 | }
|
|---|
| 299 | // removed state changes for this atom
|
|---|
| 300 | MoleculeMissedStateMap[_id].erase(iter);
|
|---|
| 301 | }
|
|---|
| 302 | // remove state change map for the molecule
|
|---|
| 303 | MoleculeMissedStateMap.erase(_id);
|
|---|
| 304 | }
|
|---|
| 305 |
|
|---|
| 306 | connect (molObject, SIGNAL(changed()), this, SIGNAL(changed()));
|
|---|
| 307 | connect (molObject, SIGNAL(changeOccured()), this, SIGNAL(changeOccured()));
|
|---|
| 308 | connect (molObject, SIGNAL(atomClicked(atomId_t)), this, SLOT(atomClicked(atomId_t)));
|
|---|
| 309 | connect (molObject, SIGNAL(moleculeClicked(moleculeId_t)), this, SLOT(moleculeClicked(moleculeId_t)));
|
|---|
| 310 | connect (molObject, SIGNAL(selectionChanged()), this, SIGNAL(changed()));
|
|---|
| 311 | connect (molObject, SIGNAL(selectionChanged()), this, SIGNAL(changed()));
|
|---|
| 312 | connect (molObject, SIGNAL(hoverChanged(const atomId_t)), this, SIGNAL(hoverChanged(const atomId_t)));
|
|---|
| 313 | connect (molObject, SIGNAL(hoverChanged(const moleculeId_t, int)), this, SIGNAL(hoverChanged(const moleculeId_t, int)));
|
|---|
| 314 | connect (molObject, SIGNAL(hoverChanged(const moleculeId_t, int)), this, SIGNAL(hoverChanged(const moleculeId_t, int)));
|
|---|
| 315 |
|
|---|
| 316 | emit changed();
|
|---|
| 317 | emit changeOccured();
|
|---|
| 318 | }
|
|---|
| 319 |
|
|---|
| 320 | /** Removes a molecule from the scene.
|
|---|
| 321 | *
|
|---|
| 322 | * @param _id id of molecule to remove
|
|---|
| 323 | */
|
|---|
| 324 | void GLWorldScene::moleculeRemoved(const moleculeId_t _id)
|
|---|
| 325 | {
|
|---|
| 326 | LOG(3, "INFO: GLWorldScene: Received signal moleculeRemoved for molecule "+toString(_id)+".");
|
|---|
| 327 |
|
|---|
| 328 | boost::recursive_mutex::scoped_lock lock(MoleculeinSceneMap_mutex);
|
|---|
| 329 |
|
|---|
| 330 | MoleculeNodeMap::iterator iter = MoleculesinSceneMap.find(_id);
|
|---|
| 331 | if ( iter == MoleculesinSceneMap.end()) {
|
|---|
| 332 | RemovalMolecules.insert(_id);
|
|---|
| 333 | } else {
|
|---|
| 334 | LOG(1, "DEBUG: Removing GLMoleculeObject_molecule to id " << _id);
|
|---|
| 335 | GLMoleculeObject_molecule *molObject = iter->second;
|
|---|
| 336 | molObject->disconnect();
|
|---|
| 337 | MoleculesinSceneMap.erase(iter);
|
|---|
| 338 | delete molObject;
|
|---|
| 339 | }
|
|---|
| 340 |
|
|---|
| 341 | // remove any possible state changes left
|
|---|
| 342 | {
|
|---|
| 343 | boost::recursive_mutex::scoped_lock lock(MoleculeMissedStateMap_mutex);
|
|---|
| 344 | MoleculeMissedStateMap.erase(_id);
|
|---|
| 345 | }
|
|---|
| 346 |
|
|---|
| 347 | emit changed();
|
|---|
| 348 | emit changeOccured();
|
|---|
| 349 | }
|
|---|
| 350 |
|
|---|
| 351 | void GLWorldScene::moleculeIndexChanged(const moleculeId_t _oldid, const moleculeId_t _newid)
|
|---|
| 352 | {
|
|---|
| 353 | MoleculeNodeMap::iterator iter = MoleculesinSceneMap.find(_oldid);
|
|---|
| 354 | if ( iter == MoleculesinSceneMap.end()) {
|
|---|
| 355 | RemovalMolecule_t::iterator removeiter =
|
|---|
| 356 | RemovalMolecules.find(_oldid);
|
|---|
| 357 | ASSERT( removeiter != RemovalMolecules.end(),
|
|---|
| 358 | "GLWorldScene::moleculeIndexChanged() - cannot find id "+toString(_oldid)+" in any map.");
|
|---|
| 359 | RemovalMolecules.erase(removeiter);
|
|---|
| 360 | RemovalMolecules.insert(_newid);
|
|---|
| 361 | } else {
|
|---|
| 362 | LOG(1, "DEBUG: Changing GLMoleculeObject_molecule from " << _oldid << " to id " << _newid);
|
|---|
| 363 | GLMoleculeObject_molecule* molObject = iter->second;
|
|---|
| 364 | MoleculesinSceneMap.erase(iter);
|
|---|
| 365 | MoleculesinSceneMap.insert(
|
|---|
| 366 | std::make_pair( _newid, molObject) );
|
|---|
| 367 | }
|
|---|
| 368 | }
|
|---|
| 369 |
|
|---|
| 370 | void GLWorldScene::moleculesVisibilityChanged(const moleculeId_t _id, bool _visible)
|
|---|
| 371 | {
|
|---|
| 372 | boost::recursive_mutex::scoped_lock lock(MoleculeinSceneMap_mutex);
|
|---|
| 373 | MoleculeNodeMap::iterator iter = MoleculesinSceneMap.find(_id);
|
|---|
| 374 | ASSERT( iter != MoleculesinSceneMap.end(),
|
|---|
| 375 | "GLWorldScene::moleculeInserted() - molecule's id "+toString(_id)+" is unknown.");
|
|---|
| 376 |
|
|---|
| 377 | GLMoleculeObject_molecule *molObject = iter->second;
|
|---|
| 378 | molObject->setVisible(_visible);
|
|---|
| 379 |
|
|---|
| 380 | emit changed();
|
|---|
| 381 | emit changeOccured();
|
|---|
| 382 | }
|
|---|
| 383 |
|
|---|
| 384 | /** Adds a shape to the scene.
|
|---|
| 385 | *
|
|---|
| 386 | */
|
|---|
| 387 | void GLWorldScene::addShape(const std::string &_name)
|
|---|
| 388 | {
|
|---|
| 389 | Shape * const shape = ShapeRegistry::getInstance().getByName(_name);
|
|---|
| 390 | if (shape != NULL) {
|
|---|
| 391 | GLMoleculeObject_shape *shapeObject = new GLMoleculeObject_shape(*shape, this);
|
|---|
| 392 | ShapeNodeMap::iterator iter = ShapesinSceneMap.find(_name);
|
|---|
| 393 | ASSERT(iter == ShapesinSceneMap.end(),
|
|---|
| 394 | "GLWorldScene::addShape() - same shape "+_name+" added again.");
|
|---|
| 395 | ShapesinSceneMap.insert( make_pair(_name, shapeObject) );
|
|---|
| 396 | } else
|
|---|
| 397 | ELOG(2, "GLWorldScene::addShape() - shape disappeared before we could draw it.");
|
|---|
| 398 |
|
|---|
| 399 | emit changed();
|
|---|
| 400 | }
|
|---|
| 401 |
|
|---|
| 402 | void GLWorldScene::removeShape(const std::string &_name)
|
|---|
| 403 | {
|
|---|
| 404 | ShapeNodeMap::iterator iter = ShapesinSceneMap.find(_name);
|
|---|
| 405 | ASSERT(iter != ShapesinSceneMap.end(),
|
|---|
| 406 | "GLWorldScene::removeShape() - shape "+_name+" not in scene.");
|
|---|
| 407 | ShapesinSceneMap.erase(iter);
|
|---|
| 408 | delete(iter->second);
|
|---|
| 409 |
|
|---|
| 410 | emit changed();
|
|---|
| 411 | }
|
|---|
| 412 |
|
|---|
| 413 | void GLWorldScene::updateSelectedShapes()
|
|---|
| 414 | {
|
|---|
| 415 | foreach (QObject *obj, children()) {
|
|---|
| 416 | GLMoleculeObject_shape *shapeobj = qobject_cast<GLMoleculeObject_shape *>(obj);
|
|---|
| 417 | if (shapeobj){
|
|---|
| 418 | shapeobj->enable(ShapeRegistry::getInstance().isSelected(shapeobj->getShape()));
|
|---|
| 419 | }
|
|---|
| 420 | }
|
|---|
| 421 |
|
|---|
| 422 | emit changed();
|
|---|
| 423 | }
|
|---|
| 424 |
|
|---|
| 425 | void GLWorldScene::initialize(QGLView *view, QGLPainter *painter) const
|
|---|
| 426 | {
|
|---|
| 427 | // Initialize all of the mesh objects that we have as children.
|
|---|
| 428 | foreach (QObject *obj, children()) {
|
|---|
| 429 | GLMoleculeObject *meshobj = qobject_cast<GLMoleculeObject *>(obj);
|
|---|
| 430 | if (meshobj)
|
|---|
| 431 | meshobj->initialize(view, painter);
|
|---|
| 432 | }
|
|---|
| 433 | }
|
|---|
| 434 |
|
|---|
| 435 | void GLWorldScene::draw(QGLPainter *painter, const QVector4D &cameraPlane) const
|
|---|
| 436 | {
|
|---|
| 437 | // Draw all of the mesh objects that we have as children.
|
|---|
| 438 | foreach (QObject *obj, children()) {
|
|---|
| 439 | GLMoleculeObject *meshobj = qobject_cast<GLMoleculeObject *>(obj);
|
|---|
| 440 | if (meshobj)
|
|---|
| 441 | meshobj->draw(painter, cameraPlane);
|
|---|
| 442 | }
|
|---|
| 443 | }
|
|---|
| 444 |
|
|---|
| 445 | void GLWorldScene::setSelectionMode(SelectionModeType mode)
|
|---|
| 446 | {
|
|---|
| 447 | selectionMode = mode;
|
|---|
| 448 | // TODO send update to toolbar
|
|---|
| 449 | }
|
|---|
| 450 |
|
|---|
| 451 | void GLWorldScene::setSelectionChangedAgent(QtSelectionChangedAgent *agent)
|
|---|
| 452 | {
|
|---|
| 453 | connect(agent, SIGNAL(atomSelected(const moleculeId_t, const atomId_t)),
|
|---|
| 454 | this, SLOT(AtomSelected(const moleculeId_t, const atomId_t)));
|
|---|
| 455 | connect(agent, SIGNAL(atomUnselected(const moleculeId_t, const atomId_t)),
|
|---|
| 456 | this, SLOT(AtomUnselected(const moleculeId_t, const atomId_t)));
|
|---|
| 457 |
|
|---|
| 458 | connect(agent, SIGNAL(moleculeSelected(const moleculeId_t)),
|
|---|
| 459 | this, SLOT(MoleculeSelected(const moleculeId_t)));
|
|---|
| 460 | connect(agent, SIGNAL(moleculeUnselected(const moleculeId_t)),
|
|---|
| 461 | this, SLOT(MoleculeUnselected(const moleculeId_t)));
|
|---|
| 462 | }
|
|---|
| 463 |
|
|---|
| 464 | void GLWorldScene::setSelectionModeAtom()
|
|---|
| 465 | {
|
|---|
| 466 | setSelectionMode(SelectAtom);
|
|---|
| 467 | }
|
|---|
| 468 |
|
|---|
| 469 | void GLWorldScene::setSelectionModeMolecule()
|
|---|
| 470 | {
|
|---|
| 471 | setSelectionMode(SelectMolecule);
|
|---|
| 472 | }
|
|---|
| 473 |
|
|---|
| 474 | void GLWorldScene::changeMoleculeId(
|
|---|
| 475 | GLMoleculeObject_molecule *ob,
|
|---|
| 476 | const moleculeId_t oldId,
|
|---|
| 477 | const moleculeId_t newId)
|
|---|
| 478 | {
|
|---|
| 479 | LOG(3, "INFO: GLWorldScene - change molecule id " << oldId << " to " << newId << ".");
|
|---|
| 480 |
|
|---|
| 481 | {
|
|---|
| 482 | boost::recursive_mutex::scoped_lock lock(MoleculeinSceneMap_mutex);
|
|---|
| 483 | // Remove from map.
|
|---|
| 484 | MoleculeNodeMap::iterator iter = MoleculesinSceneMap.find(oldId);
|
|---|
| 485 | ASSERT(iter != MoleculesinSceneMap.end(),
|
|---|
| 486 | "GLWorldScene::changeMoleculeId() - molecule with old id "+toString(oldId)+" not on display.");
|
|---|
| 487 | ASSERT(iter->second == ob,
|
|---|
| 488 | "GLWorldScene::changeMoleculeId() - molecule with id "
|
|---|
| 489 | +toString(oldId)+" does not match with object in MoleculesinSceneMap.");
|
|---|
| 490 | MoleculesinSceneMap.erase(iter);
|
|---|
| 491 |
|
|---|
| 492 | // Reinsert with new id.
|
|---|
| 493 | {
|
|---|
| 494 | MoleculeNodeMap::iterator iter = MoleculesinSceneMap.find(newId);
|
|---|
| 495 | ASSERT(iter == MoleculesinSceneMap.end(),
|
|---|
| 496 | "GLWorldScene::changeMoleculeId() - moleculewith new id "+toString(newId)+" already known.");
|
|---|
| 497 | }
|
|---|
| 498 | MoleculesinSceneMap.insert( make_pair(newId, ob) );
|
|---|
| 499 | }
|
|---|
| 500 |
|
|---|
| 501 | {
|
|---|
| 502 | boost::recursive_mutex::scoped_lock lock(MoleculeMissedStateMap_mutex);
|
|---|
| 503 | // Remove and re-insert from map if present.
|
|---|
| 504 | MoleculeMissedStateMap_t::iterator iter = MoleculeMissedStateMap.find(oldId);
|
|---|
| 505 | if (iter != MoleculeMissedStateMap.end()) {
|
|---|
| 506 | StateChangeMap_t changemap = iter->second;
|
|---|
| 507 | MoleculeMissedStateMap.erase(iter);
|
|---|
| 508 | MoleculeMissedStateMap.insert( std::make_pair(newId, changemap) );
|
|---|
| 509 | }
|
|---|
| 510 | }
|
|---|
| 511 | }
|
|---|
| 512 |
|
|---|
| 513 | void GLWorldScene::AtomSelected(const moleculeId_t _molid, const atomId_t _id)
|
|---|
| 514 | {
|
|---|
| 515 | boost::recursive_mutex::scoped_lock lock(MoleculeinSceneMap_mutex);
|
|---|
| 516 | MoleculeNodeMap::iterator iter = MoleculesinSceneMap.find(_molid);
|
|---|
| 517 | // use direct connection as it's lightweight and we avoid index change troubles
|
|---|
| 518 | if (iter != MoleculesinSceneMap.end())
|
|---|
| 519 | QMetaObject::invokeMethod(iter->second, // pointer to a QObject
|
|---|
| 520 | "AtomSelected", // member name (no parameters here)
|
|---|
| 521 | Qt::DirectConnection, // connection type
|
|---|
| 522 | Q_ARG(const atomId_t, _id)); // parameters
|
|---|
| 523 | else
|
|---|
| 524 | ELOG(2, "DEBUG: GLWorldScene::AtomSelected() - molecule " <<
|
|---|
| 525 | _molid << " unknown to GLWorldScene.");
|
|---|
| 526 | }
|
|---|
| 527 |
|
|---|
| 528 | void GLWorldScene::AtomUnselected(const moleculeId_t _molid, const atomId_t _id)
|
|---|
| 529 | {
|
|---|
| 530 | boost::recursive_mutex::scoped_lock lock(MoleculeinSceneMap_mutex);
|
|---|
| 531 | MoleculeNodeMap::iterator iter = MoleculesinSceneMap.find(_molid);
|
|---|
| 532 | // use direct connection as it's lightweight and we avoid index change troubles
|
|---|
| 533 | if (iter != MoleculesinSceneMap.end())
|
|---|
| 534 | QMetaObject::invokeMethod(iter->second, // pointer to a QObject
|
|---|
| 535 | "AtomUnselected", // member name (no parameters here)
|
|---|
| 536 | Qt::DirectConnection, // connection type
|
|---|
| 537 | Q_ARG(const atomId_t, _id)); // parameters
|
|---|
| 538 | else
|
|---|
| 539 | ELOG(2, "GLWorldScene::AtomUnselected() - molecule "
|
|---|
| 540 | << _molid << " unknown to GLWorldScene.");
|
|---|
| 541 | }
|
|---|
| 542 |
|
|---|
| 543 | void GLWorldScene::MoleculeSelected(const moleculeId_t _molid)
|
|---|
| 544 | {
|
|---|
| 545 | boost::recursive_mutex::scoped_lock lock(MoleculeinSceneMap_mutex);
|
|---|
| 546 | MoleculeNodeMap::iterator iter = MoleculesinSceneMap.find(_molid);
|
|---|
| 547 | if (iter != MoleculesinSceneMap.end())
|
|---|
| 548 | QMetaObject::invokeMethod(iter->second, // pointer to a QObject
|
|---|
| 549 | "Selected", // member name (no parameters here)
|
|---|
| 550 | Qt::QueuedConnection); // connection type
|
|---|
| 551 | else
|
|---|
| 552 | ELOG(2, "GLWorldScene::MoleculeSelected() - molecule "
|
|---|
| 553 | << _molid << " unknown to GLWorldScene.");
|
|---|
| 554 | }
|
|---|
| 555 |
|
|---|
| 556 | void GLWorldScene::MoleculeUnselected(const moleculeId_t _molid)
|
|---|
| 557 | {
|
|---|
| 558 | boost::recursive_mutex::scoped_lock lock(MoleculeinSceneMap_mutex);
|
|---|
| 559 | MoleculeNodeMap::iterator iter = MoleculesinSceneMap.find(_molid);
|
|---|
| 560 | if (iter != MoleculesinSceneMap.end())
|
|---|
| 561 | QMetaObject::invokeMethod(iter->second, // pointer to a QObject
|
|---|
| 562 | "Unselected", // member name (no parameters here)
|
|---|
| 563 | Qt::QueuedConnection); // connection type
|
|---|
| 564 | else
|
|---|
| 565 | ELOG(2, "GLWorldScene::MoleculeUnselected() - molecule "
|
|---|
| 566 | << _molid << " unknown to GLWorldScene.");
|
|---|
| 567 | }
|
|---|