QGIS API Documentation 3.41.0-Master (af5edcb665c)
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 "qgis_core.h"
19#include "qgsexpression.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
32class QgsColorRamp;
33
34
55class CORE_EXPORT QgsCurveTransform
56{
57 public:
58
64
70 QgsCurveTransform( const QList< QgsPointXY > &controlPoints );
71
73
75
76 QgsCurveTransform &operator=( const QgsCurveTransform &other );
77
82 QList< QgsPointXY > controlPoints() const { return mControlPoints; }
83
89 void setControlPoints( const QList< QgsPointXY > &points );
90
96 void addControlPoint( double x, double y );
97
103 void removeControlPoint( double x, double y );
104
108 double y( double x ) const;
109
115 QVector< double > y( const QVector< double > &x ) const;
116
123 bool readXml( const QDomElement &elem, const QDomDocument &doc );
124
131 bool writeXml( QDomElement &transformElem, QDomDocument &doc ) const;
132
139 QVariant toVariant() const;
140
147 bool loadVariant( const QVariant &transformer );
148
149 private:
150
151 void calcSecondDerivativeArray();
152
153 QList< QgsPointXY > mControlPoints;
154
155 double *mSecondDerivativeArray = nullptr;
156};
157
158
165class CORE_EXPORT QgsPropertyTransformer
166{
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
270 QgsCurveTransform *curveTransform() const { return mCurveTransform.get(); }
271
278 void setCurveTransform( QgsCurveTransform *transform SIP_TRANSFER ) { mCurveTransform.reset( transform ); }
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:
304
310 double transformNumeric( double input ) const;
311#ifndef SIP_RUN
313 double mMinValue;
314
316 double mMaxValue;
317
319 std::unique_ptr< QgsCurveTransform > mCurveTransform;
320#endif
321};
322
330{
331 public:
332
342 QgsGenericNumericTransformer( double minValue = 0.0,
343 double maxValue = 1.0,
344 double minOutput = 0.0,
345 double maxOutput = 1.0,
346 double nullOutput = 0.0,
347 double exponent = 1.0 );
348
349 Type transformerType() const override { return GenericNumericTransformer; }
350 QgsGenericNumericTransformer *clone() const override SIP_FACTORY;
351 QVariant toVariant() const override;
352 bool loadVariant( const QVariant &definition ) override;
353 QVariant transform( const QgsExpressionContext &context, const QVariant &value ) const override;
354 QString toExpression( const QString &baseExpression ) const override;
355
368 static QgsGenericNumericTransformer *fromExpression( const QString &expression, QString &baseExpression SIP_OUT, QString &fieldName SIP_OUT ) SIP_FACTORY; // cppcheck-suppress duplInheritedMember
369
374 double value( double input ) const;
375
381 double minOutputValue() const { return mMinOutput; }
382
389 void setMinOutputValue( double size ) { mMinOutput = size; }
390
395 double maxOutputValue() const { return mMaxOutput; }
396
403 void setMaxOutputValue( double size ) { mMaxOutput = size; }
404
409 double nullOutputValue() const { return mNullOutput; }
410
416 void setNullOutputValue( double size ) { mNullOutput = size; }
417
422 double exponent() const { return mExponent; }
423
429 void setExponent( double exponent ) { mExponent = exponent; }
430
431 private:
432 double mMinOutput;
433 double mMaxOutput;
434 double mNullOutput;
435 double mExponent;
436
437};
438
447{
448 public:
449
458
469 QgsSizeScaleTransformer( ScaleType type = Linear,
470 double minValue = 0.0,
471 double maxValue = 1.0,
472 double minSize = 0.0,
473 double maxSize = 1.0,
474 double nullSize = 0.0,
475 double exponent = 1.0 );
476
477 Type transformerType() const override { return SizeScaleTransformer; }
478 QgsSizeScaleTransformer *clone() const override SIP_FACTORY;
479 QVariant toVariant() const override;
480 bool loadVariant( const QVariant &definition ) override;
481 QVariant transform( const QgsExpressionContext &context, const QVariant &value ) const override;
482 QString toExpression( const QString &baseExpression ) const override;
483
491 static QgsSizeScaleTransformer *fromExpression( const QString &expression, QString &baseExpression SIP_OUT, QString &fieldName SIP_OUT ) SIP_FACTORY; // cppcheck-suppress duplInheritedMember
492
498 double size( double value ) const;
499
505 double minSize() const { return mMinSize; }
506
513 void setMinSize( double size ) { mMinSize = size; }
514
519 double maxSize() const { return mMaxSize; }
520
527 void setMaxSize( double size ) { mMaxSize = size; }
528
533 double nullSize() const { return mNullSize; }
534
540 void setNullSize( double size ) { mNullSize = size; }
541
547 double exponent() const { return mExponent; }
548
554 void setExponent( double exponent ) { mExponent = exponent; }
555
561 ScaleType type() const { return mType; }
562
569 void setType( ScaleType type );
570
571 private:
572 ScaleType mType = Linear;
573 double mMinSize;
574 double mMaxSize;
575 double mNullSize;
576 double mExponent;
577
578};
579
588{
589 public:
590
599 QgsColorRampTransformer( double minValue = 0.0,
600 double maxValue = 1.0,
601 QgsColorRamp *ramp SIP_TRANSFER = nullptr,
602 const QColor &nullColor = QColor( 0, 0, 0, 0 ),
603 const QString &rampName = QString() );
604
606 QgsColorRampTransformer &operator=( const QgsColorRampTransformer &other );
607
608 Type transformerType() const override { return ColorRampTransformer; }
609 QgsColorRampTransformer *clone() const override SIP_FACTORY;
610 QVariant toVariant() const override;
611 bool loadVariant( const QVariant &definition ) override;
612 QVariant transform( const QgsExpressionContext &context, const QVariant &value ) const override;
613 QString toExpression( const QString &baseExpression ) const override;
614
620 QColor color( double value ) const;
621
627 QgsColorRamp *colorRamp() const;
628
634 void setColorRamp( QgsColorRamp *ramp SIP_TRANSFER );
635
640 QColor nullColor() const { return mNullColor; }
641
647 void setNullColor( const QColor &color ) { mNullColor = color; }
648
653 QString rampName() const { return mRampName; }
654
661 void setRampName( const QString &name ) { mRampName = name; }
662
663 private:
664
665 std::unique_ptr< QgsColorRamp > mGradientRamp;
666 QColor mNullColor;
667 QString mRampName;
668
669};
670
671#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.
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...
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.
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.
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.
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.
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.
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.
virtual ~QgsPropertyTransformer()
@ GenericNumericTransformer
Generic transformer for numeric values (QgsGenericNumericTransformer)
@ SizeScaleTransformer
Size scaling transformer (QgsSizeScaleTransformer)
@ ColorRampTransformer
Color ramp transformer (QgsColorRampTransformer)
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.
void setNullSize(double size)
Sets the size value for when an expression evaluates to NULL.
@ 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.
#define SIP_CONVERT_TO_SUBCLASS_CODE(code)
Definition qgis_sip.h:191
#define SIP_TRANSFER
Definition qgis_sip.h:36
#define SIP_OUT
Definition qgis_sip.h:58
#define SIP_FACTORY
Definition qgis_sip.h:76
#define SIP_END
Definition qgis_sip.h:208