QGIS API Documentation  3.0.2-Girona (307d082)
qgslayoutitemlegend.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgslayoutitemlegend.h
3  ---------------------
4  begin : October 2017
5  copyright : (C) 2017 by Nyall Dawson
6  email : nyall dot dawson at gmail dot com
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 QGSLAYOUTITEMLEGEND_H
19 #define QGSLAYOUTITEMLEGEND_H
20 
21 #include "qgis_core.h"
22 #include "qgis.h"
23 #include "qgslayoutitem.h"
24 #include "qgslayertreemodel.h"
25 #include "qgslegendsettings.h"
26 #include "qgslayertreegroup.h"
27 
28 class QgsLayerTreeModel;
29 class QgsSymbol;
30 class QgsLayoutItemMap;
31 class QgsLegendRenderer;
32 
41 class CORE_EXPORT QgsLegendModel : public QgsLayerTreeModel
42 {
43  Q_OBJECT
44 
45  public:
47  QgsLegendModel( QgsLayerTree *rootNode, QObject *parent SIP_TRANSFERTHIS = 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 
55 
61 class CORE_EXPORT QgsLayoutItemLegend : public QgsLayoutItem
62 {
63  Q_OBJECT
64 
65  public:
66 
70  QgsLayoutItemLegend( QgsLayout *layout );
71 
77  static QgsLayoutItemLegend *create( QgsLayout *layout ) SIP_FACTORY;
78 
79  int type() const override;
80  QIcon icon() const override;
81  //Overridden to show legend title
82  QString displayName() const override;
83 
87  void adjustBoxSize();
88 
95  void setResizeToContents( bool enabled );
96 
101  bool resizeToContents() const;
102 
106  QgsLegendModel *model() { return mLegendModel.get(); }
107 
113  void setAutoUpdateModel( bool autoUpdate );
114 
120  bool autoUpdateModel() const;
121 
126  void setLegendFilterByMapEnabled( bool enabled );
127 
132  bool legendFilterByMapEnabled() const { return mLegendFilterByMap; }
133 
139  void setLegendFilterOutAtlas( bool doFilter );
140 
145  bool legendFilterOutAtlas() const;
146 
151  void setTitle( const QString &title );
152 
157  QString title() const;
158 
163  Qt::AlignmentFlag titleAlignment() const;
164 
169  void setTitleAlignment( Qt::AlignmentFlag alignment );
170 
175 
179  QgsLegendStyle style( QgsLegendStyle::Style s ) const;
180 
184  void setStyle( QgsLegendStyle::Style component, const QgsLegendStyle &style );
185 
190  QFont styleFont( QgsLegendStyle::Style component ) const;
191 
196  void setStyleFont( QgsLegendStyle::Style component, const QFont &font );
197 
201  void setStyleMargin( QgsLegendStyle::Style component, double margin );
202 
206  void setStyleMargin( QgsLegendStyle::Style component, QgsLegendStyle::Side side, double margin );
207 
212  double lineSpacing() const;
213 
218  void setLineSpacing( double spacing );
219 
224  double boxSpace() const;
225 
230  void setBoxSpace( double space );
231 
236  double columnSpace() const;
237 
242  void setColumnSpace( double spacing );
243 
248  QColor fontColor() const;
249 
254  void setFontColor( const QColor &color );
255 
260  double symbolWidth() const;
261 
266  void setSymbolWidth( double width );
267 
272  double symbolHeight() const;
273 
278  void setSymbolHeight( double height );
279 
284  double wmsLegendWidth() const;
285 
290  void setWmsLegendWidth( double width );
291 
296  double wmsLegendHeight() const;
297 
302  void setWmsLegendHeight( double height );
303 
308  void setWrapString( const QString &string );
309 
314  QString wrapString() const;
315 
320  int columnCount() const;
321 
326  void setColumnCount( int count );
327 
333  bool splitLayer() const;
334 
340  void setSplitLayer( bool enabled );
341 
346  bool equalColumnWidth() const;
347 
352  void setEqualColumnWidth( bool equalize );
353 
360  bool drawRasterStroke() const;
361 
369  void setDrawRasterStroke( bool enabled );
370 
378  QColor rasterStrokeColor() const;
379 
387  void setRasterStrokeColor( const QColor &color );
388 
396  double rasterStrokeWidth() const;
397 
405  void setRasterStrokeWidth( double width );
406 
411  void setLinkedMap( QgsLayoutItemMap *map );
412 
417  QgsLayoutItemMap *linkedMap() const { return mMap; }
418 
422  void updateLegend();
423 
427  void updateFilterByMap( bool redraw = true );
428 
432  const QgsLegendSettings &legendSettings() const { return mSettings; }
433 
434  void paint( QPainter *painter, const QStyleOptionGraphicsItem *itemStyle, QWidget *pWidget ) override;
435 
436  void finalizeRestoreFromXml() override;
437 
438 
439  public slots:
440 
441  void refresh() override;
443 
444  protected:
445  void draw( QgsLayoutItemRenderContext &context ) override;
446  bool writePropertiesToElement( QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context ) const override;
447  bool readPropertiesFromElement( const QDomElement &element, const QDomDocument &document, const QgsReadWriteContext &context ) override;
448 
449  private slots:
450 
452  void synchronizeWithModel();
453 
455  void invalidateCurrentMap();
456 
457  void updateFilterByMapAndRedraw();
458 
459 
461  void mapLayerStyleOverridesChanged();
462 
464  void onAtlasEnded();
465  void onAtlasFeature();
466 
467  void nodeCustomPropertyChanged( QgsLayerTreeNode *node, const QString &key );
468 
469  private:
470  QgsLayoutItemLegend() = delete;
471 
473  void setCustomLayerTree( QgsLayerTree *rootGroup );
474 
475  void setupMapConnections( QgsLayoutItemMap *map, bool connect = true );
476 
477  std::unique_ptr< QgsLegendModel > mLegendModel;
478  std::unique_ptr< QgsLayerTreeGroup > mCustomLayerTree;
479 
480  QgsLegendSettings mSettings;
481 
482  QString mTitle;
483  int mColumnCount = 1;
484 
485  QString mMapUuid;
486  QgsLayoutItemMap *mMap = nullptr;
487 
488  bool mLegendFilterByMap = false;
489  bool mLegendFilterByExpression = false;
490 
492  bool mFilterOutAtlas = false;
493 
495  bool mFilterAskedForUpdate = false;
497  void doUpdateFilterByMap();
498 
499  bool mInAtlas = false;
500 
502  bool mInitialMapScaleCalculated = false;
503 
505  bool mForceResize = false;
506 
508  bool mSizeToContents = true;
509 
511 
512 };
513 
514 #endif // QGSLAYOUTITEMLEGEND_H
515 
The class is used as a container of context for various read/write operations on other objects...
virtual QIcon icon() const
Returns the item&#39;s icon.
Item model implementation based on layer tree model for layout legend.
QgsLegendModel * model()
Returns the legend model.
Base class for graphical items within a QgsLayout.
#define SIP_TRANSFERTHIS
Definition: qgis_sip.h:46
int type() const override
Returns a unique graphics item type identifier.
const QgsLegendSettings & legendSettings() const
Returns the legend&#39;s renderer settings object.
bool legendFilterByMapEnabled() const
Find out whether legend items are filtered to show just the ones visible in the associated map...
Composer legend components style.
The QgsLayerTreeModel class is model implementation for Qt item views framework.
Layout graphical items for displaying a map.
Namespace with helper functions for layer tree operations.
Definition: qgslayertree.h:32
virtual bool readPropertiesFromElement(const QDomElement &element, const QDomDocument &document, const QgsReadWriteContext &context)
Sets item state from a DOM element.
void paint(QPainter *painter, const QStyleOptionGraphicsItem *itemStyle, QWidget *pWidget) override
Handles preparing a paint surface for the layout item and painting the item&#39;s content.
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.
#define SIP_FACTORY
Definition: qgis_sip.h:69
Side
Margin side.
Base class for layouts, which can contain items such as maps, labels, scalebars, etc.
Definition: qgslayout.h:49
Contains settings and helpers relating to a render of a QgsLayoutItem.
Definition: qgslayoutitem.h:43
virtual void finalizeRestoreFromXml()
Called after all pending items have been restored from XML.
virtual QString displayName() const
Get item display name.
QgsCompositionConverter class converts a QGIS 2.x composition to a QGIS 3.x layout.
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const override
virtual bool writePropertiesToElement(QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context) const
Stores item state within an XML DOM element.
virtual void refreshDataDefinedProperty(const QgsLayoutObject::DataDefinedProperty property=QgsLayoutObject::AllProperties)
Refreshes a data defined property for the item by reevaluating the property&#39;s value and redrawing the...
QgsLayoutItemMap * linkedMap() const
Returns the associated map.
virtual void draw(QgsLayoutItemRenderContext &context)=0
Draws the item&#39;s contents using the specified item render context.
Flags flags() const
Return OR-ed combination of model flags.
A layout item subclass for map legends.
DataDefinedProperty
Data defined properties for different item types.
void refresh() override
Refreshes the item, causing a recalculation of any property overrides and recalculation of its positi...
The QgsLegendRenderer class handles automatic layout and rendering of legend.
All properties for item.