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 "Actions/SelectionAction/Molecules/MoleculeByIdAction.hpp"
|
---|
41 | #include "Actions/SelectionAction/Molecules/NotMoleculeByIdAction.hpp"
|
---|
42 | #include "Atom/atom.hpp"
|
---|
43 | #include "Bond/bond.hpp"
|
---|
44 | #include "Descriptors/AtomIdDescriptor.hpp"
|
---|
45 | #include "Helpers/helpers.hpp"
|
---|
46 | #include "molecule.hpp"
|
---|
47 | #include "World.hpp"
|
---|
48 |
|
---|
49 | #include <iostream>
|
---|
50 |
|
---|
51 | using namespace MoleCuilder;
|
---|
52 |
|
---|
53 | std::ostream &operator<<(std::ostream &ost, const GLWorldScene::BondIds &t)
|
---|
54 | {
|
---|
55 | ost << t.first << "," << t.second;
|
---|
56 | return ost;
|
---|
57 | }
|
---|
58 |
|
---|
59 | GLWorldScene::GLWorldScene(QObject *parent)
|
---|
60 | : QObject(parent),
|
---|
61 | hoverAtom(NULL)
|
---|
62 | {
|
---|
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 |
|
---|
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 |
|
---|
83 | setSelectionMode(SelectAtom);
|
---|
84 |
|
---|
85 | init();
|
---|
86 | }
|
---|
87 |
|
---|
88 | GLWorldScene::~GLWorldScene()
|
---|
89 | {
|
---|
90 | // remove all elements
|
---|
91 | GLMoleculeObject::cleanMaterialMap();
|
---|
92 | }
|
---|
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++) {
|
---|
105 |
|
---|
106 | for (molecule::const_iterator atomiter = (*Runner)->begin();
|
---|
107 | atomiter != (*Runner)->end();
|
---|
108 | ++atomiter) {
|
---|
109 | // create atom objects in scene
|
---|
110 | atomInserted(*atomiter);
|
---|
111 |
|
---|
112 | // create bond objects in scene
|
---|
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 | }
|
---|
122 | }
|
---|
123 | }
|
---|
124 | }
|
---|
125 | }
|
---|
126 |
|
---|
127 | /** Adds an atom to the scene.
|
---|
128 | *
|
---|
129 | * @param _atom atom to add
|
---|
130 | */
|
---|
131 | void GLWorldScene::atomInserted(const atom *_atom)
|
---|
132 | {
|
---|
133 | LOG(3, "INFO: GLWorldScene: Received signal atomInserted for atom "+toString(_atom->getId())+".");
|
---|
134 | GLMoleculeObject_atom *atomObject = new GLMoleculeObject_atom(meshSphereHi, this, _atom);
|
---|
135 | AtomNodeMap::iterator iter = AtomsinSceneMap.find(_atom->getId());
|
---|
136 | ASSERT(iter == AtomsinSceneMap.end(),
|
---|
137 | "GLWorldScene::atomAdded() - same atom "+_atom->getName()+" added again.");
|
---|
138 | AtomsinSceneMap.insert( make_pair(_atom->getId(), atomObject) );
|
---|
139 | connect (atomObject, SIGNAL(clicked(atomId_t)), this, SLOT(atomClicked(atomId_t)));
|
---|
140 | connect (atomObject, SIGNAL(changed()), this, SIGNAL(changed()));
|
---|
141 | connect (atomObject, SIGNAL(hoverChanged(GLMoleculeObject *)), this, SIGNAL(changed()));
|
---|
142 | connect (atomObject, SIGNAL(hoverChanged(GLMoleculeObject *)), this, SLOT(hoverChangedSignalled(GLMoleculeObject *)));
|
---|
143 | connect (atomObject, SIGNAL(selectionChanged()), this, SIGNAL(changed()));
|
---|
144 | connect (atomObject, SIGNAL(BondsInserted(const bond *, const GLMoleculeObject_bond::SideOfBond)), this, SLOT(bondInserted(const bond *, const GLMoleculeObject_bond::SideOfBond)));
|
---|
145 | connect (atomObject, SIGNAL(indexChanged(GLMoleculeObject_atom*, int, int)), this, SLOT(changeAtomId(GLMoleculeObject_atom*, int, int)));
|
---|
146 | //bondsChanged(_atom);
|
---|
147 | emit changeOccured();
|
---|
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 | {
|
---|
156 | LOG(3, "INFO: GLWorldScene: Received signal atomRemoved for atom "+toString(_atom->getId())+".");
|
---|
157 | // bonds are removed by signal coming from ~bond
|
---|
158 | // remove atoms
|
---|
159 | AtomNodeMap::iterator iter = AtomsinSceneMap.find(_atom->getId());
|
---|
160 | ASSERT(iter != AtomsinSceneMap.end(),
|
---|
161 | "GLWorldScene::atomRemoved() - atom "+_atom->getName()+" not on display.");
|
---|
162 | GLMoleculeObject_atom *atomObject = iter->second;
|
---|
163 | atomObject->disconnect();
|
---|
164 | AtomsinSceneMap.erase(iter);
|
---|
165 | delete atomObject;
|
---|
166 | emit changeOccured();
|
---|
167 | }
|
---|
168 |
|
---|
169 | /** ....
|
---|
170 | *
|
---|
171 | */
|
---|
172 | void GLWorldScene::worldSelectionChanged()
|
---|
173 | {
|
---|
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
|
---|
189 | GLMoleculeObject_molecule *molObject = new GLMoleculeObject_molecule(meshEmpty, this, *Runner);
|
---|
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 | }
|
---|
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 | {
|
---|
214 | LOG(3, "INFO: GLWorldScene: Received signal moleculeRemoved for molecule "+toString(_molecule->getId())+".");
|
---|
215 | MoleculeNodeMap::iterator iter = MoleculesinSceneMap.find(_molecule->getId());
|
---|
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 | }
|
---|
227 | }
|
---|
228 |
|
---|
229 | /** Adds a bond to the scene.
|
---|
230 | *
|
---|
231 | * @param _bond bond to add
|
---|
232 | * @param side which side of the bond (left or right)
|
---|
233 | */
|
---|
234 | void GLWorldScene::bondInserted(const bond *_bond, const enum GLMoleculeObject_bond::SideOfBond side)
|
---|
235 | {
|
---|
236 | LOG(3, "INFO: GLWorldScene::bondInserted() - Adding bond "+toString(*_bond)+".");
|
---|
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;
|
---|
247 | }
|
---|
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 =
|
---|
254 | new GLMoleculeObject_bond(meshCylinderHi, this, _bond, side);
|
---|
255 | connect (
|
---|
256 | bondObject, SIGNAL(BondRemoved(const atomId_t, const atomId_t)),
|
---|
257 | this, SLOT(bondRemoved(const atomId_t, const atomId_t)));
|
---|
258 | connect (bondObject, SIGNAL(changed()), this, SIGNAL(changed()));
|
---|
259 | BondsinSceneMap.insert( make_pair(ids, bondObject) );
|
---|
260 | // BondIdsinSceneMap.insert( Leftids );
|
---|
261 | emit changeOccured();
|
---|
262 | }
|
---|
263 |
|
---|
264 | /** Removes a bond from the scene.
|
---|
265 | *
|
---|
266 | * @param _bond bond to remove
|
---|
267 | */
|
---|
268 | void GLWorldScene::bondRemoved(const atomId_t leftnr, const atomId_t rightnr)
|
---|
269 | {
|
---|
270 | LOG(3, "INFO: GLWorldScene::bondRemoved() - Removing bond between "+toString(leftnr)+" and "+toString(rightnr)+".");
|
---|
271 | {
|
---|
272 | // left bond
|
---|
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.");
|
---|
278 | //GLMoleculeObject_bond *bondObject = leftiter->second;
|
---|
279 | BondsinSceneMap.erase(leftiter);
|
---|
280 | //delete bondObject; // is done by signal from bond itself
|
---|
281 | //LOG(4, "INFO: Still present bonds " << BondsinSceneMap << ".");
|
---|
282 | }
|
---|
283 |
|
---|
284 | emit changeOccured();
|
---|
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 | }
|
---|
305 | }
|
---|
306 |
|
---|
307 | void GLWorldScene::atomClicked(atomId_t no)
|
---|
308 | {
|
---|
309 | LOG(3, "INFO: GLWorldScene - atom " << no << " has been clicked.");
|
---|
310 | const atom *Walker = World::getInstance().getAtom(AtomById(no));
|
---|
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 | }
|
---|
324 | emit clicked(no);
|
---|
325 | }
|
---|
326 |
|
---|
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 |
|
---|
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 |
|
---|
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 |
|
---|