QGIS API Documentation  3.8.0-Zanzibar (11aff65)
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 Nucleon
129  {
130  public:
132  Nucleon() = 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  };
153 
168  class Atom
169  {
170  public:
171 
173  QList<Nucleon> nucleons;
174 
176  QSizeF size = QSizeF( 0, 0 );
177 
179  int column = 0;
180  };
181 
188  QSizeF paintAndDetermineSize( QPainter *painter = nullptr );
189 
194  QList<Atom> createAtomList( QgsLayerTreeGroup *parentGroup, bool splitLayer );
195 
199  void setColumns( QList<Atom> &atomList );
200 
208  QSizeF drawTitle( QPainter *painter = nullptr, QPointF point = QPointF(), Qt::AlignmentFlag halignment = Qt::AlignLeft, double legendWidth = 0 );
209 
213  double spaceAboveAtom( const Atom &atom );
214 
221  QSizeF drawAtom( const Atom &atom, QPainter *painter = nullptr, QPointF point = QPointF() );
222 
226  Nucleon drawSymbolItem( QgsLayerTreeModelLegendNode *symbolItem, QPainter *painter = nullptr, QPointF point = QPointF(), double labelXOffset = 0 );
227 
235  QSizeF drawLayerTitle( QgsLayerTreeLayer *nodeLayer, QPainter *painter = nullptr, QPointF point = QPointF() );
236 
241  QSizeF drawGroupTitle( QgsLayerTreeGroup *nodeGroup, QPainter *painter = nullptr, QPointF point = QPointF() );
242 
248  void exportLegendToJson( const QgsRenderContext &context, QgsLayerTreeGroup *nodeGroup, QJsonObject &json );
249 
256  QSizeF paintAndDetermineSize( QgsRenderContext *context );
257 
265  QSizeF drawTitle( QgsRenderContext *context, QPointF point = QPointF(), Qt::AlignmentFlag halignment = Qt::AlignLeft, double legendWidth = 0 );
266 
275  QSizeF drawAtom( const Atom &atom, QgsRenderContext *rendercontext, QPointF point = QPointF() );
276 
280  Nucleon drawSymbolItem( QgsLayerTreeModelLegendNode *symbolItem, QgsRenderContext *context, QPointF point = QPointF(), double labelXOffset = 0 );
281 
289  QSizeF drawLayerTitle( QgsLayerTreeLayer *nodeLayer, QgsRenderContext *context, QPointF point = QPointF() );
290 
296  QSizeF drawGroupTitle( QgsLayerTreeGroup *nodeGroup, QgsRenderContext *context, QPointF point = QPointF() );
297 
301  QgsLegendStyle::Style nodeLegendStyle( QgsLayerTreeNode *node );
302 
303  private:
304  QgsLayerTreeModel *mLegendModel = nullptr;
305 
306  QgsLegendSettings mSettings;
307 
308  QSizeF mLegendSize;
309 
310 #endif
311  QSizeF drawTitleInternal( QgsRenderContext *context, QPainter *painter, QPointF point, Qt::AlignmentFlag halignment, double legendWidth );
312  QSizeF drawAtomInternal( const Atom &atom, QgsRenderContext *context, QPainter *painter, QPointF point );
313  QgsLegendRenderer::Nucleon drawSymbolItemInternal( QgsLayerTreeModelLegendNode *symbolItem, QgsRenderContext *context, QPainter *painter, QPointF point, double labelXOffset );
314  QSizeF drawLayerTitleInternal( QgsLayerTreeLayer *nodeLayer, QgsRenderContext *context, QPainter *painter, QPointF point );
315  QSizeF drawGroupTitleInternal( QgsLayerTreeGroup *nodeGroup, QgsRenderContext *context, QPainter *painter, QPointF point );
316  QSizeF paintAndDetermineSizeInternal( QgsRenderContext *context, QPainter *painter );
317 };
318 
319 #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.
nlohmann::json json
Definition: qgsjsonutils.h:27
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.