| 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 | // create molecule
|
|---|
| 79 | moleculeInserted(*Runner);
|
|---|
| 80 |
|
|---|
| 81 | for (molecule::const_iterator atomiter = (*Runner)->begin();
|
|---|
| 82 | atomiter != (*Runner)->end();
|
|---|
| 83 | ++atomiter) {
|
|---|
| 84 | // create atom objects in scene
|
|---|
| 85 | atomInserted(*atomiter);
|
|---|
| 86 |
|
|---|
| 87 | // create bond objects in scene
|
|---|
| 88 | const BondList &bondlist = (*atomiter)->getListOfBonds();
|
|---|
| 89 | for (BondList::const_iterator bonditer = bondlist.begin();
|
|---|
| 90 | bonditer != bondlist.end();
|
|---|
| 91 | ++bonditer) {
|
|---|
| 92 | const bond *_bond = *bonditer;
|
|---|
| 93 | const GLMoleculeObject_bond::SideOfBond side = (_bond->leftatom == *atomiter) ?
|
|---|
| 94 | GLMoleculeObject_bond::left : GLMoleculeObject_bond::right;
|
|---|
| 95 | bondInserted(_bond, side);
|
|---|
| 96 | }
|
|---|
| 97 | }
|
|---|
| 98 | }
|
|---|
| 99 | }
|
|---|
| 100 | }
|
|---|
| 101 |
|
|---|
| 102 | /** Adds an atom to the scene.
|
|---|
| 103 | *
|
|---|
| 104 | * @param _atom atom to add
|
|---|
| 105 | */
|
|---|
| 106 | void GLWorldScene::atomInserted(const atom *_atom)
|
|---|
| 107 | {
|
|---|
| 108 | LOG(3, "INFO: GLWorldScene: Received signal atomInserted for atom "+toString(_atom->getId())+".");
|
|---|
| 109 | GLMoleculeObject_atom *atomObject = new GLMoleculeObject_atom(this, _atom);
|
|---|
| 110 | AtomNodeMap::iterator iter = AtomsinSceneMap.find(_atom->getId());
|
|---|
| 111 | ASSERT(iter == AtomsinSceneMap.end(),
|
|---|
| 112 | "GLWorldScene::atomAdded() - same atom "+_atom->getName()+" added again.");
|
|---|
| 113 | AtomsinSceneMap.insert( make_pair(_atom->getId(), atomObject) );
|
|---|
| 114 | connect (atomObject, SIGNAL(clicked(atomId_t)), this, SLOT(atomClicked(atomId_t)));
|
|---|
| 115 | connect (atomObject, SIGNAL(changed()), this, SIGNAL(changed()));
|
|---|
| 116 | connect (atomObject, SIGNAL(hoverChanged()), this, SIGNAL(changed()));
|
|---|
| 117 | connect (atomObject, SIGNAL(selectionChanged()), this, SIGNAL(changed()));
|
|---|
| 118 | connect (atomObject, SIGNAL(BondsInserted(const bond *, const GLMoleculeObject_bond::SideOfBond)), this, SLOT(bondInserted(const bond *, const GLMoleculeObject_bond::SideOfBond)));
|
|---|
| 119 | //bondsChanged(_atom);
|
|---|
| 120 | emit changeOccured();
|
|---|
| 121 | }
|
|---|
| 122 |
|
|---|
| 123 | /** Removes an atom from the scene.
|
|---|
| 124 | *
|
|---|
| 125 | * @param _atom atom to remove
|
|---|
| 126 | */
|
|---|
| 127 | void GLWorldScene::atomRemoved(const atom *_atom)
|
|---|
| 128 | {
|
|---|
| 129 | LOG(3, "INFO: GLWorldScene: Received signal atomRemoved for atom "+toString(_atom->getId())+".");
|
|---|
| 130 | // bonds are removed by signal coming from ~bond
|
|---|
| 131 | // remove atoms
|
|---|
| 132 | AtomNodeMap::iterator iter = AtomsinSceneMap.find(_atom->getId());
|
|---|
| 133 | ASSERT(iter != AtomsinSceneMap.end(),
|
|---|
| 134 | "GLWorldScene::atomRemoved() - atom "+_atom->getName()+" not on display.");
|
|---|
| 135 | GLMoleculeObject_atom *atomObject = iter->second;
|
|---|
| 136 | atomObject->disconnect();
|
|---|
| 137 | AtomsinSceneMap.erase(iter);
|
|---|
| 138 | delete atomObject;
|
|---|
| 139 | emit changeOccured();
|
|---|
| 140 | }
|
|---|
| 141 |
|
|---|
| 142 | /** Adds a molecule to the scene.
|
|---|
| 143 | *
|
|---|
| 144 | * @param _molecule molecule to add
|
|---|
| 145 | */
|
|---|
| 146 | void GLWorldScene::moleculeInserted(const molecule*_molecule)
|
|---|
| 147 | {
|
|---|
| 148 | LOG(3, "INFO: GLWorldScene: Received signal moleculeInserted for atom "+toString(_molecule->getId())+".");
|
|---|
| 149 | GLMoleculeObject_molecule *molObject = new GLMoleculeObject_molecule(this, _molecule);
|
|---|
| 150 | MoleculeNodeMap::iterator iter = MoleculesinSceneMap.find(_molecule->getId());
|
|---|
| 151 | ASSERT(iter == MoleculesinSceneMap.end(),
|
|---|
| 152 | "GLWorldScene::moleculeInserted() - same molecule "+_molecule->getName()+" added again.");
|
|---|
| 153 | MoleculesinSceneMap.insert( make_pair(_molecule->getId(), molObject) );
|
|---|
| 154 | connect (molObject, SIGNAL(changed()), this, SIGNAL(changed()));
|
|---|
| 155 | connect (molObject, SIGNAL(selectionChanged()), this, SIGNAL(changed()));
|
|---|
| 156 | connect (molObject, SIGNAL(selectionChanged()), this, SIGNAL(changed()));
|
|---|
| 157 | emit changeOccured();
|
|---|
| 158 | }
|
|---|
| 159 |
|
|---|
| 160 | /** Removes a molecule from the scene.
|
|---|
| 161 | *
|
|---|
| 162 | * @param _molecule molecule to remove
|
|---|
| 163 | */
|
|---|
| 164 | void GLWorldScene::moleculeRemoved(const molecule *_molecule)
|
|---|
| 165 | {
|
|---|
| 166 | LOG(3, "INFO: GLWorldScene: Received signal moleculeRemoved for atom "+toString(_molecule->getId())+".");
|
|---|
| 167 | MoleculeNodeMap::iterator iter = MoleculesinSceneMap.find(_molecule->getId());
|
|---|
| 168 | ASSERT(iter != MoleculesinSceneMap.end(),
|
|---|
| 169 | "GLWorldScene::moleculeRemoved() - molecule "+_molecule->getName()+" not on display.");
|
|---|
| 170 | GLMoleculeObject_molecule *molObject = iter->second;
|
|---|
| 171 | molObject->disconnect();
|
|---|
| 172 | MoleculesinSceneMap.erase(iter);
|
|---|
| 173 | delete molObject;
|
|---|
| 174 | emit changeOccured();
|
|---|
| 175 | }
|
|---|
| 176 |
|
|---|
| 177 | /** Adds a bond to the scene.
|
|---|
| 178 | *
|
|---|
| 179 | * @param _bond bond to add
|
|---|
| 180 | * @param side which side of the bond (left or right)
|
|---|
| 181 | */
|
|---|
| 182 | void GLWorldScene::bondInserted(const bond *_bond, const enum GLMoleculeObject_bond::SideOfBond side)
|
|---|
| 183 | {
|
|---|
| 184 | LOG(3, "INFO: GLWorldScene::bondInserted() - Adding bond "+toString(*_bond)+".");
|
|---|
| 185 | //LOG(4, "INFO: Currently present bonds " << BondsinSceneMap << ".");
|
|---|
| 186 |
|
|---|
| 187 | BondIds ids;
|
|---|
| 188 | switch (side) {
|
|---|
| 189 | case GLMoleculeObject_bond::left:
|
|---|
| 190 | ids = std::make_pair(_bond->leftatom->getId(), _bond->rightatom->getId());
|
|---|
| 191 | break;
|
|---|
| 192 | case GLMoleculeObject_bond::right:
|
|---|
| 193 | ids = std::make_pair(_bond->rightatom->getId(), _bond->leftatom->getId());
|
|---|
| 194 | break;
|
|---|
| 195 | }
|
|---|
| 196 | #ifndef NDEBUG
|
|---|
| 197 | BondNodeMap::iterator iter = BondsinSceneMap.find(ids);
|
|---|
| 198 | ASSERT(iter == BondsinSceneMap.end(),
|
|---|
| 199 | "GLWorldScene::bondAdded() - same left-sided bond "+toString(*_bond)+" added again.");
|
|---|
| 200 | #endif
|
|---|
| 201 | GLMoleculeObject_bond *bondObject =
|
|---|
| 202 | new GLMoleculeObject_bond(this, _bond, side);
|
|---|
| 203 | connect (
|
|---|
| 204 | bondObject, SIGNAL(BondRemoved(const atomId_t, const atomId_t)),
|
|---|
| 205 | this, SLOT(bondRemoved(const atomId_t, const atomId_t)));
|
|---|
| 206 | connect (bondObject, SIGNAL(changed()), this, SIGNAL(changed()));
|
|---|
| 207 | BondsinSceneMap.insert( make_pair(ids, bondObject) );
|
|---|
| 208 | // BondIdsinSceneMap.insert( Leftids );
|
|---|
| 209 | emit changeOccured();
|
|---|
| 210 | }
|
|---|
| 211 |
|
|---|
| 212 | /** Removes a bond from the scene.
|
|---|
| 213 | *
|
|---|
| 214 | * @param _bond bond to remove
|
|---|
| 215 | */
|
|---|
| 216 | void GLWorldScene::bondRemoved(const atomId_t leftnr, const atomId_t rightnr)
|
|---|
| 217 | {
|
|---|
| 218 | LOG(3, "INFO: GLWorldScene::bondRemoved() - Removing bond between "+toString(leftnr)+" and "+toString(rightnr)+".");
|
|---|
| 219 | {
|
|---|
| 220 | // left bond
|
|---|
| 221 | const BondIds Leftids( make_pair(leftnr, rightnr) );
|
|---|
| 222 | BondNodeMap::iterator leftiter = BondsinSceneMap.find( Leftids );
|
|---|
| 223 | ASSERT(leftiter != BondsinSceneMap.end(),
|
|---|
| 224 | "GLWorldScene::bondRemoved() - bond "+toString(leftnr)+"-"
|
|---|
| 225 | +toString(rightnr)+" not on display.");
|
|---|
| 226 | //GLMoleculeObject_bond *bondObject = leftiter->second;
|
|---|
| 227 | BondsinSceneMap.erase(leftiter);
|
|---|
| 228 | //delete bondObject; // is done by signal from bond itself
|
|---|
| 229 | //LOG(4, "INFO: Still present bonds " << BondsinSceneMap << ".");
|
|---|
| 230 | }
|
|---|
| 231 |
|
|---|
| 232 | emit changeOccured();
|
|---|
| 233 | }
|
|---|
| 234 |
|
|---|
| 235 | void GLWorldScene::initialize(QGLView *view, QGLPainter *painter) const
|
|---|
| 236 | {
|
|---|
| 237 | // Initialize all of the mesh objects that we have as children.
|
|---|
| 238 | foreach (QObject *obj, children()) {
|
|---|
| 239 | GLMoleculeObject *meshobj = qobject_cast<GLMoleculeObject *>(obj);
|
|---|
| 240 | if (meshobj)
|
|---|
| 241 | meshobj->initialize(view, painter);
|
|---|
| 242 | }
|
|---|
| 243 | }
|
|---|
| 244 |
|
|---|
| 245 | void GLWorldScene::draw(QGLPainter *painter) const
|
|---|
| 246 | {
|
|---|
| 247 | // Draw all of the mesh objects that we have as children.
|
|---|
| 248 | foreach (QObject *obj, children()) {
|
|---|
| 249 | GLMoleculeObject *meshobj = qobject_cast<GLMoleculeObject *>(obj);
|
|---|
| 250 | if (meshobj)
|
|---|
| 251 | meshobj->draw(painter);
|
|---|
| 252 | }
|
|---|
| 253 | }
|
|---|
| 254 |
|
|---|
| 255 | void GLWorldScene::atomClicked(atomId_t no)
|
|---|
| 256 | {
|
|---|
| 257 | LOG(3, "INFO: GLWorldScene - atom " << no << " has been clicked.");
|
|---|
| 258 | const atom *Walker = World::getInstance().getAtom(AtomById(no));
|
|---|
| 259 | if (!World::getInstance().isSelected(Walker))
|
|---|
| 260 | SelectionAtomById(no);
|
|---|
| 261 | else
|
|---|
| 262 | SelectionNotAtomById(no);
|
|---|
| 263 | emit clicked(no);
|
|---|
| 264 | }
|
|---|
| 265 |
|
|---|