QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
qgsdirectionallightsettings.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsdirectionallightsettings.cpp
3  --------------------------------------
4  Date : June 2020
5  Copyright : (C) 2020 by Belgacem Nedjima
6  Email : gb underscore nedjima at esi dot dz
7  ***************************************************************************
8  * *
9  * This program is free software; you can redistribute it and/or modify *
10  * it under the terms of the GNU General Public License as published by *
11  * the Free Software Foundation; either version 2 of the License, or *
12  * (at your option) any later version. *
13  * *
14  ***************************************************************************/
15 
17 #include "qgssymbollayerutils.h"
18 
19 #include <QDomDocument>
20 #include <Qt3DRender/QDirectionalLight>
21 #include <Qt3DCore/QEntity>
22 
24 {
26 }
27 
29 {
30  return new QgsDirectionalLightSettings( *this );
31 }
32 
33 Qt3DCore::QEntity *QgsDirectionalLightSettings::createEntity( const Qgs3DMapSettings &, Qt3DCore::QEntity *parent ) const
34 {
35  Qt3DCore::QEntity *lightEntity = new Qt3DCore::QEntity( parent );
36  Qt3DCore::QTransform *lightTransform = new Qt3DCore::QTransform;
37 
38  Qt3DRender::QDirectionalLight *light = new Qt3DRender::QDirectionalLight;
39  light->setColor( color() );
40  light->setIntensity( intensity() );
42  light->setWorldDirection( QVector3D( direction.x(), direction.y(), direction.z() ) );
43 
44  lightEntity->addComponent( light );
45  lightEntity->addComponent( lightTransform );
46 
47  return lightEntity;
48 }
49 
50 QDomElement QgsDirectionalLightSettings::writeXml( QDomDocument &doc, const QgsReadWriteContext & ) const
51 {
52  QDomElement elemLight = doc.createElement( QStringLiteral( "directional-light" ) );
53  elemLight.setAttribute( QStringLiteral( "x" ), mDirection.x() );
54  elemLight.setAttribute( QStringLiteral( "y" ), mDirection.y() );
55  elemLight.setAttribute( QStringLiteral( "z" ), mDirection.z() );
56  elemLight.setAttribute( QStringLiteral( "color" ), QgsSymbolLayerUtils::encodeColor( mColor ) );
57  elemLight.setAttribute( QStringLiteral( "intensity" ), mIntensity );
58  return elemLight;
59 }
60 
61 void QgsDirectionalLightSettings::readXml( const QDomElement &elem, const QgsReadWriteContext & )
62 {
63  mDirection.set( elem.attribute( QStringLiteral( "x" ) ).toFloat(),
64  elem.attribute( QStringLiteral( "y" ) ).toFloat(),
65  elem.attribute( QStringLiteral( "z" ) ).toFloat() );
66  mColor = QgsSymbolLayerUtils::decodeColor( elem.attribute( QStringLiteral( "color" ) ) );
67  mIntensity = elem.attribute( QStringLiteral( "intensity" ) ).toFloat();
68 }
69 
71 {
72  return mDirection == other.mDirection && mColor == other.mColor && mIntensity == other.mIntensity;
73 }
QgsDirectionalLightSettings::clone
QgsDirectionalLightSettings * clone() const override
Returns a copy of the light source.
Definition: qgsdirectionallightsettings.cpp:28
QgsDirectionalLightSettings::color
QColor color() const
Returns color of the light.
Definition: qgsdirectionallightsettings.h:51
QgsSymbolLayerUtils::encodeColor
static QString encodeColor(const QColor &color)
Definition: qgssymbollayerutils.cpp:64
QgsReadWriteContext
The class is used as a container of context for various read/write operations on other objects.
Definition: qgsreadwritecontext.h:34
QgsVector3D::y
double y() const
Returns Y coordinate.
Definition: qgsvector3d.h:64
QgsVector3D
Class for storage of 3D vectors similar to QVector3D, with the difference that it uses double precisi...
Definition: qgsvector3d.h:31
qgssymbollayerutils.h
QgsDirectionalLightSettings::createEntity
Qt3DCore::QEntity * createEntity(const Qgs3DMapSettings &map, Qt3DCore::QEntity *parent) const override
Creates an entity representing the light source.
Definition: qgsdirectionallightsettings.cpp:33
QgsVector3D::set
void set(double x, double y, double z)
Sets vector coordinates.
Definition: qgsvector3d.h:69
QgsSymbolLayerUtils::decodeColor
static QColor decodeColor(const QString &str)
Definition: qgssymbollayerutils.cpp:69
Qgis::LightSourceType::Directional
@ Directional
Directional light source.
Qgis::LightSourceType
LightSourceType
Light source types for 3D scenes.
Definition: qgis.h:1837
QgsDirectionalLightSettings::type
Qgis::LightSourceType type() const override
Returns the light source type.
Definition: qgsdirectionallightsettings.cpp:23
QgsVector3D::z
double z() const
Returns Z coordinate.
Definition: qgsvector3d.h:66
Qgs3DMapSettings
Definition of the world.
Definition: qgs3dmapsettings.h:57
QgsDirectionalLightSettings::QgsDirectionalLightSettings
QgsDirectionalLightSettings()=default
Construct a directional light with default values.
QgsDirectionalLightSettings::intensity
float intensity() const
Returns intensity of the light.
Definition: qgsdirectionallightsettings.h:56
QgsDirectionalLightSettings::direction
QgsVector3D direction() const
Returns the direction of the light in degrees.
Definition: qgsdirectionallightsettings.h:46
QgsDirectionalLightSettings::operator==
bool operator==(const QgsDirectionalLightSettings &other)
Definition: qgsdirectionallightsettings.cpp:70
QgsDirectionalLightSettings::writeXml
QDomElement writeXml(QDomDocument &doc, const QgsReadWriteContext &context=QgsReadWriteContext()) const override
Writes the light source's configuration to a new DOM element and returns it.
Definition: qgsdirectionallightsettings.cpp:50
QgsVector3D::x
double x() const
Returns X coordinate.
Definition: qgsvector3d.h:62
QgsDirectionalLightSettings::readXml
void readXml(const QDomElement &elem, const QgsReadWriteContext &context=QgsReadWriteContext()) override
Reads configuration from a DOM element previously written using writeXml().
Definition: qgsdirectionallightsettings.cpp:61
qgsdirectionallightsettings.h
QgsDirectionalLightSettings
Definition of a directional light in a 3D map scene.
Definition: qgsdirectionallightsettings.h:33