QGIS API Documentation 3.35.0-Master (f6e073f0eed)
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
545#ifdef SIP_RUN
546 SIP_PYOBJECT __repr__();
547 % MethodCode
548 QString str = QStringLiteral( "<QgsSymbolLegendNode: %1 \"%2\"" ).arg(
549 sipCpp->data( static_cast< int >( QgsLayerTreeModelLegendNode::CustomRole::RuleKey ) ).toString(),
550 sipCpp->data( Qt::DisplayRole ).toString() );
551 sipRes = PyUnicode_FromString( str.toUtf8().constData() );
552 % End
553#endif
554
555 private:
556 void updateLabel();
557
558 private:
560 mutable QPixmap mPixmap; // cached symbol preview
561 QString mLabel;
562 bool mSymbolUsesMapUnits;
563
564 QSize mIconSize;
565
566 QString mTextOnSymbolLabel;
567 QgsTextFormat mTextOnSymbolTextFormat;
568
569 std::unique_ptr< QgsSymbol > mCustomSymbol;
570
571 // ident the symbol icon to make it look like a tree structure
572 static const int INDENT_SIZE = 20;
573
578 QgsExpressionContextScope *createSymbolScope() const SIP_FACTORY;
579
580};
581
582
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
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
679 QgsRasterSymbolLegendNode( QgsLayerTreeLayer *nodeLayer, const QColor &color, const QString &label, QObject *parent SIP_TRANSFERTHIS = nullptr, bool isCheckable = false, const QString &ruleKey = QString() );
680
681 Qt::ItemFlags flags() const override;
682 QVariant data( int role ) const override;
683 bool setData( const QVariant &value, int role ) override;
684 QSizeF drawSymbol( const QgsLegendSettings &settings, ItemContext *ctx, double itemHeight ) const override;
685 QJsonObject exportSymbolToJson( const QgsLegendSettings &settings, const QgsRenderContext &context ) const override;
686
692 QString ruleKey() const { return mRuleKey; }
693
699 bool isCheckable() const { return mCheckable; }
700
701#ifdef SIP_RUN
702 SIP_PYOBJECT __repr__();
703 % MethodCode
704 QString str = QStringLiteral( "<QgsRasterSymbolLegendNode: \"%1\">" ).arg( sipCpp->data( Qt::DisplayRole ).toString() );
705 sipRes = PyUnicode_FromString( str.toUtf8().constData() );
706 % End
707#endif
708
709 private:
710 QColor mColor;
711 QString mLabel;
712 bool mCheckable = false;
713 QString mRuleKey;
714};
715
716class QgsImageFetcher;
717
724{
725 Q_OBJECT
726
727 public:
728
734 QgsWmsLegendNode( QgsLayerTreeLayer *nodeLayer, QObject *parent SIP_TRANSFERTHIS = nullptr );
735
737
738 QVariant data( int role ) const override;
739
740 QSizeF drawSymbol( const QgsLegendSettings &settings, ItemContext *ctx, double itemHeight ) const override;
741
742 QJsonObject exportSymbolToJson( const QgsLegendSettings &settings, const QgsRenderContext &context ) const override;
743
744 void invalidateMapBasedData() override;
745
746#ifdef SIP_RUN
747 SIP_PYOBJECT __repr__();
748 % MethodCode
749 QString str = QStringLiteral( "<QgsWmsLegendNode: \"%1\">" ).arg( sipCpp->data( Qt::DisplayRole ).toString() );
750 sipRes = PyUnicode_FromString( str.toUtf8().constData() );
751 % End
752#endif
753
754 private slots:
755
756 void getLegendGraphicFinished( const QImage & );
757 void getLegendGraphicErrored( const QString & );
758 void getLegendGraphicProgress( qint64, qint64 );
759
760 private:
761
762 // Lazily initializes mImage
763 QImage getLegendGraphic( bool synchronous = false ) const;
764
765 QImage renderMessage( const QString &msg ) const;
766
767 QImage mImage;
768
769 bool mValid;
770
771 mutable std::unique_ptr<QgsImageFetcher> mFetcher;
772};
773
774
780{
781 Q_OBJECT
782
783 public:
785 QgsDataDefinedSizeLegendNode( QgsLayerTreeLayer *nodeLayer, const QgsDataDefinedSizeLegend &settings, QObject *parent SIP_TRANSFERTHIS = nullptr );
787
788 QVariant data( int role ) const override;
789
790 ItemMetrics draw( const QgsLegendSettings &settings, ItemContext *ctx ) override;
791
792#ifdef SIP_RUN
793 SIP_PYOBJECT __repr__();
794 % MethodCode
795 QString str = QStringLiteral( "<QgsDataDefinedSizeLegendNode: \"%1\">" ).arg( sipCpp->data( Qt::DisplayRole ).toString() );
796 sipRes = PyUnicode_FromString( str.toUtf8().constData() );
797 % End
798#endif
799
800 private:
801 void cacheImage() const;
802 QgsDataDefinedSizeLegend *mSettings = nullptr;
803 mutable QImage mImage;
804};
805
812{
813 Q_OBJECT
814 public:
815
822 QgsVectorLabelLegendNode( QgsLayerTreeLayer *nodeLayer, const QgsPalLayerSettings &labelSettings, QObject *parent = nullptr );
823 ~QgsVectorLabelLegendNode() override;
824
830 QVariant data( int role ) const override;
831
839 QSizeF drawSymbol( const QgsLegendSettings &settings, ItemContext *ctx, double itemHeight ) const override;
840
847 QJsonObject exportSymbolToJson( const QgsLegendSettings &settings, const QgsRenderContext &context ) const override;
848
849#ifdef SIP_RUN
850 SIP_PYOBJECT __repr__();
851 % MethodCode
852 QString str = QStringLiteral( "<QgsVectorLabelLegendNode: \"%1\">" ).arg( sipCpp->data( Qt::DisplayRole ).toString() );
853 sipRes = PyUnicode_FromString( str.toUtf8().constData() );
854 % End
855#endif
856
857 private:
858 QgsPalLayerSettings mLabelSettings;
859 QSizeF drawSymbol( const QgsLegendSettings &settings, const QgsRenderContext &renderContext, double xOffset = 0.0, double yOffset = 0.0 ) const;
860 void textWidthHeight( double &width, double &height, QgsRenderContext &ctx, const QgsTextFormat &textFormat, const QStringList &textLines ) const;
861};
862
863
864#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:5713
#define Q_NOWARN_DEPRECATED_PUSH
Definition qgis.h:5712
#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.