QGIS API Documentation  3.20.0-Odense (decaadbb31)
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 
556  QgsExpressionContext createExpressionContext() const override;
557  ExportLayerBehavior exportLayerBehavior() const override;
558  bool accept( QgsStyleEntityVisitorInterface *visitor ) const override;
559 
560  public slots:
561 
562  void refresh() override;
563  void refreshDataDefinedProperty( QgsLayoutObject::DataDefinedProperty property = QgsLayoutObject::AllProperties ) 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 
QgsCompositionConverter class converts a QGIS 2.x composition to a QGIS 3.x layout.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
Layer tree node points to a map layer.
The QgsLayerTreeModel class is model implementation for Qt item views framework.
int columnCount(const QModelIndex &parent=QModelIndex()) const override
Flags flags() const
Returns OR-ed combination of model flags.
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...
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const override
void nodeCustomPropertyChanged(QgsLayerTreeNode *node, const QString &key)
This class is a base class for nodes in a layer tree.
Namespace with helper functions for layer tree operations.
Definition: qgslayertree.h:33
A layout item subclass for map legends.
QgsLayoutItemMap * linkedMap() const
Returns the associated map.
QgsLegendModel * model()
Returns the legend model.
const QgsLegendSettings & legendSettings() const
Returns the legend's renderer settings object.
bool legendFilterByMapEnabled() const
Find out whether legend items are filtered to show just the ones visible in the associated map.
Layout graphical items for displaying a map.
Contains settings and helpers relating to a render of a QgsLayoutItem.
Definition: qgslayoutitem.h:45
Base class for graphical items within a QgsLayout.
DataDefinedProperty
Data defined properties for different item types.
@ AllProperties
All properties for item.
Base class for layouts, which can contain items such as maps, labels, scalebars, etc.
Definition: qgslayout.h:51
Item model implementation based on layer tree model for layout legend.
void refreshLegend()
Emitted to refresh the legend.
The QgsLegendRenderer class handles automatic layout and rendering of legend.
The QgsLegendSettings class stores the appearance and layout settings for legend drawing with QgsLege...
Contains detailed styling information relating to how a layout legend should be rendered.
Side
Margin sides.
Style
Component of legends which can be styled.
The class is used as a container of context for various read/write operations on other objects.
An interface for classes which can visit style entity (e.g.
Abstract base class for all rendered symbols.
Definition: qgssymbol.h:38
#define SIP_TRANSFERTHIS
Definition: qgis_sip.h:53
#define SIP_SKIP
Definition: qgis_sip.h:126
#define SIP_FACTORY
Definition: qgis_sip.h:76