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

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 407638e was 407638e, checked in by Michael Ankele <ankele@…>, 13 years ago

GL: Qt signals rewired

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