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