-
-
Notifications
You must be signed in to change notification settings - Fork 3k
/
qgsdatadefinedsizelegend.h
142 lines (114 loc) · 6.51 KB
/
qgsdatadefinedsizelegend.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/***************************************************************************
qgsdatadefinedsizelegend.h
--------------------------------------
Date : June 2017
Copyright : (C) 2017 by Martin Dobias
Email : wonder dot sk at gmail dot com
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#ifndef QGSDATADEFINEDSIZELEGEND_H
#define QGSDATADEFINEDSIZELEGEND_H
#include "qgslegendsymbolitem.h"
#include <QColor>
#include <QFont>
class QDomElement;
class QgsMarkerSymbol;
class QgsProperty;
class QgsReadWriteContext;
class QgsRenderContext;
/** \ingroup core
* Object that keeps configuration of appearance of marker symbol's data-defined size in legend.
* For example: the list of classes (size values), whether the classes should appear in separate
* legend nodes or whether to collapse them into one legend node.
*
* \since QGIS 3.0
*/
class CORE_EXPORT QgsDataDefinedSizeLegend
{
public:
QgsDataDefinedSizeLegend();
QgsDataDefinedSizeLegend( const QgsDataDefinedSizeLegend &other );
QgsDataDefinedSizeLegend &operator=( const QgsDataDefinedSizeLegend &other );
//! Determines how to display data-defined size legend
enum LegendType
{
LegendSeparated, //!< Each class (size value) has a separate legend node
LegendCollapsed, //!< All classes are rendered within one legend node
};
//! How to vertically align symbols when all classes go into one node
enum VerticalAlignment
{
AlignCenter, //!< Symbols are aligned to the center
AlignBottom, //!< Symbols are aligned to the bottom
};
//! Definition of one class for the legend
struct SizeClass
{
SizeClass( double size, const QString &label ): size( size ), label( label ) {}
double size; //!< Marker size in units used by the symbol (usually millimeters)
QString label; //!< Label to be shown with the particular symbol size
};
//! Sets how the legend should be rendered
void setLegendType( LegendType type ) { mType = type; }
//! Returns how the legend should be rendered
LegendType legendType() const { return mType; }
//! Sets marker symbol that will be used to draw markers in legend
void setSymbol( QgsMarkerSymbol *symbol SIP_TRANSFER );
//! Returns marker symbol that will be used to draw markers in legend
QgsMarkerSymbol *symbol() const;
//! Sets list of classes: each class is a pair of symbol size (in units used by the symbol) and label
void setClasses( const QList<QgsDataDefinedSizeLegend::SizeClass> &classes ) { mSizeClasses = classes; }
//! Returns list of classes: each class is a pair of symbol size (in units used by the symbol) and label
QList<QgsDataDefinedSizeLegend::SizeClass> classes() const { return mSizeClasses; }
//! Sets title label for data-defined size legend
void setTitle( const QString &title ) { mTitleLabel = title; }
//! Returns title label for data-defined size legend
QString title() const { return mTitleLabel; }
//! Sets vertical alignment of symbols - only valid for collapsed legend
void setVerticalAlignment( VerticalAlignment vAlign ) { mVAlign = vAlign; }
//! Returns vertical alignment of symbols - only valid for collapsed legend
VerticalAlignment verticalAlignment() const { return mVAlign; }
//! Sets font used for rendering of labels - only valid for collapsed legend
void setFont( const QFont &font ) { mFont = font; }
//! Returns font used for rendering of labels - only valid for collapsed legend
QFont font() const { return mFont; }
//! Sets text color for rendering of labels - only valid for collapsed legend
void setTextColor( const QColor &color ) { mTextColor = color; }
//! Returns text color for rendering of labels - only valid for collapsed legend
QColor textColor() const { return mTextColor; }
//! Sets horizontal text alignment for rendering of labels - only valid for collapsed legend
void setTextAlignment( Qt::AlignmentFlag flag ) { mTextAlignment = flag; }
//! Returns horizontal text alignment for rendering of labels - only valid for collapsed legend
Qt::AlignmentFlag textAlignment() const { return mTextAlignment; }
//
//! Updates the list of classes, source symbol and title label from given symbol and property
void updateFromSymbolAndProperty( const QgsMarkerSymbol *symbol, const QgsProperty &ddSize );
//! Generates legend symbol items according to the configuration
QgsLegendSymbolList legendSymbolList() const;
//! Draw the legend if using LegendOneNodeForAll and optionally output size of the legend and x offset of labels (in painter units).
//! If the painter in context is null, it only does size calculation without actual rendering.
//! Does nothing if legend is not configured as collapsed.
void drawCollapsedLegend( QgsRenderContext &context, QSize *outputSize SIP_OUT = nullptr, int *labelXOffset SIP_OUT = nullptr ) const;
//! Returns output image that would be shown in the legend. Returns invalid image if legend is not configured as collapsed.
QImage collapsedLegendImage( QgsRenderContext &context, double paddingMM = 1 ) const;
//! Creates instance from given element and returns it (caller takes ownership). Returns null on error.
static QgsDataDefinedSizeLegend *readXml( const QDomElement &elem, const QgsReadWriteContext &context ) SIP_FACTORY;
//! Writes configuration to the given XML element.
void writeXml( QDomElement &elem, const QgsReadWriteContext &context ) const;
private:
LegendType mType = LegendSeparated;
QString mTitleLabel; //!< Title label for the following size-based item(s)
QList<SizeClass> mSizeClasses; //!< List of classes: symbol size (in whatever units symbol uses) + label
std::unique_ptr<QgsMarkerSymbol> mSymbol;
VerticalAlignment mVAlign = AlignBottom;
QFont mFont;
QColor mTextColor = Qt::black;
Qt::AlignmentFlag mTextAlignment = Qt::AlignLeft;
};
#endif // QGSDATADEFINEDSIZELEGEND_H