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