QGIS API Documentation  3.20.0-Odense (decaadbb31)
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 #include "qgspallabeling.h"
31 
32 class QgsLayerTreeLayer;
33 class QgsLayerTreeModel;
34 class QgsLegendSettings;
35 class QgsMapSettings;
36 class QgsSymbol;
37 class QgsRenderContext;
38 
49 class CORE_EXPORT QgsLayerTreeModelLegendNode : public QObject
50 {
51 #ifdef SIP_RUN
52 #include "qgscolorramplegendnode.h"
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;
73  SIP_END
74 #endif
75 
76  Q_OBJECT
77 
78  public:
79 
82  {
83  RuleKeyRole = Qt::UserRole,
86  };
87 
89  enum NodeTypes
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 
168  struct ItemContext
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 
237  struct ItemMetrics
238  {
239  QSizeF symbolSize;
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:
314  void dataChanged();
315 
321  void sizeChanged();
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;
334  QgsLegendPatchShape mPatchShape;
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 );
374 
375  Qt::ItemFlags flags() const override;
376  QVariant data( int role ) const override;
377  bool setData( const QVariant &value, int role ) override;
378 
379  QSizeF drawSymbol( const QgsLegendSettings &settings, ItemContext *ctx, double itemHeight ) const override;
380 
381  QJsonObject exportSymbolToJson( const QgsLegendSettings &settings, const QgsRenderContext &context ) const override;
382 
383  void setEmbeddedInParent( bool embedded ) override;
384 
385  void setUserLabel( const QString &userLabel ) override { mUserLabel = userLabel; updateLabel(); }
386 
387  bool isScaleOK( double scale ) const override { return mItem.isScaleOK( scale ); }
388 
389  void invalidateMapBasedData() override;
390 
395  void setIconSize( QSize sz ) { mIconSize = sz; }
397  QSize iconSize() const { return mIconSize; }
398 
406  QSize minimumIconSize() const;
407 
414  QSize minimumIconSize( QgsRenderContext *context ) const;
415 
421  const QgsSymbol *symbol() const;
422 
430  void setSymbol( QgsSymbol *symbol SIP_TRANSFER );
431 
436  QString textOnSymbolLabel() const { return mTextOnSymbolLabel; }
437 
442  void setTextOnSymbolLabel( const QString &label ) { mTextOnSymbolLabel = label; }
443 
448  QgsTextFormat textOnSymbolTextFormat() const { return mTextOnSymbolTextFormat; }
449 
454  void setTextOnSymbolTextFormat( const QgsTextFormat &format ) { mTextOnSymbolTextFormat = format; }
455 
460  QString symbolLabel() const;
461 
468  QgsLegendPatchShape patchShape() const;
469 
476  void setPatchShape( const QgsLegendPatchShape &shape );
477 
487  QgsSymbol *customSymbol() const;
488 
500  void setCustomSymbol( QgsSymbol *symbol SIP_TRANSFER );
501 
508  QString evaluateLabel( const QgsExpressionContext &context = QgsExpressionContext(), const QString &label = QString() );
509 
510  private:
511  void updateLabel();
512 
513  private:
514  QgsLegendSymbolItem mItem;
515  mutable QPixmap mPixmap; // cached symbol preview
516  QString mLabel;
517  bool mSymbolUsesMapUnits;
518 
519  QSize mIconSize;
520 
521  QString mTextOnSymbolLabel;
522  QgsTextFormat mTextOnSymbolTextFormat;
523 
524  std::unique_ptr< QgsSymbol > mCustomSymbol;
525 
526  // ident the symbol icon to make it look like a tree structure
527  static const int INDENT_SIZE = 20;
528 
533  QgsExpressionContextScope *createSymbolScope() const SIP_FACTORY;
534 
535 };
536 
537 
545 {
546  Q_OBJECT
547 
548  public:
549 
558  QgsSimpleLegendNode( QgsLayerTreeLayer *nodeLayer, const QString &label, const QIcon &icon = QIcon(), QObject *parent SIP_TRANSFERTHIS = nullptr, const QString &key = QString() );
559 
560  QVariant data( int role ) const override;
561 
562  private:
563  QString mLabel;
564  QString mId;
565  QIcon mIcon;
566  QString mKey;
567 };
568 
569 
577 {
578  Q_OBJECT
579 
580  public:
581 
588  QgsImageLegendNode( QgsLayerTreeLayer *nodeLayer, const QImage &img, QObject *parent SIP_TRANSFERTHIS = nullptr );
589 
590  QVariant data( int role ) const override;
591 
592  QSizeF drawSymbol( const QgsLegendSettings &settings, ItemContext *ctx, double itemHeight ) const override;
593 
594  QJsonObject exportSymbolToJson( const QgsLegendSettings &settings, const QgsRenderContext &context ) const override;
595 
596  private:
597  QImage mImage;
598 };
599 
607 {
608  Q_OBJECT
609 
610  public:
611 
621  QgsRasterSymbolLegendNode( QgsLayerTreeLayer *nodeLayer, const QColor &color, const QString &label, QObject *parent SIP_TRANSFERTHIS = nullptr, bool isCheckable = false, const QString &ruleKey = QString() );
622 
623  Qt::ItemFlags flags() const override;
624  QVariant data( int role ) const override;
625  bool setData( const QVariant &value, int role ) override;
626  QSizeF drawSymbol( const QgsLegendSettings &settings, ItemContext *ctx, double itemHeight ) const override;
627  QJsonObject exportSymbolToJson( const QgsLegendSettings &settings, const QgsRenderContext &context ) const override;
628 
634  QString ruleKey() const { return mRuleKey; }
635 
641  bool isCheckable() const { return mCheckable; }
642 
643  private:
644  QColor mColor;
645  QString mLabel;
646  bool mCheckable = false;
647  QString mRuleKey;
648 };
649 
650 class QgsImageFetcher;
651 
659 {
660  Q_OBJECT
661 
662  public:
663 
669  QgsWmsLegendNode( QgsLayerTreeLayer *nodeLayer, QObject *parent SIP_TRANSFERTHIS = nullptr );
670 
671  ~QgsWmsLegendNode() override;
672 
673  QVariant data( int role ) const override;
674 
675  QSizeF drawSymbol( const QgsLegendSettings &settings, ItemContext *ctx, double itemHeight ) const override;
676 
677  QJsonObject exportSymbolToJson( const QgsLegendSettings &settings, const QgsRenderContext &context ) const override;
678 
679  void invalidateMapBasedData() override;
680 
681  private slots:
682 
683  void getLegendGraphicFinished( const QImage & );
684  void getLegendGraphicErrored( const QString & );
685  void getLegendGraphicProgress( qint64, qint64 );
686 
687  private:
688 
689  // Lazily initializes mImage
690  QImage getLegendGraphic() const;
691 
692  QImage renderMessage( const QString &msg ) const;
693 
694  QImage mImage;
695 
696  bool mValid;
697 
698  mutable std::unique_ptr<QgsImageFetcher> mFetcher;
699 };
700 
701 
708 {
709  Q_OBJECT
710 
711  public:
713  QgsDataDefinedSizeLegendNode( QgsLayerTreeLayer *nodeLayer, const QgsDataDefinedSizeLegend &settings, QObject *parent SIP_TRANSFERTHIS = nullptr );
714  ~QgsDataDefinedSizeLegendNode() override;
715 
716  QVariant data( int role ) const override;
717 
718  ItemMetrics draw( const QgsLegendSettings &settings, ItemContext *ctx ) override;
719 
720  private:
721  void cacheImage() const;
722  QgsDataDefinedSizeLegend *mSettings = nullptr;
723  mutable QImage mImage;
724 };
725 
732 {
733  public:
734 
741  QgsVectorLabelLegendNode( QgsLayerTreeLayer *nodeLayer, const QgsPalLayerSettings &labelSettings, QObject *parent = nullptr );
742  ~QgsVectorLabelLegendNode() override;
743 
749  QVariant data( int role ) const override;
750 
758  QSizeF drawSymbol( const QgsLegendSettings &settings, ItemContext *ctx, double itemHeight ) const override;
759 
766  QJsonObject exportSymbolToJson( const QgsLegendSettings &settings, const QgsRenderContext &context ) const override;
767 
768  private:
769  QgsPalLayerSettings mLabelSettings;
770  QSizeF drawSymbol( const QgsLegendSettings &settings, const QgsRenderContext &renderContext, double xOffset = 0.0, double yOffset = 0.0 ) const;
771  void textWidthHeight( double &width, double &height, QgsRenderContext &ctx, const QgsTextFormat &textFormat, const QStringList &textLines ) const;
772 };
773 
774 
775 #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.
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:38
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:1080
#define Q_NOWARN_DEPRECATED_PUSH
Definition: qgis.h:1079
#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.