| 1 | /*
 | 
|---|
| 2 |  * Project: MoleCuilder
 | 
|---|
| 3 |  * Description: creates and alters molecular systems
 | 
|---|
| 4 |  * Copyright (C)  2010-2012 University of Bonn. All rights reserved.
 | 
|---|
| 5 |  * Please see the LICENSE file or "Copyright notice" in builder.cpp for details.
 | 
|---|
| 6 |  */
 | 
|---|
| 7 | 
 | 
|---|
| 8 | /*
 | 
|---|
| 9 |  * GLWorldScene.cpp
 | 
|---|
| 10 |  *
 | 
|---|
| 11 |  *  This is based on the Qt3D example "teaservice", specifically parts of teaservice.cpp.
 | 
|---|
| 12 |  *
 | 
|---|
| 13 |  *  Created on: Aug 17, 2011
 | 
|---|
| 14 |  *      Author: heber
 | 
|---|
| 15 |  */
 | 
|---|
| 16 | 
 | 
|---|
| 17 | // include config.h
 | 
|---|
| 18 | #ifdef HAVE_CONFIG_H
 | 
|---|
| 19 | #include <config.h>
 | 
|---|
| 20 | #endif
 | 
|---|
| 21 | 
 | 
|---|
| 22 | #include "GLWorldScene.hpp"
 | 
|---|
| 23 | 
 | 
|---|
| 24 | #include "GLMoleculeObject.hpp"
 | 
|---|
| 25 | #include "GLMoleculeObject_atom.hpp"
 | 
|---|
| 26 | #include "GLMoleculeObject_bond.hpp"
 | 
|---|
| 27 | #include "GLMoleculeObject_molecule.hpp"
 | 
|---|
| 28 | 
 | 
|---|
| 29 | #include "CodePatterns/MemDebug.hpp"
 | 
|---|
| 30 | 
 | 
|---|
| 31 | #include "CodePatterns/Log.hpp"
 | 
|---|
| 32 | 
 | 
|---|
| 33 | #include "Actions/SelectionAction/Atoms/AtomByIdAction.hpp"
 | 
|---|
| 34 | #include "Actions/SelectionAction/Atoms/NotAtomByIdAction.hpp"
 | 
|---|
| 35 | #include "Atom/atom.hpp"
 | 
|---|
| 36 | #include "Bond/bond.hpp"
 | 
|---|
| 37 | #include "Descriptors/AtomIdDescriptor.hpp"
 | 
|---|
| 38 | #include "Helpers/helpers.hpp"
 | 
|---|
| 39 | #include "molecule.hpp"
 | 
|---|
| 40 | #include "World.hpp"
 | 
|---|
| 41 | 
 | 
|---|
| 42 | #include <iostream>
 | 
|---|
| 43 | 
 | 
|---|
| 44 | using namespace MoleCuilder;
 | 
|---|
| 45 | 
 | 
|---|
| 46 | std::ostream &operator<<(std::ostream &ost, const GLWorldScene::BondIds &t)
 | 
|---|
| 47 | {
 | 
|---|
| 48 |   ost << t.first << "," << t.second;
 | 
|---|
| 49 |   return ost;
 | 
|---|
| 50 | }
 | 
|---|
| 51 | 
 | 
|---|
| 52 | GLWorldScene::GLWorldScene(QObject *parent)
 | 
|---|
| 53 |    : QObject(parent)
 | 
|---|
| 54 | {
 | 
|---|
| 55 |   init();
 | 
|---|
| 56 | 
 | 
|---|
| 57 |   //changeMaterials(false);
 | 
|---|
| 58 | }
 | 
|---|
| 59 | 
 | 
|---|
| 60 | GLWorldScene::~GLWorldScene()
 | 
|---|
| 61 | {
 | 
|---|
| 62 |   // remove all elements
 | 
|---|
| 63 |   GLMoleculeObject::cleanMaterialMap();
 | 
|---|
| 64 | }
 | 
|---|
| 65 | 
 | 
|---|
| 66 | /** Initialise the WorldScene with molecules and atoms from World.
 | 
|---|
| 67 |  *
 | 
|---|
| 68 |  */
 | 
