QGIS API Documentation 3.37.0-Master (fdefdf9c27f)
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 "qgslayertree.h"
28
30class QgsSymbol;
34
42class CORE_EXPORT QgsLegendModel : public QgsLayerTreeModel
43{
44 Q_OBJECT
45
46 public:
48 QgsLegendModel( QgsLayerTree *rootNode, QObject *parent SIP_TRANSFERTHIS = nullptr, QgsLayoutItemLegend *layout = nullptr );
49
52
53 QVariant data( const QModelIndex &index, int role ) const override;
54
55 Qt::ItemFlags flags( const QModelIndex &index ) const override;
56
65 QList<QgsLayerTreeModelLegendNode *> layerLegendNodes( QgsLayerTreeLayer *nodeLayer, bool skipNodeEmbeddedInParent = false ) const SIP_SKIP;
66
71 void clearCachedData( QgsLayerTreeNode *node ) const;
72
73 signals:
74
80
81 private slots:
82
87 void forceRefresh();
88
89 private:
90
95 QgsLayoutItemLegend *mLayoutLegend = nullptr;
96
101 QString evaluateLayerExpressions( QgsLayerTreeLayer *nodeLayer ) const;
102
103};
104
105
106
111class CORE_EXPORT QgsLayoutItemLegend : public QgsLayoutItem
112{
113 Q_OBJECT
114
115 public:
116
121
127 static QgsLayoutItemLegend *create( QgsLayout *layout ) SIP_FACTORY;
128
129 int type() const override;
130 QIcon icon() const override;
131 QgsLayoutItem::Flags itemFlags() const override;
132 //Overridden to show legend title
133 QString displayName() const override;
134 bool requiresRasterization() const override;
135 bool containsAdvancedEffects() const override;
136
140 void adjustBoxSize();
141
148 void setResizeToContents( bool enabled );
149
154 bool resizeToContents() const;
155
159 QgsLegendModel *model();
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::DataDefinedProperty::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 void ensureModelIsInitialized();
638 std::unique_ptr< QgsLegendModel > mLegendModel;
639 std::unique_ptr< QgsLayerTree > mCustomLayerTree;
640 bool mDeferLegendModelInitialization = true;
641
642 QgsLegendSettings mSettings;
643
644 QString mTitle;
645 int mColumnCount = 1;
646
647 QString mMapUuid;
648 QgsLayoutItemMap *mMap = nullptr;
649
650 QList< QString > mFilterByMapUuids;
651 QList< QPointer< QgsLayoutItemMap >> mFilterByMapItems;
652
653 bool mLegendFilterByMap = false;
654 bool mLegendFilterByExpression = false;
655
657 bool mFilterOutAtlas = false;
658
660 bool mFilterAskedForUpdate = false;
662 void doUpdateFilterByMap();
663
664 bool mInAtlas = false;
665
667 bool mInitialMapScaleCalculated = false;
668
670 bool mForceResize = false;
671
673 bool mSizeToContents = true;
674
676 QString mThemeName;
677
679
680};
681
682#endif // QGSLAYOUTITEMLEGEND_H
683
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:32
A layout item subclass for map legends.
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:43
Base class for graphical items within a QgsLayout.
QFlags< Flag > Flags
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:49
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