[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>
|
---|
[bca99d] | 24 | #include <Qt3D/qglbuilder.h>
|
---|
| 25 | #include <Qt3D/qglscenenode.h>
|
---|
| 26 | #include <Qt3D/qglsphere.h>
|
---|
| 27 | #include <Qt3D/qglcylinder.h>
|
---|
[907636] | 28 |
|
---|
| 29 | #include "GLMoleculeObject.hpp"
|
---|
[7188b1] | 30 | #include "GLMoleculeObject_atom.hpp"
|
---|
| 31 | #include "GLMoleculeObject_bond.hpp"
|
---|
[c67518] | 32 | #include "GLMoleculeObject_molecule.hpp"
|
---|
[907636] | 33 |
|
---|
| 34 | #include "CodePatterns/MemDebug.hpp"
|
---|
| 35 |
|
---|
[7188b1] | 36 | #include "CodePatterns/Log.hpp"
|
---|
| 37 |
|
---|
[0e9ffe] | 38 | #include "Actions/SelectionAction/Atoms/AtomByIdAction.hpp"
|
---|
[89643d] | 39 | #include "Actions/SelectionAction/Atoms/NotAtomByIdAction.hpp"
|
---|
[6f0841] | 40 | #include "Atom/atom.hpp"
|
---|
[7188b1] | 41 | #include "Bond/bond.hpp"
|
---|
[89643d] | 42 | #include "Descriptors/AtomIdDescriptor.hpp"
|
---|
[37b2575] | 43 | #include "Helpers/helpers.hpp"
|
---|
[907636] | 44 | #include "molecule.hpp"
|
---|
| 45 | #include "World.hpp"
|
---|
| 46 |
|
---|
[2ad1ec] | 47 | #include <iostream>
|
---|
| 48 |
|
---|
[ce7fdc] | 49 | using namespace MoleCuilder;
|
---|
[907636] | 50 |
|
---|
[2ad1ec] | 51 | std::ostream &operator<<(std::ostream &ost, const GLWorldScene::BondIds &t)
|
---|
| 52 | {
|
---|
| 53 | ost << t.first << "," << t.second;
|
---|
| 54 | return ost;
|
---|
| 55 | }
|
---|
| 56 |
|
---|
[907636] | 57 | GLWorldScene::GLWorldScene(QObject *parent)
|
---|
| 58 | : QObject(parent)
|
---|
| 59 | {
|
---|
[bca99d] | 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 |
|
---|
[0a6ff9] | 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 |
|
---|
[907636] | 80 | init();
|
---|
| 81 | }
|
---|
| 82 |
|
---|
| 83 | GLWorldScene::~GLWorldScene()
|
---|
[7188b1] | 84 | {
|
---|
| 85 | // remove all elements
|
---|
| 86 | GLMoleculeObject::cleanMaterialMap();
|
---|
| 87 | }
|
---|
[907636] | 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++) {
|
---|
[c67518] | 100 |
|
---|
[7188b1] | 101 | for (molecule::const_iterator atomiter = (*Runner)->begin();
|
---|
| 102 | atomiter != (*Runner)->end();
|
---|
| 103 | ++atomiter) {
|
---|
| 104 | // create atom objects in scene
|
---|
| 105 | atomInserted(*atomiter);
|
---|
| 106 |
|
---|
[9c18e4] | 107 | // create bond objects in scene
|
---|
[2ad1ec] | 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 | }
|
---|
[7188b1] | 117 | }
|
---|
[907636] | 118 | }
|
---|
| 119 | }
|
---|
| 120 | }
|
---|
| 121 |
|
---|
[7188b1] | 122 | /** Adds an atom to the scene.
|
---|
| 123 | *
|
---|
| 124 | * @param _atom atom to add
|
---|
| 125 | */
|
---|
| 126 | void GLWorldScene::atomInserted(const atom *_atom)
|
---|
[907636] | 127 | {
|
---|
[57a770] | 128 | LOG(3, "INFO: GLWorldScene: Received signal atomInserted for atom "+toString(_atom->getId())+".");
|
---|
[bca99d] | 129 | GLMoleculeObject_atom *atomObject = new GLMoleculeObject_atom(meshSphereHi, this, _atom);
|
---|
[37b2575] | 130 | AtomNodeMap::iterator iter = AtomsinSceneMap.find(_atom->getId());
|
---|
| 131 | ASSERT(iter == AtomsinSceneMap.end(),
|
---|
[7188b1] | 132 | "GLWorldScene::atomAdded() - same atom "+_atom->getName()+" added again.");
|
---|
[37b2575] | 133 | AtomsinSceneMap.insert( make_pair(_atom->getId(), atomObject) );
|
---|
[7188b1] | 134 | connect (atomObject, SIGNAL(clicked(atomId_t)), this, SLOT(atomClicked(atomId_t)));
|
---|
[5a2a06] | 135 | connect (atomObject, SIGNAL(changed()), this, SIGNAL(changed()));
|
---|
[a099d3] | 136 | connect (atomObject, SIGNAL(hoverChanged()), this, SIGNAL(changed()));
|
---|
[5a2a06] | 137 | connect (atomObject, SIGNAL(selectionChanged()), this, SIGNAL(changed()));
|
---|
[2ad1ec] | 138 | connect (atomObject, SIGNAL(BondsInserted(const bond *, const GLMoleculeObject_bond::SideOfBond)), this, SLOT(bondInserted(const bond *, const GLMoleculeObject_bond::SideOfBond)));
|
---|
| 139 | //bondsChanged(_atom);
|
---|
[65487f] | 140 | emit changeOccured();
|
---|
[7188b1] | 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 | {
|
---|
[57a770] | 149 | LOG(3, "INFO: GLWorldScene: Received signal atomRemoved for atom "+toString(_atom->getId())+".");
|
---|
[2ad1ec] | 150 | // bonds are removed by signal coming from ~bond
|
---|
[7188b1] | 151 | // remove atoms
|
---|
[37b2575] | 152 | AtomNodeMap::iterator iter = AtomsinSceneMap.find(_atom->getId());
|
---|
| 153 | ASSERT(iter != AtomsinSceneMap.end(),
|
---|
[7188b1] | 154 | "GLWorldScene::atomRemoved() - atom "+_atom->getName()+" not on display.");
|
---|
| 155 | GLMoleculeObject_atom *atomObject = iter->second;
|
---|
| 156 | atomObject->disconnect();
|
---|
[37b2575] | 157 | AtomsinSceneMap.erase(iter);
|
---|
[7188b1] | 158 | delete atomObject;
|
---|
[65487f] | 159 | emit changeOccured();
|
---|
[907636] | 160 | }
|
---|
| 161 |
|
---|
[3927ef] | 162 | /** ....
|
---|
[c67518] | 163 | *
|
---|
| 164 | */
|
---|
[3927ef] | 165 | void GLWorldScene::worldSelectionChanged()
|
---|
[c67518] | 166 | {
|
---|
[3927ef] | 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
|
---|
[bca99d] | 182 | GLMoleculeObject_molecule *molObject = new GLMoleculeObject_molecule(meshEmpty, this, *Runner);
|
---|
[3927ef] | 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 | }
|
---|
[c67518] | 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 | {
|
---|
[3927ef] | 207 | LOG(3, "INFO: GLWorldScene: Received signal moleculeRemoved for molecule "+toString(_molecule->getId())+".");
|
---|
[c67518] | 208 | MoleculeNodeMap::iterator iter = MoleculesinSceneMap.find(_molecule->getId());
|
---|
[3927ef] | 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 | }
|
---|
[c67518] | 220 | }
|
---|
| 221 |
|
---|
[37b2575] | 222 | /** Adds a bond to the scene.
|
---|
[7188b1] | 223 | *
|
---|
| 224 | * @param _bond bond to add
|
---|
[2ad1ec] | 225 | * @param side which side of the bond (left or right)
|
---|
[7188b1] | 226 | */
|
---|
[2ad1ec] | 227 | void GLWorldScene::bondInserted(const bond *_bond, const enum GLMoleculeObject_bond::SideOfBond side)
|
---|
[7188b1] | 228 | {
|
---|
[57a770] | 229 | LOG(3, "INFO: GLWorldScene::bondInserted() - Adding bond "+toString(*_bond)+".");
|
---|
[2ad1ec] | 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;
|
---|
[7188b1] | 240 | }
|
---|
[2ad1ec] | 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 =
|
---|
[bca99d] | 247 | new GLMoleculeObject_bond(meshCylinderHi, this, _bond, side);
|
---|
[2ad1ec] | 248 | connect (
|
---|
| 249 | bondObject, SIGNAL(BondRemoved(const atomId_t, const atomId_t)),
|
---|
| 250 | this, SLOT(bondRemoved(const atomId_t, const atomId_t)));
|
---|
[5a2a06] | 251 | connect (bondObject, SIGNAL(changed()), this, SIGNAL(changed()));
|
---|
[2ad1ec] | 252 | BondsinSceneMap.insert( make_pair(ids, bondObject) );
|
---|
| 253 | // BondIdsinSceneMap.insert( Leftids );
|
---|
[65487f] | 254 | emit changeOccured();
|
---|
[7188b1] | 255 | }
|
---|
[907636] | 256 |
|
---|
[37b2575] | 257 | /** Removes a bond from the scene.
|
---|
[7188b1] | 258 | *
|
---|
| 259 | * @param _bond bond to remove
|
---|
| 260 | */
|
---|
[37b2575] | 261 | void GLWorldScene::bondRemoved(const atomId_t leftnr, const atomId_t rightnr)
|
---|
[907636] | 262 | {
|
---|
[2ad1ec] | 263 | LOG(3, "INFO: GLWorldScene::bondRemoved() - Removing bond between "+toString(leftnr)+" and "+toString(rightnr)+".");
|
---|
[7188b1] | 264 | {
|
---|
| 265 | // left bond
|
---|
[37b2575] | 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.");
|
---|
[2ad1ec] | 271 | //GLMoleculeObject_bond *bondObject = leftiter->second;
|
---|
[37b2575] | 272 | BondsinSceneMap.erase(leftiter);
|
---|
[2ad1ec] | 273 | //delete bondObject; // is done by signal from bond itself
|
---|
| 274 | //LOG(4, "INFO: Still present bonds " << BondsinSceneMap << ".");
|
---|
[7188b1] | 275 | }
|
---|
[2ad1ec] | 276 |
|
---|
[65487f] | 277 | emit changeOccured();
|
---|
[7188b1] | 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 | }
|
---|
[907636] | 298 | }
|
---|
[06ebf5] | 299 |
|
---|
[7188b1] | 300 | void GLWorldScene::atomClicked(atomId_t no)
|
---|
[907636] | 301 | {
|
---|
[57a770] | 302 | LOG(3, "INFO: GLWorldScene - atom " << no << " has been clicked.");
|
---|
[89643d] | 303 | const atom *Walker = World::getInstance().getAtom(AtomById(no));
|
---|
| 304 | if (!World::getInstance().isSelected(Walker))
|
---|
[0e9ffe] | 305 | SelectionAtomById(no);
|
---|
[89643d] | 306 | else
|
---|
| 307 | SelectionNotAtomById(no);
|
---|
[7188b1] | 308 | emit clicked(no);
|
---|
[907636] | 309 | }
|
---|
[029bb4] | 310 |
|
---|