QGIS API Documentation 3.99.0-Master (2fe06baccd8)
Loading...
Searching...
No Matches
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"
24#include "qgslayertree.h"
26#include "qgslayertreemodel.h"
27#include "qgslayoutitem.h"
28#include "qgslegendsettings.h"
29
31class QgsSymbol;
35
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#ifndef SIP_RUN
114{
115 Q_OBJECT
116 public:
117
122
127 void setIsDefaultLegend( bool isDefault );
128
129 private:
130
131 bool layerShown( QgsMapLayer *layer ) const override;
132
133 bool mIsDefaultLegend = true;
134
135};
136#endif
137
142class CORE_EXPORT QgsLayoutItemLegend : public QgsLayoutItem
143{
144 Q_OBJECT
145
146 public:
147
152
159
160 int type() const override;
161 QIcon icon() const override;
162 QgsLayoutItem::Flags itemFlags() const override;
163 //Overridden to show legend title
164 QString displayName() const override;
165 bool requiresRasterization() const override;
166 bool containsAdvancedEffects() const override;
167
171 void adjustBoxSize();
172
179 void setResizeToContents( bool enabled );
180
185 bool resizeToContents() const;
186
191
197 const QgsLegendModel *model() const SIP_SKIP;
198
204 void setAutoUpdateModel( bool autoUpdate );
205
211 bool autoUpdateModel() const;
212
217 void setLegendFilterByMapEnabled( bool enabled );
218
223 bool legendFilterByMapEnabled() const { return mLegendFilterByMap; }
224
230 void setLegendFilterOutAtlas( bool doFilter );
231
236 bool legendFilterOutAtlas() const;
237
242 void setTitle( const QString &title );
243
248 QString title() const;
249
254 Qt::AlignmentFlag titleAlignment() const;
255
260 void setTitleAlignment( Qt::AlignmentFlag alignment );
261
266
270 QgsLegendStyle style( Qgis::LegendComponent s ) const;
271
275 void setStyle( Qgis::LegendComponent component, const QgsLegendStyle &style );
276
283 Q_DECL_DEPRECATED QFont styleFont( Qgis::LegendComponent component ) const SIP_DEPRECATED;
284
291 Q_DECL_DEPRECATED void setStyleFont( Qgis::LegendComponent component, const QFont &font ) SIP_DEPRECATED;
292
296 void setStyleMargin( Qgis::LegendComponent component, double margin );
297
301 void setStyleMargin( Qgis::LegendComponent component, QgsLegendStyle::Side side, double margin );
302
309 Q_DECL_DEPRECATED double lineSpacing() const SIP_DEPRECATED;
310
317 Q_DECL_DEPRECATED void setLineSpacing( double spacing ) SIP_DEPRECATED;
318
323 double boxSpace() const;
324
329 void setBoxSpace( double space );
330
335 double columnSpace() const;
336
341 void setColumnSpace( double spacing );
342
349 Q_DECL_DEPRECATED QColor fontColor() const SIP_DEPRECATED;
350
357 Q_DECL_DEPRECATED void setFontColor( const QColor &color ) SIP_DEPRECATED;
358
363 double symbolWidth() const;
364
369 void setSymbolWidth( double width );
370
377 double maximumSymbolSize() const;
378
387 void setMaximumSymbolSize( double size );
388
395 double minimumSymbolSize() const;
396
405 void setMinimumSymbolSize( double size );
406
415 void setSymbolAlignment( Qt::AlignmentFlag alignment );
416
425 Qt::AlignmentFlag symbolAlignment() const;
426
431 double symbolHeight() const;
432
437 void setSymbolHeight( double height );
438
443 double wmsLegendWidth() const;
444
449 void setWmsLegendWidth( double width );
450
455 double wmsLegendHeight() const;
456
461 void setWmsLegendHeight( double height );
462
467 void setWrapString( const QString &string );
468
473 QString wrapString() const;
474
479 int columnCount() const;
480
485 void setColumnCount( int count );
486
492 bool splitLayer() const;
493
499 void setSplitLayer( bool enabled );
500
505 bool equalColumnWidth() const;
506
511 void setEqualColumnWidth( bool equalize );
512
519 bool drawRasterStroke() const;
520
528 void setDrawRasterStroke( bool enabled );
529
537 QColor rasterStrokeColor() const;
538
546 void setRasterStrokeColor( const QColor &color );
547
555 double rasterStrokeWidth() const;
556
564 void setRasterStrokeWidth( double width );
565
576 double autoWrapLinesAfter() const;
577
588 void setAutoWrapLinesAfter( double length );
589
595 void setLinkedMap( QgsLayoutItemMap *map );
596
601 QgsLayoutItemMap *linkedMap() const { return mMap; }
602
611 void setFilterByMapItems( const QList< QgsLayoutItemMap * > &maps );
612
621 QList< QgsLayoutItemMap * > filterByMapItems() const;
622
630 QString themeName() const;
631
635 void updateLegend();
636
640 void updateFilterByMap( bool redraw = true );
641
645 const QgsLegendSettings &legendSettings() const { return mSettings; }
646
647 void paint( QPainter *painter, const QStyleOptionGraphicsItem *itemStyle, QWidget *pWidget ) override;
648
649 void finalizeRestoreFromXml() override;
650
651 QgsExpressionContext createExpressionContext() const override;
652 ExportLayerBehavior exportLayerBehavior() const override;
653 bool accept( QgsStyleEntityVisitorInterface *visitor ) const override;
654 bool isRefreshing() const override;
655
656 public slots:
657
658 void refresh() override;
659 void invalidateCache() override;
660 void refreshDataDefinedProperty( QgsLayoutObject::DataDefinedProperty property = QgsLayoutObject::DataDefinedProperty::AllProperties ) override;
661
662 protected:
663 void draw( QgsLayoutItemRenderContext &context ) override;
664 bool writePropertiesToElement( QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context ) const override;
665 bool readPropertiesFromElement( const QDomElement &element, const QDomDocument &document, const QgsReadWriteContext &context ) override;
666
667 private slots:
668
670 void invalidateCurrentMap();
671
672 void updateFilterByMapAndRedraw();
673
674
676 void mapLayerStyleOverridesChanged();
678 void mapThemeChanged( const QString &theme );
679
681 void onAtlasEnded();
682 void onAtlasFeature();
683
684 void nodeCustomPropertyChanged( QgsLayerTreeNode *node, const QString &key );
685
687 void clearLegendCachedData();
688
689 private:
690 QgsLayoutItemLegend() = delete;
691
693 void setCustomLayerTree( QgsLayerTree *rootGroup );
694
695 void setupMapConnections( QgsLayoutItemMap *map, bool connect = true );
696
697 void setModelStyleOverrides( const QMap<QString, QString> &overrides );
698
699 void ensureModelIsInitialized() const;
700
701 QgsLegendRenderer createRenderer() const;
702
703 std::unique_ptr< QgsLegendModel > mLegendModel;
704 std::unique_ptr< QgsLayerTree > mCustomLayerTree;
705 bool mDeferLegendModelInitialization = true;
706
707 QgsLegendSettings mSettings;
708
709 QString mTitle;
710 int mColumnCount = 1;
711
712 QString mMapUuid;
713 QgsLayoutItemMap *mMap = nullptr;
714
715 QList< QString > mFilterByMapUuids;
716 QList< QPointer< QgsLayoutItemMap >> mFilterByMapItems;
717
718 bool mLegendFilterByMap = false;
719 bool mLegendFilterByExpression = false;
720
722 bool mFilterOutAtlas = false;
723
725 bool mFilterAskedForUpdate = false;
727 void doUpdateFilterByMap();
728
729 bool mInAtlas = false;
730
732 bool mInitialMapScaleCalculated = false;
733
735 bool mForceResize = false;
736
738 bool mSizeToContents = true;
739
741 QString mThemeName;
742
744
745};
746
747#endif // QGSLAYOUTITEMLEGEND_H
748
LegendComponent
Component of legends which can be styled.
Definition qgis.h:4575
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
QModelIndex parent(const QModelIndex &child) const override
QgsLayerTreeFilterProxyModel(QObject *parent=nullptr)
Constructor.
Layer tree node points to a map layer.
A model representing the layer tree, including layers and groups of layers.
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)
QgsLayerTreeModel(QgsLayerTree *rootNode, QObject *parent=nullptr)
Construct a new tree model with given layer tree (root node must not be nullptr).
QModelIndex parent(const QModelIndex &child) const override
QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const override
Base class for nodes in a layer tree.
Namespace with helper functions for layer tree operations.
A layout item subclass for map legends.
bool autoUpdateModel() const
Returns whether the legend content should auto update to reflect changes in the project's layer tree.
QgsLegendModel * model()
Returns the legend model.
void adjustBoxSize()
Sets the legend's item bounds to fit the whole legend content.
QgsLayoutItemMap * linkedMap() const
Returns the associated map.
static QgsLayoutItemLegend * create(QgsLayout *layout)
Returns a new legend item for the specified layout.
bool requiresRasterization() const override
Returns true if the item is drawn in such a way that forces the whole layout to be rasterized when ex...
const QgsLegendSettings & legendSettings() const
Returns the legend's renderer settings object.
bool resizeToContents() const
Returns whether the legend should automatically resize to fit its contents.
void setResizeToContents(bool enabled)
Sets whether the legend should automatically resize to fit its contents.
QgsLayoutItemLegend(QgsLayout *layout)
Constructor for QgsLayoutItemLegend, with the specified parent layout.
QString displayName() const override
Gets item display name.
int type() const override
QgsLayoutItem::Flags itemFlags() const override
Returns the item's flags, which indicate how the item behaves.
void setLegendFilterByMapEnabled(bool enabled)
Set whether legend items should be filtered to show just the ones visible in the associated map.
friend class QgsCompositionConverter
void setAutoUpdateModel(bool autoUpdate)
Sets whether the legend content should auto update to reflect changes in the project's layer tree.
QIcon icon() const override
Returns the item's icon.
bool containsAdvancedEffects() const override
Returns true if the item contains contents with blend modes or transparency effects which can only be...
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.
friend class QgsLayout
QgsLayoutItem(QgsLayout *layout, bool manageZValue=true)
Constructor for QgsLayoutItem, with the specified parent layout.
friend class QgsLayoutItemMap
QFlags< Flag > Flags
const QgsLayout * layout() const
Returns the layout the object is attached to.
DataDefinedProperty
Data defined properties for different item types.
@ AllProperties
All properties for item.
void setIsDefaultLegend(bool isDefault)
Sets whether the legend is showing the default legend for a project (as opposed to a customized legen...
QgsLegendFilterProxyModel(QObject *parent=nullptr)
Constructor for QgsLegendFilterProxyModel, with the specified parent object.
Item model implementation based on layer tree model for layout legend.
void clearCachedData(QgsLayerTreeNode *node) const
Clears any previously cached data for the specified node.
void refreshLegend()
Emitted to refresh the legend.
QgsLegendModel(QgsLayerTree *rootNode, QObject *parent=nullptr, QgsLayoutItemLegend *layout=nullptr)
Construct the model based on the given layer tree.
Handles automatic layout and rendering of legends.
Stores the appearance and layout settings for legend drawing with QgsLegendRenderer.
Contains detailed styling information relating to how a layout legend should be rendered.
Side
Margin sides.
Base class for all map layer types.
Definition qgsmaplayer.h:80
A container for the context for various read/write operations on objects.
An interface for classes which can visit style entity (e.g.
Abstract base class for all rendered symbols.
Definition qgssymbol.h:231
#define SIP_DEPRECATED
Definition qgis_sip.h:114
#define SIP_TRANSFERTHIS
Definition qgis_sip.h:53
#define SIP_SKIP
Definition qgis_sip.h:134
#define SIP_FACTORY
Definition qgis_sip.h:84