source: src/UIElements/Views/Qt4/Qt3D/GLWorldScene.cpp@ 73b13c

Action_Thermostats Add_AtomRandomPerturbation Add_FitFragmentPartialChargesAction Add_RotateAroundBondAction Add_SelectAtomByNameAction Added_ParseSaveFragmentResults AddingActions_SaveParseParticleParameters Adding_Graph_to_ChangeBondActions Adding_MD_integration_tests Adding_ParticleName_to_Atom Adding_StructOpt_integration_tests AtomFragments Automaking_mpqc_open AutomationFragmentation_failures Candidate_v1.5.4 Candidate_v1.6.0 Candidate_v1.6.1 ChangeBugEmailaddress ChangingTestPorts ChemicalSpaceEvaluator CombiningParticlePotentialParsing Combining_Subpackages Debian_Package_split Debian_package_split_molecuildergui_only Disabling_MemDebug Docu_Python_wait EmpiricalPotential_contain_HomologyGraph EmpiricalPotential_contain_HomologyGraph_documentation Enable_parallel_make_install Enhance_userguide Enhanced_StructuralOptimization Enhanced_StructuralOptimization_continued Example_ManyWaysToTranslateAtom Exclude_Hydrogens_annealWithBondGraph FitPartialCharges_GlobalError Fix_BoundInBox_CenterInBox_MoleculeActions Fix_ChargeSampling_PBC Fix_ChronosMutex Fix_FitPartialCharges Fix_FitPotential_needs_atomicnumbers Fix_ForceAnnealing Fix_IndependentFragmentGrids Fix_ParseParticles Fix_ParseParticles_split_forward_backward_Actions Fix_PopActions Fix_QtFragmentList_sorted_selection Fix_Restrictedkeyset_FragmentMolecule Fix_StatusMsg Fix_StepWorldTime_single_argument Fix_Verbose_Codepatterns Fix_fitting_potentials Fixes ForceAnnealing_goodresults ForceAnnealing_oldresults ForceAnnealing_tocheck ForceAnnealing_with_BondGraph ForceAnnealing_with_BondGraph_continued ForceAnnealing_with_BondGraph_continued_betteresults ForceAnnealing_with_BondGraph_contraction-expansion FragmentAction_writes_AtomFragments FragmentMolecule_checks_bonddegrees GeometryObjects Gui_Fixes Gui_displays_atomic_force_velocity ImplicitCharges IndependentFragmentGrids IndependentFragmentGrids_IndividualZeroInstances IndependentFragmentGrids_IntegrationTest IndependentFragmentGrids_Sole_NN_Calculation JobMarket_RobustOnKillsSegFaults JobMarket_StableWorkerPool JobMarket_unresolvable_hostname_fix MoreRobust_FragmentAutomation ODR_violation_mpqc_open PartialCharges_OrthogonalSummation PdbParser_setsAtomName PythonUI_with_named_parameters QtGui_reactivate_TimeChanged_changes Recreated_GuiChecks Rewrite_FitPartialCharges RotateToPrincipalAxisSystem_UndoRedo SaturateAtoms_findBestMatching SaturateAtoms_singleDegree StoppableMakroAction Subpackage_CodePatterns Subpackage_JobMarket Subpackage_LinearAlgebra Subpackage_levmar Subpackage_mpqc_open Subpackage_vmg Switchable_LogView ThirdParty_MPQC_rebuilt_buildsystem TrajectoryDependenant_MaxOrder TremoloParser_IncreasedPrecision TremoloParser_MultipleTimesteps TremoloParser_setsAtomName Ubuntu_1604_changes stable
Last change on this file since 73b13c was 73b13c, checked in by Frederik Heber <heber@…>, 10 years ago

Extracted signing on to molecule into activateObserver.

  • is called by GLWorldScene after all "missed" signals have been emitted.
  • Property mode set to 100644
