QGIS API Documentation 3.30.0-'s-Hertogenbosch (f186b8efe0)
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
52class CORE_EXPORT QgsLayerTreeModelLegendNode : public QObject
53{
54#ifdef SIP_RUN
56#endif
57
58#ifdef SIP_RUN
60 if ( qobject_cast<QgsSymbolLegendNode *> ( sipCpp ) )
61 sipType = sipType_QgsSymbolLegendNode;
62 else if ( qobject_cast<QgsDataDefinedSizeLegendNode *> ( sipCpp ) )
63 sipType = sipType_QgsDataDefinedSizeLegendNode;
64 else if ( qobject_cast<QgsImageLegendNode *> ( sipCpp ) )
65 sipType = sipType_QgsImageLegendNode;
66 else if ( qobject_cast<QgsRasterSymbolLegendNode *> ( sipCpp ) )
67 sipType = sipType_QgsRasterSymbolLegendNode;
68 else if ( qobject_cast<QgsSimpleLegendNode *> ( sipCpp ) )
69 sipType = sipType_QgsSimpleLegendNode;
70 else if ( qobject_cast<QgsWmsLegendNode *> ( sipCpp ) )
71 sipType = sipType_QgsWmsLegendNode;
72 else if ( qobject_cast<QgsColorRampLegendNode *> ( sipCpp ) )
73 sipType = sipType_QgsColorRampLegendNode;
74 else
75 sipType = 0;
77#endif
78
79 Q_OBJECT
80
81 public:
82
85 {
86 RuleKeyRole = Qt::UserRole,
89 };
90
93 {
102 };
103
105 QgsLayerTreeLayer *layerNode() const { return mLayerNode; }
106
108 QgsLayerTreeModel *model() const;
109
111 virtual Qt::ItemFlags flags() const;
112
114 virtual QVariant data( int role ) const = 0;
115
117 virtual bool setData( const QVariant &value, int role );
118
119 virtual bool isEmbeddedInParent() const { return mEmbeddedInParent; }
120 virtual void setEmbeddedInParent( bool embedded ) { mEmbeddedInParent = embedded; }
121
122 virtual QString userLabel() const { return mUserLabel; }
123 virtual void setUserLabel( const QString &userLabel ) { mUserLabel = userLabel; }
124
134 virtual QSizeF userPatchSize() const;
135
145 virtual void setUserPatchSize( QSizeF size );
146
153 virtual void setColumnBreak( bool breakBeforeNode ) { mColumnBreakBeforeNode = breakBeforeNode; }
154
161 virtual bool columnBreak() const { return mColumnBreakBeforeNode; }
162
163 virtual bool isScaleOK( double scale ) const { Q_UNUSED( scale ) return true; }
164
169 virtual void invalidateMapBasedData() {}
170
172 {
173 Q_NOWARN_DEPRECATED_PUSH //because of deprecated members
174 ItemContext() = default;
176
178 QgsRenderContext *context = nullptr;
180 QPainter *painter = nullptr;
181
186 Q_DECL_DEPRECATED QPointF point;
187
192 Q_DECL_DEPRECATED double labelXOffset = 0.0;
193
198 double top = 0.0;
199
206 double columnLeft = 0.0;
207
214 double columnRight = 0.0;
215
221 double maxSiblingSymbolWidth = 0.0;
222
229
237 QSizeF patchSize;
238
244 const QgsTextDocument *textDocument = nullptr;
245
251 const QgsTextDocumentMetrics *textDocumentMetrics = nullptr;
252
253 };
254
256 {
258 QSizeF labelSize;
259 };
260
267 virtual ItemMetrics draw( const QgsLegendSettings &settings, ItemContext *ctx );
268
276 QJsonObject exportToJson( const QgsLegendSettings &settings, const QgsRenderContext &context );
277
285 virtual QSizeF drawSymbol( const QgsLegendSettings &settings, ItemContext *ctx, double itemHeight ) const;
286
293 virtual QJsonObject exportSymbolToJson( const QgsLegendSettings &settings, const QgsRenderContext &context ) const;
294
302 virtual QSizeF drawSymbolText( const QgsLegendSettings &settings, ItemContext *ctx, QSizeF symbolSize ) const;
303
304 public slots:
305
312 void checkAllItems();
313
320 void uncheckAllItems();
321
328 void toggleAllItems();
329
330 signals:
333
340
341 protected:
343 explicit QgsLayerTreeModelLegendNode( QgsLayerTreeLayer *nodeL, QObject *parent SIP_TRANSFERTHIS = nullptr );
344
346 QgsRenderContext *createTemporaryRenderContext() const SIP_FACTORY;
347
348 protected:
349 QgsLayerTreeLayer *mLayerNode = nullptr;
350 bool mEmbeddedInParent;
351 QString mUserLabel;
353 QSizeF mUserSize;
354 bool mColumnBreakBeforeNode = false;
355
356 private:
357
362 void checkAll( bool state );
363};
365
366#include "qgslegendsymbolitem.h"
367#include "qgstextformat.h"
368
377{
378 Q_OBJECT
379
380 public:
381
382 static double MINIMUM_SIZE;
383 static double MAXIMUM_SIZE;
384
391 QgsSymbolLegendNode( QgsLayerTreeLayer *nodeLayer, const QgsLegendSymbolItem &item, QObject *parent SIP_TRANSFERTHIS = nullptr );
393
394 Qt::ItemFlags flags() const override;
395 QVariant data( int role ) const override;
396 bool setData( const QVariant &value, int role ) override;
397
398 QSizeF drawSymbol( const QgsLegendSettings &settings, ItemContext *ctx, double itemHeight ) const override;
399
400 QJsonObject exportSymbolToJson( const QgsLegendSettings &settings, const QgsRenderContext &context ) const override;
401
402 void setEmbeddedInParent( bool embedded ) override;
403
404 void setUserLabel( const QString &userLabel ) override { mUserLabel = userLabel; updateLabel(); }
405
406 bool isScaleOK( double scale ) const override { return mItem.isScaleOK( scale ); }
407
408 void invalidateMapBasedData() override;
409
414 void setIconSize( QSize sz ) { mIconSize = sz; }
416 QSize iconSize() const { return mIconSize; }
417
425 QSize minimumIconSize() const;
426
433 QSize minimumIconSize( QgsRenderContext *context ) const;
434
440 const QgsSymbol *symbol() const;
441
449 void setSymbol( QgsSymbol *symbol SIP_TRANSFER );
450
455 QString textOnSymbolLabel() const { return mTextOnSymbolLabel; }
456
461 void setTextOnSymbolLabel( const QString &label ) { mTextOnSymbolLabel = label; }
462
467 QgsTextFormat textOnSymbolTextFormat() const { return mTextOnSymbolTextFormat; }
468
473 void setTextOnSymbolTextFormat( const QgsTextFormat &format ) { mTextOnSymbolTextFormat = format; }
474
479 QString symbolLabel() const;
480
487 QgsLegendPatchShape patchShape() const;
488
495 void setPatchShape( const QgsLegendPatchShape &shape );
496
506 QgsSymbol *customSymbol() const;
507
519 void setCustomSymbol( QgsSymbol *symbol SIP_TRANSFER );
520
527 QString evaluateLabel( const QgsExpressionContext &context = QgsExpressionContext(), const QString &label = QString() );
528
529 private:
530 void updateLabel();
531
532 private:
534 mutable QPixmap mPixmap; // cached symbol preview
535 QString mLabel;
536 bool mSymbolUsesMapUnits;
537
538 QSize mIconSize;
539
540 QString mTextOnSymbolLabel;
541 QgsTextFormat mTextOnSymbolTextFormat;
542
543 std::unique_ptr< QgsSymbol > mCustomSymbol;
544
545 // ident the symbol icon to make it look like a tree structure
546 static const int INDENT_SIZE = 20;
547
552 QgsExpressionContextScope *createSymbolScope() const SIP_FACTORY;
553
554};
555
556
564{
565 Q_OBJECT
566
567 public:
568
577 QgsSimpleLegendNode( QgsLayerTreeLayer *nodeLayer, const QString &label, const QIcon &icon = QIcon(), QObject *parent SIP_TRANSFERTHIS = nullptr, const QString &key = QString() );
578
579 QVariant data( int role ) const override;
580
581 private:
582 QString mLabel;
583 QString mId;
584 QIcon mIcon;
585 QString mKey;
586};
587
588
596{
597 Q_OBJECT
598
599 public:
600
607 QgsImageLegendNode( QgsLayerTreeLayer *nodeLayer, const QImage &img, QObject *parent SIP_TRANSFERTHIS = nullptr );
608
609 QVariant data( int role ) const override;
610
611 QSizeF drawSymbol( const QgsLegendSettings &settings, ItemContext *ctx, double itemHeight ) const override;
612
613 QJsonObject exportSymbolToJson( const QgsLegendSettings &settings, const QgsRenderContext &context ) const override;
614
615 private:
616 QImage mImage;
617};
618
626{
627 Q_OBJECT
628
629 public:
630
640 QgsRasterSymbolLegendNode( QgsLayerTreeLayer *nodeLayer, const QColor &color, const QString &label, QObject *parent SIP_TRANSFERTHIS = nullptr, bool isCheckable = false, const QString &ruleKey = QString() );
641
642 Qt::ItemFlags flags() const override;
643 QVariant data( int role ) const override;
644 bool setData( const QVariant &value, int role ) override;
645 QSizeF drawSymbol( const QgsLegendSettings &settings, ItemContext *ctx, double itemHeight ) const override;
646 QJsonObject exportSymbolToJson( const QgsLegendSettings &settings, const QgsRenderContext &context ) const override;
647
653 QString ruleKey() const { return mRuleKey; }
654
660 bool isCheckable() const { return mCheckable; }
661
662 private:
663 QColor mColor;
664 QString mLabel;
665 bool mCheckable = false;
666 QString mRuleKey;
667};
668
669class QgsImageFetcher;
670
678{
679 Q_OBJECT
680
681 public:
682
688 QgsWmsLegendNode( QgsLayerTreeLayer *nodeLayer, QObject *parent SIP_TRANSFERTHIS = nullptr );
689
691
692 QVariant data( int role ) const override;
693
694 QSizeF drawSymbol( const QgsLegendSettings &settings, ItemContext *ctx, double itemHeight ) const override;
695
696 QJsonObject exportSymbolToJson( const QgsLegendSettings &settings, const QgsRenderContext &context ) const override;
697
698 void invalidateMapBasedData() override;
699
700 private slots:
701
702 void getLegendGraphicFinished( const QImage & );
703 void getLegendGraphicErrored( const QString & );
704 void getLegendGraphicProgress( qint64, qint64 );
705
706 private:
707
708 // Lazily initializes mImage
709 QImage getLegendGraphic() const;
710
711 QImage renderMessage( const QString &msg ) const;
712
713 QImage mImage;
714
715 bool mValid;
716
717 mutable std::unique_ptr<QgsImageFetcher> mFetcher;
718};
719
720
727{
728 Q_OBJECT
729
730 public:
732 QgsDataDefinedSizeLegendNode( QgsLayerTreeLayer *nodeLayer, const QgsDataDefinedSizeLegend &settings, QObject *parent SIP_TRANSFERTHIS = nullptr );
734
735 QVariant data( int role ) const override;
736
737 ItemMetrics draw( const QgsLegendSettings &settings, ItemContext *ctx ) override;
738
739 private:
740 void cacheImage() const;
741 QgsDataDefinedSizeLegend *mSettings = nullptr;
742 mutable QImage mImage;
743};
744
751{
752 Q_OBJECT
753 public:
754
761 QgsVectorLabelLegendNode( QgsLayerTreeLayer *nodeLayer, const QgsPalLayerSettings &labelSettings, QObject *parent = nullptr );
762 ~QgsVectorLabelLegendNode() override;
763
769 QVariant data( int role ) const override;
770
778 QSizeF drawSymbol( const QgsLegendSettings &settings, ItemContext *ctx, double itemHeight ) const override;
779
786 QJsonObject exportSymbolToJson( const QgsLegendSettings &settings, const QgsRenderContext &context ) const override;
787
788 private:
789 QgsPalLayerSettings mLabelSettings;
790 QSizeF drawSymbol( const QgsLegendSettings &settings, const QgsRenderContext &renderContext, double xOffset = 0.0, double yOffset = 0.0 ) const;
791 void textWidthHeight( double &width, double &height, QgsRenderContext &ctx, const QgsTextFormat &textFormat, const QStringList &textLines ) const;
792};
793
794
795#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.
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.
@ ParentRuleKeyRole
Rule key of the parent legend node - for legends with tree hierarchy (QString). Added in 2....
@ NodeTypeRole
Type of node. Added in 3.16.
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.
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 setIconSize(QSize sz)
Set the icon size.
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:93
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.
Definition: qgstextformat.h:41
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:4093
#define Q_NOWARN_DEPRECATED_PUSH
Definition: qgis.h:4092
#define SIP_CONVERT_TO_SUBCLASS_CODE(code)
Definition: qgis_sip.h:186
#define SIP_TRANSFERTHIS
Definition: qgis_sip.h:53
#define SIP_TRANSFER
Definition: qgis_sip.h:36
#define SIP_FACTORY
Definition: qgis_sip.h:76
#define SIP_END
Definition: qgis_sip.h:203
Q_DECLARE_METATYPE(QgsDatabaseQueryLogEntry)
Q_NOWARN_DEPRECATED_PUSH ItemContext()=default
QgsLegendPatchShape patchShape
The patch shape to render for the node.
QSizeF patchSize
Symbol patch size to render for the node.
Q_DECL_DEPRECATED QPointF point
Top-left corner of the legend item.