QGIS API Documentation 3.41.0-Master (3440c17df1d)
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
684 QgsRasterSymbolLegendNode( QgsLayerTreeLayer *nodeLayer, const QColor &color, const QString &label, QObject *parent SIP_TRANSFERTHIS = nullptr,
685 bool isCheckable = false, const QString &ruleKey = QString(), const QString &parentRuleKey = QString() );
686
687 Qt::ItemFlags flags() const override;
688 QVariant data( int role ) const override;
689 bool setData( const QVariant &value, int role ) override;
690 QSizeF drawSymbol( const QgsLegendSettings &settings, ItemContext *ctx, double itemHeight ) const override;
691 QJsonObject exportSymbolToJson( const QgsLegendSettings &settings, const QgsRenderContext &context ) const override;
692
698 QString ruleKey() const { return mRuleKey; }
699
705 bool isCheckable() const { return mCheckable; }
706
707#ifdef SIP_RUN
708 SIP_PYOBJECT __repr__();
709 % MethodCode
710 QString str = QStringLiteral( "<QgsRasterSymbolLegendNode: \"%1\">" ).arg( sipCpp->data( Qt::DisplayRole ).toString() );
711 sipRes = PyUnicode_FromString( str.toUtf8().constData() );
712 % End
713#endif
714
715 private:
716 QColor mColor;
717 QString mLabel;
718 bool mCheckable = false;
719 QString mRuleKey;
720 QString mParentRuleKey;
721};
722
723class QgsImageFetcher;
724
731{
732 Q_OBJECT
733
734 public:
735
741 QgsWmsLegendNode( QgsLayerTreeLayer *nodeLayer, QObject *parent SIP_TRANSFERTHIS = nullptr );
742
744
745 QVariant data( int role ) const override;
746
747 QSizeF drawSymbol( const QgsLegendSettings &settings, ItemContext *ctx, double itemHeight ) const override;
748
749 QJsonObject exportSymbolToJson( const QgsLegendSettings &settings, const QgsRenderContext &context ) const override;
750
751 void invalidateMapBasedData() override;
752
757 QImage getLegendGraphicBlocking( ) const;
758
759
760#ifdef SIP_RUN
761 SIP_PYOBJECT __repr__();
762 % MethodCode
763 QString str = QStringLiteral( "<QgsWmsLegendNode: \"%1\">" ).arg( sipCpp->data( Qt::DisplayRole ).toString() );
764 sipRes = PyUnicode_FromString( str.toUtf8().constData() );
765 % End
766#endif
767
768 private slots:
769
770 void getLegendGraphicFinished( const QImage & );
771 void getLegendGraphicErrored( const QString & );
772 void getLegendGraphicProgress( qint64, qint64 );
773
774 private:
775
776 // Lazy loading of the image
777 QImage getLegendGraphic( bool synchronous = false ) const;
778
779 QImage renderMessage( const QString &msg ) const;
780
781 QImage mImage;
782
783 bool mValid;
784
785 mutable std::unique_ptr<QgsImageFetcher> mFetcher;
786};
787
788
794{
795 Q_OBJECT
796
797 public:
799 QgsDataDefinedSizeLegendNode( QgsLayerTreeLayer *nodeLayer, const QgsDataDefinedSizeLegend &settings, QObject *parent SIP_TRANSFERTHIS = nullptr );
801
802 QVariant data( int role ) const override;
803
804 ItemMetrics draw( const QgsLegendSettings &settings, ItemContext *ctx ) override;
805
806#ifdef SIP_RUN
807 SIP_PYOBJECT __repr__();
808 % MethodCode
809 QString str = QStringLiteral( "<QgsDataDefinedSizeLegendNode: \"%1\">" ).arg( sipCpp->data( Qt::DisplayRole ).toString() );
810 sipRes = PyUnicode_FromString( str.toUtf8().constData() );
811 % End
812#endif
813
814 private:
815 void cacheImage() const;
816 QgsDataDefinedSizeLegend *mSettings = nullptr;
817 mutable QImage mImage;
818};
819
826{
827 Q_OBJECT
828 public:
829
836 QgsVectorLabelLegendNode( QgsLayerTreeLayer *nodeLayer, const QgsPalLayerSettings &labelSettings, QObject *parent = nullptr );
837 ~QgsVectorLabelLegendNode() override;
838
844 QVariant data( int role ) const override;
845
853 QSizeF drawSymbol( const QgsLegendSettings &settings, ItemContext *ctx, double itemHeight ) const override;
854
861 QJsonObject exportSymbolToJson( const QgsLegendSettings &settings, const QgsRenderContext &context ) const override;
862
863#ifdef SIP_RUN
864 SIP_PYOBJECT __repr__();
865 % MethodCode
866 QString str = QStringLiteral( "<QgsVectorLabelLegendNode: \"%1\">" ).arg( sipCpp->data( Qt::DisplayRole ).toString() );
867 sipRes = PyUnicode_FromString( str.toUtf8().constData() );
868 % End
869#endif
870
871 private:
872 QgsPalLayerSettings mLabelSettings;
873 QSizeF drawSymbol( const QgsLegendSettings &settings, const QgsRenderContext &renderContext, double xOffset = 0.0, double yOffset = 0.0 ) const;
874 void textWidthHeight( double &width, double &height, QgsRenderContext &ctx, const QgsTextFormat &textFormat, const QStringList &textLines ) const;
875};
876
877
878#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.
@ 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: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.
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:39
#define Q_NOWARN_DEPRECATED_POP
Definition qgis.h:6535
#define Q_NOWARN_DEPRECATED_PUSH
Definition qgis.h:6534
#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.