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