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 |
|
---|
25 | #include "GLMoleculeObject.hpp"
|
---|
26 | #include "GLMoleculeObject_atom.hpp"
|
---|
27 | #include "GLMoleculeObject_bond.hpp"
|
---|
28 | #include "GLMoleculeObject_molecule.hpp"
|
---|
29 |
|
---|
30 | #include "CodePatterns/MemDebug.hpp"
|
---|
31 |
|
---|
32 | #include "CodePatterns/Log.hpp"
|
---|
33 |
|
---|
34 | #include "Actions/SelectionAction/Atoms/AtomByIdAction.hpp"
|
---|
35 | #include "Actions/SelectionAction/Atoms/NotAtomByIdAction.hpp"
|
---|
36 | #include "Atom/atom.hpp"
|
---|
37 | #include "Bond/bond.hpp"
|
---|
38 | #include "Descriptors/AtomIdDescriptor.hpp"
|
---|
39 | #include "Helpers/helpers.hpp"
|
---|
40 | #include "molecule.hpp"
|
---|
41 | #include "World.hpp"
|
---|
42 |
|
---|
43 | #include <iostream>
|
---|
44 |
|
---|
45 | using namespace MoleCuilder;
|
---|
46 |
|
---|
47 | std::ostream &operator<<(std::ostream &ost, const GLWorldScene::BondIds &t)
|
---|
48 | {
|
---|
49 | ost << t.first << "," << t.second;
|
---|
50 | return ost;
|
---|
51 | }
|
---|
52 |
|
---|
53 | GLWorldScene::GLWorldScene(QObject *parent)
|
---|
54 | : QObject(parent)
|
---|
55 | {
|
---|
56 | init();
|
---|
57 |
|
---|
58 | //changeMaterials(false);
|
---|
59 | domainBoxMaterial = new QGLMaterial;
|
---|
60 | domainBoxMaterial->setAmbientColor(QColor(0,0,0,255));
|
---|
61 | domainBoxMaterial->setDiffuseColor(QColor(0,0,0,255));
|
---|
62 | domainBoxMaterial->setEmittedLight(QColor(150,160,200,255));
|
---|
63 |
|
---|
64 | dreiBeinMaterial = new QGLMaterial;
|
---|
65 | dreiBeinMaterial->setAmbientColor(QColor(0,0,0,255));
|
---|
66 | dreiBeinMaterial->setDiffuseColor(QColor(0,0,0,255));
|
---|
67 | dreiBeinMaterial->setEmittedLight(QColor(150,200,150,255));
|
---|
68 | }
|
---|
69 |
|
---|
70 | GLWorldScene::~GLWorldScene()
|
---|
71 | {
|
---|
72 | // remove all elements
|
---|
73 | GLMoleculeObject::cleanMaterialMap();
|
---|
74 |
|
---|
75 | delete(domainBoxMaterial);
|
---|
76 | delete(dreiBeinMaterial);
|
---|
77 | }
|
---|
78 |
|
---|
79 | /** Initialise the WorldScene with molecules and atoms from World.
|
---|
80 | *
|
---|
81 | */
|
---|
82 | void GLWorldScene::init()
|
---|
83 | {
|
---|
84 | const std::vector<molecule*> &molecules = World::getInstance().getAllMolecules();
|
---|
85 |
|
---|
86 | if (molecules.size() > 0) {
|
---|
87 | for (std::vector<molecule*>::const_iterator Runner = molecules.begin();
|
---|
88 | Runner != molecules.end();
|
---|
89 | Runner++) {
|
---|
90 |
|
---|
91 | for (molecule::const_iterator atomiter = (*Runner)->begin();
|
---|
92 | atomiter != (*Runner)->end();
|
---|
93 | ++atomiter) {
|
---|
94 | // create atom objects in scene
|
---|
95 | atomInserted(*atomiter);
|
---|
96 |
|
---|
97 | // create bond objects in scene
|
---|
98 | const BondList &bondlist = (*atomiter)->getListOfBonds();
|
---|
99 | for (BondList::const_iterator bonditer = bondlist.begin();
|
---|
100 | bonditer != bondlist.end();
|
---|
101 | ++bonditer) {
|
---|
102 | const bond *_bond = *bonditer;
|
---|
103 | const GLMoleculeObject_bond::SideOfBond side = (_bond->leftatom == *atomiter) ?
|
---|
104 | GLMoleculeObject_bond::left : GLMoleculeObject_bond::right;
|
---|
105 | bondInserted(_bond, side);
|
---|
106 | }
|
---|
107 | }
|
---|
108 | }
|
---|
109 | }
|
---|
110 | }
|
---|
111 |
|
---|
112 | /** Adds an atom to the scene.
|
---|
113 | *
|
---|
114 | * @param _atom atom to add
|
---|
115 | */
|
---|
116 | void GLWorldScene::atomInserted(const atom *_atom)
|
---|
117 | {
|
---|
118 | LOG(3, "INFO: GLWorldScene: Received signal atomInserted for atom "+toString(_atom->getId())+".");
|
---|
119 | GLMoleculeObject_atom *atomObject = new GLMoleculeObject_atom(this, _atom);
|
---|
120 | AtomNodeMap::iterator iter = AtomsinSceneMap.find(_atom->getId());
|
---|
121 | ASSERT(iter == AtomsinSceneMap.end(),
|
---|
122 | "GLWorldScene::atomAdded() - same atom "+_atom->getName()+" added again.");
|
---|
123 | AtomsinSceneMap.insert( make_pair(_atom->getId(), atomObject) );
|
---|
124 | connect (atomObject, SIGNAL(clicked(atomId_t)), this, SLOT(atomClicked(atomId_t)));
|
---|
125 | connect (atomObject, SIGNAL(changed()), this, SIGNAL(changed()));
|
---|
126 | connect (atomObject, SIGNAL(hoverChanged()), this, SIGNAL(changed()));
|
---|
127 | connect (atomObject, SIGNAL(selectionChanged()), this, SIGNAL(changed()));
|
---|
128 | connect (atomObject, SIGNAL(BondsInserted(const bond *, const GLMoleculeObject_bond::SideOfBond)), this, SLOT(bondInserted(const bond *, const GLMoleculeObject_bond::SideOfBond)));
|
---|
129 | //bondsChanged(_atom);
|
---|
130 | emit changeOccured();
|
---|
131 | }
|
---|
132 |
|
---|
133 | /** Removes an atom from the scene.
|
---|
134 | *
|
---|
135 | * @param _atom atom to remove
|
---|
136 | */
|
---|
137 | void GLWorldScene::atomRemoved(const atom *_atom)
|
---|
138 | {
|
---|
139 | LOG(3, "INFO: GLWorldScene: Received signal atomRemoved for atom "+toString(_atom->getId())+".");
|
---|
140 | // bonds are removed by signal coming from ~bond
|
---|
141 | // remove atoms
|
---|
142 | AtomNodeMap::iterator iter = AtomsinSceneMap.find(_atom->getId());
|
---|
143 | ASSERT(iter != AtomsinSceneMap.end(),
|
---|
144 | "GLWorldScene::atomRemoved() - atom "+_atom->getName()+" not on display.");
|
---|
145 | GLMoleculeObject_atom *atomObject = iter->second;
|
---|
146 | atomObject->disconnect();
|
---|
147 | AtomsinSceneMap.erase(iter);
|
---|
148 | delete atomObject;
|
---|
149 | emit changeOccured();
|
---|
150 | }
|
---|
151 |
|
---|
152 | /** ....
|
---|
153 | *
|
---|
154 | */
|
---|
155 | void GLWorldScene::worldSelectionChanged()
|
---|
156 | {
|
---|
157 | LOG(3, "INFO: GLWorldScene: Received signal selectionChanged.");
|
---|
158 |
|
---|
159 | const std::vector<molecule*> &molecules = World::getInstance().getAllMolecules();
|
---|
160 |
|
---|
161 | if (molecules.size() > 0) {
|
---|
162 | for (std::vector<molecule*>::const_iterator Runner = molecules.begin();
|
---|
163 | Runner != molecules.end();
|
---|
164 | Runner++) {
|
---|
165 |
|
---|
166 | MoleculeNodeMap::iterator iter = MoleculesinSceneMap.find((*Runner)->getId());
|
---|
167 | bool isSelected = World::getInstance().isSelected(*Runner);
|
---|
168 |
|
---|
169 | // molecule selected but not in scene?
|
---|
170 | if (isSelected && (iter == MoleculesinSceneMap.end())){
|
---|
171 | // -> create new mesh
|
---|
172 | GLMoleculeObject_molecule *molObject = new GLMoleculeObject_molecule(this, *Runner);
|
---|
173 | MoleculesinSceneMap.insert( make_pair((*Runner)->getId(), molObject) );
|
---|
174 | connect (molObject, SIGNAL(changed()), this, SIGNAL(changed()));
|
---|
175 | connect (molObject, SIGNAL(selectionChanged()), this, SIGNAL(changed()));
|
---|
176 | connect (molObject, SIGNAL(selectionChanged()), this, SIGNAL(changed()));
|
---|
177 | emit changed();
|
---|
178 | emit changeOccured();
|
---|
179 | }
|
---|
180 |
|
---|
181 | // molecule not selected but in scene?
|
---|
182 | if (!isSelected && (iter != MoleculesinSceneMap.end())){
|
---|
183 | // -> remove from scene
|
---|
184 | moleculeRemoved(*Runner);
|
---|
185 | }
|
---|
186 |
|
---|
187 | }
|
---|
188 | }
|
---|
189 | }
|
---|
190 |
|
---|
191 | /** Removes a molecule from the scene.
|
---|
192 | *
|
---|
193 | * @param _molecule molecule to remove
|
---|
194 | */
|
---|
195 | void GLWorldScene::moleculeRemoved(const molecule *_molecule)
|
---|
196 | {
|
---|
197 | LOG(3, "INFO: GLWorldScene: Received signal moleculeRemoved for molecule "+toString(_molecule->getId())+".");
|
---|
198 | MoleculeNodeMap::iterator iter = MoleculesinSceneMap.find(_molecule->getId());
|
---|
199 |
|
---|
200 | // only remove if the molecule is in the scene
|
---|
201 | // (= is selected)
|
---|
202 | if (iter != MoleculesinSceneMap.end()){
|
---|
203 | GLMoleculeObject_molecule *molObject = iter->second;
|
---|
204 | molObject->disconnect();
|
---|
205 | MoleculesinSceneMap.erase(iter);
|
---|
206 | delete molObject;
|
---|
207 | emit changed();
|
---|
208 | emit changeOccured();
|
---|
209 | }
|
---|
210 | }
|
---|
211 |
|
---|
212 | /** Adds a bond to the scene.
|
---|
213 | *
|
---|
214 | * @param _bond bond to add
|
---|
215 | * @param side which side of the bond (left or right)
|
---|
216 | */
|
---|
217 | void GLWorldScene::bondInserted(const bond *_bond, const enum GLMoleculeObject_bond::SideOfBond side)
|
---|
218 | {
|
---|
219 | LOG(3, "INFO: GLWorldScene::bondInserted() - Adding bond "+toString(*_bond)+".");
|
---|
220 | //LOG(4, "INFO: Currently present bonds " << BondsinSceneMap << ".");
|
---|
221 |
|
---|
222 | BondIds ids;
|
---|
223 | switch (side) {
|
---|
224 | case GLMoleculeObject_bond::left:
|
---|
225 | ids = std::make_pair(_bond->leftatom->getId(), _bond->rightatom->getId());
|
---|
226 | break;
|
---|
227 | case GLMoleculeObject_bond::right:
|
---|
228 | ids = std::make_pair(_bond->rightatom->getId(), _bond->leftatom->getId());
|
---|
229 | break;
|
---|
230 | }
|
---|
231 | #ifndef NDEBUG
|
---|
232 | BondNodeMap::iterator iter = BondsinSceneMap.find(ids);
|
---|
233 | ASSERT(iter == BondsinSceneMap.end(),
|
---|
234 | "GLWorldScene::bondAdded() - same left-sided bond "+toString(*_bond)+" added again.");
|
---|
235 | #endif
|
---|
236 | GLMoleculeObject_bond *bondObject =
|
---|
237 | new GLMoleculeObject_bond(this, _bond, side);
|
---|
238 | connect (
|
---|
239 | bondObject, SIGNAL(BondRemoved(const atomId_t, const atomId_t)),
|
---|
240 | this, SLOT(bondRemoved(const atomId_t, const atomId_t)));
|
---|
241 | connect (bondObject, SIGNAL(changed()), this, SIGNAL(changed()));
|
---|
242 | BondsinSceneMap.insert( make_pair(ids, bondObject) );
|
---|
243 | // BondIdsinSceneMap.insert( Leftids );
|
---|
244 | emit changeOccured();
|
---|
245 | }
|
---|
246 |
|
---|
247 | /** Removes a bond from the scene.
|
---|
248 | *
|
---|
249 | * @param _bond bond to remove
|
---|
250 | */
|
---|
251 | void GLWorldScene::bondRemoved(const atomId_t leftnr, const atomId_t rightnr)
|
---|
252 | {
|
---|
253 | LOG(3, "INFO: GLWorldScene::bondRemoved() - Removing bond between "+toString(leftnr)+" and "+toString(rightnr)+".");
|
---|
254 | {
|
---|
255 | // left bond
|
---|
256 | const BondIds Leftids( make_pair(leftnr, rightnr) );
|
---|
257 | BondNodeMap::iterator leftiter = BondsinSceneMap.find( Leftids );
|
---|
258 | ASSERT(leftiter != BondsinSceneMap.end(),
|
---|
259 | "GLWorldScene::bondRemoved() - bond "+toString(leftnr)+"-"
|
---|
260 | +toString(rightnr)+" not on display.");
|
---|
261 | //GLMoleculeObject_bond *bondObject = leftiter->second;
|
---|
262 | BondsinSceneMap.erase(leftiter);
|
---|
263 | //delete bondObject; // is done by signal from bond itself
|
---|
264 | //LOG(4, "INFO: Still present bonds " << BondsinSceneMap << ".");
|
---|
265 | }
|
---|
266 |
|
---|
267 | emit changeOccured();
|
---|
268 | }
|
---|
269 |
|
---|
270 | void GLWorldScene::initialize(QGLView *view, QGLPainter *painter) const
|
---|
271 | {
|
---|
272 | // Initialize all of the mesh objects that we have as children.
|
---|
273 | foreach (QObject *obj, children()) {
|
---|
274 | GLMoleculeObject *meshobj = qobject_cast<GLMoleculeObject *>(obj);
|
---|
275 | if (meshobj)
|
---|
276 | meshobj->initialize(view, painter);
|
---|
277 | }
|
---|
278 | }
|
---|
279 |
|
---|
280 | void GLWorldScene::drawDomainBox(QGLPainter *painter) const
|
---|
281 | {
|
---|
282 | RealSpaceMatrix m = World::getInstance().getDomain().getM();
|
---|
283 | painter->modelViewMatrix().push();
|
---|
284 | painter->modelViewMatrix() *= QMatrix4x4(m.at(0,0), m.at(0,1), m.at(0,2), 0.0,
|
---|
285 | m.at(1,0), m.at(1,1), m.at(1,2), 0.0,
|
---|
286 | m.at(2,0), m.at(2,1), m.at(2,2), 0.0,
|
---|
287 | 0.0, 0.0, 0.0, 1.0);
|
---|
288 |
|
---|
289 | painter->setFaceMaterial(QGL::AllFaces, domainBoxMaterial);
|
---|
290 | //glEnable(GL_LINE_SMOOTH);
|
---|
291 | QVector3DArray array;
|
---|
292 | array.append(0, 0, 0); array.append(1, 0, 0);
|
---|
293 | array.append(1, 0, 0); array.append(1, 1, 0);
|
---|
294 | array.append(1, 1, 0); array.append(0, 1, 0);
|
---|
295 | array.append(0, 1, 0); array.append(0, 0, 0);
|
---|
296 |
|
---|
297 | array.append(0, 0, 1); array.append(1, 0, 1);
|
---|
298 | array.append(1, 0, 1); array.append(1, 1, 1);
|
---|
299 | array.append(1, 1, 1); array.append(0, 1, 1);
|
---|
300 | array.append(0, 1, 1); array.append(0, 0, 1);
|
---|
301 |
|
---|
302 | array.append(0, 0, 0); array.append(0, 0, 1);
|
---|
303 | array.append(1, 0, 0); array.append(1, 0, 1);
|
---|
304 | array.append(0, 1, 0); array.append(0, 1, 1);
|
---|
305 | array.append(1, 1, 0); array.append(1, 1, 1);
|
---|
306 | painter->clearAttributes();
|
---|
307 | painter->setVertexAttribute(QGL::Position, array);
|
---|
308 | painter->draw(QGL::Lines, 24);
|
---|
309 | painter->modelViewMatrix().pop();
|
---|
310 | }
|
---|
311 |
|
---|
312 | void GLWorldScene::drawDreiBein(QGLPainter *painter) const
|
---|
313 | {
|
---|
314 | QGLView *view = dynamic_cast<QGLView*>(this->parent());
|
---|
315 | painter->modelViewMatrix().push();
|
---|
316 | painter->modelViewMatrix().translate(view->camera()->center());
|
---|
317 | painter->setFaceMaterial(QGL::AllFaces, dreiBeinMaterial);
|
---|
318 | QVector3DArray array;
|
---|
319 | array.append(0, 0, 0); array.append(1, 0, 0);
|
---|
320 | array.append(0, 0, 0); array.append(0, 1, 0);
|
---|
321 | array.append(0, 0, 0); array.append(0, 0, 1);
|
---|
322 | painter->clearAttributes();
|
---|
323 | painter->setVertexAttribute(QGL::Position, array);
|
---|
324 | painter->draw(QGL::Lines, 6);
|
---|
325 | painter->modelViewMatrix().pop();
|
---|
326 | }
|
---|
327 |
|
---|
328 | void GLWorldScene::draw(QGLPainter *painter) const
|
---|
329 | {
|
---|
330 | // Draw all of the mesh objects that we have as children.
|
---|
331 | foreach (QObject *obj, children()) {
|
---|
332 | GLMoleculeObject *meshobj = qobject_cast<GLMoleculeObject *>(obj);
|
---|
333 | if (meshobj)
|
---|
334 | meshobj->draw(painter);
|
---|
335 | }
|
---|
336 |
|
---|
337 | drawDomainBox(painter);
|
---|
338 | drawDreiBein(painter);
|
---|
339 | }
|
---|
340 |
|
---|
341 | void GLWorldScene::atomClicked(atomId_t no)
|
---|
342 | {
|
---|
343 | LOG(3, "INFO: GLWorldScene - atom " << no << " has been clicked.");
|
---|
344 | const atom *Walker = World::getInstance().getAtom(AtomById(no));
|
---|
345 | if (!World::getInstance().isSelected(Walker))
|
---|
346 | SelectionAtomById(no);
|
---|
347 | else
|
---|
348 | SelectionNotAtomById(no);
|
---|
349 | emit clicked(no);
|
---|
350 | }
|
---|
351 |
|
---|