QGIS API Documentation  3.0.2-Girona (307d082)
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 
437  double exponent() const { return mExponent; }
438 
444  void setExponent( double exponent ) { mExponent = exponent; }
445 
446  private:
447  double mMinOutput;
448  double mMaxOutput;
449  double mNullOutput;
450  double mExponent;
451 
452 };
453 
463 {
464  public:
465 
468  {
473  };
474 
485  QgsSizeScaleTransformer( ScaleType type = Linear,
486  double minValue = 0.0,
487  double maxValue = 1.0,
488  double minSize = 0.0,
489  double maxSize = 1.0,
490  double nullSize = 0.0,
491  double exponent = 1.0 );
492 
493  Type transformerType() const override { return SizeScaleTransformer; }
494  QgsSizeScaleTransformer *clone() const override SIP_FACTORY;
495  QVariant toVariant() const override;
496  bool loadVariant( const QVariant &definition ) override;
497  QVariant transform( const QgsExpressionContext &context, const QVariant &value ) const override;
498  QString toExpression( const QString &baseExpression ) const override;
499 
512  static QgsSizeScaleTransformer *fromExpression( const QString &expression, QString &baseExpression SIP_OUT, QString &fieldName SIP_OUT ) SIP_FACTORY;
513 
519  double size( double value ) const;
520 
526  double minSize() const { return mMinSize; }
527 
534  void setMinSize( double size ) { mMinSize = size; }
535 
540  double maxSize() const { return mMaxSize; }
541 
548  void setMaxSize( double size ) { mMaxSize = size; }
549 
554  double nullSize() const { return mNullSize; }
555 
561  void setNullSize( double size ) { mNullSize = size; }
562 
568  double exponent() const { return mExponent; }
569 
575  void setExponent( double exponent ) { mExponent = exponent; }
576 
582  ScaleType type() const { return mType; }
583 
590  void setType( ScaleType type );
591 
592  private:
593  ScaleType mType = Linear;
594  double mMinSize;
595  double mMaxSize;
596  double mNullSize;
597  double mExponent;
598 
599 };
600 
610 {
611  public:
612 
620  QgsColorRampTransformer( double minValue = 0.0,
621  double maxValue = 1.0,
622  QgsColorRamp *ramp SIP_TRANSFER = nullptr,
623  const QColor &nullColor = QColor( 0, 0, 0, 0 ) );
624 
627 
629 
630  Type transformerType() const override { return ColorRampTransformer; }
631  QgsColorRampTransformer *clone() const override SIP_FACTORY;
632  QVariant toVariant() const override;
633  bool loadVariant( const QVariant &definition ) override;
634  QVariant transform( const QgsExpressionContext &context, const QVariant &value ) const override;
635  QString toExpression( const QString &baseExpression ) const override;
636 
642  QColor color( double value ) const;
643 
649  QgsColorRamp *colorRamp() const;
650 
656  void setColorRamp( QgsColorRamp *ramp SIP_TRANSFER );
657 
662  QColor nullColor() const { return mNullColor; }
663 
669  void setNullColor( const QColor &color ) { mNullColor = color; }
670 
675  QString rampName() const { return mRampName; }
676 
683  void setRampName( const QString &name ) { mRampName = name; }
684 
685  private:
686 
687  std::unique_ptr< QgsColorRamp > mGradientRamp;
688  QColor mNullColor;
689  QString mRampName;
690 
691 };
692 
693 #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:175
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)...