QGIS API Documentation 3.99.0-Master (8e76e220402)
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
18#include "qgscolorutils.h"
19#include "qgslinematerial_p.h"
20
21#include <QMap>
22#include <QString>
23#include <Qt3DCore/QAttribute>
24#include <Qt3DCore/QBuffer>
25#include <Qt3DCore/QGeometry>
26#include <Qt3DRender/QEffect>
27#include <Qt3DRender/QParameter>
28#include <Qt3DRender/QTexture>
29
30using namespace Qt::StringLiterals;
31
33{
34 return u"simpleline"_s;
35}
36
54
59
64
66{
67 const QgsSimpleLineMaterialSettings *otherLine = dynamic_cast<const QgsSimpleLineMaterialSettings *>( other );
68 if ( !otherLine )
69 return false;
70
71 return *this == *otherLine;
72}
73
74void QgsSimpleLineMaterialSettings::readXml( const QDomElement &elem, const QgsReadWriteContext &context )
75{
76 mAmbient = QgsColorUtils::colorFromString( elem.attribute( u"ambient"_s, u"25,25,25"_s ) );
77
79}
80
81void QgsSimpleLineMaterialSettings::writeXml( QDomElement &elem, const QgsReadWriteContext &context ) const
82{
83 elem.setAttribute( u"ambient"_s, QgsColorUtils::colorToString( mAmbient ) );
84
86}
87
89{
90 switch ( technique )
91 {
98 return nullptr;
99
101 {
102 if ( context.isHighlighted() )
103 {
104 // QgsHighlightMaterial does not support lines
105 return nullptr;
106 }
107
108 QgsLineMaterial *mat = new QgsLineMaterial;
109 if ( !context.isSelected() )
110 {
111 mat->setLineColor( mAmbient );
112 mat->setUseVertexColors( dataDefinedProperties().isActive( QgsAbstractMaterialSettings::Property::Ambient ) );
113 }
114 else
115 {
116 // update the material with selection colors
117 mat->setLineColor( context.selectionColor() );
118 mat->setUseVertexColors( false );
119 }
120 return mat;
121 }
122 }
123 return nullptr;
124}
125
127{
128 QMap<QString, QString> parameters;
129 parameters[u"Ka"_s] = u"%1 %2 %3"_s.arg( mAmbient.redF() ).arg( mAmbient.greenF() ).arg( mAmbient.blueF() );
130 return parameters;
131}
132
133void QgsSimpleLineMaterialSettings::addParametersToEffect( Qt3DRender::QEffect *effect, const QgsMaterialContext &materialContext ) const
134{
135 const QColor ambient = materialContext.isSelected() ? materialContext.selectionColor().darker() : mAmbient;
136 Qt3DRender::QParameter *ambientParameter = new Qt3DRender::QParameter( u"ambientColor"_s, ambient );
137 effect->addParameter( ambientParameter );
138}
139
141{
142 const QColor ambient = dataDefinedProperties().valueAsColor( QgsAbstractMaterialSettings::Property::Ambient, expressionContext, mAmbient );
143
144 QByteArray array;
145 array.resize( sizeof( unsigned char ) * 3 );
146 unsigned char *fptr = reinterpret_cast<unsigned char *>( array.data() );
147
148 *fptr++ = static_cast<unsigned char>( ambient.red() );
149 *fptr++ = static_cast<unsigned char>( ambient.green() );
150 *fptr++ = static_cast<unsigned char>( ambient.blue() );
151 return array;
152}
153
154void QgsSimpleLineMaterialSettings::applyDataDefinedToGeometry( Qt3DCore::QGeometry *geometry, int vertexCount, const QByteArray &data ) const
155{
156 Qt3DCore::QBuffer *dataBuffer = new Qt3DCore::QBuffer( geometry );
157
158 Qt3DCore::QAttribute *colorAttribute = new Qt3DCore::QAttribute( geometry );
159 colorAttribute->setName( u"dataDefinedColor"_s );
160 colorAttribute->setVertexBaseType( Qt3DCore::QAttribute::UnsignedByte );
161 colorAttribute->setVertexSize( 3 );
162 colorAttribute->setAttributeType( Qt3DCore::QAttribute::VertexAttribute );
163 colorAttribute->setBuffer( dataBuffer );
164 colorAttribute->setByteStride( 3 * sizeof( unsigned char ) );
165 colorAttribute->setByteOffset( 0 );
166 colorAttribute->setCount( vertexCount );
167 geometry->addAttribute( colorAttribute );
168
169 dataBuffer->setData( data );
170}
Abstract base class for material settings.
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.
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...
Context settings for a material.
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.
bool isHighlighted() const
Returns true if the material should represent a highlighted state.
Base class for all materials used within QGIS 3D views.
Definition qgsmaterial.h:39
A container for the context for various read/write operations on 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.
@ 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).