QGIS API Documentation 3.43.0-Master (32433f7016e)
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
166class CORE_EXPORT QgsPropertyTransformer
167{
168
169#ifdef SIP_RUN
171 if ( sipCpp->transformerType() == QgsPropertyTransformer::GenericNumericTransformer )
172 sipType = sipType_QgsGenericNumericTransformer;
173 else if ( sipCpp->transformerType() == QgsPropertyTransformer::SizeScaleTransformer )
174 sipType = sipType_QgsSizeScaleTransformer;
175 else if ( sipCpp->transformerType() == QgsPropertyTransformer::ColorRampTransformer )
176 sipType = sipType_QgsColorRampTransformer;
177 else
178 sipType = sipType_QgsPropertyTransformer;
179 SIP_END
180#endif
181
182 public:
183
191
196 static QgsPropertyTransformer *create( Type type ) SIP_FACTORY;
197
203 QgsPropertyTransformer( double minValue = 0.0, double maxValue = 1.0 );
204
206 QgsPropertyTransformer &operator=( const QgsPropertyTransformer &other );
207
209
213 virtual Type transformerType() const = 0;
214
219
226 virtual bool loadVariant( const QVariant &transformer );
227
234 virtual QVariant toVariant() const;
235
241 double minValue() const { return mMinValue; }
242
249 void setMinValue( double min ) { mMinValue = min; }
250
256 double maxValue() const { return mMaxValue; }
257
264 void setMaxValue( double max ) { mMaxValue = max; }
265
271 QgsCurveTransform *curveTransform() const { return mCurveTransform.get(); }
272
279 void setCurveTransform( QgsCurveTransform *transform SIP_TRANSFER ) { mCurveTransform.reset( transform ); }
280
287 virtual QVariant transform( const QgsExpressionContext &context, const QVariant &value ) const = 0;
288
293 virtual QString toExpression( const QString &baseExpression ) const = 0;
294
302 static QgsPropertyTransformer *fromExpression( const QString &expression, QString &baseExpression SIP_OUT, QString &fieldName SIP_OUT ) SIP_FACTORY;
303
304 protected:
305
311 double transformNumeric( double input ) const;
312#ifndef SIP_RUN
314 double mMinValue;
315
317 double mMaxValue;
318
320 std::unique_ptr< QgsCurveTransform > mCurveTransform;
321#endif
322};
323
331{
332 public:
333
343 QgsGenericNumericTransformer( double minValue = 0.0,
344 double maxValue = 1.0,
345 double minOutput = 0.0,
346 double maxOutput = 1.0,
347 double nullOutput = 0.0,
348 double exponent = 1.0 );
349
350 Type transformerType() const override { return GenericNumericTransformer; }
351 QgsGenericNumericTransformer *clone() const override SIP_FACTORY;
352 QVariant toVariant() const override;
353 bool loadVariant( const QVariant &definition ) override;
354 QVariant transform( const QgsExpressionContext &context, const QVariant &value ) const override;
355 QString toExpression( const QString &baseExpression ) const override;
356
369 static QgsGenericNumericTransformer *fromExpression( const QString &expression, QString &baseExpression SIP_OUT, QString &fieldName SIP_OUT ) SIP_FACTORY; // cppcheck-suppress duplInheritedMember
370
375 double value( double input ) const;
376
382 double minOutputValue() const { return mMinOutput; }
383
390 void setMinOutputValue( double size ) { mMinOutput = size; }
391
396 double maxOutputValue() const { return mMaxOutput; }
397
404 void setMaxOutputValue( double size ) { mMaxOutput = size; }
405
410 double nullOutputValue() const { return mNullOutput; }
411
417 void setNullOutputValue( double size ) { mNullOutput = size; }
418
423 double exponent() const { return mExponent; }
424
430 void setExponent( double exponent ) { mExponent = exponent; }
431
432 private:
433 double mMinOutput;
434 double mMaxOutput;
435 double mNullOutput;
436 double mExponent;
437
438};
439
448{
449 public:
450
459
470 QgsSizeScaleTransformer( ScaleType type = Linear,
471 double minValue = 0.0,
472 double maxValue = 1.0,
473 double minSize = 0.0,
474 double maxSize = 1.0,
475 double nullSize = 0.0,
476 double exponent = 1.0 );
477
478 Type transformerType() const override { return SizeScaleTransformer; }
479 QgsSizeScaleTransformer *clone() const override SIP_FACTORY;
480 QVariant toVariant() const override;
481 bool loadVariant( const QVariant &definition ) override;
482 QVariant transform( const QgsExpressionContext &context, const QVariant &value ) const override;
483 QString toExpression( const QString &baseExpression ) const override;
484
492 static QgsSizeScaleTransformer *fromExpression( const QString &expression, QString &baseExpression SIP_OUT, QString &fieldName SIP_OUT ) SIP_FACTORY; // cppcheck-suppress duplInheritedMember
493
499 double size( double value ) const;
500
506 double minSize() const { return mMinSize; }
507
514 void setMinSize( double size ) { mMinSize = size; }
515
520 double maxSize() const { return mMaxSize; }
521
528 void setMaxSize( double size ) { mMaxSize = size; }
529
534 double nullSize() const { return mNullSize; }
535
541 void setNullSize( double size ) { mNullSize = size; }
542
548 double exponent() const { return mExponent; }
549
555 void setExponent( double exponent ) { mExponent = exponent; }
556
562 ScaleType type() const { return mType; }
563
570 void setType( ScaleType type );
571
572 private:
573 ScaleType mType = Linear;
574 double mMinSize;
575 double mMaxSize;
576 double mNullSize;
577 double mExponent;
578
579};
580
589{
590 public:
591
600 QgsColorRampTransformer( double minValue = 0.0,
601 double maxValue = 1.0,
602 QgsColorRamp *ramp SIP_TRANSFER = nullptr,
603 const QColor &nullColor = QColor( 0, 0, 0, 0 ),
604 const QString &rampName = QString() );
605
607 QgsColorRampTransformer &operator=( const QgsColorRampTransformer &other );
608
609 Type transformerType() const override { return ColorRampTransformer; }
610 QgsColorRampTransformer *clone() const override SIP_FACTORY;
611 QVariant toVariant() const override;
612 bool loadVariant( const QVariant &definition ) override;
613 QVariant transform( const QgsExpressionContext &context, const QVariant &value ) const override;
614 QString toExpression( const QString &baseExpression ) const override;
615
621 QColor color( double value ) const;
622
628 QgsColorRamp *colorRamp() const;
629
635 void setColorRamp( QgsColorRamp *ramp SIP_TRANSFER );
636
641 QColor nullColor() const { return mNullColor; }
642
648 void setNullColor( const QColor &color ) { mNullColor = color; }
649
654 QString rampName() const { return mRampName; }
655
662 void setRampName( const QString &name ) { mRampName = name; }
663
664 private:
665
666 std::unique_ptr< QgsColorRamp > mGradientRamp;
667 QColor mNullColor;
668 QString mRampName;
669
670};
671
672#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