|---|
| 69 | void GLWorldScene::init()
 | 
|---|
| 70 | {
 | 
|---|
| 71 |   const std::vector<molecule*> &molecules = World::getInstance().getAllMolecules();
 | 
|---|
| 72 | 
 | 
|---|
| 73 |   if (molecules.size() > 0) {
 | 
|---|
| 74 |     for (std::vector<molecule*>::const_iterator Runner = molecules.begin();
 | 
|---|
| 75 |         Runner != molecules.end();
 | 
|---|
| 76 |         Runner++) {
 | 
|---|
| 77 | 
 | 
|---|
| 78 |       for (molecule::const_iterator atomiter = (*Runner)->begin();
 | 
|---|
| 79 |           atomiter != (*Runner)->end();
 | 
|---|
| 80 |           ++atomiter) {
 | 
|---|
| 81 |         // create atom objects in scene
 | 
|---|
| 82 |         atomInserted(*atomiter);
 | 
|---|
| 83 | 
 | 
|---|
| 84 |         // create bond objects in scene
 | 
|---|
| 85 |         const BondList &bondlist = (*atomiter)->getListOfBonds();
 | 
|---|
| 86 |         for (BondList::const_iterator bonditer = bondlist.begin();
 | 
|---|
| 87 |             bonditer != bondlist.end();
 | 
|---|
| 88 |             ++bonditer) {
 | 
|---|
| 89 |           const bond *_bond = *bonditer;
 | 
|---|
| 90 |           const GLMoleculeObject_bond::SideOfBond side = (_bond->leftatom == *atomiter) ?
 | 
|---|
| 91 |               GLMoleculeObject_bond::left : GLMoleculeObject_bond::right;
 | 
|---|
| 92 |           bondInserted(_bond, side);
 | 
|---|
| 93 |         }
 | 
|---|
| 94 |       }
 | 
|---|
| 95 |     }
 | 
|---|
| 96 |   }
 | 
|---|
| 97 | }
 | 
|---|
| 98 | 
 | 
|---|
| 99 | /** Adds an atom to the scene.
 | 
|---|
| 100 |  *
 | 
|---|
| 101 |  * @param _atom atom to add
 | 
|---|
| 102 |  */
 | 
|---|
| 103 | void GLWorldScene::atomInserted(const atom *_atom)
 | 
|---|
| 104 | {
 | 
|---|
| 105 |   LOG(3, "INFO: GLWorldScene: Received signal atomInserted for atom "+toString(_atom->getId())+".");
 | 
|---|
| 106 |   GLMoleculeObject_atom *atomObject = new GLMoleculeObject_atom(this, _atom);
 | 
|---|
| 107 |   AtomNodeMap::iterator iter = AtomsinSceneMap.find(_atom->getId());
 | 
|---|
| 108 |   ASSERT(iter == AtomsinSceneMap.end(),
 | 
|---|
| 109 |       "GLWorldScene::atomAdded() - same atom "+_atom->getName()+" added again.");
 | 
|---|
| 110 |   AtomsinSceneMap.insert( make_pair(_atom->getId(), atomObject) );
 | 
|---|
| 111 |   connect (atomObject, SIGNAL(clicked(atomId_t)), this, SLOT(atomClicked(atomId_t)));
 | 
|---|
| 112 |   connect (atomObject, SIGNAL(changed()), this, SIGNAL(changed()));
 | 
|---|
| 113 |   connect (atomObject, SIGNAL(hoverChanged()), this, SIGNAL(changed()));
 | 
|---|
| 114 |   connect (atomObject, SIGNAL(selectionChanged()), this, SIGNAL(changed()));
 | 
|---|
| 115 |   connect (atomObject, SIGNAL(BondsInserted(const bond *, const GLMoleculeObject_bond::SideOfBond)), this, SLOT(bondInserted(const bond *, const GLMoleculeObject_bond::SideOfBond)));
 | 
|---|
| 116 |   //bondsChanged(_atom);
 | 
|---|
| 117 |   emit changeOccured();
 | 
|---|
| 118 | }
 | 