File size: 16.5 KB
Line 
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
70using namespace MoleCuilder;
71
72GLWorldScene::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 setSelectionMode(SelectAtom);
95
96 init();
97}
98
99GLWorldScene::~GLWorldScene()
100{
101 // remove all elements
102 GLMoleculeObject::cleanMaterialMap();
103}
104
105/** Initialise the WorldScene with molecules and atoms from World.
106 *
107 */
108void GLWorldScene::init()
109{
110// const std::vector<const molecule *> &molecules =
111// const_cast<const World &>(World::getInstance()).getAllMolecules();
112//
113// for (std::vector<const molecule*>::const_iterator moliter = molecules.begin();
114// moliter != molecules.end();
115// moliter++) {
116// // create molecule objects in scene
117// moleculeInserted((*moliter)->getId());
118// }
119}
120
121/** Update the WorldScene with molecules and atoms from World.
122 *
123 * This function should be called after e.g. WorldTime::TimeChanged was
124 * received or after another molecule has been loaded.
125 *
126 */
127void GLWorldScene::update()
128{
129 const std::vector<const molecule *> &molecules =
130 const_cast<const World &>(World::getInstance()).getAllMolecules();
131
132 for (std::vector<const molecule*>::const_iterator moliter = molecules.begin();
133 moliter != molecules.end();
134 moliter++) {
135 // check whether molecule already exists
136 const moleculeId_t molid = (*moliter)->getId();
137 const bool mol_present = MoleculesinSceneMap.count(molid);
138 if (!mol_present)
139 moleculeInserted((*moliter)->getId());
140 }
141
142 MoleculeNodeMap::iterator iter = MoleculesinSceneMap.begin();
143 for (;iter != MoleculesinSceneMap.end();) {
144 const moleculeId_t molid = iter->first;
145 const molecule * const mol = const_cast<const World &>(World::getInstance()).
146 getMolecule(MoleculeById(molid));
147 const bool mol_absent = (mol == NULL);
148 // step on to next molecule before possibly removing entry and invalidating iter
149 ++iter;
150 if (mol_absent)
151 moleculeRemoved(molid);
152 }
153
154}
155
156void GLWorldScene::atomClicked(atomId_t no)
157{
158 LOG(3, "INFO: GLMoleculeObject_molecule - atom " << no << " has been clicked.");
159 const atom * const Walker = const_cast<const World &>(World::getInstance()).
160 getAtom(AtomById(no));
161 if (selectionMode == SelectAtom){
162 if (!World::getInstance().isSelected(Walker))
163 SelectionAtomById(std::vector<atomId_t>(1,no));
164 else
165 SelectionNotAtomById(std::vector<atomId_t>(1,no));
166 }else if (selectionMode == SelectMolecule){
167 const molecule *mol = Walker->getMolecule();
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 }
175 emit clicked(no);
176}
177
178void GLWorldScene::moleculeClicked(moleculeId_t no)
179{
180 LOG(3, "INFO: GLMoleculeObject_molecule - mol " << no << " has been clicked.");
181 const molecule * const mol= const_cast<const World &>(World::getInstance()).
182 getMolecule(MoleculeById(no));
183 ASSERT(mol, "Atom without molecule has been clicked.");
184 molids_t ids(1, mol->getId());
185 if (!World::getInstance().isSelected(mol))
186 SelectionMoleculeById(ids);
187 else
188 SelectionNotMoleculeById(ids);
189 emit clicked(no);
190}
191
192/** ....
193 *
194 */
195void GLWorldScene::worldSelectionChanged()
196{
197 LOG(3, "INFO: GLWorldScene: Received signal selectionChanged.");
198
199 const std::vector<const molecule*> &molecules =
200 const_cast<const World &>(World::getInstance()).getAllMolecules();
201
202 if (molecules.size() > 0) {
203 for (std::vector<const molecule*>::const_iterator Runner = molecules.begin();
204 Runner != molecules.end();
205 Runner++) {
206
207 // molecule selected but not in scene?
208 const bool isSelected =
209 const_cast<const World &>(World::getInstance()).isSelected(*Runner);
210 if (isSelected){
211 MoleculeNodeMap::iterator iter = MoleculesinSceneMap.find((*Runner)->getId());
212 ASSERT( iter != MoleculesinSceneMap.end(),
213 "GLWorldScene::worldSelectionChanged() - selected molecule is unknown.");
214 GLMoleculeObject_molecule *molObject = iter->second;
215 // inform molecule object
216 molObject->selected(isSelected);
217 }
218 }
219 }
220}
221
222/** Inserts an atom into the scene before molecule is present.
223 *
224 * @param _molid molecule to insert atom for
225 * @param _atomid atom to insert
226 */
227void GLWorldScene::atomInserted(const moleculeId_t _molid, const atomId_t _atomid)
228{
229 LOG(3, "INFO: GLWorldScene: Received signal atomInserted for atom "+toString(_atomid)+".");
230
231 boost::recursive_mutex::scoped_lock lock(MoleculeMissedStateMap_mutex);
232
233 // check of molecule is already present
234 if (MoleculesinSceneMap.count(_molid) != 0) {
235 // pass signal through
236 } else {
237 // store signal for when it is instantiated
238 if (MoleculeMissedStateMap.count(_molid) == 0)
239 MoleculeMissedStateMap.insert( std::make_pair(_molid ,StateChangeMap_t()) );
240 MoleculeMissedStateMap[_molid].insert( std::make_pair(_atomid, atomInsertedState) );
241 }
242}
243
244/** Removes an atom into the scene before molecule is present.
245 *
246 * @param _molid molecule to insert atom for
247 * @param _atomid atom to insert
248 */
249void GLWorldScene::atomRemoved(const moleculeId_t _molid, const atomId_t _atomid)
250{
251 LOG(3, "INFO: GLWorldScene: Received signal atomRemoved for atom "+toString(_atomid)+".");
252
253 boost::recursive_mutex::scoped_lock lock(MoleculeMissedStateMap_mutex);
254
255 // check of molecule is already present
256 if (MoleculesinSceneMap.count(_molid) != 0) {
257 // pass signal through
258 } else {
259 // store signal for when it is instantiated
260 if (MoleculeMissedStateMap.count(_molid) == 0)
261 MoleculeMissedStateMap.insert( std::make_pair(_molid ,StateChangeMap_t()) );
262 MoleculeMissedStateMap[_molid].insert( std::make_pair(_atomid, atomRemovedState) );
263 }
264}
265
266/** Inserts a molecule into the scene.
267 *
268 * @param _mol molecule to insert
269 */
270void GLWorldScene::moleculeInserted(const moleculeId_t _id)
271{
272 LOG(3, "INFO: GLWorldScene: Received signal moleculeInserted for molecule "+toString(_id)+".");
273 MoleculeNodeMap::const_iterator iter = MoleculesinSceneMap.find(_id);
274 ASSERT( iter == MoleculesinSceneMap.end(),
275 "GLWorldScene::moleculeInserted() - molecule's id "+toString(_id)+" already present.");
276
277 // add new object
278 GLMoleculeObject_molecule *molObject = new GLMoleculeObject_molecule(GLMoleculeObject::meshEmpty, this, _id);
279 ASSERT( molObject != NULL,
280 "GLWorldScene::moleculeInserted - could not create molecule object for "+toString(_id));
281 MoleculesinSceneMap.insert( make_pair(_id, molObject) );
282
283 // now handle all state changes that came up before the instantiation7
284 while (MoleculeMissedStateMap.count(_id) != 0) {
285 ASSERT( !MoleculeMissedStateMap[_id].empty(),
286 "GLWorldScene::moleculeInserted() - we have an empty state change map for molecule with id "
287 +toString(_id));
288 boost::recursive_mutex::scoped_lock lock(MoleculeMissedStateMap_mutex);
289 for (StateChangeMap_t::iterator iter = MoleculeMissedStateMap[_id].begin();
290 !MoleculeMissedStateMap[_id].empty();
291 iter = MoleculeMissedStateMap[_id].begin()) {
292 std::pair<StateChangeMap_t::iterator, StateChangeMap_t::iterator> rangeiter =
293 MoleculeMissedStateMap[_id].equal_range(iter->first);
294 const size_t StateCounts = std::distance(rangeiter.first, rangeiter.second);
295 if (StateCounts > 1) {
296 // more than one state change, have to combine
297 typedef std::map<StateChangeType, size_t> StateChangeAmounts_t;
298 StateChangeAmounts_t StateChangeAmounts;
299 for (StateChangeMap_t::const_iterator stateiter = rangeiter.first;
300 stateiter != rangeiter.second; ++stateiter)
301 ++StateChangeAmounts[stateiter->second];
302 ASSERT( StateChangeAmounts[atomInsertedState] >= StateChangeAmounts[atomRemovedState],
303 "GLWorldScene::moleculeInserted() - more atomRemoved states than atomInserted for atom "
304 +toString(iter->first));
305 if (StateChangeAmounts[atomInsertedState] > StateChangeAmounts[atomRemovedState]) {
306 LOG(1, "INFO: invoking atomInserted for atom " << iter->first);
307 QMetaObject::invokeMethod(molObject, // pointer to a QObject
308 "atomInserted", // member name (no parameters here)
309 Qt::DirectConnection, // connection type
310 Q_ARG(const atomId_t, iter->first)); // parameters
311 } else {
312 LOG(1, "INFO: Atom " << iter->first << " has been inserted and removed already.");
313 }
314 } else {
315 // can only be an insertion
316 switch (rangeiter.first->second) {
317 case atomRemovedState:
318 ASSERT( 0,
319 "GLWorldScene::moleculeInserted() - atomRemoved state without atomInserted for atom "
320 +toString(iter->first));
321 break;
322 case atomInsertedState:
323 LOG(1, "INFO: invoking atomInserted for atom " << iter->first);
324 QMetaObject::invokeMethod(molObject, // pointer to a QObject
325 "atomInserted", // member name (no parameters here)
326 Qt::DirectConnection, // connection type
327 Q_ARG(const atomId_t, iter->first)); // parameters
328 break;
329 default:
330 ASSERT( 0,
331 "GLWorldScene::moleculeInserted() - there are unknown change states.");
332 break;
333 }
334 }
335 // removed state changes for this atom
336 MoleculeMissedStateMap[_id].erase(iter);
337 }
338 // remove state change map for the molecule
339 MoleculeMissedStateMap.erase(_id);
340 }
341
342 // now let the molObject sign on to molecule
343 molObject->activateObserver();
344
345 connect (molObject, SIGNAL(changed()), this, SIGNAL(changed()));
346 connect (molObject, SIGNAL(changeOccured()), this, SIGNAL(changeOccured()));
347 connect (molObject, SIGNAL(atomClicked(atomId_t)), this, SLOT(atomClicked(atomId_t)));
348 connect (molObject, SIGNAL(moleculeClicked(moleculeId_t)), this, SLOT(moleculeClicked(moleculeId_t)));
349 connect (molObject, SIGNAL(selectionChanged()), this, SIGNAL(changed()));
350 connect (molObject, SIGNAL(selectionChanged()), this, SIGNAL(changed()));
351 connect (molObject, SIGNAL(hoverChanged(const atomId_t)), this, SIGNAL(hoverChanged(const atomId_t)));
352 connect (molObject, SIGNAL(hoverChanged(const moleculeId_t, int)), this, SIGNAL(hoverChanged(const moleculeId_t, int)));
353
354 emit changed();
355 emit changeOccured();
356}
357
358/** Removes a molecule from the scene.
359 *
360 * @param _id id of molecule to remove
361 */
362void GLWorldScene::moleculeRemoved(const moleculeId_t _id)
363{
364 LOG(3, "INFO: GLWorldScene: Received signal moleculeRemoved for molecule "+toString(_id)+".");
365 MoleculeNodeMap::iterator iter = MoleculesinSceneMap.find(_id);
366 ASSERT( iter != MoleculesinSceneMap.end(),
367 "GLWorldScene::moleculeInserted() - molecule's id "+toString(_id)+" is unknown.");
368
369 GLMoleculeObject_molecule *molObject = iter->second;
370 molObject->disconnect();
371 MoleculesinSceneMap.erase(iter);
372 delete molObject;
373
374 // remove any possible state changes left
375 {
376 boost::recursive_mutex::scoped_lock lock(MoleculeMissedStateMap_mutex);
377 MoleculeMissedStateMap.erase(_id);
378 }
379
380 emit changed();
381 emit changeOccured();
382}
383
384void GLWorldScene::moleculesVisibilityChanged(const moleculeId_t _id, bool _visible)
385{
386 MoleculeNodeMap::iterator iter = MoleculesinSceneMap.find(_id);
387 ASSERT( iter != MoleculesinSceneMap.end(),
388 "GLWorldScene::moleculeInserted() - molecule's id "+toString(_id)+" is unknown.");
389
390 GLMoleculeObject_molecule *molObject = iter->second;
391 molObject->setVisible(_visible);
392
393 emit changed();
394 emit changeOccured();
395}
396
397/** Adds a shape to the scene.
398 *
399 * uses ShapeRegistry::lastChanged()
400 *
401 */
402void GLWorldScene::addShape()
403{
404 Shape &shape = *ShapeRegistry::getInstance().lastChanged();
405 GLMoleculeObject_shape *shapeObject = new GLMoleculeObject_shape(shape, this);
406 ShapeNodeMap::iterator iter = ShapesinSceneMap.find(shape.getName());
407 ASSERT(iter == ShapesinSceneMap.end(),
408 "GLWorldScene::addShape() - same shape "+shape.getName()+" added again.");
409 ShapesinSceneMap.insert( make_pair(shape.getName(), shapeObject) );
410}
411
412void GLWorldScene::removeShape()
413{
414 Shape &shape = *ShapeRegistry::getInstance().lastChanged();
415 ShapeNodeMap::iterator iter = ShapesinSceneMap.find(shape.getName());
416 ASSERT(iter != ShapesinSceneMap.end(),
417 "GLWorldScene::removeShape() - shape "+shape.getName()+" not in scene.");
418 ShapesinSceneMap.erase(iter);
419 delete(iter->second);
420}
421
422void GLWorldScene::updateSelectedShapes()
423{
424 foreach (QObject *obj, children()) {
425 GLMoleculeObject_shape *shapeobj = qobject_cast<GLMoleculeObject_shape *>(obj);
426 if (shapeobj){
427 shapeobj->enable(ShapeRegistry::getInstance().isSelected(shapeobj->getShape()));
428 }
429 }
430}
431
432void GLWorldScene::initialize(QGLView *view, QGLPainter *painter) const
433{
434 // Initialize all of the mesh objects that we have as children.
435 foreach (QObject *obj, children()) {
436 GLMoleculeObject *meshobj = qobject_cast<GLMoleculeObject *>(obj);
437 if (meshobj)
438 meshobj->initialize(view, painter);
439 }
440}
441
442void GLWorldScene::draw(QGLPainter *painter, const QVector4D &cameraPlane) const
443{
444 // Draw all of the mesh objects that we have as children.
445 foreach (QObject *obj, children()) {
446 GLMoleculeObject *meshobj = qobject_cast<GLMoleculeObject *>(obj);
447 if (meshobj)
448 meshobj->draw(painter, cameraPlane);
449 }
450}
451
452void GLWorldScene::setSelectionMode(SelectionModeType mode)
453{
454 selectionMode = mode;
455 // TODO send update to toolbar
456}
457
458void GLWorldScene::setSelectionModeAtom()
459{
460 setSelectionMode(SelectAtom);
461}
462
463void GLWorldScene::setSelectionModeMolecule()
464{
465 setSelectionMode(SelectMolecule);
466}
Note: See TracBrowser for help on using the repository browser.