QGIS API Documentation 3.99.0-Master (e9821da5c6b)
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"
30
32class QgsSymbol;
36
44class CORE_EXPORT QgsLegendModel : public QgsLayerTreeModel
45{
46 Q_OBJECT
47
48 public:
50 QgsLegendModel( QgsLayerTree *rootNode, QObject *parent SIP_TRANSFERTHIS = nullptr, QgsLayoutItemLegend *layout = nullptr );
51
54
55 QVariant data( const QModelIndex &index, int role ) const override;
56
57 Qt::ItemFlags flags( const QModelIndex &index ) const override;
58
67 QList<QgsLayerTreeModelLegendNode *> layerLegendNodes( QgsLayerTreeLayer *nodeLayer, bool skipNodeEmbeddedInParent = false ) const SIP_SKIP;
68
73 void clearCachedData( QgsLayerTreeNode *node ) const;
74
75 signals:
76
82
83 private slots:
84
89 void forceRefresh();
90
91 private:
92
97 QgsLayoutItemLegend *mLayoutLegend = nullptr;
98
103 QString evaluateLayerExpressions( QgsLayerTreeLayer *nodeLayer ) const;
104
105};
106
107#ifndef SIP_RUN
115{
116 Q_OBJECT
117 public:
118
123
128 void setIsDefaultLegend( bool isDefault );
129
137 void setFilterToCheckedLayers( bool filter );
138
139 private:
140
141 bool layerShown( QgsMapLayer *layer ) const override;
142
143 bool mIsDefaultLegend = true;
144 bool mFilterToCheckedLayers = false;
145
146};
147#endif
148
153class CORE_EXPORT QgsLayoutItemLegend : public QgsLayoutItem
154{
155 Q_OBJECT
156
157 public:
158
165
170
177
178 int type() const override;
179 QIcon icon() const override;
180 QgsLayoutItem::Flags itemFlags() const override;
181 //Overridden to show legend title
182 QString displayName() const override;
183 bool requiresRasterization() const override;
184 bool containsAdvancedEffects() const override;
185
189 void adjustBoxSize();
190
197 void setResizeToContents( bool enabled );
198
203 bool resizeToContents() const;
204
209
215 const QgsLegendModel *model() const SIP_SKIP;
216
223 Q_DECL_DEPRECATED void setAutoUpdateModel( bool autoUpdate ) SIP_DEPRECATED;
224
231 Q_DECL_DEPRECATED bool autoUpdateModel() const SIP_DEPRECATED;
232
242 void setSyncMode( Qgis::LegendSyncMode mode );
243
253 void resetManualLayers( Qgis::LegendSyncMode mode );
254
264 Qgis::LegendSyncMode syncMode() const;
265
270 void setLegendFilterByMapEnabled( bool enabled );
271
276 bool legendFilterByMapEnabled() const { return mLegendFilterByMap; }
277
283 void setLegendFilterOutAtlas( bool doFilter );
284
289 bool legendFilterOutAtlas() const;
290
295 void setTitle( const QString &title );
296
301 QString title() const;
302
307 Qt::AlignmentFlag titleAlignment() const;
308
313 void setTitleAlignment( Qt::AlignmentFlag alignment );
314
319
323 QgsLegendStyle style( Qgis::LegendComponent s ) const;
324
328 void setStyle( Qgis::LegendComponent component, const QgsLegendStyle &style );
329
336 Q_DECL_DEPRECATED QFont styleFont( Qgis::LegendComponent component ) const SIP_DEPRECATED;
337
344 Q_DECL_DEPRECATED void setStyleFont( Qgis::LegendComponent component, const QFont &font ) SIP_DEPRECATED;
345
349 void setStyleMargin( Qgis::LegendComponent component, double margin );
350
354 void setStyleMargin( Qgis::LegendComponent component, QgsLegendStyle::Side side, double margin );
355
362 Q_DECL_DEPRECATED double lineSpacing() const SIP_DEPRECATED;
363
370 Q_DECL_DEPRECATED void setLineSpacing( double spacing ) SIP_DEPRECATED;
371
376 double boxSpace() const;
377
382 void setBoxSpace( double space );
383
388 double columnSpace() const;
389
394 void setColumnSpace( double spacing );
395
402 Q_DECL_DEPRECATED QColor fontColor() const SIP_DEPRECATED;
403
410 Q_DECL_DEPRECATED void setFontColor( const QColor &color ) SIP_DEPRECATED;
411
416 double symbolWidth() const;
417
422 void setSymbolWidth( double width );
423
430 double maximumSymbolSize() const;
431
440 void setMaximumSymbolSize( double size );
441
448 double minimumSymbolSize() const;
449
458 void setMinimumSymbolSize( double size );
459
468 void setSymbolAlignment( Qt::AlignmentFlag alignment );
469
478 Qt::AlignmentFlag symbolAlignment() const;
479
484 double symbolHeight() const;
485
490 void setSymbolHeight( double height );
491
496 double wmsLegendWidth() const;
497
502 void setWmsLegendWidth( double width );
503
508 double wmsLegendHeight() const;
509
514 void setWmsLegendHeight( double height );
515
520 void setWrapString( const QString &string );
521
526 QString wrapString() const;
527
532 int columnCount() const;
533
538 void setColumnCount( int count );
539
545 bool splitLayer() const;
546
552 void setSplitLayer( bool enabled );
553
558 bool equalColumnWidth() const;
559
564 void setEqualColumnWidth( bool equalize );
565
572 bool drawRasterStroke() const;
573
581 void setDrawRasterStroke( bool enabled );
582
590 QColor rasterStrokeColor() const;
591
599 void setRasterStrokeColor( const QColor &color );
600
608 double rasterStrokeWidth() const;
609
617 void setRasterStrokeWidth( double width );
618
629 double autoWrapLinesAfter() const;
630
641 void setAutoWrapLinesAfter( double length );
642
648 void setLinkedMap( QgsLayoutItemMap *map );
649
654 QgsLayoutItemMap *linkedMap() const { return mMap; }
655
664 void setFilterByMapItems( const QList< QgsLayoutItemMap * > &maps );
665
674 QList< QgsLayoutItemMap * > filterByMapItems() const;
675
683 QString themeName() const;
684
688 void updateLegend();
689
693 void updateFilterByMap( bool redraw = true );
694
698 const QgsLegendSettings &legendSettings() const { return mSettings; }
699
700 void paint( QPainter *painter, const QStyleOptionGraphicsItem *itemStyle, QWidget *pWidget ) override;
701
702 void finalizeRestoreFromXml() override;
703
704 QgsExpressionContext createExpressionContext() const override;
705 ExportLayerBehavior exportLayerBehavior() const override;
706 bool accept( QgsStyleEntityVisitorInterface *visitor ) const override;
707 bool isRefreshing() const override;
708
709 public slots:
710
711 void refresh() override;
712 void invalidateCache() override;
713 void refreshDataDefinedProperty( QgsLayoutObject::DataDefinedProperty property = QgsLayoutObject::DataDefinedProperty::AllProperties ) override;
714
715 protected:
716 void draw( QgsLayoutItemRenderContext &context ) override;
717 bool writePropertiesToElement( QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context ) const override;
718 bool readPropertiesFromElement( const QDomElement &element, const QDomDocument &document, const QgsReadWriteContext &context ) override;
719
720 private slots:
721
723 void invalidateCurrentMap();
724
725 void updateFilterByMapAndRedraw();
726
727
729 void mapLayerStyleOverridesChanged();
731 void mapThemeChanged( const QString &theme );
732
734 void onAtlasEnded();
735 void onAtlasFeature();
736
737 void nodeCustomPropertyChanged( QgsLayerTreeNode *node, const QString &key );
739
741 void clearLegendCachedData();
742
743 private:
744 QgsLayoutItemLegend() = delete;
745
747 void setCustomLayerTree( QgsLayerTree *rootGroup );
748
749 void setupMapConnections( QgsLayoutItemMap *map, bool connect = true );
750
751 void setModelStyleOverrides( const QMap<QString, QString> &overrides );
752
753 void ensureModelIsInitialized() const;
754
760 bool requiresFilteringBecauseOfRendererSetting();
761
762 QgsLegendRenderer createRenderer() const;
763
764 std::unique_ptr< QgsLegendModel > mLegendModel;
765 std::unique_ptr< QgsLayerTree > mCustomLayerTree;
766
768
769 bool mDeferLegendModelInitialization = true;
770
771 QgsLegendSettings mSettings;
772
773 QString mTitle;
774 int mColumnCount = 1;
775
776 QString mMapUuid;
777 QgsLayoutItemMap *mMap = nullptr;
778
779 QList< QString > mFilterByMapUuids;
780 QList< QPointer< QgsLayoutItemMap >> mFilterByMapItems;
781
782 bool mLegendFilterByMap = false;
783 bool mLegendFilterByExpression = false;
784
786 bool mFilterOutAtlas = false;
787
789 bool mFilterAskedForUpdate = false;
791 void doUpdateFilterByMap();
792
793 bool mInAtlas = false;
794
796 bool mInitialMapScaleCalculated = false;
797
799 bool mForceResize = false;
800
802 bool mSizeToContents = true;
803
805 QString mThemeName;
806
808
809};
810
811#endif // QGSLAYOUTITEMLEGEND_H
812
Provides global constants and enumerations for use throughout the application.
Definition qgis.h:59
LegendComponent
Component of legends which can be styled.
Definition qgis.h:4658
LegendSyncMode
Legend synchronization mode.
Definition qgis.h:4676
@ AllProjectLayers
Synchronize to all project layers.
Definition qgis.h:4677
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
void nodeVisibilityChanged(QgsLayerTreeNode *node)
Base class for nodes in a layer tree.
Namespace with helper functions for layer tree operations.
A layout item subclass for map legends.
Q_DECL_DEPRECATED 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.
Qgis::LegendSyncMode syncMode() const
Returns the legend's synchronization mode.
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...
static const QgsSettingsEntryEnumFlag< Qgis::LegendSyncMode > * settingDefaultLegendSyncMode
Settings entry - Layout legend synchronization mode.
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 setSyncMode(Qgis::LegendSyncMode mode)
Sets the legend's synchronization mode.
Q_DECL_DEPRECATED 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...
void resetManualLayers(Qgis::LegendSyncMode mode)
Resets the current legend manual configuration, including layer set and settings.
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...
void setFilterToCheckedLayers(bool filter)
Sets whether only checked layers should be shown.
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:83
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