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