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

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

GL: atoms show if they're selected

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