QGIS API Documentation 3.29.0-Master (ade4f0cf0f)
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;
38
49class CORE_EXPORT QgsLayerTreeModelLegendNode : public QObject
50{
51#ifdef SIP_RUN
53#endif
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
82 {
83 RuleKeyRole = Qt::UserRole,
86 };
87
90 {
99 };
100
102 QgsLayerTreeLayer *layerNode() const { return mLayerNode; }
103
105 QgsLayerTreeModel *model() const;
106
108 virtual Qt::ItemFlags flags() const;
109
111 virtual QVariant data( int role ) const = 0;
112
114 virtual bool setData( const QVariant &value, int role );
115
116 virtual bool isEmbeddedInParent() const { return mEmbeddedInParent; }
117 virtual void setEmbeddedInParent( bool embedded ) { mEmbeddedInParent = embedded; }
118
119 virtual QString userLabel() const { return mUserLabel; }
120 virtual void setUserLabel( const QString &userLabel ) { mUserLabel = userLabel; }
121
131 virtual QSizeF userPatchSize() const;
132
142 virtual void setUserPatchSize( QSizeF size );
143
150 virtual void setColumnBreak( bool breakBeforeNode ) { mColumnBreakBeforeNode = breakBeforeNode; }
151
158 virtual bool columnBreak() const { return mColumnBreakBeforeNode; }
159
160 virtual bool isScaleOK( double scale ) const { Q_UNUSED( scale ) return true; }
161
166 virtual void invalidateMapBasedData() {}
167
169 {
170 Q_NOWARN_DEPRECATED_PUSH //because of deprecated members
171 ItemContext() = default;
173
175 QgsRenderContext *context = nullptr;
177 QPainter *painter = nullptr;
178
183 Q_DECL_DEPRECATED QPointF point;
184
189 Q_DECL_DEPRECATED double labelXOffset = 0.0;
190
195 double top = 0.0;
196
203 double columnLeft = 0.0;
204
211 double columnRight = 0.0;
212
218 double maxSiblingSymbolWidth = 0.0;
219
226
234 QSizeF patchSize;
235 };
236
238 {
240 QSizeF labelSize;
241 };
242
249 virtual ItemMetrics draw( const QgsLegendSettings &settings, ItemContext *ctx );
250
258 QJsonObject exportToJson( const QgsLegendSettings &settings, const QgsRenderContext &context );
259
267 virtual QSizeF drawSymbol( const QgsLegendSettings &settings, ItemContext *ctx, double itemHeight ) const;
268
275 virtual QJsonObject exportSymbolToJson( const QgsLegendSettings &settings, const QgsRenderContext &context ) const;
276
284 virtual QSizeF drawSymbolText( const QgsLegendSettings &settings, ItemContext *ctx, QSizeF symbolSize ) const;
285
286 public slots:
287
294 void checkAllItems();
295
302 void uncheckAllItems();
303
310 void toggleAllItems();
311
312 signals:
315
322
323 protected:
325 explicit QgsLayerTreeModelLegendNode( QgsLayerTreeLayer *nodeL, QObject *parent SIP_TRANSFERTHIS = nullptr );
326
328 QgsRenderContext *createTemporaryRenderContext() const SIP_FACTORY;
329
330 protected:
331 QgsLayerTreeLayer *mLayerNode = nullptr;
332 bool mEmbeddedInParent;
333 QString mUserLabel;
335 QSizeF mUserSize;
336 bool mColumnBreakBeforeNode = false;
337
338 private:
339
344 void checkAll( bool state );
345};
347
348#include "qgslegendsymbolitem.h"
349#include "qgstextformat.h"
350
359{
360 Q_OBJECT
361
362 public:
363
364 static double MINIMUM_SIZE;
365 static double MAXIMUM_SIZE;
366
373 QgsSymbolLegendNode( QgsLayerTreeLayer *nodeLayer, const QgsLegendSymbolItem &item, QObject *parent SIP_TRANSFERTHIS = nullptr );
375
376 Qt::ItemFlags flags() const override;
377 QVariant data( int role ) const override;
378 bool setData( const QVariant &value, int role ) override;
379
380 QSizeF drawSymbol( const QgsLegendSettings &settings, ItemContext *ctx, double itemHeight ) const override;
381
382 QJsonObject exportSymbolToJson( const QgsLegendSettings &settings, const QgsRenderContext &context ) const override;
383
384 void setEmbeddedInParent( bool embedded ) override;
385
386 void setUserLabel( const QString &userLabel ) override { mUserLabel = userLabel; updateLabel(); }
387
388 bool isScaleOK( double scale ) const override { return mItem.isScaleOK( scale ); }
389
390 void invalidateMapBasedData() override;
391
396 void setIconSize( QSize sz ) { mIconSize = sz; }
398 QSize iconSize() const { return mIconSize; }
399
407 QSize minimumIconSize() const;
408
415 QSize minimumIconSize( QgsRenderContext *context ) const;
416
422 const QgsSymbol *symbol() const;
423
431 void setSymbol( QgsSymbol *symbol SIP_TRANSFER );
432
437 QString textOnSymbolLabel() const { return mTextOnSymbolLabel; }
438
443 void setTextOnSymbolLabel( const QString &label ) { mTextOnSymbolLabel = label; }
444
449 QgsTextFormat textOnSymbolTextFormat() const { return mTextOnSymbolTextFormat; }
450
455 void setTextOnSymbolTextFormat( const QgsTextFormat &format ) { mTextOnSymbolTextFormat = format; }
456
461 QString symbolLabel() const;
462
469 QgsLegendPatchShape patchShape() const;
470
477 void setPatchShape( const QgsLegendPatchShape &shape );
478
488 QgsSymbol *customSymbol() const;
489
501 void setCustomSymbol( QgsSymbol *symbol SIP_TRANSFER );
502
509 QString evaluateLabel( const QgsExpressionContext &context = QgsExpressionContext(), const QString &label = QString() );
510
511 private:
512 void updateLabel();
513
514 private:
516 mutable QPixmap mPixmap; // cached symbol preview
517 QString mLabel;
518 bool mSymbolUsesMapUnits;
519
520 QSize mIconSize;
521
522 QString mTextOnSymbolLabel;
523 QgsTextFormat mTextOnSymbolTextFormat;
524
525 std::unique_ptr< QgsSymbol > mCustomSymbol;
526
527 // ident the symbol icon to make it look like a tree structure
528 static const int INDENT_SIZE = 20;
529
534 QgsExpressionContextScope *createSymbolScope() const SIP_FACTORY;
535
536};
537
538
546{
547 Q_OBJECT
548
549 public:
550
559 QgsSimpleLegendNode( QgsLayerTreeLayer *nodeLayer, const QString &label, const QIcon &icon = QIcon(), QObject *parent SIP_TRANSFERTHIS = nullptr, const QString &key = QString() );
560
561 QVariant data( int role ) const override;
562
563 private:
564 QString mLabel;
565 QString mId;
566 QIcon mIcon;
567 QString mKey;
568};
569
570
578{
579 Q_OBJECT
580
581 public:
582
589 QgsImageLegendNode( QgsLayerTreeLayer *nodeLayer, const QImage &img, QObject *parent SIP_TRANSFERTHIS = nullptr );
590
591 QVariant data( int role ) const override;
592
593 QSizeF drawSymbol( const QgsLegendSettings &settings, ItemContext *ctx, double itemHeight ) const override;
594
595 QJsonObject exportSymbolToJson( const QgsLegendSettings &settings, const QgsRenderContext &context ) const override;
596
597 private:
598 QImage mImage;
599};
600
608{
609 Q_OBJECT
610
611 public:
612
622 QgsRasterSymbolLegendNode( QgsLayerTreeLayer *nodeLayer, const QColor &color, const QString &label, QObject *parent SIP_TRANSFERTHIS = nullptr, bool isCheckable = false, const QString &ruleKey = QString() );
623
624 Qt::ItemFlags flags() const override;
625 QVariant data( int role ) const override;
626 bool setData( const QVariant &value, int role ) override;
627 QSizeF drawSymbol( const QgsLegendSettings &settings, ItemContext *ctx, double itemHeight ) const override;
628 QJsonObject exportSymbolToJson( const QgsLegendSettings &settings, const QgsRenderContext &context ) const override;
629
635 QString ruleKey() const { return mRuleKey; }
636
642 bool isCheckable() const { return mCheckable; }
643
644 private:
645 QColor mColor;
646 QString mLabel;
647 bool mCheckable = false;
648 QString mRuleKey;
649};
650
651class QgsImageFetcher;
652
660{
661 Q_OBJECT
662
663 public:
664
670 QgsWmsLegendNode( QgsLayerTreeLayer *nodeLayer, QObject *parent SIP_TRANSFERTHIS = nullptr );
671
673
674 QVariant data( int role ) const override;
675
676 QSizeF drawSymbol( const QgsLegendSettings &settings, ItemContext *ctx, double itemHeight ) const override;
677
678 QJsonObject exportSymbolToJson( const QgsLegendSettings &settings, const QgsRenderContext &context ) const override;
679
680 void invalidateMapBasedData() override;
681
682 private slots:
683
684 void getLegendGraphicFinished( const QImage & );
685 void getLegendGraphicErrored( const QString & );
686 void getLegendGraphicProgress( qint64, qint64 );
687
688 private:
689
690 // Lazily initializes mImage
691 QImage getLegendGraphic() const;
692
693 QImage renderMessage( const QString &msg ) const;
694
695 QImage mImage;
696
697 bool mValid;
698
699 mutable std::unique_ptr<QgsImageFetcher> mFetcher;
700};
701
702
709{
710 Q_OBJECT
711
712 public:
714 QgsDataDefinedSizeLegendNode( QgsLayerTreeLayer *nodeLayer, const QgsDataDefinedSizeLegend &settings, QObject *parent SIP_TRANSFERTHIS = nullptr );
716
717 QVariant data( int role ) const override;
718
719 ItemMetrics draw( const QgsLegendSettings &settings, ItemContext *ctx ) override;
720
721 private:
722 void cacheImage() const;
723 QgsDataDefinedSizeLegend *mSettings = nullptr;
724 mutable QImage mImage;
725};
726
733{
734 Q_OBJECT
735 public:
736
743 QgsVectorLabelLegendNode( QgsLayerTreeLayer *nodeLayer, const QgsPalLayerSettings &labelSettings, QObject *parent = nullptr );
744 ~QgsVectorLabelLegendNode() override;
745
751 QVariant data( int role ) const override;
752
760 QSizeF drawSymbol( const QgsLegendSettings &settings, ItemContext *ctx, double itemHeight ) const override;
761
768 QJsonObject exportSymbolToJson( const QgsLegendSettings &settings, const QgsRenderContext &context ) const override;
769
770 private:
771 QgsPalLayerSettings mLabelSettings;
772 QSizeF drawSymbol( const QgsLegendSettings &settings, const QgsRenderContext &renderContext, double xOffset = 0.0, double yOffset = 0.0 ) const;
773 void textWidthHeight( double &width, double &height, QgsRenderContext &ctx, const QgsTextFormat &textFormat, const QStringList &textLines ) const;
774};
775
776
777#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
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:3366
#define Q_NOWARN_DEPRECATED_PUSH
Definition: qgis.h:3365
#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.