QGIS API Documentation  3.14.0-Pi (9f7028fd23)
qgslegendrenderer.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgslegendrenderer.h
3  --------------------------------------
4  Date : July 2014
5  Copyright : (C) 2014 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 QGSLEGENDRENDERER_H
17 #define QGSLEGENDRENDERER_H
18 
19 #include "qgis_core.h"
20 #include <QPointF>
21 
22 class QRectF;
23 class QStandardItem;
24 class QJsonObject;
25 
26 class QgsLayerTreeGroup;
27 class QgsLayerTreeLayer;
28 class QgsLayerTreeModel;
30 class QgsLayerTreeNode;
31 class QgsSymbol;
32 class QgsRenderContext;
33 
34 #include "qgslegendsettings.h"
35 
46 class CORE_EXPORT QgsLegendRenderer
47 {
48  public:
49 
55 
61  QSizeF minimumSize( QgsRenderContext *renderContext = nullptr );
62 
71  void setLegendSize( QSizeF s ) { mLegendSize = s; }
72 
81  QSizeF legendSize() const { return mLegendSize; }
82 
89  Q_DECL_DEPRECATED void drawLegend( QPainter *painter ) SIP_DEPRECATED;
90 
96  void drawLegend( QgsRenderContext &context );
97 
103  QJsonObject exportLegendToJson( const QgsRenderContext &context );
104 
110  static void setNodeLegendStyle( QgsLayerTreeNode *node, QgsLegendStyle::Style style );
111 
117  static QgsLegendStyle::Style nodeLegendStyle( QgsLayerTreeNode *node, QgsLayerTreeModel *model );
118 
119  private:
120 
121 #ifndef SIP_RUN
122 
130  class LegendComponent
131  {
132  public:
133 
134  LegendComponent() = default;
135 
136  QObject *item = nullptr;
137 
139  QSizeF symbolSize;
140 
142  QSizeF labelSize;
143 
145  QSizeF size;
146 
153  double labelXOffset = 0.0;
154 
159  double maxSiblingSymbolWidth = 0.0;
160  };
161 
176  class LegendComponentGroup
177  {
178  public:
179 
181  QList<LegendComponent> components;
182 
184  QSizeF size = QSizeF( 0, 0 );
185 
187  int column = 0;
188 
192  bool placeColumnBreakBeforeGroup = false;
193 
194  };
195 
199  class ColumnContext
200  {
201  public:
202 
203  ColumnContext()
204  : left( 0 )
205  , right( 0 )
206  {}
207 
209  double left = 0;
211  double right = 0;
212  };
213 
218  QList<LegendComponentGroup> createComponentGroupList( QgsLayerTreeGroup *parentGroup, QgsRenderContext &context );
219 
225  int setColumns( QList<LegendComponentGroup> &groupList );
226 
230  double spaceAboveGroup( const LegendComponentGroup &group );
231 
237  QJsonObject exportLegendToJson( const QgsRenderContext &context, QgsLayerTreeGroup *nodeGroup );
238 
245  QSizeF paintAndDetermineSize( QgsRenderContext &context );
246 
254  QSizeF drawTitle( QgsRenderContext &context, double top, Qt::AlignmentFlag halignment = Qt::AlignLeft, double legendWidth = 0 );
255 
264  QSizeF drawGroup( const LegendComponentGroup &group, QgsRenderContext &context, ColumnContext columnContext, double top = 0 );
265 
269  LegendComponent drawSymbolItem( QgsLayerTreeModelLegendNode *symbolItem, QgsRenderContext &context, ColumnContext columnContext, double top, double maxSiblingSymbolWidth = 0 );
270 
278  QSizeF drawLayerTitle( QgsLayerTreeLayer *nodeLayer, QgsRenderContext &context, ColumnContext columnContext = ColumnContext(), double top = 0 );
279 
285  QSizeF drawGroupTitle( QgsLayerTreeGroup *nodeGroup, QgsRenderContext &context, ColumnContext columnContext = ColumnContext(), double top = 0 );
286 
290  QgsLegendStyle::Style nodeLegendStyle( QgsLayerTreeNode *node );
291 
292  QgsLayerTreeModel *mLegendModel = nullptr;
293 
294  QgsLegendSettings mSettings;
295 
296  QSizeF mLegendSize;
297 
298 #endif
299 
300  void widthAndOffsetForTitleText( const Qt::AlignmentFlag halignment, double legendWidth, double &width, double &offset );
301 };
302 
303 #endif // QGSLEGENDRENDERER_H
QgsLegendStyle::Style
Style
Component of legends which can be styled.
Definition: qgslegendstyle.h:53
QgsLayerTreeNode
Definition: qgslayertreenode.h:74
QgsLegendRenderer::setLegendSize
void setLegendSize(QSizeF s)
Sets the preferred resulting legend size.
Definition: qgslegendrenderer.h:71
QgsRenderContext
Definition: qgsrendercontext.h:57
QgsLegendRenderer::legendSize
QSizeF legendSize() const
Returns the preferred legend size set by the client.
Definition: qgslegendrenderer.h:81
QgsLayerTreeModel
Definition: qgslayertreemodel.h:53
QgsSymbol
Definition: qgssymbol.h:63
SIP_DEPRECATED
#define SIP_DEPRECATED
Definition: qgis_sip.h:106
qgslegendsettings.h
QgsLayerTreeLayer
Definition: qgslayertreelayer.h:43
QgsLayerTreeGroup
Definition: qgslayertreegroup.h:34
QgsLegendSettings
The QgsLegendSettings class stores the appearance and layout settings for legend drawing with QgsLege...
Definition: qgslegendsettings.h:38
QgsWms::legendModel
QgsLayerTreeModel * legendModel(const QgsWmsRenderContext &context, QgsLayerTree &tree)
Definition: qgswmsgetlegendgraphics.cpp:243
QgsLegendRenderer
The QgsLegendRenderer class handles automatic layout and rendering of legend. The content is given by...
Definition: qgslegendrenderer.h:46
QgsLayerTreeModelLegendNode
Definition: qgslayertreemodellegendnode.h:50