source: src/UIElements/Views/Qt4/Qt3D/GLWorldScene.cpp@ 8923ad8

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 8923ad8 was 8923ad8, checked in by Frederik Heber <heber@…>, 9 years ago

Removed GLMoleculeObject_molecule's init() and reinit().

  • also removed InitAtomMap and associated code.
  • removed changeAtomId slot/signal in GLWorldScene.
  • Property mode set to 100644
File size: 11.6 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{
75 int sphereDetails[] = {5, 3, 2, 0};
76 int cylinderDetails[] = {16, 8, 6, 3};
77 for (int i=0;i<GLMoleculeObject::DETAILTYPES_MAX;i++){
78 QGLBuilder emptyBuilder;
79 GLMoleculeObject::meshEmpty[i] = emptyBuilder.finalizedSceneNode();
80 QGLBuilder sphereBuilder;
81 sphereBuilder << QGLSphere(2.0, sphereDetails[i]);
82 GLMoleculeObject::meshSphere[i] = sphereBuilder.finalizedSceneNode();
83 GLMoleculeObject::meshSphere[i]->setOption(QGLSceneNode::CullBoundingBox, true);
84 QGLBuilder cylinderBuilder;
85 cylinderBuilder << QGLCylinder(.25,.25,1.0,cylinderDetails[i]);
86 GLMoleculeObject::meshCylinder[i] = cylinderBuilder.finalizedSceneNode();
87 GLMoleculeObject::meshCylinder[i]->setOption(QGLSceneNode::CullBoundingBox, true);
88 }
89
90 connect(this, SIGNAL(updated()), this, SLOT(update()));
91
92
93 setSelectionMode(SelectAtom);
94
95 init();
96}
97
98GLWorldScene::~GLWorldScene()
99{
100 // remove all elements
101 GLMoleculeObject::cleanMaterialMap();
102}
103
104/** Initialise the WorldScene with molecules and atoms from World.
105 *
106 */
107void GLWorldScene::init()
108{
109// const std::vector<const molecule *> &molecules =
110// const_cast<const World &>(World::getInstance()).getAllMolecules();
111//
112// for (std::vector<const molecule*>::const_iterator moliter = molecules.begin();
113// moliter != molecules.end();
114// moliter++) {
115// // create molecule objects in scene
116// moleculeInserted((*moliter)->getId());
117// }
118}
119
120/** Update the WorldScene with molecules and atoms from World.
121 *
122 * This function should be called after e.g. WorldTime::TimeChanged was
123 * received or after another molecule has been loaded.
124 *
125 */
126void GLWorldScene::update()
127{
128 const std::vector<const molecule *> &molecules =
129 const_cast<const World &>(World::getInstance()).getAllMolecules();
130
131 for (std::vector<const molecule*>::const_iterator moliter = molecules.begin();
132 moliter != molecules.end();
133 moliter++) {
134 // check whether molecule already exists
135 const moleculeId_t molid = (*moliter)->getId();
136 const bool mol_present = MoleculesinSceneMap.count(molid);
137 if (!mol_present)
138 moleculeInserted((*moliter)->getId());
139 }
140
141 MoleculeNodeMap::iterator iter = MoleculesinSceneMap.begin();
142 for (;iter != MoleculesinSceneMap.end();) {
143 const moleculeId_t molid = iter->first;
144 const molecule * const mol = const_cast<const World &>(World::getInstance()).
145 getMolecule(MoleculeById(molid));
146 const bool mol_absent = (mol == NULL);
147 // step on to next molecule before possibly removing entry and invalidating iter
148 ++iter;
149 if (mol_absent)
150 moleculeRemoved(molid);
151 }
152
153}
154
155void GLWorldScene::atomClicked(atomId_t no)
156{
157 LOG(3, "INFO: GLMoleculeObject_molecule - atom " << no << " has been clicked.");
158 const atom * const Walker = const_cast<const World &>(World::getInstance()).
159 getAtom(AtomById(no));
160 if (selectionMode == SelectAtom){
161 if (!World::getInstance().isSelected(Walker))
162 SelectionAtomById(std::vector<atomId_t>(1,no));
163 else
164 SelectionNotAtomById(std::vector<atomId_t>(1,no));
165 }else if (selectionMode == SelectMolecule){
166 const molecule *mol = Walker->getMolecule();
167 ASSERT(mol, "Atom without molecule has been clicked.");
168 molids_t ids(1, mol->getId());
169 if (!World::getInstance().isSelected(mol))
170 SelectionMoleculeById(ids);
171 else
172 SelectionNotMoleculeById(ids);
173 }
174 emit clicked(no);
175}
176
177void GLWorldScene::moleculeClicked(moleculeId_t no)
178{
179 LOG(3, "INFO: GLMoleculeObject_molecule - mol " << no << " has been clicked.");
180 const molecule * const mol= const_cast<const World &>(World::getInstance()).
181 getMolecule(MoleculeById(no));
182 ASSERT(mol, "Atom without molecule has been clicked.");
183 molids_t ids(1, mol->getId());
184 if (!World::getInstance().isSelected(mol))
185 SelectionMoleculeById(ids);
186 else
187 SelectionNotMoleculeById(ids);
188 emit clicked(no);
189}
190
191/** ....
192 *
193 */
194void GLWorldScene::worldSelectionChanged()
195{
196 LOG(3, "INFO: GLWorldScene: Received signal selectionChanged.");
197
198 const std::vector<const molecule*> &molecules =
199 const_cast<const World &>(World::getInstance()).getAllMolecules();
200
201 if (molecules.size() > 0) {
202 for (std::vector<const molecule*>::const_iterator Runner = molecules.begin();
203 Runner != molecules.end();
204 Runner++) {
205
206 // molecule selected but not in scene?
207 const bool isSelected =
208 const_cast<const World &>(World::getInstance()).isSelected(*Runner);
209 if (isSelected){
210 MoleculeNodeMap::iterator iter = MoleculesinSceneMap.find((*Runner)->getId());
211 ASSERT( iter != MoleculesinSceneMap.end(),
212 "GLWorldScene::worldSelectionChanged() - selected molecule is unknown.");
213 GLMoleculeObject_molecule *molObject = iter->second;
214 // inform molecule object
215 molObject->selected(isSelected);
216 }
217 }
218 }
219}
220
221/** Inserts a molecule into the scene.
222 *
223 * @param _mol molecule to insert
224 */
225void GLWorldScene::moleculeInserted(const moleculeId_t _id)
226{
227 LOG(3, "INFO: GLWorldScene: Received signal moleculeInserted for molecule "+toString(_id)+".");
228 MoleculeNodeMap::const_iterator iter = MoleculesinSceneMap.find(_id);
229 ASSERT( iter == MoleculesinSceneMap.end(),
230 "GLWorldScene::moleculeInserted() - molecule's id "+toString(_id)+" already present.");
231
232 // add new object
233 GLMoleculeObject_molecule *molObject = new GLMoleculeObject_molecule(GLMoleculeObject::meshEmpty, this, _id);
234 ASSERT( molObject != NULL,
235 "GLWorldScene::moleculeInserted - could not create molecule object for "+toString(_id));
236 MoleculesinSceneMap.insert( make_pair(_id, molObject) );
237 connect (molObject, SIGNAL(changed()), this, SIGNAL(changed()));
238 connect (molObject, SIGNAL(changeOccured()), this, SIGNAL(changeOccured()));
239 connect (molObject, SIGNAL(atomClicked(atomId_t)), this, SLOT(atomClicked(atomId_t)));
240 connect (molObject, SIGNAL(moleculeClicked(moleculeId_t)), this, SLOT(moleculeClicked(moleculeId_t)));
241 connect (molObject, SIGNAL(selectionChanged()), this, SIGNAL(changed()));
242 connect (molObject, SIGNAL(selectionChanged()), this, SIGNAL(changed()));
243 connect (molObject, SIGNAL(hoverChanged(const atomId_t)), this, SIGNAL(hoverChanged(const atomId_t)));
244 connect (molObject, SIGNAL(hoverChanged(const moleculeId_t, int)), this, SIGNAL(hoverChanged(const moleculeId_t, int)));
245 emit changed();
246 emit changeOccured();
247}
248
249/** Removes a molecule from the scene.
250 *
251 * @param _id id of molecule to remove
252 */
253void GLWorldScene::moleculeRemoved(const moleculeId_t _id)
254{
255 LOG(3, "INFO: GLWorldScene: Received signal moleculeRemoved for molecule "+toString(_id)+".");
256 MoleculeNodeMap::iterator iter = MoleculesinSceneMap.find(_id);
257 ASSERT( iter != MoleculesinSceneMap.end(),
258 "GLWorldScene::moleculeInserted() - molecule's id "+toString(_id)+" is unknown.");
259
260 GLMoleculeObject_molecule *molObject = iter->second;
261 molObject->disconnect();
262 MoleculesinSceneMap.erase(iter);
263 delete molObject;
264 emit changed();
265 emit changeOccured();
266}
267
268void GLWorldScene::moleculesVisibilityChanged(const moleculeId_t _id, bool _visible)
269{
270 MoleculeNodeMap::iterator iter = MoleculesinSceneMap.find(_id);
271 ASSERT( iter != MoleculesinSceneMap.end(),
272 "GLWorldScene::moleculeInserted() - molecule's id "+toString(_id)+" is unknown.");
273
274 GLMoleculeObject_molecule *molObject = iter->second;
275 molObject->setVisible(_visible);
276
277 emit changed();
278 emit changeOccured();
279}
280
281/** Adds a shape to the scene.
282 *
283 * uses ShapeRegistry::lastChanged()
284 *
285 */
286void GLWorldScene::addShape()
287{
288 Shape &shape = *ShapeRegistry::getInstance().lastChanged();
289 GLMoleculeObject_shape *shapeObject = new GLMoleculeObject_shape(shape, this);
290 ShapeNodeMap::iterator iter = ShapesinSceneMap.find(shape.getName());
291 ASSERT(iter == ShapesinSceneMap.end(),
292 "GLWorldScene::addShape() - same shape "+shape.getName()+" added again.");
293 ShapesinSceneMap.insert( make_pair(shape.getName(), shapeObject) );
294}
295
296void GLWorldScene::removeShape()
297{
298 Shape &shape = *ShapeRegistry::getInstance().lastChanged();
299 ShapeNodeMap::iterator iter = ShapesinSceneMap.find(shape.getName());
300 ASSERT(iter != ShapesinSceneMap.end(),
301 "GLWorldScene::removeShape() - shape "+shape.getName()+" not in scene.");
302 ShapesinSceneMap.erase(iter);
303 delete(iter->second);
304}
305
306void GLWorldScene::updateSelectedShapes()
307{
308 foreach (QObject *obj, children()) {
309 GLMoleculeObject_shape *shapeobj = qobject_cast<GLMoleculeObject_shape *>(obj);
310 if (shapeobj){
311 shapeobj->enable(ShapeRegistry::getInstance().isSelected(shapeobj->getShape()));
312 }
313 }
314}
315
316void GLWorldScene::initialize(QGLView *view, QGLPainter *painter) const
317{
318 // Initialize all of the mesh objects that we have as children.
319 foreach (QObject *obj, children()) {
320 GLMoleculeObject *meshobj = qobject_cast<GLMoleculeObject *>(obj);
321 if (meshobj)
322 meshobj->initialize(view, painter);
323 }
324}
325
326void GLWorldScene::draw(QGLPainter *painter, const QVector4D &cameraPlane) const
327{
328 // Draw all of the mesh objects that we have as children.
329 foreach (QObject *obj, children()) {
330 GLMoleculeObject *meshobj = qobject_cast<GLMoleculeObject *>(obj);
331 if (meshobj)
332 meshobj->draw(painter, cameraPlane);
333 }
334}
335
336void GLWorldScene::setSelectionMode(SelectionModeType mode)
337{
338 selectionMode = mode;
339 // TODO send update to toolbar
340}
341
342void GLWorldScene::setSelectionModeAtom()
343{
344 setSelectionMode(SelectAtom);
345}
346
347void GLWorldScene::setSelectionModeMolecule()
348{
349 setSelectionMode(SelectMolecule);
350}
Note: See TracBrowser for help on using the repository browser.