QGIS API Documentation  3.23.0-Master (dd0cd13a00)
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 
151  double indent = 0;
152 
159  double labelXOffset = 0.0;
160 
165  double maxSiblingSymbolWidth = 0.0;
166  };
167 
182  class LegendComponentGroup
183  {
184  public:
185 
187  QList<LegendComponent> components;
188 
190  QSizeF size = QSizeF( 0, 0 );
191 
193  int column = 0;
194 
198  bool placeColumnBreakBeforeGroup = false;
199 
200  };
201 
205  class ColumnContext
206  {
207  public:
208 
209  ColumnContext()
210  : left( 0 )
211  , right( 0 )
212  {}
213 
215  double left = 0;
217  double right = 0;
218  };
219 
224  QList<LegendComponentGroup> createComponentGroupList( QgsLayerTreeGroup *parentGroup, QgsRenderContext &context, double indent = 0 );
225 
231  int setColumns( QList<LegendComponentGroup> &groupList );
232 
236  double spaceAboveGroup( const LegendComponentGroup &group );
237 
243  QJsonObject exportLegendToJson( const QgsRenderContext &context, QgsLayerTreeGroup *nodeGroup );
244 
251  QSizeF paintAndDetermineSize( QgsRenderContext &context );
252 
260  QSizeF drawTitle( QgsRenderContext &context, double top, Qt::AlignmentFlag halignment = Qt::AlignLeft, double legendWidth = 0 );
261 
270  QSizeF drawGroup( const LegendComponentGroup &group, QgsRenderContext &context, ColumnContext columnContext, double top = 0 );
271 
275  LegendComponent drawSymbolItem( QgsLayerTreeModelLegendNode *symbolItem, QgsRenderContext &context, ColumnContext columnContext, double top, double maxSiblingSymbolWidth = 0 );
276 
284  QSizeF drawLayerTitle( QgsLayerTreeLayer *nodeLayer, QgsRenderContext &context, ColumnContext columnContext = ColumnContext(), double top = 0 );
285 
291  QSizeF drawGroupTitle( QgsLayerTreeGroup *nodeGroup, QgsRenderContext &context, ColumnContext columnContext = ColumnContext(), double top = 0 );
292 
296  QgsLegendStyle::Style nodeLegendStyle( QgsLayerTreeNode *node );
297 
298  QgsLayerTreeModel *mLegendModel = nullptr;
299 
300  QgsLegendSettings mSettings;
301 
302  QSizeF mLegendSize;
303 
304 #endif
305 
306  void widthAndOffsetForTitleText( const Qt::AlignmentFlag halignment, double legendWidth, double &width, double &offset );
307 };
308 
309 #endif // QGSLEGENDRENDERER_H
Layer tree group node serves as a container for layers and further groups.
Layer tree node points to a map layer.
The QgsLegendRendererItem class is abstract interface for legend items returned from QgsMapLayerLegen...
The QgsLayerTreeModel class is model implementation for Qt item views framework.
This class is a base class for nodes in a layer tree.
The QgsLegendRenderer class handles automatic layout and rendering of legend.
void setLegendSize(QSizeF s)
Sets the preferred resulting legend size.
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...
Style
Component of legends which can be styled.
Contains information about the context of a rendering operation.
Abstract base class for all rendered symbols.
Definition: qgssymbol.h:38
QgsLayerTreeModel * legendModel(const QgsWmsRenderContext &context, QgsLayerTree &tree)
#define SIP_DEPRECATED
Definition: qgis_sip.h:106