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