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/Qt4/InstanceBoard/QtObservedInstanceBoard.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(
|
---|
75 | QtObservedInstanceBoard * _board,
|
---|
76 | QObject *parent) :
|
---|
77 | QObject(parent),
|
---|
78 | selectionMode(SelectAtom),
|
---|
79 | board(_board)
|
---|
80 | {
|
---|
81 | int sphereDetails[] = {5, 3, 2, 0};
|
---|
82 | int cylinderDetails[] = {16, 8, 6, 3};
|
---|
83 | for (int i=0;i<GLMoleculeObject::DETAILTYPES_MAX;i++){
|
---|
84 | QGLBuilder emptyBuilder;
|
---|
85 | GLMoleculeObject::meshEmpty[i] = emptyBuilder.finalizedSceneNode();
|
---|
86 | QGLBuilder sphereBuilder;
|
---|
87 | sphereBuilder << QGLSphere(2.0, sphereDetails[i]);
|
---|
88 | GLMoleculeObject::meshSphere[i] = sphereBuilder.finalizedSceneNode();
|
---|
89 | GLMoleculeObject::meshSphere[i]->setOption(QGLSceneNode::CullBoundingBox, true);
|
---|
90 | QGLBuilder cylinderBuilder;
|
---|
91 | cylinderBuilder << QGLCylinder(.25,.25,1.0,cylinderDetails[i]);
|
---|
92 | GLMoleculeObject::meshCylinder[i] = cylinderBuilder.finalizedSceneNode();
|
---|
93 | GLMoleculeObject::meshCylinder[i]->setOption(QGLSceneNode::CullBoundingBox, true);
|
---|
94 | }
|
---|
95 | connect(board, SIGNAL(moleculeInserted(QtObservedMolecule::ptr)),
|
---|
96 | this, SLOT(moleculeSignOn(QtObservedMolecule::ptr)), Qt::DirectConnection);
|
---|
97 | connect(board, SIGNAL(moleculeRemoved(const moleculeId_t)),
|
---|
98 | this, SLOT(moleculeSignOff(const moleculeId_t)), Qt::DirectConnection);
|
---|
99 | connect(board, SIGNAL(moleculeIndexChanged(const moleculeId_t, const moleculeId_t)),
|
---|
100 | this, SLOT(moleculeIndexChanged(const moleculeId_t, const moleculeId_t)));
|
---|
101 | connect(board, SIGNAL(atomInserted(QtObservedAtom::ptr)),
|
---|
102 | this, SLOT(atomInserted(QtObservedAtom::ptr)));
|
---|
103 | connect(board, SIGNAL(atomRemoved(const atomId_t)),
|
---|
104 | this, SLOT(atomRemoved(const atomId_t)));
|
---|
105 | connect(this, SIGNAL(insertMolecule(QtObservedMolecule::ptr)),
|
---|
106 | this, SLOT(moleculeInserted(QtObservedMolecule::ptr)) );
|
---|
107 | connect(this, SIGNAL(removeMolecule(QtObservedMolecule*)),
|
---|
108 | this, SLOT(moleculeRemoved(QtObservedMolecule*)) );
|
---|
109 |
|
---|
110 | // connect(this, SIGNAL(updated()), this, SLOT(update()));
|
---|
111 | }
|
---|
112 |
|
---|
113 | GLWorldScene::~GLWorldScene()
|
---|
114 | {
|
---|
115 | // remove all elements
|
---|
116 | GLMoleculeObject::cleanMaterialMap();
|
---|
117 | }
|
---|
118 |
|
---|
119 | void GLWorldScene::atomClicked(atomId_t no)
|
---|
120 | {
|
---|
121 | LOG(3, "INFO: GLMoleculeObject_molecule - atom " << no << " has been clicked.");
|
---|
122 | const atom * const Walker = const_cast<const World &>(World::getInstance()).
|
---|
123 | getAtom(AtomById(no));
|
---|
124 | ASSERT( Walker != NULL,
|
---|
125 | "GLWorldScene::atomClicked() - clicked atom has disappeared.");
|
---|
126 | if (selectionMode == SelectAtom){
|
---|
127 | if (!World::getInstance().isSelected(Walker))
|
---|
128 | SelectionAtomById(std::vector<atomId_t>(1,no));
|
---|
129 | else
|
---|
130 | SelectionNotAtomById(std::vector<atomId_t>(1,no));
|
---|
131 | }else if (selectionMode == SelectMolecule){
|
---|
132 | const molecule *mol = Walker->getMolecule();
|
---|
133 | ASSERT(mol, "Atom without molecule has been clicked.");
|
---|
134 | molids_t ids(1, mol->getId());
|
---|
135 | if (!World::getInstance().isSelected(mol))
|
---|
136 | SelectionMoleculeById(ids);
|
---|
137 | else
|
---|
138 | SelectionNotMoleculeById(ids);
|
---|
139 | }
|
---|
140 | emit clicked(no);
|
---|
141 | }
|
---|
142 |
|
---|
143 | void GLWorldScene::moleculeClicked(moleculeId_t no)
|
---|
144 | {
|
---|
145 | LOG(3, "INFO: GLMoleculeObject_molecule - mol " << no << " has been clicked.");
|
---|
146 | const molecule * const mol= const_cast<const World &>(World::getInstance()).
|
---|
147 | getMolecule(MoleculeById(no));
|
---|
148 | ASSERT(mol, "Atom without molecule has been clicked.");
|
---|
149 | molids_t ids(1, mol->getId());
|
---|
150 | if (!World::getInstance().isSelected(mol))
|
---|
151 | SelectionMoleculeById(ids);
|
---|
152 | else
|
---|
153 | SelectionNotMoleculeById(ids);
|
---|
154 | emit clicked(no);
|
---|
155 | }
|
---|
156 |
|
---|
157 | /** Prepares insertion of a general atom.
|
---|
158 | *
|
---|
159 | * This is called before the insertion into a molecule and thus before the
|
---|
160 | * insertion into the scene.
|
---|
161 | *
|
---|
162 | * @param _atom atom to insert
|
---|
163 | */
|
---|
164 | void GLWorldScene::atomInserted(QtObservedAtom::ptr _atom)
|
---|
165 | {
|
---|
166 | const atomId_t atomid = _atom->getAtomIndex();
|
---|
167 | ASSERT( QtObservedAtomMap.find(atomid) == QtObservedAtomMap.end(),
|
---|
168 | "GLWorldScene::AtomInserted() - atom with id "+toString(atomid)
|
---|
169 | +" is already present in QtObservedAtomMap.");
|
---|
170 | QtObservedAtomMap[atomid] = _atom;
|
---|
171 | connect(_atom.get(), SIGNAL(indexChanged(const atomId_t,const atomId_t)),
|
---|
172 | this, SLOT(atomIndexChanged(const atomId_t,const atomId_t)) );
|
---|
173 | }
|
---|
174 |
|
---|
175 | /** Removes an general atom.
|
---|
176 | *
|
---|
177 | * This is called when the atom has been removed from the molecule.
|
---|
178 | *
|
---|
179 | * @param _atom atom to remove
|
---|
180 | */
|
---|
181 | void GLWorldScene::atomRemoved(const atomId_t _atomid)
|
---|
182 | {
|
---|
183 | const QtObservedAtomMap_t::iterator eraseiter = QtObservedAtomMap.find(_atomid);
|
---|
184 | ASSERT( eraseiter != QtObservedAtomMap.end(),
|
---|
185 | "GLWorldScene::AtomRemoved() - atom with id "+toString(_atomid)
|
---|
186 | +" is not present in QtObservedAtomMap.");
|
---|
187 | disconnect(eraseiter->second.get(), SIGNAL(indexChanged(const atomId_t,const atomId_t)),
|
---|
188 | this, SLOT(atomIndexChanged(const atomId_t,const atomId_t)) );
|
---|
189 | QtObservedAtomMap.erase(eraseiter);
|
---|
190 | }
|
---|
191 |
|
---|
192 | /** Inserts an atom into the scene when molecule is present.
|
---|
193 | *
|
---|
194 | * @param _atom atom to insert
|
---|
195 | */
|
---|
196 | void GLWorldScene::moleculesAtomInserted(QtObservedAtom::ptr _atom, QtObservedMolecule * _mol)
|
---|
197 | {
|
---|
198 | const atomId_t atomid = _atom->getAtomIndex();
|
---|
199 | QtObservedMolecule* const atommol = _atom->getAtomMolecule();
|
---|
200 | // this ASSERT cannot work: The same molecule may get reassigned to a different
|
---|
201 | // molecule, when e.g. the time step changes (which triggers an update-molecules).
|
---|
202 | // The GUI is slow and may lack behind and thus get to execute the event
|
---|
203 | // moleculeAtomInserted when the change has been already been done.
|
---|
204 | // ASSERT( (atommol == NULL) || (_mol == atommol),
|
---|
205 | // "GLWorldScene::moleculesAtomInserted() - atom "+toString(atomid)
|
---|
206 | // +" claims to belong to QtObservedMolecule "+toString(atommol)
|
---|
207 | // +" but signal came from QtObservedMolecule "+toString(_mol)+".");
|
---|
208 | LOG(3, "INFO: GLWorldScene: Received signal atomInserted for atom "+toString(atomid)+".");
|
---|
209 | const moleculeId_t molid = _mol->getMolIndex();
|
---|
210 |
|
---|
211 | // check of molecule is already present
|
---|
212 | boost::recursive_mutex::scoped_lock lock(MoleculeinSceneMap_mutex);
|
---|
213 | const MoleculeNodeMap::iterator moliter = MoleculesinSceneMap.find(molid);
|
---|
214 | if (moliter != MoleculesinSceneMap.end()) {
|
---|
215 | const RemovedMoleculesMap_t::iterator emptyiter = EmptyMolecules.find(_mol);
|
---|
216 | if (emptyiter != EmptyMolecules.end())
|
---|
217 | EmptyMolecules.erase(emptyiter);
|
---|
218 | // check that it is the right molecule
|
---|
219 | QtObservedMolecule::ptr &checkmol = moliter->second->ObservedMolecule;
|
---|
220 | if (checkmol.get() == _mol) {
|
---|
221 | LOG(3, "INFO: GLWorldScene: Sending signal moleculesAtomInserted for atom "+toString(atomid)+".");
|
---|
222 | QMetaObject::invokeMethod(moliter->second, // pointer to a QObject
|
---|
223 | "atomInserted", // member name (no parameters here)
|
---|
224 | Qt::QueuedConnection, // connection type
|
---|
225 | Q_ARG(QtObservedAtom::ptr, _atom)); // parameters
|
---|
226 | } else {
|
---|
227 | // relay atomRemoved to GLMoleculeObject_molecule in RemovedMolecules
|
---|
228 | // LOG(3, "INFO: GLWorldScene: Sending signal moleculesAtomInserted for atom "+toString(_atomid)
|
---|
229 | // +" to molecule in RemovedMolecules.");
|
---|
230 | // const RemovedMoleculesMap_t::iterator removedmoliter = RemovedMolecules.find(molid);
|
---|
231 | // ASSERT( removedmoliter != RemovedMolecules.end(),
|
---|
232 | // "GLWorldScene::moleculesAtomInserted() - signal from old mol "
|
---|
233 | // +toString(molid)+", but not present in RemovedMolecules");
|
---|
234 | // QMetaObject::invokeMethod(removedmoliter->second, // pointer to a QObject
|
---|
235 | // "atomInserted ", // member name (no parameters here)
|
---|
236 | // Qt::QueuedConnection, // connection type
|
---|
237 | // Q_ARG(QtObservedAtom::ptr, _atom)); // parameters
|
---|
238 | ASSERT( 0,
|
---|
239 | "GLWorldScene::moleculesAtomInserted() - would need to send atomInserted to already removed molecule.");
|
---|
240 | }
|
---|
241 | } else {
|
---|
242 | const RemovedMoleculesMap_t::iterator removedmoliter = RemovedMolecules.find(_mol);
|
---|
243 | if (removedmoliter != RemovedMolecules.end()) {
|
---|
244 | ASSERT( 0,
|
---|
245 | "GLWorldScene::moleculesAtomInserted() - would need to send atomInserted to already removed molecule.");
|
---|
246 | } else {
|
---|
247 | boost::recursive_mutex::scoped_lock lock(MoleculeMissedStateMap_mutex);
|
---|
248 | // only record missed state for molecule if (still) present but not instantiated
|
---|
249 | if (QtObservedMoleculeMap.count(molid)) {
|
---|
250 | // store signal for when it is instantiated
|
---|
251 | if (MoleculeMissedStateMap.count(molid) == 0)
|
---|
252 | MoleculeMissedStateMap.insert( std::make_pair(molid ,StateChangeMap_t()) );
|
---|
253 | MoleculeMissedStateMap[molid].insert( std::make_pair(atomid, atomInsertedState) );
|
---|
254 | ASSERT( QtObservedAtomMap[atomid] == _atom,
|
---|
255 | "GLWorldScene::moleculesAtomInserted() - atom "+toString(atomid)
|
---|
256 | +" inserted in molecule "+toString(molid)
|
---|
257 | +" which does not match atom in QtObservedAtomMap.");
|
---|
258 | LOG(3, "INFO: GLWorldScene: Placing atomInserted for atom " << atomid
|
---|
259 | << " and molecule " << molid << " into missed state map.");
|
---|
260 | }
|
---|
261 | }
|
---|
262 | }
|
---|
263 | }
|
---|
264 |
|
---|
265 | /** Removes an atom into the scene before molecule is present.
|
---|
266 | *
|
---|
267 | * @param _atomid atom to remove
|
---|
268 | */
|
---|
269 | void GLWorldScene::moleculesAtomRemoved(const atomId_t _atomid, QtObservedMolecule * _mol)
|
---|
270 | {
|
---|
271 | LOG(3, "INFO: GLWorldScene: Received signal atomRemoved for atom "+toString(_atomid)+".");
|
---|
272 |
|
---|
273 | const moleculeId_t molid = _mol->getMolIndex();
|
---|
274 | // check of molecule is already present
|
---|
275 | boost::recursive_mutex::scoped_lock lock(MoleculeinSceneMap_mutex);
|
---|
276 | const MoleculeNodeMap::iterator moliter = MoleculesinSceneMap.find(molid);
|
---|
277 | if (moliter != MoleculesinSceneMap.end()) {
|
---|
278 | const QtObservedMolecule::ptr &checkmol = moliter->second->ObservedMolecule;
|
---|
279 | if (checkmol.get() == _mol) {
|
---|
280 | LOG(3, "INFO: GLWorldScene: Sending signal moleculesAtomRemoved for atom "+toString(_atomid)+".");
|
---|
281 | QMetaObject::invokeMethod(moliter->second, // pointer to a QObject
|
---|
282 | "atomRemoved", // member name (no parameters here)
|
---|
283 | Qt::QueuedConnection, // connection type
|
---|
284 | Q_ARG(const atomId_t, _atomid)); // parameters
|
---|
285 | } else {
|
---|
286 | // relay atomRemoved to GLMoleculeObject_molecule in RemovedMolecules
|
---|
287 | LOG(3, "INFO: GLWorldScene: Sending signal moleculesAtomRemoved for atom "+toString(_atomid)
|
---|
288 | +" to molecule in RemovedMolecules.");
|
---|
289 | const RemovedMoleculesMap_t::iterator removedmoliter = RemovedMolecules.find(_mol);
|
---|
290 | ASSERT( removedmoliter != RemovedMolecules.end(),
|
---|
291 | "GLWorldScene::moleculesAtomRemoved() - signal from old molecule "
|
---|
292 | +toString(molid)+", but not present in RemovedMolecules");
|
---|
293 | #ifndef NDEBUG
|
---|
294 | const QtObservedMolecule::ptr &othercheckmol = removedmoliter->second->ObservedMolecule;
|
---|
295 | #endif
|
---|
296 | ASSERT( othercheckmol.get() == _mol,
|
---|
297 | "GLWorldScene::moleculesAtomRemoved() - signal from old molecule "
|
---|
298 | +toString(molid)+", but different one "+toString(othercheckmol)
|
---|
299 | +" present in RemovedMolecules.");
|
---|
300 | QMetaObject::invokeMethod(removedmoliter->second, // pointer to a QObject
|
---|
301 | "atomRemoved", // member name (no parameters here)
|
---|
302 | Qt::QueuedConnection, // connection type
|
---|
303 | Q_ARG(const atomId_t, _atomid)); // parameters
|
---|
304 | }
|
---|
305 | } else {
|
---|
306 | const RemovedMoleculesMap_t::iterator removedmoliter = RemovedMolecules.find(_mol);
|
---|
307 | if (removedmoliter != RemovedMolecules.end()) {
|
---|
308 | ASSERT( removedmoliter != RemovedMolecules.end(),
|
---|
309 | "GLWorldScene::moleculesAtomRemoved() - signal from old molecule "
|
---|
310 | +toString(molid)+", but not present in RemovedMolecules");
|
---|
311 | #ifndef NDEBUG
|
---|
312 | const QtObservedMolecule::ptr &othercheckmol = removedmoliter->second->ObservedMolecule;
|
---|
313 | #endif
|
---|
314 | ASSERT( othercheckmol.get() == _mol,
|
---|
315 | "GLWorldScene::moleculesAtomRemoved() - signal from old molecule "
|
---|
316 | +toString(molid)+", but different one "+toString(othercheckmol)
|
---|
317 | +" present in RemovedMolecules.");
|
---|
318 | QMetaObject::invokeMethod(removedmoliter->second, // pointer to a QObject
|
---|
319 | "atomRemoved", // member name (no parameters here)
|
---|
320 | Qt::QueuedConnection, // connection type
|
---|
321 | Q_ARG(const atomId_t, _atomid)); // parameters
|
---|
322 | } else {
|
---|
323 | boost::recursive_mutex::scoped_lock lock(MoleculeMissedStateMap_mutex);
|
---|
324 | // only record missed state for molecule if (still) present but not instantiated
|
---|
325 | if (QtObservedMoleculeMap.count(molid)) {
|
---|
326 | // store signal for when it is instantiated
|
---|
327 | if (MoleculeMissedStateMap.count(molid) == 0)
|
---|
328 | MoleculeMissedStateMap.insert( std::make_pair(molid, StateChangeMap_t()) );
|
---|
329 | MoleculeMissedStateMap[molid].insert( std::make_pair(_atomid, atomRemovedState) );
|
---|
330 | LOG(3, "INFO: GLWorldScene: Placing atomRemoved for atom " << _atomid
|
---|
331 | << " and molecule " << molid << " into missed state map.");
|
---|
332 | }
|
---|
333 | }
|
---|
334 | }
|
---|
335 | }
|
---|
336 |
|
---|
337 | void GLWorldScene::moleculeSignOn(QtObservedMolecule::ptr _mol)
|
---|
338 | {
|
---|
339 | // sign on to QtObservedMolecule to get atomInserted/..Removed signals from same
|
---|
340 | // source as GLMoleculeObject_molecule would
|
---|
341 | connect(_mol.get(), SIGNAL(atomInserted(QtObservedAtom::ptr, QtObservedMolecule *)),
|
---|
342 | this, SLOT(moleculesAtomInserted(QtObservedAtom::ptr, QtObservedMolecule *)) );
|
---|
343 | connect(_mol.get(), SIGNAL(atomRemoved(const atomId_t, QtObservedMolecule *)),
|
---|
344 | this, SLOT(moleculesAtomRemoved(const atomId_t, QtObservedMolecule *)) );
|
---|
345 | const moleculeId_t molid = _mol->getMolIndex();
|
---|
346 | ASSERT( QtObservedMoleculeMap.find(molid) == QtObservedMoleculeMap.end(),
|
---|
347 | "GLWorldScene::moleculeSignOn() - molecule with id "+toString(molid)
|
---|
348 | +" is already present in QtObservedMoleculeMap.");
|
---|
349 | QtObservedMoleculeMap[molid] = _mol;
|
---|
350 | connect(_mol.get(), SIGNAL(indexChanged(const moleculeId_t,const moleculeId_t)),
|
---|
351 | this, SLOT(moleculeIndexChanged(const moleculeId_t,const moleculeId_t)) );
|
---|
352 |
|
---|
353 | LOG(3, "INFO: GLWorldScene: Received signal moleculeSignOn for molecule "+toString(molid)+".");
|
---|
354 |
|
---|
355 | emit insertMolecule(_mol);
|
---|
356 | }
|
---|
357 |
|
---|
358 | void GLWorldScene::moleculeSignOff(const moleculeId_t _id)
|
---|
359 | {
|
---|
360 | const QtObservedMoleculeMap_t::iterator eraseiter = QtObservedMoleculeMap.find(_id);
|
---|
361 | ASSERT( eraseiter != QtObservedMoleculeMap.end(),
|
---|
362 | "GLWorldScene::moleculeSignOff() - cannot find id "+toString(_id)+" in map.");
|
---|
363 | disconnect(eraseiter->second.get(), SIGNAL(indexChanged(const moleculeId_t,const moleculeId_t)),
|
---|
364 | this, SLOT(moleculeIndexChanged(const moleculeId_t,const moleculeId_t)) );
|
---|
365 | QtObservedMolecule * mol = eraseiter->second.get();
|
---|
366 | QtObservedMoleculeMap.erase(eraseiter);
|
---|
367 |
|
---|
368 | LOG(3, "INFO: GLWorldScene: Received signal moleculeSignOff for molecule "+toString(_id)+".");
|
---|
369 |
|
---|
370 | emit removeMolecule(mol);
|
---|
371 | }
|
---|
372 |
|
---|
373 | /** Inserts a molecule into the scene.
|
---|
374 | *
|
---|
375 | * @param _mol molecule to insert
|
---|
376 | */
|
---|
377 | void GLWorldScene::moleculeInserted(QtObservedMolecule::ptr _mol)
|
---|
378 | {
|
---|
379 | ASSERT (_mol,
|
---|
380 | "GLWorldScene::moleculeInserted() - received shared_ptr for molecule is empty?");
|
---|
381 | const moleculeId_t molid = _mol->getMolIndex();
|
---|
382 | LOG(3, "INFO: GLWorldScene: Received signal moleculeInserted for molecule "+toString(molid)+".");
|
---|
383 |
|
---|
384 | boost::recursive_mutex::scoped_lock lock(MoleculeinSceneMap_mutex);
|
---|
385 |
|
---|
386 | MoleculeNodeMap::const_iterator iter = MoleculesinSceneMap.find(molid);
|
---|
387 | ASSERT( iter == MoleculesinSceneMap.end(),
|
---|
388 | "GLWorldScene::moleculeInserted() - molecule's id "+toString(molid)+" already present.");
|
---|
389 |
|
---|
390 | // add new object
|
---|
391 | LOG(1, "DEBUG: Adding GLMoleculeObject_molecule to id " << molid);
|
---|
392 | GLMoleculeObject_molecule *molObject =
|
---|
393 | new GLMoleculeObject_molecule(
|
---|
394 | GLMoleculeObject::meshEmpty,
|
---|
395 | this,
|
---|
396 | *board,
|
---|
397 | _mol);
|
---|
398 | ASSERT( molObject != NULL,
|
---|
399 | "GLWorldScene::moleculeInserted - could not create molecule object for "+toString(molid));
|
---|
400 | #ifndef NDEBUG
|
---|
401 | std::pair<MoleculeNodeMap::iterator, bool> inserter =
|
---|
402 | #endif
|
---|
403 | MoleculesinSceneMap.insert( make_pair(molid, molObject) );
|
---|
404 | ASSERT(inserter.second,
|
---|
405 | "GLWorldScene::moleculeInserted() - molecule "+toString(molid)+" already present in scene.");
|
---|
406 |
|
---|
407 | // now handle all state changes that came up before the instantiation
|
---|
408 | if (MoleculeMissedStateMap.count(molid) != 0) {
|
---|
409 | // ASSERT( !MoleculeMissedStateMap[molid].empty(),
|
---|
410 | // "GLWorldScene::moleculeInserted() - we have an empty state change map for molecule with id "
|
---|
411 | // +toString(molid));
|
---|
412 | boost::recursive_mutex::scoped_lock lock(MoleculeMissedStateMap_mutex);
|
---|
413 | for (StateChangeMap_t::iterator iter = MoleculeMissedStateMap[molid].begin();
|
---|
414 | !MoleculeMissedStateMap[molid].empty();
|
---|
415 | iter = MoleculeMissedStateMap[molid].begin()) {
|
---|
416 | std::pair<StateChangeMap_t::iterator, StateChangeMap_t::iterator> rangeiter =
|
---|
417 | MoleculeMissedStateMap[molid].equal_range(iter->first);
|
---|
418 | const size_t StateCounts = std::distance(rangeiter.first, rangeiter.second);
|
---|
419 | if (StateCounts > 1) {
|
---|
420 | // more than one state change, have to combine
|
---|
421 | typedef std::map<StateChangeType, size_t> StateChangeAmounts_t;
|
---|
422 | StateChangeAmounts_t StateChangeAmounts;
|
---|
423 | for (StateChangeMap_t::const_iterator stateiter = rangeiter.first;
|
---|
424 | stateiter != rangeiter.second; ++stateiter)
|
---|
425 | ++StateChangeAmounts[stateiter->second];
|
---|
426 | ASSERT( StateChangeAmounts[atomInsertedState] >= StateChangeAmounts[atomRemovedState],
|
---|
427 | "GLWorldScene::moleculeInserted() - more atomRemoved states "
|
---|
428 | +toString(StateChangeAmounts[atomRemovedState])+" than atomInserted "
|
---|
429 | +toString(StateChangeAmounts[atomInsertedState])+" for atom "+toString(iter->first));
|
---|
430 | if (StateChangeAmounts[atomInsertedState] > StateChangeAmounts[atomRemovedState]) {
|
---|
431 | LOG(1, "INFO: invoking atomInserted for atom " << iter->first);
|
---|
432 | QMetaObject::invokeMethod(molObject, // pointer to a QObject
|
---|
433 | "atomInserted", // member name (no parameters here)
|
---|
434 | Qt::QueuedConnection, // connection type
|
---|
435 | Q_ARG(QtObservedAtom::ptr, QtObservedAtomMap[iter->first])); // parameters
|
---|
436 | } else {
|
---|
437 | LOG(1, "INFO: Atom " << iter->first << " has been inserted and removed already.");
|
---|
438 | }
|
---|
439 | // removed all state changes for this atom
|
---|
440 | MoleculeMissedStateMap[molid].erase(rangeiter.first, rangeiter.second);
|
---|
441 | } else {
|
---|
442 | // can only be an insertion
|
---|
443 | switch (rangeiter.first->second) {
|
---|
444 | case atomRemovedState:
|
---|
445 | ASSERT( 0,
|
---|
446 | "GLWorldScene::moleculeInserted() - atomRemoved state without atomInserted for atom "
|
---|
447 | +toString(iter->first));
|
---|
448 | break;
|
---|
449 | case atomInsertedState:
|
---|
450 | {
|
---|
451 | LOG(1, "INFO: invoking atomInserted for atom " << iter->first);
|
---|
452 | QMetaObject::invokeMethod(molObject, // pointer to a QObject
|
---|
453 | "atomInserted", // member name (no parameters here)
|
---|
454 | Qt::QueuedConnection, // connection type
|
---|
455 | Q_ARG(QtObservedAtom::ptr, QtObservedAtomMap[iter->first])); // parameters
|
---|
456 | break;
|
---|
457 | }
|
---|
458 | default:
|
---|
459 | ASSERT( 0,
|
---|
460 | "GLWorldScene::moleculeInserted() - there are unknown change states.");
|
---|
461 | break;
|
---|
462 | }
|
---|
463 | // removed state changes for this atom
|
---|
464 | MoleculeMissedStateMap[molid].erase(iter);
|
---|
465 | }
|
---|
466 | }
|
---|
467 | }
|
---|
468 |
|
---|
469 | connect (molObject, SIGNAL(changed()), this, SIGNAL(changed()));
|
---|
470 | connect (molObject, SIGNAL(changeOccured()), this, SIGNAL(changeOccured()));
|
---|
471 | connect (molObject, SIGNAL(atomClicked(atomId_t)), this, SLOT(atomClicked(atomId_t)));
|
---|
472 | connect (molObject, SIGNAL(moleculeClicked(moleculeId_t)), this, SLOT(moleculeClicked(moleculeId_t)));
|
---|
473 | connect (molObject, SIGNAL(moleculeEmptied(QtObservedMolecule::ptr)), this, SLOT(moleculeEmpty(QtObservedMolecule::ptr)));
|
---|
474 | connect (molObject, SIGNAL(selectionChanged()), this, SIGNAL(changed()));
|
---|
475 | connect (molObject, SIGNAL(selectionChanged()), this, SIGNAL(changed()));
|
---|
476 | connect (molObject, SIGNAL(hoverChanged(const atomId_t)), this, SIGNAL(hoverChanged(const atomId_t)));
|
---|
477 | connect (molObject, SIGNAL(hoverChanged(const moleculeId_t, int)), this, SIGNAL(hoverChanged(const moleculeId_t, int)));
|
---|
478 | connect (molObject, SIGNAL(hoverChanged(const moleculeId_t, int)), this, SIGNAL(hoverChanged(const moleculeId_t, int)));
|
---|
479 |
|
---|
480 | emit changed();
|
---|
481 | emit changeOccured();
|
---|
482 |
|
---|
483 | // remove state change map for the molecule
|
---|
484 | {
|
---|
485 | boost::recursive_mutex::scoped_lock lock(MoleculeMissedStateMap_mutex);
|
---|
486 | MoleculeMissedStateMap.erase(molid);
|
---|
487 | }
|
---|
488 | }
|
---|
489 |
|
---|
490 | /** Removes a molecule from the scene.
|
---|
491 | *
|
---|
492 | * @param _mol QtObservedMolecule instance of molecule to remove
|
---|
493 | */
|
---|
494 | void GLWorldScene::moleculeRemoved(QtObservedMolecule* _mol)
|
---|
495 | {
|
---|
496 | const moleculeId_t molid = _mol->getMolIndex();
|
---|
497 | LOG(3, "INFO: GLWorldScene: Received signal moleculeRemoved for molecule "+toString(molid)+".");
|
---|
498 |
|
---|
499 | boost::recursive_mutex::scoped_lock lock(MoleculeinSceneMap_mutex);
|
---|
500 |
|
---|
501 | MoleculeNodeMap::iterator iter = MoleculesinSceneMap.find(molid);
|
---|
502 | ASSERT ( iter != MoleculesinSceneMap.end(),
|
---|
503 | "GLWorldScene::moleculeRemoved() - to be removed molecule "+toString(molid)
|
---|
504 | +" is already gone.");
|
---|
505 | // check if it's already empty
|
---|
506 | const RemovedMoleculesMap_t::iterator emptyiter = EmptyMolecules.find(_mol);
|
---|
507 | if (emptyiter != EmptyMolecules.end()) {
|
---|
508 | LOG(1, "DEBUG: Removing empty GLMoleculeObject_molecule to id " << molid << " from scene.");
|
---|
509 | // it's already empty, remove it
|
---|
510 | ASSERT( emptyiter->second == iter->second,
|
---|
511 | "GLWorldScene::moleculeRemoved() - empty molecule "
|
---|
512 | +toString(emptyiter->second)+" and removed molecule "
|
---|
513 | +toString(iter->second)+" don't match.");
|
---|
514 | LOG(1, "DEBUG: Deleting already empty GLMoleculeObject_molecule to id " << molid);
|
---|
515 | GLMoleculeObject_molecule *molObject = emptyiter->second;
|
---|
516 | EmptyMolecules.erase(emptyiter);
|
---|
517 | molObject->disconnect();
|
---|
518 | delete molObject;
|
---|
519 |
|
---|
520 | emit changed();
|
---|
521 | emit changeOccured();
|
---|
522 | } else {
|
---|
523 | // otherwise note it as removal candidate
|
---|
524 | RemovedMolecules.insert( std::make_pair(_mol, iter->second) );
|
---|
525 | }
|
---|
526 | MoleculesinSceneMap.erase(iter);
|
---|
527 |
|
---|
528 | // remove any possible state changes left
|
---|
529 | {
|
---|
530 | boost::recursive_mutex::scoped_lock lock(MoleculeMissedStateMap_mutex);
|
---|
531 | MoleculeMissedStateMap.erase(molid);
|
---|
532 | }
|
---|
533 | }
|
---|
534 |
|
---|
535 | void GLWorldScene::moleculeEmpty(QtObservedMolecule::ptr _mol)
|
---|
536 | {
|
---|
537 | const moleculeId_t molid = _mol->getMolIndex();
|
---|
538 | LOG(3, "INFO: GLWorldScene: Received signal moleculeEmpty for molecule "+toString(molid)+".");
|
---|
539 |
|
---|
540 | boost::recursive_mutex::scoped_lock lock(MoleculeinSceneMap_mutex);
|
---|
541 |
|
---|
542 | MoleculeNodeMap::iterator iter = MoleculesinSceneMap.find(molid);
|
---|
543 | if ( iter == MoleculesinSceneMap.end()) {
|
---|
544 | RemovedMoleculesMap_t::iterator removeiter = RemovedMolecules.find(_mol.get());
|
---|
545 | ASSERT ( removeiter != RemovedMolecules.end(),
|
---|
546 | "GLWorldScene::moleculeEmpty() - to be removed molecule "+toString(molid)
|
---|
547 | +" is neither in MoleculesinSceneMap, nor in RemovedMolecules.");
|
---|
548 | // it's noted for removal already, remove it
|
---|
549 | LOG(1, "DEBUG: Deleting empty GLMoleculeObject_molecule to id " << molid);
|
---|
550 | GLMoleculeObject_molecule *molObject = removeiter->second;
|
---|
551 | RemovedMolecules.erase(removeiter);
|
---|
552 | molObject->disconnect();
|
---|
553 | delete molObject;
|
---|
554 |
|
---|
555 | emit changed();
|
---|
556 | emit changeOccured();
|
---|
557 | } else {
|
---|
558 | // otherwise just note it as empty
|
---|
559 | EmptyMolecules.insert( std::make_pair(_mol.get(), iter->second) );
|
---|
560 | }
|
---|
561 |
|
---|
562 | // // remove any possible state changes left
|
---|
563 | // {
|
---|
564 | // boost::recursive_mutex::scoped_lock lock(MoleculeMissedStateMap_mutex);
|
---|
565 | // MoleculeMissedStateMap.erase(molid);
|
---|
566 | // }
|
---|
567 | }
|
---|
568 |
|
---|
569 | void GLWorldScene::atomIndexChanged(const atomId_t _oldid, const atomId_t _newid)
|
---|
570 | {
|
---|
571 | if (QtObservedAtomMap.count(_oldid)) {
|
---|
572 | const QtObservedAtom::ptr atom = QtObservedAtomMap[_oldid];
|
---|
573 | QtObservedAtomMap.erase(_oldid);
|
---|
574 | QtObservedAtomMap.insert( std::make_pair(_newid, atom) );
|
---|
575 | }
|
---|
576 | }
|
---|
577 |
|
---|
578 | void GLWorldScene::moleculeIndexChanged(const moleculeId_t _oldid, const moleculeId_t _newid)
|
---|
579 | {
|
---|
580 | {
|
---|
581 | QtObservedMoleculeMap_t::iterator const olditer = QtObservedMoleculeMap.find(_oldid);
|
---|
582 | ASSERT ( olditer != QtObservedMoleculeMap.end(),
|
---|
583 | "GLWorldScene::moleculeIndexChanged() - molecule "
|
---|
584 | +toString(_oldid)+" not present in QtObservedMoleculeMap.");
|
---|
585 | #ifndef NDEBUG
|
---|
586 | QtObservedMoleculeMap_t::iterator const newiter = QtObservedMoleculeMap.find(_newid);
|
---|
587 | ASSERT ( newiter == QtObservedMoleculeMap.end(),
|
---|
588 | "GLWorldScene::moleculeIndexChanged() - molecule "
|
---|
589 | +toString(_newid)+" already present in QtObservedMoleculeMap.");
|
---|
590 | #endif
|
---|
591 | const QtObservedMolecule::ptr mol = olditer->second;
|
---|
592 | QtObservedMoleculeMap.erase(olditer);
|
---|
593 | QtObservedMoleculeMap[_newid] = mol;
|
---|
594 | }
|
---|
595 | {
|
---|
596 | MoleculeNodeMap::iterator const olditer = MoleculesinSceneMap.find(_oldid);
|
---|
597 | if ( olditer != MoleculesinSceneMap.end()) {
|
---|
598 | #ifndef NDEBUG
|
---|
599 | MoleculeNodeMap::iterator const newiter = MoleculesinSceneMap.find(_newid);
|
---|
600 | ASSERT ( newiter == MoleculesinSceneMap.end(),
|
---|
601 | "GLWorldScene::moleculeIndexChanged() - molecule "
|
---|
602 | +toString(_newid)+" already present in MoleculesinSceneMap.");
|
---|
603 | #endif
|
---|
604 | LOG(1, "DEBUG: Changing GLMoleculeObject_molecule in MoleculesinSceneMapfrom "
|
---|
605 | << _oldid << " to id " << _newid);
|
---|
606 | GLMoleculeObject_molecule* const molObject = olditer->second;
|
---|
607 | MoleculesinSceneMap.erase(olditer);
|
---|
608 | MoleculesinSceneMap[_newid] = molObject;
|
---|
609 | }
|
---|
610 | }
|
---|
611 | }
|
---|
612 |
|
---|
613 | void GLWorldScene::moleculesVisibilityChanged(const moleculeId_t _id, bool _visible)
|
---|
614 | {
|
---|
615 | boost::recursive_mutex::scoped_lock lock(MoleculeinSceneMap_mutex);
|
---|
616 | MoleculeNodeMap::iterator iter = MoleculesinSceneMap.find(_id);
|
---|
617 | ASSERT( iter != MoleculesinSceneMap.end(),
|
---|
618 | "GLWorldScene::moleculeInserted() - molecule's id "+toString(_id)+" is unknown.");
|
---|
619 |
|
---|
620 | GLMoleculeObject_molecule *molObject = iter->second;
|
---|
621 | molObject->setVisible(_visible);
|
---|
622 |
|
---|
623 | emit changed();
|
---|
624 | emit changeOccured();
|
---|
625 | }
|
---|
626 |
|
---|
627 | /** Adds a shape to the scene.
|
---|
628 | *
|
---|
629 | */
|
---|
630 | void GLWorldScene::addShape(const std::string &_name)
|
---|
631 | {
|
---|
632 | Shape * const shape = ShapeRegistry::getInstance().getByName(_name);
|
---|
633 | if (shape != NULL) {
|
---|
634 | GLMoleculeObject_shape *shapeObject = new GLMoleculeObject_shape(*shape, this);
|
---|
635 | ShapeNodeMap::iterator iter = ShapesinSceneMap.find(_name);
|
---|
636 | ASSERT(iter == ShapesinSceneMap.end(),
|
---|
637 | "GLWorldScene::addShape() - same shape "+_name+" added again.");
|
---|
638 | ShapesinSceneMap.insert( make_pair(_name, shapeObject) );
|
---|
639 | } else
|
---|
640 | ELOG(2, "GLWorldScene::addShape() - shape disappeared before we could draw it.");
|
---|
641 |
|
---|
642 | emit changed();
|
---|
643 | }
|
---|
644 |
|
---|
645 | void GLWorldScene::removeShape(const std::string &_name)
|
---|
646 | {
|
---|
647 | ShapeNodeMap::iterator iter = ShapesinSceneMap.find(_name);
|
---|
648 | ASSERT(iter != ShapesinSceneMap.end(),
|
---|
649 | "GLWorldScene::removeShape() - shape "+_name+" not in scene.");
|
---|
650 | ShapesinSceneMap.erase(iter);
|
---|
651 | delete(iter->second);
|
---|
652 |
|
---|
653 | emit changed();
|
---|
654 | }
|
---|
655 |
|
---|
656 | void GLWorldScene::updateSelectedShapes()
|
---|
657 | {
|
---|
658 | foreach (QObject *obj, children()) {
|
---|
659 | GLMoleculeObject_shape *shapeobj = qobject_cast<GLMoleculeObject_shape *>(obj);
|
---|
660 | if (shapeobj){
|
---|
661 | shapeobj->enable(ShapeRegistry::getInstance().isSelected(shapeobj->getShape()));
|
---|
662 | }
|
---|
663 | }
|
---|
664 |
|
---|
665 | emit changed();
|
---|
666 | }
|
---|
667 |
|
---|
668 | void GLWorldScene::initialize(QGLView *view, QGLPainter *painter) const
|
---|
669 | {
|
---|
670 | // Initialize all of the mesh objects that we have as children.
|
---|
671 | foreach (QObject *obj, children()) {
|
---|
672 | GLMoleculeObject *meshobj = qobject_cast<GLMoleculeObject *>(obj);
|
---|
673 | if (meshobj)
|
---|
674 | meshobj->initialize(view, painter);
|
---|
675 | }
|
---|
676 | }
|
---|
677 |
|
---|
678 | void GLWorldScene::draw(QGLPainter *painter, const QVector4D &cameraPlane) const
|
---|
679 | {
|
---|
680 | // Draw all of the mesh objects that we have as children.
|
---|
681 | foreach (QObject *obj, children()) {
|
---|
682 | GLMoleculeObject *meshobj = qobject_cast<GLMoleculeObject *>(obj);
|
---|
683 | if (meshobj)
|
---|
684 | meshobj->draw(painter, cameraPlane);
|
---|
685 | }
|
---|
686 | }
|
---|
687 |
|
---|
688 | void GLWorldScene::setSelectionMode(SelectionModeType mode)
|
---|
689 | {
|
---|
690 | selectionMode = mode;
|
---|
691 | // TODO send update to toolbar
|
---|
692 | }
|
---|
693 |
|
---|
694 | void GLWorldScene::setSelectionModeAtom()
|
---|
695 | {
|
---|
696 | setSelectionMode(SelectAtom);
|
---|
697 | }
|
---|
698 |
|
---|
699 | void GLWorldScene::setSelectionModeMolecule()
|
---|
700 | {
|
---|
701 | setSelectionMode(SelectMolecule);
|
---|
702 | }
|
---|
703 |
|
---|