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