QGIS API Documentation 3.39.0-Master (3aed037ce22)
Loading...
Searching...
No Matches
qgspropertytransformer.h
Go to the documentation of this file.
1/***************************************************************************
2 qgspropertytransformer.h
3 ------------------------
4 Date : January 2017
5 Copyright : (C) 2017 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#ifndef QGSPROPERTYTRANSFORMER_H
16#define QGSPROPERTYTRANSFORMER_H
17
18#include "qgis_core.h"
19#include "qgsexpression.h"
21#include "qgspointxy.h"
22#include <QVariant>
23#include <QHash>
24#include <QString>
25#include <QStringList>
26#include <QDomElement>
27#include <QDomDocument>
28#include <QColor>
29#include <memory>
30#include <algorithm>
31
32class QgsColorRamp;
33
34
55class CORE_EXPORT QgsCurveTransform
56{
57 public:
58
64
70 QgsCurveTransform( const QList< QgsPointXY > &controlPoints );
71
73
75
76 QgsCurveTransform &operator=( const QgsCurveTransform &other );
77
82 QList< QgsPointXY > controlPoints() const { return mControlPoints; }
83
89 void setControlPoints( const QList< QgsPointXY > &points );
90
96 void addControlPoint( double x, double y );
97
103 void removeControlPoint( double x, double y );
104
108 double y( double x ) const;
109
115 QVector< double > y( const QVector< double > &x ) const;
116
123 bool readXml( const QDomElement &elem, const QDomDocument &doc );
124
131 bool writeXml( QDomElement &transformElem, QDomDocument &doc ) const;
132
139 QVariant toVariant() const;
140
147 bool loadVariant( const QVariant &transformer );
148
149 private:
150
151 void calcSecondDerivativeArray();
152
153 QList< QgsPointXY > mControlPoints;
154
155 double *mSecondDerivativeArray = nullptr;
156};
157
158
165class CORE_EXPORT QgsPropertyTransformer
166{
167
168#ifdef SIP_RUN
170 if ( sipCpp->transformerType() == QgsPropertyTransformer::GenericNumericTransformer )
171 sipType = sipType_QgsGenericNumericTransformer;
172 else if ( sipCpp->transformerType() == QgsPropertyTransformer::SizeScaleTransformer )
173 sipType = sipType_QgsSizeScaleTransformer;
174 else if ( sipCpp->transformerType() == QgsPropertyTransformer::ColorRampTransformer )
175 sipType = sipType_QgsColorRampTransformer;
176 else
177 sipType = sipType_QgsPropertyTransformer;
178 SIP_END
179#endif
180
181 public:
182
190
195 static QgsPropertyTransformer *create( Type type ) SIP_FACTORY;
196
202 QgsPropertyTransformer( double minValue = 0.0, double maxValue = 1.0 );
203
205 QgsPropertyTransformer &operator=( const QgsPropertyTransformer &other );
206
208
212 virtual Type transformerType() const = 0;
213
218
225 virtual bool loadVariant( const QVariant &transformer );
226
233 virtual QVariant toVariant() const;
234
240 double minValue() const { return mMinValue; }
241
248 void setMinValue( double min ) { mMinValue = min; }
249
255 double maxValue() const { return mMaxValue; }
256
263 void setMaxValue( double max ) { mMaxValue = max; }
264
270 QgsCurveTransform *curveTransform() const { return mCurveTransform.get(); }
271
278 void setCurveTransform( QgsCurveTransform *transform SIP_TRANSFER ) { mCurveTransform.reset( transform ); }
279
286 virtual QVariant transform( const QgsExpressionContext &context, const QVariant &value ) const = 0;
287
292 virtual QString toExpression( const QString &baseExpression ) const = 0;
293
306 static QgsPropertyTransformer *fromExpression( const QString &expression, QString &baseExpression SIP_OUT, QString &fieldName SIP_OUT ) SIP_FACTORY;
307
308 protected:
309
315 double transformNumeric( double input ) const;
316#ifndef SIP_RUN
318 double mMinValue;
319
321 double mMaxValue;
322
324 std::unique_ptr< QgsCurveTransform > mCurveTransform;
325#endif
326};
327
335{
336 public:
337
347 QgsGenericNumericTransformer( double minValue = 0.0,
348 double maxValue = 1.0,
349 double minOutput = 0.0,
350 double maxOutput = 1.0,
351 double nullOutput = 0.0,
352 double exponent = 1.0 );
353
354 Type transformerType() const override { return GenericNumericTransformer; }
355 QgsGenericNumericTransformer *clone() const override SIP_FACTORY;
356 QVariant toVariant() const override;
357 bool loadVariant( const QVariant &definition ) override;
358 QVariant transform( const QgsExpressionContext &context, const QVariant &value ) const override;
359 QString toExpression( const QString &baseExpression ) const override;
360
373 static QgsGenericNumericTransformer *fromExpression( const QString &expression, QString &baseExpression SIP_OUT, QString &fieldName SIP_OUT ) SIP_FACTORY;
374
379 double value( double input ) const;
380
386 double minOutputValue() const { return mMinOutput; }
387
394 void setMinOutputValue( double size ) { mMinOutput = size; }
395
400 double maxOutputValue() const { return mMaxOutput; }
401
408 void setMaxOutputValue( double size ) { mMaxOutput = size; }
409
414 double nullOutputValue() const { return mNullOutput; }
415
421 void setNullOutputValue( double size ) { mNullOutput = size; }
422
427 double exponent() const { return mExponent; }
428
434 void setExponent( double exponent ) { mExponent = exponent; }
435
436 private:
437 double mMinOutput;
438 double mMaxOutput;
439 double mNullOutput;
440 double mExponent;
441
442};
443
452{
453 public:
454
463
474 QgsSizeScaleTransformer( ScaleType type = Linear,
475 double minValue = 0.0,
476 double maxValue = 1.0,
477 double minSize = 0.0,
478 double maxSize = 1.0,
479 double nullSize = 0.0,
480 double exponent = 1.0 );
481
482 Type transformerType() const override { return SizeScaleTransformer; }
483 QgsSizeScaleTransformer *clone() const override SIP_FACTORY;
484 QVariant toVariant() const override;
485 bool loadVariant( const QVariant &definition ) override;
486 QVariant transform( const QgsExpressionContext &context, const QVariant &value ) const override;
487 QString toExpression( const QString &baseExpression ) const override;
488
501 static QgsSizeScaleTransformer *fromExpression( const QString &expression, QString &baseExpression SIP_OUT, QString &fieldName SIP_OUT ) SIP_FACTORY;
502
508 double size( double value ) const;
509
515 double minSize() const { return mMinSize; }
516
523 void setMinSize( double size ) { mMinSize = size; }
524
529 double maxSize() const { return mMaxSize; }
530
537 void setMaxSize( double size ) { mMaxSize = size; }
538
543 double nullSize() const { return mNullSize; }
544
550 void setNullSize( double size ) { mNullSize = size; }
551
557 double exponent() const { return mExponent; }
558
564 void setExponent( double exponent ) { mExponent = exponent; }
565
571 ScaleType type() const { return mType; }
572
579 void setType( ScaleType type );
580
581 private:
582 ScaleType mType = Linear;
583 double mMinSize;
584 double mMaxSize;
585 double mNullSize;
586 double mExponent;
587
588};
589
598{
599 public:
600
609 QgsColorRampTransformer( double minValue = 0.0,
610 double maxValue = 1.0,
611 QgsColorRamp *ramp SIP_TRANSFER = nullptr,
612 const QColor &nullColor = QColor( 0, 0, 0, 0 ),
613 const QString &rampName = QString() );
614
616 QgsColorRampTransformer &operator=( const QgsColorRampTransformer &other );
617
618 Type transformerType() const override { return ColorRampTransformer; }
619 QgsColorRampTransformer *clone() const override SIP_FACTORY;
620 QVariant toVariant() const override;
621 bool loadVariant( const QVariant &definition ) override;
622 QVariant transform( const QgsExpressionContext &context, const QVariant &value ) const override;
623 QString toExpression( const QString &baseExpression ) const override;
624
630 QColor color( double value ) const;
631
637 QgsColorRamp *colorRamp() const;
638
644 void setColorRamp( QgsColorRamp *ramp SIP_TRANSFER );
645
650 QColor nullColor() const { return mNullColor; }
651
657 void setNullColor( const QColor &color ) { mNullColor = color; }
658
663 QString rampName() const { return mRampName; }
664
671 void setRampName( const QString &name ) { mRampName = name; }
672
673 private:
674
675 std::unique_ptr< QgsColorRamp > mGradientRamp;
676 QColor mNullColor;
677 QString mRampName;
678
679};
680
681#endif // QGSPROPERTYTRANSFORMER_H
QgsPropertyTransformer subclass for transforming a numeric value into a color from a color ramp.
void setRampName(const QString &name)
Sets the color ramp's name.
QString rampName() const
Returns the color ramp's name.
void setNullColor(const QColor &color)
Sets the color corresponding to a null value.
Type transformerType() const override
Returns the transformer type.
Abstract base class for color ramps.
Handles scaling of input values to output values by using a curve created from smoothly joining a num...
QList< QgsPointXY > controlPoints() const
Returns a list of the control points for the transform.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
QgsPropertyTransformer subclass for scaling an input numeric value into an output numeric value.
double maxOutputValue() const
Returns the maximum calculated size.
void setMinOutputValue(double size)
Sets the minimum calculated size.
void setExponent(double exponent)
Sets the exponent for an exponential expression.
double nullOutputValue() const
Returns the size value when an expression evaluates to NULL.
double exponent() const
Returns the exponent for an exponential expression.
Type transformerType() const override
Returns the transformer type.
void setMaxOutputValue(double size)
Sets the maximum calculated size.
void setNullOutputValue(double size)
Sets the size value for when an expression evaluates to NULL.
Abstract base class for objects which transform the calculated value of a property.
QgsCurveTransform * curveTransform() const
Returns the curve transform applied to input values before they are transformed by the individual tra...
virtual QVariant transform(const QgsExpressionContext &context, const QVariant &value) const =0
Calculates the transform of a value.
void setMinValue(double min)
Sets the minimum value expected by the transformer.
virtual QString toExpression(const QString &baseExpression) const =0
Converts the transformer to a QGIS expression string.
double mMinValue
Minimum value expected by the transformer.
double maxValue() const
Returns the maximum value expected by the transformer.
std::unique_ptr< QgsCurveTransform > mCurveTransform
Optional curve transform.
virtual ~QgsPropertyTransformer()
@ GenericNumericTransformer
Generic transformer for numeric values (QgsGenericNumericTransformer)
@ SizeScaleTransformer
Size scaling transformer (QgsSizeScaleTransformer)
@ ColorRampTransformer
Color ramp transformer (QgsColorRampTransformer)
virtual QgsPropertyTransformer * clone() const =0
Returns a clone of the transformer.
void setMaxValue(double max)
Sets the maximum value expected by the transformer.
virtual Type transformerType() const =0
Returns the transformer type.
double minValue() const
Returns the minimum value expected by the transformer.
double mMaxValue
Maximum value expected by the transformer.
void setCurveTransform(QgsCurveTransform *transform)
Sets a curve transform to apply to input values before they are transformed by the individual transfo...
QgsPropertyTransformer subclass for scaling a value into a size according to various scaling methods.
ScaleType type() const
Returns the size transformer's scaling type (the method used to calculate the size from a value).
void setMaxSize(double size)
Sets the maximum calculated size.
Type transformerType() const override
Returns the transformer type.
double maxSize() const
Returns the maximum calculated size.
double nullSize() const
Returns the size value when an expression evaluates to NULL.
void setMinSize(double size)
Sets the minimum calculated size.
void setNullSize(double size)
Sets the size value for when an expression evaluates to NULL.
@ Exponential
Scale using set exponent.
@ Flannery
Flannery scaling method.
void setExponent(double exponent)
Sets the exponent for an exponential expression.
double exponent() const
Returns the exponent for an exponential expression.
#define SIP_CONVERT_TO_SUBCLASS_CODE(code)
Definition qgis_sip.h:191
#define SIP_TRANSFER
Definition qgis_sip.h:36
#define SIP_OUT
Definition qgis_sip.h:58
#define SIP_FACTORY
Definition qgis_sip.h:76
#define SIP_END
Definition qgis_sip.h:208