QGIS API Documentation 3.41.0-Master (d5b93354e9c)
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 //SIP_TYPEHEADER_INCLUDE( "qgscolorramplegendnode.h" );
54
55#ifdef SIP_RUN
57 if ( qobject_cast<QgsSymbolLegendNode *> ( sipCpp ) )
58 sipType = sipType_QgsSymbolLegendNode;
59 else if ( qobject_cast<QgsDataDefinedSizeLegendNode *> ( sipCpp ) )
60 sipType = sipType_QgsDataDefinedSizeLegendNode;
61 else if ( qobject_cast<QgsImageLegendNode *> ( sipCpp ) )
62 sipType = sipType_QgsImageLegendNode;
63 else if ( qobject_cast<QgsRasterSymbolLegendNode *> ( sipCpp ) )
64 sipType = sipType_QgsRasterSymbolLegendNode;
65 else if ( qobject_cast<QgsSimpleLegendNode *> ( sipCpp ) )
66 sipType = sipType_QgsSimpleLegendNode;
67 else if ( qobject_cast<QgsWmsLegendNode *> ( sipCpp ) )
68 sipType = sipType_QgsWmsLegendNode;
69 else if ( qobject_cast<QgsColorRampLegendNode *> ( sipCpp ) )
70 sipType = sipType_QgsColorRampLegendNode;
71 else
72 sipType = 0;
74#endif
75
76 Q_OBJECT
77
78 public:
79
80 // *INDENT-OFF*
81
89 {
90 RuleKey SIP_MONKEYPATCH_COMPAT_NAME( RuleKeyRole ) = Qt::UserRole,
91 ParentRuleKey SIP_MONKEYPATCH_COMPAT_NAME( ParentRuleKeyRole ),
92 NodeType SIP_MONKEYPATCH_COMPAT_NAME( NodeTypeRole ),
93 IsDataDefinedSize SIP_MONKEYPATCH_COMPAT_NAME( IsDataDefinedSizeRole ),
94 };
95 Q_ENUM( CustomRole )
96 // *INDENT-ON*
97
98
110
112 QgsLayerTreeLayer *layerNode() const { return mLayerNode; }
113
115 QgsLayerTreeModel *model() const;
116
118 virtual Qt::ItemFlags flags() const;
119
121 virtual QVariant data( int role ) const = 0;
122
124 virtual bool setData( const QVariant &value, int role );
125
126 virtual bool isEmbeddedInParent() const { return mEmbeddedInParent; }
127 virtual void setEmbeddedInParent( bool embedded ) { mEmbeddedInParent = embedded; }
128
129 virtual QString userLabel() const { return mUserLabel; }
130 virtual void setUserLabel( const QString &userLabel ) { mUserLabel = userLabel; }
131
141 virtual QSizeF userPatchSize() const;
142
152 virtual void setUserPatchSize( QSizeF size );
153
160 virtual void setColumnBreak( bool breakBeforeNode ) { mColumnBreakBeforeNode = breakBeforeNode; }
161
168 virtual bool columnBreak() const { return mColumnBreakBeforeNode; }
169
170 virtual bool isScaleOK( double scale ) const { Q_UNUSED( scale ) return true; }
171
176 virtual void invalidateMapBasedData() {}
177
179 {
180 Q_NOWARN_DEPRECATED_PUSH //because of deprecated members
181 ItemContext() = default;
183
185 QgsRenderContext *context = nullptr;
187 QPainter *painter = nullptr;
188
193 Q_DECL_DEPRECATED QPointF point;
194
199 Q_DECL_DEPRECATED double labelXOffset = 0.0;
200
205 double top = 0.0;
206
213 double columnLeft = 0.0;
214
221 double columnRight = 0.0;
222
228 double maxSiblingSymbolWidth = 0.0;
229
236
244 QSizeF patchSize;
245
251 const QgsTextDocument *textDocument = nullptr;
252
258 const QgsTextDocumentMetrics *textDocumentMetrics = nullptr;
259
266
267 };
268
270 {
272 QSizeF labelSize;
273 };
274
281 virtual ItemMetrics draw( const QgsLegendSettings &settings, ItemContext *ctx );
282
290 QJsonObject exportToJson( const QgsLegendSettings &settings, const QgsRenderContext &context );
291
299 virtual QSizeF drawSymbol( const QgsLegendSettings &settings, ItemContext *ctx, double itemHeight ) const;
300
307 virtual QJsonObject exportSymbolToJson( const QgsLegendSettings &settings, const QgsRenderContext &context ) const;
308
316 virtual QSizeF drawSymbolText( const QgsLegendSettings &settings, ItemContext *ctx, QSizeF symbolSize ) const;
317
318#ifdef SIP_RUN
319 SIP_PYOBJECT __repr__();
320 % MethodCode
321 QString str = QStringLiteral( "<QgsLayerTreeModelLegendNode: \"%1\">" ).arg( sipCpp->data( Qt::DisplayRole ).toString() );
322 sipRes = PyUnicode_FromString( str.toUtf8().constData() );
323 % End
324#endif
325
326 public slots:
327
334 void checkAllItems();
335
342 void uncheckAllItems();
343
350 void toggleAllItems();
351
352 signals:
355
362
363 protected:
365 explicit QgsLayerTreeModelLegendNode( QgsLayerTreeLayer *nodeL, QObject *parent SIP_TRANSFERTHIS = nullptr );
366
368 QgsRenderContext *createTemporaryRenderContext() const SIP_FACTORY;
369
370 protected:
371 QgsLayerTreeLayer *mLayerNode = nullptr;
372 bool mEmbeddedInParent;
373 QString mUserLabel;
375 QSizeF mUserSize;
376 bool mColumnBreakBeforeNode = false;
377
378 private:
379
384 void checkAll( bool state );
385};
387
388#include "qgslegendsymbolitem.h"
389#include "qgstextformat.h"
390
398{
399 Q_OBJECT
400
401 public:
402
403 static double MINIMUM_SIZE;
404 static double MAXIMUM_SIZE;
405
412 QgsSymbolLegendNode( QgsLayerTreeLayer *nodeLayer, const QgsLegendSymbolItem &item, QObject *parent SIP_TRANSFERTHIS = nullptr );
414
415 Qt::ItemFlags flags() const override;
416 QVariant data( int role ) const override;
417 bool setData( const QVariant &value, int role ) override;
418
419 QSizeF drawSymbol( const QgsLegendSettings &settings, ItemContext *ctx, double itemHeight ) const override;
420
421 QJsonObject exportSymbolToJson( const QgsLegendSettings &settings, const QgsRenderContext &context ) const override;
422
423 void setEmbeddedInParent( bool embedded ) override;
424
425 void setUserLabel( const QString &userLabel ) override { mUserLabel = userLabel; updateLabel(); }
426
427 bool isScaleOK( double scale ) const override { return mItem.isScaleOK( scale ); }
428
429 void invalidateMapBasedData() override;
430
434 void setIconSize( QSize sz );
435 QSize iconSize() const { return mIconSize; }
436
443 QSize minimumIconSize() const;
444
450 QSize minimumIconSize( QgsRenderContext *context ) const;
451
456 const QgsSymbol *symbol() const;
457
464 void setSymbol( QgsSymbol *symbol SIP_TRANSFER );
465
470 QString textOnSymbolLabel() const { return mTextOnSymbolLabel; }
471
476 void setTextOnSymbolLabel( const QString &label ) { mTextOnSymbolLabel = label; }
477
482 QgsTextFormat textOnSymbolTextFormat() const { return mTextOnSymbolTextFormat; }
483
488 void setTextOnSymbolTextFormat( const QgsTextFormat &format ) { mTextOnSymbolTextFormat = format; }
489
494 QString symbolLabel() const;
495
502 QgsLegendPatchShape patchShape() const;
503
510 void setPatchShape( const QgsLegendPatchShape &shape );
511
521 QgsSymbol *customSymbol() const;
522
534 void setCustomSymbol( QgsSymbol *symbol SIP_TRANSFER );
535
542 QString evaluateLabel( const QgsExpressionContext &context = QgsExpressionContext(), const QString &label = QString() );
543
551 QgsExpressionContextScope *createSymbolScope() const SIP_FACTORY;
552
553#ifdef SIP_RUN
554 SIP_PYOBJECT __repr__();
555 % MethodCode
556 QString str = QStringLiteral( "<QgsSymbolLegendNode: %1 \"%2\"" ).arg(
557 sipCpp->data( static_cast< int >( QgsLayerTreeModelLegendNode::CustomRole::RuleKey ) ).toString(),
558 sipCpp->data( Qt::DisplayRole ).toString() );
559 sipRes = PyUnicode_FromString( str.toUtf8().constData() );
560 % End
561#endif
562
563 private:
564 void updateLabel();
565
566 private:
568 mutable QPixmap mPixmap; // cached symbol preview
569 QString mLabel;
570 bool mSymbolUsesMapUnits;
571
572 QSize mIconSize;
573
574 QString mTextOnSymbolLabel;
575 QgsTextFormat mTextOnSymbolTextFormat;
576
577 std::unique_ptr< QgsSymbol > mCustomSymbol;
578
579 // ident the symbol icon to make it look like a tree structure
580 static const int INDENT_SIZE = 20;
581
582};
583
584
591{
592 Q_OBJECT
593
594 public:
595
604 QgsSimpleLegendNode( QgsLayerTreeLayer *nodeLayer, const QString &label, const QIcon &icon = QIcon(), QObject *parent SIP_TRANSFERTHIS = nullptr, const QString &key = QString() );
605
606 QVariant data( int role ) const override;
607
608#ifdef SIP_RUN
609 SIP_PYOBJECT __repr__();
610 % MethodCode
611 QString str = QStringLiteral( "<QgsSimpleLegendNode: \"%1\">" ).arg( sipCpp->data( Qt::DisplayRole ).toString() );
612 sipRes = PyUnicode_FromString( str.toUtf8().constData() );
613 % End
614#endif
615
616 private:
617 QString mLabel;
618 QString mId;
619 QIcon mIcon;
620 QString mKey;
621};
622
623
630{
631 Q_OBJECT
632
633 public:
634
641 QgsImageLegendNode( QgsLayerTreeLayer *nodeLayer, const QImage &img, QObject *parent SIP_TRANSFERTHIS = nullptr );
642
643 QVariant data( int role ) const override;
644
645 QSizeF drawSymbol( const QgsLegendSettings &settings, ItemContext *ctx, double itemHeight ) const override;
646
647 QJsonObject exportSymbolToJson( const QgsLegendSettings &settings, const QgsRenderContext &context ) const override;
648
649#ifdef SIP_RUN
650 SIP_PYOBJECT __repr__();
651 % MethodCode
652 QString str = QStringLiteral( "<QgsImageLegendNode: \"%1\">" ).arg( sipCpp->data( Qt::DisplayRole ).toString() );
653 sipRes = PyUnicode_FromString( str.toUtf8().constData() );
654 % End
655#endif
656
657 private:
658 QImage mImage;
659};
660
667{
668 Q_OBJECT
669
670 public:
671
682 QgsRasterSymbolLegendNode( QgsLayerTreeLayer *nodeLayer, const QColor &color, const QString &label, QObject *parent SIP_TRANSFERTHIS = nullptr,
683 bool isCheckable = false, const QString &ruleKey = QString(), const QString &parentRuleKey = 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 QString mParentRuleKey;
719};
720
721class QgsImageFetcher;
722
729{
730 Q_OBJECT
731
732 public:
733
739 QgsWmsLegendNode( QgsLayerTreeLayer *nodeLayer, QObject *parent SIP_TRANSFERTHIS = nullptr );
740
742
743 QVariant data( int role ) const override;
744
745 QSizeF drawSymbol( const QgsLegendSettings &settings, ItemContext *ctx, double itemHeight ) const override;
746
747 QJsonObject exportSymbolToJson( const QgsLegendSettings &settings, const QgsRenderContext &context ) const override;
748
749 void invalidateMapBasedData() override;
750
755 QImage getLegendGraphicBlocking( ) const;
756
757
758#ifdef SIP_RUN
759 SIP_PYOBJECT __repr__();
760 % MethodCode
761 QString str = QStringLiteral( "<QgsWmsLegendNode: \"%1\">" ).arg( sipCpp->data( Qt::DisplayRole ).toString() );
762 sipRes = PyUnicode_FromString( str.toUtf8().constData() );
763 % End
764#endif
765
766 private slots:
767
768 void getLegendGraphicFinished( const QImage & );
769 void getLegendGraphicErrored( const QString & );
770 void getLegendGraphicProgress( qint64, qint64 );
771
772 private:
773
774 // Lazy loading of the image
775 QImage getLegendGraphic( bool synchronous = false ) const;
776
777 QImage renderMessage( const QString &msg ) const;
778
779 QImage mImage;
780
781 bool mValid;
782
783 mutable std::unique_ptr<QgsImageFetcher> mFetcher;
784};
785
786
792{
793 Q_OBJECT
794
795 public:
797 QgsDataDefinedSizeLegendNode( QgsLayerTreeLayer *nodeLayer, const QgsDataDefinedSizeLegend &settings, QObject *parent SIP_TRANSFERTHIS = nullptr );
799
800 QVariant data( int role ) const override;
801
802 ItemMetrics draw( const QgsLegendSettings &settings, ItemContext *ctx ) override;
803
804#ifdef SIP_RUN
805 SIP_PYOBJECT __repr__();
806 % MethodCode
807 QString str = QStringLiteral( "<QgsDataDefinedSizeLegendNode: \"%1\">" ).arg( sipCpp->data( Qt::DisplayRole ).toString() );
808 sipRes = PyUnicode_FromString( str.toUtf8().constData() );
809 % End
810#endif
811
812 private:
813 void cacheImage() const;
814 QgsDataDefinedSizeLegend *mSettings = nullptr;
815 mutable QImage mImage;
816};
817
824{
825 Q_OBJECT
826 public:
827
834 QgsVectorLabelLegendNode( QgsLayerTreeLayer *nodeLayer, const QgsPalLayerSettings &labelSettings, QObject *parent = nullptr );
835 ~QgsVectorLabelLegendNode() override;
836
842 QVariant data( int role ) const override;
843
851 QSizeF drawSymbol( const QgsLegendSettings &settings, ItemContext *ctx, double itemHeight ) const override;
852
859 QJsonObject exportSymbolToJson( const QgsLegendSettings &settings, const QgsRenderContext &context ) const override;
860
861#ifdef SIP_RUN
862 SIP_PYOBJECT __repr__();
863 % MethodCode
864 QString str = QStringLiteral( "<QgsVectorLabelLegendNode: \"%1\">" ).arg( sipCpp->data( Qt::DisplayRole ).toString() );
865 sipRes = PyUnicode_FromString( str.toUtf8().constData() );
866 % End
867#endif
868
869 private:
870 QgsPalLayerSettings mLabelSettings;
871 QSizeF drawSymbol( const QgsLegendSettings &settings, const QgsRenderContext &renderContext, double xOffset = 0.0, double yOffset = 0.0 ) const;
872 void textWidthHeight( double &width, double &height, QgsRenderContext &ctx, const QgsTextFormat &textFormat, const QStringList &textLines ) const;
873};
874
875
876#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 Q_NOWARN_DEPRECATED_POP
Definition qgis.h:6601
#define Q_NOWARN_DEPRECATED_PUSH
Definition qgis.h:6600
#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.