QGIS API Documentation  3.10.0-A Coruña (6c816b4204)
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 
87  void drawLegend( QPainter *painter );
88 
94  void drawLegend( QgsRenderContext &context );
95 
101  void exportLegendToJson( const QgsRenderContext &context, QJsonObject &json );
102 
108  static void setNodeLegendStyle( QgsLayerTreeNode *node, QgsLegendStyle::Style style );
109 
115  static QgsLegendStyle::Style nodeLegendStyle( QgsLayerTreeNode *node, QgsLayerTreeModel *model );
116 
117  private:
118 
119 #ifndef SIP_RUN
120 
128  class LegendComponent
129  {
130  public:
131 
132  LegendComponent() = default;
133 
134  QObject *item = nullptr;
135 
137  QSizeF symbolSize;
138 
140  QSizeF labelSize;
141 
143  QSizeF size;
144 
151  double labelXOffset = 0.0;
152 
157  double maxSiblingSymbolWidth = 0.0;
158  };
159 
174  class LegendComponentGroup
175  {
176  public:
177 
179  QList<LegendComponent> components;
180 
182  QSizeF size = QSizeF( 0, 0 );
183 
185  int column = 0;
186  };
187 
191  class ColumnContext
192  {
193  public:
194 
195  ColumnContext()
196  : left( 0 )
197  , right( 0 )
198  {}
199 
201  double left = 0;
203  double right = 0;
204  };
205 
212  QSizeF paintAndDetermineSize( QPainter *painter = nullptr );
213 
218  QList<LegendComponentGroup> createComponentGroupList( QgsLayerTreeGroup *parentGroup, bool splitLayer, QgsRenderContext *context );
219 
223  void setColumns( QList<LegendComponentGroup> &groupList );
224 
232  QSizeF drawTitle( QPainter *painter = nullptr, double top = 0, Qt::AlignmentFlag halignment = Qt::AlignLeft, double legendWidth = 0 );
233 
237  double spaceAboveGroup( const LegendComponentGroup &group );
238 
245  QSizeF drawGroup( const LegendComponentGroup &group, ColumnContext columnContext, QPainter *painter = nullptr, double top = 0 );
246 
250  LegendComponent drawSymbolItem( QgsLayerTreeModelLegendNode *symbolItem, ColumnContext columnContext = ColumnContext(), QPainter *painter = nullptr, double top = 0, double maxSiblingSymbolWidth = 0 );
251 
259  QSizeF drawLayerTitle( QgsLayerTreeLayer *nodeLayer, ColumnContext columnContext = ColumnContext(), QPainter *painter = nullptr, double top = 0 );
260 
265  QSizeF drawGroupTitle( QgsLayerTreeGroup *nodeGroup, ColumnContext columnContext = ColumnContext(), QPainter *painter = nullptr, double top = 0 );
266 
272  void exportLegendToJson( const QgsRenderContext &context, QgsLayerTreeGroup *nodeGroup, QJsonObject &json );
273 
280  QSizeF paintAndDetermineSize( QgsRenderContext *context );
281 
289  QSizeF drawTitle( QgsRenderContext *context, double top, Qt::AlignmentFlag halignment = Qt::AlignLeft, double legendWidth = 0 );
290 
299  QSizeF drawGroup( const LegendComponentGroup &group, QgsRenderContext *rendercontext, ColumnContext columnContext, double top = 0 );
300 
304  LegendComponent drawSymbolItem( QgsLayerTreeModelLegendNode *symbolItem, QgsRenderContext *context, ColumnContext columnContext, double top, double maxSiblingSymbolWidth = 0 );
305 
313  QSizeF drawLayerTitle( QgsLayerTreeLayer *nodeLayer, QgsRenderContext *context, ColumnContext columnContext, double top = 0 );
314 
320  QSizeF drawGroupTitle( QgsLayerTreeGroup *nodeGroup, QgsRenderContext *context, ColumnContext columnContext = ColumnContext(), double top = 0 );
321 
325  QgsLegendStyle::Style nodeLegendStyle( QgsLayerTreeNode *node );
326 
327  QgsLayerTreeModel *mLegendModel = nullptr;
328 
329  QgsLegendSettings mSettings;
330 
331  QSizeF mLegendSize;
332 
333 #endif
334  QSizeF drawTitleInternal( QgsRenderContext *context, QPainter *painter, double top, Qt::AlignmentFlag halignment, double legendWidth );
335  QSizeF drawGroupInternal( const LegendComponentGroup &group, QgsRenderContext *context, ColumnContext columnContext, QPainter *painter, double top );
336  QgsLegendRenderer::LegendComponent drawSymbolItemInternal( QgsLayerTreeModelLegendNode *symbolItem, ColumnContext columnContext, QgsRenderContext *context, QPainter *painter, double top, double maxSiblingSymbolWidth );
337  QSizeF drawLayerTitleInternal( QgsLayerTreeLayer *nodeLayer, ColumnContext columnContext, QgsRenderContext *context, QPainter *painter, double top );
338  QSizeF drawGroupTitleInternal( QgsLayerTreeGroup *nodeGroup, ColumnContext columnContext, QgsRenderContext *context, QPainter *painter, double top );
339  QSizeF paintAndDetermineSizeInternal( QgsRenderContext *context, QPainter *painter );
340 
341  void widthAndOffsetForTitleText( const Qt::AlignmentFlag halignment, double legendWidth, double &width, double &offset );
342 };
343 
344 #endif // QGSLEGENDRENDERER_H
Layer tree group node serves as a container for layers and further groups.
void setLegendSize(QSizeF s)
Sets the preferred resulting legend size.
Abstract base class for all rendered symbols.
Definition: qgssymbol.h:61
The QgsLayerTreeModel class is model implementation for Qt item views framework.
QSizeF legendSize() const
Returns the preferred legend size set by the client.
The QgsLegendSettings class stores the appearance and layout settings for legend drawing with QgsLege...
This class is a base class for nodes in a layer tree.
Style
Component of legends which can be styled.
Contains information about the context of a rendering operation.
QgsLayerTreeModel * legendModel(const QgsWmsRenderContext &context, QgsLayerTree &tree)
The QgsLegendRendererItem class is abstract interface for legend items returned from QgsMapLayerLegen...
Layer tree node points to a map layer.
The QgsLegendRenderer class handles automatic layout and rendering of legend.