QGIS API Documentation  2.99.0-Master (7d4f81d)
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 "qgis.h"
23 #include "qgscomposeritem.h"
24 #include "qgslayertreemodel.h"
25 #include "qgslegendsettings.h"
26 #include "qgslayertreegroup.h"
27 
28 class QgsLayerTreeModel;
29 class QgsSymbol;
30 class QgsComposerMap;
31 class QgsLegendRenderer;
32 
33 
40 class CORE_EXPORT QgsLegendModel : public QgsLayerTreeModel
41 {
42  Q_OBJECT
43 
44  public:
46  QgsLegendModel( QgsLayerTree *rootNode, QObject *parent SIP_TRANSFERTHIS = 0 );
47 
48  QVariant data( const QModelIndex &index, int role ) const override;
49 
50  Qt::ItemFlags flags( const QModelIndex &index ) const override;
51 };
52 
53 
57 class CORE_EXPORT QgsComposerLegend : public QgsComposerItem
58 {
59  Q_OBJECT
60 
61  public:
63 
65  virtual int type() const override { return ComposerLegend; }
66 
68  void paint( QPainter *painter, const QStyleOptionGraphicsItem *itemStyle, QWidget *pWidget ) override;
69 
71  QSizeF paintAndDetermineSize( QPainter *painter );
72 
74  void adjustBoxSize();
75 
82  void setResizeToContents( bool enabled );
83 
88  bool resizeToContents() const;
89 
90 
94  QgsLegendModel *model() { return mLegendModel.get(); }
95 
97  void setAutoUpdateModel( bool autoUpdate );
99  bool autoUpdateModel() const;
100 
103  void setLegendFilterByMapEnabled( bool enabled );
106  bool legendFilterByMapEnabled() const { return mLegendFilterByMap; }
107 
110  virtual void updateItem() override;
111 
115  void setLegendFilterOutAtlas( bool doFilter );
116 
120  bool legendFilterOutAtlas() const;
121 
122  //setters and getters
123  void setTitle( const QString &t );
124  QString title() const;
125 
131  Qt::AlignmentFlag titleAlignment() const;
132 
138  void setTitleAlignment( Qt::AlignmentFlag alignment );
139 
143  QgsLegendStyle style( QgsLegendStyle::Style s ) const;
144  void setStyle( QgsLegendStyle::Style s, const QgsLegendStyle &style );
145 
146  QFont styleFont( QgsLegendStyle::Style s ) const;
148  void setStyleFont( QgsLegendStyle::Style s, const QFont &f );
149 
151  void setStyleMargin( QgsLegendStyle::Style s, double margin );
152  void setStyleMargin( QgsLegendStyle::Style s, QgsLegendStyle::Side side, double margin );
153 
158  double lineSpacing() const;
159 
165  void setLineSpacing( double spacing );
166 
167  double boxSpace() const;
168  void setBoxSpace( double s );
169 
170  double columnSpace() const;
171  void setColumnSpace( double s );
172 
173  QColor fontColor() const;
174  void setFontColor( const QColor &c );
175 
176  double symbolWidth() const;
177  void setSymbolWidth( double w );
178 
179  double symbolHeight() const;
180  void setSymbolHeight( double h );
181 
182  double wmsLegendWidth() const;
183  void setWmsLegendWidth( double w );
184 
185  double wmsLegendHeight() const;
186  void setWmsLegendHeight( double h );
187 
188  void setWrapChar( const QString &t );
189  QString wrapChar() const;
190 
191  int columnCount() const;
192  void setColumnCount( int c );
193 
194  bool splitLayer() const;
195  void setSplitLayer( bool s );
196 
197  bool equalColumnWidth() const;
198  void setEqualColumnWidth( bool s );
199 
206  bool drawRasterStroke() const;
207 
215  void setDrawRasterStroke( bool enabled );
216 
224  QColor rasterStrokeColor() const;
225 
234  void setRasterStrokeColor( const QColor &color );
235 
243  double rasterStrokeWidth() const;
244 
253  void setRasterStrokeWidth( double width );
254 
255  void setComposerMap( const QgsComposerMap *map );
256  const QgsComposerMap *composerMap() const { return mComposerMap;}
257 
259  void updateLegend();
260 
265  bool writeXml( QDomElement &elem, QDomDocument &doc ) const override;
266 
271  bool readXml( const QDomElement &itemElem, const QDomDocument &doc ) override;
272 
273  //Overridden to show legend title
274  virtual QString displayName() const override;
275 
280  const QgsLegendSettings &legendSettings() const { return mSettings; }
281 
282  public slots:
284  void synchronizeWithModel();
286  void invalidateCurrentMap();
287 
288  virtual void refreshDataDefinedProperty( const QgsComposerObject::DataDefinedProperty property = QgsComposerObject::AllProperties, const QgsExpressionContext *context = nullptr ) override;
289 
290 
291  private slots:
292 
293  void updateFilterByMapAndRedraw();
294 
295  void updateFilterByMap( bool redraw = true );
296 
298  void mapLayerStyleOverridesChanged();
299 
301  void onAtlasEnded();
302  void onAtlasFeature( QgsFeature * );
303 
304  void nodeCustomPropertyChanged( QgsLayerTreeNode *node, const QString &key );
305 
306  private:
307  QgsComposerLegend(); //forbidden
308 
310  void setCustomLayerTree( QgsLayerTree *rootGroup );
311 
312  std::unique_ptr< QgsLegendModel > mLegendModel;
313  std::unique_ptr< QgsLayerTreeGroup > mCustomLayerTree;
314 
315  QgsLegendSettings mSettings;
316 
317  QString mTitle;
318  int mColumnCount = 1;
319 
320  const QgsComposerMap *mComposerMap = nullptr;
321 
322  bool mLegendFilterByMap;
323  bool mLegendFilterByExpression;
324 
326  bool mFilterOutAtlas;
327 
329  bool mFilterAskedForUpdate;
331  void doUpdateFilterByMap();
332 
333  bool mInAtlas;
334 
336  bool mInitialMapScaleCalculated;
337 
339  bool mForceResize;
340 
342  bool mSizeToContents;
343 };
344 
345 #endif
346 
virtual QString displayName() const
Get item display name.
Item model implementation based on layer tree model for composer legend.
#define SIP_TRANSFERTHIS
Definition: qgis_sip.h:46
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:61
DataDefinedProperty
Data defined properties for different item types.
The QgsLayerTreeModel class is model implementation for Qt item views framework.
Namespace with helper functions for layer tree operations.
Definition: qgslayertree.h:31
virtual bool readXml(const QDomElement &itemElem, const QDomDocument &doc)
Sets item state from DOM element.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
The QgsLegendSettings class stores the appearance and layout settings for legend drawing with QgsLege...
virtual void refreshDataDefinedProperty(const QgsComposerObject::DataDefinedProperty property=QgsComposerObject::AllProperties, const QgsExpressionContext *context=nullptr) override
Refreshes a data defined property for the item by reevaluating the property&#39;s value and redrawing the...
This class is a base class for nodes in a layer tree.
Side
Margin side.
Graphics scene for map printing.
Object representing map window.
const QgsComposerMap * composerMap() const
const QgsLegendSettings & legendSettings() const
Returns the legend&#39;s renderer settings object.
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
virtual void updateItem()
Updates (redraws) the item, with the possibility to do custom update for subclasses.
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.
All properties for item.
The QgsLegendRenderer class handles automatic layout and rendering of legend.