QGIS API Documentation 3.34.0-Prizren (ffbdd678812)
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 "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 bool requiresRasterization() const override;
137 bool containsAdvancedEffects() const override;
138
142 void adjustBoxSize();
143
150 void setResizeToContents( bool enabled );
151
156 bool resizeToContents() const;
157
161 QgsLegendModel *model() { return mLegendModel.get(); }
162
168 void setAutoUpdateModel( bool autoUpdate );
169
175 bool autoUpdateModel() const;
176
181 void setLegendFilterByMapEnabled( bool enabled );
182
187 bool legendFilterByMapEnabled() const { return mLegendFilterByMap; }
188
194 void setLegendFilterOutAtlas( bool doFilter );
195
200 bool legendFilterOutAtlas() const;
201
206 void setTitle( const QString &title );
207
212 QString title() const;
213
218 Qt::AlignmentFlag titleAlignment() const;
219
224 void setTitleAlignment( Qt::AlignmentFlag alignment );
225
230
234 QgsLegendStyle style( QgsLegendStyle::Style s ) const;
235
239 void setStyle( QgsLegendStyle::Style component, const QgsLegendStyle &style );
240
247 Q_DECL_DEPRECATED QFont styleFont( QgsLegendStyle::Style component ) const SIP_DEPRECATED;
248
255 Q_DECL_DEPRECATED void setStyleFont( QgsLegendStyle::Style component, const QFont &font ) SIP_DEPRECATED;
256
260 void setStyleMargin( QgsLegendStyle::Style component, double margin );
261
265 void setStyleMargin( QgsLegendStyle::Style component, QgsLegendStyle::Side side, double margin );
266
273 Q_DECL_DEPRECATED double lineSpacing() const SIP_DEPRECATED;
274
281 Q_DECL_DEPRECATED void setLineSpacing( double spacing ) SIP_DEPRECATED;
282
287 double boxSpace() const;
288
293 void setBoxSpace( double space );
294
299 double columnSpace() const;
300
305 void setColumnSpace( double spacing );
306
313 Q_DECL_DEPRECATED QColor fontColor() const SIP_DEPRECATED;
314
321 Q_DECL_DEPRECATED void setFontColor( const QColor &color ) SIP_DEPRECATED;
322
327 double symbolWidth() const;
328
333 void setSymbolWidth( double width );
334
341 double maximumSymbolSize() const;
342
351 void setMaximumSymbolSize( double size );
352
359 double minimumSymbolSize() const;
360
369 void setMinimumSymbolSize( double size );
370
379 void setSymbolAlignment( Qt::AlignmentFlag alignment );
380
389 Qt::AlignmentFlag symbolAlignment() const;
390
395 double symbolHeight() const;
396
401 void setSymbolHeight( double height );
402
407 double wmsLegendWidth() const;
408
413 void setWmsLegendWidth( double width );
414
419 double wmsLegendHeight() const;
420
425 void setWmsLegendHeight( double height );
426
431 void setWrapString( const QString &string );
432
437 QString wrapString() const;
438
443 int columnCount() const;
444
449 void setColumnCount( int count );
450
456 bool splitLayer() const;
457
463 void setSplitLayer( bool enabled );
464
469 bool equalColumnWidth() const;
470
475 void setEqualColumnWidth( bool equalize );
476
483 bool drawRasterStroke() const;
484
492 void setDrawRasterStroke( bool enabled );
493
501 QColor rasterStrokeColor() const;
502
510 void setRasterStrokeColor( const QColor &color );
511
519 double rasterStrokeWidth() const;
520
528 void setRasterStrokeWidth( double width );
529
535 void setLinkedMap( QgsLayoutItemMap *map );
536
541 QgsLayoutItemMap *linkedMap() const { return mMap; }
542
551 void setFilterByMapItems( const QList< QgsLayoutItemMap * > &maps );
552
561 QList< QgsLayoutItemMap * > filterByMapItems() const;
562
570 QString themeName() const;
571
575 void updateLegend();
576
580 void updateFilterByMap( bool redraw = true );
581
585 const QgsLegendSettings &legendSettings() const { return mSettings; }
586
587 void paint( QPainter *painter, const QStyleOptionGraphicsItem *itemStyle, QWidget *pWidget ) override;
588
589 void finalizeRestoreFromXml() override;
590
591 QgsExpressionContext createExpressionContext() const override;
592 ExportLayerBehavior exportLayerBehavior() const override;
593 bool accept( QgsStyleEntityVisitorInterface *visitor ) const override;
594 bool isRefreshing() const override;
595
596 public slots:
597
598 void refresh() override;
599 void invalidateCache() override;
600 void refreshDataDefinedProperty( QgsLayoutObject::DataDefinedProperty property = QgsLayoutObject::AllProperties ) override;
601
602 protected:
603 void draw( QgsLayoutItemRenderContext &context ) override;
604 bool writePropertiesToElement( QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context ) const override;
605 bool readPropertiesFromElement( const QDomElement &element, const QDomDocument &document, const QgsReadWriteContext &context ) override;
606
607 private slots:
608
610 void invalidateCurrentMap();
611
612 void updateFilterByMapAndRedraw();
613
614
616 void mapLayerStyleOverridesChanged();
618 void mapThemeChanged( const QString &theme );
619
621 void onAtlasEnded();
622 void onAtlasFeature();
623
624 void nodeCustomPropertyChanged( QgsLayerTreeNode *node, const QString &key );
625
627 void clearLegendCachedData();
628
629 private:
630 QgsLayoutItemLegend() = delete;
631
633 void setCustomLayerTree( QgsLayerTree *rootGroup );
634
635 void setupMapConnections( QgsLayoutItemMap *map, bool connect = true );
636
637 void setModelStyleOverrides( const QMap<QString, QString> &overrides );
638
639 std::unique_ptr< QgsLegendModel > mLegendModel;
640 std::unique_ptr< QgsLayerTreeGroup > mCustomLayerTree;
641
642 QgsLegendSettings mSettings;
643
644 QString mTitle;
645 int mColumnCount = 1;
646
647 QString mMapUuid;
648 QgsLayoutItemMap *mMap = nullptr;
649
650 QList< QString > mFilterByMapUuids;
651 QList< QPointer< QgsLayoutItemMap >> mFilterByMapItems;
652
653 bool mLegendFilterByMap = false;
654 bool mLegendFilterByExpression = false;
655
657 bool mFilterOutAtlas = false;
658
660 bool mFilterAskedForUpdate = false;
662 void doUpdateFilterByMap();
663
664 bool mInAtlas = false;
665
667 bool mInitialMapScaleCalculated = false;
668
670 bool mForceResize = false;
671
673 bool mSizeToContents = true;
674
676 QString mThemeName;
677
679
680};
681
682#endif // QGSLAYOUTITEMLEGEND_H
683
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.
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.
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:94
#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