source: src/UIElements/Views/Qt4/Qt3D/GLWorldScene.cpp@ 0eb7bf3

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 0eb7bf3 was 30cd0d, checked in by Michael Ankele <ankele@…>, 13 years ago

GLWorldScene handles IndexChanged message from atoms

  • Property mode set to 100644
File size: 12.1 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 * Please see the LICENSE file or "Copyright notice" in builder.cpp for details.
6 */
7
8/*
9 * GLWorldScene.cpp
10 *
11 * This is based on the Qt3D example "teaservice", specifically parts of teaservice.cpp.
12 *
13 * Created on: Aug 17, 2011
14 * Author: heber
15 */
16
17// include config.h
18#ifdef HAVE_CONFIG_H
19#include <config.h>
20#endif
21
22#include "GLWorldScene.hpp"
23#include <Qt3D/qglview.h>
24#include <Qt3D/qglbuilder.h>
25#include <Qt3D/qglscenenode.h>
26#include <Qt3D/qglsphere.h>
27#include <Qt3D/qglcylinder.h>
28
29#include "GLMoleculeObject.hpp"
30#include "GLMoleculeObject_atom.hpp"
31#include "GLMoleculeObject_bond.hpp"
32#include "GLMoleculeObject_molecule.hpp"
33
34#include "CodePatterns/MemDebug.hpp"
35
36#include "CodePatterns/Log.hpp"
37
38#include "Actions/SelectionAction/Atoms/AtomByIdAction.hpp"
39#include "Actions/SelectionAction/Atoms/NotAtomByIdAction.hpp"
40#include "Actions/SelectionAction/Molecules/MoleculeByIdAction.hpp"
41#include "Actions/SelectionAction/Molecules/NotMoleculeByIdAction.hpp"
42#include "Atom/atom.hpp"
43#include "Bond/bond.hpp"
44#include "Descriptors/AtomIdDescriptor.hpp"
45#include "Helpers/helpers.hpp"
46#include "molecule.hpp"
47#include "World.hpp"
48
49#include <iostream>
50
51using namespace MoleCuilder;
52
53std::ostream &operator<<(std::ostream &ost, const GLWorldScene::BondIds &t)
54{
55 ost << t.first << "," << t.second;
56 return ost;
57}
58
59GLWorldScene::GLWorldScene(QObject *parent)
60 : QObject(parent),
61 hoverAtom(NULL)
62{
63 QGLBuilder builder0;
64 meshEmpty = builder0.finalizedSceneNode();
65 QGLBuilder builder1;
66 builder1 << QGLSphere(2.0, 5);
67 meshSphereHi = builder1.finalizedSceneNode();
68 QGLBuilder builder2;
69 builder2 << QGLSphere(2.0, 1);
70 meshSphereLo = builder2.finalizedSceneNode();
71 QGLBuilder builder3;
72 builder3 << QGLCylinder(.25,.25,1.0,16);
73 meshCylinderHi = builder3.finalizedSceneNode();
74 QGLBuilder builder4;
75 builder4 << QGLCylinder(.25,.25,1.0,16);
76 meshCylinderLo = builder4.finalizedSceneNode();
77
78 meshSphereHi->setOption(QGLSceneNode::CullBoundingBox, true);
79 meshSphereLo->setOption(QGLSceneNode::CullBoundingBox, true);
80 meshCylinderHi->setOption(QGLSceneNode::CullBoundingBox, true);
81 meshCylinderLo->setOption(QGLSceneNode::CullBoundingBox, true);
82
83 setSelectionMode(SelectAtom);
84
85 init();
86}
87
88GLWorldScene::~GLWorldScene()
89{
90 // remove all elements
91 GLMoleculeObject::cleanMaterialMap();
92}
93
94/** Initialise the WorldScene with molecules and atoms from World.
95 *
96 */
97void GLWorldScene::init()
98{
99 const std::vector<molecule*> &molecules = World::getInstance().getAllMolecules();
100
101 if (molecules.size() > 0) {
102 for (std::vector<molecule*>::const_iterator Runner = molecules.begin();
103 Runner != molecules.end();
104 Runner++) {
105
106 for (molecule::const_iterator atomiter = (*Runner)->begin();
107 atomiter != (*Runner)->end();
108 ++atomiter) {
109 // create atom objects in scene
110 atomInserted(*atomiter);
111
112 // create bond objects in scene
113 const BondList &bondlist = (*atomiter)->getListOfBonds();
114 for (BondList::const_iterator bonditer = bondlist.begin();
115 bonditer != bondlist.end();
116 ++bonditer) {
117 const bond *_bond = *bonditer;
118 const GLMoleculeObject_bond::SideOfBond side = (_bond->leftatom == *atomiter) ?
119 GLMoleculeObject_bond::left : GLMoleculeObject_bond::right;
120 bondInserted(_bond, side);
121 }
122 }
123 }
124 }
125}
126
127/** Adds an atom to the scene.
128 *
129 * @param _atom atom to add
130 */
131void GLWorldScene::atomInserted(const atom *_atom)
132{
133 LOG(3, "INFO: GLWorldScene: Received signal atomInserted for atom "+toString(_atom->getId())+".");
134 GLMoleculeObject_atom *atomObject = new GLMoleculeObject_atom(meshSphereHi, this, _atom);
135 AtomNodeMap::iterator iter = AtomsinSceneMap.find(_atom->getId());
136 ASSERT(iter == AtomsinSceneMap.end(),
137 "GLWorldScene::atomAdded() - same atom "+_atom->getName()+" added again.");
138 AtomsinSceneMap.insert( make_pair(_atom->getId(), atomObject) );
139 connect (atomObject, SIGNAL(clicked(atomId_t)), this, SLOT(atomClicked(atomId_t)));
140 connect (atomObject, SIGNAL(changed()), this, SIGNAL(changed()));
141 connect (atomObject, SIGNAL(hoverChanged(GLMoleculeObject *)), this, SIGNAL(changed()));
142 connect (atomObject, SIGNAL(hoverChanged(GLMoleculeObject *)), this, SLOT(hoverChangedSignalled(GLMoleculeObject *)));
143 connect (atomObject, SIGNAL(selectionChanged()), this, SIGNAL(changed()));
144 connect (atomObject, SIGNAL(BondsInserted(const bond *, const GLMoleculeObject_bond::SideOfBond)), this, SLOT(bondInserted(const bond *, const GLMoleculeObject_bond::SideOfBond)));
145 connect (atomObject, SIGNAL(indexChanged(GLMoleculeObject_atom*, int, int)), this, SLOT(changeAtomId(GLMoleculeObject_atom*, int, int)));
146 //bondsChanged(_atom);
147 emit changeOccured();
148}
149
150/** Removes an atom from the scene.
151 *
152 * @param _atom atom to remove
153 */
154void GLWorldScene::atomRemoved(const atom *_atom)
155{
156 LOG(3, "INFO: GLWorldScene: Received signal atomRemoved for atom "+toString(_atom->getId())+".");
157 // bonds are removed by signal coming from ~bond
158 // remove atoms
159 AtomNodeMap::iterator iter = AtomsinSceneMap.find(_atom->getId());
160 ASSERT(iter != AtomsinSceneMap.end(),
161 "GLWorldScene::atomRemoved() - atom "+_atom->getName()+" not on display.");
162 GLMoleculeObject_atom *atomObject = iter->second;
163 atomObject->disconnect();
164 AtomsinSceneMap.erase(iter);
165 delete atomObject;
166 emit changeOccured();
167}
168
169/** ....
170 *
171 */
172void GLWorldScene::worldSelectionChanged()
173{
174 LOG(3, "INFO: GLWorldScene: Received signal selectionChanged.");
175
176 const std::vector<molecule*> &molecules = World::getInstance().getAllMolecules();
177
178 if (molecules.size() > 0) {
179 for (std::vector<molecule*>::const_iterator Runner = molecules.begin();
180 Runner != molecules.end();
181 Runner++) {
182
183 MoleculeNodeMap::iterator iter = MoleculesinSceneMap.find((*Runner)->getId());
184 bool isSelected = World::getInstance().isSelected(*Runner);
185
186 // molecule selected but not in scene?
187 if (isSelected && (iter == MoleculesinSceneMap.end())){
188 // -> create new mesh
189 GLMoleculeObject_molecule *molObject = new GLMoleculeObject_molecule(meshEmpty, this, *Runner);
190 MoleculesinSceneMap.insert( make_pair((*Runner)->getId(), molObject) );
191 connect (molObject, SIGNAL(changed()), this, SIGNAL(changed()));
192 connect (molObject, SIGNAL(selectionChanged()), this, SIGNAL(changed()));
193 connect (molObject, SIGNAL(selectionChanged()), this, SIGNAL(changed()));
194 emit changed();
195 emit changeOccured();
196 }
197
198 // molecule not selected but in scene?
199 if (!isSelected && (iter != MoleculesinSceneMap.end())){
200 // -> remove from scene
201 moleculeRemoved(*Runner);
202 }
203
204 }
205 }
206}
207
208/** Removes a molecule from the scene.
209 *
210 * @param _molecule molecule to remove
211 */
212void GLWorldScene::moleculeRemoved(const molecule *_molecule)
213{
214 LOG(3, "INFO: GLWorldScene: Received signal moleculeRemoved for molecule "+toString(_molecule->getId())+".");
215 MoleculeNodeMap::iterator iter = MoleculesinSceneMap.find(_molecule->getId());
216
217 // only remove if the molecule is in the scene
218 // (= is selected)
219 if (iter != MoleculesinSceneMap.end()){
220 GLMoleculeObject_molecule *molObject = iter->second;
221 molObject->disconnect();
222 MoleculesinSceneMap.erase(iter);
223 delete molObject;
224 emit changed();
225 emit changeOccured();
226 }
227}
228
229/** Adds a bond to the scene.
230 *
231 * @param _bond bond to add
232 * @param side which side of the bond (left or right)
233 */
234void GLWorldScene::bondInserted(const bond *_bond, const enum GLMoleculeObject_bond::SideOfBond side)
235{
236 LOG(3, "INFO: GLWorldScene::bondInserted() - Adding bond "+toString(*_bond)+".");
237 //LOG(4, "INFO: Currently present bonds " << BondsinSceneMap << ".");
238
239 BondIds ids;
240 switch (side) {
241 case GLMoleculeObject_bond::left:
242 ids = std::make_pair(_bond->leftatom->getId(), _bond->rightatom->getId());
243 break;
244 case GLMoleculeObject_bond::right:
245 ids = std::make_pair(_bond->rightatom->getId(), _bond->leftatom->getId());
246 break;
247 }
248#ifndef NDEBUG
249 BondNodeMap::iterator iter = BondsinSceneMap.find(ids);
250 ASSERT(iter == BondsinSceneMap.end(),
251 "GLWorldScene::bondAdded() - same left-sided bond "+toString(*_bond)+" added again.");
252#endif
253 GLMoleculeObject_bond *bondObject =
254 new GLMoleculeObject_bond(meshCylinderHi, this, _bond, side);
255 connect (
256 bondObject, SIGNAL(BondRemoved(const atomId_t, const atomId_t)),
257 this, SLOT(bondRemoved(const atomId_t, const atomId_t)));
258 connect (bondObject, SIGNAL(changed()), this, SIGNAL(changed()));
259 BondsinSceneMap.insert( make_pair(ids, bondObject) );
260// BondIdsinSceneMap.insert( Leftids );
261 emit changeOccured();
262}
263
264/** Removes a bond from the scene.
265 *
266 * @param _bond bond to remove
267 */
268void GLWorldScene::bondRemoved(const atomId_t leftnr, const atomId_t rightnr)
269{
270 LOG(3, "INFO: GLWorldScene::bondRemoved() - Removing bond between "+toString(leftnr)+" and "+toString(rightnr)+".");
271 {
272 // left bond
273 const BondIds Leftids( make_pair(leftnr, rightnr) );
274 BondNodeMap::iterator leftiter = BondsinSceneMap.find( Leftids );
275 ASSERT(leftiter != BondsinSceneMap.end(),
276 "GLWorldScene::bondRemoved() - bond "+toString(leftnr)+"-"
277 +toString(rightnr)+" not on display.");
278 //GLMoleculeObject_bond *bondObject = leftiter->second;
279 BondsinSceneMap.erase(leftiter);
280 //delete bondObject; // is done by signal from bond itself
281 //LOG(4, "INFO: Still present bonds " << BondsinSceneMap << ".");
282 }
283
284 emit changeOccured();
285}
286
287void GLWorldScene::initialize(QGLView *view, QGLPainter *painter) const
288{
289 // Initialize all of the mesh objects that we have as children.
290 foreach (QObject *obj, children()) {
291 GLMoleculeObject *meshobj = qobject_cast<GLMoleculeObject *>(obj);
292 if (meshobj)
293 meshobj->initialize(view, painter);
294 }
295}
296
297void GLWorldScene::draw(QGLPainter *painter) const
298{
299 // Draw all of the mesh objects that we have as children.
300 foreach (QObject *obj, children()) {
301 GLMoleculeObject *meshobj = qobject_cast<GLMoleculeObject *>(obj);
302 if (meshobj)
303 meshobj->draw(painter);
304 }
305}
306
307void GLWorldScene::atomClicked(atomId_t no)
308{
309 LOG(3, "INFO: GLWorldScene - atom " << no << " has been clicked.");
310 const atom *Walker = World::getInstance().getAtom(AtomById(no));
311 if (selectionMode == SelectAtom){
312 if (!World::getInstance().isSelected(Walker))
313 SelectionAtomById(no);
314 else
315 SelectionNotAtomById(no);
316 }else if (selectionMode == SelectMolecule){
317 const molecule *mol = Walker->getMolecule();
318 ASSERT(mol, "Atom without molecule has been clicked.");
319 if (!World::getInstance().isSelected(mol))
320 SelectionMoleculeById(mol->getId());
321 else
322 SelectionNotMoleculeById(mol->getId());
323 }
324 emit clicked(no);
325}
326
327void GLWorldScene::setSelectionMode(SelectionModeType mode)
328{
329 selectionMode = mode;
330 // TODO send update to toolbar
331}
332
333void GLWorldScene::setSelectionModeAtom()
334{
335 setSelectionMode(SelectAtom);
336}
337
338void GLWorldScene::setSelectionModeMolecule()
339{
340 setSelectionMode(SelectMolecule);
341}
342
343void GLWorldScene::hoverChangedSignalled(GLMoleculeObject *ob)
344{
345 // Find the atom, ob corresponds to.
346 hoverAtom = NULL;
347 GLMoleculeObject_atom *atomObject = dynamic_cast<GLMoleculeObject_atom *>(ob);
348 if (atomObject){
349 for (AtomNodeMap::iterator iter = AtomsinSceneMap.begin();iter != AtomsinSceneMap.end(); ++ iter){
350 if (iter->second == atomObject)
351 hoverAtom = World::getInstance().getAtom(AtomById(iter->first));
352 }
353 }
354
355 // Propagate signal.
356 emit hoverChanged(hoverAtom);
357}
358
359void GLWorldScene::changeAtomId(GLMoleculeObject_atom *ob, int oldId, int newId)
360{
361 LOG(3, "INFO: GLWorldScene - change atom id " << oldId << " to " << newId << ".");
362 // Remove from map.
363 AtomNodeMap::iterator iter = AtomsinSceneMap.find(oldId);
364 ASSERT(iter != AtomsinSceneMap.end(),
365 "GLWorldScene::objectIdChangedSignalled() - atom with id "+toString(oldId)+" not on display.");
366 GLMoleculeObject_atom *atomObject = iter->second;
367 AtomsinSceneMap.erase(iter);
368
369 // Reinsert with new id.
370 AtomsinSceneMap.insert( make_pair(newId, atomObject) );
371}
372
Note: See TracBrowser for help on using the repository browser.