QGIS API Documentation 3.32.0-Lima (311a8cb8a6)
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"
28
30class QgsSymbol;
34
43class 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
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
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
113class CORE_EXPORT QgsLayoutItemLegend : public QgsLayoutItem
114{
115 Q_OBJECT
116
117 public:
118
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
245 Q_DECL_DEPRECATED QFont styleFont( QgsLegendStyle::Style component ) const SIP_DEPRECATED;
246
253 Q_DECL_DEPRECATED void setStyleFont( QgsLegendStyle::Style component, const QFont &font ) SIP_DEPRECATED;
254
258 void setStyleMargin( QgsLegendStyle::Style component, double margin );
259
263 void setStyleMargin( QgsLegendStyle::Style component, QgsLegendStyle::Side side, double margin );
264
271 Q_DECL_DEPRECATED double lineSpacing() const SIP_DEPRECATED;
272
279 Q_DECL_DEPRECATED void setLineSpacing( double spacing ) SIP_DEPRECATED;
280
285 double boxSpace() const;
286
291 void setBoxSpace( double space );
292
297 double columnSpace() const;
298
303 void setColumnSpace( double spacing );
304
311 Q_DECL_DEPRECATED QColor fontColor() const SIP_DEPRECATED;
312
319 Q_DECL_DEPRECATED void setFontColor( const QColor &color ) SIP_DEPRECATED;
320
325 double symbolWidth() const;
326
331 void setSymbolWidth( double width );
332
339 double maximumSymbolSize() const;
340
349 void setMaximumSymbolSize( double size );
350
357 double minimumSymbolSize() const;
358
367 void setMinimumSymbolSize( double size );
368
377 void setSymbolAlignment( Qt::AlignmentFlag alignment );
378
387 Qt::AlignmentFlag symbolAlignment() const;
388
393 double symbolHeight() const;
394
399 void setSymbolHeight( double height );
400
405 double wmsLegendWidth() const;
406
411 void setWmsLegendWidth( double width );
412
417 double wmsLegendHeight() const;
418
423 void setWmsLegendHeight( double height );
424
429 void setWrapString( const QString &string );
430
435 QString wrapString() const;
436
441 int columnCount() const;
442
447 void setColumnCount( int count );
448
454 bool splitLayer() const;
455
461 void setSplitLayer( bool enabled );
462
467 bool equalColumnWidth() const;
468
473 void setEqualColumnWidth( bool equalize );
474
481 bool drawRasterStroke() const;
482
490 void setDrawRasterStroke( bool enabled );
491
499 QColor rasterStrokeColor() const;
500
508 void setRasterStrokeColor( const QColor &color );
509
517 double rasterStrokeWidth() const;
518
526 void setRasterStrokeWidth( double width );
527
533 void setLinkedMap( QgsLayoutItemMap *map );
534
539 QgsLayoutItemMap *linkedMap() const { return mMap; }
540
549 void setFilterByMapItems( const QList< QgsLayoutItemMap * > &maps );
550
559 QList< QgsLayoutItemMap * > filterByMapItems() const;
560
568 QString themeName() const;
569
573 void updateLegend();
574
578 void updateFilterByMap( bool redraw = true );
579
583 const QgsLegendSettings &legendSettings() const { return mSettings; }
584
585 void paint( QPainter *painter, const QStyleOptionGraphicsItem *itemStyle, QWidget *pWidget ) override;
586
587 void finalizeRestoreFromXml() override;
588
589 QgsExpressionContext createExpressionContext() const override;
590 ExportLayerBehavior exportLayerBehavior() const override;
591 bool accept( QgsStyleEntityVisitorInterface *visitor ) const override;
592 bool isRefreshing() const override;
593
594 public slots:
595
596 void refresh() override;
597 void invalidateCache() override;
598 void refreshDataDefinedProperty( QgsLayoutObject::DataDefinedProperty property = QgsLayoutObject::AllProperties ) override;
599
600 protected:
601 void draw( QgsLayoutItemRenderContext &context ) override;
602 bool writePropertiesToElement( QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context ) const override;
603 bool readPropertiesFromElement( const QDomElement &element, const QDomDocument &document, const QgsReadWriteContext &context ) override;
604
605 private slots:
606
608 void invalidateCurrentMap();
609
610 void updateFilterByMapAndRedraw();
611
612
614 void mapLayerStyleOverridesChanged();
616 void mapThemeChanged( const QString &theme );
617
619 void onAtlasEnded();
620 void onAtlasFeature();
621
622 void nodeCustomPropertyChanged( QgsLayerTreeNode *node, const QString &key );
623
625 void clearLegendCachedData();
626
627 private:
628 QgsLayoutItemLegend() = delete;
629
631 void setCustomLayerTree( QgsLayerTree *rootGroup );
632
633 void setupMapConnections( QgsLayoutItemMap *map, bool connect = true );
634
635 void setModelStyleOverrides( const QMap<QString, QString> &overrides );
636
637 std::unique_ptr< QgsLegendModel > mLegendModel;
638 std::unique_ptr< QgsLayerTreeGroup > mCustomLayerTree;
639
640 QgsLegendSettings mSettings;
641
642 QString mTitle;
643 int mColumnCount = 1;
644
645 QString mMapUuid;
646 QgsLayoutItemMap *mMap = nullptr;
647
648 QList< QString > mFilterByMapUuids;
649 QList< QPointer< QgsLayoutItemMap >> mFilterByMapItems;
650
651 bool mLegendFilterByMap = false;
652 bool mLegendFilterByExpression = false;
653
655 bool mFilterOutAtlas = false;
656
658 bool mFilterAskedForUpdate = false;
660 void doUpdateFilterByMap();
661
662 bool mInAtlas = false;
663
665 bool mInitialMapScaleCalculated = false;
666
668 bool mForceResize = false;
669
671 bool mSizeToContents = true;
672
674 QString mThemeName;
675
677
678};
679
680#endif // QGSLAYOUTITEMLEGEND_H
681
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.
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.
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:44
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:50
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:94
#define SIP_DEPRECATED
Definition: qgis_sip.h:106
#define SIP_TRANSFERTHIS
Definition: qgis_sip.h:53
#define SIP_SKIP
Definition: qgis_sip.h:126
#define SIP_FACTORY
Definition: qgis_sip.h:76