QGIS API Documentation  3.16.0-Hannover (43b64b13f3)
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 
26 #include "qgis_core.h"
27 #include "qgis_sip.h"
28 
29 #include "qgsrasterdataprovider.h" // for QgsImageFetcher dtor visibility
30 #include "qgsexpressioncontext.h"
31 #include "qgslegendpatchshape.h"
32 
33 class QgsLayerTreeLayer;
34 class QgsLayerTreeModel;
35 class QgsLegendSettings;
36 class QgsMapSettings;
37 class QgsSymbol;
38 class QgsRenderContext;
39 
50 class CORE_EXPORT QgsLayerTreeModelLegendNode : public QObject
51 {
52  Q_OBJECT
53 #ifdef SIP_RUN
55  if ( qobject_cast<QgsSymbolLegendNode *> ( sipCpp ) )
56  sipType = sipType_QgsSymbolLegendNode;
57  else
58  sipType = 0;
59  SIP_END
60 #endif
61  public:
62 
64  {
65  RuleKeyRole = Qt::UserRole,
66  ParentRuleKeyRole
67  };
68 
70  QgsLayerTreeLayer *layerNode() const { return mLayerNode; }
71 
73  QgsLayerTreeModel *model() const;
74 
76  virtual Qt::ItemFlags flags() const;
77 
79  virtual QVariant data( int role ) const = 0;
80 
82  virtual bool setData( const QVariant &value, int role );
83 
84  virtual bool isEmbeddedInParent() const { return mEmbeddedInParent; }
85  virtual void setEmbeddedInParent( bool embedded ) { mEmbeddedInParent = embedded; }
86 
87  virtual QString userLabel() const { return mUserLabel; }
88  virtual void setUserLabel( const QString &userLabel ) { mUserLabel = userLabel; }
89 
99  virtual QSizeF userPatchSize() const;
100 
110  virtual void setUserPatchSize( QSizeF size );
111 
118  virtual void setColumnBreak( bool breakBeforeNode ) { mColumnBreakBeforeNode = breakBeforeNode; }
119 
126  virtual bool columnBreak() const { return mColumnBreakBeforeNode; }
127 
128  virtual bool isScaleOK( double scale ) const { Q_UNUSED( scale ) return true; }
129 
134  virtual void invalidateMapBasedData() {}
135 
136  struct ItemContext
137  {
138  Q_NOWARN_DEPRECATED_PUSH //because of deprecated members
139  ItemContext() = default;
141 
143  QgsRenderContext *context = nullptr;
145  QPainter *painter = nullptr;
146 
151  Q_DECL_DEPRECATED QPointF point;
152 
157  Q_DECL_DEPRECATED double labelXOffset = 0.0;
158 
163  double top = 0.0;
164 
171  double columnLeft = 0.0;
172 
179  double columnRight = 0.0;
180 
186  double maxSiblingSymbolWidth = 0.0;
187 
194 
202  QSizeF patchSize;
203  };
204 
205  struct ItemMetrics
206  {
207  QSizeF symbolSize;
208  QSizeF labelSize;
209  };
210 
217  virtual ItemMetrics draw( const QgsLegendSettings &settings, ItemContext *ctx );
218 
226  QJsonObject exportToJson( const QgsLegendSettings &settings, const QgsRenderContext &context );
227 
235  virtual QSizeF drawSymbol( const QgsLegendSettings &settings, ItemContext *ctx, double itemHeight ) const;
236 
243  virtual QJsonObject exportSymbolToJson( const QgsLegendSettings &settings, const QgsRenderContext &context ) const;
244 
252  virtual QSizeF drawSymbolText( const QgsLegendSettings &settings, ItemContext *ctx, QSizeF symbolSize ) const;
253 
254  signals:
256  void dataChanged();
257 
263  void sizeChanged();
264 
265  protected:
267  explicit QgsLayerTreeModelLegendNode( QgsLayerTreeLayer *nodeL, QObject *parent SIP_TRANSFERTHIS = nullptr );
268 
270  QgsRenderContext *createTemporaryRenderContext() const SIP_FACTORY;
271 
272  protected:
273  QgsLayerTreeLayer *mLayerNode = nullptr;
274  bool mEmbeddedInParent;
275  QString mUserLabel;
276  QgsLegendPatchShape mPatchShape;
277  QSizeF mUserSize;
278  bool mColumnBreakBeforeNode = false;
279 };
280 
281 #include "qgslegendsymbolitem.h"
282 #include "qgstextformat.h"
283 
292 {
293  Q_OBJECT
294 
295 
296  public:
297 
304  QgsSymbolLegendNode( QgsLayerTreeLayer *nodeLayer, const QgsLegendSymbolItem &item, QObject *parent SIP_TRANSFERTHIS = nullptr );
305 
306  Qt::ItemFlags flags() const override;
307  QVariant data( int role ) const override;
308  bool setData( const QVariant &value, int role ) override;
309 
310  QSizeF drawSymbol( const QgsLegendSettings &settings, ItemContext *ctx, double itemHeight ) const override;
311 
312  QJsonObject exportSymbolToJson( const QgsLegendSettings &settings, const QgsRenderContext &context ) const override;
313 
314  void setEmbeddedInParent( bool embedded ) override;
315 
316  void setUserLabel( const QString &userLabel ) override { mUserLabel = userLabel; updateLabel(); }
317 
318  bool isScaleOK( double scale ) const override { return mItem.isScaleOK( scale ); }
319 
320  void invalidateMapBasedData() override;
321 
326  void setIconSize( QSize sz ) { mIconSize = sz; }
328  QSize iconSize() const { return mIconSize; }
329 
337  QSize minimumIconSize() const;
338 
345  QSize minimumIconSize( QgsRenderContext *context ) const;
346 
352  const QgsSymbol *symbol() const;
353 
361  void setSymbol( QgsSymbol *symbol SIP_TRANSFER );
362 
367  QString textOnSymbolLabel() const { return mTextOnSymbolLabel; }
368 
373  void setTextOnSymbolLabel( const QString &label ) { mTextOnSymbolLabel = label; }
374 
379  QgsTextFormat textOnSymbolTextFormat() const { return mTextOnSymbolTextFormat; }
380 
385  void setTextOnSymbolTextFormat( const QgsTextFormat &format ) { mTextOnSymbolTextFormat = format; }
386 
391  QString symbolLabel() const;
392 
399  QgsLegendPatchShape patchShape() const;
400 
407  void setPatchShape( const QgsLegendPatchShape &shape );
408 
418  QgsSymbol *customSymbol() const;
419 
431  void setCustomSymbol( QgsSymbol *symbol SIP_TRANSFER );
432 
439  QString evaluateLabel( const QgsExpressionContext &context = QgsExpressionContext(), const QString &label = QString() );
440 
441  public slots:
442 
449  void checkAllItems();
450 
457  void uncheckAllItems();
458 
465  void toggleAllItems();
466 
467  private:
468  void updateLabel();
469 
470  private:
471  QgsLegendSymbolItem mItem;
472  mutable QPixmap mPixmap; // cached symbol preview
473  QString mLabel;
474  bool mSymbolUsesMapUnits;
475  QSize mIconSize;
476 
477  QString mTextOnSymbolLabel;
478  QgsTextFormat mTextOnSymbolTextFormat;
479 
480  std::unique_ptr< QgsSymbol > mCustomSymbol;
481 
482  // ident the symbol icon to make it look like a tree structure
483  static const int INDENT_SIZE = 20;
484 
489  QgsExpressionContextScope *createSymbolScope() const SIP_FACTORY;
490 
495  void checkAll( bool state );
496 };
497 
498 
506 {
507  Q_OBJECT
508 
509  public:
510 
519  QgsSimpleLegendNode( QgsLayerTreeLayer *nodeLayer, const QString &label, const QIcon &icon = QIcon(), QObject *parent SIP_TRANSFERTHIS = nullptr, const QString &key = QString() );
520 
521  QVariant data( int role ) const override;
522 
523  private:
524  QString mLabel;
525  QString mId;
526  QIcon mIcon;
527  QString mKey;
528 };
529 
530 
538 {
539  Q_OBJECT
540 
541  public:
542 
549  QgsImageLegendNode( QgsLayerTreeLayer *nodeLayer, const QImage &img, QObject *parent SIP_TRANSFERTHIS = nullptr );
550 
551  QVariant data( int role ) const override;
552 
553  QSizeF drawSymbol( const QgsLegendSettings &settings, ItemContext *ctx, double itemHeight ) const override;
554 
555  QJsonObject exportSymbolToJson( const QgsLegendSettings &settings, const QgsRenderContext &context ) const override;
556 
557  private:
558  QImage mImage;
559 };
560 
568 {
569  Q_OBJECT
570 
571  public:
572 
580  QgsRasterSymbolLegendNode( QgsLayerTreeLayer *nodeLayer, const QColor &color, const QString &label, QObject *parent SIP_TRANSFERTHIS = nullptr );
581 
582  QVariant data( int role ) const override;
583 
584  QSizeF drawSymbol( const QgsLegendSettings &settings, ItemContext *ctx, double itemHeight ) const override;
585 
586  QJsonObject exportSymbolToJson( const QgsLegendSettings &settings, const QgsRenderContext &context ) const override;
587 
588  private:
589  QColor mColor;
590  QString mLabel;
591 };
592 
593 class QgsImageFetcher;
594 
602 {
603  Q_OBJECT
604 
605  public:
606 
612  QgsWmsLegendNode( QgsLayerTreeLayer *nodeLayer, QObject *parent SIP_TRANSFERTHIS = nullptr );
613 
614  QVariant data( int role ) const override;
615 
616  QSizeF drawSymbol( const QgsLegendSettings &settings, ItemContext *ctx, double itemHeight ) const override;
617 
618  QJsonObject exportSymbolToJson( const QgsLegendSettings &settings, const QgsRenderContext &context ) const override;
619 
620  void invalidateMapBasedData() override;
621 
622  private slots:
623 
624  void getLegendGraphicFinished( const QImage & );
625  void getLegendGraphicErrored( const QString & );
626  void getLegendGraphicProgress( qint64, qint64 );
627 
628  private:
629 
630  // Lazily initializes mImage
631  QImage getLegendGraphic() const;
632 
633  QImage renderMessage( const QString &msg ) const;
634 
635  QImage mImage;
636 
637  bool mValid;
638 
639  mutable std::unique_ptr<QgsImageFetcher> mFetcher;
640 };
641 
642 
649 {
650  Q_OBJECT
651 
652  public:
654  QgsDataDefinedSizeLegendNode( QgsLayerTreeLayer *nodeLayer, const QgsDataDefinedSizeLegend &settings, QObject *parent SIP_TRANSFERTHIS = nullptr );
655  ~QgsDataDefinedSizeLegendNode() override;
656 
657  QVariant data( int role ) const override;
658 
659  ItemMetrics draw( const QgsLegendSettings &settings, ItemContext *ctx ) override;
660 
661  private:
662  void cacheImage() const;
663  QgsDataDefinedSizeLegend *mSettings = nullptr;
664  mutable QImage mImage;
665 };
666 
667 #endif // QGSLAYERTREEMODELLEGENDNODE_H
QgsExpressionContext
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
Definition: qgsexpressioncontext.h:370
QgsImageFetcher
Handles asynchronous download of images.
Definition: qgsrasterdataprovider.h:58
QgsLayerTreeModelLegendNode::isEmbeddedInParent
virtual bool isEmbeddedInParent() const
Definition: qgslayertreemodellegendnode.h:84
QgsSymbolLegendNode::textOnSymbolTextFormat
QgsTextFormat textOnSymbolTextFormat() const
Returns text format of the label to be shown on top of the symbol.
Definition: qgslayertreemodellegendnode.h:379
QgsLayerTreeModelLegendNode::invalidateMapBasedData
virtual void invalidateMapBasedData()
Notification from model that information from associated map view has changed.
Definition: qgslayertreemodellegendnode.h:134
QgsLayerTreeModelLegendNode::data
virtual QVariant data(int role) const =0
Returns data associated with the item. Must be implemented in derived class.
QgsLayerTreeModelLegendNode::ItemContext::patchSize
QSizeF patchSize
Symbol patch size to render for the node.
Definition: qgslayertreemodellegendnode.h:202
QgsSimpleLegendNode
Implementation of legend node interface for displaying arbitrary label with icon.
Definition: qgslayertreemodellegendnode.h:506
QgsLayerTreeModelLegendNode::ItemContext::point
Q_DECL_DEPRECATED QPointF point
Top-left corner of the legend item.
Definition: qgslayertreemodellegendnode.h:151
QgsDataDefinedSizeLegend
Object that keeps configuration of appearance of marker symbol's data-defined size in legend.
Definition: qgsdatadefinedsizelegend.h:42
QgsRenderContext
Contains information about the context of a rendering operation.
Definition: qgsrendercontext.h:58
QgsSymbolLegendNode::isScaleOK
bool isScaleOK(double scale) const override
Definition: qgslayertreemodellegendnode.h:318
QgsLayerTreeModelLegendNode::ItemContext::patchShape
QgsLegendPatchShape patchShape
The patch shape to render for the node.
Definition: qgslayertreemodellegendnode.h:193
QgsLayerTreeModel
The QgsLayerTreeModel class is model implementation for Qt item views framework.
Definition: qgslayertreemodel.h:54
QgsSymbol
Abstract base class for all rendered symbols.
Definition: qgssymbol.h:64
QgsDataDefinedSizeLegendNode
Produces legend node with a marker symbol.
Definition: qgslayertreemodellegendnode.h:649
QgsLayerTreeModelLegendNode::LegendNodeRoles
LegendNodeRoles
Definition: qgslayertreemodellegendnode.h:64
QgsLegendPatchShape
Represents a patch shape for use in map legends.
Definition: qgslegendpatchshape.h:31
SIP_FACTORY
#define SIP_FACTORY
Definition: qgis_sip.h:76
QgsLayerTreeModelLegendNode::exportSymbolToJson
virtual QJsonObject exportSymbolToJson(const QgsLegendSettings &settings, const QgsRenderContext &context) const
Adds a symbol in base64 string within a JSON object with the key "icon".
Definition: qgslayertreemodellegendnode.cpp:144
QgsLayerTreeModelLegendNode::userLabel
virtual QString userLabel() const
Definition: qgslayertreemodellegendnode.h:87
QgsLayerTreeModelLegendNode::sizeChanged
void sizeChanged()
Emitted when the size of this node changes.
QgsLayerTreeModelLegendNode::flags
virtual Qt::ItemFlags flags() const
Returns item flags associated with the item. Default implementation returns Qt::ItemIsEnabled.
Definition: qgslayertreemodellegendnode.cpp:51
QgsLayerTreeModelLegendNode::draw
virtual ItemMetrics draw(const QgsLegendSettings &settings, ItemContext *ctx)
Entry point called from QgsLegendRenderer to do the rendering.
Definition: qgslayertreemodellegendnode.cpp:80
Q_NOWARN_DEPRECATED_POP
#define Q_NOWARN_DEPRECATED_POP
Definition: qgis.h:797
SIP_CONVERT_TO_SUBCLASS_CODE
#define SIP_CONVERT_TO_SUBCLASS_CODE(code)
Definition: qgis_sip.h:177
QgsTextFormat
Container for all settings relating to text rendering.
Definition: qgstextformat.h:40
QgsLayerTreeModelLegendNode::ItemMetrics::labelSize
QSizeF labelSize
Definition: qgslayertreemodellegendnode.h:208
QgsLegendSymbolItem
The class stores information about one class/rule of a vector layer renderer in a unified way that ca...
Definition: qgslegendsymbolitem.h:37
qgsexpressioncontext.h
QgsLayerTreeLayer
Layer tree node points to a map layer.
Definition: qgslayertreelayer.h:44
QgsSymbolLegendNode::setTextOnSymbolTextFormat
void setTextOnSymbolTextFormat(const QgsTextFormat &format)
Sets format of text to be shown on top of the symbol.
Definition: qgslayertreemodellegendnode.h:385
qgis_sip.h
QgsSymbolLegendNode::setIconSize
void setIconSize(QSize sz)
Set the icon size.
Definition: qgslayertreemodellegendnode.h:326
SIP_TRANSFER
#define SIP_TRANSFER
Definition: qgis_sip.h:36
QgsSymbolLegendNode::setTextOnSymbolLabel
void setTextOnSymbolLabel(const QString &label)
Sets label of text to be shown on top of the symbol.
Definition: qgslayertreemodellegendnode.h:373
QgsLegendSettings
The QgsLegendSettings class stores the appearance and layout settings for legend drawing with QgsLege...
Definition: qgslegendsettings.h:39
QgsWmsLegendNode
Implementation of legend node interface for displaying WMS legend entries.
Definition: qgslayertreemodellegendnode.h:602
qgslegendpatchshape.h
QgsLayerTreeModelLegendNode::ItemContext
Definition: qgslayertreemodellegendnode.h:137
QgsLayerTreeModelLegendNode::ItemContext::ItemContext
Q_NOWARN_DEPRECATED_PUSH ItemContext()=default
QgsLayerTreeModelLegendNode::setEmbeddedInParent
virtual void setEmbeddedInParent(bool embedded)
Definition: qgslayertreemodellegendnode.h:85
QgsLayerTreeModelLegendNode::ItemMetrics::symbolSize
QSizeF symbolSize
Definition: qgslayertreemodellegendnode.h:207
QgsLayerTreeModelLegendNode::columnBreak
virtual bool columnBreak() const
Returns whether a forced column break should occur before the node.
Definition: qgslayertreemodellegendnode.h:126
QgsExpressionContextScope
Single scope for storing variables and functions for use within a QgsExpressionContext.
Definition: qgsexpressioncontext.h:112
QgsLayerTreeModelLegendNode::setData
virtual bool setData(const QVariant &value, int role)
Sets some data associated with the item. Default implementation does nothing and returns false.
Definition: qgslayertreemodellegendnode.cpp:56
QgsLayerTreeModelLegendNode::ItemMetrics
Definition: qgslayertreemodellegendnode.h:206
QgsLayerTreeModelLegendNode::setUserLabel
virtual void setUserLabel(const QString &userLabel)
Definition: qgslayertreemodellegendnode.h:88
QgsLayerTreeModelLegendNode::layerNode
QgsLayerTreeLayer * layerNode() const
Returns pointer to the parent layer node.
Definition: qgslayertreemodellegendnode.h:70
QgsSymbolLegendNode::setUserLabel
void setUserLabel(const QString &userLabel) override
Definition: qgslayertreemodellegendnode.h:316
QgsImageLegendNode
Implementation of legend node interface for displaying arbitrary raster image.
Definition: qgslayertreemodellegendnode.h:538
QgsSymbolLegendNode::textOnSymbolLabel
QString textOnSymbolLabel() const
Returns label of text to be shown on top of the symbol.
Definition: qgslayertreemodellegendnode.h:367
QgsRasterSymbolLegendNode
Implementation of legend node interface for displaying raster legend entries.
Definition: qgslayertreemodellegendnode.h:568
qgstextformat.h
QgsLayerTreeModelLegendNode::drawSymbol
virtual QSizeF drawSymbol(const QgsLegendSettings &settings, ItemContext *ctx, double itemHeight) const
Draws symbol on the left side of the item.
Definition: qgslayertreemodellegendnode.cpp:104
QgsMapSettings
The QgsMapSettings class contains configuration for rendering of the map.
Definition: qgsmapsettings.h:88
QgsLayerTreeModelLegendNode::isScaleOK
virtual bool isScaleOK(double scale) const
Definition: qgslayertreemodellegendnode.h:128
QgsSymbolLegendNode
Implementation of legend node interface for displaying preview of vector symbols and their labels and...
Definition: qgslayertreemodellegendnode.h:292
Q_NOWARN_DEPRECATED_PUSH
#define Q_NOWARN_DEPRECATED_PUSH
Definition: qgis.h:796
SIP_END
#define SIP_END
Definition: qgis_sip.h:194
QgsSymbolLegendNode::iconSize
QSize iconSize() const
Definition: qgslayertreemodellegendnode.h:328
SIP_TRANSFERTHIS
#define SIP_TRANSFERTHIS
Definition: qgis_sip.h:53
QgsLayerTreeModelLegendNode::dataChanged
void dataChanged()
Emitted on internal data change so the layer tree model can forward the signal to views.
qgsrasterdataprovider.h
QgsLayerTreeModelLegendNode::setColumnBreak
virtual void setColumnBreak(bool breakBeforeNode)
Sets whether a forced column break should occur before the node.
Definition: qgslayertreemodellegendnode.h:118
QgsLayerTreeModelLegendNode
The QgsLegendRendererItem class is abstract interface for legend items returned from QgsMapLayerLegen...
Definition: qgslayertreemodellegendnode.h:51