[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"
|
---|
[6966b7] | 40 | #include "Actions/SelectionAction/Molecules/MoleculeByIdAction.hpp"
|
---|
| 41 | #include "Actions/SelectionAction/Molecules/NotMoleculeByIdAction.hpp"
|
---|
[6f0841] | 42 | #include "Atom/atom.hpp"
|
---|
[7188b1] | 43 | #include "Bond/bond.hpp"
|
---|
[89643d] | 44 | #include "Descriptors/AtomIdDescriptor.hpp"
|
---|
[37b2575] | 45 | #include "Helpers/helpers.hpp"
|
---|
[907636] | 46 | #include "molecule.hpp"
|
---|
| 47 | #include "World.hpp"
|
---|
| 48 |
|
---|
[2ad1ec] | 49 | #include <iostream>
|
---|
| 50 |
|
---|
[ce7fdc] | 51 | using namespace MoleCuilder;
|
---|
[907636] | 52 |
|
---|
[2ad1ec] | 53 | std::ostream &operator<<(std::ostream &ost, const GLWorldScene::BondIds &t)
|
---|
| 54 | {
|
---|
| 55 | ost << t.first << "," << t.second;
|
---|
| 56 | return ost;
|
---|
| 57 | }
|
---|
| 58 |
|
---|
[907636] | 59 | GLWorldScene::GLWorldScene(QObject *parent)
|
---|
[407638e] | 60 | : QObject(parent),
|
---|
| 61 | hoverAtom(NULL)
|
---|
[907636] | 62 | {
|
---|
[bca99d] | 63 | QGLBuilder builder0;
|
---|
| 64 | meshEmpty = builder0.finalizedSceneNode();
|
---|
| 65 | QGLBuilder builder1;
|
---|
| 66 | builder1 << QGLSphere(2.0, 5);
|
---|
| 67 | meshSphereHi = builder1.finalizedSceneNode();
|
---|
| 68 | QGLBuilder builder2;
|
---|
| 69 | builder2 << QGLSphere(2.0, 1);
|
---|
| 70 | meshSphereLo = builder2.finalizedSceneNode();
|
---|
| 71 | QGLBuilder builder3;
|
---|
| 72 | builder3 << QGLCylinder(.25,.25,1.0,16);
|
---|
| 73 | meshCylinderHi = builder3.finalizedSceneNode();
|
---|
| 74 | QGLBuilder builder4;
|
---|
| 75 | builder4 << QGLCylinder(.25,.25,1.0,16);
|
---|
| 76 | meshCylinderLo = builder4.finalizedSceneNode();
|
---|
| 77 |
|
---|
[0a6ff9] | 78 | meshSphereHi->setOption(QGLSceneNode::CullBoundingBox, true);
|
---|
| 79 | meshSphereLo->setOption(QGLSceneNode::CullBoundingBox, true);
|
---|
| 80 | meshCylinderHi->setOption(QGLSceneNode::CullBoundingBox, true);
|
---|
| 81 | meshCylinderLo->setOption(QGLSceneNode::CullBoundingBox, true);
|
---|
| 82 |
|
---|
[6966b7] | 83 | setSelectionMode(SelectAtom);
|
---|
| 84 |
|
---|
[907636] | 85 | init();
|
---|
| 86 | }
|
---|
| 87 |
|
---|
| 88 | GLWorldScene::~GLWorldScene()
|
---|
[7188b1] | 89 | {
|
---|
| 90 | // remove all elements
|
---|
| 91 | GLMoleculeObject::cleanMaterialMap();
|
---|
| 92 | }
|
---|
[907636] | 93 |
|
---|
| 94 | /** Initialise the WorldScene with molecules and atoms from World.
|
---|
| 95 | *
|
---|
| 96 | */
|
---|
| 97 | void GLWorldScene::init()
|
---|
| 98 | {
|
---|
| 99 | const std::vector<molecule*> &molecules = World::getInstance().getAllMolecules();
|
---|
| 100 |
|
---|
| 101 | if (molecules.size() > 0) {
|
---|
| 102 | for (std::vector<molecule*>::const_iterator Runner = molecules.begin();
|
---|
| 103 | Runner != molecules.end();
|
---|
| 104 | Runner++) {
|
---|
[c67518] | 105 |
|
---|
[7188b1] | 106 | for (molecule::const_iterator atomiter = (*Runner)->begin();
|
---|
| 107 | atomiter != (*Runner)->end();
|
---|
| 108 | ++atomiter) {
|
---|
| 109 | // create atom objects in scene
|
---|
| 110 | atomInserted(*atomiter);
|
---|
| 111 |
|
---|
[9c18e4] | 112 | // create bond objects in scene
|
---|
[2ad1ec] | 113 | const BondList &bondlist = (*atomiter)->getListOfBonds();
|
---|
| 114 | for (BondList::const_iterator bonditer = bondlist.begin();
|
---|
| 115 | bonditer != bondlist.end();
|
---|
| 116 | ++bonditer) {
|
---|
| 117 | const bond *_bond = *bonditer;
|
---|
| 118 | const GLMoleculeObject_bond::SideOfBond side = (_bond->leftatom == *atomiter) ?
|
---|
| 119 | GLMoleculeObject_bond::left : GLMoleculeObject_bond::right;
|
---|
| 120 | bondInserted(_bond, side);
|
---|
| 121 | }
|
---|
[7188b1] | 122 | }
|
---|
[907636] | 123 | }
|
---|
| 124 | }
|
---|
| 125 | }
|
---|
| 126 |
|
---|
[7188b1] | 127 | /** Adds an atom to the scene.
|
---|
| 128 | *
|
---|
| 129 | * @param _atom atom to add
|
---|
| 130 | */
|
---|
| 131 | void GLWorldScene::atomInserted(const atom *_atom)
|
---|
[907636] | 132 | {
|
---|
[57a770] | 133 | LOG(3, "INFO: GLWorldScene: Received signal atomInserted for atom "+toString(_atom->getId())+".");
|
---|
[bca99d] | 134 | GLMoleculeObject_atom *atomObject = new GLMoleculeObject_atom(meshSphereHi, this, _atom);
|
---|
[37b2575] | 135 | AtomNodeMap::iterator iter = AtomsinSceneMap.find(_atom->getId());
|
---|
| 136 | ASSERT(iter == AtomsinSceneMap.end(),
|
---|
[7188b1] | 137 | "GLWorldScene::atomAdded() - same atom "+_atom->getName()+" added again.");
|
---|
[37b2575] | 138 | AtomsinSceneMap.insert( make_pair(_atom->getId(), atomObject) );
|
---|
[7188b1] | 139 | connect (atomObject, SIGNAL(clicked(atomId_t)), this, SLOT(atomClicked(atomId_t)));
|
---|
[5a2a06] | 140 | connect (atomObject, SIGNAL(changed()), this, SIGNAL(changed()));
|
---|
[407638e] | 141 | connect (atomObject, SIGNAL(hoverChanged(GLMoleculeObject *)), this, SIGNAL(changed()));
|
---|
| 142 | connect (atomObject, SIGNAL(hoverChanged(GLMoleculeObject *)), this, SLOT(hoverChangedSignalled(GLMoleculeObject *)));
|
---|
[5a2a06] | 143 | connect (atomObject, SIGNAL(selectionChanged()), this, SIGNAL(changed()));
|
---|
[2ad1ec] | 144 | connect (atomObject, SIGNAL(BondsInserted(const bond *, const GLMoleculeObject_bond::SideOfBond)), this, SLOT(bondInserted(const bond *, const GLMoleculeObject_bond::SideOfBond)));
|
---|
[30cd0d] | 145 | connect (atomObject, SIGNAL(indexChanged(GLMoleculeObject_atom*, int, int)), this, SLOT(changeAtomId(GLMoleculeObject_atom*, int, int)));
|
---|
[2ad1ec] | 146 | //bondsChanged(_atom);
|
---|
[65487f] | 147 | emit changeOccured();
|
---|
[7188b1] | 148 | }
|
---|
| 149 |
|
---|
| 150 | /** Removes an atom from the scene.
|
---|
| 151 | *
|
---|
| 152 | * @param _atom atom to remove
|
---|
| 153 | */
|
---|
| 154 | void GLWorldScene::atomRemoved(const atom *_atom)
|
---|
| 155 | {
|
---|
[57a770] | 156 | LOG(3, "INFO: GLWorldScene: Received signal atomRemoved for atom "+toString(_atom->getId())+".");
|
---|
[2ad1ec] | 157 | // bonds are removed by signal coming from ~bond
|
---|
[7188b1] | 158 | // remove atoms
|
---|
[37b2575] | 159 | AtomNodeMap::iterator iter = AtomsinSceneMap.find(_atom->getId());
|
---|
| 160 | ASSERT(iter != AtomsinSceneMap.end(),
|
---|
[7188b1] | 161 | "GLWorldScene::atomRemoved() - atom "+_atom->getName()+" not on display.");
|
---|
| 162 | GLMoleculeObject_atom *atomObject = iter->second;
|
---|
| 163 | atomObject->disconnect();
|
---|
[37b2575] | 164 | AtomsinSceneMap.erase(iter);
|
---|
[7188b1] | 165 | delete atomObject;
|
---|
[65487f] | 166 | emit changeOccured();
|
---|
[907636] | 167 | }
|
---|
| 168 |
|
---|
[3927ef] | 169 | /** ....
|
---|
[c67518] | 170 | *
|
---|
| 171 | */
|
---|
[3927ef] | 172 | void GLWorldScene::worldSelectionChanged()
|
---|
[c67518] | 173 | {
|
---|
[3927ef] | 174 | LOG(3, "INFO: GLWorldScene: Received signal selectionChanged.");
|
---|
| 175 |
|
---|
| 176 | const std::vector<molecule*> &molecules = World::getInstance().getAllMolecules();
|
---|
| 177 |
|
---|
| 178 | if (molecules.size() > 0) {
|
---|
| 179 | for (std::vector<molecule*>::const_iterator Runner = molecules.begin();
|
---|
| 180 | Runner != molecules.end();
|
---|
| 181 | Runner++) {
|
---|
| 182 |
|
---|
| 183 | MoleculeNodeMap::iterator iter = MoleculesinSceneMap.find((*Runner)->getId());
|
---|
| 184 | bool isSelected = World::getInstance().isSelected(*Runner);
|
---|
| 185 |
|
---|
| 186 | // molecule selected but not in scene?
|
---|
| 187 | if (isSelected && (iter == MoleculesinSceneMap.end())){
|
---|
| 188 | // -> create new mesh
|
---|
[bca99d] | 189 | GLMoleculeObject_molecule *molObject = new GLMoleculeObject_molecule(meshEmpty, this, *Runner);
|
---|
[3927ef] | 190 | MoleculesinSceneMap.insert( make_pair((*Runner)->getId(), molObject) );
|
---|
| 191 | connect (molObject, SIGNAL(changed()), this, SIGNAL(changed()));
|
---|
| 192 | connect (molObject, SIGNAL(selectionChanged()), this, SIGNAL(changed()));
|
---|
| 193 | connect (molObject, SIGNAL(selectionChanged()), this, SIGNAL(changed()));
|
---|
| 194 | emit changed();
|
---|
| 195 | emit changeOccured();
|
---|
| 196 | }
|
---|
| 197 |
|
---|
| 198 | // molecule not selected but in scene?
|
---|
| 199 | if (!isSelected && (iter != MoleculesinSceneMap.end())){
|
---|
| 200 | // -> remove from scene
|
---|
| 201 | moleculeRemoved(*Runner);
|
---|
| 202 | }
|
---|
| 203 |
|
---|
| 204 | }
|
---|
| 205 | }
|
---|
[c67518] | 206 | }
|
---|
| 207 |
|
---|
| 208 | /** Removes a molecule from the scene.
|
---|
| 209 | *
|
---|
| 210 | * @param _molecule molecule to remove
|
---|
| 211 | */
|
---|
| 212 | void GLWorldScene::moleculeRemoved(const molecule *_molecule)
|
---|
| 213 | {
|
---|
[3927ef] | 214 | LOG(3, "INFO: GLWorldScene: Received signal moleculeRemoved for molecule "+toString(_molecule->getId())+".");
|
---|
[c67518] | 215 | MoleculeNodeMap::iterator iter = MoleculesinSceneMap.find(_molecule->getId());
|
---|
[3927ef] | 216 |
|
---|
| 217 | // only remove if the molecule is in the scene
|
---|
| 218 | // (= is selected)
|
---|
| 219 | if (iter != MoleculesinSceneMap.end()){
|
---|
| 220 | GLMoleculeObject_molecule *molObject = iter->second;
|
---|
| 221 | molObject->disconnect();
|
---|
| 222 | MoleculesinSceneMap.erase(iter);
|
---|
| 223 | delete molObject;
|
---|
| 224 | emit changed();
|
---|
| 225 | emit changeOccured();
|
---|
| 226 | }
|
---|
[c67518] | 227 | }
|
---|
| 228 |
|
---|
[37b2575] | 229 | /** Adds a bond to the scene.
|
---|
[7188b1] | 230 | *
|
---|
| 231 | * @param _bond bond to add
|
---|
[2ad1ec] | 232 | * @param side which side of the bond (left or right)
|
---|
[7188b1] | 233 | */
|
---|
[2ad1ec] | 234 | void GLWorldScene::bondInserted(const bond *_bond, const enum GLMoleculeObject_bond::SideOfBond side)
|
---|
[7188b1] | 235 | {
|
---|
[57a770] | 236 | LOG(3, "INFO: GLWorldScene::bondInserted() - Adding bond "+toString(*_bond)+".");
|
---|
[2ad1ec] | 237 | //LOG(4, "INFO: Currently present bonds " << BondsinSceneMap << ".");
|
---|
| 238 |
|
---|
| 239 | BondIds ids;
|
---|
| 240 | switch (side) {
|
---|
| 241 | case GLMoleculeObject_bond::left:
|
---|
| 242 | ids = std::make_pair(_bond->leftatom->getId(), _bond->rightatom->getId());
|
---|
| 243 | break;
|
---|
| 244 | case GLMoleculeObject_bond::right:
|
---|
| 245 | ids = std::make_pair(_bond->rightatom->getId(), _bond->leftatom->getId());
|
---|
| 246 | break;
|
---|
[7188b1] | 247 | }
|
---|
[2ad1ec] | 248 | #ifndef NDEBUG
|
---|
| 249 | BondNodeMap::iterator iter = BondsinSceneMap.find(ids);
|
---|
| 250 | ASSERT(iter == BondsinSceneMap.end(),
|
---|
| 251 | "GLWorldScene::bondAdded() - same left-sided bond "+toString(*_bond)+" added again.");
|
---|
| 252 | #endif
|
---|
| 253 | GLMoleculeObject_bond *bondObject =
|
---|
[bca99d] | 254 | new GLMoleculeObject_bond(meshCylinderHi, this, _bond, side);
|
---|
[2ad1ec] | 255 | connect (
|
---|
| 256 | bondObject, SIGNAL(BondRemoved(const atomId_t, const atomId_t)),
|
---|
| 257 | this, SLOT(bondRemoved(const atomId_t, const atomId_t)));
|
---|
[5a2a06] | 258 | connect (bondObject, SIGNAL(changed()), this, SIGNAL(changed()));
|
---|
[2ad1ec] | 259 | BondsinSceneMap.insert( make_pair(ids, bondObject) );
|
---|
| 260 | // BondIdsinSceneMap.insert( Leftids );
|
---|
[65487f] | 261 | emit changeOccured();
|
---|
[7188b1] | 262 | }
|
---|
[907636] | 263 |
|
---|
[37b2575] | 264 | /** Removes a bond from the scene.
|
---|
[7188b1] | 265 | *
|
---|
| 266 | * @param _bond bond to remove
|
---|
| 267 | */
|
---|
[37b2575] | 268 | void GLWorldScene::bondRemoved(const atomId_t leftnr, const atomId_t rightnr)
|
---|
[907636] | 269 | {
|
---|
[2ad1ec] | 270 | LOG(3, "INFO: GLWorldScene::bondRemoved() - Removing bond between "+toString(leftnr)+" and "+toString(rightnr)+".");
|
---|
[7188b1] | 271 | {
|
---|
| 272 | // left bond
|
---|
[37b2575] | 273 | const BondIds Leftids( make_pair(leftnr, rightnr) );
|
---|
| 274 | BondNodeMap::iterator leftiter = BondsinSceneMap.find( Leftids );
|
---|
| 275 | ASSERT(leftiter != BondsinSceneMap.end(),
|
---|
| 276 | "GLWorldScene::bondRemoved() - bond "+toString(leftnr)+"-"
|
---|
| 277 | +toString(rightnr)+" not on display.");
|
---|
[2ad1ec] | 278 | //GLMoleculeObject_bond *bondObject = leftiter->second;
|
---|
[37b2575] | 279 | BondsinSceneMap.erase(leftiter);
|
---|
[2ad1ec] | 280 | //delete bondObject; // is done by signal from bond itself
|
---|
| 281 | //LOG(4, "INFO: Still present bonds " << BondsinSceneMap << ".");
|
---|
[7188b1] | 282 | }
|
---|
[2ad1ec] | 283 |
|
---|
[65487f] | 284 | emit changeOccured();
|
---|
[7188b1] | 285 | }
|
---|
| 286 |
|
---|
| 287 | void GLWorldScene::initialize(QGLView *view, QGLPainter *painter) const
|
---|
| 288 | {
|
---|
| 289 | // Initialize all of the mesh objects that we have as children.
|
---|
| 290 | foreach (QObject *obj, children()) {
|
---|
| 291 | GLMoleculeObject *meshobj = qobject_cast<GLMoleculeObject *>(obj);
|
---|
| 292 | if (meshobj)
|
---|
| 293 | meshobj->initialize(view, painter);
|
---|
| 294 | }
|
---|
| 295 | }
|
---|
| 296 |
|
---|
| 297 | void GLWorldScene::draw(QGLPainter *painter) const
|
---|
| 298 | {
|
---|
| 299 | // Draw all of the mesh objects that we have as children.
|
---|
| 300 | foreach (QObject *obj, children()) {
|
---|
| 301 | GLMoleculeObject *meshobj = qobject_cast<GLMoleculeObject *>(obj);
|
---|
| 302 | if (meshobj)
|
---|
| 303 | meshobj->draw(painter);
|
---|
| 304 | }
|
---|
[907636] | 305 | }
|
---|
[06ebf5] | 306 |
|
---|
[7188b1] | 307 | void GLWorldScene::atomClicked(atomId_t no)
|
---|
[907636] | 308 | {
|
---|
[57a770] | 309 | LOG(3, "INFO: GLWorldScene - atom " << no << " has been clicked.");
|
---|
[89643d] | 310 | const atom *Walker = World::getInstance().getAtom(AtomById(no));
|
---|
[6966b7] | 311 | if (selectionMode == SelectAtom){
|
---|
| 312 | if (!World::getInstance().isSelected(Walker))
|
---|
| 313 | SelectionAtomById(no);
|
---|
| 314 | else
|
---|
| 315 | SelectionNotAtomById(no);
|
---|
| 316 | }else if (selectionMode == SelectMolecule){
|
---|
| 317 | const molecule *mol = Walker->getMolecule();
|
---|
| 318 | ASSERT(mol, "Atom without molecule has been clicked.");
|
---|
| 319 | if (!World::getInstance().isSelected(mol))
|
---|
| 320 | SelectionMoleculeById(mol->getId());
|
---|
| 321 | else
|
---|
| 322 | SelectionNotMoleculeById(mol->getId());
|
---|
| 323 | }
|
---|
[7188b1] | 324 | emit clicked(no);
|
---|
[907636] | 325 | }
|
---|
[029bb4] | 326 |
|
---|
[6966b7] | 327 | void GLWorldScene::setSelectionMode(SelectionModeType mode)
|
---|
| 328 | {
|
---|
| 329 | selectionMode = mode;
|
---|
| 330 | // TODO send update to toolbar
|
---|
| 331 | }
|
---|
| 332 |
|
---|
| 333 | void GLWorldScene::setSelectionModeAtom()
|
---|
| 334 | {
|
---|
| 335 | setSelectionMode(SelectAtom);
|
---|
| 336 | }
|
---|
| 337 |
|
---|
| 338 | void GLWorldScene::setSelectionModeMolecule()
|
---|
| 339 | {
|
---|
| 340 | setSelectionMode(SelectMolecule);
|
---|
| 341 | }
|
---|
| 342 |
|
---|
[407638e] | 343 | void GLWorldScene::hoverChangedSignalled(GLMoleculeObject *ob)
|
---|
| 344 | {
|
---|
| 345 | // Find the atom, ob corresponds to.
|
---|
| 346 | hoverAtom = NULL;
|
---|
| 347 | GLMoleculeObject_atom *atomObject = dynamic_cast<GLMoleculeObject_atom *>(ob);
|
---|
| 348 | if (atomObject){
|
---|
| 349 | for (AtomNodeMap::iterator iter = AtomsinSceneMap.begin();iter != AtomsinSceneMap.end(); ++ iter){
|
---|
| 350 | if (iter->second == atomObject)
|
---|
| 351 | hoverAtom = World::getInstance().getAtom(AtomById(iter->first));
|
---|
| 352 | }
|
---|
| 353 | }
|
---|
| 354 |
|
---|
| 355 | // Propagate signal.
|
---|
| 356 | emit hoverChanged(hoverAtom);
|
---|
| 357 | }
|
---|
| 358 |
|
---|
[30cd0d] | 359 | void GLWorldScene::changeAtomId(GLMoleculeObject_atom *ob, int oldId, int newId)
|
---|
| 360 | {
|
---|
| 361 | LOG(3, "INFO: GLWorldScene - change atom id " << oldId << " to " << newId << ".");
|
---|
| 362 | // Remove from map.
|
---|
| 363 | AtomNodeMap::iterator iter = AtomsinSceneMap.find(oldId);
|
---|
| 364 | ASSERT(iter != AtomsinSceneMap.end(),
|
---|
| 365 | "GLWorldScene::objectIdChangedSignalled() - atom with id "+toString(oldId)+" not on display.");
|
---|
| 366 | GLMoleculeObject_atom *atomObject = iter->second;
|
---|
| 367 | AtomsinSceneMap.erase(iter);
|
---|
| 368 |
|
---|
| 369 | // Reinsert with new id.
|
---|
| 370 | AtomsinSceneMap.insert( make_pair(newId, atomObject) );
|
---|
| 371 | }
|
---|
| 372 |
|
---|