QGIS API Documentation  3.2.0-Bonn (bc43194)
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 
215  virtual ~QgsPropertyTransformer() = default;
216 
220  virtual Type transformerType() const = 0;
221 
225  virtual QgsPropertyTransformer *clone() const = 0 SIP_FACTORY;
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
ScaleType
Size scaling methods.
QColor nullColor() const
Returns the color corresponding to a null value.
virtual QVariant transform(const QgsExpressionContext &context, const QVariant &value) const =0
Calculates the transform of a value.
std::unique_ptr< QgsCurveTransform > mCurveTransform
Optional curve transform.
virtual QVariant toVariant() const
Saves this transformer to a QVariantMap, wrapped in a QVariant.
Size scaling transformer (QgsSizeScaleTransformer)
Type transformerType() const override
Returns the transformer type.
void setMinOutputValue(double size)
Sets the minimum calculated size.
Abstract base class for color ramps.
Definition: qgscolorramp.h:31
QgsPropertyTransformer subclass for scaling an input numeric value into an output numeric value...
Abstract base class for objects which transform the calculated value of a property.
double exponent() const
Returns the exponent for an exponential expression.
void setMaxSize(double size)
Sets the maximum calculated size.
static QgsPropertyTransformer * fromExpression(const QString &expression, QString &baseExpression, QString &fieldName)
Attempts to parse an expression into a corresponding property transformer.
void setNullColor(const QColor &color)
Sets the color corresponding to a null value.
QgsPropertyTransformer & operator=(const QgsPropertyTransformer &other)
void setMaxOutputValue(double size)
Sets the maximum calculated size.
void setExponent(double exponent)
Sets the exponent for an exponential expression.
virtual QString toExpression(const QString &baseExpression) const =0
Converts the transformer to a QGIS expression string.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
void setNullOutputValue(double size)
Sets the size value for when an expression evaluates to NULL.
void setMaxValue(double max)
Sets the maximum value expected by the transformer.
#define SIP_TRANSFER
Definition: qgis_sip.h:36
#define SIP_END
Definition: qgis_sip.h:182
double minValue() const
Returns the minimum value expected by the transformer.
QgsCurveTransform * curveTransform() const
Returns the curve transform applied to input values before they are transformed by the individual tra...
double maxOutputValue() const
Returns the maximum calculated size.
#define SIP_FACTORY
Definition: qgis_sip.h:69
Type transformerType() const override
Returns the transformer type.
QList< QgsPointXY > controlPoints() const
Returns a list of the control points for the transform.
void setRampName(const QString &name)
Sets the color ramp&#39;s name.
double minOutputValue() const
Returns the minimum calculated size.
Color ramp transformer (QgsColorRampTransformer)
double maxSize() const
Returns the maximum calculated size.
QString rampName() const
Returns the color ramp&#39;s name.
double mMaxValue
Maximum value expected by the transformer.
Generic transformer for numeric values (QgsGenericNumericTransformer)
void setExponent(double exponent)
Sets the exponent for an exponential expression.
#define SIP_OUT
Definition: qgis_sip.h:51
double exponent() const
Returns the exponent for an exponential expression.
Handles scaling of input values to output values by using a curve created from smoothly joining a num...
Type transformerType() const override
Returns the transformer type.
void setNullSize(double size)
Sets the size value for when an expression evaluates to NULL.
#define SIP_CONVERT_TO_SUBCLASS_CODE(code)
Definition: qgis_sip.h:165
QgsPropertyTransformer subclass for scaling a value into a size according to various scaling methods...
virtual QgsPropertyTransformer * clone() const =0
Returns a clone of the transformer.
double nullSize() const
Returns the size value when an expression evaluates to NULL.
void setMinSize(double size)
Sets the minimum calculated size.
double nullOutputValue() const
Returns the size value when an expression evaluates to NULL.
void setMinValue(double min)
Sets the minimum value expected by the transformer.
double minSize() const
Returns the minimum calculated size.
QgsPropertyTransformer subclass for transforming a numeric value into a color from a color ramp...
virtual bool loadVariant(const QVariant &transformer)
Loads this transformer from a QVariantMap, wrapped in a QVariant.
void setCurveTransform(QgsCurveTransform *transform)
Sets a curve transform to apply to input values before they are transformed by the individual transfo...
double maxValue() const
Returns the maximum value expected by the transformer.
ScaleType type() const
Returns the size transformer&#39;s scaling type (the method used to calculate the size from a value)...