source: src/UIElements/Views/Qt4/Qt3D/GLWorldScene.cpp@ 2b596f

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 2b596f was 2b596f, checked in by Frederik Heber <heber@…>, 10 years ago

Hovering over molecules is working including update of QtInfoBox.

  • Property mode set to 100644
File size: 15.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 * 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<molecule *> &molecules = World::getInstance().getAllMolecules();
110
111 for (std::vector<molecule*>::const_iterator moliter = molecules.begin();
112 moliter != molecules.end();
113 moliter++) {
114 // create molecule objects in scene
115 moleculeInserted(*moliter);
116 }
117}
118
119/** Update the WorldScene with molecules and atoms from World.
120 *
121 * This function should be called after e.g. WorldTime::TimeChanged was
122 * received or after another molecule has been loaded.
123 *
124 */
125void GLWorldScene::update()
126{
127 const std::vector<molecule *> &molecules = World::getInstance().getAllMolecules();
128
129 for (std::vector<molecule*>::const_iterator moliter = molecules.begin();
130 moliter != molecules.end();
131 moliter++) {
132 // check whether molecule already exists
133 const moleculeId_t molid = (*moliter)->getId();
134 const bool mol_present = MoleculesinSceneMap.count(molid);
135 if (!mol_present)
136 moleculeInserted(*moliter);
137 }
138}
139
140void GLWorldScene::atomClicked(atomId_t no)
141{
142 LOG(3, "INFO: GLMoleculeObject_molecule - atom " << no << " has been clicked.");
143 const atom *Walker = World::getInstance().getAtom(AtomById(no));
144 if (selectionMode == SelectAtom){
145 if (!World::getInstance().isSelected(Walker))
146 SelectionAtomById(std::vector<atomId_t>(1,no));
147 else
148 SelectionNotAtomById(std::vector<atomId_t>(1,no));
149 }else if (selectionMode == SelectMolecule){
150 const molecule *mol = Walker->getMolecule();
151 ASSERT(mol, "Atom without molecule has been clicked.");
152 if (!World::getInstance().isSelected(mol))
153 SelectionMoleculeById(mol->getId());
154 else
155 SelectionNotMoleculeById(mol->getId());
156 }
157 emit clicked(no);
158}
159
160
161/** Adds an atom to the scene.
162 *
163 * @param _atom atom to add
164 */
165void GLWorldScene::atomInserted(const atomId_t _id)
166{
167 LOG(3, "INFO: GLWorldScene: Received signal atomInserted for atom "+toString(_id)+".");
168 // find associated molecule
169 const moleculeId_t molid = World::getInstance().getAtom(AtomById(_id))->getMolecule()->getId();
170 MoleculeNodeMap::const_iterator moliter = MoleculesinSceneMap.find(molid );
171 ASSERT(moliter != MoleculesinSceneMap.end(),
172 "GLWorldScene::atomAdded() - molecule with id of "+toString(molid)
173 +" atom with id "+toString(_id)+" is unknown.");
174 GLMoleculeObject_molecule *molObject = moliter->second;
175
176 // add atom to internal list
177#ifndef NDEBUG
178 AtomMoleculeMap::const_iterator atomiter = AtomsinSceneMap.find(_id);
179 ASSERT(atomiter == AtomsinSceneMap.end(),
180 "GLWorldScene::atomRemoved() - atom "+toString(_id)+" already known.");
181#endif
182 AtomsinSceneMap.insert( std::make_pair( _id, molObject ));
183
184 // inform its GlMoleculeObject_molecule.
185 molObject->atomInserted(_id);
186
187 // emit change
188 emit changeOccured();
189}
190
191/** Removes an atom from the scene.
192 *
193 * We just the id as the atom might have already been destroyed.
194 *
195 * @param _id id of atom to remove
196 */
197void GLWorldScene::atomRemoved(const atomId_t _id)
198{
199 LOG(3, "INFO: GLWorldScene: Received signal atomRemoved for atom "+toString(_id)+".");
200 // find associated molecule
201 AtomMoleculeMap::iterator iter = AtomsinSceneMap.find(_id);
202 ASSERT(iter != AtomsinSceneMap.end(),
203 "GLWorldScene::atomRemoved() - atom "+toString(_id)+" not on display.");
204 GLMoleculeObject_molecule *molObject = iter->second;
205
206 // remove from internal list
207 AtomsinSceneMap.erase(iter);
208
209 // inform its GlMoleculeObject_molecule.
210 molObject->atomRemoved(_id);
211
212 // emit change
213 emit changeOccured();
214}
215
216/** ....
217 *
218 */
219void GLWorldScene::worldSelectionChanged()
220{
221 LOG(3, "INFO: GLWorldScene: Received signal selectionChanged.");
222
223 const std::vector<molecule*> &molecules = World::getInstance().getAllMolecules();
224
225 if (molecules.size() > 0) {
226 for (std::vector<molecule*>::const_iterator Runner = molecules.begin();
227 Runner != molecules.end();
228 Runner++) {
229
230 // molecule selected but not in scene?
231 const bool isSelected = World::getInstance().isSelected(*Runner);
232 if (isSelected){
233 MoleculeNodeMap::iterator iter = MoleculesinSceneMap.find((*Runner)->getId());
234 ASSERT( iter != MoleculesinSceneMap.end(),
235 "GLWorldScene::worldSelectionChanged() - selected molecule is unknown.");
236 GLMoleculeObject_molecule *molObject = iter->second;
237 // inform molecule object
238 molObject->selected(isSelected);
239 }
240 }
241 }
242}
243
244/** Inserts a molecule into the scene.
245 *
246 * @param _mol molecule to insert
247 */
248void GLWorldScene::moleculeInserted(const molecule * _mol)
249{
250 LOG(3, "INFO: GLWorldScene: Received signal moleculeRemoved for molecule "+toString(_mol->getId())+".");
251 MoleculeNodeMap::const_iterator iter = MoleculesinSceneMap.find(_mol->getId());
252 ASSERT( iter == MoleculesinSceneMap.end(),
253 "GLWorldScene::moleculeInserted() - molecule's id "+toString(_mol->getId())+" already present.");
254
255 // add new object
256 GLMoleculeObject_molecule *molObject = new GLMoleculeObject_molecule(GLMoleculeObject::meshEmpty, this, _mol);
257 MoleculesinSceneMap.insert( make_pair(_mol->getId(), molObject) );
258 connect (molObject, SIGNAL(changed()), this, SIGNAL(changed()));
259 connect (molObject, SIGNAL(changeOccured()), this, SIGNAL(changeOccured()));
260 connect (molObject, SIGNAL(atomClicked(atomId_t)), this, SLOT(atomClicked(atomId_t)));
261 connect (molObject, SIGNAL(changeAtomId(GLMoleculeObject_atom *, int, int)), this, SLOT(changeAtomId(GLMoleculeObject_atom *, int, int)));
262 connect (molObject, SIGNAL(selectionChanged()), this, SIGNAL(changed()));
263 connect (molObject, SIGNAL(selectionChanged()), this, SIGNAL(changed()));
264 connect (molObject, SIGNAL(hoverChanged(const atom &)), this, SIGNAL(hoverChanged(const atom &)));
265 connect (molObject, SIGNAL(hoverChanged(const molecule &, int)), this, SIGNAL(hoverChanged(const molecule &, int)));
266 emit changed();
267 emit changeOccured();
268}
269
270/** Removes a molecule from the scene.
271 *
272 * @param _id id of molecule to remove
273 */
274void GLWorldScene::moleculeRemoved(const moleculeId_t _id)
275{
276 LOG(3, "INFO: GLWorldScene: Received signal moleculeRemoved for molecule "+toString(_id)+".");
277 MoleculeNodeMap::iterator iter = MoleculesinSceneMap.find(_id);
278 ASSERT( iter != MoleculesinSceneMap.end(),
279 "GLWorldScene::moleculeInserted() - molecule's id "+toString(_id)+" is unknown.");
280
281 GLMoleculeObject_molecule *molObject = iter->second;
282 molObject->disconnect();
283 MoleculesinSceneMap.erase(iter);
284 delete molObject;
285 emit changed();
286 emit changeOccured();
287}
288
289void GLWorldScene::moleculesVisibilityChanged(const moleculeId_t _id, bool _visible)
290{
291 MoleculeNodeMap::iterator iter = MoleculesinSceneMap.find(_id);
292 ASSERT( iter != MoleculesinSceneMap.end(),
293 "GLWorldScene::moleculeInserted() - molecule's id "+toString(_id)+" is unknown.");
294
295 GLMoleculeObject_molecule *molObject = iter->second;
296 molObject->setVisible(_visible);
297
298 emit changed();
299 emit changeOccured();
300}
301
302/** Adds a bond to the scene.
303 *
304 * @param _bond bond to add
305 * @param side which side of the bond (left or right)
306 */
307void GLWorldScene::bondInserted(const bond::ptr _bond, const enum GLMoleculeObject_bond::SideOfBond _side)
308{
309 LOG(3, "INFO: GLWorldScene::bondInserted() - Adding bond "+toString(*_bond)+".");
310 //LOG(4, "INFO: Currently present bonds " << BondsinSceneMap << ".");
311 // bond partners must both belong to same atom
312 ASSERT( _bond->leftatom->getMolecule() == _bond->rightatom->getMolecule(),
313 "GLWorldScene::bondInserted() - bond partners do not belong to same molecule.");
314
315 // find associated molecule object
316 const moleculeId_t molid = _bond->leftatom->getMolecule()->getId();
317 MoleculeNodeMap::const_iterator moliter = MoleculesinSceneMap.find(molid );
318 ASSERT(moliter != MoleculesinSceneMap.end(),
319 "GLWorldScene::bondInserted() - molecule with id of "+toString(molid)
320 +" atom with id "+toString(_bond->leftatom->getId())+" is unknown.");
321 GLMoleculeObject_molecule *molObject = moliter->second;
322
323 // add to internal list
324 const GLMoleculeObject_molecule::BondIds ids =
325 GLMoleculeObject_molecule::getBondIds(_bond, _side);
326 BondMoleculeMap::const_iterator iter = BondsinSceneMap.find(ids);
327 ASSERT(iter == BondsinSceneMap.end(),
328 "GLWorldScene::bondInserted() - bond with ids "+toString(ids.first)
329 +","+toString(ids.second)+" already present.");
330 BondsinSceneMap.insert( std::make_pair( ids, molObject ));
331
332 // inform its GlMoleculeObject_molecule.
333 molObject->bondInserted(_bond, _side);
334
335 // emit change
336 emit changeOccured();
337}
338
339/** Removes a bond from the scene.
340 *
341 * @param _bond bond to remove
342 */
343void GLWorldScene::bondRemoved(const atomId_t leftnr, const atomId_t rightnr)
344{
345 LOG(3, "INFO: GLWorldScene::bondRemoved() - Removing bond between "+toString(leftnr)+" and "+toString(rightnr)+".");
346 {
347 // left bond
348 const GLMoleculeObject_molecule::BondIds Leftids( make_pair(leftnr, rightnr) );
349 BondMoleculeMap::iterator leftiter = BondsinSceneMap.find( Leftids );
350 ASSERT(leftiter != BondsinSceneMap.end(),
351 "GLWorldScene::bondRemoved() - bond "+toString(leftnr)+"-"
352 +toString(rightnr)+" not on display.");
353 GLMoleculeObject_molecule *molObject = leftiter->second;
354
355 // remove from internal list
356 BondsinSceneMap.erase(leftiter);
357
358 // inform its GlMoleculeObject_molecule.
359 molObject->bondRemoved( leftnr, rightnr );
360 }
361
362 // emit change
363 emit changeOccured();
364}
365
366/** Adds a shape to the scene.
367 *
368 * uses ShapeRegistry::lastChanged()
369 *
370 */
371void GLWorldScene::addShape()
372{
373 Shape &shape = *ShapeRegistry::getInstance().lastChanged();
374 GLMoleculeObject_shape *shapeObject = new GLMoleculeObject_shape(shape, this);
375 ShapeNodeMap::iterator iter = ShapesinSceneMap.find(shape.getName());
376 ASSERT(iter == ShapesinSceneMap.end(),
377 "GLWorldScene::addShape() - same shape "+shape.getName()+" added again.");
378 ShapesinSceneMap.insert( make_pair(shape.getName(), shapeObject) );
379}
380
381void GLWorldScene::removeShape()
382{
383 Shape &shape = *ShapeRegistry::getInstance().lastChanged();
384 ShapeNodeMap::iterator iter = ShapesinSceneMap.find(shape.getName());
385 ASSERT(iter != ShapesinSceneMap.end(),
386 "GLWorldScene::removeShape() - shape "+shape.getName()+" not in scene.");
387 ShapesinSceneMap.erase(iter);
388 delete(iter->second);
389}
390
391void GLWorldScene::updateSelectedShapes()
392{
393 foreach (QObject *obj, children()) {
394 GLMoleculeObject_shape *shapeobj = qobject_cast<GLMoleculeObject_shape *>(obj);
395 if (shapeobj){
396 shapeobj->enable(ShapeRegistry::getInstance().isSelected(shapeobj->getShape()));
397 }
398 }
399}
400
401void GLWorldScene::initialize(QGLView *view, QGLPainter *painter) const
402{
403 // Initialize all of the mesh objects that we have as children.
404 foreach (QObject *obj, children()) {
405 GLMoleculeObject *meshobj = qobject_cast<GLMoleculeObject *>(obj);
406 if (meshobj)
407 meshobj->initialize(view, painter);
408 }
409}
410
411void GLWorldScene::draw(QGLPainter *painter, const QVector4D &cameraPlane) const
412{
413 // Draw all of the mesh objects that we have as children.
414 foreach (QObject *obj, children()) {
415 GLMoleculeObject *meshobj = qobject_cast<GLMoleculeObject *>(obj);
416 if (meshobj)
417 meshobj->draw(painter, cameraPlane);
418 }
419}
420
421void GLWorldScene::setSelectionMode(SelectionModeType mode)
422{
423 selectionMode = mode;
424 // TODO send update to toolbar
425}
426
427void GLWorldScene::setSelectionModeAtom()
428{
429 setSelectionMode(SelectAtom);
430}
431
432void GLWorldScene::setSelectionModeMolecule()
433{
434 setSelectionMode(SelectMolecule);
435}
436
437void GLWorldScene::changeAtomId(GLMoleculeObject_atom *ob, int oldId, int newId)
438{
439 LOG(3, "INFO: GLWorldScene - change atom id " << oldId << " to " << newId << ".");
440 // Remove from map.
441 AtomMoleculeMap::iterator iter = AtomsinSceneMap.find(oldId);
442 ASSERT(iter != AtomsinSceneMap.end(),
443 "GLWorldScene::changeAtomId() - atom with old id "+toString(oldId)+" not on display.");
444 GLMoleculeObject_molecule *molObject = iter->second;
445
446 // erase by signalling removal
447 molObject->atomRemoved(oldId);
448
449 // remove from internal list
450 AtomsinSceneMap.erase(iter);
451
452 // Reinsert with new id.
453 {
454 AtomMoleculeMap::iterator iter = AtomsinSceneMap.find(newId);
455 ASSERT(iter == AtomsinSceneMap.end(),
456 "GLWorldScene::changeAtomId() - atom with new id "+toString(newId)+" already known.");
457 }
458 AtomsinSceneMap.insert( make_pair(newId, molObject) );
459
460 // inform molecule object
461 molObject->atomInserted(oldId);
462}
Note: See TracBrowser for help on using the repository browser.