QGIS API Documentation  3.16.0-Hannover (43b64b13f3)
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_sip.h"
23 #include "qgslayoutitem.h"
24 #include "qgslayertreemodel.h"
25 #include "qgslegendsettings.h"
26 #include "qgslayertreegroup.h"
27 #include "qgsexpressioncontext.h"
28 
29 class QgsLayerTreeModel;
30 class QgsSymbol;
31 class QgsLayoutItemMap;
32 class QgsLegendRenderer;
34 
43 class CORE_EXPORT QgsLegendModel : public QgsLayerTreeModel
44 {
45  Q_OBJECT
46 
47  public:
49  QgsLegendModel( QgsLayerTree *rootNode, QObject *parent SIP_TRANSFERTHIS = nullptr, QgsLayoutItemLegend *layout = nullptr );
50 
52  QgsLegendModel( QgsLayerTree *rootNode, QgsLayoutItemLegend *layout );
53 
54  QVariant data( const QModelIndex &index, int role ) const override;
55 
56  Qt::ItemFlags flags( const QModelIndex &index ) const override;
57 
66  QList<QgsLayerTreeModelLegendNode *> layerLegendNodes( QgsLayerTreeLayer *nodeLayer, bool skipNodeEmbeddedInParent = false ) const SIP_SKIP;
67 
72  void clearCachedData( QgsLayerTreeNode *node ) const;
73 
74  signals:
75 
80  void refreshLegend();
81 
82  private slots:
83 
88  void forceRefresh();
89 
90  private:
91 
96  QgsLayoutItemLegend *mLayoutLegend = nullptr;
97 
102  QString evaluateLayerExpressions( QgsLayerTreeLayer *nodeLayer ) const;
103 
104 };
105 
106 
107 
113 class CORE_EXPORT QgsLayoutItemLegend : public QgsLayoutItem
114 {
115  Q_OBJECT
116 
117  public:
118 
122  QgsLayoutItemLegend( QgsLayout *layout );
123 
129  static QgsLayoutItemLegend *create( QgsLayout *layout ) SIP_FACTORY;
130 
131  int type() const override;
132  QIcon icon() const override;
133  QgsLayoutItem::Flags itemFlags() const override;
134  //Overridden to show legend title
135  QString displayName() const override;
136 
140  void adjustBoxSize();
141 
148  void setResizeToContents( bool enabled );
149 
154  bool resizeToContents() const;
155 
159  QgsLegendModel *model() { return mLegendModel.get(); }
160 
166  void setAutoUpdateModel( bool autoUpdate );
167 
173  bool autoUpdateModel() const;
174 
179  void setLegendFilterByMapEnabled( bool enabled );
180 
185  bool legendFilterByMapEnabled() const { return mLegendFilterByMap; }
186 
192  void setLegendFilterOutAtlas( bool doFilter );
193 
198  bool legendFilterOutAtlas() const;
199 
204  void setTitle( const QString &title );
205 
210  QString title() const;
211 
216  Qt::AlignmentFlag titleAlignment() const;
217 
222  void setTitleAlignment( Qt::AlignmentFlag alignment );
223 
228 
232  QgsLegendStyle style( QgsLegendStyle::Style s ) const;
233 
237  void setStyle( QgsLegendStyle::Style component, const QgsLegendStyle &style );
238 
243  QFont styleFont( QgsLegendStyle::Style component ) const;
244 
249  void setStyleFont( QgsLegendStyle::Style component, const QFont &font );
250 
254  void setStyleMargin( QgsLegendStyle::Style component, double margin );
255 
259  void setStyleMargin( QgsLegendStyle::Style component, QgsLegendStyle::Side side, double margin );
260 
265  double lineSpacing() const;
266 
271  void setLineSpacing( double spacing );
272 
277  double boxSpace() const;
278 
283  void setBoxSpace( double space );
284 
289  double columnSpace() const;
290 
295  void setColumnSpace( double spacing );
296 
301  QColor fontColor() const;
302 
307  void setFontColor( const QColor &color );
308 
313  double symbolWidth() const;
314 
319  void setSymbolWidth( double width );
320 
327  double maximumSymbolSize() const;
328 
337  void setMaximumSymbolSize( double size );
338 
345  double minimumSymbolSize() const;
346 
355  void setMinimumSymbolSize( double size );
356 
365  void setSymbolAlignment( Qt::AlignmentFlag alignment );
366 
375  Qt::AlignmentFlag symbolAlignment() const;
376 
381  double symbolHeight() const;
382 
387  void setSymbolHeight( double height );
388 
393  double wmsLegendWidth() const;
394 
399  void setWmsLegendWidth( double width );
400 
405  double wmsLegendHeight() const;
406 
411  void setWmsLegendHeight( double height );
412 
417  void setWrapString( const QString &string );
418 
423  QString wrapString() const;
424 
429  int columnCount() const;
430 
435  void setColumnCount( int count );
436 
442  bool splitLayer() const;
443 
449  void setSplitLayer( bool enabled );
450 
455  bool equalColumnWidth() const;
456 
461  void setEqualColumnWidth( bool equalize );
462 
469  bool drawRasterStroke() const;
470 
478  void setDrawRasterStroke( bool enabled );
479 
487  QColor rasterStrokeColor() const;
488 
496  void setRasterStrokeColor( const QColor &color );
497 
505  double rasterStrokeWidth() const;
506 
514  void setRasterStrokeWidth( double width );
515 
520  void setLinkedMap( QgsLayoutItemMap *map );
521 
526  QgsLayoutItemMap *linkedMap() const { return mMap; }
527 
535  QString themeName() const;
536 
540  void updateLegend();
541 
545  void updateFilterByMap( bool redraw = true );
546 
550  const QgsLegendSettings &legendSettings() const { return mSettings; }
551 
552  void paint( QPainter *painter, const QStyleOptionGraphicsItem *itemStyle, QWidget *pWidget ) override;
553 
554  void finalizeRestoreFromXml() override;
555 
557  ExportLayerBehavior exportLayerBehavior() const override;
558  bool accept( QgsStyleEntityVisitorInterface *visitor ) const override;
559 
560  public slots:
561 
562  void refresh() override;
564 
565  protected:
566  void draw( QgsLayoutItemRenderContext &context ) override;
567  bool writePropertiesToElement( QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context ) const override;
568  bool readPropertiesFromElement( const QDomElement &element, const QDomDocument &document, const QgsReadWriteContext &context ) override;
569 
570  private slots:
571 
573  void invalidateCurrentMap();
574 
575  void updateFilterByMapAndRedraw();
576 
577 
579  void mapLayerStyleOverridesChanged();
581  void mapThemeChanged( const QString &theme );
582 
584  void onAtlasEnded();
585  void onAtlasFeature();
586 
587  void nodeCustomPropertyChanged( QgsLayerTreeNode *node, const QString &key );
588 
590  void clearLegendCachedData();
591 
592  private:
593  QgsLayoutItemLegend() = delete;
594 
596  void setCustomLayerTree( QgsLayerTree *rootGroup );
597 
598  void setupMapConnections( QgsLayoutItemMap *map, bool connect = true );
599 
600  void setModelStyleOverrides( const QMap<QString, QString> &overrides );
601 
602  std::unique_ptr< QgsLegendModel > mLegendModel;
603  std::unique_ptr< QgsLayerTreeGroup > mCustomLayerTree;
604 
605  QgsLegendSettings mSettings;
606 
607  QString mTitle;
608  int mColumnCount = 1;
609 
610  QString mMapUuid;
611  QgsLayoutItemMap *mMap = nullptr;
612 
613  bool mLegendFilterByMap = false;
614  bool mLegendFilterByExpression = false;
615 
617  bool mFilterOutAtlas = false;
618 
620  bool mFilterAskedForUpdate = false;
622  void doUpdateFilterByMap();
623 
624  bool mInAtlas = false;
625 
627  bool mInitialMapScaleCalculated = false;
628 
630  bool mForceResize = false;
631 
633  bool mSizeToContents = true;
634 
636  QString mThemeName;
637 
639 
640 };
641 
642 #endif // QGSLAYOUTITEMLEGEND_H
643 
QgsExpressionContext
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
Definition: qgsexpressioncontext.h:370
QgsLegendStyle::Style
Style
Component of legends which can be styled.
Definition: qgslegendstyle.h:41
QgsLayerTreeNode
This class is a base class for nodes in a layer tree.
Definition: qgslayertreenode.h:75
QgsLayoutItem::writePropertiesToElement
virtual bool writePropertiesToElement(QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context) const
Stores item state within an XML DOM element.
Definition: qgslayoutitem.cpp:1325
QgsReadWriteContext
The class is used as a container of context for various read/write operations on other objects.
Definition: qgsreadwritecontext.h:35
QgsLayoutItem::icon
virtual QIcon icon() const
Returns the item's icon.
Definition: qgslayoutitem.h:334
QgsLayoutItem::finalizeRestoreFromXml
virtual void finalizeRestoreFromXml()
Called after all pending items have been restored from XML.
Definition: qgslayoutitem.cpp:824
QgsLayoutItemRenderContext
Contains settings and helpers relating to a render of a QgsLayoutItem.
Definition: qgslayoutitem.h:45
QgsStyleEntityVisitorInterface
An interface for classes which can visit style entity (e.g.
Definition: qgsstyleentityvisitor.h:34
QgsLayoutItem::refreshDataDefinedProperty
virtual void refreshDataDefinedProperty(QgsLayoutObject::DataDefinedProperty property=QgsLayoutObject::AllProperties)
Refreshes a data defined property for the item by reevaluating the property's value and redrawing the...
Definition: qgslayoutitem.cpp:1081
QgsLayoutItemLegend::model
QgsLegendModel * model()
Returns the legend model.
Definition: qgslayoutitemlegend.h:159
QgsLayerTreeModel
The QgsLayerTreeModel class is model implementation for Qt item views framework.
Definition: qgslayertreemodel.h:54
QgsSymbol
Abstract base class for all rendered symbols.
Definition: qgssymbol.h:64
QgsLayoutItem::readPropertiesFromElement
virtual bool readPropertiesFromElement(const QDomElement &element, const QDomDocument &document, const QgsReadWriteContext &context)
Sets item state from a DOM element.
Definition: qgslayoutitem.cpp:1330
QgsLegendModel::refreshLegend
void refreshLegend()
Emitted to refresh the legend.
QgsLayoutItem::draw
virtual void draw(QgsLayoutItemRenderContext &context)=0
Draws the item's contents using the specified item render context.
SIP_FACTORY
#define SIP_FACTORY
Definition: qgis_sip.h:76
QgsLayoutItemLegend::legendSettings
const QgsLegendSettings & legendSettings() const
Returns the legend's renderer settings object.
Definition: qgslayoutitemlegend.h:550
QgsLayoutItem::type
int type() const override
Returns a unique graphics item type identifier.
Definition: qgslayoutitem.cpp:124
QgsLayoutItemLegend
A layout item subclass for map legends.
Definition: qgslayoutitemlegend.h:114
QgsLayoutObject::AllProperties
@ AllProperties
All properties for item.
Definition: qgslayoutobject.h:135
qgsexpressioncontext.h
QgsLayoutItemLegend::legendFilterByMapEnabled
bool legendFilterByMapEnabled() const
Find out whether legend items are filtered to show just the ones visible in the associated map.
Definition: qgslayoutitemlegend.h:185
SIP_SKIP
#define SIP_SKIP
Definition: qgis_sip.h:126
QgsLayerTree
Namespace with helper functions for layer tree operations.
Definition: qgslayertree.h:33
QgsLayoutItem::exportLayerBehavior
virtual ExportLayerBehavior exportLayerBehavior() const
Returns the behavior of this item during exporting to layered exports (e.g.
Definition: qgslayoutitem.cpp:247
QgsLegendStyle::Side
Side
Margin sides.
Definition: qgslegendstyle.h:55
qgslayoutitem.h
QgsCompositionConverter
QgsCompositionConverter class converts a QGIS 2.x composition to a QGIS 3.x layout.
Definition: qgscompositionconverter.h:55
QgsLayoutItem::accept
virtual bool accept(QgsStyleEntityVisitorInterface *visitor) const
Accepts the specified style entity visitor, causing it to visit all style entities associated with th...
Definition: qgslayoutitem.cpp:1165
qgslayertreegroup.h
qgslegendsettings.h
QgsLayerTreeLayer
Layer tree node points to a map layer.
Definition: qgslayertreelayer.h:44
qgis_sip.h
QgsLegendSettings
The QgsLegendSettings class stores the appearance and layout settings for legend drawing with QgsLege...
Definition: qgslegendsettings.h:39
QgsLayoutItem::paint
void paint(QPainter *painter, const QStyleOptionGraphicsItem *itemStyle, QWidget *pWidget) override
Handles preparing a paint surface for the layout item and painting the item's content.
Definition: qgslayoutitem.cpp:285
QgsLayoutItemLegend::linkedMap
QgsLayoutItemMap * linkedMap() const
Returns the associated map.
Definition: qgslayoutitemlegend.h:526
QgsLayoutItem
Base class for graphical items within a QgsLayout.
Definition: qgslayoutitem.h:113
QgsLayoutItem::refresh
void refresh() override
Refreshes the item, causing a recalculation of any property overrides and recalculation of its positi...
Definition: qgslayoutitem.cpp:1176
QgsLayoutItemMap
Layout graphical items for displaying a map.
Definition: qgslayoutitemmap.h:318
QgsLayoutItem::itemFlags
virtual Flags itemFlags() const
Returns the item's flags, which indicate how the item behaves.
Definition: qgslayoutitem.cpp:129
QgsLayout
Base class for layouts, which can contain items such as maps, labels, scalebars, etc.
Definition: qgslayout.h:50
QgsLayoutItem::displayName
virtual QString displayName() const
Gets item display name.
Definition: qgslayoutitem.cpp:107
QgsLayerTreeModel::flags
Flags flags() const
Returns OR-ed combination of model flags.
Definition: qgslayertreemodel.cpp:1183
qgslayertreemodel.h
QgsLegendRenderer
The QgsLegendRenderer class handles automatic layout and rendering of legend.
Definition: qgslegendrenderer.h:47
QgsLayerTreeModel::layerLegendNodes
QList< QgsLayerTreeModelLegendNode * > layerLegendNodes(QgsLayerTreeLayer *nodeLayer, bool skipNodeEmbeddedInParent=false)
Returns filtered list of active legend nodes attached to a particular layer node (by default it retur...
Definition: qgslayertreemodel.cpp:1550
QgsLayerTreeModel::data
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const override
Definition: qgslayertreemodel.cpp:158
QgsLegendModel
Item model implementation based on layer tree model for layout legend.
Definition: qgslayoutitemlegend.h:44
SIP_TRANSFERTHIS
#define SIP_TRANSFERTHIS
Definition: qgis_sip.h:53
QgsLayoutItem::createExpressionContext
QgsExpressionContext createExpressionContext() const override
This method needs to be reimplemented in all classes which implement this interface and return an exp...
Definition: qgslayoutitem.cpp:1158
QgsLayoutObject::DataDefinedProperty
DataDefinedProperty
Data defined properties for different item types.
Definition: qgslayoutobject.h:133
QgsLegendStyle
Contains detailed styling information relating to how a layout legend should be rendered.
Definition: qgslegendstyle.h:36