source: src/UIElements/Views/Qt4/Qt3D/GLWorldScene.cpp@ 4f7f0bf

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 4f7f0bf was 407638e, checked in by Michael Ankele <ankele@…>, 13 years ago

GL: Qt signals rewired

  • GLWorldView now tells us, over which atom the mouse is hovering
  • Property mode set to 100644
File size: 11.4 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 //bondsChanged(_atom);
146 emit changeOccured();
147}
148
149/** Removes an atom from the scene.
150 *
151 * @param _atom atom to remove
152 */
153void GLWorldScene::atomRemoved(const atom *_atom)
154{
155 LOG(3, "INFO: GLWorldScene: Received signal atomRemoved for atom "+toString(_atom->getId())+".");
156 // bonds are removed by signal coming from ~bond
157 // remove atoms
158 AtomNodeMap::iterator iter = AtomsinSceneMap.find(_atom->getId());
159 ASSERT(iter != AtomsinSceneMap.end(),
160 "GLWorldScene::atomRemoved() - atom "+_atom->getName()+" not on display.");
161 GLMoleculeObject_atom *atomObject = iter->second;
162 atomObject->disconnect();
163 AtomsinSceneMap.erase(iter);
164 delete atomObject;
165 emit changeOccured();
166}
167
168/** ....
169 *
170 */
171void GLWorldScene::worldSelectionChanged()
172{
173 LOG(3, "INFO: GLWorldScene: Received signal selectionChanged.");
174
175 const std::vector<molecule*> &molecules = World::getInstance().getAllMolecules();
176
177 if (molecules.size() > 0) {
178 for (std::vector<molecule*>::const_iterator Runner = molecules.begin();
179 Runner != molecules.end();
180 Runner++) {
181
182 MoleculeNodeMap::iterator iter = MoleculesinSceneMap.find((*Runner)->getId());
183 bool isSelected = World::getInstance().isSelected(*Runner);
184
185 // molecule selected but not in scene?
186 if (isSelected && (iter == MoleculesinSceneMap.end())){
187 // -> create new mesh
188 GLMoleculeObject_molecule *molObject = new GLMoleculeObject_molecule(meshEmpty, this, *Runner);
189 MoleculesinSceneMap.insert( make_pair((*Runner)->getId(), molObject) );
190 connect (molObject, SIGNAL(changed()), this, SIGNAL(changed()));
191 connect (molObject, SIGNAL(selectionChanged()), this, SIGNAL(changed()));
192 connect (molObject, SIGNAL(selectionChanged()), this, SIGNAL(changed()));
193 emit changed();
194 emit changeOccured();
195 }
196
197 // molecule not selected but in scene?
198 if (!isSelected && (iter != MoleculesinSceneMap.end())){
199 // -> remove from scene
200 moleculeRemoved(*Runner);
201 }
202
203 }
204 }
205}
206
207/** Removes a molecule from the scene.
208 *
209 * @param _molecule molecule to remove
210 */
211void GLWorldScene::moleculeRemoved(const molecule *_molecule)
212{
213 LOG(3, "INFO: GLWorldScene: Received signal moleculeRemoved for molecule "+toString(_molecule->getId())+".");
214 MoleculeNodeMap::iterator iter = MoleculesinSceneMap.find(_molecule->getId());
215
216 // only remove if the molecule is in the scene
217 // (= is selected)
218 if (iter != MoleculesinSceneMap.end()){
219 GLMoleculeObject_molecule *molObject = iter->second;
220 molObject->disconnect();
221 MoleculesinSceneMap.erase(iter);
222 delete molObject;
223 emit changed();
224 emit changeOccured();
225 }
226}
227
228/** Adds a bond to the scene.
229 *
230 * @param _bond bond to add
231 * @param side which side of the bond (left or right)
232 */
233void GLWorldScene::bondInserted(const bond *_bond, const enum GLMoleculeObject_bond::SideOfBond side)
234{
235 LOG(3, "INFO: GLWorldScene::bondInserted() - Adding bond "+toString(*_bond)+".");
236 //LOG(4, "INFO: Currently present bonds " << BondsinSceneMap << ".");
237
238 BondIds ids;
239 switch (side) {
240 case GLMoleculeObject_bond::left:
241 ids = std::make_pair(_bond->leftatom->getId(), _bond->rightatom->getId());
242 break;
243 case GLMoleculeObject_bond::right:
244 ids = std::make_pair(_bond->rightatom->getId(), _bond->leftatom->getId());
245 break;
246 }
247#ifndef NDEBUG
248 BondNodeMap::iterator iter = BondsinSceneMap.find(ids);
249 ASSERT(iter == BondsinSceneMap.end(),
250 "GLWorldScene::bondAdded() - same left-sided bond "+toString(*_bond)+" added again.");
251#endif
252 GLMoleculeObject_bond *bondObject =
253 new GLMoleculeObject_bond(meshCylinderHi, this, _bond, side);
254 connect (
255 bondObject, SIGNAL(BondRemoved(const atomId_t, const atomId_t)),
256 this, SLOT(bondRemoved(const atomId_t, const atomId_t)));
257 connect (bondObject, SIGNAL(changed()), this, SIGNAL(changed()));
258 BondsinSceneMap.insert( make_pair(ids, bondObject) );
259// BondIdsinSceneMap.insert( Leftids );
260 emit changeOccured();
261}
262
263/** Removes a bond from the scene.
264 *
265 * @param _bond bond to remove
266 */
267void GLWorldScene::bondRemoved(const atomId_t leftnr, const atomId_t rightnr)
268{
269 LOG(3, "INFO: GLWorldScene::bondRemoved() - Removing bond between "+toString(leftnr)+" and "+toString(rightnr)+".");
270 {
271 // left bond
272 const BondIds Leftids( make_pair(leftnr, rightnr) );
273 BondNodeMap::iterator leftiter = BondsinSceneMap.find( Leftids );
274 ASSERT(leftiter != BondsinSceneMap.end(),
275 "GLWorldScene::bondRemoved() - bond "+toString(leftnr)+"-"
276 +toString(rightnr)+" not on display.");
277 //GLMoleculeObject_bond *bondObject = leftiter->second;
278 BondsinSceneMap.erase(leftiter);
279 //delete bondObject; // is done by signal from bond itself
280 //LOG(4, "INFO: Still present bonds " << BondsinSceneMap << ".");
281 }
282
283 emit changeOccured();
284}
285
286void GLWorldScene::initialize(QGLView *view, QGLPainter *painter) const
287{
288 // Initialize all of the mesh objects that we have as children.
289 foreach (QObject *obj, children()) {
290 GLMoleculeObject *meshobj = qobject_cast<GLMoleculeObject *>(obj);
291 if (meshobj)
292 meshobj->initialize(view, painter);
293 }
294}
295
296void GLWorldScene::draw(QGLPainter *painter) const
297{
298 // Draw all of the mesh objects that we have as children.
299 foreach (QObject *obj, children()) {
300 GLMoleculeObject *meshobj = qobject_cast<GLMoleculeObject *>(obj);
301 if (meshobj)
302 meshobj->draw(painter);
303 }
304}
305
306void GLWorldScene::atomClicked(atomId_t no)
307{
308 LOG(3, "INFO: GLWorldScene - atom " << no << " has been clicked.");
309 const atom *Walker = World::getInstance().getAtom(AtomById(no));
310 if (selectionMode == SelectAtom){
311 if (!World::getInstance().isSelected(Walker))
312 SelectionAtomById(no);
313 else
314 SelectionNotAtomById(no);
315 }else if (selectionMode == SelectMolecule){
316 const molecule *mol = Walker->getMolecule();
317 ASSERT(mol, "Atom without molecule has been clicked.");
318 if (!World::getInstance().isSelected(mol))
319 SelectionMoleculeById(mol->getId());
320 else
321 SelectionNotMoleculeById(mol->getId());
322 }
323 emit clicked(no);
324}
325
326void GLWorldScene::setSelectionMode(SelectionModeType mode)
327{
328 selectionMode = mode;
329 // TODO send update to toolbar
330}
331
332void GLWorldScene::setSelectionModeAtom()
333{
334 setSelectionMode(SelectAtom);
335}
336
337void GLWorldScene::setSelectionModeMolecule()
338{
339 setSelectionMode(SelectMolecule);
340}
341
342void GLWorldScene::hoverChangedSignalled(GLMoleculeObject *ob)
343{
344 // Find the atom, ob corresponds to.
345 hoverAtom = NULL;
346 GLMoleculeObject_atom *atomObject = dynamic_cast<GLMoleculeObject_atom *>(ob);
347 if (atomObject){
348 for (AtomNodeMap::iterator iter = AtomsinSceneMap.begin();iter != AtomsinSceneMap.end(); ++ iter){
349 if (iter->second == atomObject)
350 hoverAtom = World::getInstance().getAtom(AtomById(iter->first));
351 }
352 }
353
354 // Propagate signal.
355 emit hoverChanged(hoverAtom);
356}
357
Note: See TracBrowser for help on using the repository browser.