QGIS API Documentation  2.99.0-Master (f1c3692)
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 
29 class QgsLayerTreeModel;
30 class QgsSymbol;
31 class QgsComposerMap;
32 class QgsLegendRenderer;
33 class QgsLegendModel;
34 
35 
40 class CORE_EXPORT QgsComposerLegend : public QgsComposerItem
41 {
42  Q_OBJECT
43 
44  public:
47 
49  virtual int type() const override { return ComposerLegend; }
50 
52  void paint( QPainter *painter, const QStyleOptionGraphicsItem *itemStyle, QWidget *pWidget ) override;
53 
55  QSizeF paintAndDetermineSize( QPainter *painter );
56 
58  void adjustBoxSize();
59 
67  void setResizeToContents( bool enabled );
68 
74  bool resizeToContents() const;
75 
76 
80  QgsLegendModel *model();
81 
83  void setAutoUpdateModel( bool autoUpdate );
85  bool autoUpdateModel() const;
86 
91  void setLegendFilterByMapEnabled( bool enabled );
92 
97  bool legendFilterByMapEnabled() const { return mLegendFilterByMap; }
98 
103  virtual void updateItem() override;
104 
110  void setLegendFilterOutAtlas( bool doFilter );
111 
117  bool legendFilterOutAtlas() const;
118 
119  //setters and getters
120  void setTitle( const QString &t );
121  QString title() const;
122 
129  Qt::AlignmentFlag titleAlignment() const;
130 
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 
158  double lineSpacing() const;
159 
166  void setLineSpacing( double spacing );
167 
168  double boxSpace() const;
169  void setBoxSpace( double s );
170 
171  double columnSpace() const;
172  void setColumnSpace( double s );
173 
174  QColor fontColor() const;
175  void setFontColor( const QColor &c );
176 
177  double symbolWidth() const;
178  void setSymbolWidth( double w );
179 
180  double symbolHeight() const;
181  void setSymbolHeight( double h );
182 
183  double wmsLegendWidth() const;
184  void setWmsLegendWidth( double w );
185 
186  double wmsLegendHeight() const;
187  void setWmsLegendHeight( double h );
188 
189  void setWrapChar( const QString &t );
190  QString wrapChar() const;
191 
192  int columnCount() const;
193  void setColumnCount( int c );
194 
195  bool splitLayer() const;
196  void setSplitLayer( bool s );
197 
198  bool equalColumnWidth() const;
199  void setEqualColumnWidth( bool s );
200 
208  bool drawRasterStroke() const;
209 
218  void setDrawRasterStroke( bool enabled );
219 
228  QColor rasterStrokeColor() const;
229 
239  void setRasterStrokeColor( const QColor &color );
240 
249  double rasterStrokeWidth() const;
250 
260  void setRasterStrokeWidth( double width );
261 
262  void setComposerMap( const QgsComposerMap *map );
263  const QgsComposerMap *composerMap() const { return mComposerMap;}
264 
266  void updateLegend();
267 
273  bool writeXml( QDomElement &elem, QDomDocument &doc ) const override;
274 
280  bool readXml( const QDomElement &itemElem, const QDomDocument &doc ) override;
281 
282  //Overridden to show legend title
283  virtual QString displayName() const override;
284 
289  const QgsLegendSettings &legendSettings() const { return mSettings; }
290 
291  public slots:
293  void synchronizeWithModel();
295  void invalidateCurrentMap();
296 
297  virtual void refreshDataDefinedProperty( const QgsComposerObject::DataDefinedProperty property = QgsComposerObject::AllProperties, const QgsExpressionContext *context = nullptr ) override;
298 
299 
300  private slots:
301 
302  void updateFilterByMapAndRedraw();
303 
304  void updateFilterByMap( bool redraw = true );
305 
307  void mapLayerStyleOverridesChanged();
308 
310  void onAtlasEnded();
311  void onAtlasFeature( QgsFeature * );
312 
313  void nodeCustomPropertyChanged( QgsLayerTreeNode *node, const QString &key );
314 
315  private:
316  QgsComposerLegend(); //forbidden
317 
319  void setCustomLayerTree( QgsLayerTree *rootGroup );
320 
321  QgsLegendModel *mLegendModel = nullptr;
322  std::unique_ptr< QgsLayerTreeGroup > mCustomLayerTree;
323 
324  QgsLegendSettings mSettings;
325 
326  QString mTitle;
327  int mColumnCount = 1;
328 
329  const QgsComposerMap *mComposerMap = nullptr;
330 
331  bool mLegendFilterByMap = false;
332  bool mLegendFilterByExpression = false;
333 
335  bool mFilterOutAtlas = false;
336 
338  bool mFilterAskedForUpdate = false;
340  void doUpdateFilterByMap();
341 
342  bool mInAtlas = false;
343 
345  bool mInitialMapScaleCalculated = false;
346 
348  bool mForceResize = false;
349 
351  bool mSizeToContents = true;
352 };
353 
354 #endif
355 
virtual QString displayName() const
Get item display name.
Item model implementation based on layer tree model for layout 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:62
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:32
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...
virtual void updateItem()
Updates (redraws) the item, with the possibility to do custom update for subclasses.
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.