QGIS API Documentation 3.30.0-'s-Hertogenbosch (f186b8efe0)
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 "qgslayertreegroup.h"
28
30class QgsSymbol;
34
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
107
113class CORE_EXPORT QgsLayoutItemLegend : public QgsLayoutItem
114{
115 Q_OBJECT
116
117 public:
118
123
129 static QgsLayoutItemLegend *create( QgsLayout *layout ) SIP_FACTORY;
130
131 int type() const override;
132 QIcon icon() const override;
133 QgsLayoutItem::Flags itemFlags() const override;
134 //Overridden to show legend title
135 QString displayName() const override;
136
140 void adjustBoxSize();
141
148 void setResizeToContents( bool enabled );
149
154 bool resizeToContents() const;
155
159 QgsLegendModel *model() { return mLegendModel.get(); }
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
532 void setLinkedMap( QgsLayoutItemMap *map );
533
538 QgsLayoutItemMap *linkedMap() const { return mMap; }
539
547 QString themeName() const;
548
552 void updateLegend();
553
557 void updateFilterByMap( bool redraw = true );
558
562 const QgsLegendSettings &legendSettings() const { return mSettings; }
563
564 void paint( QPainter *painter, const QStyleOptionGraphicsItem *itemStyle, QWidget *pWidget ) override;
565
566 void finalizeRestoreFromXml() override;
567
568 QgsExpressionContext createExpressionContext() const override;
569 ExportLayerBehavior exportLayerBehavior() const override;
570 bool accept( QgsStyleEntityVisitorInterface *visitor ) const override;
571
572 public slots:
573
574 void refresh() override;
575 void refreshDataDefinedProperty( QgsLayoutObject::DataDefinedProperty property = QgsLayoutObject::AllProperties ) override;
576
577 protected:
578 void draw( QgsLayoutItemRenderContext &context ) override;
579 bool writePropertiesToElement( QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context ) const override;
580 bool readPropertiesFromElement( const QDomElement &element, const QDomDocument &document, const QgsReadWriteContext &context ) override;
581
582 private slots:
583
585 void invalidateCurrentMap();
586
587 void updateFilterByMapAndRedraw();
588
589
591 void mapLayerStyleOverridesChanged();
593 void mapThemeChanged( const QString &theme );
594
596 void onAtlasEnded();
597 void onAtlasFeature();
598
599 void nodeCustomPropertyChanged( QgsLayerTreeNode *node, const QString &key );
600
602 void clearLegendCachedData();
603
604 private:
605 QgsLayoutItemLegend() = delete;
606
608 void setCustomLayerTree( QgsLayerTree *rootGroup );
609
610 void setupMapConnections( QgsLayoutItemMap *map, bool connect = true );
611
612 void setModelStyleOverrides( const QMap<QString, QString> &overrides );
613
614 std::unique_ptr< QgsLegendModel > mLegendModel;
615 std::unique_ptr< QgsLayerTreeGroup > mCustomLayerTree;
616
617 QgsLegendSettings mSettings;
618
619 QString mTitle;
620 int mColumnCount = 1;
621
622 QString mMapUuid;
623 QgsLayoutItemMap *mMap = nullptr;
624
625 bool mLegendFilterByMap = false;
626 bool mLegendFilterByExpression = false;
627
629 bool mFilterOutAtlas = false;
630
632 bool mFilterAskedForUpdate = false;
634 void doUpdateFilterByMap();
635
636 bool mInAtlas = false;
637
639 bool mInitialMapScaleCalculated = false;
640
642 bool mForceResize = false;
643
645 bool mSizeToContents = true;
646
648 QString mThemeName;
649
651
652};
653
654#endif // QGSLAYOUTITEMLEGEND_H
655
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:33
A layout item subclass for map legends.
QgsLegendModel * model()
Returns the legend model.
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:44
Base class for graphical items within a QgsLayout.
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:50
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:93
#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