QGIS API Documentation 4.0.0-Norrköping (1ddcee3d0e4)
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:
96 QgsLayoutItemLegend *mLayoutLegend = nullptr;
97
102 QString evaluateLayerExpressions( QgsLayerTreeLayer *nodeLayer ) const;
103};
104
105#ifndef SIP_RUN
113{
114 Q_OBJECT
115 public:
120
125 void setIsDefaultLegend( bool isDefault );
126
134 void setFilterToCheckedLayers( bool filter );
135
136 private:
137 bool layerShown( QgsMapLayer *layer ) const override;
138
139 bool mIsDefaultLegend = true;
140 bool mFilterToCheckedLayers = false;
141};
142#endif
143
148class CORE_EXPORT QgsLayoutItemLegend : public QgsLayoutItem
149{
150 Q_OBJECT
151
152 public:
159
164
171
172 int type() const override;
173 QIcon icon() const override;
174 QgsLayoutItem::Flags itemFlags() const override;
175 //Overridden to show legend title
176 QString displayName() const override;
177 bool requiresRasterization() const override;
178 bool containsAdvancedEffects() const override;
179
183 void adjustBoxSize();
184
191 void setResizeToContents( bool enabled );
192
197 bool resizeToContents() const;
198
203
209 const QgsLegendModel *model() const SIP_SKIP;
210
217 Q_DECL_DEPRECATED void setAutoUpdateModel( bool autoUpdate ) SIP_DEPRECATED;
218
225 Q_DECL_DEPRECATED bool autoUpdateModel() const SIP_DEPRECATED;
226
236 void setSyncMode( Qgis::LegendSyncMode mode );
237
247 void resetManualLayers( Qgis::LegendSyncMode mode );
248
258 Qgis::LegendSyncMode syncMode() const;
259
264 void setLegendFilterByMapEnabled( bool enabled );
265
270 bool legendFilterByMapEnabled() const { return mLegendFilterByMap; }
271
277 void setLegendFilterOutAtlas( bool doFilter );
278
283 bool legendFilterOutAtlas() const;
284
289 void setTitle( const QString &title );
290
295 QString title() const;
296
301 Qt::AlignmentFlag titleAlignment() const;
302
307 void setTitleAlignment( Qt::AlignmentFlag alignment );
308
313
317 QgsLegendStyle style( Qgis::LegendComponent s ) const;
318
322 void setStyle( Qgis::LegendComponent component, const QgsLegendStyle &style );
323
330 Q_DECL_DEPRECATED QFont styleFont( Qgis::LegendComponent component ) const SIP_DEPRECATED;
331
338 Q_DECL_DEPRECATED void setStyleFont( Qgis::LegendComponent component, const QFont &font ) SIP_DEPRECATED;
339
343 void setStyleMargin( Qgis::LegendComponent component, double margin );
344
348 void setStyleMargin( Qgis::LegendComponent component, QgsLegendStyle::Side side, double margin );
349
356 Q_DECL_DEPRECATED double lineSpacing() const SIP_DEPRECATED;
357
364 Q_DECL_DEPRECATED void setLineSpacing( double spacing ) SIP_DEPRECATED;
365
370 double boxSpace() const;
371
376 void setBoxSpace( double space );
377
382 double columnSpace() const;
383
388 void setColumnSpace( double spacing );
389
396 Q_DECL_DEPRECATED QColor fontColor() const SIP_DEPRECATED;
397
404 Q_DECL_DEPRECATED void setFontColor( const QColor &color ) SIP_DEPRECATED;
405
410 double symbolWidth() const;
411
416 void setSymbolWidth( double width );
417
424 double maximumSymbolSize() const;
425
434 void setMaximumSymbolSize( double size );
435
442 double minimumSymbolSize() const;
443
452 void setMinimumSymbolSize( double size );
453
462 void setSymbolAlignment( Qt::AlignmentFlag alignment );
463
472 Qt::AlignmentFlag symbolAlignment() const;
473
478 double symbolHeight() const;
479
484 void setSymbolHeight( double height );
485
490 double wmsLegendWidth() const;
491
496 void setWmsLegendWidth( double width );
497
502 double wmsLegendHeight() const;
503
508 void setWmsLegendHeight( double height );
509
514 void setWrapString( const QString &string );
515
520 QString wrapString() const;
521
526 int columnCount() const;
527
532 void setColumnCount( int count );
533
539 bool splitLayer() const;
540
546 void setSplitLayer( bool enabled );
547
552 bool equalColumnWidth() const;
553
558 void setEqualColumnWidth( bool equalize );
559
566 bool drawRasterStroke() const;
567
575 void setDrawRasterStroke( bool enabled );
576
584 QColor rasterStrokeColor() const;
585
593 void setRasterStrokeColor( const QColor &color );
594
602 double rasterStrokeWidth() const;
603
611 void setRasterStrokeWidth( double width );
612
623 double autoWrapLinesAfter() const;
624
635 void setAutoWrapLinesAfter( double length );
636
642 void setLinkedMap( QgsLayoutItemMap *map );
643
648 QgsLayoutItemMap *linkedMap() const { return mMap; }
649
658 void setFilterByMapItems( const QList< QgsLayoutItemMap * > &maps );
659
668 QList< QgsLayoutItemMap * > filterByMapItems() const;
669
677 QString themeName() const;
678
682 void updateLegend();
683
687 void updateFilterByMap( bool redraw = true );
688
692 const QgsLegendSettings &legendSettings() const { return mSettings; }
693
694 void paint( QPainter *painter, const QStyleOptionGraphicsItem *itemStyle, QWidget *pWidget ) override;
695
696 void finalizeRestoreFromXml() override;
697
698 QgsExpressionContext createExpressionContext() const override;
699 ExportLayerBehavior exportLayerBehavior() const override;
700 bool accept( QgsStyleEntityVisitorInterface *visitor ) const override;
701 bool isRefreshing() const override;
702
703 public slots:
704
705 void refresh() override;
706 void invalidateCache() override;
707 void refreshDataDefinedProperty( QgsLayoutObject::DataDefinedProperty property = QgsLayoutObject::DataDefinedProperty::AllProperties ) override;
708
709 protected:
710 void draw( QgsLayoutItemRenderContext &context ) override;
711 bool writePropertiesToElement( QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context ) const override;
712 bool readPropertiesFromElement( const QDomElement &element, const QDomDocument &document, const QgsReadWriteContext &context ) override;
713
714 private slots:
715
717 void invalidateCurrentMap();
718
719 void updateFilterByMapAndRedraw();
720
721
723 void mapLayerStyleOverridesChanged();
725 void mapThemeChanged( const QString &theme );
726
728 void onAtlasEnded();
729 void onAtlasFeature();
730
731 void nodeCustomPropertyChanged( QgsLayerTreeNode *node, const QString &key );
733
735 void clearLegendCachedData();
736
737 private:
738 QgsLayoutItemLegend() = delete;
739
741 void setCustomLayerTree( QgsLayerTree *rootGroup );
742
743 void setupMapConnections( QgsLayoutItemMap *map, bool connect = true );
744
745 void setModelStyleOverrides( const QMap<QString, QString> &overrides );
746
747 void ensureModelIsInitialized() const;
748
754 bool requiresFilteringBecauseOfRendererSetting();
755
756 QgsLegendRenderer createRenderer() const;
757
758 std::unique_ptr< QgsLegendModel > mLegendModel;
759 std::unique_ptr< QgsLayerTree > mCustomLayerTree;
760
762
763 bool mDeferLegendModelInitialization = true;
764
765 QgsLegendSettings mSettings;
766
767 QString mTitle;
768 int mColumnCount = 1;
769
770 QString mMapUuid;
771 QgsLayoutItemMap *mMap = nullptr;
772
773 QList< QString > mFilterByMapUuids;
774 QList< QPointer< QgsLayoutItemMap >> mFilterByMapItems;
775
776 bool mLegendFilterByMap = false;
777 bool mLegendFilterByExpression = false;
778
780 bool mFilterOutAtlas = false;
781
783 bool mFilterAskedForUpdate = false;
785 void doUpdateFilterByMap();
786
787 bool mInAtlas = false;
788
790 bool mInitialMapScaleCalculated = false;
791
793 bool mForceResize = false;
794
796 bool mSizeToContents = true;
797
799 QString mThemeName;
800
802};
803
804#endif // QGSLAYOUTITEMLEGEND_H
Provides global constants and enumerations for use throughout the application.
Definition qgis.h:62
LegendComponent
Component of legends which can be styled.
Definition qgis.h:4719
LegendSyncMode
Legend synchronization mode.
Definition qgis.h:4737
@ AllProjectLayers
Synchronize to all project layers.
Definition qgis.h:4738
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:227
#define SIP_DEPRECATED
Definition qgis_sip.h:113
#define SIP_TRANSFERTHIS
Definition qgis_sip.h:52
#define SIP_SKIP
Definition qgis_sip.h:133
#define SIP_FACTORY
Definition qgis_sip.h:83