QGIS API Documentation  3.14.0-Pi (9f7028fd23)
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 
41 class CORE_EXPORT QgsDataDefinedSizeLegend
42 {
43  public:
44 
49 
51 
54  QgsDataDefinedSizeLegend &operator=( const QgsDataDefinedSizeLegend &other );
55 
58  {
61  };
62 
65  {
68  };
69 
71  struct SizeClass
72  {
73  SizeClass( double size, const QString &label ): size( size ), label( label ) {}
74 
75  double size;
76  QString label;
77  };
78 
80  void setLegendType( LegendType type ) { mType = type; }
82  LegendType legendType() const { return mType; }
83 
85  void setSymbol( QgsMarkerSymbol *symbol SIP_TRANSFER );
87  QgsMarkerSymbol *symbol() const;
88 
97  void setLineSymbol( QgsLineSymbol *symbol SIP_TRANSFER );
98 
104  QgsLineSymbol *lineSymbol() const;
105 
107  void setSizeScaleTransformer( QgsSizeScaleTransformer *transformer SIP_TRANSFER );
109  QgsSizeScaleTransformer *sizeScaleTransformer() const;
110 
112  void setClasses( const QList< QgsDataDefinedSizeLegend::SizeClass > &classes ) { mSizeClasses = classes; }
114  QList< QgsDataDefinedSizeLegend::SizeClass > classes() const { return mSizeClasses; }
115 
117  void setTitle( const QString &title ) { mTitleLabel = title; }
119  QString title() const { return mTitleLabel; }
120 
122  void setVerticalAlignment( VerticalAlignment vAlign ) { mVAlign = vAlign; }
124  VerticalAlignment verticalAlignment() const { return mVAlign; }
125 
127  void setFont( const QFont &font ) { mFont = font; }
129  QFont font() const { return mFont; }
130 
132  void setTextColor( const QColor &color ) { mTextColor = color; }
134  QColor textColor() const { return mTextColor; }
135 
137  void setTextAlignment( Qt::AlignmentFlag flag ) { mTextAlignment = flag; }
139  Qt::AlignmentFlag textAlignment() const { return mTextAlignment; }
140 
141  //
142 
144  void updateFromSymbolAndProperty( const QgsMarkerSymbol *symbol, const QgsProperty &ddSize );
145 
147  QgsLegendSymbolList legendSymbolList() const;
148 
154  void drawCollapsedLegend( QgsRenderContext &context, QSizeF *outputSize SIP_OUT = nullptr, double *labelXOffset SIP_OUT = nullptr ) const;
155 
157  QImage collapsedLegendImage( QgsRenderContext &context, const QColor &backgroundColor = Qt::transparent, double paddingMM = 1 ) const;
158 
160  static QgsDataDefinedSizeLegend *readXml( const QDomElement &elem, const QgsReadWriteContext &context ) SIP_FACTORY;
161 
163  void writeXml( QDomElement &elem, const QgsReadWriteContext &context ) const;
164 
165  private:
166  LegendType mType = LegendSeparated;
167  QString mTitleLabel;
168  QList< SizeClass > mSizeClasses;
169  std::unique_ptr<QgsMarkerSymbol> mSymbol;
170  std::unique_ptr<QgsLineSymbol> mLineSymbol;
171  std::unique_ptr<QgsSizeScaleTransformer> mSizeScaleTransformer;
172  VerticalAlignment mVAlign = AlignBottom;
173  QFont mFont;
174  QColor mTextColor = Qt::black;
175  Qt::AlignmentFlag mTextAlignment = Qt::AlignLeft;
176 };
177 
178 #endif // QGSDATADEFINEDSIZELEGEND_H
QgsDataDefinedSizeLegend::SizeClass::size
double size
Marker size in units used by the symbol (usually millimeters). May be further scaled before rendering...
Definition: qgsdatadefinedsizelegend.h:75
QgsDataDefinedSizeLegend::textAlignment
Qt::AlignmentFlag textAlignment() const
Returns horizontal text alignment for rendering of labels - only valid for collapsed legend.
Definition: qgsdatadefinedsizelegend.h:139
QgsProperty
A store for object properties.
Definition: qgsproperty.h:231
QgsDataDefinedSizeLegend::LegendType
LegendType
Determines how to display data-defined size legend.
Definition: qgsdatadefinedsizelegend.h:57
QgsDataDefinedSizeLegend::AlignBottom
@ AlignBottom
Symbols are aligned to the bottom.
Definition: qgsdatadefinedsizelegend.h:67
QgsReadWriteContext
Definition: qgsreadwritecontext.h:34
QgsDataDefinedSizeLegend::title
QString title() const
Returns title label for data-defined size legend.
Definition: qgsdatadefinedsizelegend.h:119
SIP_OUT
#define SIP_OUT
Definition: qgis_sip.h:58
QgsDataDefinedSizeLegend
Definition: qgsdatadefinedsizelegend.h:41
QgsRenderContext
Definition: qgsrendercontext.h:57
QgsDataDefinedSizeLegend::setClasses
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.
Definition: qgsdatadefinedsizelegend.h:112
SIP_FACTORY
#define SIP_FACTORY
Definition: qgis_sip.h:76
QgsDataDefinedSizeLegend::AlignCenter
@ AlignCenter
Symbols are aligned to the center.
Definition: qgsdatadefinedsizelegend.h:66
QgsDataDefinedSizeLegend::SizeClass
Definition of one class for the legend.
Definition: qgsdatadefinedsizelegend.h:71
QgsDataDefinedSizeLegend::LegendCollapsed
@ LegendCollapsed
All classes are rendered within one legend node.
Definition: qgsdatadefinedsizelegend.h:60
QgsMarkerSymbol
Definition: qgssymbol.h:917
QgsSizeScaleTransformer
QgsPropertyTransformer subclass for scaling a value into a size according to various scaling methods.
Definition: qgspropertytransformer.h:461
SIP_TRANSFER
#define SIP_TRANSFER
Definition: qgis_sip.h:36
qgslegendsymbolitem.h
QgsLineSymbol
Definition: qgssymbol.h:1117
QgsDataDefinedSizeLegend::LegendSeparated
@ LegendSeparated
Each class (size value) has a separate legend node.
Definition: qgsdatadefinedsizelegend.h:59
QgsDataDefinedSizeLegend::setTextColor
void setTextColor(const QColor &color)
Sets text color for rendering of labels - only valid for collapsed legend.
Definition: qgsdatadefinedsizelegend.h:132
QgsDataDefinedSizeLegend::textColor
QColor textColor() const
Returns text color for rendering of labels - only valid for collapsed legend.
Definition: qgsdatadefinedsizelegend.h:134
QgsDataDefinedSizeLegend::setVerticalAlignment
void setVerticalAlignment(VerticalAlignment vAlign)
Sets vertical alignment of symbols - only valid for collapsed legend.
Definition: qgsdatadefinedsizelegend.h:122
QgsDataDefinedSizeLegend::setTitle
void setTitle(const QString &title)
Sets title label for data-defined size legend.
Definition: qgsdatadefinedsizelegend.h:117
QgsDataDefinedSizeLegend::VerticalAlignment
VerticalAlignment
How to vertically align symbols when all classes go into one node.
Definition: qgsdatadefinedsizelegend.h:64
QgsDataDefinedSizeLegend::font
QFont font() const
Returns font used for rendering of labels - only valid for collapsed legend.
Definition: qgsdatadefinedsizelegend.h:129
QgsDataDefinedSizeLegend::classes
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.
Definition: qgsdatadefinedsizelegend.h:114
QgsDataDefinedSizeLegend::SizeClass::SizeClass
SizeClass(double size, const QString &label)
Definition: qgsdatadefinedsizelegend.h:73
QgsDataDefinedSizeLegend::setLegendType
void setLegendType(LegendType type)
Sets how the legend should be rendered.
Definition: qgsdatadefinedsizelegend.h:80
QgsLegendSymbolList
QList< QgsLegendSymbolItem > QgsLegendSymbolList
Definition: qgslegendsymbolitem.h:144
QgsDataDefinedSizeLegend::SizeClass::label
QString label
Label to be shown with the particular symbol size.
Definition: qgsdatadefinedsizelegend.h:76
QgsDataDefinedSizeLegend::setTextAlignment
void setTextAlignment(Qt::AlignmentFlag flag)
Sets horizontal text alignment for rendering of labels - only valid for collapsed legend.
Definition: qgsdatadefinedsizelegend.h:137
QgsDataDefinedSizeLegend::verticalAlignment
VerticalAlignment verticalAlignment() const
Returns vertical alignment of symbols - only valid for collapsed legend.
Definition: qgsdatadefinedsizelegend.h:124
QgsDataDefinedSizeLegend::legendType
LegendType legendType() const
Returns how the legend should be rendered.
Definition: qgsdatadefinedsizelegend.h:82
QgsDataDefinedSizeLegend::setFont
void setFont(const QFont &font)
Sets font used for rendering of labels - only valid for collapsed legend.
Definition: qgsdatadefinedsizelegend.h:127