1 | /*
|
---|
2 | * Project: MoleCuilder
|
---|
3 | * Description: creates and alters molecular systems
|
---|
4 | * Copyright (C) 2010-2012 University of Bonn. All rights reserved.
|
---|
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 | #include <Qt3D/qglview.h>
|
---|
24 | #include <Qt3D/qglbuilder.h>
|
---|
25 | #include <Qt3D/qglscenenode.h>
|
---|
26 | #include <Qt3D/qglsphere.h>
|
---|
27 | #include <Qt3D/qglcylinder.h>
|
---|
28 |
|
---|
29 | #include "GLMoleculeObject.hpp"
|
---|
30 | #include "GLMoleculeObject_atom.hpp"
|
---|
31 | #include "GLMoleculeObject_bond.hpp"
|
---|
32 | #include "GLMoleculeObject_molecule.hpp"
|
---|
33 |
|
---|
34 | #include "CodePatterns/MemDebug.hpp"
|
---|
35 |
|
---|
36 | #include "CodePatterns/Log.hpp"
|
---|
37 |
|
---|
38 | #include "Actions/SelectionAction/Atoms/AtomByIdAction.hpp"
|
---|
39 | #include "Actions/SelectionAction/Atoms/NotAtomByIdAction.hpp"
|
---|
40 | #include "Atom/atom.hpp"
|
---|
41 | #include "Bond/bond.hpp"
|
---|
42 | #include "Descriptors/AtomIdDescriptor.hpp"
|
---|
43 | #include "Helpers/helpers.hpp"
|
---|
44 | #include "molecule.hpp"
|
---|
45 | #include "World.hpp"
|
---|
46 |
|
---|
47 | #include <iostream>
|
---|
48 |
|
---|
49 | using namespace MoleCuilder;
|
---|
50 |
|
---|
51 | std::ostream &operator<<(std::ostream &ost, const GLWorldScene::BondIds &t)
|
---|
52 | {
|
---|
53 | ost << t.first << "," << t.second;
|
---|
54 | return ost;
|
---|
55 | }
|
---|
56 |
|
---|
57 | GLWorldScene::GLWorldScene(QObject *parent)
|
---|
58 | : QObject(parent)
|
---|
59 | {
|
---|
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 |
|
---|
75 | init();
|
---|
76 | }
|
---|
77 |
|
---|
78 | GLWorldScene::~GLWorldScene()
|
---|
79 | {
|
---|
80 | // remove all elements
|
---|
81 | GLMoleculeObject::cleanMaterialMap();
|
---|
82 | }
|
---|
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++) {
|
---|
95 |
|
---|
96 | for (molecule::const_iterator atomiter = (*Runner)->begin();
|
---|
97 | atomiter != (*Runner)->end();
|
---|
98 | ++atomiter) {
|
---|
99 | // create atom objects in scene
|
---|
100 | atomInserted(*atomiter);
|
---|
101 |
|
---|
102 | // create bond objects in scene
|
---|
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 | }
|
---|
112 | }
|
---|
113 | }
|
---|
114 | }
|
---|
115 | }
|
---|
116 |
|
---|
117 | /** Adds an atom to the scene.
|
---|
118 | *
|
---|
119 | * @param _atom atom to add
|
---|
120 | */
|
---|
121 | void GLWorldScene::atomInserted(const atom *_atom)
|
---|
122 | {
|
---|
123 | LOG(3, "INFO: GLWorldScene: Received signal atomInserted for atom "+toString(_atom->getId())+".");
|
---|
124 | GLMoleculeObject_atom *atomObject = new GLMoleculeObject_atom(meshSphereHi, this, _atom);
|
---|
125 | AtomNodeMap::iterator iter = AtomsinSceneMap.find(_atom->getId());
|
---|
126 | ASSERT(iter == AtomsinSceneMap.end(),
|
---|
127 | "GLWorldScene::atomAdded() - same atom "+_atom->getName()+" added again.");
|
---|
128 | AtomsinSceneMap.insert( make_pair(_atom->getId(), atomObject) );
|
---|
129 | connect (atomObject, SIGNAL(clicked(atomId_t)), this, SLOT(atomClicked(atomId_t)));
|
---|
130 | connect (atomObject, SIGNAL(changed()), this, SIGNAL(changed()));
|
---|
131 | connect (atomObject, SIGNAL(hoverChanged()), this, SIGNAL(changed()));
|
---|
132 | connect (atomObject, SIGNAL(selectionChanged()), this, SIGNAL(changed()));
|
---|
133 | connect (atomObject, SIGNAL(BondsInserted(const bond *, const GLMoleculeObject_bond::SideOfBond)), this, SLOT(bondInserted(const bond *, const GLMoleculeObject_bond::SideOfBond)));
|
---|
134 | //bondsChanged(_atom);
|
---|
135 | emit changeOccured();
|
---|
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 | {
|
---|
144 | LOG(3, "INFO: GLWorldScene: Received signal atomRemoved for atom "+toString(_atom->getId())+".");
|
---|
145 | // bonds are removed by signal coming from ~bond
|
---|
146 | // remove atoms
|
---|
147 | AtomNodeMap::iterator iter = AtomsinSceneMap.find(_atom->getId());
|
---|
148 | ASSERT(iter != AtomsinSceneMap.end(),
|
---|
149 | "GLWorldScene::atomRemoved() - atom "+_atom->getName()+" not on display.");
|
---|
150 | GLMoleculeObject_atom *atomObject = iter->second;
|
---|
151 | atomObject->disconnect();
|
---|
152 | AtomsinSceneMap.erase(iter);
|
---|
153 | delete atomObject;
|
---|
154 | emit changeOccured();
|
---|
155 | }
|
---|
156 |
|
---|
157 | /** ....
|
---|
158 | *
|
---|
159 | */
|
---|
160 | void GLWorldScene::worldSelectionChanged()
|
---|
161 | {
|
---|
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
|
---|
177 | GLMoleculeObject_molecule *molObject = new GLMoleculeObject_molecule(meshEmpty, this, *Runner);
|
---|
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 | }
|
---|
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 | {
|
---|
202 | LOG(3, "INFO: GLWorldScene: Received signal moleculeRemoved for molecule "+toString(_molecule->getId())+".");
|
---|
203 | MoleculeNodeMap::iterator iter = MoleculesinSceneMap.find(_molecule->getId());
|
---|
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 | }
|
---|
215 | }
|
---|
216 |
|
---|
217 | /** Adds a bond to the scene.
|
---|
218 | *
|
---|
219 | * @param _bond bond to add
|
---|
220 | * @param side which side of the bond (left or right)
|
---|
221 | */
|
---|
222 | void GLWorldScene::bondInserted(const bond *_bond, const enum GLMoleculeObject_bond::SideOfBond side)
|
---|
223 | {
|
---|
224 | LOG(3, "INFO: GLWorldScene::bondInserted() - Adding bond "+toString(*_bond)+".");
|
---|
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;
|
---|
235 | }
|
---|
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 =
|
---|
242 | new GLMoleculeObject_bond(meshCylinderHi, this, _bond, side);
|
---|
243 | connect (
|
---|
244 | bondObject, SIGNAL(BondRemoved(const atomId_t, const atomId_t)),
|
---|
245 | this, SLOT(bondRemoved(const atomId_t, const atomId_t)));
|
---|
246 | connect (bondObject, SIGNAL(changed()), this, SIGNAL(changed()));
|
---|
247 | BondsinSceneMap.insert( make_pair(ids, bondObject) );
|
---|
248 | // BondIdsinSceneMap.insert( Leftids );
|
---|
249 | emit changeOccured();
|
---|
250 | }
|
---|
251 |
|
---|
252 | /** Removes a bond from the scene.
|
---|
253 | *
|
---|
254 | * @param _bond bond to remove
|
---|
255 | */
|
---|
256 | void GLWorldScene::bondRemoved(const atomId_t leftnr, const atomId_t rightnr)
|
---|
257 | {
|
---|
258 | LOG(3, "INFO: GLWorldScene::bondRemoved() - Removing bond between "+toString(leftnr)+" and "+toString(rightnr)+".");
|
---|
259 | {
|
---|
260 | // left bond
|
---|
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.");
|
---|
266 | //GLMoleculeObject_bond *bondObject = leftiter->second;
|
---|
267 | BondsinSceneMap.erase(leftiter);
|
---|
268 | //delete bondObject; // is done by signal from bond itself
|
---|
269 | //LOG(4, "INFO: Still present bonds " << BondsinSceneMap << ".");
|
---|
270 | }
|
---|
271 |
|
---|
272 | emit changeOccured();
|
---|
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 | }
|
---|
293 | }
|
---|
294 |
|
---|
295 | void GLWorldScene::atomClicked(atomId_t no)
|
---|
296 | {
|
---|
297 | LOG(3, "INFO: GLWorldScene - atom " << no << " has been clicked.");
|
---|
298 | const atom *Walker = World::getInstance().getAtom(AtomById(no));
|
---|
299 | if (!World::getInstance().isSelected(Walker))
|
---|
300 | SelectionAtomById(no);
|
---|
301 | else
|
---|
302 | SelectionNotAtomById(no);
|
---|
303 | emit clicked(no);
|
---|
304 | }
|
---|
305 |
|
---|