QGIS API Documentation 3.37.0-Master (fdefdf9c27f)
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
78
79 QgsCurveTransform &operator=( const QgsCurveTransform &other );
80
85 QList< QgsPointXY > controlPoints() const { return mControlPoints; }
86
92 void setControlPoints( const QList< QgsPointXY > &points );
93
99 void addControlPoint( double x, double y );
100
106 void removeControlPoint( double x, double y );
107
111 double y( double x ) const;
112
118 QVector< double > y( const QVector< double > &x ) const;
119
126 bool readXml( const QDomElement &elem, const QDomDocument &doc );
127
134 bool writeXml( QDomElement &transformElem, QDomDocument &doc ) const;
135
142 QVariant toVariant() const;
143
150 bool loadVariant( const QVariant &transformer );
151
152 private:
153
154 void calcSecondDerivativeArray();
155
156 QList< QgsPointXY > mControlPoints;
157
158 double *mSecondDerivativeArray = nullptr;
159};
160
161
168class CORE_EXPORT QgsPropertyTransformer
169{
170
171#ifdef SIP_RUN
173 if ( sipCpp->transformerType() == QgsPropertyTransformer::GenericNumericTransformer )
174 sipType = sipType_QgsGenericNumericTransformer;
175 else if ( sipCpp->transformerType() == QgsPropertyTransformer::SizeScaleTransformer )
176 sipType = sipType_QgsSizeScaleTransformer;
177 else if ( sipCpp->transformerType() == QgsPropertyTransformer::ColorRampTransformer )
178 sipType = sipType_QgsColorRampTransformer;
179 else
180 sipType = sipType_QgsPropertyTransformer;
181 SIP_END
182#endif
183
184 public:
185
187 enum Type
188 {
192 };
193
198 static QgsPropertyTransformer *create( Type type ) SIP_FACTORY;
199
205 QgsPropertyTransformer( double minValue = 0.0, double maxValue = 1.0 );
206
211 QgsPropertyTransformer &operator=( const QgsPropertyTransformer &other );
212
214
218 virtual Type transformerType() const = 0;
219
224
231 virtual bool loadVariant( const QVariant &transformer );
232
239 virtual QVariant toVariant() const;
240
246 double minValue() const { return mMinValue; }
247
254 void setMinValue( double min ) { mMinValue = min; }
255
261 double maxValue() const { return mMaxValue; }
262
269 void setMaxValue( double max ) { mMaxValue = max; }
270
276 QgsCurveTransform *curveTransform() const { return mCurveTransform.get(); }
277
284 void setCurveTransform( QgsCurveTransform *transform SIP_TRANSFER ) { mCurveTransform.reset( transform ); }
285
292 virtual QVariant transform( const QgsExpressionContext &context, const QVariant &value ) const = 0;
293
298 virtual QString toExpression( const QString &baseExpression ) const = 0;
299
312 static QgsPropertyTransformer *fromExpression( const QString &expression, QString &baseExpression SIP_OUT, QString &fieldName SIP_OUT ) SIP_FACTORY;
313
314 protected:
315
321 double transformNumeric( double input ) const;
322#ifndef SIP_RUN
324 double mMinValue;
325
327 double mMaxValue;
328
330 std::unique_ptr< QgsCurveTransform > mCurveTransform;
331#endif
332};
333
341{
342 public:
343
353 QgsGenericNumericTransformer( double minValue = 0.0,
354 double maxValue = 1.0,
355 double minOutput = 0.0,
356 double maxOutput = 1.0,
357 double nullOutput = 0.0,
358 double exponent = 1.0 );
359
360 Type transformerType() const override { return GenericNumericTransformer; }
361 QgsGenericNumericTransformer *clone() const override SIP_FACTORY;
362 QVariant toVariant() const override;
363 bool loadVariant( const QVariant &definition ) override;
364 QVariant transform( const QgsExpressionContext &context, const QVariant &value ) const override;
365 QString toExpression( const QString &baseExpression ) const override;
366
379 static QgsGenericNumericTransformer *fromExpression( const QString &expression, QString &baseExpression SIP_OUT, QString &fieldName SIP_OUT ) SIP_FACTORY;
380
385 double value( double input ) const;
386
392 double minOutputValue() const { return mMinOutput; }
393
400 void setMinOutputValue( double size ) { mMinOutput = size; }
401
406 double maxOutputValue() const { return mMaxOutput; }
407
414 void setMaxOutputValue( double size ) { mMaxOutput = size; }
415
420 double nullOutputValue() const { return mNullOutput; }
421
427 void setNullOutputValue( double size ) { mNullOutput = size; }
428
433 double exponent() const { return mExponent; }
434
440 void setExponent( double exponent ) { mExponent = exponent; }
441
442 private:
443 double mMinOutput;
444 double mMaxOutput;
445 double mNullOutput;
446 double mExponent;
447
448};
449
458{
459 public:
460
463 {
468 };
469
480 QgsSizeScaleTransformer( ScaleType type = Linear,
481 double minValue = 0.0,
482 double maxValue = 1.0,
483 double minSize = 0.0,
484 double maxSize = 1.0,
485 double nullSize = 0.0,
486 double exponent = 1.0 );
487
488 Type transformerType() const override { return SizeScaleTransformer; }
489 QgsSizeScaleTransformer *clone() const override SIP_FACTORY;
490 QVariant toVariant() const override;
491 bool loadVariant( const QVariant &definition ) override;
492 QVariant transform( const QgsExpressionContext &context, const QVariant &value ) const override;
493 QString toExpression( const QString &baseExpression ) const override;
494
507 static QgsSizeScaleTransformer *fromExpression( const QString &expression, QString &baseExpression SIP_OUT, QString &fieldName SIP_OUT ) SIP_FACTORY;
508
514 double size( double value ) const;
515
521 double minSize() const { return mMinSize; }
522
529 void setMinSize( double size ) { mMinSize = size; }
530
535 double maxSize() const { return mMaxSize; }
536
543 void setMaxSize( double size ) { mMaxSize = size; }
544
549 double nullSize() const { return mNullSize; }
550
556 void setNullSize( double size ) { mNullSize = size; }
557
563 double exponent() const { return mExponent; }
564
570 void setExponent( double exponent ) { mExponent = exponent; }
571
577 ScaleType type() const { return mType; }
578
585 void setType( ScaleType type );
586
587 private:
588 ScaleType mType = Linear;
589 double mMinSize;
590 double mMaxSize;
591 double mNullSize;
592 double mExponent;
593
594};
595
604{
605 public:
606
615 QgsColorRampTransformer( double minValue = 0.0,
616 double maxValue = 1.0,
617 QgsColorRamp *ramp SIP_TRANSFER = nullptr,
618 const QColor &nullColor = QColor( 0, 0, 0, 0 ),
619 const QString &rampName = QString() );
620
623
624 QgsColorRampTransformer &operator=( const QgsColorRampTransformer &other );
625
626 Type transformerType() const override { return ColorRampTransformer; }
627 QgsColorRampTransformer *clone() const override SIP_FACTORY;
628 QVariant toVariant() const override;
629 bool loadVariant( const QVariant &definition ) override;
630 QVariant transform( const QgsExpressionContext &context, const QVariant &value ) const override;
631 QString toExpression( const QString &baseExpression ) const override;
632
638 QColor color( double value ) const;
639
645 QgsColorRamp *colorRamp() const;
646
652 void setColorRamp( QgsColorRamp *ramp SIP_TRANSFER );
653
658 QColor nullColor() const { return mNullColor; }
659
665 void setNullColor( const QColor &color ) { mNullColor = color; }
666
671 QString rampName() const { return mRampName; }
672
679 void setRampName( const QString &name ) { mRampName = name; }
680
681 private:
682
683 std::unique_ptr< QgsColorRamp > mGradientRamp;
684 QColor mNullColor;
685 QString mRampName;
686
687};
688
689#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.
Definition: qgscolorramp.h:29
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.
ScaleType
Size scaling methods.
@ 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