QGIS API Documentation  3.0.2-Girona (307d082)
qgspointdistancerenderer.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgspointdistancerenderer.cpp
3  ----------------------------
4  begin : January 26, 2010
5  copyright : (C) 2010 by Marco Hugentobler
6  email : marco at hugis dot net
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU General Public License as published by *
13  * the Free Software Foundation; either version 2 of the License, or *
14  * (at your option) any later version. *
15  * *
16  ***************************************************************************/
17 
18 #ifndef QGSPOINTDISTANCERENDERER_H
19 #define QGSPOINTDISTANCERENDERER_H
20 
21 #include "qgis_core.h"
22 #include "qgis.h"
23 #include "qgsrenderer.h"
24 #include <QFont>
25 
26 class QgsSpatialIndex;
27 
39 {
40  public:
41 
44  {
45 
53  GroupedFeature( const QgsFeature &feature, QgsMarkerSymbol *symbol SIP_TRANSFER, bool isSelected, const QString &label = QString() )
54  : feature( feature )
55  , isSelected( isSelected )
56  , label( label )
57  , mSymbol( symbol )
58  {}
59 
62 
64  QgsMarkerSymbol *symbol() const { return mSymbol.get(); }
65 
67  bool isSelected;
68 
70  QString label;
71 
72  private:
73  std::shared_ptr< QgsMarkerSymbol > mSymbol;
74  };
75 
77  typedef QList< QgsPointDistanceRenderer::GroupedFeature > ClusteredGroup;
78 
84  QgsPointDistanceRenderer( const QString &rendererName, const QString &labelAttributeName = QString() );
85 
86  void toSld( QDomDocument &doc, QDomElement &element, const QgsStringMap &props = QgsStringMap() ) const override;
87  bool renderFeature( QgsFeature &feature, QgsRenderContext &context, int layer = -1, bool selected = false, bool drawVertexMarker = false ) override;
88  QSet<QString> usedAttributes( const QgsRenderContext &context ) const override;
89  QgsFeatureRenderer::Capabilities capabilities() override;
90  QgsSymbolList symbols( QgsRenderContext &context ) override;
91  QgsSymbol *symbolForFeature( QgsFeature &feature, QgsRenderContext &context ) override;
92  QgsSymbol *originalSymbolForFeature( QgsFeature &feat, QgsRenderContext &context ) override;
93  QgsSymbolList symbolsForFeature( QgsFeature &feat, QgsRenderContext &context ) override;
94  QgsSymbolList originalSymbolsForFeature( QgsFeature &feat, QgsRenderContext &context ) override;
95  QSet< QString > legendKeysForFeature( QgsFeature &feature, QgsRenderContext &context ) override;
96  bool willRenderFeature( QgsFeature &feat, QgsRenderContext &context ) override;
97  void startRender( QgsRenderContext &context, const QgsFields &fields ) override;
98  void stopRender( QgsRenderContext &context ) override;
99  QgsLegendSymbolList legendSymbolItems() const override;
100  void setEmbeddedRenderer( QgsFeatureRenderer *r SIP_TRANSFER ) override;
101  const QgsFeatureRenderer *embeddedRenderer() const override;
102  void setLegendSymbolItem( const QString &key, QgsSymbol *symbol SIP_TRANSFER ) override;
103  bool legendSymbolItemsCheckable() const override;
104  bool legendSymbolItemChecked( const QString &key ) override;
105  void checkLegendSymbolItem( const QString &key, bool state ) override;
106  QString filter( const QgsFields &fields = QgsFields() ) override;
107 
116  void setLabelAttributeName( const QString &name ) { mLabelAttributeName = name; }
117 
126  QString labelAttributeName() const { return mLabelAttributeName; }
127 
135  void setLabelFont( const QFont &font ) { mLabelFont = font; }
136 
143  QFont labelFont() const { return mLabelFont;}
144 
151  void setMinimumLabelScale( double scale ) { mMinLabelScale = scale; }
152 
159  double minimumLabelScale() const { return mMinLabelScale; }
160 
168  void setLabelColor( const QColor &color ) { mLabelColor = color;}
169 
176  QColor labelColor() const { return mLabelColor; }
177 
185  void setTolerance( double distance ) { mTolerance = distance; }
186 
193  double tolerance() const { return mTolerance; }
194 
202  void setToleranceUnit( QgsUnitTypes::RenderUnit unit ) { mToleranceUnit = unit; }
203 
210  QgsUnitTypes::RenderUnit toleranceUnit() const { return mToleranceUnit; }
211 
219  void setToleranceMapUnitScale( const QgsMapUnitScale &scale ) { mToleranceMapUnitScale = scale; }
220 
227  const QgsMapUnitScale &toleranceMapUnitScale() const { return mToleranceMapUnitScale; }
228 
229  protected:
230 
232  std::unique_ptr< QgsFeatureRenderer > mRenderer;
233 
236 
239 
241  double mTolerance;
246 
248  QFont mLabelFont;
250  QColor mLabelColor;
254  double mMinLabelScale = 0;
255 
257  QList<ClusteredGroup> mClusteredGroups;
258 
260  QMap<QgsFeatureId, int> mGroupIndex;
261 
263  QMap<QgsFeatureId, QgsPointXY > mGroupLocations;
264 
266  QgsSpatialIndex *mSpatialIndex = nullptr;
267 
276  void drawLabels( QPointF centerPoint, QgsSymbolRenderContext &context, const QList<QPointF> &labelShifts, const ClusteredGroup &group );
277 
278  private:
279 
286  virtual void drawGroup( QPointF centerPoint, QgsRenderContext &context, const ClusteredGroup &group ) = 0 SIP_FORCE;
287 
289  QgsRectangle searchRect( const QgsPointXY &p, double distance ) const;
290 
292  void printGroupInfo() const;
293 
295  QString getLabel( const QgsFeature &feature ) const;
296 
298  void drawGroup( const ClusteredGroup &group, QgsRenderContext &context );
299 
305  QgsMarkerSymbol *firstSymbolForFeature( QgsFeature &feature, QgsRenderContext &context );
306 
312  QgsExpressionContextScope *createGroupScope( const ClusteredGroup &group ) const;
313 
314 };
315 
316 #endif // QGSPOINTDISTANCERENDERER_H
An abstract base class for distance based point renderers (e.g., clusterer and displacement renderers...
A rectangle specified with double values.
Definition: qgsrectangle.h:39
QString mLabelAttributeName
Attribute name for labeling. An empty string indicates that no labels should be rendered.
double minimumLabelScale() const
Returns the minimum map scale (i.e.
QList< QgsLegendSymbolItem > QgsLegendSymbolList
QgsUnitTypes::RenderUnit mToleranceUnit
Unit for distance tolerance.
void setLabelFont(const QFont &font)
Sets the font used for labeling points.
GroupedFeature(const QgsFeature &feature, QgsMarkerSymbol *symbol, bool isSelected, const QString &label=QString())
Constructor for GroupedFeature.
A class to represent a 2D point.
Definition: qgspointxy.h:43
void setTolerance(double distance)
Sets the tolerance distance for grouping points.
void setLabelColor(const QColor &color)
Sets the color to use for for labeling points.
void setToleranceMapUnitScale(const QgsMapUnitScale &scale)
Sets the map unit scale object for the distance tolerance.
void setToleranceUnit(QgsUnitTypes::RenderUnit unit)
Sets the units for the tolerance distance.
Container of fields for a vector layer.
Definition: qgsfields.h:42
QMap< QgsFeatureId, int > mGroupIndex
Mapping of feature ID to the feature&#39;s group index.
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:62
QString labelAttributeName() const
Returns the attribute name used for labeling points, or an empty string if no labeling will be done b...
QMap< QString, QString > QgsStringMap
Definition: qgis.h:479
QgsUnitTypes::RenderUnit toleranceUnit() const
Returns the units for the tolerance distance.
bool mDrawLabels
Whether labels should be drawn for points. This is set internally from startRender() depending on sca...
QFont labelFont() const
Returns the font used for labeling points.
QList< QgsSymbol * > QgsSymbolList
Definition: qgsrenderer.h:43
bool isSelected
True if feature is selected and should be rendered in a selected state.
double mTolerance
Distance tolerance. Points that are closer together than this distance are considered clustered...
QMap< QgsFeatureId, QgsPointXY > mGroupLocations
Mapping of feature ID to approximate group location.
#define SIP_FORCE
Definition: qgis_sip.h:124
void setLabelAttributeName(const QString &name)
Sets the attribute name for labeling points.
#define SIP_TRANSFER
Definition: qgis_sip.h:36
QColor mLabelColor
Label text color.
Single scope for storing variables and functions for use within a QgsExpressionContext.
QgsMapUnitScale mToleranceMapUnitScale
Map unit scale for distance tolerance.
double tolerance() const
Returns the tolerance distance for grouping points.
const QgsMapUnitScale & toleranceMapUnitScale() const
Returns the map unit scale object for the distance tolerance.
Contains properties for a feature within a clustered group.
QgsMarkerSymbol * symbol() const
Base symbol for rendering feature.
Contains information about the context of a rendering operation.
A spatial index for QgsFeature objects.
Struct for storing maximum and minimum scales for measurements in map units.
void setMinimumLabelScale(double scale)
Sets the minimum map scale (i.e.
std::unique_ptr< QgsFeatureRenderer > mRenderer
Embedded base renderer. This can be used for rendering individual, isolated points.
QList< QgsPointDistanceRenderer::GroupedFeature > ClusteredGroup
A group of clustered points (ie features within the distance tolerance).
QList< ClusteredGroup > mClusteredGroups
Groups of features that are considered clustered together.
int mLabelIndex
Label attribute index (or -1 if none). This index is not stored, it is requested in the startRender()...
RenderUnit
Rendering size units.
Definition: qgsunittypes.h:100
QColor labelColor() const
Returns the color used for for labeling points.