|---|
| 119 | 
 | 
|---|
| 120 | /** Removes an atom from the scene.
 | 
|---|
| 121 |  *
 | 
|---|
| 122 |  * @param _atom atom to remove
 | 
|---|
| 123 |  */
 | 
|---|
| 124 | void GLWorldScene::atomRemoved(const atom *_atom)
 | 
|---|
| 125 | {
 | 
|---|
| 126 |   LOG(3, "INFO: GLWorldScene: Received signal atomRemoved for atom "+toString(_atom->getId())+".");
 | 
|---|
| 127 |   // bonds are removed by signal coming from ~bond
 | 
|---|
| 128 |   // remove atoms
 | 
|---|
| 129 |   AtomNodeMap::iterator iter = AtomsinSceneMap.find(_atom->getId());
 | 
|---|
| 130 |   ASSERT(iter != AtomsinSceneMap.end(),
 | 
|---|
| 131 |       "GLWorldScene::atomRemoved() - atom "+_atom->getName()+" not on display.");
 | 
|---|
| 132 |   GLMoleculeObject_atom *atomObject = iter->second;
 | 
|---|
| 133 |   atomObject->disconnect();
 | 
|---|
| 134 |   AtomsinSceneMap.erase(iter);
 | 
|---|
| 135 |   delete atomObject;
 | 
|---|
| 136 |   emit changeOccured();
 | 
|---|
| 137 | }
 | 
|---|
| 138 | 
 | 
|---|
| 139 | /** ....
 | 
|---|
| 140 |  *
 | 
|---|
| 141 |  */
 | 
|---|
| 142 | void GLWorldScene::worldSelectionChanged()
 | 
|---|
| 143 | {
 | 
|---|
| 144 |   LOG(3, "INFO: GLWorldScene: Received signal selectionChanged.");
 | 
|---|
| 145 | 
 | 
|---|
| 146 |   const std::vector<molecule*> &molecules = World::getInstance().getAllMolecules();
 | 
|---|
| 147 | 
 | 
|---|
| 148 |   if (molecules.size() > 0) {
 | 
|---|
| 149 |     for (std::vector<molecule*>::const_iterator Runner = molecules.begin();
 | 
|---|
| 150 |         Runner != molecules.end();
 | 
|---|
| 151 |         Runner++) {
 | 
|---|
| 152 | 
 | 
|---|
| 153 |       MoleculeNodeMap::iterator iter = MoleculesinSceneMap.find((*Runner)->getId());
 | 
|---|
| 154 |       bool isSelected = World::getInstance().isSelected(*Runner);
 | 
|---|
| 155 | 
 | 
|---|
| 156 |       // molecule selected but not in scene?
 | 
|---|
| 157 |       if (isSelected && (iter == MoleculesinSceneMap.end())){
 | 
|---|
| 158 |         // -> create new mesh
 | 
|---|
| 159 |         GLMoleculeObject_molecule *molObject = new GLMoleculeObject_molecule(this, *Runner);
 | 
|---|
| 160 |         MoleculesinSceneMap.insert( make_pair((*Runner)->getId(), molObject) );
 | 
|---|
| 161 |         connect (molObject, SIGNAL(changed()), this, SIGNAL(changed()));
 | 
|---|
| 162 |         connect (molObject, SIGNAL(selectionChanged()), this, SIGNAL(changed()));
 | 
|---|
| 163 |         connect (molObject, SIGNAL(selectionChanged()), this, SIGNAL(changed()));
 | 
|---|
| 164 |         emit changed();
 | 
|---|
| 165 |         emit changeOccured();
 | 
|---|
| 166 |       }
 | 
|---|
| 167 | 
 | 
|---|
| 168 |       // molecule not selected but in scene?
 | 
|---|
| 169 |       if (!isSelected && (iter != MoleculesinSceneMap.end())){
 | 
|---|
| 170 |         // -> remove from scene
 | 
|---|
| 171 |         moleculeRemoved(*Runner);
 | 
|---|
| 172 |       }
 | 
|---|
| 173 | 
 | 
|---|
| 174 |     }
 | 
|---|
| 175 |   }
 | 
|---|
| 176 | }
 | 
