QGIS API Documentation  2.99.0-Master (0a63d1f)
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 "qgis_core.h"
22 #include "qgscomposeritem.h"
23 #include "qgslayertreemodel.h"
24 #include "qgslegendsettings.h"
25 
26 class QgsLayerTreeModel;
27 class QgsSymbol;
28 class QgsComposerMap;
29 class QgsLegendRenderer;
30 
31 
38 class CORE_EXPORT QgsLegendModel : public QgsLayerTreeModel
39 {
40  Q_OBJECT
41 
42  public:
44  QgsLegendModel( 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  QgsLegendModel* 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 
142  QgsLegendStyle style( QgsLegendStyle::Style s ) const;
143  void setStyle( QgsLegendStyle::Style s, const QgsLegendStyle& style );
144 
145  QFont styleFont( QgsLegendStyle::Style s ) const;
147  void setStyleFont( QgsLegendStyle::Style s, const QFont& f );
148 
150  void setStyleMargin( QgsLegendStyle::Style s, double margin );
151  void setStyleMargin( QgsLegendStyle::Style s, QgsLegendStyle::Side side, double margin );
152 
157  double lineSpacing() const;
158 
164  void setLineSpacing( double spacing );
165 
166  double boxSpace() const;
167  void setBoxSpace( double s );
168 
169  double columnSpace() const;
170  void setColumnSpace( double s );
171 
172  QColor fontColor() const;
173  void setFontColor( const QColor& c );
174 
175  double symbolWidth() const;
176  void setSymbolWidth( double w );
177 
178  double symbolHeight() const;
179  void setSymbolHeight( double h );
180 
181  double wmsLegendWidth() const;
182  void setWmsLegendWidth( double w );
183 
184  double wmsLegendHeight() const;
185  void setWmsLegendHeight( double h );
186 
187  void setWrapChar( const QString& t );
188  QString wrapChar() const;
189 
190  int columnCount() const;
191  void setColumnCount( int c );
192 
193  bool splitLayer() const;
194  void setSplitLayer( bool s );
195 
196  bool equalColumnWidth() const;
197  void setEqualColumnWidth( bool s );
198 
205  bool drawRasterBorder() const;
206 
214  void setDrawRasterBorder( bool enabled );
215 
223  QColor rasterBorderColor() const;
224 
233  void setRasterBorderColor( const QColor& color );
234 
242  double rasterBorderWidth() const;
243 
252  void setRasterBorderWidth( double width );
253 
254  void setComposerMap( const QgsComposerMap* map );
255  const QgsComposerMap* composerMap() const { return mComposerMap;}
256 
258  void updateLegend();
259 
264  bool writeXml( QDomElement& elem, QDomDocument & doc ) const override;
265 
270  bool readXml( const QDomElement& itemElem, const QDomDocument& doc ) override;
271 
272  //Overridden to show legend title
273  virtual QString displayName() const override;
274 
275  public slots:
277  void synchronizeWithModel();
279  void invalidateCurrentMap();
280 
281  private slots:
282  void updateFilterByMap( bool redraw = true );
283 
285  void mapLayerStyleOverridesChanged();
286 
288  void onAtlasEnded();
289  void onAtlasFeature( QgsFeature* );
290 
291  void nodeCustomPropertyChanged( QgsLayerTreeNode* node, const QString& key );
292 
293  private:
294  QgsComposerLegend(); //forbidden
295 
297  void setCustomLayerTree( QgsLayerTreeGroup* rootGroup );
298 
299  QgsLegendModel* mLegendModel;
300  QgsLayerTreeGroup* mCustomLayerTree;
301 
302  QgsLegendSettings mSettings;
303 
304  const QgsComposerMap* mComposerMap;
305 
306  bool mLegendFilterByMap;
307  bool mLegendFilterByExpression;
308 
310  bool mFilterOutAtlas;
311 
313  bool mFilterAskedForUpdate;
315  void doUpdateFilterByMap();
316 
317  bool mInAtlas;
318 
320  bool mInitialMapScaleCalculated;
321 
323  bool mForceResize;
324 
326  bool mSizeToContents;
327 };
328 
329 #endif
330 
Layer tree group node serves as a container for layers and further groups.
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.
Composer legend components style.
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:136
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
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.
QgsLegendModel * model()
Returns the legend model.
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.