QGIS API Documentation 3.43.0-Master (0bee5d6404c)
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 const QgsLegendModel *model() const SIP_SKIP;
167
173 void setAutoUpdateModel( bool autoUpdate );
174
180 bool autoUpdateModel() const;
181
186 void setLegendFilterByMapEnabled( bool enabled );
187
192 bool legendFilterByMapEnabled() const { return mLegendFilterByMap; }
193
199 void setLegendFilterOutAtlas( bool doFilter );
200
205 bool legendFilterOutAtlas() const;
206
211 void setTitle( const QString &title );
212
217 QString title() const;
218
223 Qt::AlignmentFlag titleAlignment() const;
224
229 void setTitleAlignment( Qt::AlignmentFlag alignment );
230
235
239 QgsLegendStyle style( Qgis::LegendComponent s ) const;
240
244 void setStyle( Qgis::LegendComponent component, const QgsLegendStyle &style );
245
252 Q_DECL_DEPRECATED QFont styleFont( Qgis::LegendComponent component ) const SIP_DEPRECATED;
253
260 Q_DECL_DEPRECATED void setStyleFont( Qgis::LegendComponent component, const QFont &font ) SIP_DEPRECATED;
261
265 void setStyleMargin( Qgis::LegendComponent component, double margin );
266
270 void setStyleMargin( Qgis::LegendComponent component, QgsLegendStyle::Side side, double margin );
271
278 Q_DECL_DEPRECATED double lineSpacing() const SIP_DEPRECATED;
279
286 Q_DECL_DEPRECATED void setLineSpacing( double spacing ) SIP_DEPRECATED;
287
292 double boxSpace() const;
293
298 void setBoxSpace( double space );
299
304 double columnSpace() const;
305
310 void setColumnSpace( double spacing );
311
318 Q_DECL_DEPRECATED QColor fontColor() const SIP_DEPRECATED;
319
326 Q_DECL_DEPRECATED void setFontColor( const QColor &color ) SIP_DEPRECATED;
327
332 double symbolWidth() const;
333
338 void setSymbolWidth( double width );
339
346 double maximumSymbolSize() const;
347
356 void setMaximumSymbolSize( double size );
357
364 double minimumSymbolSize() const;
365
374 void setMinimumSymbolSize( double size );
375
384 void setSymbolAlignment( Qt::AlignmentFlag alignment );
385
394 Qt::AlignmentFlag symbolAlignment() const;
395
400 double symbolHeight() const;
401
406 void setSymbolHeight( double height );
407
412 double wmsLegendWidth() const;
413
418 void setWmsLegendWidth( double width );
419
424 double wmsLegendHeight() const;
425
430 void setWmsLegendHeight( double height );
431
436 void setWrapString( const QString &string );
437
442 QString wrapString() const;
443
448 int columnCount() const;
449
454 void setColumnCount( int count );
455
461 bool splitLayer() const;
462
468 void setSplitLayer( bool enabled );
469
474 bool equalColumnWidth() const;
475
480 void setEqualColumnWidth( bool equalize );
481
488 bool drawRasterStroke() const;
489
497 void setDrawRasterStroke( bool enabled );
498
506 QColor rasterStrokeColor() const;
507
515 void setRasterStrokeColor( const QColor &color );
516
524 double rasterStrokeWidth() const;
525
533 void setRasterStrokeWidth( double width );
534
545 double autoWrapLinesAfter() const;
546
557 void setAutoWrapLinesAfter( double length );
558
564 void setLinkedMap( QgsLayoutItemMap *map );
565
570 QgsLayoutItemMap *linkedMap() const { return mMap; }
571
580 void setFilterByMapItems( const QList< QgsLayoutItemMap * > &maps );
581
590 QList< QgsLayoutItemMap * > filterByMapItems() const;
591
599 QString themeName() const;
600
604 void updateLegend();
605
609 void updateFilterByMap( bool redraw = true );
610
614 const QgsLegendSettings &legendSettings() const { return mSettings; }
615
616 void paint( QPainter *painter, const QStyleOptionGraphicsItem *itemStyle, QWidget *pWidget ) override;
617
618 void finalizeRestoreFromXml() override;
619
620 QgsExpressionContext createExpressionContext() const override;
621 ExportLayerBehavior exportLayerBehavior() const override;
622 bool accept( QgsStyleEntityVisitorInterface *visitor ) const override;
623 bool isRefreshing() const override;
624
625 public slots:
626
627 void refresh() override;
628 void invalidateCache() override;
629 void refreshDataDefinedProperty( QgsLayoutObject::DataDefinedProperty property = QgsLayoutObject::DataDefinedProperty::AllProperties ) override;
630
631 protected:
632 void draw( QgsLayoutItemRenderContext &context ) override;
633 bool writePropertiesToElement( QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context ) const override;
634 bool readPropertiesFromElement( const QDomElement &element, const QDomDocument &document, const QgsReadWriteContext &context ) override;
635
636 private slots:
637
639 void invalidateCurrentMap();
640
641 void updateFilterByMapAndRedraw();
642
643
645 void mapLayerStyleOverridesChanged();
647 void mapThemeChanged( const QString &theme );
648
650 void onAtlasEnded();
651 void onAtlasFeature();
652
653 void nodeCustomPropertyChanged( QgsLayerTreeNode *node, const QString &key );
654
656 void clearLegendCachedData();
657
658 private:
659 QgsLayoutItemLegend() = delete;
660
662 void setCustomLayerTree( QgsLayerTree *rootGroup );
663
664 void setupMapConnections( QgsLayoutItemMap *map, bool connect = true );
665
666 void setModelStyleOverrides( const QMap<QString, QString> &overrides );
667
668 void ensureModelIsInitialized() const;
669
670 QgsLegendRenderer createRenderer() const;
671
672 std::unique_ptr< QgsLegendModel > mLegendModel;
673 std::unique_ptr< QgsLayerTree > mCustomLayerTree;
674 bool mDeferLegendModelInitialization = true;
675
676 QgsLegendSettings mSettings;
677
678 QString mTitle;
679 int mColumnCount = 1;
680
681 QString mMapUuid;
682 QgsLayoutItemMap *mMap = nullptr;
683
684 QList< QString > mFilterByMapUuids;
685 QList< QPointer< QgsLayoutItemMap >> mFilterByMapItems;
686
687 bool mLegendFilterByMap = false;
688 bool mLegendFilterByExpression = false;
689
691 bool mFilterOutAtlas = false;
692
694 bool mFilterAskedForUpdate = false;
696 void doUpdateFilterByMap();
697
698 bool mInAtlas = false;
699
701 bool mInitialMapScaleCalculated = false;
702
704 bool mForceResize = false;
705
707 bool mSizeToContents = true;
708
710 QString mThemeName;
711
713
714};
715
716#endif // QGSLAYOUTITEMLEGEND_H
717
LegendComponent
Component of legends which can be styled.
Definition qgis.h:4406
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.
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)
Base class for nodes in a layer tree.
Namespace with helper functions for layer tree operations.
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.
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.
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.
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: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