QGIS API Documentation  2.99.0-Master (6a61179)
qgscomposerlegend.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgscomposerlegend.h - description
3  -------------------
4  begin : June 2008
5  copyright : (C) 2008 by Marco Hugentobler
6  email : marco dot hugentobler at karto dot baug dot ethz dot ch
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU General Public License as published by *
13  * the Free Software Foundation; either version 2 of the License, or *
14  * (at your option) any later version. *
15  * *
16  ***************************************************************************/
17 
18 #ifndef QGSCOMPOSERLEGEND_H
19 #define QGSCOMPOSERLEGEND_H
20 
21 #include "qgscomposeritem.h"
22 #include "qgslayertreemodel.h"
23 #include "qgslegendsettings.h"
24 
25 class QgsLayerTreeModel;
26 class QgsSymbol;
29 class QgsComposerMap;
30 class QgsLegendRenderer;
31 
32 
39 class CORE_EXPORT QgsLegendModelV2 : public QgsLayerTreeModel
40 {
41  Q_OBJECT
42 
43  public:
44  QgsLegendModelV2( QgsLayerTreeGroup* rootNode, QObject *parent = nullptr );
45 
46  QVariant data( const QModelIndex& index, int role ) const override;
47 
48  Qt::ItemFlags flags( const QModelIndex &index ) const override;
49 };
50 
51 
55 class CORE_EXPORT QgsComposerLegend : public QgsComposerItem
56 {
57  Q_OBJECT
58 
59  public:
60  QgsComposerLegend( QgsComposition* composition );
62 
64  virtual int type() const override { return ComposerLegend; }
65 
67  void paint( QPainter* painter, const QStyleOptionGraphicsItem* itemStyle, QWidget* pWidget ) override;
68 
70  QSizeF paintAndDetermineSize( QPainter* painter );
71 
73  void adjustBoxSize();
74 
81  void setResizeToContents( bool enabled );
82 
87  bool resizeToContents() const;
88 
89 
93  QgsLegendModelV2* model() { return mLegendModel; }
94 
96  void setAutoUpdateModel( bool autoUpdate );
98  bool autoUpdateModel() const;
99 
102  void setLegendFilterByMapEnabled( bool enabled );
105  bool legendFilterByMapEnabled() const { return mLegendFilterByMap; }
106 
109  virtual void updateItem() override;
110 
114  void setLegendFilterOutAtlas( bool doFilter );
115 
119  bool legendFilterOutAtlas() const;
120 
121  //setters and getters
122  void setTitle( const QString& t );
123  QString title() const;
124 
130  Qt::AlignmentFlag titleAlignment() const;
131 
137  void setTitleAlignment( Qt::AlignmentFlag alignment );
138 
143  void setStyle( QgsComposerLegendStyle::Style s, const QgsComposerLegendStyle& style );
144 
145  QFont styleFont( QgsComposerLegendStyle::Style s ) const;
147  void setStyleFont( QgsComposerLegendStyle::Style s, const QFont& f );
148 
150  void setStyleMargin( QgsComposerLegendStyle::Style s, double margin );
151  void setStyleMargin( QgsComposerLegendStyle::Style s, QgsComposerLegendStyle::Side side, double margin );
152 
153  double boxSpace() const;
154  void setBoxSpace( double s );
155 
156  double columnSpace() const;
157  void setColumnSpace( double s );
158 
159  QColor fontColor() const;
160  void setFontColor( const QColor& c );
161 
162  double symbolWidth() const;
163  void setSymbolWidth( double w );
164 
165  double symbolHeight() const;
166  void setSymbolHeight( double h );
167 
168  double wmsLegendWidth() const;
169  void setWmsLegendWidth( double w );
170 
171  double wmsLegendHeight() const;
172  void setWmsLegendHeight( double h );
173 
174  void setWrapChar( const QString& t );
175  QString wrapChar() const;
176 
177  int columnCount() const;
178  void setColumnCount( int c );
179 
180  bool splitLayer() const;
181  void setSplitLayer( bool s );
182 
183  bool equalColumnWidth() const;
184  void setEqualColumnWidth( bool s );
185 
192  bool drawRasterBorder() const;
193 
201  void setDrawRasterBorder( bool enabled );
202 
210  QColor rasterBorderColor() const;
211 
220  void setRasterBorderColor( const QColor& color );
221 
229  double rasterBorderWidth() const;
230 
239  void setRasterBorderWidth( double width );
240 
241  void setComposerMap( const QgsComposerMap* map );
242  const QgsComposerMap* composerMap() const { return mComposerMap;}
243 
245  void updateLegend();
246 
251  bool writeXml( QDomElement& elem, QDomDocument & doc ) const override;
252 
257  bool readXml( const QDomElement& itemElem, const QDomDocument& doc ) override;
258 
259  //Overridden to show legend title
260  virtual QString displayName() const override;
261 
262  public slots:
264  void synchronizeWithModel();
266  void invalidateCurrentMap();
267 
268  private slots:
269  void updateFilterByMap( bool redraw = true );
270 
272  void mapLayerStyleOverridesChanged();
273 
275  void onAtlasEnded();
276  void onAtlasFeature( QgsFeature* );
277 
278  void nodeCustomPropertyChanged( QgsLayerTreeNode* node, const QString& key );
279 
280  private:
281  QgsComposerLegend(); //forbidden
282 
284  void setCustomLayerTree( QgsLayerTreeGroup* rootGroup );
285 
286  QgsLegendModelV2* mLegendModel;
287  QgsLayerTreeGroup* mCustomLayerTree;
288 
289  QgsLegendSettings mSettings;
290 
291  const QgsComposerMap* mComposerMap;
292 
293  bool mLegendFilterByMap;
294  bool mLegendFilterByExpression;
295 
297  bool mFilterOutAtlas;
298 
300  bool mFilterAskedForUpdate;
302  void doUpdateFilterByMap();
303 
304  bool mInAtlas;
305 
307  bool mInitialMapScaleCalculated;
308 
310  bool mForceResize;
311 
313  bool mSizeToContents;
314 };
315 
316 #endif
317 
Layer tree group node serves as a container for layers and further groups.
QgsLegendModelV2 * model()
Returns the legend model.
static unsigned index
virtual QString displayName() const
Get item display name.
Item model implementation based on layer tree model for composer legend.
A item that forms part of a map composition.
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:135
The QgsLayerTreeModel class is model implementation for Qt item views framework.
virtual void updateItem()
Updates item, with the possibility to do custom update for subclasses.
virtual bool readXml(const QDomElement &itemElem, const QDomDocument &doc)
Sets item state from DOM element.
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.
Graphics scene for map printing.
Object representing map window.
const QgsComposerMap * composerMap() const
Composer legend components style.
virtual int type() const override
Return correct graphics item type.
bool legendFilterByMapEnabled() const
Find out whether legend items are filtered to show just the ones visible in the associated map...
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const override
Flags flags() const
Return OR-ed combination of model flags.
virtual bool writeXml(QDomElement &elem, QDomDocument &doc) const
Stores item state in DOM element.
A legend that can be placed onto a map composition.
The QgsLegendRenderer class handles automatic layout and rendering of legend.