QGIS API Documentation 3.38.0-Grenoble (exported)
Loading...
Searching...
No Matches
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
51class CORE_EXPORT QgsLayerTreeModelLegendNode : public QObject
52{
53#ifdef SIP_RUN
55#endif
56
57#ifdef SIP_RUN
59 if ( qobject_cast<QgsSymbolLegendNode *> ( sipCpp ) )
60 sipType = sipType_QgsSymbolLegendNode;
61 else if ( qobject_cast<QgsDataDefinedSizeLegendNode *> ( sipCpp ) )
62 sipType = sipType_QgsDataDefinedSizeLegendNode;
63 else if ( qobject_cast<QgsImageLegendNode *> ( sipCpp ) )
64 sipType = sipType_QgsImageLegendNode;
65 else if ( qobject_cast<QgsRasterSymbolLegendNode *> ( sipCpp ) )
66 sipType = sipType_QgsRasterSymbolLegendNode;
67 else if ( qobject_cast<QgsSimpleLegendNode *> ( sipCpp ) )
68 sipType = sipType_QgsSimpleLegendNode;
69 else if ( qobject_cast<QgsWmsLegendNode *> ( sipCpp ) )
70 sipType = sipType_QgsWmsLegendNode;
71 else if ( qobject_cast<QgsColorRampLegendNode *> ( sipCpp ) )
72 sipType = sipType_QgsColorRampLegendNode;
73 else
74 sipType = 0;
76#endif
77
78 Q_OBJECT
79
80 public:
81
82 // *INDENT-OFF*
83
91 {
92 RuleKey SIP_MONKEYPATCH_COMPAT_NAME( RuleKeyRole ) = Qt::UserRole,
93 ParentRuleKey SIP_MONKEYPATCH_COMPAT_NAME( ParentRuleKeyRole ),
94 NodeType SIP_MONKEYPATCH_COMPAT_NAME( NodeTypeRole ),
95 };
96 Q_ENUM( CustomRole )
97 // *INDENT-ON*
98
99
111
113 QgsLayerTreeLayer *layerNode() const { return mLayerNode; }
114
116 QgsLayerTreeModel *model() const;
117
119 virtual Qt::ItemFlags flags() const;
120
122 virtual QVariant data( int role ) const = 0;
123
125 virtual bool setData( const QVariant &value, int role );
126
127 virtual bool isEmbeddedInParent() const { return mEmbeddedInParent; }
128 virtual void setEmbeddedInParent( bool embedded ) { mEmbeddedInParent = embedded; }
129
130 virtual QString userLabel() const { return mUserLabel; }
131 virtual void setUserLabel( const QString &userLabel ) { mUserLabel = userLabel; }
132
142 virtual QSizeF userPatchSize() const;
143
153 virtual void setUserPatchSize( QSizeF size );
154
161 virtual void setColumnBreak( bool breakBeforeNode ) { mColumnBreakBeforeNode = breakBeforeNode; }
162
169 virtual bool columnBreak() const { return mColumnBreakBeforeNode; }
170
171 virtual bool isScaleOK( double scale ) const { Q_UNUSED( scale ) return true; }
172
177 virtual void invalidateMapBasedData() {}
178
180 {
181 Q_NOWARN_DEPRECATED_PUSH //because of deprecated members
182 ItemContext() = default;
184
186 QgsRenderContext *context = nullptr;
188 QPainter *painter = nullptr;
189
194 Q_DECL_DEPRECATED QPointF point;
195
200 Q_DECL_DEPRECATED double labelXOffset = 0.0;
201
206 double top = 0.0;
207
214 double columnLeft = 0.0;
215
222 double columnRight = 0.0;
223
229 double maxSiblingSymbolWidth = 0.0;
230
237
245 QSizeF patchSize;
246
252 const QgsTextDocument *textDocument = nullptr;
253
259 const QgsTextDocumentMetrics *textDocumentMetrics = nullptr;
260
267
268 };
269
271 {
273 QSizeF labelSize;
274 };
275
282 virtual ItemMetrics draw( const QgsLegendSettings &settings, ItemContext *ctx );
283
291 QJsonObject exportToJson( const QgsLegendSettings &settings, const QgsRenderContext &context );
292
300 virtual QSizeF drawSymbol( const QgsLegendSettings &settings, ItemContext *ctx, double itemHeight ) const;
301
308 virtual QJsonObject exportSymbolToJson( const QgsLegendSettings &settings, const QgsRenderContext &context ) const;
309
317 virtual QSizeF drawSymbolText( const QgsLegendSettings &settings, ItemContext *ctx, QSizeF symbolSize ) const;
318
319#ifdef SIP_RUN
320 SIP_PYOBJECT __repr__();
321 % MethodCode
322 QString str = QStringLiteral( "<QgsLayerTreeModelLegendNode: \"%1\">" ).arg( sipCpp->data( Qt::DisplayRole ).toString() );
323 sipRes = PyUnicode_FromString( str.toUtf8().constData() );
324 % End
325#endif
326
327 public slots:
328
335 void checkAllItems();
336
343 void uncheckAllItems();
344
351 void toggleAllItems();
352
353 signals:
356
363
364 protected:
366 explicit QgsLayerTreeModelLegendNode( QgsLayerTreeLayer *nodeL, QObject *parent SIP_TRANSFERTHIS = nullptr );
367
369 QgsRenderContext *createTemporaryRenderContext() const SIP_FACTORY;
370
371 protected:
372 QgsLayerTreeLayer *mLayerNode = nullptr;
373 bool mEmbeddedInParent;
374 QString mUserLabel;
376 QSizeF mUserSize;
377 bool mColumnBreakBeforeNode = false;
378
379 private:
380
385 void checkAll( bool state );
386};
388
389#include "qgslegendsymbolitem.h"
390#include "qgstextformat.h"
391
399{
400 Q_OBJECT
401
402 public:
403
404 static double MINIMUM_SIZE;
405 static double MAXIMUM_SIZE;
406
413 QgsSymbolLegendNode( QgsLayerTreeLayer *nodeLayer, const QgsLegendSymbolItem &item, QObject *parent SIP_TRANSFERTHIS = nullptr );
415
416 Qt::ItemFlags flags() const override;
417 QVariant data( int role ) const override;
418 bool setData( const QVariant &value, int role ) override;
419
420 QSizeF drawSymbol( const QgsLegendSettings &settings, ItemContext *ctx, double itemHeight ) const override;
421
422 QJsonObject exportSymbolToJson( const QgsLegendSettings &settings, const QgsRenderContext &context ) const override;
423
424 void setEmbeddedInParent( bool embedded ) override;
425
426 void setUserLabel( const QString &userLabel ) override { mUserLabel = userLabel; updateLabel(); }
427
428 bool isScaleOK( double scale ) const override { return mItem.isScaleOK( scale ); }
429
430 void invalidateMapBasedData() override;
431
435 void setIconSize( QSize sz );
436 QSize iconSize() const { return mIconSize; }
437
444 QSize minimumIconSize() const;
445
451 QSize minimumIconSize( QgsRenderContext *context ) const;
452
457 const QgsSymbol *symbol() const;
458
465 void setSymbol( QgsSymbol *symbol SIP_TRANSFER );
466
471 QString textOnSymbolLabel() const { return mTextOnSymbolLabel; }
472
477 void setTextOnSymbolLabel( const QString &label ) { mTextOnSymbolLabel = label; }
478
483 QgsTextFormat textOnSymbolTextFormat() const { return mTextOnSymbolTextFormat; }
484
489 void setTextOnSymbolTextFormat( const QgsTextFormat &format ) { mTextOnSymbolTextFormat = format; }
490
495 QString symbolLabel() const;
496
503 QgsLegendPatchShape patchShape() const;
504
511 void setPatchShape( const QgsLegendPatchShape &shape );
512
522 QgsSymbol *customSymbol() const;
523
535 void setCustomSymbol( QgsSymbol *symbol SIP_TRANSFER );
536
543 QString evaluateLabel( const QgsExpressionContext &context = QgsExpressionContext(), const QString &label = QString() );
544
552 QgsExpressionContextScope *createSymbolScope() const SIP_FACTORY;
553
554#ifdef SIP_RUN
555 SIP_PYOBJECT __repr__();
556 % MethodCode
557 QString str = QStringLiteral( "<QgsSymbolLegendNode: %1 \"%2\"" ).arg(
558 sipCpp->data( static_cast< int >( QgsLayerTreeModelLegendNode::CustomRole::RuleKey ) ).toString(),
559 sipCpp->data( Qt::DisplayRole ).toString() );
560 sipRes = PyUnicode_FromString( str.toUtf8().constData() );
561 % End
562#endif
563
564 private:
565 void updateLabel();
566
567 private:
569 mutable QPixmap mPixmap; // cached symbol preview
570 QString mLabel;
571 bool mSymbolUsesMapUnits;
572
573 QSize mIconSize;
574
575 QString mTextOnSymbolLabel;
576 QgsTextFormat mTextOnSymbolTextFormat;
577
578 std::unique_ptr< QgsSymbol > mCustomSymbol;
579
580 // ident the symbol icon to make it look like a tree structure
581 static const int INDENT_SIZE = 20;
582
583};
584
585
592{
593 Q_OBJECT
594
595 public:
596
605 QgsSimpleLegendNode( QgsLayerTreeLayer *nodeLayer, const QString &label, const QIcon &icon = QIcon(), QObject *parent SIP_TRANSFERTHIS = nullptr, const QString &key = QString() );
606
607 QVariant data( int role ) const override;
608
609#ifdef SIP_RUN
610 SIP_PYOBJECT __repr__();
611 % MethodCode
612 QString str = QStringLiteral( "<QgsSimpleLegendNode: \"%1\">" ).arg( sipCpp->data( Qt::DisplayRole ).toString() );
613 sipRes = PyUnicode_FromString( str.toUtf8().constData() );
614 % End
615#endif
616
617 private:
618 QString mLabel;
619 QString mId;
620 QIcon mIcon;
621 QString mKey;
622};
623
624
631{
632 Q_OBJECT
633
634 public:
635
642 QgsImageLegendNode( QgsLayerTreeLayer *nodeLayer, const QImage &img, QObject *parent SIP_TRANSFERTHIS = nullptr );
643
644 QVariant data( int role ) const override;
645
646 QSizeF drawSymbol( const QgsLegendSettings &settings, ItemContext *ctx, double itemHeight ) const override;
647
648 QJsonObject exportSymbolToJson( const QgsLegendSettings &settings, const QgsRenderContext &context ) const override;
649
650#ifdef SIP_RUN
651 SIP_PYOBJECT __repr__();
652 % MethodCode
653 QString str = QStringLiteral( "<QgsImageLegendNode: \"%1\">" ).arg( sipCpp->data( Qt::DisplayRole ).toString() );
654 sipRes = PyUnicode_FromString( str.toUtf8().constData() );
655 % End
656#endif
657
658 private:
659 QImage mImage;
660};
661
668{
669 Q_OBJECT
670
671 public:
672
682 QgsRasterSymbolLegendNode( QgsLayerTreeLayer *nodeLayer, const QColor &color, const QString &label, QObject *parent SIP_TRANSFERTHIS = nullptr, bool isCheckable = false, const QString &ruleKey = QString() );
683
684 Qt::ItemFlags flags() const override;
685 QVariant data( int role ) const override;
686 bool setData( const QVariant &value, int role ) override;
687 QSizeF drawSymbol( const QgsLegendSettings &settings, ItemContext *ctx, double itemHeight ) const override;
688 QJsonObject exportSymbolToJson( const QgsLegendSettings &settings, const QgsRenderContext &context ) const override;
689
695 QString ruleKey() const { return mRuleKey; }
696
702 bool isCheckable() const { return mCheckable; }
703
704#ifdef SIP_RUN
705 SIP_PYOBJECT __repr__();
706 % MethodCode
707 QString str = QStringLiteral( "<QgsRasterSymbolLegendNode: \"%1\">" ).arg( sipCpp->data( Qt::DisplayRole ).toString() );
708 sipRes = PyUnicode_FromString( str.toUtf8().constData() );
709 % End
710#endif
711
712 private:
713 QColor mColor;
714 QString mLabel;
715 bool mCheckable = false;
716 QString mRuleKey;
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
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( bool synchronous = false ) 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
783{
784 Q_OBJECT
785
786 public:
788 QgsDataDefinedSizeLegendNode( QgsLayerTreeLayer *nodeLayer, const QgsDataDefinedSizeLegend &settings, QObject *parent SIP_TRANSFERTHIS = nullptr );
790
791 QVariant data( int role ) const override;
792
793 ItemMetrics draw( const QgsLegendSettings &settings, ItemContext *ctx ) override;
794
795#ifdef SIP_RUN
796 SIP_PYOBJECT __repr__();
797 % MethodCode
798 QString str = QStringLiteral( "<QgsDataDefinedSizeLegendNode: \"%1\">" ).arg( sipCpp->data( Qt::DisplayRole ).toString() );
799 sipRes = PyUnicode_FromString( str.toUtf8().constData() );
800 % End
801#endif
802
803 private:
804 void cacheImage() const;
805 QgsDataDefinedSizeLegend *mSettings = nullptr;
806 mutable QImage mImage;
807};
808
815{
816 Q_OBJECT
817 public:
818
825 QgsVectorLabelLegendNode( QgsLayerTreeLayer *nodeLayer, const QgsPalLayerSettings &labelSettings, QObject *parent = nullptr );
826 ~QgsVectorLabelLegendNode() override;
827
833 QVariant data( int role ) const override;
834
842 QSizeF drawSymbol( const QgsLegendSettings &settings, ItemContext *ctx, double itemHeight ) const override;
843
850 QJsonObject exportSymbolToJson( const QgsLegendSettings &settings, const QgsRenderContext &context ) const override;
851
852#ifdef SIP_RUN
853 SIP_PYOBJECT __repr__();
854 % MethodCode
855 QString str = QStringLiteral( "<QgsVectorLabelLegendNode: \"%1\">" ).arg( sipCpp->data( Qt::DisplayRole ).toString() );
856 sipRes = PyUnicode_FromString( str.toUtf8().constData() );
857 % End
858#endif
859
860 private:
861 QgsPalLayerSettings mLabelSettings;
862 QSizeF drawSymbol( const QgsLegendSettings &settings, const QgsRenderContext &renderContext, double xOffset = 0.0, double yOffset = 0.0 ) const;
863 void textWidthHeight( double &width, double &height, QgsRenderContext &ctx, const QgsTextFormat &textFormat, const QStringList &textLines ) const;
864};
865
866
867#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.
@ 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)
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.
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:6022
#define Q_NOWARN_DEPRECATED_PUSH
Definition qgis.h:6021
#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.