QGIS API Documentation 4.1.0-Master (60fea48833c)
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 <algorithm>
19#include <memory>
20
21#include "qgis_core.h"
22#include "qgsexpression.h"
24#include "qgspointxy.h"
25
26#include <QColor>
27#include <QDomDocument>
28#include <QDomElement>
29#include <QHash>
30#include <QString>
31#include <QStringList>
32#include <QVariant>
33
34class QgsColorRamp;
35
36
56
57class CORE_EXPORT QgsCurveTransform
58{
59 public:
65
71 QgsCurveTransform( const QList< QgsPointXY > &controlPoints );
72
74
76
78
83 QList< QgsPointXY > controlPoints() const { return mControlPoints; }
84
90 void setControlPoints( const QList< QgsPointXY > &points );
91
97 void addControlPoint( double x, double y );
98
104 void removeControlPoint( double x, double y );
105
109 double y( double x ) const;
110
116 QVector< double > y( const QVector< double > &x ) const;
117
124 bool readXml( const QDomElement &elem, const QDomDocument &doc );
125
132 bool writeXml( QDomElement &transformElem, QDomDocument &doc ) const;
133
140 QVariant toVariant() const;
141
148 bool loadVariant( const QVariant &transformer );
149
150 private:
151 void calcSecondDerivativeArray();
152
153 QList< QgsPointXY > mControlPoints;
154
155 double *mSecondDerivativeArray = nullptr;
156};
157
158
166class CORE_EXPORT QgsPropertyTransformer
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
271
279
286 virtual QVariant transform( const QgsExpressionContext &context, const QVariant &value ) const = 0;
287
292 virtual QString toExpression( const QString &baseExpression ) const = 0;
293
301 static QgsPropertyTransformer *fromExpression( const QString &expression, QString &baseExpression SIP_OUT, QString &fieldName SIP_OUT ) SIP_FACTORY;
302
303 protected:
309 double transformNumeric( double input ) const;
310#ifndef SIP_RUN
312 double mMinValue;
313
315 double mMaxValue;
316
318 std::unique_ptr< QgsCurveTransform > mCurveTransform;
319#endif
320};
321
327
329{
330 public:
340 QgsGenericNumericTransformer( double minValue = 0.0, double maxValue = 1.0, double minOutput = 0.0, double maxOutput = 1.0, double nullOutput = 0.0, double exponent = 1.0 );
341
342 Type transformerType() const override { return GenericNumericTransformer; }
343 QgsGenericNumericTransformer *clone() const override SIP_FACTORY;
344 QVariant toVariant() const override;
345 bool loadVariant( const QVariant &definition ) override;
346 QVariant transform( const QgsExpressionContext &context, const QVariant &value ) const override;
347 QString toExpression( const QString &baseExpression ) const override;
348
361 static QgsGenericNumericTransformer *fromExpression( const QString &expression, QString &baseExpression SIP_OUT, QString &fieldName SIP_OUT ) SIP_FACTORY; // cppcheck-suppress duplInheritedMember
362
367 double value( double input ) const;
368
374 double minOutputValue() const { return mMinOutput; }
375
382 void setMinOutputValue( double size ) { mMinOutput = size; }
383
388 double maxOutputValue() const { return mMaxOutput; }
389
396 void setMaxOutputValue( double size ) { mMaxOutput = size; }
397
402 double nullOutputValue() const { return mNullOutput; }
403
409 void setNullOutputValue( double size ) { mNullOutput = size; }
410
415 double exponent() const { return mExponent; }
416
422 void setExponent( double exponent ) { mExponent = exponent; }
423
424 private:
425 double mMinOutput;
426 double mMaxOutput;
427 double mNullOutput;
428 double mExponent;
429};
430
437
439{
440 public:
449
460 QgsSizeScaleTransformer( ScaleType type = Linear, double minValue = 0.0, double maxValue = 1.0, double minSize = 0.0, double maxSize = 1.0, double nullSize = 0.0, double exponent = 1.0 );
461
462 Type transformerType() const override { return SizeScaleTransformer; }
463 QgsSizeScaleTransformer *clone() const override SIP_FACTORY;
464 QVariant toVariant() const override;
465 bool loadVariant( const QVariant &definition ) override;
466 QVariant transform( const QgsExpressionContext &context, const QVariant &value ) const override;
467 QString toExpression( const QString &baseExpression ) const override;
468
476 static QgsSizeScaleTransformer *fromExpression( const QString &expression, QString &baseExpression SIP_OUT, QString &fieldName SIP_OUT ) SIP_FACTORY; // cppcheck-suppress duplInheritedMember
477
483 double size( double value ) const;
484
490 double minSize() const { return mMinSize; }
491
498 void setMinSize( double size ) { mMinSize = size; }
499
504 double maxSize() const { return mMaxSize; }
505
512 void setMaxSize( double size ) { mMaxSize = size; }
513
518 double nullSize() const { return mNullSize; }
519
525 void setNullSize( double size ) { mNullSize = size; }
526
532 double exponent() const { return mExponent; }
533
539 void setExponent( double exponent ) { mExponent = exponent; }
540
546 ScaleType type() const { return mType; }
547
554 void setType( ScaleType type );
555
556 private:
557 ScaleType mType = Linear;
558 double mMinSize;
559 double mMaxSize;
560 double mNullSize;
561 double mExponent;
562};
563
570
572{
573 public:
582 QgsColorRampTransformer( double minValue = 0.0, double maxValue = 1.0, QgsColorRamp *ramp SIP_TRANSFER = nullptr, const QColor &nullColor = QColor( 0, 0, 0, 0 ), const QString &rampName = QString() );
583
586
587 Type transformerType() const override { return ColorRampTransformer; }
588 QgsColorRampTransformer *clone() const override SIP_FACTORY;
589 QVariant toVariant() const override;
590 bool loadVariant( const QVariant &definition ) override;
591 QVariant transform( const QgsExpressionContext &context, const QVariant &value ) const override;
592 QString toExpression( const QString &baseExpression ) const override;
593
599 QColor color( double value ) const;
600
606 QgsColorRamp *colorRamp() const;
607
613 void setColorRamp( QgsColorRamp *ramp SIP_TRANSFER );
614
619 QColor nullColor() const { return mNullColor; }
620
626 void setNullColor( const QColor &color ) { mNullColor = color; }
627
632 QString rampName() const { return mRampName; }
633
640 void setRampName( const QString &name ) { mRampName = name; }
641
642 private:
643 std::unique_ptr< QgsColorRamp > mGradientRamp;
644 QColor mNullColor;
645 QString mRampName;
646};
647
648#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.
QColor color(double value) const
Calculates the color corresponding to a specific value.
QColor nullColor() const
Returns the color corresponding to a null value.
QgsColorRampTransformer & operator=(const QgsColorRampTransformer &other)
QgsColorRampTransformer(double minValue=0.0, double maxValue=1.0, QgsColorRamp *ramp=nullptr, const QColor &nullColor=QColor(0, 0, 0, 0), const QString &rampName=QString())
Constructor for QgsColorRampTransformer.
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...
QgsCurveTransform & operator=(const QgsCurveTransform &other)
QgsCurveTransform()
Constructs a default QgsCurveTransform which linearly maps values between 0 and 1 unchanged.
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.
double minOutputValue() const
Returns the minimum 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.
QgsGenericNumericTransformer(double minValue=0.0, double maxValue=1.0, double minOutput=0.0, double maxOutput=1.0, double nullOutput=0.0, double exponent=1.0)
Constructor for QgsGenericNumericTransformer.
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.
static QgsPropertyTransformer * fromExpression(const QString &expression, QString &baseExpression, QString &fieldName)
Attempts to parse an expression into a corresponding property transformer.
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.
virtual bool loadVariant(const QVariant &transformer)
Loads this transformer from a QVariantMap, wrapped in a QVariant.
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.
virtual QVariant toVariant() const
Saves this transformer to a QVariantMap, wrapped in a QVariant.
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.
QgsPropertyTransformer(double minValue=0.0, double maxValue=1.0)
Constructor for QgsPropertyTransformer.
virtual ~QgsPropertyTransformer()
@ GenericNumericTransformer
Generic transformer for numeric values (QgsGenericNumericTransformer).
@ SizeScaleTransformer
Size scaling transformer (QgsSizeScaleTransformer).
@ ColorRampTransformer
Color ramp transformer (QgsColorRampTransformer).
double transformNumeric(double input) const
Applies base class numeric transformations.
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.
double minSize() const
Returns the minimum calculated size.
void setNullSize(double size)
Sets the size value for when an expression evaluates to NULL.
double size(double value) const
Calculates the size corresponding to a specific value.
@ 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.
QgsSizeScaleTransformer(ScaleType type=Linear, double minValue=0.0, double maxValue=1.0, double minSize=0.0, double maxSize=1.0, double nullSize=0.0, double exponent=1.0)
Constructor for QgsSizeScaleTransformer.
#define SIP_CONVERT_TO_SUBCLASS_CODE(code)
Definition qgis_sip.h:198
#define SIP_TRANSFER
Definition qgis_sip.h:35
#define SIP_OUT
Definition qgis_sip.h:57
#define SIP_FACTORY
Definition qgis_sip.h:83
#define SIP_END
Definition qgis_sip.h:215