QGIS API Documentation  2.18.21-Las Palmas (9fba24a)
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 "qgscomposerlegendstyle.h"
22 #include "qgscomposeritem.h"
23 #include "qgscomposerlegenditem.h"
24 #include "qgslayertreemodel.h"
25 #include "qgslegendmodel.h"
26 #include "qgslegendsettings.h"
27 
28 class QgsLayerTreeModel;
29 class QgsSymbolV2;
32 class QgsComposerMap;
33 class QgsLegendRenderer;
34 
35 
42 class CORE_EXPORT QgsLegendModelV2 : public QgsLayerTreeModel
43 {
44  Q_OBJECT
45 
46  public:
47  QgsLegendModelV2( QgsLayerTreeGroup* rootNode, QObject *parent = nullptr );
48 
49  QVariant data( const QModelIndex& index, int role ) const override;
50 
51  Qt::ItemFlags flags( const QModelIndex &index ) const override;
52 };
53 
54 
58 class CORE_EXPORT QgsComposerLegend : public QgsComposerItem
59 {
60  Q_OBJECT
61 
62  public:
63  QgsComposerLegend( QgsComposition* composition );
65 
67  virtual int type() const override { return ComposerLegend; }
68 
70  void paint( QPainter* painter, const QStyleOptionGraphicsItem* itemStyle, QWidget* pWidget ) override;
71 
73  QSizeF paintAndDetermineSize( QPainter* painter );
74 
76  void adjustBoxSize();
77 
84  void setResizeToContents( bool enabled );
85 
90  bool resizeToContents() const;
91 
93  Q_DECL_DEPRECATED QgsLegendModel* model() {return &mLegendModel;}
95 
97  QgsLegendModelV2* modelV2() { return mLegendModel2; }
98 
100  void setAutoUpdateModel( bool autoUpdate );
102  bool autoUpdateModel() const;
103 
106  void setLegendFilterByMapEnabled( bool enabled );
109  bool legendFilterByMapEnabled() const { return mLegendFilterByMap; }
110 
113  virtual void updateItem() override;
114 
118  void setLegendFilterOutAtlas( bool doFilter );
119 
123  bool legendFilterOutAtlas() const;
124 
125  //setters and getters
126  void setTitle( const QString& t );
127  QString title() const;
128 
134  Qt::AlignmentFlag titleAlignment() const;
140  void setTitleAlignment( Qt::AlignmentFlag alignment );
141 
146  void setStyle( QgsComposerLegendStyle::Style s, const QgsComposerLegendStyle& style );
147 
148  QFont styleFont( QgsComposerLegendStyle::Style s ) const;
150  void setStyleFont( QgsComposerLegendStyle::Style s, const QFont& f );
151 
153  void setStyleMargin( QgsComposerLegendStyle::Style s, double margin );
154  void setStyleMargin( QgsComposerLegendStyle::Style s, QgsComposerLegendStyle::Side side, double margin );
155 
156  double boxSpace() const;
157  void setBoxSpace( double s );
158 
159  double columnSpace() const;
160  void setColumnSpace( double s );
161 
162  QColor fontColor() const;
163  void setFontColor( const QColor& c );
164 
165  double symbolWidth() const;
166  void setSymbolWidth( double w );
167 
168  double symbolHeight() const;
169  void setSymbolHeight( double h );
170 
171  double wmsLegendWidth() const;
172  void setWmsLegendWidth( double w );
173 
174  double wmsLegendHeight() const;
175  void setWmsLegendHeight( double h );
176 
177  void setWrapChar( const QString& t );
178  QString wrapChar() const;
179 
180  int columnCount() const;
181  void setColumnCount( int c );
182 
183  bool splitLayer() const;
184  void setSplitLayer( bool s );
185 
186  bool equalColumnWidth() const;
187  void setEqualColumnWidth( bool s );
188 
195  bool drawRasterBorder() const;
196 
204  void setDrawRasterBorder( bool enabled );
205 
213  QColor rasterBorderColor() const;
214 
223  void setRasterBorderColor( const QColor& color );
224 
232  double rasterBorderWidth() const;
233 
242  void setRasterBorderWidth( double width );
243 
244  void setComposerMap( const QgsComposerMap* map );
245  const QgsComposerMap* composerMap() const { return mComposerMap;}
246 
248  void updateLegend();
249 
254  bool writeXML( QDomElement& elem, QDomDocument & doc ) const override;
255 
260  bool readXML( const QDomElement& itemElem, const QDomDocument& doc ) override;
261 
262  //Overridden to show legend title
263  virtual QString displayName() const override;
264 
265  public slots:
267  void synchronizeWithModel();
269  void invalidateCurrentMap();
270 
271  private slots:
272  void updateFilterByMap( bool redraw = true );
273 
275  void mapLayerStyleOverridesChanged();
276 
278  void onAtlasEnded();
279  void onAtlasFeature( QgsFeature* );
280 
281  void nodeCustomPropertyChanged( QgsLayerTreeNode* node, const QString& key );
282 
283  private:
284  QgsComposerLegend(); //forbidden
285 
287  void setCustomLayerTree( QgsLayerTreeGroup* rootGroup );
288 
289  QgsLegendModel mLegendModel;
290 
291  QgsLegendModelV2* mLegendModel2;
292  QgsLayerTreeGroup* mCustomLayerTree;
293 
294  QgsLegendSettings mSettings;
295 
296  const QgsComposerMap* mComposerMap;
297 
298  bool mLegendFilterByMap;
299  bool mLegendFilterByExpression;
300 
302  bool mFilterOutAtlas;
303 
305  bool mFilterAskedForUpdate;
307  void doUpdateFilterByMap();
308 
309  bool mInAtlas;
310 
312  bool mInitialMapScaleCalculated;
313 
315  bool mForceResize;
316 
318  bool mSizeToContents;
319 };
320 
321 #endif
322 
Layer tree group node serves as a container for layers and further groups.
static unsigned index
virtual QString displayName() const
Get item display name.
A model that provides group, layer and classification items.
Item model implementation based on layer tree model for composer legend.
A item that forms part of a map composition.
QgsLegendModelV2 * modelV2()
virtual bool writeXML(QDomElement &elem, QDomDocument &doc) const
Stores item state in DOM element.
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:187
The QgsLayerTreeModel class is model implementation for Qt item views framework.
virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
virtual void updateItem()
Updates item, with the possibility to do custom update for subclasses.
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.
virtual bool readXML(const QDomElement &itemElem, const QDomDocument &doc)
Sets item state from DOM element.
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.
A legend that can be placed onto a map composition.
The QgsLegendRenderer class handles automatic layout and rendering of legend.
typedef ItemFlags