QGIS API Documentation  3.20.0-Odense (decaadbb31)
qgsinterpolatedlinerenderer.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgsinterpolatedlinerenderer.h
3  --------------------------------------
4  Date : April 2020
5  Copyright : (C) 2020 by Vincent Cloarec
6  Email : vcloarec 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 
16 #ifndef QGSINTERPOLATEDLINERENDERER_H
17 #define QGSINTERPOLATEDLINERENDERER_H
18 
19 #include <QDebug>
20 
21 #include "qgis.h"
22 #include "qgscolorrampshader.h"
23 #include "qgsreadwritecontext.h"
24 #include "qgsrendercontext.h"
25 #include "qgsrenderer.h"
26 #include "qgsunittypes.h"
27 #include "qgssymbollayer.h"
28 
29 class QgsLayerTreeLayer;
30 
38 class CORE_EXPORT QgsInterpolatedLineColor
39 {
40  public:
41 
46  {
48  SingleColor = 0,
50  ColorRamp
51  };
52 
56  QgsInterpolatedLineColor( const QgsColorRampShader &colorRampShader );
58  QgsInterpolatedLineColor( const QColor &color );
59 
61  void setColor( const QgsColorRampShader &colorRampShader );
62 
64  void setColor( const QColor &color );
65 
67  QColor color( double magnitude ) const;
68 
73  void setColoringMethod( const QgsInterpolatedLineColor::ColoringMethod &coloringMethod );
74 
76  QgsInterpolatedLineColor::ColoringMethod coloringMethod() const;
77 
79  QgsColorRampShader colorRampShader() const;
80 
85  QColor singleColor() const;
86 
88  QDomElement writeXml( QDomDocument &doc, const QgsReadWriteContext &context ) const;
90  void readXml( const QDomElement &elem, const QgsReadWriteContext &context );
91 
101  void graduatedColors( double value1, double value2, QList<double> &breakValues, QList<QColor> &breakColors, QList<QLinearGradient> &gradients ) const;
102 
103  private:
104  QgsColorRampShader mColorRampShader;
105  QColor mSingleColor = Qt::black;
106 
107  QgsInterpolatedLineColor::ColoringMethod mColoringMethod = SingleColor;
108 
109  QLinearGradient makeSimpleLinearGradient( const QColor &color1, const QColor &color2 ) const;
110 
112  int itemColorIndexInf( double value ) const;
113 
114  void graduatedColorsExact( double value1, double value2, QList<double> &breakValues, QList<QColor> &breakColors, QList<QLinearGradient> &gradients ) const;
115  void graduatedColorsInterpolated( double value1, double value2, QList<double> &breakValues, QList<QColor> &breakColors, QList<QLinearGradient> &gradients ) const;
116  void graduatedColorsDiscrete( double value1, double value2, QList<double> &breakValues, QList<QColor> &breakColors, QList<QLinearGradient> &gradients ) const;
117 };
118 
126 class CORE_EXPORT QgsInterpolatedLineWidth
127 {
128  public:
130  double minimumValue() const;
132  void setMinimumValue( double minimumValue );
133 
135  double maximumValue() const;
137  void setMaximumValue( double maximumValue );
138 
140  double minimumWidth() const;
142  void setMinimumWidth( double minimumWidth );
143 
145  double maximumWidth() const;
147  void setMaximumWidth( double maximumWidth );
148 
150  bool ignoreOutOfRange() const;
152  void setIgnoreOutOfRange( bool ignoreOutOfRange );
153 
155  bool useAbsoluteValue() const;
157  void setUseAbsoluteValue( bool useAbsoluteValue );
158 
160  bool isVariableWidth() const;
162  void setIsVariableWidth( bool isVariableWidth );
163 
165  double fixedStrokeWidth() const;
167  void setFixedStrokeWidth( double fixedWidth );
168 
170  double strokeWidth( double value ) const;
171 
173  QDomElement writeXml( QDomDocument &doc, const QgsReadWriteContext &context ) const;
175  void readXml( const QDomElement &elem, const QgsReadWriteContext &context );
176 
177  private:
178  bool mIsWidthVariable = false;
179 
180  double mFixedWidth = DEFAULT_LINE_WIDTH;
181 
182  double mMinimumValue = 0;
183  double mMaximumValue = 10;
184  double mMinimumWidth = DEFAULT_LINE_WIDTH;
185  double mMaximumWidth = 3;
186  bool mIgnoreOutOfRange = false;
187  bool mUseAbsoluteValue = false;
188 
189  mutable double mLinearCoef = 1;
190  mutable bool mNeedUpdateFormula = true;
191  void updateLinearFormula() const;
192 };
193 
201 {
202  public:
203 
205  void setInterpolatedWidth( const QgsInterpolatedLineWidth &strokeWidth );
206 
211  QgsInterpolatedLineWidth interpolatedLineWidth() const;
212 
214  void setWidthUnit( const QgsUnitTypes::RenderUnit &strokeWidthUnit );
215 
220  QgsUnitTypes::RenderUnit widthUnit() const;
221 
223  void setInterpolatedColor( const QgsInterpolatedLineColor &strokeColoring );
224 
229  QgsInterpolatedLineColor interpolatedColor() const;
230 
235  void render( double value1, double value2, const QgsPointXY &point1, const QgsPointXY &point2, QgsRenderContext &context ) const;
236 
243  void render( double valueColor1, double valueColor2, double valueWidth1, double valueWidth2, const QgsPointXY &point1, const QgsPointXY &point2, QgsRenderContext &context ) const;
244 
250  void setSelected( bool selected );
251 
252  private:
253 
254  QgsInterpolatedLineWidth mStrokeWidth;
255  QgsInterpolatedLineColor mStrokeColoring;
257  void adjustLine( const double &value, const double &value1, const double &value2, double &width, double &adjusting ) const;
258  bool mSelected = false;
259 
264  void renderInDeviceCoordinate( double valueColor1, double valueColor2, double valueWidth1, double valueWidth2, const QPointF &p1, const QPointF &p2, QgsRenderContext &context ) const;
265 
267 };
268 
277 {
278  public:
279 
282 
284  static QgsSymbolLayer *create( const QVariantMap &properties ) SIP_FACTORY;
285 
286  QString layerType() const override;
287  void startRender( QgsSymbolRenderContext &context ) override;
288  void stopRender( QgsSymbolRenderContext &context ) override;
289  QgsInterpolatedLineSymbolLayer *clone() const override SIP_FACTORY;
290  QVariantMap properties() const override;
291  void drawPreviewIcon( QgsSymbolRenderContext &context, QSize size ) override;
292 
293  void startFeatureRender( const QgsFeature &feature, QgsRenderContext &context ) override;
294  void stopFeatureRender( const QgsFeature &feature, QgsRenderContext &context ) override;
295  void renderPolyline( const QPolygonF &points, QgsSymbolRenderContext &context ) override;
296  bool isCompatibleWithSymbol( QgsSymbol *symbol ) const override;
297  QSet<QString> usedAttributes( const QgsRenderContext &context ) const override;
298  bool canCauseArtifactsBetweenAdjacentTiles() const override;
299 
301  void setExpressionsStringForWidth( QString start, QString end );
302 
304  QString startValueExpressionForWidth() const;
305 
307  QString endValueExpressionForWidth() const;
308 
310  void setWidthUnit( const QgsUnitTypes::RenderUnit &strokeWidthUnit );
311 
313  QgsUnitTypes::RenderUnit widthUnit() const;
314 
316  void setInterpolatedWidth( const QgsInterpolatedLineWidth &interpolatedLineWidth );
317 
319  QgsInterpolatedLineWidth interpolatedWidth() const;
320 
322  void setExpressionsStringForColor( QString start, QString end );
323 
325  QString startValueExpressionForColor() const;
326 
328  QString endValueExpressionForColor() const;
329 
331  void setInterpolatedColor( const QgsInterpolatedLineColor &interpolatedLineColor );
332 
334  QgsInterpolatedLineColor interpolatedColor() const;
335 
336  private:
337 #ifdef SIP_RUN
339 #endif
340 
341  QgsInterpolatedLineRenderer mLineRender;
342  QString mStartWidthExpressionString;
343  QString mEndWidthExpressionString;
344  QString mStartColorExpressionString;
345  QString mEndColorExpressionString;
346 
347  int mStartWidthAttributeIndex = -1;
348  int mEndWidthAttributeIndex = -1;
349  int mStartColorAttributeIndex = -1;
350  int mEndColorAttributeIndex = -1;
351 
352  std::unique_ptr<QgsExpression> mStartWidthExpression;
353  std::unique_ptr<QgsExpression> mEndWithExpression;
354  std::unique_ptr<QgsExpression> mStartColorExpression;
355  std::unique_ptr<QgsExpression> mEndColorExpression;
356  QgsFeature mFeature;
357 
358  QVariant colorRampShaderProperties() const;
359  static QgsColorRampShader createColorRampShaderFromProperties( const QVariant &properties );
360 };
361 
362 
363 #endif // QGSINTERPOLATEDLINERENDERER_H
A ramp shader will color a raster pixel based on a list of values ranges in a ramp.
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition: qgsfeature.h:56
Class defining color to render mesh datasets.
ColoringMethod
Defines how the color is defined.
Represents a simple line renderer with width and color varying depending on values.
A symbol layer that represents vector layer line feature as interpolated line The interpolation is do...
Represents a width than can vary depending on values.
Layer tree node points to a map layer.
A class to represent a 2D point.
Definition: qgspointxy.h:59
The class is used as a container of context for various read/write operations on other objects.
Contains information about the context of a rendering operation.
Abstract base class for all rendered symbols.
Definition: qgssymbol.h:38
RenderUnit
Rendering size units.
Definition: qgsunittypes.h:168
@ RenderMillimeters
Millimeters.
Definition: qgsunittypes.h:169
const double DEFAULT_LINE_WIDTH
Definition: qgis.h:1036
#define SIP_FACTORY
Definition: qgis_sip.h:76