QGIS API Documentation  3.18.1-Zürich (202f1bf7e5)
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 
28 #include "qgsexpressioncontext.h"
29 #include "qgslegendpatchshape.h"
30 
31 class QgsLayerTreeLayer;
32 class QgsLayerTreeModel;
33 class QgsLegendSettings;
34 class QgsMapSettings;
35 class QgsSymbol;
36 class QgsRenderContext;
37 
48 class CORE_EXPORT QgsLayerTreeModelLegendNode : public QObject
49 {
50 #ifdef SIP_RUN
51 #include "qgscolorramplegendnode.h"
52 #endif
53 
54 #ifdef SIP_RUN
56  if ( qobject_cast<QgsSymbolLegendNode *> ( sipCpp ) )
57  sipType = sipType_QgsSymbolLegendNode;
58  else if ( qobject_cast<QgsDataDefinedSizeLegendNode *> ( sipCpp ) )
59  sipType = sipType_QgsDataDefinedSizeLegendNode;
60  else if ( qobject_cast<QgsImageLegendNode *> ( sipCpp ) )
61  sipType = sipType_QgsImageLegendNode;
62  else if ( qobject_cast<QgsRasterSymbolLegendNode *> ( sipCpp ) )
63  sipType = sipType_QgsRasterSymbolLegendNode;
64  else if ( qobject_cast<QgsSimpleLegendNode *> ( sipCpp ) )
65  sipType = sipType_QgsSimpleLegendNode;
66  else if ( qobject_cast<QgsWmsLegendNode *> ( sipCpp ) )
67  sipType = sipType_QgsWmsLegendNode;
68  else if ( qobject_cast<QgsColorRampLegendNode *> ( sipCpp ) )
69  sipType = sipType_QgsColorRampLegendNode;
70  else
71  sipType = 0;
72  SIP_END
73 #endif
74 
75  Q_OBJECT
76 
77  public:
78 
81  {
82  RuleKeyRole = Qt::UserRole,
85  };
86 
88  enum NodeTypes
89  {
98  };
99 
101  QgsLayerTreeLayer *layerNode() const { return mLayerNode; }
102 
104  QgsLayerTreeModel *model() const;
105 
107  virtual Qt::ItemFlags flags() const;
108 
110  virtual QVariant data( int role ) const = 0;
111 
113  virtual bool setData( const QVariant &value, int role );
114 
115  virtual bool isEmbeddedInParent() const { return mEmbeddedInParent; }
116  virtual void setEmbeddedInParent( bool embedded ) { mEmbeddedInParent = embedded; }
117 
118  virtual QString userLabel() const { return mUserLabel; }
119  virtual void setUserLabel( const QString &userLabel ) { mUserLabel = userLabel; }
120 
130  virtual QSizeF userPatchSize() const;
131 
141  virtual void setUserPatchSize( QSizeF size );
142 
149  virtual void setColumnBreak( bool breakBeforeNode ) { mColumnBreakBeforeNode = breakBeforeNode; }
150 
157  virtual bool columnBreak() const { return mColumnBreakBeforeNode; }
158 
159  virtual bool isScaleOK( double scale ) const { Q_UNUSED( scale ) return true; }
160 
165  virtual void invalidateMapBasedData() {}
166 
167  struct ItemContext
168  {
169  Q_NOWARN_DEPRECATED_PUSH //because of deprecated members
170  ItemContext() = default;
172 
174  QgsRenderContext *context = nullptr;
176  QPainter *painter = nullptr;
177 
182  Q_DECL_DEPRECATED QPointF point;
183 
188  Q_DECL_DEPRECATED double labelXOffset = 0.0;
189 
194  double top = 0.0;
195 
202  double columnLeft = 0.0;
203 
210  double columnRight = 0.0;
211 
217  double maxSiblingSymbolWidth = 0.0;
218 
225 
233  QSizeF patchSize;
234  };
235 
236  struct ItemMetrics
237  {
238  QSizeF symbolSize;
239  QSizeF labelSize;
240  };
241 
248  virtual ItemMetrics draw( const QgsLegendSettings &settings, ItemContext *ctx );
249 
257  QJsonObject exportToJson( const QgsLegendSettings &settings, const QgsRenderContext &context );
258 
266  virtual QSizeF drawSymbol( const QgsLegendSettings &settings, ItemContext *ctx, double itemHeight ) const;
267 
274  virtual QJsonObject exportSymbolToJson( const QgsLegendSettings &settings, const QgsRenderContext &context ) const;
275 
283  virtual QSizeF drawSymbolText( const QgsLegendSettings &settings, ItemContext *ctx, QSizeF symbolSize ) const;
284 
285  public slots:
286 
293  void checkAllItems();
294 
301  void uncheckAllItems();
302 
309  void toggleAllItems();
310 
311  signals:
313  void dataChanged();
314 
320  void sizeChanged();
321 
322  protected:
324  explicit QgsLayerTreeModelLegendNode( QgsLayerTreeLayer *nodeL, QObject *parent SIP_TRANSFERTHIS = nullptr );
325 
327  QgsRenderContext *createTemporaryRenderContext() const SIP_FACTORY;
328 
329  protected:
330  QgsLayerTreeLayer *mLayerNode = nullptr;
331  bool mEmbeddedInParent;
332  QString mUserLabel;
333  QgsLegendPatchShape mPatchShape;
334  QSizeF mUserSize;
335  bool mColumnBreakBeforeNode = false;
336 
337  private:
338 
343  void checkAll( bool state );
344 };
346 
347 #include "qgslegendsymbolitem.h"
348 #include "qgstextformat.h"
349 
358 {
359  Q_OBJECT
360 
361  public:
362 
363  static double MINIMUM_SIZE;
364  static double MAXIMUM_SIZE;
365 
372  QgsSymbolLegendNode( QgsLayerTreeLayer *nodeLayer, const QgsLegendSymbolItem &item, QObject *parent SIP_TRANSFERTHIS = nullptr );
373 
374  Qt::ItemFlags flags() const override;
375  QVariant data( int role ) const override;
376  bool setData( const QVariant &value, int role ) override;
377 
378  QSizeF drawSymbol( const QgsLegendSettings &settings, ItemContext *ctx, double itemHeight ) const override;
379 
380  QJsonObject exportSymbolToJson( const QgsLegendSettings &settings, const QgsRenderContext &context ) const override;
381 
382  void setEmbeddedInParent( bool embedded ) override;
383 
384  void setUserLabel( const QString &userLabel ) override { mUserLabel = userLabel; updateLabel(); }
385 
386  bool isScaleOK( double scale ) const override { return mItem.isScaleOK( scale ); }
387 
388  void invalidateMapBasedData() override;
389 
394  void setIconSize( QSize sz ) { mIconSize = sz; }
396  QSize iconSize() const { return mIconSize; }
397 
405  QSize minimumIconSize() const;
406 
413  QSize minimumIconSize( QgsRenderContext *context ) const;
414 
420  const QgsSymbol *symbol() const;
421 
429  void setSymbol( QgsSymbol *symbol SIP_TRANSFER );
430 
435  QString textOnSymbolLabel() const { return mTextOnSymbolLabel; }
436 
441  void setTextOnSymbolLabel( const QString &label ) { mTextOnSymbolLabel = label; }
442 
447  QgsTextFormat textOnSymbolTextFormat() const { return mTextOnSymbolTextFormat; }
448 
453  void setTextOnSymbolTextFormat( const QgsTextFormat &format ) { mTextOnSymbolTextFormat = format; }
454 
459  QString symbolLabel() const;
460 
467  QgsLegendPatchShape patchShape() const;
468 
475  void setPatchShape( const QgsLegendPatchShape &shape );
476 
486  QgsSymbol *customSymbol() const;
487 
499  void setCustomSymbol( QgsSymbol *symbol SIP_TRANSFER );
500 
507  QString evaluateLabel( const QgsExpressionContext &context = QgsExpressionContext(), const QString &label = QString() );
508 
509  private:
510  void updateLabel();
511 
512  private:
513  QgsLegendSymbolItem mItem;
514  mutable QPixmap mPixmap; // cached symbol preview
515  QString mLabel;
516  bool mSymbolUsesMapUnits;
517 
518  QSize mIconSize;
519 
520  QString mTextOnSymbolLabel;
521  QgsTextFormat mTextOnSymbolTextFormat;
522 
523  std::unique_ptr< QgsSymbol > mCustomSymbol;
524 
525  // ident the symbol icon to make it look like a tree structure
526  static const int INDENT_SIZE = 20;
527 
532  QgsExpressionContextScope *createSymbolScope() const SIP_FACTORY;
533 
534 };
535 
536 
544 {
545  Q_OBJECT
546 
547  public:
548 
557  QgsSimpleLegendNode( QgsLayerTreeLayer *nodeLayer, const QString &label, const QIcon &icon = QIcon(), QObject *parent SIP_TRANSFERTHIS = nullptr, const QString &key = QString() );
558 
559  QVariant data( int role ) const override;
560 
561  private:
562  QString mLabel;
563  QString mId;
564  QIcon mIcon;
565  QString mKey;
566 };
567 
568 
576 {
577  Q_OBJECT
578 
579  public:
580 
587  QgsImageLegendNode( QgsLayerTreeLayer *nodeLayer, const QImage &img, QObject *parent SIP_TRANSFERTHIS = nullptr );
588 
589  QVariant data( int role ) const override;
590 
591  QSizeF drawSymbol( const QgsLegendSettings &settings, ItemContext *ctx, double itemHeight ) const override;
592 
593  QJsonObject exportSymbolToJson( const QgsLegendSettings &settings, const QgsRenderContext &context ) const override;
594 
595  private:
596  QImage mImage;
597 };
598 
606 {
607  Q_OBJECT
608 
609  public:
610 
620  QgsRasterSymbolLegendNode( QgsLayerTreeLayer *nodeLayer, const QColor &color, const QString &label, QObject *parent SIP_TRANSFERTHIS = nullptr, bool isCheckable = false, const QString &ruleKey = QString() );
621 
622  Qt::ItemFlags flags() const override;
623  QVariant data( int role ) const override;
624  bool setData( const QVariant &value, int role ) override;
625  QSizeF drawSymbol( const QgsLegendSettings &settings, ItemContext *ctx, double itemHeight ) const override;
626  QJsonObject exportSymbolToJson( const QgsLegendSettings &settings, const QgsRenderContext &context ) const override;
627 
633  QString ruleKey() const { return mRuleKey; }
634 
640  bool isCheckable() const { return mCheckable; }
641 
642  private:
643  QColor mColor;
644  QString mLabel;
645  bool mCheckable = false;
646  QString mRuleKey;
647 };
648 
649 class QgsImageFetcher;
650 
658 {
659  Q_OBJECT
660 
661  public:
662 
668  QgsWmsLegendNode( QgsLayerTreeLayer *nodeLayer, QObject *parent SIP_TRANSFERTHIS = nullptr );
669 
670  ~QgsWmsLegendNode() override;
671 
672  QVariant data( int role ) const override;
673 
674  QSizeF drawSymbol( const QgsLegendSettings &settings, ItemContext *ctx, double itemHeight ) const override;
675 
676  QJsonObject exportSymbolToJson( const QgsLegendSettings &settings, const QgsRenderContext &context ) const override;
677 
678  void invalidateMapBasedData() override;
679 
680  private slots:
681 
682  void getLegendGraphicFinished( const QImage & );
683  void getLegendGraphicErrored( const QString & );
684  void getLegendGraphicProgress( qint64, qint64 );
685 
686  private:
687 
688  // Lazily initializes mImage
689  QImage getLegendGraphic() const;
690 
691  QImage renderMessage( const QString &msg ) const;
692 
693  QImage mImage;
694 
695  bool mValid;
696 
697  mutable std::unique_ptr<QgsImageFetcher> mFetcher;
698 };
699 
700 
707 {
708  Q_OBJECT
709 
710  public:
712  QgsDataDefinedSizeLegendNode( QgsLayerTreeLayer *nodeLayer, const QgsDataDefinedSizeLegend &settings, QObject *parent SIP_TRANSFERTHIS = nullptr );
713  ~QgsDataDefinedSizeLegendNode() override;
714 
715  QVariant data( int role ) const override;
716 
717  ItemMetrics draw( const QgsLegendSettings &settings, ItemContext *ctx ) override;
718 
719  private:
720  void cacheImage() const;
721  QgsDataDefinedSizeLegend *mSettings = nullptr;
722  mutable QImage mImage;
723 };
724 
725 #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.
QgsLayerTreeLayer * layerNode() const
Returns pointer to the parent layer 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.
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.
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:65
Container for all settings relating to text rendering.
Definition: qgstextformat.h:41
Implementation of legend node interface for displaying WMS legend entries.
~QgsWmsLegendNode() override
#define Q_NOWARN_DEPRECATED_POP
Definition: qgis.h:798
#define Q_NOWARN_DEPRECATED_PUSH
Definition: qgis.h:797
#define SIP_CONVERT_TO_SUBCLASS_CODE(code)
Definition: qgis_sip.h:177
#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:194
Q_DECLARE_METATYPE(QgsMeshTimeSettings)
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.