QGIS API Documentation  3.20.0-Odense (decaadbb31)
qgsdatadefinedsizelegend.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgsdatadefinedsizelegend.h
3  --------------------------------------
4  Date : June 2017
5  Copyright : (C) 2017 by Martin Dobias
6  Email : wonder dot sk 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 QGSDATADEFINEDSIZELEGEND_H
17 #define QGSDATADEFINEDSIZELEGEND_H
18 
19 #include "qgslegendsymbolitem.h"
20 
21 #include <QColor>
22 #include <QFont>
23 
24 class QDomElement;
25 class QgsMarkerSymbol;
26 class QgsProperty;
28 class QgsRenderContext;
30 class QgsLineSymbol;
31 
32 
42 class CORE_EXPORT QgsDataDefinedSizeLegend
43 {
44  public:
45 
50 
52 
55  QgsDataDefinedSizeLegend &operator=( const QgsDataDefinedSizeLegend &other );
56 
59  {
62  };
63 
66  {
69  };
70 
72  struct SizeClass
73  {
74  SizeClass( double size, const QString &label ): size( size ), label( label ) {}
75 
76  double size;
77  QString label;
78  };
79 
81  void setLegendType( LegendType type ) { mType = type; }
83  LegendType legendType() const { return mType; }
84 
86  void setSymbol( QgsMarkerSymbol *symbol SIP_TRANSFER );
88  QgsMarkerSymbol *symbol() const;
89 
98  void setLineSymbol( QgsLineSymbol *symbol SIP_TRANSFER );
99 
105  QgsLineSymbol *lineSymbol() const;
106 
108  void setSizeScaleTransformer( QgsSizeScaleTransformer *transformer SIP_TRANSFER );
110  QgsSizeScaleTransformer *sizeScaleTransformer() const;
111 
113  void setClasses( const QList< QgsDataDefinedSizeLegend::SizeClass > &classes ) { mSizeClasses = classes; }
115  QList< QgsDataDefinedSizeLegend::SizeClass > classes() const { return mSizeClasses; }
116 
118  void setTitle( const QString &title ) { mTitleLabel = title; }
120  QString title() const { return mTitleLabel; }
121 
123  void setVerticalAlignment( VerticalAlignment vAlign ) { mVAlign = vAlign; }
125  VerticalAlignment verticalAlignment() const { return mVAlign; }
126 
128  void setFont( const QFont &font ) { mFont = font; }
130  QFont font() const { return mFont; }
131 
133  void setTextColor( const QColor &color ) { mTextColor = color; }
135  QColor textColor() const { return mTextColor; }
136 
138  void setTextAlignment( Qt::AlignmentFlag flag ) { mTextAlignment = flag; }
140  Qt::AlignmentFlag textAlignment() const { return mTextAlignment; }
141 
142  //
143 
145  void updateFromSymbolAndProperty( const QgsMarkerSymbol *symbol, const QgsProperty &ddSize );
146 
148  QgsLegendSymbolList legendSymbolList() const;
149 
155  void drawCollapsedLegend( QgsRenderContext &context, QSizeF *outputSize SIP_OUT = nullptr, double *labelXOffset SIP_OUT = nullptr ) const;
156 
158  QImage collapsedLegendImage( QgsRenderContext &context, const QColor &backgroundColor = Qt::transparent, double paddingMM = 1 ) const;
159 
161  static QgsDataDefinedSizeLegend *readXml( const QDomElement &elem, const QgsReadWriteContext &context ) SIP_FACTORY;
162 
164  void writeXml( QDomElement &elem, const QgsReadWriteContext &context ) const;
165 
166  private:
167  LegendType mType = LegendSeparated;
168  QString mTitleLabel;
169  QList< SizeClass > mSizeClasses;
170  std::unique_ptr<QgsMarkerSymbol> mSymbol;
171  std::unique_ptr<QgsLineSymbol> mLineSymbol;
172  std::unique_ptr<QgsSizeScaleTransformer> mSizeScaleTransformer;
173  VerticalAlignment mVAlign = AlignBottom;
174  QFont mFont;
175  QColor mTextColor = Qt::black;
176  Qt::AlignmentFlag mTextAlignment = Qt::AlignLeft;
177 };
178 
179 #endif // QGSDATADEFINEDSIZELEGEND_H
Object that keeps configuration of appearance of marker symbol's data-defined size in legend.
void setTitle(const QString &title)
Sets title label for data-defined size legend.
QList< QgsDataDefinedSizeLegend::SizeClass > classes() const
Returns list of classes: each class is a pair of symbol size (in units used by the symbol) and label.
void setVerticalAlignment(VerticalAlignment vAlign)
Sets vertical alignment of symbols - only valid for collapsed legend.
LegendType legendType() const
Returns how the legend should be rendered.
VerticalAlignment
How to vertically align symbols when all classes go into one node.
@ AlignCenter
Symbols are aligned to the center.
@ AlignBottom
Symbols are aligned to the bottom.
void setTextAlignment(Qt::AlignmentFlag flag)
Sets horizontal text alignment for rendering of labels - only valid for collapsed legend.
void setClasses(const QList< QgsDataDefinedSizeLegend::SizeClass > &classes)
Sets list of classes: each class is a pair of symbol size (in units used by the symbol) and label.
void setLegendType(LegendType type)
Sets how the legend should be rendered.
void setFont(const QFont &font)
Sets font used for rendering of labels - only valid for collapsed legend.
void setTextColor(const QColor &color)
Sets text color for rendering of labels - only valid for collapsed legend.
QString title() const
Returns title label for data-defined size legend.
Qt::AlignmentFlag textAlignment() const
Returns horizontal text alignment for rendering of labels - only valid for collapsed legend.
LegendType
Determines how to display data-defined size legend.
@ LegendSeparated
Each class (size value) has a separate legend node.
@ LegendCollapsed
All classes are rendered within one legend node.
QFont font() const
Returns font used for rendering of labels - only valid for collapsed legend.
VerticalAlignment verticalAlignment() const
Returns vertical alignment of symbols - only valid for collapsed legend.
QColor textColor() const
Returns text color for rendering of labels - only valid for collapsed legend.
A line symbol type, for rendering LineString and MultiLineString geometries.
Definition: qgslinesymbol.h:30
A marker symbol type, for rendering Point and MultiPoint geometries.
A store for object properties.
Definition: qgsproperty.h:232
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.
QgsPropertyTransformer subclass for scaling a value into a size according to various scaling methods.
#define SIP_TRANSFER
Definition: qgis_sip.h:36
#define SIP_OUT
Definition: qgis_sip.h:58
#define SIP_FACTORY
Definition: qgis_sip.h:76
QList< QgsLegendSymbolItem > QgsLegendSymbolList
Definition of one class for the legend.
SizeClass(double size, const QString &label)
QString label
Label to be shown with the particular symbol size.
double size
Marker size in units used by the symbol (usually millimeters). May be further scaled before rendering...