QGIS API Documentation 3.32.0-Lima (311a8cb8a6)
qgslayertreemodellegendnode.h
Go to the documentation of this file.
1/***************************************************************************
2 qgslayertreemodellegendnode.h
3 --------------------------------------
4 Date : August 2014
5 Copyright : (C) 2014 by Martin Dobias
6 Email : wonder dot sk at gmail dot com
7
8 QgsWMSLegendNode : Sandro Santilli < strk at keybit dot net >
9
10 ***************************************************************************
11 * *
12 * This program is free software; you can redistribute it and/or modify *
13 * it under the terms of the GNU General Public License as published by *
14 * the Free Software Foundation; either version 2 of the License, or *
15 * (at your option) any later version. *
16 * *
17 ***************************************************************************/
18
19#ifndef QGSLAYERTREEMODELLEGENDNODE_H
20#define QGSLAYERTREEMODELLEGENDNODE_H
21
22#include <QIcon>
23#include <QObject>
24
25#include "qgis_core.h"
26#include "qgis_sip.h"
27
29#include "qgslegendpatchshape.h"
30#include "qgspallabeling.h"
31
35class QgsMapSettings;
36class QgsSymbol;
38class QgsTextFormat;
39class QgsTextDocument;
41
52class CORE_EXPORT QgsLayerTreeModelLegendNode : public QObject
53{
54#ifdef SIP_RUN
56#endif
57
58#ifdef SIP_RUN
60 if ( qobject_cast<QgsSymbolLegendNode *> ( sipCpp ) )
61 sipType = sipType_QgsSymbolLegendNode;
62 else if ( qobject_cast<QgsDataDefinedSizeLegendNode *> ( sipCpp ) )
63 sipType = sipType_QgsDataDefinedSizeLegendNode;
64 else if ( qobject_cast<QgsImageLegendNode *> ( sipCpp ) )
65 sipType = sipType_QgsImageLegendNode;
66 else if ( qobject_cast<QgsRasterSymbolLegendNode *> ( sipCpp ) )
67 sipType = sipType_QgsRasterSymbolLegendNode;
68 else if ( qobject_cast<QgsSimpleLegendNode *> ( sipCpp ) )
69 sipType = sipType_QgsSimpleLegendNode;
70 else if ( qobject_cast<QgsWmsLegendNode *> ( sipCpp ) )
71 sipType = sipType_QgsWmsLegendNode;
72 else if ( qobject_cast<QgsColorRampLegendNode *> ( sipCpp ) )
73 sipType = sipType_QgsColorRampLegendNode;
74 else
75 sipType = 0;
77#endif
78
79 Q_OBJECT
80
81 public:
82
85 {
86 RuleKeyRole = Qt::UserRole,
89 };
90
93 {
102 };
103
105 QgsLayerTreeLayer *layerNode() const { return mLayerNode; }
106
108 QgsLayerTreeModel *model() const;
109
111 virtual Qt::ItemFlags flags() const;
112
114 virtual QVariant data( int role ) const = 0;
115
117 virtual bool setData( const QVariant &value, int role );
118
119 virtual bool isEmbeddedInParent() const { return mEmbeddedInParent; }
120 virtual void setEmbeddedInParent( bool embedded ) { mEmbeddedInParent = embedded; }
121
122 virtual QString userLabel() const { return mUserLabel; }
123 virtual void setUserLabel( const QString &userLabel ) { mUserLabel = userLabel; }
124
134 virtual QSizeF userPatchSize() const;
135
145 virtual void setUserPatchSize( QSizeF size );
146
153 virtual void setColumnBreak( bool breakBeforeNode ) { mColumnBreakBeforeNode = breakBeforeNode; }
154
161 virtual bool columnBreak() const { return mColumnBreakBeforeNode; }
162
163 virtual bool isScaleOK( double scale ) const { Q_UNUSED( scale ) return true; }
164
169 virtual void invalidateMapBasedData() {}
170
172 {
173 Q_NOWARN_DEPRECATED_PUSH //because of deprecated members
174 ItemContext() = default;
176
178 QgsRenderContext *context = nullptr;
180 QPainter *painter = nullptr;
181
186 Q_DECL_DEPRECATED QPointF point;
187
192 Q_DECL_DEPRECATED double labelXOffset = 0.0;
193
198 double top = 0.0;
199
206 double columnLeft = 0.0;
207
214 double columnRight = 0.0;
215
221 double maxSiblingSymbolWidth = 0.0;
222
229
237 QSizeF patchSize;
238
244 const QgsTextDocument *textDocument = nullptr;
245
251 const QgsTextDocumentMetrics *textDocumentMetrics = nullptr;
252
259
260 };
261
263 {
265 QSizeF labelSize;
266 };
267
274 virtual ItemMetrics draw( const QgsLegendSettings &settings, ItemContext *ctx );
275
283 QJsonObject exportToJson( const QgsLegendSettings &settings, const QgsRenderContext &context );
284
292 virtual QSizeF drawSymbol( const QgsLegendSettings &settings, ItemContext *ctx, double itemHeight ) const;
293
300 virtual QJsonObject exportSymbolToJson( const QgsLegendSettings &settings, const QgsRenderContext &context ) const;
301
309 virtual QSizeF drawSymbolText( const QgsLegendSettings &settings, ItemContext *ctx, QSizeF symbolSize ) const;
310
311#ifdef SIP_RUN
312 SIP_PYOBJECT __repr__();
313 % MethodCode
314 QString str = QStringLiteral( "<QgsLayerTreeModelLegendNode: \"%1\">" ).arg( sipCpp->data( Qt::DisplayRole ).toString() );
315 sipRes = PyUnicode_FromString( str.toUtf8().constData() );
316 % End
317#endif
318
319 public slots:
320
327 void checkAllItems();
328
335 void uncheckAllItems();
336
343 void toggleAllItems();
344
345 signals:
348
355
356 protected:
358 explicit QgsLayerTreeModelLegendNode( QgsLayerTreeLayer *nodeL, QObject *parent SIP_TRANSFERTHIS = nullptr );
359
361 QgsRenderContext *createTemporaryRenderContext() const SIP_FACTORY;
362
363 protected:
364 QgsLayerTreeLayer *mLayerNode = nullptr;
365 bool mEmbeddedInParent;
366 QString mUserLabel;
368 QSizeF mUserSize;
369 bool mColumnBreakBeforeNode = false;
370
371 private:
372
377 void checkAll( bool state );
378};
380
381#include "qgslegendsymbolitem.h"
382#include "qgstextformat.h"
383
392{
393 Q_OBJECT
394
395 public:
396
397 static double MINIMUM_SIZE;
398 static double MAXIMUM_SIZE;
399
406 QgsSymbolLegendNode( QgsLayerTreeLayer *nodeLayer, const QgsLegendSymbolItem &item, QObject *parent SIP_TRANSFERTHIS = nullptr );
408
409 Qt::ItemFlags flags() const override;
410 QVariant data( int role ) const override;
411 bool setData( const QVariant &value, int role ) override;
412
413 QSizeF drawSymbol( const QgsLegendSettings &settings, ItemContext *ctx, double itemHeight ) const override;
414
415 QJsonObject exportSymbolToJson( const QgsLegendSettings &settings, const QgsRenderContext &context ) const override;
416
417 void setEmbeddedInParent( bool embedded ) override;
418
419 void setUserLabel( const QString &userLabel ) override { mUserLabel = userLabel; updateLabel(); }
420
421 bool isScaleOK( double scale ) const override { return mItem.isScaleOK( scale ); }
422
423 void invalidateMapBasedData() override;
424
429 void setIconSize( QSize sz );
431 QSize iconSize() const { return mIconSize; }
432
440 QSize minimumIconSize() const;
441
448 QSize minimumIconSize( QgsRenderContext *context ) const;
449
455 const QgsSymbol *symbol() const;
456
464 void setSymbol( QgsSymbol *symbol SIP_TRANSFER );
465
470 QString textOnSymbolLabel() const { return mTextOnSymbolLabel; }
471
476 void setTextOnSymbolLabel( const QString &label ) { mTextOnSymbolLabel = label; }
477
482 QgsTextFormat textOnSymbolTextFormat() const { return mTextOnSymbolTextFormat; }
483
488 void setTextOnSymbolTextFormat( const QgsTextFormat &format ) { mTextOnSymbolTextFormat = format; }
489
494 QString symbolLabel() const;
495
502 QgsLegendPatchShape patchShape() const;
503
510 void setPatchShape( const QgsLegendPatchShape &shape );
511
521 QgsSymbol *customSymbol() const;
522
534 void setCustomSymbol( QgsSymbol *symbol SIP_TRANSFER );
535
542 QString evaluateLabel( const QgsExpressionContext &context = QgsExpressionContext(), const QString &label = QString() );
543
544#ifdef SIP_RUN
545 SIP_PYOBJECT __repr__();
546 % MethodCode
547 QString str = QStringLiteral( "<QgsSymbolLegendNode: %1 \"%2\"" ).arg(
548 sipCpp->data( QgsLayerTreeModelLegendNode::RuleKeyRole ).toString(),
549 sipCpp->data( Qt::DisplayRole ).toString() );
550 sipRes = PyUnicode_FromString( str.toUtf8().constData() );
551 % End
552#endif
553
554 private:
555 void updateLabel();
556
557 private:
559 mutable QPixmap mPixmap; // cached symbol preview
560 QString mLabel;
561 bool mSymbolUsesMapUnits;
562
563 QSize mIconSize;
564
565 QString mTextOnSymbolLabel;
566 QgsTextFormat mTextOnSymbolTextFormat;
567
568 std::unique_ptr< QgsSymbol > mCustomSymbol;
569
570 // ident the symbol icon to make it look like a tree structure
571 static const int INDENT_SIZE = 20;
572
577 QgsExpressionContextScope *createSymbolScope() const SIP_FACTORY;
578
579};
580
581
589{
590 Q_OBJECT
591
592 public:
593
602 QgsSimpleLegendNode( QgsLayerTreeLayer *nodeLayer, const QString &label, const QIcon &icon = QIcon(), QObject *parent SIP_TRANSFERTHIS = nullptr, const QString &key = QString() );
603
604 QVariant data( int role ) const override;
605
606#ifdef SIP_RUN
607 SIP_PYOBJECT __repr__();
608 % MethodCode
609 QString str = QStringLiteral( "<QgsSimpleLegendNode: \"%1\">" ).arg( sipCpp->data( Qt::DisplayRole ).toString() );
610 sipRes = PyUnicode_FromString( str.toUtf8().constData() );
611 % End
612#endif
613
614 private:
615 QString mLabel;
616 QString mId;
617 QIcon mIcon;
618 QString mKey;
619};
620
621
629{
630 Q_OBJECT
631
632 public:
633
640 QgsImageLegendNode( QgsLayerTreeLayer *nodeLayer, const QImage &img, QObject *parent SIP_TRANSFERTHIS = nullptr );
641
642 QVariant data( int role ) const override;
643
644 QSizeF drawSymbol( const QgsLegendSettings &settings, ItemContext *ctx, double itemHeight ) const override;
645
646 QJsonObject exportSymbolToJson( const QgsLegendSettings &settings, const QgsRenderContext &context ) const override;
647
648#ifdef SIP_RUN
649 SIP_PYOBJECT __repr__();
650 % MethodCode
651 QString str = QStringLiteral( "<QgsImageLegendNode: \"%1\">" ).arg( sipCpp->data( Qt::DisplayRole ).toString() );
652 sipRes = PyUnicode_FromString( str.toUtf8().constData() );
653 % End
654#endif
655
656 private:
657 QImage mImage;
658};
659
667{
668 Q_OBJECT
669
670 public:
671
681 QgsRasterSymbolLegendNode( QgsLayerTreeLayer *nodeLayer, const QColor &color, const QString &label, QObject *parent SIP_TRANSFERTHIS = nullptr, bool isCheckable = false, const QString &ruleKey = QString() );
682
683 Qt::ItemFlags flags() const override;
684 QVariant data( int role ) const override;
685 bool setData( const QVariant &value, int role ) override;
686 QSizeF drawSymbol( const QgsLegendSettings &settings, ItemContext *ctx, double itemHeight ) const override;
687 QJsonObject exportSymbolToJson( const QgsLegendSettings &settings, const QgsRenderContext &context ) const override;
688
694 QString ruleKey() const { return mRuleKey; }
695
701 bool isCheckable() const { return mCheckable; }
702
703#ifdef SIP_RUN
704 SIP_PYOBJECT __repr__();
705 % MethodCode
706 QString str = QStringLiteral( "<QgsRasterSymbolLegendNode: \"%1\">" ).arg( sipCpp->data( Qt::DisplayRole ).toString() );
707 sipRes = PyUnicode_FromString( str.toUtf8().constData() );
708 % End
709#endif
710
711 private:
712 QColor mColor;
713 QString mLabel;
714 bool mCheckable = false;
715 QString mRuleKey;
716};
717
718class QgsImageFetcher;
719
727{
728 Q_OBJECT
729
730 public:
731
737 QgsWmsLegendNode( QgsLayerTreeLayer *nodeLayer, QObject *parent SIP_TRANSFERTHIS = nullptr );
738
740
741 QVariant data( int role ) const override;
742
743 QSizeF drawSymbol( const QgsLegendSettings &settings, ItemContext *ctx, double itemHeight ) const override;
744
745 QJsonObject exportSymbolToJson( const QgsLegendSettings &settings, const QgsRenderContext &context ) const override;
746
747 void invalidateMapBasedData() override;
748
749#ifdef SIP_RUN
750 SIP_PYOBJECT __repr__();
751 % MethodCode
752 QString str = QStringLiteral( "<QgsWmsLegendNode: \"%1\">" ).arg( sipCpp->data( Qt::DisplayRole ).toString() );
753 sipRes = PyUnicode_FromString( str.toUtf8().constData() );
754 % End
755#endif
756
757 private slots:
758
759 void getLegendGraphicFinished( const QImage & );
760 void getLegendGraphicErrored( const QString & );
761 void getLegendGraphicProgress( qint64, qint64 );
762
763 private:
764
765 // Lazily initializes mImage
766 QImage getLegendGraphic() const;
767
768 QImage renderMessage( const QString &msg ) const;
769
770 QImage mImage;
771
772 bool mValid;
773
774 mutable std::unique_ptr<QgsImageFetcher> mFetcher;
775};
776
777
784{
785 Q_OBJECT
786
787 public:
789 QgsDataDefinedSizeLegendNode( QgsLayerTreeLayer *nodeLayer, const QgsDataDefinedSizeLegend &settings, QObject *parent SIP_TRANSFERTHIS = nullptr );
791
792 QVariant data( int role ) const override;
793
794 ItemMetrics draw( const QgsLegendSettings &settings, ItemContext *ctx ) override;
795
796#ifdef SIP_RUN
797 SIP_PYOBJECT __repr__();
798 % MethodCode
799 QString str = QStringLiteral( "<QgsDataDefinedSizeLegendNode: \"%1\">" ).arg( sipCpp->data( Qt::DisplayRole ).toString() );
800 sipRes = PyUnicode_FromString( str.toUtf8().constData() );
801 % End
802#endif
803
804 private:
805 void cacheImage() const;
806 QgsDataDefinedSizeLegend *mSettings = nullptr;
807 mutable QImage mImage;
808};
809
816{
817 Q_OBJECT
818 public:
819
826 QgsVectorLabelLegendNode( QgsLayerTreeLayer *nodeLayer, const QgsPalLayerSettings &labelSettings, QObject *parent = nullptr );
827 ~QgsVectorLabelLegendNode() override;
828
834 QVariant data( int role ) const override;
835
843 QSizeF drawSymbol( const QgsLegendSettings &settings, ItemContext *ctx, double itemHeight ) const override;
844
851 QJsonObject exportSymbolToJson( const QgsLegendSettings &settings, const QgsRenderContext &context ) const override;
852
853#ifdef SIP_RUN
854 SIP_PYOBJECT __repr__();
855 % MethodCode
856 QString str = QStringLiteral( "<QgsVectorLabelLegendNode: \"%1\">" ).arg( sipCpp->data( Qt::DisplayRole ).toString() );
857 sipRes = PyUnicode_FromString( str.toUtf8().constData() );
858 % End
859#endif
860
861 private:
862 QgsPalLayerSettings mLabelSettings;
863 QSizeF drawSymbol( const QgsLegendSettings &settings, const QgsRenderContext &renderContext, double xOffset = 0.0, double yOffset = 0.0 ) const;
864 void textWidthHeight( double &width, double &height, QgsRenderContext &ctx, const QgsTextFormat &textFormat, const QStringList &textLines ) const;
865};
866
867
868#endif // QGSLAYERTREEMODELLEGENDNODE_H
Produces legend node with a marker symbol.
Object that keeps configuration of appearance of marker symbol's data-defined size in legend.
Single scope for storing variables and functions for use within a QgsExpressionContext.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
Handles asynchronous download of images.
Implementation of legend node interface for displaying arbitrary raster image.
Layer tree node points to a map layer.
The QgsLegendRendererItem class is abstract interface for legend items returned from QgsMapLayerLegen...
virtual QVariant data(int role) const =0
Returns data associated with the item. Must be implemented in derived class.
virtual bool columnBreak() const
Returns whether a forced column break should occur before the node.
@ SimpleLegend
Simple label with icon legend node type.
@ RasterSymbolLegend
Raster symbol legend node type.
@ ImageLegend
Raster image legend node type.
@ DataDefinedSizeLegend
Marker symbol legend node type.
@ EmbeddedWidget
Embedded widget placeholder node type.
@ ColorRampLegend
Color ramp legend (since QGIS 3.18)
@ SymbolLegend
Vector symbol legend node type.
virtual bool isScaleOK(double scale) const
virtual void setColumnBreak(bool breakBeforeNode)
Sets whether a forced column break should occur before the node.
virtual void invalidateMapBasedData()
Notification from model that information from associated map view has changed.
@ ParentRuleKeyRole
Rule key of the parent legend node - for legends with tree hierarchy (QString). Added in 2....
@ RuleKeyRole
Rule key of the node (QString)
@ NodeTypeRole
Type of node. Added in 3.16.
void sizeChanged()
Emitted when the size of this node changes.
void dataChanged()
Emitted on internal data change so the layer tree model can forward the signal to views.
QgsLayerTreeLayer * layerNode() const
Returns pointer to the parent layer node.
virtual void setEmbeddedInParent(bool embedded)
virtual void setUserLabel(const QString &userLabel)
The QgsLayerTreeModel class is model implementation for Qt item views framework.
Represents a patch shape for use in map legends.
The QgsLegendSettings class stores the appearance and layout settings for legend drawing with QgsLege...
The class stores information about one class/rule of a vector layer renderer in a unified way that ca...
The QgsMapSettings class contains configuration for rendering of the map.
Contains settings for how a map layer will be labeled.
Implementation of legend node interface for displaying raster legend entries.
QString ruleKey() const
Returns the unique identifier of node for identification of the item within renderer.
bool isCheckable() const
Returns whether the item is user-checkable - whether renderer supports enabling/disabling it.
Contains information about the context of a rendering operation.
Stores properties relating to a screen.
Implementation of legend node interface for displaying arbitrary label with icon.
Implementation of legend node interface for displaying preview of vector symbols and their labels and...
QString textOnSymbolLabel() const
Returns label of text to be shown on top of the symbol.
void setUserLabel(const QString &userLabel) override
void setTextOnSymbolTextFormat(const QgsTextFormat &format)
Sets format of text to be shown on top of the symbol.
void setTextOnSymbolLabel(const QString &label)
Sets label of text to be shown on top of the symbol.
bool isScaleOK(double scale) const override
QgsTextFormat textOnSymbolTextFormat() const
Returns text format of the label to be shown on top of the symbol.
Abstract base class for all rendered symbols.
Definition: qgssymbol.h:94
Contains pre-calculated metrics of a QgsTextDocument.
Represents a document consisting of one or more QgsTextBlock objects.
Container for all settings relating to text rendering.
Definition: qgstextformat.h:42
Produces legend node for a labeling text symbol.
Implementation of legend node interface for displaying WMS legend entries.
~QgsWmsLegendNode() override
#define str(x)
Definition: qgis.cpp:38
#define Q_NOWARN_DEPRECATED_POP
Definition: qgis.h:4572
#define Q_NOWARN_DEPRECATED_PUSH
Definition: qgis.h:4571
#define SIP_CONVERT_TO_SUBCLASS_CODE(code)
Definition: qgis_sip.h:186
#define SIP_TRANSFERTHIS
Definition: qgis_sip.h:53
#define SIP_TRANSFER
Definition: qgis_sip.h:36
#define SIP_FACTORY
Definition: qgis_sip.h:76
#define SIP_END
Definition: qgis_sip.h:203
Q_DECLARE_METATYPE(QgsDatabaseQueryLogEntry)
Q_NOWARN_DEPRECATED_PUSH ItemContext()=default
QgsLegendPatchShape patchShape
The patch shape to render for the node.
QgsScreenProperties screenProperties
Destination screen properties.
QSizeF patchSize
Symbol patch size to render for the node.
Q_DECL_DEPRECATED QPointF point
Top-left corner of the legend item.