|---|
| 177 | 
 | 
|---|
| 178 | /** Removes a molecule from the scene.
 | 
|---|
| 179 |  *
 | 
|---|
| 180 |  * @param _molecule molecule to remove
 | 
|---|
| 181 |  */
 | 
|---|
| 182 | void GLWorldScene::moleculeRemoved(const molecule *_molecule)
 | 
|---|
| 183 | {
 | 
|---|
| 184 |   LOG(3, "INFO: GLWorldScene: Received signal moleculeRemoved for molecule "+toString(_molecule->getId())+".");
 | 
|---|
| 185 |   MoleculeNodeMap::iterator iter = MoleculesinSceneMap.find(_molecule->getId());
 | 
|---|
| 186 | 
 | 
|---|
| 187 |   // only remove if the molecule is in the scene
 | 
|---|
| 188 |   //  (= is selected)
 | 
|---|
| 189 |   if (iter != MoleculesinSceneMap.end()){
 | 
|---|
| 190 |     GLMoleculeObject_molecule *molObject = iter->second;
 | 
|---|
| 191 |     molObject->disconnect();
 | 
|---|
| 192 |     MoleculesinSceneMap.erase(iter);
 | 
|---|
| 193 |     delete molObject;
 | 
|---|
| 194 |     emit changed();
 | 
|---|
| 195 |     emit changeOccured();
 | 
|---|
| 196 |   }
 | 
|---|
| 197 | }
 | 
|---|
| 198 | 
 | 
|---|
| 199 | /** Adds a bond to the scene.
 | 
|---|
| 200 |  *
 | 
|---|
| 201 |  * @param _bond bond to add
 | 
|---|
| 202 |  * @param side which side of the bond (left or right)
 | 
|---|
| 203 |  */
 | 
|---|
| 204 | void GLWorldScene::bondInserted(const bond *_bond, const enum GLMoleculeObject_bond::SideOfBond side)
 | 
|---|
| 205 | {
 | 
|---|
| 206 |   LOG(3, "INFO: GLWorldScene::bondInserted() - Adding bond "+toString(*_bond)+".");
 | 
|---|
| 207 |   //LOG(4, "INFO: Currently present bonds " << BondsinSceneMap << ".");
 | 
|---|
| 208 | 
 | 
|---|
| 209 |   BondIds ids;
 | 
|---|
| 210 |   switch (side) {
 | 
|---|
| 211 |     case GLMoleculeObject_bond::left:
 | 
|---|
| 212 |       ids = std::make_pair(_bond->leftatom->getId(), _bond->rightatom->getId());
 | 
|---|
| 213 |       break;
 | 
|---|
| 214 |     case GLMoleculeObject_bond::right:
 | 
|---|
| 215 |       ids = std::make_pair(_bond->rightatom->getId(), _bond->leftatom->getId());
 | 
|---|
| 216 |       break;
 | 
|---|
| 217 |   }
 | 
|---|
| 218 | #ifndef NDEBUG
 | 
|---|
| 219 |   BondNodeMap::iterator iter = BondsinSceneMap.find(ids);
 | 
|---|
| 220 |   ASSERT(iter == BondsinSceneMap.end(),
 | 
|---|
| 221 |       "GLWorldScene::bondAdded() - same left-sided bond "+toString(*_bond)+" added again.");
 | 
|---|
| 222 | #endif
 | 
|---|
| 223 |   GLMoleculeObject_bond *bondObject =
 | 
|---|
| 224 |       new GLMoleculeObject_bond(this, _bond, side);
 | 
|---|
| 225 |   connect (
 | 
|---|
| 226 |       bondObject, SIGNAL(BondRemoved(const atomId_t, const atomId_t)),
 | 
|---|
| 227 |       this, SLOT(bondRemoved(const atomId_t, const atomId_t)));
 | 
|---|
| 228 |   connect (bondObject, SIGNAL(changed()), this, SIGNAL(changed()));
 | 
|---|
| 229 |   BondsinSceneMap.insert( make_pair(ids, bondObject) );
 | 
|---|
| 230 | //    BondIdsinSceneMap.insert( Leftids );
 | 
|---|
| 231 |   emit changeOccured();
 | 
|---|
| 232 | }
 | 
