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 "UIElements/Views/Qt4/Qt3D/GLMoleculeObject.hpp"
|
---|
46 | #include "UIElements/Views/Qt4/Qt3D/GLMoleculeObject_atom.hpp"
|
---|
47 | #include "UIElements/Views/Qt4/Qt3D/GLMoleculeObject_bond.hpp"
|
---|
48 | #include "UIElements/Views/Qt4/Qt3D/GLMoleculeObject_molecule.hpp"
|
---|
49 | #include "UIElements/Views/Qt4/Qt3D/GLMoleculeObject_shape.hpp"
|
---|
50 |
|
---|
51 | #include "UIElements/Views/Qt4/QtSelectionChangedAgent.hpp"
|
---|
52 |
|
---|
53 | #include "CodePatterns/MemDebug.hpp"
|
---|
54 |
|
---|
55 | #include "CodePatterns/Log.hpp"
|
---|
56 |
|
---|
57 | #include "Actions/SelectionAction/Atoms/AtomByIdAction.hpp"
|
---|
58 | #include "Actions/SelectionAction/Atoms/NotAtomByIdAction.hpp"
|
---|
59 | #include "Actions/SelectionAction/Molecules/MoleculeByIdAction.hpp"
|
---|
60 | #include "Actions/SelectionAction/Molecules/NotMoleculeByIdAction.hpp"
|
---|
61 | #include "Atom/atom.hpp"
|
---|
62 | #include "Bond/bond.hpp"
|
---|
63 | #include "Descriptors/AtomIdDescriptor.hpp"
|
---|
64 | #include "Descriptors/MoleculeIdDescriptor.hpp"
|
---|
65 | #include "Helpers/helpers.hpp"
|
---|
66 | #include "Shapes/ShapeRegistry.hpp"
|
---|
67 | #include "molecule.hpp"
|
---|
68 | #include "World.hpp"
|
---|
69 |
|
---|
70 | #include <iostream>
|
---|
71 |
|
---|
72 | using namespace MoleCuilder;
|
---|
73 |
|
---|
74 | GLWorldScene::GLWorldScene(QObject *parent) :
|
---|
75 | QObject(parent),
|
---|
76 | selectionMode(SelectAtom)
|
---|
77 | {
|
---|
78 | int sphereDetails[] = {5, 3, 2, 0};
|
---|
79 | int cylinderDetails[] = {16, 8, 6, 3};
|
---|
80 | for (int i=0;i<GLMoleculeObject::DETAILTYPES_MAX;i++){
|
---|
81 | QGLBuilder emptyBuilder;
|
---|
82 | GLMoleculeObject::meshEmpty[i] = emptyBuilder.finalizedSceneNode();
|
---|
83 | QGLBuilder sphereBuilder;
|
---|
84 | sphereBuilder << QGLSphere(2.0, sphereDetails[i]);
|
---|
85 | GLMoleculeObject::meshSphere[i] = sphereBuilder.finalizedSceneNode();
|
---|
86 | GLMoleculeObject::meshSphere[i]->setOption(QGLSceneNode::CullBoundingBox, true);
|
---|
87 | QGLBuilder cylinderBuilder;
|
---|
88 | cylinderBuilder << QGLCylinder(.25,.25,1.0,cylinderDetails[i]);
|
---|
89 | GLMoleculeObject::meshCylinder[i] = cylinderBuilder.finalizedSceneNode();
|
---|
90 | GLMoleculeObject::meshCylinder[i]->setOption(QGLSceneNode::CullBoundingBox, true);
|
---|
91 | }
|
---|
92 | connect(this, SIGNAL(moleculePreparedInserted(const moleculeId_t)), this, SLOT(moleculeInserted(const moleculeId_t)));
|
---|
93 | connect(this, SIGNAL(moleculePreparedRemoved(const moleculeId_t)), this, SLOT(moleculeRemoved(const moleculeId_t)));
|
---|
94 |
|
---|
95 | // connect(this, SIGNAL(updated()), this, SLOT(update()));
|
---|
96 | }
|
---|
97 |
|
---|
98 | GLWorldScene::~GLWorldScene()
|
---|
99 | {
|
---|
100 | // remove all elements
|
---|
101 | GLMoleculeObject::cleanMaterialMap();
|
---|
102 | }
|
---|
103 |
|
---|
104 | ///** Update the WorldScene with molecules and atomsfrom World.
|
---|
105 | // *
|
---|
106 | // * This function should be called after e.g. WorldTime::TimeChanged was
|
---|
107 | // * received or after another molecule has been loaded.
|
---|
108 | // *
|
---|
109 | // */
|
---|
110 | //void GLWorldScene::update()
|
---|
111 | //{
|
---|
112 | // const std::vector<const molecule *> &molecules =
|
---|
113 | // const_cast<const World &>(World::getInstance()).getAllMolecules();
|
---|
114 | //
|
---|
115 | // for (std::vector<const molecule*>::const_iterator moliter = molecules.begin();
|
---|
116 | // moliter != molecules.end();
|
---|
117 | // moliter++) {
|
---|
118 | // // check whether molecule already exists
|
---|
119 | // const moleculeId_t molid = (*moliter)->getId();
|
---|
120 | // const bool mol_present = MoleculesinSceneMap.count(molid);
|
---|
121 | // if (!mol_present)
|
---|
122 | // moleculeInserted((*moliter)->getId());
|
---|
123 | // }
|
---|
124 | //
|
---|
125 | // MoleculeNodeMap::iterator iter = MoleculesinSceneMap.begin();
|
---|
126 | // for (;iter != MoleculesinSceneMap.end();) {
|
---|
127 | // const moleculeId_t molid = iter->first;
|
---|
128 | // const molecule * const mol = const_cast<const World &>(World::getInstance()).
|
---|
129 | // getMolecule(MoleculeById(molid));
|
---|
130 | // const bool mol_absent = (mol == NULL);
|
---|
131 | // // step on to next molecule before possibly removing entry and invalidating iter
|
---|
132 | // ++iter;
|
---|
133 | // if (mol_absent)
|
---|
134 | // moleculeRemoved(molid);
|
---|
135 | // }
|
---|
136 | //
|
---|
137 | //}
|
---|
138 |
|
---|
139 | void GLWorldScene::atomClicked(atomId_t no)
|
---|
140 | {
|
---|
141 | LOG(3, "INFO: GLMoleculeObject_molecule - atom " << no << " has been clicked.");
|
---|
142 | const atom * const Walker = const_cast<const World &>(World::getInstance()).
|
---|
143 | getAtom(AtomById(no));
|
---|
144 | ASSERT( Walker != NULL,
|
---|
145 | "GLWorldScene::atomClicked() - clicked atom has disappeared.");
|
---|
146 | if (selectionMode == SelectAtom){
|
---|
147 | if (!World::getInstance().isSelected(Walker))
|
---|
148 | SelectionAtomById(std::vector<atomId_t>(1,no));
|
---|
149 | else
|
---|
150 | SelectionNotAtomById(std::vector<atomId_t>(1,no));
|
---|
151 | }else if (selectionMode == SelectMolecule){
|
---|
152 | const molecule *mol = Walker->getMolecule();
|
---|
153 | ASSERT(mol, "Atom without molecule has been clicked.");
|
---|
154 | molids_t ids(1, mol->getId());
|
---|
155 | if (!World::getInstance().isSelected(mol))
|
---|
156 | SelectionMoleculeById(ids);
|
---|
157 | else
|
---|
158 | SelectionNotMoleculeById(ids);
|
---|
159 | }
|
---|
160 | emit clicked(no);
|
---|
161 | }
|
---|
162 |
|
---|
163 | void GLWorldScene::moleculeClicked(moleculeId_t no)
|
---|
164 | {
|
---|
165 | LOG(3, "INFO: GLMoleculeObject_molecule - mol " << no << " has been clicked.");
|
---|
166 | const molecule * const mol= const_cast<const World &>(World::getInstance()).
|
---|
167 | getMolecule(MoleculeById(no));
|
---|
168 | ASSERT(mol, "Atom without molecule has been clicked.");
|
---|
169 | molids_t ids(1, mol->getId());
|
---|
170 | if (!World::getInstance().isSelected(mol))
|
---|
171 | SelectionMoleculeById(ids);
|
---|
172 | else
|
---|
173 | SelectionNotMoleculeById(ids);
|
---|
174 | emit clicked(no);
|
---|
175 | }
|
---|
176 |
|
---|
177 | /** Inserts an atom into the scene before molecule is present.
|
---|
178 | *
|
---|
179 | * @param _molid molecule to insert atom for
|
---|
180 | * @param _atomid atom to insert
|
---|
181 | */
|
---|
182 | void GLWorldScene::atomInserted(const moleculeId_t _molid, const atomId_t _atomid)
|
---|
183 | {
|
---|
184 | LOG(3, "INFO: GLWorldScene: Received signal atomInserted for atom "+toString(_atomid)+".");
|
---|
185 |
|
---|
186 | boost::recursive_mutex::scoped_lock lock(MoleculeMissedStateMap_mutex);
|
---|
187 |
|
---|
188 | // check of molecule is already present
|
---|
189 | if (MoleculesinSceneMap.count(_molid) != 0) {
|
---|
190 | // pass signal through
|
---|
191 | } else {
|
---|
192 | // store signal for when it is instantiated
|
---|
193 | if (MoleculeMissedStateMap.count(_molid) == 0)
|
---|
194 | MoleculeMissedStateMap.insert( std::make_pair(_molid ,StateChangeMap_t()) );
|
---|
195 | MoleculeMissedStateMap[_molid].insert( std::make_pair(_atomid, atomInsertedState) );
|
---|
196 | }
|
---|
197 | }
|
---|
198 |
|
---|
199 | /** Removes an atom into the scene before molecule is present.
|
---|
200 | *
|
---|
201 | * @param _molid molecule to insert atom for
|
---|
202 | * @param _atomid atom to insert
|
---|
203 | */
|
---|
204 | void GLWorldScene::atomRemoved(const moleculeId_t _molid, const atomId_t _atomid)
|
---|
205 | {
|
---|
206 | LOG(3, "INFO: GLWorldScene: Received signal atomRemoved for atom "+toString(_atomid)+".");
|
---|
207 |
|
---|
208 | boost::recursive_mutex::scoped_lock lock(MoleculeMissedStateMap_mutex);
|
---|
209 |
|
---|
210 | // check of molecule is already present
|
---|
211 | if (MoleculesinSceneMap.count(_molid) != 0) {
|
---|
212 | // pass signal through
|
---|
213 | } else {
|
---|
214 | // store signal for when it is instantiated
|
---|
215 | if (MoleculeMissedStateMap.count(_molid) == 0)
|
---|
216 | MoleculeMissedStateMap.insert( std::make_pair(_molid ,StateChangeMap_t()) );
|
---|
217 | MoleculeMissedStateMap[_molid].insert( std::make_pair(_atomid, atomRemovedState) );
|
---|
218 | }
|
---|
219 | }
|
---|
220 |
|
---|
221 | /** Prepres insertion of a molecule into the scene.
|
---|
222 | *
|
---|
223 | * This function takes up the insertion information, i.e. a molecule has been
|
---|
224 | * inserted and its id, and takes some immediate action (writing id into a map).
|
---|
225 | * But the request is then transmitted indirectly (i.e. not necessarily being
|
---|
226 | * processed in the same thread). Hence, we first take the request via a direction
|
---|
227 | * connection in the thread where the actual World::createMolecule() was called.
|
---|
228 | * However, the creation of the visual representation must occur in the thread
|
---|
229 | * who also contains GLWorldScene and GLWorldView and all further parents.
|
---|
230 | *
|
---|
231 | * That's the reason why we split here into moleculePrepareInserted and
|
---|
232 | * moleculeInserted.
|
---|
233 | *
|
---|
234 | * @param _mol molecule to insert
|
---|
235 | */
|
---|
236 | void GLWorldScene::moleculePrepareInserted(const moleculeId_t _id)
|
---|
237 | {
|
---|
238 | LOG(3, "INFO: GLWorldScene: Received signal moleculePrepareInserted for molecule "+toString(_id)+".");
|
---|
239 |
|
---|
240 | boost::recursive_mutex::scoped_lock lock(MoleculeinSceneMap_mutex);
|
---|
241 |
|
---|
242 | MoleculeNodeMap::const_iterator iter = MoleculesinSceneMap.find(_id);
|
---|
243 | ASSERT( iter == MoleculesinSceneMap.end(),
|
---|
244 | "GLWorldScene::moleculeInserted() - molecule's id "+toString(_id)+" already present.");
|
---|
245 |
|
---|
246 | emit moleculePreparedInserted(_id);
|
---|
247 | }
|
---|
248 |
|
---|
249 | /** Inserts a molecule into the scene.
|
---|
250 | *
|
---|
251 | * \sa moleculePrepareInserted()
|
---|
252 | *
|
---|
253 | * @param _mol molecule to insert
|
---|
254 | */
|
---|
255 | void GLWorldScene::moleculeInserted(const moleculeId_t _id)
|
---|
256 | {
|
---|
257 | boost::recursive_mutex::scoped_lock lock(MoleculeinSceneMap_mutex);
|
---|
258 |
|
---|
259 | // check whether molecule is still present
|
---|
260 | if (RemovalMolecules.count(_id) != 0) {
|
---|
261 | RemovalMolecules.erase(_id);
|
---|
262 | return;
|
---|
263 | }
|
---|
264 | if (const_cast<const World &>(World::getInstance()).getMolecule(MoleculeById(_id)) == NULL) {
|
---|
265 | ELOG(2, "Molecule with id " << _id << " has disappeared.");
|
---|
266 | return;
|
---|
267 | }
|
---|
268 |
|
---|
269 | // add new object
|
---|
270 | GLMoleculeObject_molecule *molObject =
|
---|
271 | new GLMoleculeObject_molecule(GLMoleculeObject::meshEmpty, this, _id);
|
---|
272 | ASSERT( molObject != NULL,
|
---|
273 | "GLWorldScene::moleculeInserted - could not create molecule object for "+toString(_id));
|
---|
274 | MoleculesinSceneMap.insert( make_pair(_id, molObject) );
|
---|
275 |
|
---|
276 | // now handle all state changes that came up before the instantiation
|
---|
277 | while (MoleculeMissedStateMap.count(_id) != 0) {
|
---|
278 | ASSERT( !MoleculeMissedStateMap[_id].empty(),
|
---|
279 | "GLWorldScene::moleculeInserted() - we have an empty state change map for molecule with id "
|
---|
280 | +toString(_id));
|
---|
281 | boost::recursive_mutex::scoped_lock lock(MoleculeMissedStateMap_mutex);
|
---|
282 | for (StateChangeMap_t::iterator iter = MoleculeMissedStateMap[_id].begin();
|
---|
283 | !MoleculeMissedStateMap[_id].empty();
|
---|
284 | iter = MoleculeMissedStateMap[_id].begin()) {
|
---|
285 | std::pair<StateChangeMap_t::iterator, StateChangeMap_t::iterator> rangeiter =
|
---|
286 | MoleculeMissedStateMap[_id].equal_range(iter->first);
|
---|
287 | const size_t StateCounts = std::distance(rangeiter.first, rangeiter.second);
|
---|
288 | if (StateCounts > 1) {
|
---|
289 | // more than one state change, have to combine
|
---|
290 | typedef std::map<StateChangeType, size_t> StateChangeAmounts_t;
|
---|
291 | StateChangeAmounts_t StateChangeAmounts;
|
---|
292 | for (StateChangeMap_t::const_iterator stateiter = rangeiter.first;
|
---|
293 | stateiter != rangeiter.second; ++stateiter)
|
---|
294 | ++StateChangeAmounts[stateiter->second];
|
---|
295 | ASSERT( StateChangeAmounts[atomInsertedState] >= StateChangeAmounts[atomRemovedState],
|
---|
296 | "GLWorldScene::moleculeInserted() - more atomRemoved states than atomInserted for atom "
|
---|
297 | +toString(iter->first));
|
---|
298 | if (StateChangeAmounts[atomInsertedState] > StateChangeAmounts[atomRemovedState]) {
|
---|
299 | LOG(1, "INFO: invoking atomInserted for atom " << iter->first);
|
---|
300 | QMetaObject::invokeMethod(molObject, // pointer to a QObject
|
---|
301 | "atomInserted", // member name (no parameters here)
|
---|
302 | Qt::DirectConnection, // connection type
|
---|
303 | Q_ARG(const atomId_t, iter->first)); // parameters
|
---|
304 | } else {
|
---|
305 | LOG(1, "INFO: Atom " << iter->first << " has been inserted and removed already.");
|
---|
306 | }
|
---|
307 | } else {
|
---|
308 | // can only be an insertion
|
---|
309 | switch (rangeiter.first->second) {
|
---|
310 | case atomRemovedState:
|
---|
311 | ASSERT( 0,
|
---|
312 | "GLWorldScene::moleculeInserted() - atomRemoved state without atomInserted for atom "
|
---|
313 | +toString(iter->first));
|
---|
314 | break;
|
---|
315 | case atomInsertedState:
|
---|
316 | LOG(1, "INFO: invoking atomInserted for atom " << iter->first);
|
---|
317 | QMetaObject::invokeMethod(molObject, // pointer to a QObject
|
---|
318 | "atomInserted", // member name (no parameters here)
|
---|
319 | Qt::DirectConnection, // connection type
|
---|
320 | Q_ARG(const atomId_t, iter->first)); // parameters
|
---|
321 | break;
|
---|
322 | default:
|
---|
323 | ASSERT( 0,
|
---|
324 | "GLWorldScene::moleculeInserted() - there are unknown change states.");
|
---|
325 | break;
|
---|
326 | }
|
---|
327 | }
|
---|
328 | // removed state changes for this atom
|
---|
329 | MoleculeMissedStateMap[_id].erase(iter);
|
---|
330 | }
|
---|
331 | // remove state change map for the molecule
|
---|
332 | MoleculeMissedStateMap.erase(_id);
|
---|
333 | }
|
---|
334 |
|
---|
335 | // now let the molObject sign on to molecule
|
---|
336 | molObject->activateObserver();
|
---|
337 |
|
---|
338 | connect (molObject, SIGNAL(changed()), this, SIGNAL(changed()));
|
---|
339 | connect (molObject, SIGNAL(changeOccured()), this, SIGNAL(changeOccured()));
|
---|
340 | connect (molObject, SIGNAL(atomClicked(atomId_t)), this, SLOT(atomClicked(atomId_t)));
|
---|
341 | connect (molObject, SIGNAL(moleculeClicked(moleculeId_t)), this, SLOT(moleculeClicked(moleculeId_t)));
|
---|
342 | connect (molObject, SIGNAL(selectionChanged()), this, SIGNAL(changed()));
|
---|
343 | connect (molObject, SIGNAL(selectionChanged()), this, SIGNAL(changed()));
|
---|
344 | connect (molObject, SIGNAL(hoverChanged(const atomId_t)), this, SIGNAL(hoverChanged(const atomId_t)));
|
---|
345 | connect (molObject, SIGNAL(hoverChanged(const moleculeId_t, int)), this, SIGNAL(hoverChanged(const moleculeId_t, int)));
|
---|
346 | connect (molObject, SIGNAL(indexChanged(GLMoleculeObject_molecule *, const moleculeId_t, const moleculeId_t)), this, SLOT(changeMoleculeId(GLMoleculeObject_molecule *, const moleculeId_t, const moleculeId_t)));
|
---|
347 |
|
---|
348 | emit changed();
|
---|
349 | emit changeOccured();
|
---|
350 | }
|
---|
351 |
|
---|
352 | /** Prepares removing a molecule from the scene.
|
---|
353 | *
|
---|
354 | * \sa moleculePrepareInserted()
|
---|
355 | *
|
---|
356 | * @param _id id of molecule to remove
|
---|
357 | */
|
---|
358 | void GLWorldScene::moleculePrepareRemoved(const moleculeId_t _id)
|
---|
359 | {
|
---|
360 | LOG(3, "INFO: GLWorldScene: Received signal moleculePrepareRemoved for molecule "+toString(_id)+".");
|
---|
361 |
|
---|
362 | boost::recursive_mutex::scoped_lock lock(MoleculeinSceneMap_mutex);
|
---|
363 |
|
---|
364 | MoleculeNodeMap::iterator iter = MoleculesinSceneMap.find(_id);
|
---|
365 | if ( iter == MoleculesinSceneMap.end())
|
---|
366 | RemovalMolecules.insert(_id);
|
---|
367 |
|
---|
368 | emit moleculePreparedRemoved(_id);
|
---|
369 | }
|
---|
370 |
|
---|
371 | /** Removes a molecule from the scene.
|
---|
372 | *
|
---|
373 | * * \sa moleculePrepareRemoved()
|
---|
374 | *
|
---|
375 | * @param _id id of molecule to remove
|
---|
376 | */
|
---|
377 | void GLWorldScene::moleculeRemoved(const moleculeId_t _id)
|
---|
378 | {
|
---|
379 | boost::recursive_mutex::scoped_lock lock(MoleculeinSceneMap_mutex);
|
---|
380 |
|
---|
381 | MoleculeNodeMap::iterator iter = MoleculesinSceneMap.find(_id);
|
---|
382 | GLMoleculeObject_molecule *molObject = iter->second;
|
---|
383 | molObject->disconnect();
|
---|
384 | MoleculesinSceneMap.erase(iter);
|
---|
385 | delete molObject;
|
---|
386 |
|
---|
387 | // remove any possible state changes left
|
---|
388 | {
|
---|
389 | boost::recursive_mutex::scoped_lock lock(MoleculeMissedStateMap_mutex);
|
---|
390 | MoleculeMissedStateMap.erase(_id);
|
---|
391 | }
|
---|
392 |
|
---|
393 | emit changed();
|
---|
394 | emit changeOccured();
|
---|
395 | }
|
---|
396 |
|
---|
397 | void GLWorldScene::moleculesVisibilityChanged(const moleculeId_t _id, bool _visible)
|
---|
398 | {
|
---|
399 | MoleculeNodeMap::iterator iter = MoleculesinSceneMap.find(_id);
|
---|
400 | ASSERT( iter != MoleculesinSceneMap.end(),
|
---|
401 | "GLWorldScene::moleculeInserted() - molecule's id "+toString(_id)+" is unknown.");
|
---|
402 |
|
---|
403 | GLMoleculeObject_molecule *molObject = iter->second;
|
---|
404 | molObject->setVisible(_visible);
|
---|
405 |
|
---|
406 | emit changed();
|
---|
407 | emit changeOccured();
|
---|
408 | }
|
---|
409 |
|
---|
410 | /** Adds a shape to the scene.
|
---|
411 | *
|
---|
412 | */
|
---|
413 | void GLWorldScene::addShape(const std::string &_name)
|
---|
414 | {
|
---|
415 | Shape * const shape = ShapeRegistry::getInstance().getByName(_name);
|
---|
416 | if (shape != NULL) {
|
---|
417 | GLMoleculeObject_shape *shapeObject = new GLMoleculeObject_shape(*shape, this);
|
---|
418 | ShapeNodeMap::iterator iter = ShapesinSceneMap.find(_name);
|
---|
419 | ASSERT(iter == ShapesinSceneMap.end(),
|
---|
420 | "GLWorldScene::addShape() - same shape "+_name+" added again.");
|
---|
421 | ShapesinSceneMap.insert( make_pair(_name, shapeObject) );
|
---|
422 | } else
|
---|
423 | ELOG(2, "GLWorldScene::addShape() - shape disappeared before we could draw it.");
|
---|
424 |
|
---|
425 | emit changed();
|
---|
426 | }
|
---|
427 |
|
---|
428 | void GLWorldScene::removeShape(const std::string &_name)
|
---|
429 | {
|
---|
430 | ShapeNodeMap::iterator iter = ShapesinSceneMap.find(_name);
|
---|
431 | ASSERT(iter != ShapesinSceneMap.end(),
|
---|
432 | "GLWorldScene::removeShape() - shape "+_name+" not in scene.");
|
---|
433 | ShapesinSceneMap.erase(iter);
|
---|
434 | delete(iter->second);
|
---|
435 |
|
---|
436 | emit changed();
|
---|
437 | }
|
---|
438 |
|
---|
439 | void GLWorldScene::updateSelectedShapes()
|
---|
440 | {
|
---|
441 | foreach (QObject *obj, children()) {
|
---|
442 | GLMoleculeObject_shape *shapeobj = qobject_cast<GLMoleculeObject_shape *>(obj);
|
---|
443 | if (shapeobj){
|
---|
444 | shapeobj->enable(ShapeRegistry::getInstance().isSelected(shapeobj->getShape()));
|
---|
445 | }
|
---|
446 | }
|
---|
447 |
|
---|
448 | emit changed();
|
---|
449 | }
|
---|
450 |
|
---|
451 | void GLWorldScene::initialize(QGLView *view, QGLPainter *painter) const
|
---|
452 | {
|
---|
453 | // Initialize all of the mesh objects that we have as children.
|
---|
454 | foreach (QObject *obj, children()) {
|
---|
455 | GLMoleculeObject *meshobj = qobject_cast<GLMoleculeObject *>(obj);
|
---|
456 | if (meshobj)
|
---|
457 | meshobj->initialize(view, painter);
|
---|
458 | }
|
---|
459 | }
|
---|
460 |
|
---|
461 | void GLWorldScene::draw(QGLPainter *painter, const QVector4D &cameraPlane) const
|
---|
462 | {
|
---|
463 | // Draw all of the mesh objects that we have as children.
|
---|
464 | foreach (QObject *obj, children()) {
|
---|
465 | GLMoleculeObject *meshobj = qobject_cast<GLMoleculeObject *>(obj);
|
---|
466 | if (meshobj)
|
---|
467 | meshobj->draw(painter, cameraPlane);
|
---|
468 | }
|
---|
469 | }
|
---|
470 |
|
---|
471 | void GLWorldScene::setSelectionMode(SelectionModeType mode)
|
---|
472 | {
|
---|
473 | selectionMode = mode;
|
---|
474 | // TODO send update to toolbar
|
---|
475 | }
|
---|
476 |
|
---|
477 | void GLWorldScene::setSelectionChangedAgent(QtSelectionChangedAgent *agent)
|
---|
478 | {
|
---|
479 | connect(agent, SIGNAL(atomSelected(const moleculeId_t, const atomId_t)),
|
---|
480 | this, SLOT(AtomSelected(const moleculeId_t, const atomId_t)));
|
---|
481 | connect(agent, SIGNAL(atomUnselected(const moleculeId_t, const atomId_t)),
|
---|
482 | this, SLOT(AtomUnselected(const moleculeId_t, const atomId_t)));
|
---|
483 |
|
---|
484 | connect(agent, SIGNAL(moleculeSelected(const moleculeId_t)),
|
---|
485 | this, SLOT(MoleculeSelected(const moleculeId_t)));
|
---|
486 | connect(agent, SIGNAL(moleculeUnselected(const moleculeId_t)),
|
---|
487 | this, SLOT(MoleculeUnselected(const moleculeId_t)));
|
---|
488 | }
|
---|
489 |
|
---|
490 | void GLWorldScene::setSelectionModeAtom()
|
---|
491 | {
|
---|
492 | setSelectionMode(SelectAtom);
|
---|
493 | }
|
---|
494 |
|
---|
495 | void GLWorldScene::setSelectionModeMolecule()
|
---|
496 | {
|
---|
497 | setSelectionMode(SelectMolecule);
|
---|
498 | }
|
---|
499 |
|
---|
500 | void GLWorldScene::changeMoleculeId(
|
---|
501 | GLMoleculeObject_molecule *ob,
|
---|
502 | const moleculeId_t oldId,
|
---|
503 | const moleculeId_t newId)
|
---|
504 | {
|
---|
505 | LOG(3, "INFO: GLWorldScene - change molecule id " << oldId << " to " << newId << ".");
|
---|
506 |
|
---|
507 | {
|
---|
508 | // Remove from map.
|
---|
509 | MoleculeNodeMap::iterator iter = MoleculesinSceneMap.find(oldId);
|
---|
510 | ASSERT(iter != MoleculesinSceneMap.end(),
|
---|
511 | "GLWorldScene::changeMoleculeId() - molecule with old id "+toString(oldId)+" not on display.");
|
---|
512 | ASSERT(iter->second == ob,
|
---|
513 | "GLWorldScene::changeMoleculeId() - molecule with id "
|
---|
514 | +toString(oldId)+" does not match with object in MoleculesinSceneMap.");
|
---|
515 | MoleculesinSceneMap.erase(iter);
|
---|
516 |
|
---|
517 | // Reinsert with new id.
|
---|
518 | {
|
---|
519 | MoleculeNodeMap::iterator iter = MoleculesinSceneMap.find(newId);
|
---|
520 | ASSERT(iter == MoleculesinSceneMap.end(),
|
---|
521 | "GLWorldScene::changeMoleculeId() - moleculewith new id "+toString(newId)+" already known.");
|
---|
522 | }
|
---|
523 | MoleculesinSceneMap.insert( make_pair(newId, ob) );
|
---|
524 | }
|
---|
525 |
|
---|
526 | {
|
---|
527 | // Remove and re-insert from map if present.
|
---|
528 | MoleculeMissedStateMap_t::iterator iter = MoleculeMissedStateMap.find(oldId);
|
---|
529 | if (iter != MoleculeMissedStateMap.end()) {
|
---|
530 | StateChangeMap_t changemap = iter->second;
|
---|
531 | MoleculeMissedStateMap.erase(iter);
|
---|
532 | MoleculeMissedStateMap.insert( std::make_pair(newId, changemap) );
|
---|
533 | }
|
---|
534 | }
|
---|
535 | }
|
---|
536 |
|
---|
537 | void GLWorldScene::AtomSelected(const moleculeId_t _molid, const atomId_t _id)
|
---|
538 | {
|
---|
539 | MoleculeNodeMap::iterator iter = MoleculesinSceneMap.find(_molid);
|
---|
540 | if (iter != MoleculesinSceneMap.end())
|
---|
541 | QMetaObject::invokeMethod(iter->second, // pointer to a QObject
|
---|
542 | "AtomSelected", // member name (no parameters here)
|
---|
543 | Qt::QueuedConnection, // connection type
|
---|
544 | Q_ARG(const atomId_t, _id)); // parameters
|
---|
545 | else
|
---|
546 | ELOG(2, "DEBUG: GLWorldScene::AtomSelected() - molecule " <<
|
---|
547 | _molid << " unknown to GLWorldScene.");
|
---|
548 | }
|
---|
549 |
|
---|
550 | void GLWorldScene::AtomUnselected(const moleculeId_t _molid, const atomId_t _id)
|
---|
551 | {
|
---|
552 | MoleculeNodeMap::iterator iter = MoleculesinSceneMap.find(_molid);
|
---|
553 | if (iter != MoleculesinSceneMap.end())
|
---|
554 | QMetaObject::invokeMethod(iter->second, // pointer to a QObject
|
---|
555 | "AtomUnselected", // member name (no parameters here)
|
---|
556 | Qt::QueuedConnection, // connection type
|
---|
557 | Q_ARG(const atomId_t, _id)); // parameters
|
---|
558 | else
|
---|
559 | ELOG(2, "GLWorldScene::AtomUnselected() - molecule "
|
---|
560 | << _molid << " unknown to GLWorldScene.");
|
---|
561 | }
|
---|
562 |
|
---|
563 | void GLWorldScene::MoleculeSelected(const moleculeId_t _molid)
|
---|
564 | {
|
---|
565 | MoleculeNodeMap::iterator iter = MoleculesinSceneMap.find(_molid);
|
---|
566 | if (iter != MoleculesinSceneMap.end())
|
---|
567 | QMetaObject::invokeMethod(iter->second, // pointer to a QObject
|
---|
568 | "Selected", // member name (no parameters here)
|
---|
569 | Qt::QueuedConnection); // connection type
|
---|
570 | else
|
---|
571 | ELOG(2, "GLWorldScene::MoleculeSelected() - molecule "
|
---|
572 | << _molid << " unknown to GLWorldScene.");
|
---|
573 | }
|
---|
574 |
|
---|
575 | void GLWorldScene::MoleculeUnselected(const moleculeId_t _molid)
|
---|
576 | {
|
---|
577 | MoleculeNodeMap::iterator iter = MoleculesinSceneMap.find(_molid);
|
---|
578 | if (iter != MoleculesinSceneMap.end())
|
---|
579 | QMetaObject::invokeMethod(iter->second, // pointer to a QObject
|
---|
580 | "Unselected", // member name (no parameters here)
|
---|
581 | Qt::QueuedConnection); // connection type
|
---|
582 | else
|
---|
583 | ELOG(2, "GLWorldScene::MoleculeUnselected() - molecule "
|
---|
584 | << _molid << " unknown to GLWorldScene.");
|
---|
585 | }
|
---|