QGIS API Documentation 3.43.0-Master (32433f7016e)
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
50class CORE_EXPORT QgsLayerTreeModelLegendNode : public QObject
51{
52 //SIP_TYPEHEADER_INCLUDE( "qgscolorramplegendnode.h" );
53
54#ifdef SIP_RUN
56 if ( qobject_cast<QgsSymbolLegendNode *> ( sipCpp ) )
57 sipType = sipType_QgsSymbolLegendNode;
58 else if ( qobject_cast<QgsDataDefinedSizeLegendNode *> ( sipCpp ) )
59 sipType = sipType_QgsDataDefinedSizeLegendNode;
60 else if ( qobject_cast<QgsImageLegendNode *> ( sipCpp ) )
61 sipType = sipType_QgsImageLegendNode;
62 else if ( qobject_cast<QgsRasterSymbolLegendNode *> ( sipCpp ) )
63 sipType = sipType_QgsRasterSymbolLegendNode;
64 else if ( qobject_cast<QgsSimpleLegendNode *> ( sipCpp ) )
65 sipType = sipType_QgsSimpleLegendNode;
66 else if ( qobject_cast<QgsWmsLegendNode *> ( sipCpp ) )
67 sipType = sipType_QgsWmsLegendNode;
68 else if ( qobject_cast<QgsColorRampLegendNode *> ( sipCpp ) )
69 sipType = sipType_QgsColorRampLegendNode;
70 else
71 sipType = 0;
73#endif
74
75 Q_OBJECT
76
77 public:
78
79 // *INDENT-OFF*
80
88 {
89 RuleKey SIP_MONKEYPATCH_COMPAT_NAME( RuleKeyRole ) = Qt::UserRole,
90 ParentRuleKey SIP_MONKEYPATCH_COMPAT_NAME( ParentRuleKeyRole ),
91 NodeType SIP_MONKEYPATCH_COMPAT_NAME( NodeTypeRole ),
92 IsDataDefinedSize SIP_MONKEYPATCH_COMPAT_NAME( IsDataDefinedSizeRole ),
93 };
94 Q_ENUM( CustomRole )
95 // *INDENT-ON*
96
97
109
111 QgsLayerTreeLayer *layerNode() const { return mLayerNode; }
112
114 QgsLayerTreeModel *model() const;
115
117 virtual Qt::ItemFlags flags() const;
118
120 virtual QVariant data( int role ) const = 0;
121
123 virtual bool setData( const QVariant &value, int role );
124
125 virtual bool isEmbeddedInParent() const { return mEmbeddedInParent; }
126 virtual void setEmbeddedInParent( bool embedded ) { mEmbeddedInParent = embedded; }
127
128 virtual QString userLabel() const { return mUserLabel; }
129 virtual void setUserLabel( const QString &userLabel ) { mUserLabel = userLabel; }
130
140 virtual QSizeF userPatchSize() const;
141
151 virtual void setUserPatchSize( QSizeF size );
152
159 virtual void setColumnBreak( bool breakBeforeNode ) { mColumnBreakBeforeNode = breakBeforeNode; }
160
167 virtual bool columnBreak() const { return mColumnBreakBeforeNode; }
168
169 virtual bool isScaleOK( double scale ) const { Q_UNUSED( scale ) return true; }
170
175 virtual void invalidateMapBasedData() {}
176
178 {
179 Q_NOWARN_DEPRECATED_PUSH //because of deprecated members
180 ItemContext() = default;
182
184 QgsRenderContext *context = nullptr;
186 QPainter *painter = nullptr;
187
192 Q_DECL_DEPRECATED QPointF point;
193
198 Q_DECL_DEPRECATED double labelXOffset = 0.0;
199
204 double top = 0.0;
205
212 double columnLeft = 0.0;
213
220 double columnRight = 0.0;
221
227 double maxSiblingSymbolWidth = 0.0;
228
235
243 QSizeF patchSize;
244
250 const QgsTextDocument *textDocument = nullptr;
251
257 const QgsTextDocumentMetrics *textDocumentMetrics = nullptr;
258
265
266 };
267
269 {
271 QSizeF labelSize;
272 };
273
280 virtual ItemMetrics draw( const QgsLegendSettings &settings, ItemContext *ctx );
281
289 QJsonObject exportToJson( const QgsLegendSettings &settings, const QgsRenderContext &context );
290
298 virtual QSizeF drawSymbol( const QgsLegendSettings &settings, ItemContext *ctx, double itemHeight ) const;
299
306 virtual QJsonObject exportSymbolToJson( const QgsLegendSettings &settings, const QgsRenderContext &context ) const;
307
315 virtual QSizeF drawSymbolText( const QgsLegendSettings &settings, ItemContext *ctx, QSizeF symbolSize ) const;
316
317#ifdef SIP_RUN
318 SIP_PYOBJECT __repr__();
319 % MethodCode
320 QString str = QStringLiteral( "<QgsLayerTreeModelLegendNode: \"%1\">" ).arg( sipCpp->data( Qt::DisplayRole ).toString() );
321 sipRes = PyUnicode_FromString( str.toUtf8().constData() );
322 % End
323#endif
324
325 public slots:
326
333 void checkAllItems();
334
341 void uncheckAllItems();
342
349 void toggleAllItems();
350
351 signals:
354
361
362 protected:
364 explicit QgsLayerTreeModelLegendNode( QgsLayerTreeLayer *nodeL, QObject *parent SIP_TRANSFERTHIS = nullptr );
365
367 QgsRenderContext *createTemporaryRenderContext() const SIP_FACTORY;
368
369 protected:
370 QgsLayerTreeLayer *mLayerNode = nullptr;
371 bool mEmbeddedInParent;
372 QString mUserLabel;
374 QSizeF mUserSize;
375 bool mColumnBreakBeforeNode = false;
376
377 private:
378
383 void checkAll( bool state );
384};
386
387#include "qgslegendsymbolitem.h"
388#include "qgstextformat.h"
389
397{
398 Q_OBJECT
399
400 public:
401
402 static double MINIMUM_SIZE;
403 static double MAXIMUM_SIZE;
404
411 QgsSymbolLegendNode( QgsLayerTreeLayer *nodeLayer, const QgsLegendSymbolItem &item, QObject *parent SIP_TRANSFERTHIS = nullptr );
413
414 Qt::ItemFlags flags() const override;
415 QVariant data( int role ) const override;
416 bool setData( const QVariant &value, int role ) override;
417
418 QSizeF drawSymbol( const QgsLegendSettings &settings, ItemContext *ctx, double itemHeight ) const override;
419
420 QJsonObject exportSymbolToJson( const QgsLegendSettings &settings, const QgsRenderContext &context ) const override;
421
422 void setEmbeddedInParent( bool embedded ) override;
423
424 void setUserLabel( const QString &userLabel ) override { mUserLabel = userLabel; updateLabel(); }
425
426 bool isScaleOK( double scale ) const override { return mItem.isScaleOK( scale ); }
427
428 void invalidateMapBasedData() override;
429
433 void setIconSize( QSize sz );
434 QSize iconSize() const { return mIconSize; }
435
442 QSize minimumIconSize() const;
443
449 QSize minimumIconSize( QgsRenderContext *context ) const;
450
455 const QgsSymbol *symbol() const;
456
463 void setSymbol( QgsSymbol *symbol SIP_TRANSFER );
464
469 QString textOnSymbolLabel() const { return mTextOnSymbolLabel; }
470
475 void setTextOnSymbolLabel( const QString &label ) { mTextOnSymbolLabel = label; }
476
481 QgsTextFormat textOnSymbolTextFormat() const { return mTextOnSymbolTextFormat; }
482
487 void setTextOnSymbolTextFormat( const QgsTextFormat &format ) { mTextOnSymbolTextFormat = format; }
488
493 QString symbolLabel() const;
494
501 QgsLegendPatchShape patchShape() const;
502
509 void setPatchShape( const QgsLegendPatchShape &shape );
510
520 QgsSymbol *customSymbol() const;
521
533 void setCustomSymbol( QgsSymbol *symbol SIP_TRANSFER );
534
541 QString evaluateLabel( const QgsExpressionContext &context = QgsExpressionContext(), const QString &label = QString() );
542
550 QgsExpressionContextScope *createSymbolScope() const SIP_FACTORY;
551
552#ifdef SIP_RUN
553 SIP_PYOBJECT __repr__();
554 % MethodCode
555 QString str = QStringLiteral( "<QgsSymbolLegendNode: %1 \"%2\"" ).arg(
556 sipCpp->data( static_cast< int >( QgsLayerTreeModelLegendNode::CustomRole::RuleKey ) ).toString(),
557 sipCpp->data( Qt::DisplayRole ).toString() );
558 sipRes = PyUnicode_FromString( str.toUtf8().constData() );
559 % End
560#endif
561
562 private:
563 void updateLabel();
564
565 private:
567 mutable QPixmap mPixmap; // cached symbol preview
568 QString mLabel;
569 bool mSymbolUsesMapUnits;
570
571 QSize mIconSize;
572
573 QString mTextOnSymbolLabel;
574 QgsTextFormat mTextOnSymbolTextFormat;
575
576 std::unique_ptr< QgsSymbol > mCustomSymbol;
577
578 // ident the symbol icon to make it look like a tree structure
579 static const int INDENT_SIZE = 20;
580
581};
582
583
590{
591 Q_OBJECT
592
593 public:
594
603 QgsSimpleLegendNode( QgsLayerTreeLayer *nodeLayer, const QString &label, const QIcon &icon = QIcon(), QObject *parent SIP_TRANSFERTHIS = nullptr, const QString &key = QString() );
604
605 QVariant data( int role ) const override;
606
607#ifdef SIP_RUN
608 SIP_PYOBJECT __repr__();
609 % MethodCode
610 QString str = QStringLiteral( "<QgsSimpleLegendNode: \"%1\">" ).arg( sipCpp->data( Qt::DisplayRole ).toString() );
611 sipRes = PyUnicode_FromString( str.toUtf8().constData() );
612 % End
613#endif
614
615 private:
616 QString mLabel;
617 QString mId;
618 QIcon mIcon;
619 QString mKey;
620};
621
622
628{
629 Q_OBJECT
630
631 public:
632
639 QgsImageLegendNode( QgsLayerTreeLayer *nodeLayer, const QImage &img, QObject *parent SIP_TRANSFERTHIS = nullptr );
640
641 QVariant data( int role ) const override;
642
643 QSizeF drawSymbol( const QgsLegendSettings &settings, ItemContext *ctx, double itemHeight ) const override;
644
645 QJsonObject exportSymbolToJson( const QgsLegendSettings &settings, const QgsRenderContext &context ) const override;
646
647#ifdef SIP_RUN
648 SIP_PYOBJECT __repr__();
649 % MethodCode
650 QString str = QStringLiteral( "<QgsImageLegendNode: \"%1\">" ).arg( sipCpp->data( Qt::DisplayRole ).toString() );
651 sipRes = PyUnicode_FromString( str.toUtf8().constData() );
652 % End
653#endif
654
655 private:
656 QImage mImage;
657};
658
665{
666 Q_OBJECT
667
668 public:
669
680 QgsRasterSymbolLegendNode( QgsLayerTreeLayer *nodeLayer, const QColor &color, const QString &label, QObject *parent SIP_TRANSFERTHIS = nullptr,
681 bool isCheckable = false, const QString &ruleKey = QString(), const QString &parentRuleKey = 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 QString mParentRuleKey;
717};
718
719class QgsImageFetcher;
720
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
753 QImage getLegendGraphicBlocking( ) const;
754
755
756#ifdef SIP_RUN
757 SIP_PYOBJECT __repr__();
758 % MethodCode
759 QString str = QStringLiteral( "<QgsWmsLegendNode: \"%1\">" ).arg( sipCpp->data( Qt::DisplayRole ).toString() );
760 sipRes = PyUnicode_FromString( str.toUtf8().constData() );
761 % End
762#endif
763
764 private slots:
765
766 void getLegendGraphicFinished( const QImage & );
767 void getLegendGraphicErrored( const QString & );
768 void getLegendGraphicProgress( qint64, qint64 );
769
770 private:
771
772 // Lazy loading of the image
773 QImage getLegendGraphic( bool synchronous = false ) const;
774
775 QImage renderMessage( const QString &msg ) const;
776
777 QImage mImage;
778
779 bool mValid;
780
781 mutable std::unique_ptr<QgsImageFetcher> mFetcher;
782};
783
784
790{
791 Q_OBJECT
792
793 public:
795 QgsDataDefinedSizeLegendNode( QgsLayerTreeLayer *nodeLayer, const QgsDataDefinedSizeLegend &settings, QObject *parent SIP_TRANSFERTHIS = nullptr );
797
798 QVariant data( int role ) const override;
799
800 ItemMetrics draw( const QgsLegendSettings &settings, ItemContext *ctx ) override;
801
802#ifdef SIP_RUN
803 SIP_PYOBJECT __repr__();
804 % MethodCode
805 QString str = QStringLiteral( "<QgsDataDefinedSizeLegendNode: \"%1\">" ).arg( sipCpp->data( Qt::DisplayRole ).toString() );
806 sipRes = PyUnicode_FromString( str.toUtf8().constData() );
807 % End
808#endif
809
810 private:
811 void cacheImage() const;
812 QgsDataDefinedSizeLegend *mSettings = nullptr;
813 mutable QImage mImage;
814};
815
822{
823 Q_OBJECT
824 public:
825
832 QgsVectorLabelLegendNode( QgsLayerTreeLayer *nodeLayer, const QgsPalLayerSettings &labelSettings, QObject *parent = nullptr );
833 ~QgsVectorLabelLegendNode() override;
834
840 QVariant data( int role ) const override;
841
849 QSizeF drawSymbol( const QgsLegendSettings &settings, ItemContext *ctx, double itemHeight ) const override;
850
857 QJsonObject exportSymbolToJson( const QgsLegendSettings &settings, const QgsRenderContext &context ) const override;
858
859#ifdef SIP_RUN
860 SIP_PYOBJECT __repr__();
861 % MethodCode
862 QString str = QStringLiteral( "<QgsVectorLabelLegendNode: \"%1\">" ).arg( sipCpp->data( Qt::DisplayRole ).toString() );
863 sipRes = PyUnicode_FromString( str.toUtf8().constData() );
864 % End
865#endif
866
867 private:
868 QgsPalLayerSettings mLabelSettings;
869 QSizeF drawSymbol( const QgsLegendSettings &settings, const QgsRenderContext &renderContext, double xOffset = 0.0, double yOffset = 0.0 ) const;
870 void textWidthHeight( double &width, double &height, QgsRenderContext &ctx, const QgsTextFormat &textFormat, const QStringList &textLines ) const;
871};
872
873
874#endif // QGSLAYERTREEMODELLEGENDNODE_H
A 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 images.
Layer tree node points to a map layer.
An abstract interface for legend items returned from QgsMapLayerLegend implementation.
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.
@ SymbolLegend
Vector symbol legend node type.
@ RuleKey
Rule key of the node (QString)
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.
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)
A model representing the layer tree, including layers and groups of layers.
Represents a patch shape for use in map legends.
Stores the appearance and layout settings for legend drawing with QgsLegendRenderer.
Stores information about one class/rule of a vector layer renderer in a unified way that can be used ...
Contains configuration for rendering maps.
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 labels with icons.
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:231
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.
A legend node for a labeling text symbol.
Implementation of legend node interface for displaying WMS legend entries.
~QgsWmsLegendNode() override
#define Q_NOWARN_DEPRECATED_POP
Definition qgis.h:6819
#define Q_NOWARN_DEPRECATED_PUSH
Definition qgis.h:6818
#define SIP_CONVERT_TO_SUBCLASS_CODE(code)
Definition qgis_sip.h:191
#define SIP_TRANSFERTHIS
Definition qgis_sip.h:53
#define SIP_MONKEYPATCH_SCOPEENUM_UNNEST(OUTSIDE_CLASS, FORMERNAME)
Definition qgis_sip.h:271
#define SIP_TRANSFER
Definition qgis_sip.h:36
#define SIP_FACTORY
Definition qgis_sip.h:76
#define SIP_END
Definition qgis_sip.h:208
#define SIP_MONKEYPATCH_COMPAT_NAME(FORMERNAME)
Definition qgis_sip.h:273
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.