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

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

GLMoleculeObject have m_visible, settable in QtMoleculeList.

  • is connected via QtMainWindow an GLWorldView, GLWorldScene.
  • i.e. molecule's atoms and bond display can now be switched on and off.
  • Property mode set to 100644
File size: 10.6 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 * GLMoleculeObject.cpp
26 *
27 * This is based on the Qt3D example "teaservice", specifically meshobject.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 "GLMoleculeObject.hpp"
39
40#include <Qt3D/qglview.h>
41#include <Qt3D/qglscenenode.h>
42#include <Qt3D/qglpainter.h>
43#include <Qt3D/qglmaterial.h>
44
45#include "CodePatterns/MemDebug.hpp"
46
47#include "CodePatterns/Assert.hpp"
48#include "CodePatterns/Log.hpp"
49
50#include "Helpers/defs.hpp"
51#include "Element/element.hpp"
52#include "Element/periodentafel.hpp"
53#include "World.hpp"
54
55GLMoleculeObject::ElementMaterialMap GLMoleculeObject::ElementNoMaterialMap;
56
57
58#include "CodePatterns/MemDebug.hpp"
59
60QGLMaterial *GLMoleculeObject::m_hoverMaterial = NULL;
61QGLMaterial *GLMoleculeObject::m_selectionMaterial = NULL;
62QGLMaterial *GLMoleculeObject::m_selectionBoxMaterial = NULL;
63
64QGLSceneNode *GLMoleculeObject::meshEmpty[GLMoleculeObject::DETAILTYPES_MAX];
65QGLSceneNode *GLMoleculeObject::meshSphere[GLMoleculeObject::DETAILTYPES_MAX];
66QGLSceneNode *GLMoleculeObject::meshCylinder[GLMoleculeObject::DETAILTYPES_MAX];
67
68double GLMoleculeObject::detailMinDistance[GLMoleculeObject::DETAILTYPES_MAX] = {0, 15, 30, 42};
69
70GLMoleculeObject::GLMoleculeObject(QGLSceneNode *mesh[], QObject *parent)
71 : QObject(parent)
72{
73 //mesh->setParent(this);
74 for (int i=0;i<DETAILTYPES_MAX;i++)
75 m_mesh[i] = mesh[i];
76 m_scaleX = 1.0f;
77 m_scaleY = 1.0f;
78 m_scaleZ = 1.0f;
79 m_rotationAngle = 0.0f;
80 m_effect = 0;
81 m_objectId = -1;
82 m_hovering = false;
83 m_selected = false;
84 m_visible = true;
85 m_material = 0;
86 initStaticMaterials();
87}
88
89GLMoleculeObject::GLMoleculeObject(QGLSceneNode *mesh, QObject *parent)
90 : QObject(parent)
91{
92 //mesh->setParent(this);
93 for (int i=0;i<DETAILTYPES_MAX;i++)
94 m_mesh[i] = mesh;
95 m_scaleX = 1.0f;
96 m_scaleY = 1.0f;
97 m_scaleZ = 1.0f;
98 m_rotationAngle = 0.0f;
99 m_effect = 0;
100 m_objectId = -1;
101 m_hovering = false;
102 m_selected = false;
103 m_material = 0;
104 initStaticMaterials();
105}
106
107GLMoleculeObject::GLMoleculeObject(QGLAbstractScene *scene, QObject *parent)
108 : QObject(parent)
109{
110 scene->setParent(this);
111 for (int i=0;i<DETAILTYPES_MAX;i++)
112 m_mesh[i] = scene->mainNode();
113 m_scaleX = 1.0f;
114 m_scaleY = 1.0f;
115 m_scaleZ = 1.0f;
116 m_rotationAngle = 0.0f;
117 m_effect = 0;
118 m_objectId = -1;
119 m_hovering = false;
120 m_selected = false;
121 m_material = 0;
122 initStaticMaterials();
123}
124
125GLMoleculeObject::~GLMoleculeObject()
126{
127}
128
129void GLMoleculeObject::initialize(QGLView *view, QGLPainter *painter)
130{
131 Q_UNUSED(painter);
132 if (m_objectId != -1)
133 view->registerObject(m_objectId, this);
134}
135
136
137/** Draws a box around the mesh.
138 *
139 */
140void GLMoleculeObject::drawSelectionBox(QGLPainter *painter)
141{
142 painter->setFaceMaterial(QGL::AllFaces, m_selectionBoxMaterial);
143 QVector3DArray array;
144 qreal radius = 1.0;
145 array.append(-radius, -radius, -radius); array.append( radius, -radius, -radius);
146 array.append( radius, -radius, -radius); array.append( radius, radius, -radius);
147 array.append( radius, radius, -radius); array.append(-radius, radius, -radius);
148 array.append(-radius, radius, -radius); array.append(-radius, -radius, -radius);
149
150 array.append(-radius, -radius, radius); array.append( radius, -radius, radius);
151 array.append( radius, -radius, radius); array.append( radius, radius, radius);
152 array.append( radius, radius, radius); array.append(-radius, radius, radius);
153 array.append(-radius, radius, radius); array.append(-radius, -radius, radius);
154
155 array.append(-radius, -radius, -radius); array.append(-radius, -radius, radius);
156 array.append( radius, -radius, -radius); array.append( radius, -radius, radius);
157 array.append(-radius, radius, -radius); array.append(-radius, radius, radius);
158 array.append( radius, radius, -radius); array.append( radius, radius, radius);
159 painter->clearAttributes();
160 painter->setVertexAttribute(QGL::Position, array);
161 painter->draw(QGL::Lines, 24);
162}
163
164void GLMoleculeObject::draw(QGLPainter *painter, const QVector4D &cameraPlane)
165{
166 // Position the model at its designated position, scale, and orientation.
167 painter->modelViewMatrix().push();
168 painter->modelViewMatrix().translate(m_position);
169 if (m_rotationAngle != 0.0f)
170 painter->modelViewMatrix().rotate(m_rotationAngle, m_rotationVector);
171 painter->modelViewMatrix().scale(m_scaleX, m_scaleY, m_scaleZ);
172
173 // Apply the material and effect to the painter.
174 QGLMaterial *material;
175 if (m_hovering)
176 material = m_hoverMaterial;
177 else if (m_selected)
178 material = m_selectionMaterial;
179 else
180 material = m_material;
181
182 ASSERT(material, "GLMoleculeObject::draw: chosen material is NULL");
183
184 painter->setColor(material->diffuseColor());
185 painter->setFaceMaterial(QGL::AllFaces, material);
186 if (m_effect)
187 painter->setUserEffect(m_effect);
188 else
189 painter->setStandardEffect(QGL::LitMaterial);
190
191 // Mark the object for object picking purposes.
192 int prevObjectId = painter->objectPickId();
193 if (m_objectId != -1)
194 painter->setObjectPickId(m_objectId);
195
196 // Draw the geometry mesh.
197 QVector4D pos4d(m_position, -1);
198 qreal distance = QVector4D::dotProduct(cameraPlane, pos4d);
199
200 if (distance > detailMinDistance[DETAIL_LOW])
201 m_mesh[DETAIL_LOW]->draw(painter);
202 else if (distance > detailMinDistance[DETAIL_MEDIUM])
203 m_mesh[DETAIL_MEDIUM]->draw(painter);
204 else if (distance > detailMinDistance[DETAIL_HIGH])
205 m_mesh[DETAIL_HIGH]->draw(painter);
206 else if (distance > detailMinDistance[DETAIL_HIGHEST])
207 m_mesh[DETAIL_HIGHEST]->draw(painter);
208
209 // Draw a box around the mesh, if selected.
210 if (m_selected)
211 drawSelectionBox(painter);
212
213 // Turn off the user effect, if present.
214 if (m_effect)
215 painter->setStandardEffect(QGL::LitMaterial);
216
217 // Revert to the previous object identifier.
218 painter->setObjectPickId(prevObjectId);
219
220 // Restore the modelview matrix.
221 painter->modelViewMatrix().pop();
222}
223
224bool GLMoleculeObject::event(QEvent *e)
225{
226 // Convert the raw event into a signal representing the user's action.
227 if (e->type() == QEvent::MouseButtonPress) {
228 QMouseEvent *me = (QMouseEvent *)e;
229 if (me->button() == Qt::LeftButton)
230 emit pressed();
231 } else if (e->type() == QEvent::MouseButtonRelease) {
232 QMouseEvent *me = (QMouseEvent *)e;
233 if (me->button() == Qt::LeftButton) {
234 emit released();
235 if (me->x() >= 0) // Positive: inside object, Negative: outside.
236 emit clicked();
237 }
238 } else if (e->type() == QEvent::MouseButtonDblClick) {
239 emit doubleClicked();
240 } else if (e->type() == QEvent::Enter) {
241 m_hovering = true;
242 emit hoverChanged(this);
243 } else if (e->type() == QEvent::Leave) {
244 m_hovering = false;
245 emit hoverChanged(NULL);
246 }
247 return QObject::event(e);
248}
249
250/** Returns the ref to the Material for element No \a from the map.
251 *
252 * \note We create a new one if the element is missing.
253 *
254 * @param no element no
255 * @return ref to QGLMaterial
256 */
257QGLMaterial* GLMoleculeObject::getMaterial(size_t no)
258{
259 ASSERT( (no > 0) && (no < MAX_ELEMENTS),
260 "GLMoleculeView::getMaterial() - Element no "+toString(no)+" is invalid.");
261 if (ElementNoMaterialMap.find(no) != ElementNoMaterialMap.end()){
262 // get present one
263 return ElementNoMaterialMap[no];
264 } else {
265 // create new one
266 LOG(1, "Creating new material for element "+toString(no)+".");
267 QGLMaterial *newmaterial = new QGLMaterial(NULL);
268
269 // create material for element
270 periodentafel *periode = World::getInstance().getPeriode();
271 const element *desiredelement = periode->FindElement(no);
272 ASSERT(desiredelement != NULL,
273 "GLMoleculeView::getMaterial() - desired element "+toString(no)+" not present in periodentafel.");
274 const unsigned char* color = desiredelement->getColor();
275 LOG(1, "Creating new material with color " << (int)color[0] << "," << (int)color[1] << "," << (int)color[2] << ".");
276 newmaterial->setAmbientColor( QColor((int)color[0], (int)color[1], (int)color[2]) );
277 newmaterial->setSpecularColor( QColor(60, 60, 60) );
278 newmaterial->setShininess( 128 );
279 ElementNoMaterialMap.insert( make_pair(no, newmaterial) );
280
281 return newmaterial;
282 }
283}
284
285/** Create the 3 materials shared by all objects.
286 *
287 */
288void GLMoleculeObject::initStaticMaterials()
289{
290 if (!m_hoverMaterial){
291 m_hoverMaterial = new QGLMaterial(NULL);
292 m_hoverMaterial->setAmbientColor( QColor(0, 128, 128) );
293 m_hoverMaterial->setSpecularColor( QColor(60, 60, 60) );
294 m_hoverMaterial->setShininess( 128 );
295 }
296 if (!m_selectionMaterial){
297 m_selectionMaterial = new QGLMaterial(NULL);
298 m_selectionMaterial->setAmbientColor( QColor(255, 50, 50) );
299 m_selectionMaterial->setSpecularColor( QColor(60, 60, 60) );
300 m_selectionMaterial->setShininess( 128 );
301 }
302 if (!m_selectionBoxMaterial){
303 m_selectionBoxMaterial = new QGLMaterial(NULL);
304 m_selectionBoxMaterial->setAmbientColor( QColor(0, 0, 0) );
305 m_selectionBoxMaterial->setDiffuseColor( QColor(0, 0, 0) );
306 m_selectionBoxMaterial->setEmittedLight( QColor(155, 50, 50) );
307 }
308}
309
310/** Static function to be called when Materials have to be removed.
311 *
312 */
313void GLMoleculeObject::cleanMaterialMap()
314{
315 for (ElementMaterialMap::iterator iter = ElementNoMaterialMap.begin();
316 !ElementNoMaterialMap.empty();
317 iter = ElementNoMaterialMap.begin()) {
318 delete iter->second;
319 ElementNoMaterialMap.erase(iter);
320 }
321}
322
323
324void GLMoleculeObject::setSelected(bool value)
325{
326 if (value != m_selected){
327 m_selected = value;
328 emit selectionChanged();
329 }
330}
331
332void GLMoleculeObject::setVisible(bool value)
333{
334 if (value != m_visible){
335 m_visible = value;
336 emit changed();
337 }
338}
Note: See TracBrowser for help on using the repository browser.