source: src/UIElements/Views/Qt4/Qt3D/GLMoleculeObject.cpp@ 42c6a47

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

REFACTOR: removed duplicate variable in GLMoleculeObject (m_mesh, m_GLMoleculeObject)

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