QGIS API Documentation  3.16.0-Hannover (43b64b13f3)
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"
20 #include "qgsexpressioncontext.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 
32 class QgsColorRamp;
33 
34 
56 class CORE_EXPORT QgsCurveTransform
57 {
58  public:
59 
65 
71  QgsCurveTransform( const QList< QgsPointXY > &controlPoints );
72 
74 
78  QgsCurveTransform( const QgsCurveTransform &other );
79 
80  QgsCurveTransform &operator=( const QgsCurveTransform &other );
81 
86  QList< QgsPointXY > controlPoints() const { return mControlPoints; }
87 
93  void setControlPoints( const QList< QgsPointXY > &points );
94 
100  void addControlPoint( double x, double y );
101 
107  void removeControlPoint( double x, double y );
108 
112  double y( double x ) const;
113 
119  QVector< double > y( const QVector< double > &x ) const;
120 
127  bool readXml( const QDomElement &elem, const QDomDocument &doc );
128 
135  bool writeXml( QDomElement &transformElem, QDomDocument &doc ) const;
136 
143  QVariant toVariant() const;
144 
151  bool loadVariant( const QVariant &transformer );
152 
153  private:
154 
155  void calcSecondDerivativeArray();
156 
157  QList< QgsPointXY > mControlPoints;
158 
159  double *mSecondDerivativeArray = nullptr;
160 };
161 
162 
170 class CORE_EXPORT QgsPropertyTransformer
171 {
172 
173 #ifdef SIP_RUN
175  if ( sipCpp->transformerType() == QgsPropertyTransformer::GenericNumericTransformer )
176  sipType = sipType_QgsGenericNumericTransformer;
177  else if ( sipCpp->transformerType() == QgsPropertyTransformer::SizeScaleTransformer )
178  sipType = sipType_QgsSizeScaleTransformer;
179  else if ( sipCpp->transformerType() == QgsPropertyTransformer::ColorRampTransformer )
180  sipType = sipType_QgsColorRampTransformer;
181  else
182  sipType = sipType_QgsPropertyTransformer;
183  SIP_END
184 #endif
185 
186  public:
187 
189  enum Type
190  {
194  };
195 
200  static QgsPropertyTransformer *create( Type type ) SIP_FACTORY;
201 
207  QgsPropertyTransformer( double minValue = 0.0, double maxValue = 1.0 );
208 
213  QgsPropertyTransformer &operator=( const QgsPropertyTransformer &other );
214 
216 
220  virtual Type transformerType() const = 0;
221 
226 
233  virtual bool loadVariant( const QVariant &transformer );
234 
241  virtual QVariant toVariant() const;
242 
248  double minValue() const { return mMinValue; }
249 
256  void setMinValue( double min ) { mMinValue = min; }
257 
263  double maxValue() const { return mMaxValue; }
264 
271  void setMaxValue( double max ) { mMaxValue = max; }
272 
278  QgsCurveTransform *curveTransform() const { return mCurveTransform.get(); }
279 
286  void setCurveTransform( QgsCurveTransform *transform SIP_TRANSFER ) { mCurveTransform.reset( transform ); }
287 
294  virtual QVariant transform( const QgsExpressionContext &context, const QVariant &value ) const = 0;
295 
300  virtual QString toExpression( const QString &baseExpression ) const = 0;
301 
314  static QgsPropertyTransformer *fromExpression( const QString &expression, QString &baseExpression SIP_OUT, QString &fieldName SIP_OUT ) SIP_FACTORY;
315 
316  protected:
317 
323  double transformNumeric( double input ) const;
324 #ifndef SIP_RUN
325  double mMinValue;
327 
329  double mMaxValue;
330 
332  std::unique_ptr< QgsCurveTransform > mCurveTransform;
333 #endif
334 };
335 
344 {
345  public:
346 
356  QgsGenericNumericTransformer( double minValue = 0.0,
357  double maxValue = 1.0,
358  double minOutput = 0.0,
359  double maxOutput = 1.0,
360  double nullOutput = 0.0,
361  double exponent = 1.0 );
362 
363  Type transformerType() const override { return GenericNumericTransformer; }
365  QVariant toVariant() const override;
366  bool loadVariant( const QVariant &definition ) override;
367  QVariant transform( const QgsExpressionContext &context, const QVariant &value ) const override;
368  QString toExpression( const QString &baseExpression ) const override;
369 
382  static QgsGenericNumericTransformer *fromExpression( const QString &expression, QString &baseExpression SIP_OUT, QString &fieldName SIP_OUT ) SIP_FACTORY;
383 
388  double value( double input ) const;
389 
395  double minOutputValue() const { return mMinOutput; }
396 
403  void setMinOutputValue( double size ) { mMinOutput = size; }
404 
409  double maxOutputValue() const { return mMaxOutput; }
410 
417  void setMaxOutputValue( double size ) { mMaxOutput = size; }
418 
423  double nullOutputValue() const { return mNullOutput; }
424 
430  void setNullOutputValue( double size ) { mNullOutput = size; }
431 
436  double exponent() const { return mExponent; }
437 
443  void setExponent( double exponent ) { mExponent = exponent; }
444 
445  private:
446  double mMinOutput;
447  double mMaxOutput;
448  double mNullOutput;
449  double mExponent;
450 
451 };
452 
462 {
463  public:
464 
467  {
472  };
473 
484  QgsSizeScaleTransformer( ScaleType type = Linear,
485  double minValue = 0.0,
486  double maxValue = 1.0,
487  double minSize = 0.0,
488  double maxSize = 1.0,
489  double nullSize = 0.0,
490  double exponent = 1.0 );
491 
492  Type transformerType() const override { return SizeScaleTransformer; }
493  QgsSizeScaleTransformer *clone() const override SIP_FACTORY;
494  QVariant toVariant() const override;
495  bool loadVariant( const QVariant &definition ) override;
496  QVariant transform( const QgsExpressionContext &context, const QVariant &value ) const override;
497  QString toExpression( const QString &baseExpression ) const override;
498 
511  static QgsSizeScaleTransformer *fromExpression( const QString &expression, QString &baseExpression SIP_OUT, QString &fieldName SIP_OUT ) SIP_FACTORY;
512 
518  double size( double value ) const;
519 
525  double minSize() const { return mMinSize; }
526 
533  void setMinSize( double size ) { mMinSize = size; }
534 
539  double maxSize() const { return mMaxSize; }
540 
547  void setMaxSize( double size ) { mMaxSize = size; }
548 
553  double nullSize() const { return mNullSize; }
554 
560  void setNullSize( double size ) { mNullSize = size; }
561 
567  double exponent() const { return mExponent; }
568 
574  void setExponent( double exponent ) { mExponent = exponent; }
575 
581  ScaleType type() const { return mType; }
582 
589  void setType( ScaleType type );
590 
591  private:
592  ScaleType mType = Linear;
593  double mMinSize;
594  double mMaxSize;
595  double mNullSize;
596  double mExponent;
597 
598 };
599 
609 {
610  public:
611 
619  QgsColorRampTransformer( double minValue = 0.0,
620  double maxValue = 1.0,
621  QgsColorRamp *ramp SIP_TRANSFER = nullptr,
622  const QColor &nullColor = QColor( 0, 0, 0, 0 ) );
623 
626 
628 
629  Type transformerType() const override { return ColorRampTransformer; }
630  QgsColorRampTransformer *clone() const override SIP_FACTORY;
631  QVariant toVariant() const override;
632  bool loadVariant( const QVariant &definition ) override;
633  QVariant transform( const QgsExpressionContext &context, const QVariant &value ) const override;
634  QString toExpression( const QString &baseExpression ) const override;
635 
641  QColor color( double value ) const;
642 
648  QgsColorRamp *colorRamp() const;
649 
655  void setColorRamp( QgsColorRamp *ramp SIP_TRANSFER );
656 
661  QColor nullColor() const { return mNullColor; }
662 
668  void setNullColor( const QColor &color ) { mNullColor = color; }
669 
674  QString rampName() const { return mRampName; }
675 
682  void setRampName( const QString &name ) { mRampName = name; }
683 
684  private:
685 
686  std::unique_ptr< QgsColorRamp > mGradientRamp;
687  QColor mNullColor;
688  QString mRampName;
689 
690 };
691 
692 #endif // QGSPROPERTYTRANSFORMER_H
QgsExpressionContext
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
Definition: qgsexpressioncontext.h:370
QgsColorRamp
Abstract base class for color ramps.
Definition: qgscolorramp.h:32
QgsSizeScaleTransformer::Exponential
@ Exponential
Scale using set exponent.
Definition: qgspropertytransformer.h:471
QgsPropertyTransformer::setMaxValue
void setMaxValue(double max)
Sets the maximum value expected by the transformer.
Definition: qgspropertytransformer.h:271
QgsPropertyTransformer
Abstract base class for objects which transform the calculated value of a property.
Definition: qgspropertytransformer.h:171
qgsexpression.h
SIP_OUT
#define SIP_OUT
Definition: qgis_sip.h:58
QgsSizeScaleTransformer::type
ScaleType type() const
Returns the size transformer's scaling type (the method used to calculate the size from a value).
Definition: qgspropertytransformer.h:581
QgsColorRampTransformer
QgsPropertyTransformer subclass for transforming a numeric value into a color from a color ramp.
Definition: qgspropertytransformer.h:609
QgsPropertyTransformer::clone
virtual QgsPropertyTransformer * clone() const =0
Returns a clone of the transformer.
QgsGenericNumericTransformer::setMinOutputValue
void setMinOutputValue(double size)
Sets the minimum calculated size.
Definition: qgspropertytransformer.h:403
QgsPropertyTransformer::SizeScaleTransformer
@ SizeScaleTransformer
Size scaling transformer (QgsSizeScaleTransformer)
Definition: qgspropertytransformer.h:192
QgsGenericNumericTransformer::transformerType
Type transformerType() const override
Returns the transformer type.
Definition: qgspropertytransformer.h:363
SIP_FACTORY
#define SIP_FACTORY
Definition: qgis_sip.h:76
QgsSizeScaleTransformer::setMaxSize
void setMaxSize(double size)
Sets the maximum calculated size.
Definition: qgspropertytransformer.h:547
QgsPropertyTransformer::ColorRampTransformer
@ ColorRampTransformer
Color ramp transformer (QgsColorRampTransformer)
Definition: qgspropertytransformer.h:193
SIP_CONVERT_TO_SUBCLASS_CODE
#define SIP_CONVERT_TO_SUBCLASS_CODE(code)
Definition: qgis_sip.h:177
QgsGenericNumericTransformer::setMaxOutputValue
void setMaxOutputValue(double size)
Sets the maximum calculated size.
Definition: qgspropertytransformer.h:417
qgsexpressioncontext.h
QgsPropertyTransformer::transformerType
virtual Type transformerType() const =0
Returns the transformer type.
QgsSizeScaleTransformer
QgsPropertyTransformer subclass for scaling a value into a size according to various scaling methods.
Definition: qgspropertytransformer.h:462
QgsColorRampTransformer::rampName
QString rampName() const
Returns the color ramp's name.
Definition: qgspropertytransformer.h:674
QgsPropertyTransformer::mMaxValue
double mMaxValue
Maximum value expected by the transformer.
Definition: qgspropertytransformer.h:329
QgsSizeScaleTransformer::Flannery
@ Flannery
Flannery scaling method.
Definition: qgspropertytransformer.h:470
QgsColorRampTransformer::setNullColor
void setNullColor(const QColor &color)
Sets the color corresponding to a null value.
Definition: qgspropertytransformer.h:668
SIP_TRANSFER
#define SIP_TRANSFER
Definition: qgis_sip.h:36
QgsPropertyTransformer::setCurveTransform
void setCurveTransform(QgsCurveTransform *transform)
Sets a curve transform to apply to input values before they are transformed by the individual transfo...
Definition: qgspropertytransformer.h:286
QgsGenericNumericTransformer::maxOutputValue
double maxOutputValue() const
Returns the maximum calculated size.
Definition: qgspropertytransformer.h:409
QgsSizeScaleTransformer::setExponent
void setExponent(double exponent)
Sets the exponent for an exponential expression.
Definition: qgspropertytransformer.h:574
QgsSizeScaleTransformer::maxSize
double maxSize() const
Returns the maximum calculated size.
Definition: qgspropertytransformer.h:539
QgsPropertyTransformer::maxValue
double maxValue() const
Returns the maximum value expected by the transformer.
Definition: qgspropertytransformer.h:263
QgsCurveTransform
Handles scaling of input values to output values by using a curve created from smoothly joining a num...
Definition: qgspropertytransformer.h:57
QgsPropertyTransformer::~QgsPropertyTransformer
virtual ~QgsPropertyTransformer()
QgsGenericNumericTransformer::setExponent
void setExponent(double exponent)
Sets the exponent for an exponential expression.
Definition: qgspropertytransformer.h:443
QgsPropertyTransformer::minValue
double minValue() const
Returns the minimum value expected by the transformer.
Definition: qgspropertytransformer.h:248
QgsSizeScaleTransformer::exponent
double exponent() const
Returns the exponent for an exponential expression.
Definition: qgspropertytransformer.h:567
QgsColorRampTransformer::transformerType
Type transformerType() const override
Returns the transformer type.
Definition: qgspropertytransformer.h:629
QgsSizeScaleTransformer::Area
@ Area
Area based scaling.
Definition: qgspropertytransformer.h:469
QgsSizeScaleTransformer::Linear
@ Linear
Linear scaling.
Definition: qgspropertytransformer.h:468
QgsPropertyTransformer::transform
virtual QVariant transform(const QgsExpressionContext &context, const QVariant &value) const =0
Calculates the transform of a value.
QgsSizeScaleTransformer::transformerType
Type transformerType() const override
Returns the transformer type.
Definition: qgspropertytransformer.h:492
QgsPropertyTransformer::GenericNumericTransformer
@ GenericNumericTransformer
Generic transformer for numeric values (QgsGenericNumericTransformer)
Definition: qgspropertytransformer.h:191
QgsCurveTransform::controlPoints
QList< QgsPointXY > controlPoints() const
Returns a list of the control points for the transform.
Definition: qgspropertytransformer.h:86
QgsPropertyTransformer::operator=
QgsPropertyTransformer & operator=(const QgsPropertyTransformer &other)
Definition: qgspropertytransformer.cpp:59
QgsPropertyTransformer::setMinValue
void setMinValue(double min)
Sets the minimum value expected by the transformer.
Definition: qgspropertytransformer.h:256
QgsColorRampTransformer::setRampName
void setRampName(const QString &name)
Sets the color ramp's name.
Definition: qgspropertytransformer.h:682
QgsSizeScaleTransformer::nullSize
double nullSize() const
Returns the size value when an expression evaluates to NULL.
Definition: qgspropertytransformer.h:553
QgsPropertyTransformer::Type
Type
Transformer types.
Definition: qgspropertytransformer.h:190
QgsGenericNumericTransformer
QgsPropertyTransformer subclass for scaling an input numeric value into an output numeric value.
Definition: qgspropertytransformer.h:344
QgsPropertyTransformer::toExpression
virtual QString toExpression(const QString &baseExpression) const =0
Converts the transformer to a QGIS expression string.
QgsPropertyTransformer::mCurveTransform
std::unique_ptr< QgsCurveTransform > mCurveTransform
Optional curve transform.
Definition: qgspropertytransformer.h:332
QgsGenericNumericTransformer::setNullOutputValue
void setNullOutputValue(double size)
Sets the size value for when an expression evaluates to NULL.
Definition: qgspropertytransformer.h:430
QgsSizeScaleTransformer::ScaleType
ScaleType
Size scaling methods.
Definition: qgspropertytransformer.h:467
QgsGenericNumericTransformer::exponent
double exponent() const
Returns the exponent for an exponential expression.
Definition: qgspropertytransformer.h:436
QgsPropertyTransformer::curveTransform
QgsCurveTransform * curveTransform() const
Returns the curve transform applied to input values before they are transformed by the individual tra...
Definition: qgspropertytransformer.h:278
QgsGenericNumericTransformer::nullOutputValue
double nullOutputValue() const
Returns the size value when an expression evaluates to NULL.
Definition: qgspropertytransformer.h:423
SIP_END
#define SIP_END
Definition: qgis_sip.h:194
QgsSizeScaleTransformer::setNullSize
void setNullSize(double size)
Sets the size value for when an expression evaluates to NULL.
Definition: qgspropertytransformer.h:560
QgsSizeScaleTransformer::setMinSize
void setMinSize(double size)
Sets the minimum calculated size.
Definition: qgspropertytransformer.h:533
qgspointxy.h