| 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 "GLMoleculeObject.hpp" | 
|---|
| 46 | #include "GLMoleculeObject_atom.hpp" | 
|---|
| 47 | #include "GLMoleculeObject_bond.hpp" | 
|---|
| 48 | #include "GLMoleculeObject_molecule.hpp" | 
|---|
| 49 | #include "GLMoleculeObject_shape.hpp" | 
|---|
| 50 |  | 
|---|
| 51 | #include "CodePatterns/MemDebug.hpp" | 
|---|
| 52 |  | 
|---|
| 53 | #include "CodePatterns/Log.hpp" | 
|---|
| 54 |  | 
|---|
| 55 | #include "Actions/SelectionAction/Atoms/AtomByIdAction.hpp" | 
|---|
| 56 | #include "Actions/SelectionAction/Atoms/NotAtomByIdAction.hpp" | 
|---|
| 57 | #include "Actions/SelectionAction/Molecules/MoleculeByIdAction.hpp" | 
|---|
| 58 | #include "Actions/SelectionAction/Molecules/NotMoleculeByIdAction.hpp" | 
|---|
| 59 | #include "Atom/atom.hpp" | 
|---|
| 60 | #include "Bond/bond.hpp" | 
|---|
| 61 | #include "Descriptors/AtomIdDescriptor.hpp" | 
|---|
| 62 | #include "Descriptors/MoleculeIdDescriptor.hpp" | 
|---|
| 63 | #include "Helpers/helpers.hpp" | 
|---|
| 64 | #include "Shapes/ShapeRegistry.hpp" | 
|---|
| 65 | #include "molecule.hpp" | 
|---|
| 66 | #include "World.hpp" | 
|---|
| 67 |  | 
|---|
| 68 | #include <iostream> | 
|---|
| 69 |  | 
|---|
| 70 | using namespace MoleCuilder; | 
|---|
| 71 |  | 
|---|
| 72 | GLWorldScene::GLWorldScene(QObject *parent) | 
|---|
| 73 | : QObject(parent) | 
|---|
| 74 | { | 
|---|
| 75 | int sphereDetails[] = {5, 3, 2, 0}; | 
|---|
| 76 | int cylinderDetails[] = {16, 8, 6, 3}; | 
|---|
| 77 | for (int i=0;i<GLMoleculeObject::DETAILTYPES_MAX;i++){ | 
|---|
| 78 | QGLBuilder emptyBuilder; | 
|---|
| 79 | GLMoleculeObject::meshEmpty[i] = emptyBuilder.finalizedSceneNode(); | 
|---|
| 80 | QGLBuilder sphereBuilder; | 
|---|
| 81 | sphereBuilder << QGLSphere(2.0, sphereDetails[i]); | 
|---|
| 82 | GLMoleculeObject::meshSphere[i] = sphereBuilder.finalizedSceneNode(); | 
|---|
| 83 | GLMoleculeObject::meshSphere[i]->setOption(QGLSceneNode::CullBoundingBox, true); | 
|---|
| 84 | QGLBuilder cylinderBuilder; | 
|---|
| 85 | cylinderBuilder << QGLCylinder(.25,.25,1.0,cylinderDetails[i]); | 
|---|
| 86 | GLMoleculeObject::meshCylinder[i] = cylinderBuilder.finalizedSceneNode(); | 
|---|
| 87 | GLMoleculeObject::meshCylinder[i]->setOption(QGLSceneNode::CullBoundingBox, true); | 
|---|
| 88 | } | 
|---|
| 89 |  | 
|---|
| 90 | connect(this, SIGNAL(updated()), this, SLOT(update())); | 
|---|
| 91 |  | 
|---|
| 92 |  | 
|---|
| 93 | setSelectionMode(SelectAtom); | 
|---|
| 94 |  | 
|---|
| 95 | init(); | 
|---|
| 96 | } | 
|---|
| 97 |  | 
|---|
| 98 | GLWorldScene::~GLWorldScene() | 
|---|
| 99 | { | 
|---|
| 100 | // remove all elements | 
|---|
| 101 | GLMoleculeObject::cleanMaterialMap(); | 
|---|
| 102 | } | 
|---|
| 103 |  | 
|---|
| 104 | /** Initialise the WorldScene with molecules and atoms from World. | 
|---|
| 105 | * | 
|---|
| 106 | */ | 
|---|
| 107 | void GLWorldScene::init() | 
|---|
| 108 | { | 
|---|
| 109 | const std::vector<molecule *> &molecules = World::getInstance().getAllMolecules(); | 
|---|
| 110 |  | 
|---|
| 111 | for (std::vector<molecule*>::const_iterator moliter = molecules.begin(); | 
|---|
| 112 | moliter != molecules.end(); | 
|---|
| 113 | moliter++) { | 
|---|
| 114 | // create molecule objects in scene | 
|---|
| 115 | moleculeInserted(*moliter); | 
|---|
| 116 | } | 
|---|
| 117 | } | 
|---|
| 118 |  | 
|---|
| 119 | /** Update the WorldScene with molecules and atoms from World. | 
|---|
| 120 | * | 
|---|
| 121 | * This function should be called after e.g. WorldTime::TimeChanged was | 
|---|
| 122 | * received or after another molecule has been loaded. | 
|---|
| 123 | * | 
|---|
| 124 | */ | 
|---|
| 125 | void GLWorldScene::update() | 
|---|
| 126 | { | 
|---|
| 127 | const std::vector<molecule *> &molecules = World::getInstance().getAllMolecules(); | 
|---|
| 128 |  | 
|---|
| 129 | for (std::vector<molecule*>::const_iterator moliter = molecules.begin(); | 
|---|
| 130 | moliter != molecules.end(); | 
|---|
| 131 | moliter++) { | 
|---|
| 132 | // check whether molecule already exists | 
|---|
| 133 | const moleculeId_t molid = (*moliter)->getId(); | 
|---|
| 134 | const bool mol_present = MoleculesinSceneMap.count(molid); | 
|---|
| 135 | if (!mol_present) | 
|---|
| 136 | moleculeInserted(*moliter); | 
|---|
| 137 | } | 
|---|
| 138 | } | 
|---|
| 139 |  | 
|---|
| 140 | void GLWorldScene::atomClicked(atomId_t no) | 
|---|
| 141 | { | 
|---|
| 142 | LOG(3, "INFO: GLMoleculeObject_molecule - atom " << no << " has been clicked."); | 
|---|
| 143 | const atom *Walker = World::getInstance().getAtom(AtomById(no)); | 
|---|
| 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 *mol= World::getInstance().getMolecule(MoleculeById(no)); | 
|---|
| 165 | ASSERT(mol, "Atom without molecule has been clicked."); | 
|---|
| 166 | molids_t ids(1, mol->getId()); | 
|---|
| 167 | if (!World::getInstance().isSelected(mol)) | 
|---|
| 168 | SelectionMoleculeById(ids); | 
|---|
| 169 | else | 
|---|
| 170 | SelectionNotMoleculeById(ids); | 
|---|
| 171 | emit clicked(no); | 
|---|
| 172 | } | 
|---|
| 173 |  | 
|---|
| 174 |  | 
|---|
| 175 | /** Adds an atom to the scene. | 
|---|
| 176 | * | 
|---|
| 177 | * @param _atom atom to add | 
|---|
| 178 | */ | 
|---|
| 179 | void GLWorldScene::atomInserted(const atomId_t _id) | 
|---|
| 180 | { | 
|---|
| 181 | LOG(3, "INFO: GLWorldScene: Received signal atomInserted for atom "+toString(_id)+"."); | 
|---|
| 182 | // find associated molecule | 
|---|
| 183 | const moleculeId_t molid = World::getInstance().getAtom(AtomById(_id))->getMolecule()->getId(); | 
|---|
| 184 | MoleculeNodeMap::const_iterator moliter = MoleculesinSceneMap.find(molid ); | 
|---|
| 185 | ASSERT(moliter != MoleculesinSceneMap.end(), | 
|---|
| 186 | "GLWorldScene::atomAdded() - molecule with id of "+toString(molid) | 
|---|
| 187 | +" atom with id "+toString(_id)+" is unknown."); | 
|---|
| 188 | GLMoleculeObject_molecule *molObject = moliter->second; | 
|---|
| 189 |  | 
|---|
| 190 | // add atom to internal list | 
|---|
| 191 | #ifndef NDEBUG | 
|---|
| 192 | AtomMoleculeMap::const_iterator atomiter = AtomsinSceneMap.find(_id); | 
|---|
| 193 | ASSERT(atomiter == AtomsinSceneMap.end(), | 
|---|
| 194 | "GLWorldScene::atomRemoved() - atom "+toString(_id)+" already known."); | 
|---|
| 195 | #endif | 
|---|
| 196 | AtomsinSceneMap.insert( std::make_pair( _id, molObject )); | 
|---|
| 197 |  | 
|---|
| 198 | // inform its GlMoleculeObject_molecule. | 
|---|
| 199 | molObject->atomInserted(_id); | 
|---|
| 200 |  | 
|---|
| 201 | // emit change | 
|---|
| 202 | emit changeOccured(); | 
|---|
| 203 | } | 
|---|
| 204 |  | 
|---|
| 205 | /** Removes an atom from the scene. | 
|---|
| 206 | * | 
|---|
| 207 | * We just the id as the atom might have already been destroyed. | 
|---|
| 208 | * | 
|---|
| 209 | * @param _id id of atom to remove | 
|---|
| 210 | */ | 
|---|
| 211 | void GLWorldScene::atomRemoved(const atomId_t _id) | 
|---|
| 212 | { | 
|---|
| 213 | LOG(3, "INFO: GLWorldScene: Received signal atomRemoved for atom "+toString(_id)+"."); | 
|---|
| 214 | // find associated molecule | 
|---|
| 215 | AtomMoleculeMap::iterator iter = AtomsinSceneMap.find(_id); | 
|---|
| 216 | ASSERT(iter != AtomsinSceneMap.end(), | 
|---|
| 217 | "GLWorldScene::atomRemoved() - atom "+toString(_id)+" not on display."); | 
|---|
| 218 | GLMoleculeObject_molecule *molObject = iter->second; | 
|---|
| 219 |  | 
|---|
| 220 | // remove from internal list | 
|---|
| 221 | AtomsinSceneMap.erase(iter); | 
|---|
| 222 |  | 
|---|
| 223 | // inform its GlMoleculeObject_molecule. | 
|---|
| 224 | molObject->atomRemoved(_id); | 
|---|
| 225 |  | 
|---|
| 226 | // emit change | 
|---|
| 227 | emit changeOccured(); | 
|---|
| 228 | } | 
|---|
| 229 |  | 
|---|
| 230 | /** .... | 
|---|
| 231 | * | 
|---|
| 232 | */ | 
|---|
| 233 | void GLWorldScene::worldSelectionChanged() | 
|---|
| 234 | { | 
|---|
| 235 | LOG(3, "INFO: GLWorldScene: Received signal selectionChanged."); | 
|---|
| 236 |  | 
|---|
| 237 | const std::vector<molecule*> &molecules = World::getInstance().getAllMolecules(); | 
|---|
| 238 |  | 
|---|
| 239 | if (molecules.size() > 0) { | 
|---|
| 240 | for (std::vector<molecule*>::const_iterator Runner = molecules.begin(); | 
|---|
| 241 | Runner != molecules.end(); | 
|---|
| 242 | Runner++) { | 
|---|
| 243 |  | 
|---|
| 244 | // molecule selected but not in scene? | 
|---|
| 245 | const bool isSelected = World::getInstance().isSelected(*Runner); | 
|---|
| 246 | if (isSelected){ | 
|---|
| 247 | MoleculeNodeMap::iterator iter = MoleculesinSceneMap.find((*Runner)->getId()); | 
|---|
| 248 | ASSERT( iter != MoleculesinSceneMap.end(), | 
|---|
| 249 | "GLWorldScene::worldSelectionChanged() - selected molecule is unknown."); | 
|---|
| 250 | GLMoleculeObject_molecule *molObject = iter->second; | 
|---|
| 251 | // inform molecule object | 
|---|
| 252 | molObject->selected(isSelected); | 
|---|
| 253 | } | 
|---|
| 254 | } | 
|---|
| 255 | } | 
|---|
| 256 | } | 
|---|
| 257 |  | 
|---|
| 258 | /** Inserts a molecule into the scene. | 
|---|
| 259 | * | 
|---|
| 260 | * @param _mol molecule to insert | 
|---|
| 261 | */ | 
|---|
| 262 | void GLWorldScene::moleculeInserted(const molecule * _mol) | 
|---|
| 263 | { | 
|---|
| 264 | LOG(3, "INFO: GLWorldScene: Received signal moleculeRemoved for molecule "+toString(_mol->getId())+"."); | 
|---|
| 265 | MoleculeNodeMap::const_iterator iter = MoleculesinSceneMap.find(_mol->getId()); | 
|---|
| 266 | ASSERT( iter == MoleculesinSceneMap.end(), | 
|---|
| 267 | "GLWorldScene::moleculeInserted() - molecule's id "+toString(_mol->getId())+" already present."); | 
|---|
| 268 |  | 
|---|
| 269 | // add new object | 
|---|
| 270 | GLMoleculeObject_molecule *molObject = new GLMoleculeObject_molecule(GLMoleculeObject::meshEmpty, this, _mol); | 
|---|
| 271 | MoleculesinSceneMap.insert( make_pair(_mol->getId(), molObject) ); | 
|---|
| 272 | connect (molObject, SIGNAL(changed()), this, SIGNAL(changed())); | 
|---|
| 273 | connect (molObject, SIGNAL(changeOccured()), this, SIGNAL(changeOccured())); | 
|---|
| 274 | connect (molObject, SIGNAL(atomClicked(atomId_t)), this, SLOT(atomClicked(atomId_t))); | 
|---|
| 275 | connect (molObject, SIGNAL(moleculeClicked(moleculeId_t)), this, SLOT(moleculeClicked(moleculeId_t))); | 
|---|
| 276 | connect (molObject, SIGNAL(changeAtomId(GLMoleculeObject_atom *, int, int)), this, SLOT(changeAtomId(GLMoleculeObject_atom *, int, int))); | 
|---|
| 277 | connect (molObject, SIGNAL(selectionChanged()), this, SIGNAL(changed())); | 
|---|
| 278 | connect (molObject, SIGNAL(selectionChanged()), this, SIGNAL(changed())); | 
|---|
| 279 | connect (molObject, SIGNAL(hoverChanged(const atom &)), this, SIGNAL(hoverChanged(const atom &))); | 
|---|
| 280 | connect (molObject, SIGNAL(hoverChanged(const molecule &, int)), this, SIGNAL(hoverChanged(const molecule &, int))); | 
|---|
| 281 | emit changed(); | 
|---|
| 282 | emit changeOccured(); | 
|---|
| 283 | } | 
|---|
| 284 |  | 
|---|
| 285 | /** Removes a molecule from the scene. | 
|---|
| 286 | * | 
|---|
| 287 | * @param _id id of molecule to remove | 
|---|
| 288 | */ | 
|---|
| 289 | void GLWorldScene::moleculeRemoved(const moleculeId_t _id) | 
|---|
| 290 | { | 
|---|
| 291 | LOG(3, "INFO: GLWorldScene: Received signal moleculeRemoved for molecule "+toString(_id)+"."); | 
|---|
| 292 | MoleculeNodeMap::iterator iter = MoleculesinSceneMap.find(_id); | 
|---|
| 293 | ASSERT( iter != MoleculesinSceneMap.end(), | 
|---|
| 294 | "GLWorldScene::moleculeInserted() - molecule's id "+toString(_id)+" is unknown."); | 
|---|
| 295 |  | 
|---|
| 296 | GLMoleculeObject_molecule *molObject = iter->second; | 
|---|
| 297 | molObject->disconnect(); | 
|---|
| 298 | MoleculesinSceneMap.erase(iter); | 
|---|
| 299 | delete molObject; | 
|---|
| 300 | emit changed(); | 
|---|
| 301 | emit changeOccured(); | 
|---|
| 302 | } | 
|---|
| 303 |  | 
|---|
| 304 | void GLWorldScene::moleculesVisibilityChanged(const moleculeId_t _id, bool _visible) | 
|---|
| 305 | { | 
|---|
| 306 | MoleculeNodeMap::iterator iter = MoleculesinSceneMap.find(_id); | 
|---|
| 307 | ASSERT( iter != MoleculesinSceneMap.end(), | 
|---|
| 308 | "GLWorldScene::moleculeInserted() - molecule's id "+toString(_id)+" is unknown."); | 
|---|
| 309 |  | 
|---|
| 310 | GLMoleculeObject_molecule *molObject = iter->second; | 
|---|
| 311 | molObject->setVisible(_visible); | 
|---|
| 312 |  | 
|---|
| 313 | emit changed(); | 
|---|
| 314 | emit changeOccured(); | 
|---|
| 315 | } | 
|---|
| 316 |  | 
|---|
| 317 | /** Adds a bond to the scene. | 
|---|
| 318 | * | 
|---|
| 319 | * @param _bond bond to add | 
|---|
| 320 | * @param side which side of the bond (left or right) | 
|---|
| 321 | */ | 
|---|
| 322 | void GLWorldScene::bondInserted(const bond::ptr _bond, const enum GLMoleculeObject_bond::SideOfBond _side) | 
|---|
| 323 | { | 
|---|
| 324 | LOG(3, "INFO: GLWorldScene::bondInserted() - Adding bond "+toString(*_bond)+"."); | 
|---|
| 325 | //LOG(4, "INFO: Currently present bonds " << BondsinSceneMap << "."); | 
|---|
| 326 | // bond partners must both belong to same atom | 
|---|
| 327 | ASSERT( _bond->leftatom->getMolecule() == _bond->rightatom->getMolecule(), | 
|---|
| 328 | "GLWorldScene::bondInserted() - bond partners do not belong to same molecule."); | 
|---|
| 329 |  | 
|---|
| 330 | // find associated molecule object | 
|---|
| 331 | const moleculeId_t molid = _bond->leftatom->getMolecule()->getId(); | 
|---|
| 332 | MoleculeNodeMap::const_iterator moliter = MoleculesinSceneMap.find(molid ); | 
|---|
| 333 | ASSERT(moliter != MoleculesinSceneMap.end(), | 
|---|
| 334 | "GLWorldScene::bondInserted() - molecule with id of "+toString(molid) | 
|---|
| 335 | +" atom with id "+toString(_bond->leftatom->getId())+" is unknown."); | 
|---|
| 336 | GLMoleculeObject_molecule *molObject = moliter->second; | 
|---|
| 337 |  | 
|---|
| 338 | // add to internal list | 
|---|
| 339 | const GLMoleculeObject_molecule::BondIds ids = | 
|---|
| 340 | GLMoleculeObject_molecule::getBondIds(_bond, _side); | 
|---|
| 341 | BondMoleculeMap::const_iterator iter = BondsinSceneMap.find(ids); | 
|---|
| 342 | ASSERT(iter == BondsinSceneMap.end(), | 
|---|
| 343 | "GLWorldScene::bondInserted() - bond with ids "+toString(ids.first) | 
|---|
| 344 | +","+toString(ids.second)+" already present."); | 
|---|
| 345 | BondsinSceneMap.insert( std::make_pair( ids, molObject )); | 
|---|
| 346 |  | 
|---|
| 347 | // inform its GlMoleculeObject_molecule. | 
|---|
| 348 | molObject->bondInserted(_bond, _side); | 
|---|
| 349 |  | 
|---|
| 350 | // emit change | 
|---|
| 351 | emit changeOccured(); | 
|---|
| 352 | } | 
|---|
| 353 |  | 
|---|
| 354 | /** Removes a bond from the scene. | 
|---|
| 355 | * | 
|---|
| 356 | * @param _bond bond to remove | 
|---|
| 357 | */ | 
|---|
| 358 | void GLWorldScene::bondRemoved(const atomId_t leftnr, const atomId_t rightnr) | 
|---|
| 359 | { | 
|---|
| 360 | LOG(3, "INFO: GLWorldScene::bondRemoved() - Removing bond between "+toString(leftnr)+" and "+toString(rightnr)+"."); | 
|---|
| 361 | { | 
|---|
| 362 | // left bond | 
|---|
| 363 | const GLMoleculeObject_molecule::BondIds Leftids( make_pair(leftnr, rightnr) ); | 
|---|
| 364 | BondMoleculeMap::iterator leftiter = BondsinSceneMap.find( Leftids ); | 
|---|
| 365 | ASSERT(leftiter != BondsinSceneMap.end(), | 
|---|
| 366 | "GLWorldScene::bondRemoved() - bond "+toString(leftnr)+"-" | 
|---|
| 367 | +toString(rightnr)+" not on display."); | 
|---|
| 368 | GLMoleculeObject_molecule *molObject = leftiter->second; | 
|---|
| 369 |  | 
|---|
| 370 | // remove from internal list | 
|---|
| 371 | BondsinSceneMap.erase(leftiter); | 
|---|
| 372 |  | 
|---|
| 373 | // inform its GlMoleculeObject_molecule. | 
|---|
| 374 | molObject->bondRemoved( leftnr, rightnr ); | 
|---|
| 375 | } | 
|---|
| 376 |  | 
|---|
| 377 | // emit change | 
|---|
| 378 | emit changeOccured(); | 
|---|
| 379 | } | 
|---|
| 380 |  | 
|---|
| 381 | /** Adds a shape to the scene. | 
|---|
| 382 | * | 
|---|
| 383 | * uses ShapeRegistry::lastChanged() | 
|---|
| 384 | * | 
|---|
| 385 | */ | 
|---|
| 386 | void GLWorldScene::addShape() | 
|---|
| 387 | { | 
|---|
| 388 | Shape &shape = *ShapeRegistry::getInstance().lastChanged(); | 
|---|
| 389 | GLMoleculeObject_shape *shapeObject = new GLMoleculeObject_shape(shape, this); | 
|---|
| 390 | ShapeNodeMap::iterator iter = ShapesinSceneMap.find(shape.getName()); | 
|---|
| 391 | ASSERT(iter == ShapesinSceneMap.end(), | 
|---|
| 392 | "GLWorldScene::addShape() - same shape "+shape.getName()+" added again."); | 
|---|
| 393 | ShapesinSceneMap.insert( make_pair(shape.getName(), shapeObject) ); | 
|---|
| 394 | } | 
|---|
| 395 |  | 
|---|
| 396 | void GLWorldScene::removeShape() | 
|---|
| 397 | { | 
|---|
| 398 | Shape &shape = *ShapeRegistry::getInstance().lastChanged(); | 
|---|
| 399 | ShapeNodeMap::iterator iter = ShapesinSceneMap.find(shape.getName()); | 
|---|
| 400 | ASSERT(iter != ShapesinSceneMap.end(), | 
|---|
| 401 | "GLWorldScene::removeShape() - shape "+shape.getName()+" not in scene."); | 
|---|
| 402 | ShapesinSceneMap.erase(iter); | 
|---|
| 403 | delete(iter->second); | 
|---|
| 404 | } | 
|---|
| 405 |  | 
|---|
| 406 | void GLWorldScene::updateSelectedShapes() | 
|---|
| 407 | { | 
|---|
| 408 | foreach (QObject *obj, children()) { | 
|---|
| 409 | GLMoleculeObject_shape *shapeobj = qobject_cast<GLMoleculeObject_shape *>(obj); | 
|---|
| 410 | if (shapeobj){ | 
|---|
| 411 | shapeobj->enable(ShapeRegistry::getInstance().isSelected(shapeobj->getShape())); | 
|---|
| 412 | } | 
|---|
| 413 | } | 
|---|
| 414 | } | 
|---|
| 415 |  | 
|---|
| 416 | void GLWorldScene::initialize(QGLView *view, QGLPainter *painter) const | 
|---|
| 417 | { | 
|---|
| 418 | // Initialize all of the mesh objects that we have as children. | 
|---|
| 419 | foreach (QObject *obj, children()) { | 
|---|
| 420 | GLMoleculeObject *meshobj = qobject_cast<GLMoleculeObject *>(obj); | 
|---|
| 421 | if (meshobj) | 
|---|
| 422 | meshobj->initialize(view, painter); | 
|---|
| 423 | } | 
|---|
| 424 | } | 
|---|
| 425 |  | 
|---|
| 426 | void GLWorldScene::draw(QGLPainter *painter, const QVector4D &cameraPlane) const | 
|---|
| 427 | { | 
|---|
| 428 | // Draw all of the mesh objects that we have as children. | 
|---|
| 429 | foreach (QObject *obj, children()) { | 
|---|
| 430 | GLMoleculeObject *meshobj = qobject_cast<GLMoleculeObject *>(obj); | 
|---|
| 431 | if (meshobj) | 
|---|
| 432 | meshobj->draw(painter, cameraPlane); | 
|---|
| 433 | } | 
|---|
| 434 | } | 
|---|
| 435 |  | 
|---|
| 436 | void GLWorldScene::setSelectionMode(SelectionModeType mode) | 
|---|
| 437 | { | 
|---|
| 438 | selectionMode = mode; | 
|---|
| 439 | // TODO send update to toolbar | 
|---|
| 440 | } | 
|---|
| 441 |  | 
|---|
| 442 | void GLWorldScene::setSelectionModeAtom() | 
|---|
| 443 | { | 
|---|
| 444 | setSelectionMode(SelectAtom); | 
|---|
| 445 | } | 
|---|
| 446 |  | 
|---|
| 447 | void GLWorldScene::setSelectionModeMolecule() | 
|---|
| 448 | { | 
|---|
| 449 | setSelectionMode(SelectMolecule); | 
|---|
| 450 | } | 
|---|
| 451 |  | 
|---|
| 452 | void GLWorldScene::changeAtomId(GLMoleculeObject_atom *ob, int oldId, int newId) | 
|---|
| 453 | { | 
|---|
| 454 | LOG(3, "INFO: GLWorldScene - change atom id " << oldId << " to " << newId << "."); | 
|---|
| 455 | // Remove from map. | 
|---|
| 456 | AtomMoleculeMap::iterator iter = AtomsinSceneMap.find(oldId); | 
|---|
| 457 | ASSERT(iter != AtomsinSceneMap.end(), | 
|---|
| 458 | "GLWorldScene::changeAtomId() - atom with old id "+toString(oldId)+" not on display."); | 
|---|
| 459 | GLMoleculeObject_molecule *molObject = iter->second; | 
|---|
| 460 |  | 
|---|
| 461 | // erase by signalling removal | 
|---|
| 462 | molObject->atomRemoved(oldId); | 
|---|
| 463 |  | 
|---|
| 464 | // remove from internal list | 
|---|
| 465 | AtomsinSceneMap.erase(iter); | 
|---|
| 466 |  | 
|---|
| 467 | // Reinsert with new id. | 
|---|
| 468 | { | 
|---|
| 469 | AtomMoleculeMap::iterator iter = AtomsinSceneMap.find(newId); | 
|---|
| 470 | ASSERT(iter == AtomsinSceneMap.end(), | 
|---|
| 471 | "GLWorldScene::changeAtomId() - atom with new id "+toString(newId)+" already known."); | 
|---|
| 472 | } | 
|---|
| 473 | AtomsinSceneMap.insert( make_pair(newId, molObject) ); | 
|---|
| 474 |  | 
|---|
| 475 | // inform molecule object | 
|---|
| 476 | molObject->atomInserted(oldId); | 
|---|
| 477 | } | 
|---|