source: src/UIElements/Views/Qt4/Qt3D/GLMoleculeObject.cpp@ 0aa122

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 0aa122 was 0aa122, checked in by Frederik Heber <heber@…>, 13 years ago

Updated all source files's copyright note to current year 2012.

  • Property mode set to 100644
File size: 5.9 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 * GLMoleculeObject.cpp
10 *
11 * This is based on the Qt3D example "teaservice", specifically meshobject.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 "GLMoleculeObject.hpp"
23
24#include <Qt3D/qglview.h>
25#include <Qt3D/qglscenenode.h>
26#include <Qt3D/qglpainter.h>
27#include <Qt3D/qglmaterial.h>
28
29#include "CodePatterns/MemDebug.hpp"
30
31#include "CodePatterns/Assert.hpp"
32#include "CodePatterns/Log.hpp"
33
34#include "Helpers/defs.hpp"
35#include "Element/element.hpp"
36#include "Element/periodentafel.hpp"
37#include "World.hpp"
38
39GLMoleculeObject::ElementMaterialMap GLMoleculeObject::ElementNoMaterialMap;
40
41
42#include "CodePatterns/MemDebug.hpp"
43
44
45GLMoleculeObject::GLMoleculeObject(QGLSceneNode *GLMoleculeObject, QObject *parent)
46 : QObject(parent)
47{
48 m_mesh = 0;
49 m_GLMoleculeObject = GLMoleculeObject;
50 m_scale = 1.0f;
51 m_rotationAngle = 0.0f;
52 m_effect = 0;
53 m_objectId = -1;
54 m_hovering = false;
55 m_material = 0;
56 m_hoverMaterial = 0;
57}
58
59GLMoleculeObject::GLMoleculeObject(QGLAbstractScene *scene, QObject *parent)
60 : QObject(parent)
61{
62 scene->setParent(this);
63 m_mesh = 0;
64 m_GLMoleculeObject = scene->mainNode();
65 m_scale = 1.0f;
66 m_rotationAngle = 0.0f;
67 m_effect = 0;
68 m_objectId = -1;
69 m_hovering = false;
70 m_material = 0;
71 m_hoverMaterial = 0;
72}
73
74GLMoleculeObject::~GLMoleculeObject()
75{
76 delete m_mesh;
77}
78
79void GLMoleculeObject::initialize(QGLView *view, QGLPainter *painter)
80{
81 Q_UNUSED(painter);
82 if (m_objectId != -1)
83 view->registerObject(m_objectId, this);
84}
85
86void GLMoleculeObject::draw(QGLPainter *painter)
87{
88 // Position the model at its designated position, scale, and orientation.
89 painter->modelViewMatrix().push();
90 painter->modelViewMatrix().translate(m_position);
91 if (m_scale != 1.0f)
92 painter->modelViewMatrix().scale(m_scale);
93 if (m_rotationAngle != 0.0f)
94 painter->modelViewMatrix().rotate(m_rotationAngle, m_rotationVector);
95
96 // Apply the material and effect to the painter.
97 QGLMaterial *material;
98 if (m_hovering)
99 material = m_hoverMaterial;
100 else
101 material = m_material;
102 painter->setColor(material->diffuseColor());
103 painter->setFaceMaterial(QGL::AllFaces, material);
104 if (m_effect)
105 painter->setUserEffect(m_effect);
106 else
107 painter->setStandardEffect(QGL::LitMaterial);
108
109 // Mark the object for object picking purposes.
110 int prevObjectId = painter->objectPickId();
111 if (m_objectId != -1)
112 painter->setObjectPickId(m_objectId);
113
114 // Draw the geometry mesh.
115 if (m_GLMoleculeObject)
116 m_GLMoleculeObject->draw(painter);
117 else
118 m_mesh->draw(painter);
119
120 // Turn off the user effect, if present.
121 if (m_effect)
122 painter->setStandardEffect(QGL::LitMaterial);
123
124 // Revert to the previous object identifier.
125 painter->setObjectPickId(prevObjectId);
126
127 // Restore the modelview matrix.
128 painter->modelViewMatrix().pop();
129}
130
131bool GLMoleculeObject::event(QEvent *e)
132{
133 // Convert the raw event into a signal representing the user's action.
134 if (e->type() == QEvent::MouseButtonPress) {
135 QMouseEvent *me = (QMouseEvent *)e;
136 if (me->button() == Qt::LeftButton)
137 emit pressed();
138 } else if (e->type() == QEvent::MouseButtonRelease) {
139 QMouseEvent *me = (QMouseEvent *)e;
140 if (me->button() == Qt::LeftButton) {
141 emit released();
142 if (me->x() >= 0) // Positive: inside object, Negative: outside.
143 emit clicked();
144 }
145 } else if (e->type() == QEvent::MouseButtonDblClick) {
146 emit doubleClicked();
147 } else if (e->type() == QEvent::Enter) {
148 m_hovering = true;
149 emit hoverChanged();
150 } else if (e->type() == QEvent::Leave) {
151 m_hovering = false;
152 emit hoverChanged();
153 }
154 return QObject::event(e);
155}
156
157/** Returns the ref to the Material for element No \a from the map.
158 *
159 * \note We create a new one if the element is missing.
160 *
161 * @param no element no
162 * @return ref to QGLMaterial
163 */
164QGLMaterial* GLMoleculeObject::getMaterial(size_t no)
165{
166 ASSERT( (no >= 0) && (no < MAX_ELEMENTS),
167 "GLMoleculeView::getMaterial() - Element no "+toString(no)+" is invalid.");
168 if (ElementNoMaterialMap.find(no) != ElementNoMaterialMap.end()){
169 // get present one
170 return ElementNoMaterialMap[no];
171 } else {
172 // create new one
173 LOG(1, "Creating new material for element "+toString(no)+".");
174 QGLMaterial *newmaterial = new QGLMaterial(NULL);
175
176 if (no == 0) { // create hover material
177 newmaterial->setAmbientColor( QColor(0, 128, 128) );
178 } else { // create material for element
179 periodentafel *periode = World::getInstance().getPeriode();
180 const element *desiredelement = periode->FindElement(no);
181 ASSERT(desiredelement != NULL,
182 "GLMoleculeView::getMaterial() - desired element "+toString(no)+" not present in periodentafel.");
183 const unsigned char* color = desiredelement->getColor();
184 LOG(1, "Creating new material with color " << (int)color[0] << "," << (int)color[1] << "," << (int)color[2] << ".");
185 newmaterial->setAmbientColor( QColor((int)color[0], (int)color[1], (int)color[2]) );
186 }
187 newmaterial->setSpecularColor( QColor(60, 60, 60) );
188 newmaterial->setShininess( 128 );
189 ElementNoMaterialMap.insert( make_pair(no, newmaterial) );
190
191 return newmaterial;
192 }
193}
194
195/** Static function to be called when Materials have to be removed.
196 *
197 */
198void GLMoleculeObject::cleanMaterialMap()
199{
200 for (ElementMaterialMap::iterator iter = ElementNoMaterialMap.begin();
201 !ElementNoMaterialMap.empty();
202 iter = ElementNoMaterialMap.begin()) {
203 delete iter->second;
204 ElementNoMaterialMap.erase(iter);
205 }
206}
Note: See TracBrowser for help on using the repository browser.