QGIS API Documentation 3.40.0-Bratislava (b56115d8743)
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"
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( QgsLegendStyle::Style s ) const;
240
244 void setStyle( QgsLegendStyle::Style component, const QgsLegendStyle &style );
245
252 Q_DECL_DEPRECATED QFont styleFont( QgsLegendStyle::Style component ) const SIP_DEPRECATED;
253
260 Q_DECL_DEPRECATED void setStyleFont( QgsLegendStyle::Style component, const QFont &font ) SIP_DEPRECATED;
261
265 void setStyleMargin( QgsLegendStyle::Style component, double margin );
266
270 void setStyleMargin( QgsLegendStyle::Style 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
540 void setLinkedMap( QgsLayoutItemMap *map );
541
546 QgsLayoutItemMap *linkedMap() const { return mMap; }
547
556 void setFilterByMapItems( const QList< QgsLayoutItemMap * > &maps );
557
566 QList< QgsLayoutItemMap * > filterByMapItems() const;
567
575 QString themeName() const;
576
580 void updateLegend();
581
585 void updateFilterByMap( bool redraw = true );
586
590 const QgsLegendSettings &legendSettings() const { return mSettings; }
591
592 void paint( QPainter *painter, const QStyleOptionGraphicsItem *itemStyle, QWidget *pWidget ) override;
593
594 void finalizeRestoreFromXml() override;
595
596 QgsExpressionContext createExpressionContext() const override;
597 ExportLayerBehavior exportLayerBehavior() const override;
598 bool accept( QgsStyleEntityVisitorInterface *visitor ) const override;
599 bool isRefreshing() const override;
600
601 public slots:
602
603 void refresh() override;
604 void invalidateCache() override;
605 void refreshDataDefinedProperty( QgsLayoutObject::DataDefinedProperty property = QgsLayoutObject::DataDefinedProperty::AllProperties ) override;
606
607 protected:
608 void draw( QgsLayoutItemRenderContext &context ) override;
609 bool writePropertiesToElement( QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context ) const override;
610 bool readPropertiesFromElement( const QDomElement &element, const QDomDocument &document, const QgsReadWriteContext &context ) override;
611
612 private slots:
613
615 void invalidateCurrentMap();
616
617 void updateFilterByMapAndRedraw();
618
619
621 void mapLayerStyleOverridesChanged();
623 void mapThemeChanged( const QString &theme );
624
626 void onAtlasEnded();
627 void onAtlasFeature();
628
629 void nodeCustomPropertyChanged( QgsLayerTreeNode *node, const QString &key );
630
632 void clearLegendCachedData();
633
634 private:
635 QgsLayoutItemLegend() = delete;
636
638 void setCustomLayerTree( QgsLayerTree *rootGroup );
639
640 void setupMapConnections( QgsLayoutItemMap *map, bool connect = true );
641
642 void setModelStyleOverrides( const QMap<QString, QString> &overrides );
643
644 void ensureModelIsInitialized() const;
645
646 QgsLegendRenderer createRenderer() const;
647
648 std::unique_ptr< QgsLegendModel > mLegendModel;
649 std::unique_ptr< QgsLayerTree > mCustomLayerTree;
650 bool mDeferLegendModelInitialization = true;
651
652 QgsLegendSettings mSettings;
653
654 QString mTitle;
655 int mColumnCount = 1;
656
657 QString mMapUuid;
658 QgsLayoutItemMap *mMap = nullptr;
659
660 QList< QString > mFilterByMapUuids;
661 QList< QPointer< QgsLayoutItemMap >> mFilterByMapItems;
662
663 bool mLegendFilterByMap = false;
664 bool mLegendFilterByExpression = false;
665
667 bool mFilterOutAtlas = false;
668
670 bool mFilterAskedForUpdate = false;
672 void doUpdateFilterByMap();
673
674 bool mInAtlas = false;
675
677 bool mInitialMapScaleCalculated = false;
678
680 bool mForceResize = false;
681
683 bool mSizeToContents = true;
684
686 QString mThemeName;
687
689
690};
691
692#endif // QGSLAYOUTITEMLEGEND_H
693
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.
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.
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: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