1 | /*
|
---|
2 | * Project: MoleCuilder
|
---|
3 | * Description: creates and alters molecular systems
|
---|
4 | * Copyright (C) 2010-2012 University of Bonn. All rights reserved.
|
---|
5 | * Copyright (C) 2013 Frederik Heber. All rights reserved.
|
---|
6 | *
|
---|
7 | *
|
---|
8 | * This file is part of MoleCuilder.
|
---|
9 | *
|
---|
10 | * MoleCuilder is free software: you can redistribute it and/or modify
|
---|
11 | * it under the terms of the GNU General Public License as published by
|
---|
12 | * the Free Software Foundation, either version 2 of the License, or
|
---|
13 | * (at your option) any later version.
|
---|
14 | *
|
---|
15 | * MoleCuilder is distributed in the hope that it will be useful,
|
---|
16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
18 | * GNU General Public License for more details.
|
---|
19 | *
|
---|
20 | * You should have received a copy of the GNU General Public License
|
---|
21 | * along with MoleCuilder. If not, see <http://www.gnu.org/licenses/>.
|
---|
22 | */
|
---|
23 |
|
---|
24 | /*
|
---|
25 | * GLWorldScene.cpp
|
---|
26 | *
|
---|
27 | * This is based on the Qt3D example "teaservice", specifically parts of teaservice.cpp.
|
---|
28 | *
|
---|
29 | * Created on: Aug 17, 2011
|
---|
30 | * Author: heber
|
---|
31 | */
|
---|
32 |
|
---|
33 | // include config.h
|
---|
34 | #ifdef HAVE_CONFIG_H
|
---|
35 | #include <config.h>
|
---|
36 | #endif
|
---|
37 |
|
---|
38 | #include "GLWorldScene.hpp"
|
---|
39 | #include <Qt3D/qglview.h>
|
---|
40 | #include <Qt3D/qglbuilder.h>
|
---|
41 | #include <Qt3D/qglscenenode.h>
|
---|
42 | #include <Qt3D/qglsphere.h>
|
---|
43 | #include <Qt3D/qglcylinder.h>
|
---|
44 |
|
---|
45 | #include "GLMoleculeObject.hpp"
|
---|
46 | #include "GLMoleculeObject_atom.hpp"
|
---|
47 | #include "GLMoleculeObject_bond.hpp"
|
---|
48 | #include "GLMoleculeObject_molecule.hpp"
|
---|
49 | #include "GLMoleculeObject_shape.hpp"
|
---|
50 |
|
---|
51 | #include "CodePatterns/MemDebug.hpp"
|
---|
52 |
|
---|
53 | #include "CodePatterns/Log.hpp"
|
---|
54 |
|
---|
55 | #include "Actions/SelectionAction/Atoms/AtomByIdAction.hpp"
|
---|
56 | #include "Actions/SelectionAction/Atoms/NotAtomByIdAction.hpp"
|
---|
57 | #include "Actions/SelectionAction/Molecules/MoleculeByIdAction.hpp"
|
---|
58 | #include "Actions/SelectionAction/Molecules/NotMoleculeByIdAction.hpp"
|
---|
59 | #include "Atom/atom.hpp"
|
---|
60 | #include "Bond/bond.hpp"
|
---|
61 | #include "Descriptors/AtomIdDescriptor.hpp"
|
---|
62 | #include "Descriptors/MoleculeIdDescriptor.hpp"
|
---|
63 | #include "Helpers/helpers.hpp"
|
---|
64 | #include "Shapes/ShapeRegistry.hpp"
|
---|
65 | #include "molecule.hpp"
|
---|
66 | #include "World.hpp"
|
---|
67 |
|
---|
68 | #include <iostream>
|
---|
69 |
|
---|
70 | using namespace MoleCuilder;
|
---|
71 |
|
---|
72 | GLWorldScene::GLWorldScene(QObject *parent) :
|
---|
73 | QObject(parent),
|
---|
74 | selectionMode(SelectAtom)
|
---|
75 | {
|
---|
76 | int sphereDetails[] = {5, 3, 2, 0};
|
---|
77 | int cylinderDetails[] = {16, 8, 6, 3};
|
---|
78 | for (int i=0;i<GLMoleculeObject::DETAILTYPES_MAX;i++){
|
---|
79 | QGLBuilder emptyBuilder;
|
---|
80 | GLMoleculeObject::meshEmpty[i] = emptyBuilder.finalizedSceneNode();
|
---|
81 | QGLBuilder sphereBuilder;
|
---|
82 | sphereBuilder << QGLSphere(2.0, sphereDetails[i]);
|
---|
83 | GLMoleculeObject::meshSphere[i] = sphereBuilder.finalizedSceneNode();
|
---|
84 | GLMoleculeObject::meshSphere[i]->setOption(QGLSceneNode::CullBoundingBox, true);
|
---|
85 | QGLBuilder cylinderBuilder;
|
---|
86 | cylinderBuilder << QGLCylinder(.25,.25,1.0,cylinderDetails[i]);
|
---|
87 | GLMoleculeObject::meshCylinder[i] = cylinderBuilder.finalizedSceneNode();
|
---|
88 | GLMoleculeObject::meshCylinder[i]->setOption(QGLSceneNode::CullBoundingBox, true);
|
---|
89 | }
|
---|
90 |
|
---|
91 | connect(this, SIGNAL(updated()), this, SLOT(update()));
|
---|
92 | }
|
---|
93 |
|
---|
94 | GLWorldScene::~GLWorldScene()
|
---|
95 | {
|
---|
96 | // remove all elements
|
---|
97 | GLMoleculeObject::cleanMaterialMap();
|
---|
98 | }
|
---|
99 |
|
---|
100 | /** Update the WorldScene with molecules and atoms from World.
|
---|
101 | *
|
---|
102 | * This function should be called after e.g. WorldTime::TimeChanged was
|
---|
103 | * received or after another molecule has been loaded.
|
---|
104 | *
|
---|
105 | */
|
---|
106 | void GLWorldScene::update()
|
---|
107 | {
|
---|
108 | const std::vector<const molecule *> &molecules =
|
---|
109 | const_cast<const World &>(World::getInstance()).getAllMolecules();
|
---|
110 |
|
---|
111 | for (std::vector<const molecule*>::const_iterator moliter = molecules.begin();
|
---|
112 | moliter != molecules.end();
|
---|
113 | moliter++) {
|
---|
114 | // check whether molecule already exists
|
---|
115 | const moleculeId_t molid = (*moliter)->getId();
|
---|
116 | const bool mol_present = MoleculesinSceneMap.count(molid);
|
---|
117 | if (!mol_present)
|
---|
118 | moleculeInserted((*moliter)->getId());
|
---|
119 | }
|
---|
120 |
|
---|
121 | MoleculeNodeMap::iterator iter = MoleculesinSceneMap.begin();
|
---|
122 | for (;iter != MoleculesinSceneMap.end();) {
|
---|
123 | const moleculeId_t molid = iter->first;
|
---|
124 | const molecule * const mol = const_cast<const World &>(World::getInstance()).
|
---|
125 | getMolecule(MoleculeById(molid));
|
---|
126 | const bool mol_absent = (mol == NULL);
|
---|
127 | // step on to next molecule before possibly removing entry and invalidating iter
|
---|
128 | ++iter;
|
---|
129 | if (mol_absent)
|
---|
130 | moleculeRemoved(molid);
|
---|
131 | }
|
---|
132 |
|
---|
133 | }
|
---|
134 |
|
---|
135 | void GLWorldScene::atomClicked(atomId_t no)
|
---|
136 | {
|
---|
137 | LOG(3, "INFO: GLMoleculeObject_molecule - atom " << no << " has been clicked.");
|
---|
138 | const atom * const Walker = const_cast<const World &>(World::getInstance()).
|
---|
139 | getAtom(AtomById(no));
|
---|
140 | if (selectionMode == SelectAtom){
|
---|
141 | if (!World::getInstance().isSelected(Walker))
|
---|
142 | SelectionAtomById(std::vector<atomId_t>(1,no));
|
---|
143 | else
|
---|
144 | SelectionNotAtomById(std::vector<atomId_t>(1,no));
|
---|
145 | }else if (selectionMode == SelectMolecule){
|
---|
146 | const molecule *mol = Walker->getMolecule();
|
---|
147 | ASSERT(mol, "Atom without molecule has been clicked.");
|
---|
148 | molids_t ids(1, mol->getId());
|
---|
149 | if (!World::getInstance().isSelected(mol))
|
---|
150 | SelectionMoleculeById(ids);
|
---|
151 | else
|
---|
152 | SelectionNotMoleculeById(ids);
|
---|
153 | }
|
---|
154 | emit clicked(no);
|
---|
155 | }
|
---|
156 |
|
---|
157 | void GLWorldScene::moleculeClicked(moleculeId_t no)
|
---|
158 | {
|
---|
159 | LOG(3, "INFO: GLMoleculeObject_molecule - mol " << no << " has been clicked.");
|
---|
160 | const molecule * const mol= const_cast<const World &>(World::getInstance()).
|
---|
161 | getMolecule(MoleculeById(no));
|
---|
162 | ASSERT(mol, "Atom without molecule has been clicked.");
|
---|
163 | molids_t ids(1, mol->getId());
|
---|
164 | if (!World::getInstance().isSelected(mol))
|
---|
165 | SelectionMoleculeById(ids);
|
---|
166 | else
|
---|
167 | SelectionNotMoleculeById(ids);
|
---|
168 | emit clicked(no);
|
---|
169 | }
|
---|
170 |
|
---|
171 | /** Inserts an atom into the scene before molecule is present.
|
---|
172 | *
|
---|
173 | * @param _molid molecule to insert atom for
|
---|
174 | * @param _atomid atom to insert
|
---|
175 | */
|
---|
176 | void GLWorldScene::atomInserted(const moleculeId_t _molid, const atomId_t _atomid)
|
---|
177 | {
|
---|
178 | LOG(3, "INFO: GLWorldScene: Received signal atomInserted for atom "+toString(_atomid)+".");
|
---|
179 |
|
---|
180 | boost::recursive_mutex::scoped_lock lock(MoleculeMissedStateMap_mutex);
|
---|
181 |
|
---|
182 | // check of molecule is already present
|
---|
183 | if (MoleculesinSceneMap.count(_molid) != 0) {
|
---|
184 | // pass signal through
|
---|
185 | } else {
|
---|
186 | // store signal for when it is instantiated
|
---|
187 | if (MoleculeMissedStateMap.count(_molid) == 0)
|
---|
188 | MoleculeMissedStateMap.insert( std::make_pair(_molid ,StateChangeMap_t()) );
|
---|
189 | MoleculeMissedStateMap[_molid].insert( std::make_pair(_atomid, atomInsertedState) );
|
---|
190 | }
|
---|
191 | }
|
---|
192 |
|
---|
193 | /** Removes an atom into the scene before molecule is present.
|
---|
194 | *
|
---|
195 | * @param _molid molecule to insert atom for
|
---|
196 | * @param _atomid atom to insert
|
---|
197 | */
|
---|
198 | void GLWorldScene::atomRemoved(const moleculeId_t _molid, const atomId_t _atomid)
|
---|
199 | {
|
---|
200 | LOG(3, "INFO: GLWorldScene: Received signal atomRemoved for atom "+toString(_atomid)+".");
|
---|
201 |
|
---|
202 | boost::recursive_mutex::scoped_lock lock(MoleculeMissedStateMap_mutex);
|
---|
203 |
|
---|
204 | // check of molecule is already present
|
---|
205 | if (MoleculesinSceneMap.count(_molid) != 0) {
|
---|
206 | // pass signal through
|
---|
207 | } else {
|
---|
208 | // store signal for when it is instantiated
|
---|
209 | if (MoleculeMissedStateMap.count(_molid) == 0)
|
---|
210 | MoleculeMissedStateMap.insert( std::make_pair(_molid ,StateChangeMap_t()) );
|
---|
211 | MoleculeMissedStateMap[_molid].insert( std::make_pair(_atomid, atomRemovedState) );
|
---|
212 | }
|
---|
213 | }
|
---|
214 |
|
---|
215 | /** Inserts a molecule into the scene.
|
---|
216 | *
|
---|
217 | * @param _mol molecule to insert
|
---|
218 | */
|
---|
219 | void GLWorldScene::moleculeInserted(const moleculeId_t _id)
|
---|
220 | {
|
---|
221 | LOG(3, "INFO: GLWorldScene: Received signal moleculeInserted for molecule "+toString(_id)+".");
|
---|
222 | MoleculeNodeMap::const_iterator iter = MoleculesinSceneMap.find(_id);
|
---|
223 | ASSERT( iter == MoleculesinSceneMap.end(),
|
---|
224 | "GLWorldScene::moleculeInserted() - molecule's id "+toString(_id)+" already present.");
|
---|
225 |
|
---|
226 | // add new object
|
---|
227 | GLMoleculeObject_molecule *molObject =
|
---|
228 | new GLMoleculeObject_molecule(GLMoleculeObject::meshEmpty, this, _id);
|
---|
229 | ASSERT( molObject != NULL,
|
---|
230 | "GLWorldScene::moleculeInserted - could not create molecule object for "+toString(_id));
|
---|
231 | MoleculesinSceneMap.insert( make_pair(_id, molObject) );
|
---|
232 |
|
---|
233 | // now handle all state changes that came up before the instantiation
|
---|
234 | while (MoleculeMissedStateMap.count(_id) != 0) {
|
---|
235 | ASSERT( !MoleculeMissedStateMap[_id].empty(),
|
---|
236 | "GLWorldScene::moleculeInserted() - we have an empty state change map for molecule with id "
|
---|
237 | +toString(_id));
|
---|
238 | boost::recursive_mutex::scoped_lock lock(MoleculeMissedStateMap_mutex);
|
---|
239 | for (StateChangeMap_t::iterator iter = MoleculeMissedStateMap[_id].begin();
|
---|
240 | !MoleculeMissedStateMap[_id].empty();
|
---|
241 | iter = MoleculeMissedStateMap[_id].begin()) {
|
---|
242 | std::pair<StateChangeMap_t::iterator, StateChangeMap_t::iterator> rangeiter =
|
---|
243 | MoleculeMissedStateMap[_id].equal_range(iter->first);
|
---|
244 | const size_t StateCounts = std::distance(rangeiter.first, rangeiter.second);
|
---|
245 | if (StateCounts > 1) {
|
---|
246 | // more than one state change, have to combine
|
---|
247 | typedef std::map<StateChangeType, size_t> StateChangeAmounts_t;
|
---|
248 | StateChangeAmounts_t StateChangeAmounts;
|
---|
249 | for (StateChangeMap_t::const_iterator stateiter = rangeiter.first;
|
---|
250 | stateiter != rangeiter.second; ++stateiter)
|
---|
251 | ++StateChangeAmounts[stateiter->second];
|
---|
252 | ASSERT( StateChangeAmounts[atomInsertedState] >= StateChangeAmounts[atomRemovedState],
|
---|
253 | "GLWorldScene::moleculeInserted() - more atomRemoved states than atomInserted for atom "
|
---|
254 | +toString(iter->first));
|
---|
255 | if (StateChangeAmounts[atomInsertedState] > StateChangeAmounts[atomRemovedState]) {
|
---|
256 | LOG(1, "INFO: invoking atomInserted for atom " << iter->first);
|
---|
257 | QMetaObject::invokeMethod(molObject, // pointer to a QObject
|
---|
258 | "atomInserted", // member name (no parameters here)
|
---|
259 | Qt::DirectConnection, // connection type
|
---|
260 | Q_ARG(const atomId_t, iter->first)); // parameters
|
---|
261 | } else {
|
---|
262 | LOG(1, "INFO: Atom " << iter->first << " has been inserted and removed already.");
|
---|
263 | }
|
---|
264 | } else {
|
---|
265 | // can only be an insertion
|
---|
266 | switch (rangeiter.first->second) {
|
---|
267 | case atomRemovedState:
|
---|
268 | ASSERT( 0,
|
---|
269 | "GLWorldScene::moleculeInserted() - atomRemoved state without atomInserted for atom "
|
---|
270 | +toString(iter->first));
|
---|
271 | break;
|
---|
272 | case atomInsertedState:
|
---|
273 | LOG(1, "INFO: invoking atomInserted for atom " << iter->first);
|
---|
274 | QMetaObject::invokeMethod(molObject, // pointer to a QObject
|
---|
275 | "atomInserted", // member name (no parameters here)
|
---|
276 | Qt::DirectConnection, // connection type
|
---|
277 | Q_ARG(const atomId_t, iter->first)); // parameters
|
---|
278 | break;
|
---|
279 | default:
|
---|
280 | ASSERT( 0,
|
---|
281 | "GLWorldScene::moleculeInserted() - there are unknown change states.");
|
---|
282 | break;
|
---|
283 | }
|
---|
284 | }
|
---|
285 | // removed state changes for this atom
|
---|
286 | MoleculeMissedStateMap[_id].erase(iter);
|
---|
287 | }
|
---|
288 | // remove state change map for the molecule
|
---|
289 | MoleculeMissedStateMap.erase(_id);
|
---|
290 | }
|
---|
291 |
|
---|
292 | // now let the molObject sign on to molecule
|
---|
293 | molObject->activateObserver();
|
---|
294 |
|
---|
295 | connect (molObject, SIGNAL(changed()), this, SIGNAL(changed()));
|
---|
296 | connect (molObject, SIGNAL(changeOccured()), this, SIGNAL(changeOccured()));
|
---|
297 | connect (molObject, SIGNAL(atomClicked(atomId_t)), this, SLOT(atomClicked(atomId_t)));
|
---|
298 | connect (molObject, SIGNAL(moleculeClicked(moleculeId_t)), this, SLOT(moleculeClicked(moleculeId_t)));
|
---|
299 | connect (molObject, SIGNAL(selectionChanged()), this, SIGNAL(changed()));
|
---|
300 | connect (molObject, SIGNAL(selectionChanged()), this, SIGNAL(changed()));
|
---|
301 | connect (molObject, SIGNAL(hoverChanged(const atomId_t)), this, SIGNAL(hoverChanged(const atomId_t)));
|
---|
302 | connect (molObject, SIGNAL(hoverChanged(const moleculeId_t, int)), this, SIGNAL(hoverChanged(const moleculeId_t, int)));
|
---|
303 |
|
---|
304 | emit changed();
|
---|
305 | emit changeOccured();
|
---|
306 | }
|
---|
307 |
|
---|
308 | /** Removes a molecule from the scene.
|
---|
309 | *
|
---|
310 | * @param _id id of molecule to remove
|
---|
311 | */
|
---|
312 | void GLWorldScene::moleculeRemoved(const moleculeId_t _id)
|
---|
313 | {
|
---|
314 | LOG(3, "INFO: GLWorldScene: Received signal moleculeRemoved for molecule "+toString(_id)+".");
|
---|
315 | MoleculeNodeMap::iterator iter = MoleculesinSceneMap.find(_id);
|
---|
316 | ASSERT( iter != MoleculesinSceneMap.end(),
|
---|
317 | "GLWorldScene::moleculeInserted() - molecule's id "+toString(_id)+" is unknown.");
|
---|
318 |
|
---|
319 | GLMoleculeObject_molecule *molObject = iter->second;
|
---|
320 | molObject->disconnect();
|
---|
321 | MoleculesinSceneMap.erase(iter);
|
---|
322 | delete molObject;
|
---|
323 |
|
---|
324 | // remove any possible state changes left
|
---|
325 | {
|
---|
326 | boost::recursive_mutex::scoped_lock lock(MoleculeMissedStateMap_mutex);
|
---|
327 | MoleculeMissedStateMap.erase(_id);
|
---|
328 | }
|
---|
329 |
|
---|
330 | emit changed();
|
---|
331 | emit changeOccured();
|
---|
332 | }
|
---|
333 |
|
---|
334 | void GLWorldScene::moleculesVisibilityChanged(const moleculeId_t _id, bool _visible)
|
---|
335 | {
|
---|
336 | MoleculeNodeMap::iterator iter = MoleculesinSceneMap.find(_id);
|
---|
337 | ASSERT( iter != MoleculesinSceneMap.end(),
|
---|
338 | "GLWorldScene::moleculeInserted() - molecule's id "+toString(_id)+" is unknown.");
|
---|
339 |
|
---|
340 | GLMoleculeObject_molecule *molObject = iter->second;
|
---|
341 | molObject->setVisible(_visible);
|
---|
342 |
|
---|
343 | emit changed();
|
---|
344 | emit changeOccured();
|
---|
345 | }
|
---|
346 |
|
---|
347 | /** Adds a shape to the scene.
|
---|
348 | *
|
---|
349 | * uses ShapeRegistry::lastChanged()
|
---|
350 | *
|
---|
351 | */
|
---|
352 | void GLWorldScene::addShape()
|
---|
353 | {
|
---|
354 | Shape &shape = *ShapeRegistry::getInstance().lastChanged();
|
---|
355 | GLMoleculeObject_shape *shapeObject = new GLMoleculeObject_shape(shape, this);
|
---|
356 | ShapeNodeMap::iterator iter = ShapesinSceneMap.find(shape.getName());
|
---|
357 | ASSERT(iter == ShapesinSceneMap.end(),
|
---|
358 | "GLWorldScene::addShape() - same shape "+shape.getName()+" added again.");
|
---|
359 | ShapesinSceneMap.insert( make_pair(shape.getName(), shapeObject) );
|
---|
360 | }
|
---|
361 |
|
---|
362 | void GLWorldScene::removeShape()
|
---|
363 | {
|
---|
364 | Shape &shape = *ShapeRegistry::getInstance().lastChanged();
|
---|
365 | ShapeNodeMap::iterator iter = ShapesinSceneMap.find(shape.getName());
|
---|
366 | ASSERT(iter != ShapesinSceneMap.end(),
|
---|
367 | "GLWorldScene::removeShape() - shape "+shape.getName()+" not in scene.");
|
---|
368 | ShapesinSceneMap.erase(iter);
|
---|
369 | delete(iter->second);
|
---|
370 | }
|
---|
371 |
|
---|
372 | void GLWorldScene::updateSelectedShapes()
|
---|
373 | {
|
---|
374 | foreach (QObject *obj, children()) {
|
---|
375 | GLMoleculeObject_shape *shapeobj = qobject_cast<GLMoleculeObject_shape *>(obj);
|
---|
376 | if (shapeobj){
|
---|
377 | shapeobj->enable(ShapeRegistry::getInstance().isSelected(shapeobj->getShape()));
|
---|
378 | }
|
---|
379 | }
|
---|
380 | }
|
---|
381 |
|
---|
382 | void GLWorldScene::initialize(QGLView *view, QGLPainter *painter) const
|
---|
383 | {
|
---|
384 | // Initialize all of the mesh objects that we have as children.
|
---|
385 | foreach (QObject *obj, children()) {
|
---|
386 | GLMoleculeObject *meshobj = qobject_cast<GLMoleculeObject *>(obj);
|
---|
387 | if (meshobj)
|
---|
388 | meshobj->initialize(view, painter);
|
---|
389 | }
|
---|
390 | }
|
---|
391 |
|
---|
392 | void GLWorldScene::draw(QGLPainter *painter, const QVector4D &cameraPlane) const
|
---|
393 | {
|
---|
394 | // Draw all of the mesh objects that we have as children.
|
---|
395 | foreach (QObject *obj, children()) {
|
---|
396 | GLMoleculeObject *meshobj = qobject_cast<GLMoleculeObject *>(obj);
|
---|
397 | if (meshobj)
|
---|
398 | meshobj->draw(painter, cameraPlane);
|
---|
399 | }
|
---|
400 | }
|
---|
401 |
|
---|
402 | void GLWorldScene::setSelectionMode(SelectionModeType mode)
|
---|
403 | {
|
---|
404 | selectionMode = mode;
|
---|
405 | // TODO send update to toolbar
|
---|
406 | }
|
---|
407 |
|
---|
408 | void GLWorldScene::setSelectionModeAtom()
|
---|
409 | {
|
---|
410 | setSelectionMode(SelectAtom);
|
---|
411 | }
|
---|
412 |
|
---|
413 | void GLWorldScene::setSelectionModeMolecule()
|
---|
414 | {
|
---|
415 | setSelectionMode(SelectMolecule);
|
---|
416 | }
|
---|