QGIS API Documentation 3.41.0-Master (af5edcb665c)
Loading...
Searching...
No Matches
qgssimplelinematerialsettings.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgssimplelinematerialsettings.cpp
3 --------------------------------------
4 Date : August 2020
5 Copyright : (C) 2020 by Nyall Dawson
6 Email : nyall dot dawson at gmail dot com
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 "qgscolorutils.h"
18#include "qgslinematerial_p.h"
19
20#include <Qt3DRender/QTexture>
21#include <Qt3DRender/QParameter>
22#include <Qt3DRender/QEffect>
23#include <QMap>
24
25#if QT_VERSION < QT_VERSION_CHECK( 6, 0, 0 )
26#include <Qt3DRender/QAttribute>
27#include <Qt3DRender/QBuffer>
28#include <Qt3DRender/QGeometry>
29
30typedef Qt3DRender::QAttribute Qt3DQAttribute;
31typedef Qt3DRender::QBuffer Qt3DQBuffer;
32typedef Qt3DRender::QGeometry Qt3DQGeometry;
33#else
34#include <Qt3DCore/QAttribute>
35#include <Qt3DCore/QBuffer>
36#include <Qt3DCore/QGeometry>
37
38typedef Qt3DCore::QAttribute Qt3DQAttribute;
39typedef Qt3DCore::QBuffer Qt3DQBuffer;
40typedef Qt3DCore::QGeometry Qt3DQGeometry;
41#endif
42
44{
45 return QStringLiteral( "simpleline" );
46}
47
65
70
75
77{
78 const QgsSimpleLineMaterialSettings *otherLine = dynamic_cast<const QgsSimpleLineMaterialSettings *>( other );
79 if ( !otherLine )
80 return false;
81
82 return *this == *otherLine;
83}
84
85void QgsSimpleLineMaterialSettings::readXml( const QDomElement &elem, const QgsReadWriteContext &context )
86{
87 mAmbient = QgsColorUtils::colorFromString( elem.attribute( QStringLiteral( "ambient" ), QStringLiteral( "25,25,25" ) ) );
88
90}
91
92void QgsSimpleLineMaterialSettings::writeXml( QDomElement &elem, const QgsReadWriteContext &context ) const
93{
94 elem.setAttribute( QStringLiteral( "ambient" ), QgsColorUtils::colorToString( mAmbient ) );
95
97}
98
100{
101 switch ( technique )
102 {
109 return nullptr;
110
112 {
113 QgsLineMaterial *mat = new QgsLineMaterial;
114 if ( !context.isSelected() )
115 {
116 mat->setLineColor( mAmbient );
117 mat->setUseVertexColors( dataDefinedProperties().isActive( QgsAbstractMaterialSettings::Property::Ambient ) );
118 }
119 else
120 {
121 // update the material with selection colors
122 mat->setLineColor( context.selectionColor() );
123 mat->setUseVertexColors( false );
124 }
125 return mat;
126 }
127 }
128 return nullptr;
129}
130
132{
133 QMap<QString, QString> parameters;
134 parameters[QStringLiteral( "Ka" )] = QStringLiteral( "%1 %2 %3" ).arg( mAmbient.redF() ).arg( mAmbient.greenF() ).arg( mAmbient.blueF() );
135 return parameters;
136}
137
138void QgsSimpleLineMaterialSettings::addParametersToEffect( Qt3DRender::QEffect *effect, const QgsMaterialContext &materialContext ) const
139{
140 const QColor ambient = materialContext.isSelected() ? materialContext.selectionColor().darker() : mAmbient;
141 Qt3DRender::QParameter *ambientParameter = new Qt3DRender::QParameter( QStringLiteral( "ambientColor" ), ambient );
142 effect->addParameter( ambientParameter );
143}
144
146{
147 const QColor ambient = dataDefinedProperties().valueAsColor( QgsAbstractMaterialSettings::Property::Ambient, expressionContext, mAmbient );
148
149 QByteArray array;
150 array.resize( sizeof( unsigned char ) * 3 );
151 unsigned char *fptr = reinterpret_cast<unsigned char *>( array.data() );
152
153 *fptr++ = static_cast<unsigned char>( ambient.red() );
154 *fptr++ = static_cast<unsigned char>( ambient.green() );
155 *fptr++ = static_cast<unsigned char>( ambient.blue() );
156 return array;
157}
158
159void QgsSimpleLineMaterialSettings::applyDataDefinedToGeometry( Qt3DQGeometry *geometry, int vertexCount, const QByteArray &data ) const
160{
161 Qt3DQBuffer *dataBuffer = new Qt3DQBuffer( geometry );
162
163 Qt3DQAttribute *colorAttribute = new Qt3DQAttribute( geometry );
164 colorAttribute->setName( QStringLiteral( "dataDefinedColor" ) );
165 colorAttribute->setVertexBaseType( Qt3DQAttribute::UnsignedByte );
166 colorAttribute->setVertexSize( 3 );
167 colorAttribute->setAttributeType( Qt3DQAttribute::VertexAttribute );
168 colorAttribute->setBuffer( dataBuffer );
169 colorAttribute->setByteStride( 3 * sizeof( unsigned char ) );
170 colorAttribute->setByteOffset( 0 );
171 colorAttribute->setCount( vertexCount );
172 geometry->addAttribute( colorAttribute );
173
174 dataBuffer->setData( data );
175}
virtual void writeXml(QDomElement &element, const QgsReadWriteContext &) const
Writes settings to a DOM element.
virtual void readXml(const QDomElement &element, const QgsReadWriteContext &)
Reads settings from a DOM element.
@ Ambient
Ambient color (phong material)
QgsPropertyCollection dataDefinedProperties() const
Returns the symbol material property collection, used for data defined overrides.
QColor valueAsColor(int key, const QgsExpressionContext &context, const QColor &defaultColor=QColor(), bool *ok=nullptr) const
Calculates the current value of the property with the specified key and interprets it as a color.
static QColor colorFromString(const QString &string)
Decodes a string into a color value.
static QString colorToString(const QColor &color)
Encodes a color into a string value.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
QColor selectionColor() const
Returns the color for representing materials in a selected state.
bool isSelected() const
Returns true if the material should represent a selected state.
The class is used as a container of context for various read/write operations on other objects.
QgsSimpleLineMaterialSettings * clone() const override
Clones the material settings.
void applyDataDefinedToGeometry(Qt3DCore::QGeometry *geometry, int vertexCount, const QByteArray &data) const override
Applies the data defined bytes, dataDefinedBytes, on the geometry by filling a specific vertex buffer...
void writeXml(QDomElement &elem, const QgsReadWriteContext &context) const override
Writes settings to a DOM element.
static bool supportsTechnique(QgsMaterialSettingsRenderingTechnique technique)
Returns true if the specified technique is supported by the material.
static QgsAbstractMaterialSettings * create()
Returns a new instance of QgsSimpleLineMaterialSettings.
void readXml(const QDomElement &elem, const QgsReadWriteContext &context) override
Reads settings from a DOM element.
QByteArray dataDefinedVertexColorsAsByte(const QgsExpressionContext &expressionContext) const override
Returns byte array corresponding to the data defined colors depending of the expressionContext,...
QColor ambient() const
Returns the ambient color component.
QString type() const override
Returns the unique type name for the material.
QMap< QString, QString > toExportParameters() const override
Returns the parameters to be exported to .mtl file.
bool equals(const QgsAbstractMaterialSettings *other) const override
Returns true if this settings exactly matches an other settings.
QgsMaterial * toMaterial(QgsMaterialSettingsRenderingTechnique technique, const QgsMaterialContext &context) const override
Creates a new QgsMaterial object representing the material settings.
void addParametersToEffect(Qt3DRender::QEffect *effect, const QgsMaterialContext &materialContext) const override
Adds parameters from the material to a destination effect.
QgsMaterialSettingsRenderingTechnique
Material rendering techniques 3.
@ Points
Point based rendering, requires point data.
@ Triangles
Triangle based rendering (default)
@ TrianglesFromModel
Triangle based rendering, using a model object source.
@ Lines
Line based rendering, requires line data.
@ TrianglesDataDefined
Triangle based rendering with possibility of datadefined color.
@ InstancedPoints
Instanced based rendering, requiring triangles and point data.
@ TrianglesWithFixedTexture
Triangle based rendering, using a fixed, non-user-configurable texture (e.g. for terrain rendering)
Qt3DCore::QAttribute Qt3DQAttribute
Qt3DCore::QBuffer Qt3DQBuffer
Qt3DCore::QGeometry Qt3DQGeometry
Qt3DCore::QAttribute Qt3DQAttribute
Qt3DCore::QBuffer Qt3DQBuffer
Qt3DCore::QGeometry Qt3DQGeometry