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