QGIS API Documentation 3.39.0-Master (47f7b3a4989)
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 IsDataDefinedSize SIP_MONKEYPATCH_COMPAT_NAME( IsDataDefinedSizeRole ),
96 };
97 Q_ENUM( CustomRole )
98 // *INDENT-ON*
99
100
112
114 QgsLayerTreeLayer *layerNode() const { return mLayerNode; }
115
117 QgsLayerTreeModel *model() const;
118
120 virtual Qt::ItemFlags flags() const;
121
123 virtual QVariant data( int role ) const = 0;
124
126 virtual bool setData( const QVariant &value, int role );
127
128 virtual bool isEmbeddedInParent() const { return mEmbeddedInParent; }
129 virtual void setEmbeddedInParent( bool embedded ) { mEmbeddedInParent = embedded; }
130
131 virtual QString userLabel() const { return mUserLabel; }
132 virtual void setUserLabel( const QString &userLabel ) { mUserLabel = userLabel; }
133
143 virtual QSizeF userPatchSize() const;
144
154 virtual void setUserPatchSize( QSizeF size );
155
162 virtual void setColumnBreak( bool breakBeforeNode ) { mColumnBreakBeforeNode = breakBeforeNode; }
163
170 virtual bool columnBreak() const { return mColumnBreakBeforeNode; }
171
172 virtual bool isScaleOK( double scale ) const { Q_UNUSED( scale ) return true; }
173
178 virtual void invalidateMapBasedData() {}
179
181 {
182 Q_NOWARN_DEPRECATED_PUSH //because of deprecated members
183 ItemContext() = default;
185
187 QgsRenderContext *context = nullptr;
189 QPainter *painter = nullptr;
190
195 Q_DECL_DEPRECATED QPointF point;
196
201 Q_DECL_DEPRECATED double labelXOffset = 0.0;
202
207 double top = 0.0;
208
215 double columnLeft = 0.0;
216
223 double columnRight = 0.0;
224
230 double maxSiblingSymbolWidth = 0.0;
231
238
246 QSizeF patchSize;
247
253 const QgsTextDocument *textDocument = nullptr;
254
260 const QgsTextDocumentMetrics *textDocumentMetrics = nullptr;
261
268
269 };
270
272 {
274 QSizeF labelSize;
275 };
276
283 virtual ItemMetrics draw( const QgsLegendSettings &settings, ItemContext *ctx );
284
292 QJsonObject exportToJson( const QgsLegendSettings &settings, const QgsRenderContext &context );
293
301 virtual QSizeF drawSymbol( const QgsLegendSettings &settings, ItemContext *ctx, double itemHeight ) const;
302
309 virtual QJsonObject exportSymbolToJson( const QgsLegendSettings &settings, const QgsRenderContext &context ) const;
310
318 virtual QSizeF drawSymbolText( const QgsLegendSettings &settings, ItemContext *ctx, QSizeF symbolSize ) const;
319
320#ifdef SIP_RUN
321 SIP_PYOBJECT __repr__();
322 % MethodCode
323 QString str = QStringLiteral( "<QgsLayerTreeModelLegendNode: \"%1\">" ).arg( sipCpp->data( Qt::DisplayRole ).toString() );
324 sipRes = PyUnicode_FromString( str.toUtf8().constData() );
325 % End
326#endif
327
328 public slots:
329
336 void checkAllItems();
337
344 void uncheckAllItems();
345
352 void toggleAllItems();
353
354 signals:
357
364
365 protected:
367 explicit QgsLayerTreeModelLegendNode( QgsLayerTreeLayer *nodeL, QObject *parent SIP_TRANSFERTHIS = nullptr );
368
370 QgsRenderContext *createTemporaryRenderContext() const SIP_FACTORY;
371
372 protected:
373 QgsLayerTreeLayer *mLayerNode = nullptr;
374 bool mEmbeddedInParent;
375 QString mUserLabel;
377 QSizeF mUserSize;
378 bool mColumnBreakBeforeNode = false;
379
380 private:
381
386 void checkAll( bool state );
387};
389
390#include "qgslegendsymbolitem.h"
391#include "qgstextformat.h"
392
400{
401 Q_OBJECT
402
403 public:
404
405 static double MINIMUM_SIZE;
406 static double MAXIMUM_SIZE;
407
414 QgsSymbolLegendNode( QgsLayerTreeLayer *nodeLayer, const QgsLegendSymbolItem &item, QObject *parent SIP_TRANSFERTHIS = nullptr );
416
417 Qt::ItemFlags flags() const override;
418 QVariant data( int role ) const override;
419 bool setData( const QVariant &value, int role ) override;
420
421 QSizeF drawSymbol( const QgsLegendSettings &settings, ItemContext *ctx, double itemHeight ) const override;
422
423 QJsonObject exportSymbolToJson( const QgsLegendSettings &settings, const QgsRenderContext &context ) const override;
424
425 void setEmbeddedInParent( bool embedded ) override;
426
427 void setUserLabel( const QString &userLabel ) override { mUserLabel = userLabel; updateLabel(); }
428
429 bool isScaleOK( double scale ) const override { return mItem.isScaleOK( scale ); }
430
431 void invalidateMapBasedData() override;
432
436 void setIconSize( QSize sz );
437 QSize iconSize() const { return mIconSize; }
438
445 QSize minimumIconSize() const;
446
452 QSize minimumIconSize( QgsRenderContext *context ) const;
453
458 const QgsSymbol *symbol() const;
459
466 void setSymbol( QgsSymbol *symbol SIP_TRANSFER );
467
472 QString textOnSymbolLabel() const { return mTextOnSymbolLabel; }
473
478 void setTextOnSymbolLabel( const QString &label ) { mTextOnSymbolLabel = label; }
479
484 QgsTextFormat textOnSymbolTextFormat() const { return mTextOnSymbolTextFormat; }
485
490 void setTextOnSymbolTextFormat( const QgsTextFormat &format ) { mTextOnSymbolTextFormat = format; }
491
496 QString symbolLabel() const;
497
504 QgsLegendPatchShape patchShape() const;
505
512 void setPatchShape( const QgsLegendPatchShape &shape );
513
523 QgsSymbol *customSymbol() const;
524
536 void setCustomSymbol( QgsSymbol *symbol SIP_TRANSFER );
537
544 QString evaluateLabel( const QgsExpressionContext &context = QgsExpressionContext(), const QString &label = QString() );
545
553 QgsExpressionContextScope *createSymbolScope() const SIP_FACTORY;
554
555#ifdef SIP_RUN
556 SIP_PYOBJECT __repr__();
557 % MethodCode
558 QString str = QStringLiteral( "<QgsSymbolLegendNode: %1 \"%2\"" ).arg(
559 sipCpp->data( static_cast< int >( QgsLayerTreeModelLegendNode::CustomRole::RuleKey ) ).toString(),
560 sipCpp->data( Qt::DisplayRole ).toString() );
561 sipRes = PyUnicode_FromString( str.toUtf8().constData() );
562 % End
563#endif
564
565 private:
566 void updateLabel();
567
568 private:
570 mutable QPixmap mPixmap; // cached symbol preview
571 QString mLabel;
572 bool mSymbolUsesMapUnits;
573
574 QSize mIconSize;
575
576 QString mTextOnSymbolLabel;
577 QgsTextFormat mTextOnSymbolTextFormat;
578
579 std::unique_ptr< QgsSymbol > mCustomSymbol;
580
581 // ident the symbol icon to make it look like a tree structure
582 static const int INDENT_SIZE = 20;
583
584};
585
586
593{
594 Q_OBJECT
595
596 public:
597
606 QgsSimpleLegendNode( QgsLayerTreeLayer *nodeLayer, const QString &label, const QIcon &icon = QIcon(), QObject *parent SIP_TRANSFERTHIS = nullptr, const QString &key = QString() );
607
608 QVariant data( int role ) const override;
609
610#ifdef SIP_RUN
611 SIP_PYOBJECT __repr__();
612 % MethodCode
613 QString str = QStringLiteral( "<QgsSimpleLegendNode: \"%1\">" ).arg( sipCpp->data( Qt::DisplayRole ).toString() );
614 sipRes = PyUnicode_FromString( str.toUtf8().constData() );
615 % End
616#endif
617
618 private:
619 QString mLabel;
620 QString mId;
621 QIcon mIcon;
622 QString mKey;
623};
624
625
632{
633 Q_OBJECT
634
635 public:
636
643 QgsImageLegendNode( QgsLayerTreeLayer *nodeLayer, const QImage &img, QObject *parent SIP_TRANSFERTHIS = nullptr );
644
645 QVariant data( int role ) const override;
646
647 QSizeF drawSymbol( const QgsLegendSettings &settings, ItemContext *ctx, double itemHeight ) const override;
648
649 QJsonObject exportSymbolToJson( const QgsLegendSettings &settings, const QgsRenderContext &context ) const override;
650
651#ifdef SIP_RUN
652 SIP_PYOBJECT __repr__();
653 % MethodCode
654 QString str = QStringLiteral( "<QgsImageLegendNode: \"%1\">" ).arg( sipCpp->data( Qt::DisplayRole ).toString() );
655 sipRes = PyUnicode_FromString( str.toUtf8().constData() );
656 % End
657#endif
658
659 private:
660 QImage mImage;
661};
662
669{
670 Q_OBJECT
671
672 public:
673
683 QgsRasterSymbolLegendNode( QgsLayerTreeLayer *nodeLayer, const QColor &color, const QString &label, QObject *parent SIP_TRANSFERTHIS = nullptr, bool isCheckable = false, const QString &ruleKey = QString() );
684
685 Qt::ItemFlags flags() const override;
686 QVariant data( int role ) const override;
687 bool setData( const QVariant &value, int role ) override;
688 QSizeF drawSymbol( const QgsLegendSettings &settings, ItemContext *ctx, double itemHeight ) const override;
689 QJsonObject exportSymbolToJson( const QgsLegendSettings &settings, const QgsRenderContext &context ) const override;
690
696 QString ruleKey() const { return mRuleKey; }
697
703 bool isCheckable() const { return mCheckable; }
704
705#ifdef SIP_RUN
706 SIP_PYOBJECT __repr__();
707 % MethodCode
708 QString str = QStringLiteral( "<QgsRasterSymbolLegendNode: \"%1\">" ).arg( sipCpp->data( Qt::DisplayRole ).toString() );
709 sipRes = PyUnicode_FromString( str.toUtf8().constData() );
710 % End
711#endif
712
713 private:
714 QColor mColor;
715 QString mLabel;
716 bool mCheckable = false;
717 QString mRuleKey;
718};
719
720class QgsImageFetcher;
721
728{
729 Q_OBJECT
730
731 public:
732
738 QgsWmsLegendNode( QgsLayerTreeLayer *nodeLayer, QObject *parent SIP_TRANSFERTHIS = nullptr );
739
741
742 QVariant data( int role ) const override;
743
744 QSizeF drawSymbol( const QgsLegendSettings &settings, ItemContext *ctx, double itemHeight ) const override;
745
746 QJsonObject exportSymbolToJson( const QgsLegendSettings &settings, const QgsRenderContext &context ) const override;
747
748 void invalidateMapBasedData() override;
749
750#ifdef SIP_RUN
751 SIP_PYOBJECT __repr__();
752 % MethodCode
753 QString str = QStringLiteral( "<QgsWmsLegendNode: \"%1\">" ).arg( sipCpp->data( Qt::DisplayRole ).toString() );
754 sipRes = PyUnicode_FromString( str.toUtf8().constData() );
755 % End
756#endif
757
758 private slots:
759
760 void getLegendGraphicFinished( const QImage & );
761 void getLegendGraphicErrored( const QString & );
762 void getLegendGraphicProgress( qint64, qint64 );
763
764 private:
765
766 // Lazily initializes mImage
767 QImage getLegendGraphic( bool synchronous = false ) const;
768
769 QImage renderMessage( const QString &msg ) const;
770
771 QImage mImage;
772
773 bool mValid;
774
775 mutable std::unique_ptr<QgsImageFetcher> mFetcher;
776};
777
778
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.
@ 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:6042
#define Q_NOWARN_DEPRECATED_PUSH
Definition qgis.h:6041
#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.