|---|
| 233 | 
 | 
|---|
| 234 | /** Removes a bond from the scene.
 | 
|---|
| 235 |  *
 | 
|---|
| 236 |  * @param _bond bond to remove
 | 
|---|
| 237 |  */
 | 
|---|
| 238 | void GLWorldScene::bondRemoved(const atomId_t leftnr, const atomId_t rightnr)
 | 
|---|
| 239 | {
 | 
|---|
| 240 |   LOG(3, "INFO: GLWorldScene::bondRemoved() - Removing bond between "+toString(leftnr)+" and "+toString(rightnr)+".");
 | 
|---|
| 241 |   {
 | 
|---|
| 242 |     // left bond
 | 
|---|
| 243 |     const BondIds Leftids( make_pair(leftnr, rightnr) );
 | 
|---|
| 244 |     BondNodeMap::iterator leftiter = BondsinSceneMap.find( Leftids );
 | 
|---|
| 245 |     ASSERT(leftiter != BondsinSceneMap.end(),
 | 
|---|
| 246 |         "GLWorldScene::bondRemoved() - bond "+toString(leftnr)+"-"
 | 
|---|
| 247 |         +toString(rightnr)+" not on display.");
 | 
|---|
| 248 |     //GLMoleculeObject_bond *bondObject = leftiter->second;
 | 
|---|
| 249 |     BondsinSceneMap.erase(leftiter);
 | 
|---|
| 250 |     //delete bondObject; // is done by signal from bond itself
 | 
|---|
| 251 |     //LOG(4, "INFO: Still present bonds " << BondsinSceneMap << ".");
 | 
|---|
| 252 |   }
 | 
|---|
| 253 | 
 | 
|---|
| 254 |   emit changeOccured();
 | 
|---|
| 255 | }
 | 
|---|
| 256 | 
 | 
|---|
| 257 | void GLWorldScene::initialize(QGLView *view, QGLPainter *painter) const
 | 
|---|
| 258 | {
 | 
|---|
| 259 |   // Initialize all of the mesh objects that we have as children.
 | 
|---|
| 260 |    foreach (QObject *obj, children()) {
 | 
|---|
| 261 |      GLMoleculeObject *meshobj = qobject_cast<GLMoleculeObject *>(obj);
 | 
|---|
| 262 |        if (meshobj)
 | 
|---|
| 263 |          meshobj->initialize(view, painter);
 | 
|---|
| 264 |    }
 | 
|---|
| 265 | }
 | 
|---|
| 266 | 
 | 
|---|
| 267 | void GLWorldScene::draw(QGLPainter *painter) const
 | 
|---|
| 268 | {
 | 
|---|
| 269 |    // Draw all of the mesh objects that we have as children.
 | 
|---|
| 270 |    foreach (QObject *obj, children()) {
 | 
|---|
| 271 |      GLMoleculeObject *meshobj = qobject_cast<GLMoleculeObject *>(obj);
 | 
|---|
| 272 |        if (meshobj)
 | 
|---|
| 273 |          meshobj->draw(painter);
 | 
|---|
| 274 |    }
 | 
|---|
| 275 | }
 | 
|---|
| 276 | 
 | 
|---|
| 277 | void GLWorldScene::atomClicked(atomId_t no)
 | 
|---|
| 278 | {
 | 
|---|
| 279 |    LOG(3, "INFO: GLWorldScene - atom " << no << " has been clicked.");
 | 
|---|
| 280 |    const atom *Walker = World::getInstance().getAtom(AtomById(no));
 | 
|---|
| 281 |    if (!World::getInstance().isSelected(Walker))
 | 
|---|
| 282 |    SelectionAtomById(no);
 | 
|---|
| 283 |    else
 | 
|---|
| 284 |      SelectionNotAtomById(no);
 | 
|---|
| 285 |    emit clicked(no);
 | 
|---|
| 286 | }
 | 
|---|
| 287 | 
 | 
|---|