QGIS API Documentation 3.99.0-Master (26c88405ac0)
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:
60
66
72 QgsCurveTransform( const QList< QgsPointXY > &controlPoints );
73
75
77
79
84 QList< QgsPointXY > controlPoints() const { return mControlPoints; }
85
91 void setControlPoints( const QList< QgsPointXY > &points );
92
98 void addControlPoint( double x, double y );
99
105 void removeControlPoint( double x, double y );
106
110 double y( double x ) const;
111
117 QVector< double > y( const QVector< double > &x ) const;
118
125 bool readXml( const QDomElement &elem, const QDomDocument &doc );
126
133 bool writeXml( QDomElement &transformElem, QDomDocument &doc ) const;
134
141 QVariant toVariant() const;
142
149 bool loadVariant( const QVariant &transformer );
150
151 private:
152
153 void calcSecondDerivativeArray();
154
155 QList< QgsPointXY > mControlPoints;
156
157 double *mSecondDerivativeArray = nullptr;
158};
159
160
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
193
198 static QgsPropertyTransformer *create( Type type ) SIP_FACTORY;
199
205 QgsPropertyTransformer( double minValue = 0.0, double maxValue = 1.0 );
206
208 QgsPropertyTransformer &operator=( const QgsPropertyTransformer &other );
209
211
215 virtual Type transformerType() const = 0;
216
221
228 virtual bool loadVariant( const QVariant &transformer );
229
236 virtual QVariant toVariant() const;
237
243 double minValue() const { return mMinValue; }
244
251 void setMinValue( double min ) { mMinValue = min; }
252
258 double maxValue() const { return mMaxValue; }
259
266 void setMaxValue( double max ) { mMaxValue = max; }
267
274
282
289 virtual QVariant transform( const QgsExpressionContext &context, const QVariant &value ) const = 0;
290
295 virtual QString toExpression( const QString &baseExpression ) const = 0;
296
304 static QgsPropertyTransformer *fromExpression( const QString &expression, QString &baseExpression SIP_OUT, QString &fieldName SIP_OUT ) SIP_FACTORY;
305
306 protected:
307
313 double transformNumeric( double input ) const;
314#ifndef SIP_RUN
316 double mMinValue;
317
319 double mMaxValue;
320
322 std::unique_ptr< QgsCurveTransform > mCurveTransform;
323#endif
324};
325
331
333{
334 public:
335
346 double maxValue = 1.0,
347 double minOutput = 0.0,
348 double maxOutput = 1.0,
349 double nullOutput = 0.0,
350 double exponent = 1.0 );
351
352 Type transformerType() const override { return GenericNumericTransformer; }
353 QgsGenericNumericTransformer *clone() const override SIP_FACTORY;
354 QVariant toVariant() const override;
355 bool loadVariant( const QVariant &definition ) override;
356 QVariant transform( const QgsExpressionContext &context, const QVariant &value ) const override;
357 QString toExpression( const QString &baseExpression ) const override;
358
371 static QgsGenericNumericTransformer *fromExpression( const QString &expression, QString &baseExpression SIP_OUT, QString &fieldName SIP_OUT ) SIP_FACTORY; // cppcheck-suppress duplInheritedMember
372
377 double value( double input ) const;
378
384 double minOutputValue() const { return mMinOutput; }
385
392 void setMinOutputValue( double size ) { mMinOutput = size; }
393
398 double maxOutputValue() const { return mMaxOutput; }
399
406 void setMaxOutputValue( double size ) { mMaxOutput = size; }
407
412 double nullOutputValue() const { return mNullOutput; }
413
419 void setNullOutputValue( double size ) { mNullOutput = size; }
420
425 double exponent() const { return mExponent; }
426
432 void setExponent( double exponent ) { mExponent = exponent; }
433
434 private:
435 double mMinOutput;
436 double mMaxOutput;
437 double mNullOutput;
438 double mExponent;
439
440};
441
448
450{
451 public:
452
461
472 QgsSizeScaleTransformer( ScaleType type = Linear,
473 double minValue = 0.0,
474 double maxValue = 1.0,
475 double minSize = 0.0,
476 double maxSize = 1.0,
477 double nullSize = 0.0,
478 double exponent = 1.0 );
479
480 Type transformerType() const override { return SizeScaleTransformer; }
481 QgsSizeScaleTransformer *clone() const override SIP_FACTORY;
482 QVariant toVariant() const override;
483 bool loadVariant( const QVariant &definition ) override;
484 QVariant transform( const QgsExpressionContext &context, const QVariant &value ) const override;
485 QString toExpression( const QString &baseExpression ) const override;
486
494 static QgsSizeScaleTransformer *fromExpression( const QString &expression, QString &baseExpression SIP_OUT, QString &fieldName SIP_OUT ) SIP_FACTORY; // cppcheck-suppress duplInheritedMember
495
501 double size( double value ) const;
502
508 double minSize() const { return mMinSize; }
509
516 void setMinSize( double size ) { mMinSize = size; }
517
522 double maxSize() const { return mMaxSize; }
523
530 void setMaxSize( double size ) { mMaxSize = size; }
531
536 double nullSize() const { return mNullSize; }
537
543 void setNullSize( double size ) { mNullSize = size; }
544
550 double exponent() const { return mExponent; }
551
557 void setExponent( double exponent ) { mExponent = exponent; }
558
564 ScaleType type() const { return mType; }
565
572 void setType( ScaleType type );
573
574 private:
575 ScaleType mType = Linear;
576 double mMinSize;
577 double mMaxSize;
578 double mNullSize;
579 double mExponent;
580
581};
582
589
591{
592 public:
593
602 QgsColorRampTransformer( double minValue = 0.0,
603 double maxValue = 1.0,
604 QgsColorRamp *ramp SIP_TRANSFER = nullptr,
605 const QColor &nullColor = QColor( 0, 0, 0, 0 ),
606 const QString &rampName = QString() );
607
610
611 Type transformerType() const override { return ColorRampTransformer; }
612 QgsColorRampTransformer *clone() const override SIP_FACTORY;
613 QVariant toVariant() const override;
614 bool loadVariant( const QVariant &definition ) override;
615 QVariant transform( const QgsExpressionContext &context, const QVariant &value ) const override;
616 QString toExpression( const QString &baseExpression ) const override;
617
623 QColor color( double value ) const;
624
630 QgsColorRamp *colorRamp() const;
631
637 void setColorRamp( QgsColorRamp *ramp SIP_TRANSFER );
638
643 QColor nullColor() const { return mNullColor; }
644
650 void setNullColor( const QColor &color ) { mNullColor = color; }
651
656 QString rampName() const { return mRampName; }
657
664 void setRampName( const QString &name ) { mRampName = name; }
665
666 private:
667
668 std::unique_ptr< QgsColorRamp > mGradientRamp;
669 QColor mNullColor;
670 QString mRampName;
671
672};
673
674#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:199
#define SIP_TRANSFER
Definition qgis_sip.h:36
#define SIP_OUT
Definition qgis_sip.h:58
#define SIP_FACTORY
Definition qgis_sip.h:84
#define SIP_END
Definition qgis_sip.h:216