QGIS API Documentation  3.14.0-Pi (9f7028fd23)
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 ) { mUserSize = 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 
133  virtual void invalidateMapBasedData() {}
134 
135  struct ItemContext
136  {
137  Q_NOWARN_DEPRECATED_PUSH //because of deprecated members
138  ItemContext() = default;
140 
142  QgsRenderContext *context = nullptr;
144  QPainter *painter = nullptr;
145 
150  Q_DECL_DEPRECATED QPointF point;
151 
156  Q_DECL_DEPRECATED double labelXOffset = 0.0;
157 
162  double top = 0.0;
163 
170  double columnLeft = 0.0;
171 
178  double columnRight = 0.0;
179 
185  double maxSiblingSymbolWidth = 0.0;
186 
193 
201  QSizeF patchSize;
202  };
203 
204  struct ItemMetrics
205  {
206  QSizeF symbolSize;
207  QSizeF labelSize;
208  };
209 
216  virtual ItemMetrics draw( const QgsLegendSettings &settings, ItemContext *ctx );
217 
225  QJsonObject exportToJson( const QgsLegendSettings &settings, const QgsRenderContext &context );
226 
234  virtual QSizeF drawSymbol( const QgsLegendSettings &settings, ItemContext *ctx, double itemHeight ) const;
235 
242  virtual QJsonObject exportSymbolToJson( const QgsLegendSettings &settings, const QgsRenderContext &context ) const;
243 
251  virtual QSizeF drawSymbolText( const QgsLegendSettings &settings, ItemContext *ctx, QSizeF symbolSize ) const;
252 
253  signals:
255  void dataChanged();
256 
257  protected:
259  explicit QgsLayerTreeModelLegendNode( QgsLayerTreeLayer *nodeL, QObject *parent SIP_TRANSFERTHIS = nullptr );
260 
262  QgsRenderContext *createTemporaryRenderContext() const SIP_FACTORY;
263 
264  protected:
265  QgsLayerTreeLayer *mLayerNode = nullptr;
266  bool mEmbeddedInParent;
267  QString mUserLabel;
268  QgsLegendPatchShape mPatchShape;
269  QSizeF mUserSize;
270  bool mColumnBreakBeforeNode = false;
271 };
272 
273 #include "qgslegendsymbolitem.h"
274 #include "qgstextformat.h"
275 
284 {
285  Q_OBJECT
286 
287 
288  public:
289 
296  QgsSymbolLegendNode( QgsLayerTreeLayer *nodeLayer, const QgsLegendSymbolItem &item, QObject *parent SIP_TRANSFERTHIS = nullptr );
297 
298  Qt::ItemFlags flags() const override;
299  QVariant data( int role ) const override;
300  bool setData( const QVariant &value, int role ) override;
301 
302  QSizeF drawSymbol( const QgsLegendSettings &settings, ItemContext *ctx, double itemHeight ) const override;
303 
304  QJsonObject exportSymbolToJson( const QgsLegendSettings &settings, const QgsRenderContext &context ) const override;
305 
306  void setEmbeddedInParent( bool embedded ) override;
307 
308  void setUserLabel( const QString &userLabel ) override { mUserLabel = userLabel; updateLabel(); }
309 
310  bool isScaleOK( double scale ) const override { return mItem.isScaleOK( scale ); }
311 
312  void invalidateMapBasedData() override;
313 
318  void setIconSize( QSize sz ) { mIconSize = sz; }
320  QSize iconSize() const { return mIconSize; }
321 
329  QSize minimumIconSize() const;
330 
337  QSize minimumIconSize( QgsRenderContext *context ) const;
338 
344  const QgsSymbol *symbol() const;
345 
353  void setSymbol( QgsSymbol *symbol SIP_TRANSFER );
354 
359  QString textOnSymbolLabel() const { return mTextOnSymbolLabel; }
360 
365  void setTextOnSymbolLabel( const QString &label ) { mTextOnSymbolLabel = label; }
366 
371  QgsTextFormat textOnSymbolTextFormat() const { return mTextOnSymbolTextFormat; }
372 
377  void setTextOnSymbolTextFormat( const QgsTextFormat &format ) { mTextOnSymbolTextFormat = format; }
378 
383  QString symbolLabel() const;
384 
391  QgsLegendPatchShape patchShape() const;
392 
399  void setPatchShape( const QgsLegendPatchShape &shape );
400 
410  QgsSymbol *customSymbol() const;
411 
423  void setCustomSymbol( QgsSymbol *symbol SIP_TRANSFER );
424 
431  QString evaluateLabel( const QgsExpressionContext &context = QgsExpressionContext(), const QString &label = QString() );
432 
433  public slots:
434 
441  void checkAllItems();
442 
449  void uncheckAllItems();
450 
457  void toggleAllItems();
458 
459  private:
460  void updateLabel();
461 
462  private:
463  QgsLegendSymbolItem mItem;
464  mutable QPixmap mPixmap; // cached symbol preview
465  QString mLabel;
466  bool mSymbolUsesMapUnits;
467  QSize mIconSize;
468 
469  QString mTextOnSymbolLabel;
470  QgsTextFormat mTextOnSymbolTextFormat;
471 
472  std::unique_ptr< QgsSymbol > mCustomSymbol;
473 
474  // ident the symbol icon to make it look like a tree structure
475  static const int INDENT_SIZE = 20;
476 
481  QgsExpressionContextScope *createSymbolScope() const SIP_FACTORY;
482 
487  void checkAll( bool state );
488 };
489 
490 
498 {
499  Q_OBJECT
500 
501  public:
502 
511  QgsSimpleLegendNode( QgsLayerTreeLayer *nodeLayer, const QString &label, const QIcon &icon = QIcon(), QObject *parent SIP_TRANSFERTHIS = nullptr, const QString &key = QString() );
512 
513  QVariant data( int role ) const override;
514 
515  private:
516  QString mLabel;
517  QString mId;
518  QIcon mIcon;
519  QString mKey;
520 };
521 
522 
530 {
531  Q_OBJECT
532 
533  public:
534 
541  QgsImageLegendNode( QgsLayerTreeLayer *nodeLayer, const QImage &img, QObject *parent SIP_TRANSFERTHIS = nullptr );
542 
543  QVariant data( int role ) const override;
544 
545  QSizeF drawSymbol( const QgsLegendSettings &settings, ItemContext *ctx, double itemHeight ) const override;
546 
547  QJsonObject exportSymbolToJson( const QgsLegendSettings &settings, const QgsRenderContext &context ) const override;
548 
549  private:
550  QImage mImage;
551 };
552 
560 {
561  Q_OBJECT
562 
563  public:
564 
572  QgsRasterSymbolLegendNode( QgsLayerTreeLayer *nodeLayer, const QColor &color, const QString &label, QObject *parent SIP_TRANSFERTHIS = nullptr );
573 
574  QVariant data( int role ) const override;
575 
576  QSizeF drawSymbol( const QgsLegendSettings &settings, ItemContext *ctx, double itemHeight ) const override;
577 
578  QJsonObject exportSymbolToJson( const QgsLegendSettings &settings, const QgsRenderContext &context ) const override;
579 
580  private:
581  QColor mColor;
582  QString mLabel;
583 };
584 
585 class QgsImageFetcher;
586 
594 {
595  Q_OBJECT
596 
597  public:
598 
604  QgsWmsLegendNode( QgsLayerTreeLayer *nodeLayer, QObject *parent SIP_TRANSFERTHIS = nullptr );
605 
606  QVariant data( int role ) const override;
607 
608  QSizeF drawSymbol( const QgsLegendSettings &settings, ItemContext *ctx, double itemHeight ) const override;
609 
610  QJsonObject exportSymbolToJson( const QgsLegendSettings &settings, const QgsRenderContext &context ) const override;
611 
612  void invalidateMapBasedData() override;
613 
614  private slots:
615 
616  void getLegendGraphicFinished( const QImage & );
617  void getLegendGraphicErrored( const QString & );
618  void getLegendGraphicProgress( qint64, qint64 );
619 
620  private:
621 
622  // Lazily initializes mImage
623  QImage getLegendGraphic() const;
624 
625  QImage renderMessage( const QString &msg ) const;
626 
627  QImage mImage;
628 
629  bool mValid;
630 
631  mutable std::unique_ptr<QgsImageFetcher> mFetcher;
632 };
633 
634 
641 {
642  Q_OBJECT
643 
644  public:
646  QgsDataDefinedSizeLegendNode( QgsLayerTreeLayer *nodeLayer, const QgsDataDefinedSizeLegend &settings, QObject *parent SIP_TRANSFERTHIS = nullptr );
647  ~QgsDataDefinedSizeLegendNode() override;
648 
649  QVariant data( int role ) const override;
650 
651  ItemMetrics draw( const QgsLegendSettings &settings, ItemContext *ctx ) override;
652 
653  private:
654  void cacheImage() const;
655  QgsDataDefinedSizeLegend *mSettings = nullptr;
656  mutable QImage mImage;
657 };
658 
659 #endif // QGSLAYERTREEMODELLEGENDNODE_H
QgsExpressionContext
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
Definition: qgsexpressioncontext.h:369
QgsImageFetcher
Handles asynchronous download of images.
Definition: qgsrasterdataprovider.h:57
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:371
QgsLayerTreeModelLegendNode::invalidateMapBasedData
virtual void invalidateMapBasedData()
Notification from model that information from associated map view has changed.
Definition: qgslayertreemodellegendnode.h:133
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:201
QgsSimpleLegendNode
Definition: qgslayertreemodellegendnode.h:497
QgsLayerTreeModelLegendNode::ItemContext::point
Q_DECL_DEPRECATED QPointF point
Top-left corner of the legend item.
Definition: qgslayertreemodellegendnode.h:150
QgsDataDefinedSizeLegend
Definition: qgsdatadefinedsizelegend.h:41
QgsRenderContext
Definition: qgsrendercontext.h:57
QgsLayerTreeModelLegendNode::setUserPatchSize
virtual void setUserPatchSize(QSizeF size)
Sets the user (overridden) size for the legend node.
Definition: qgslayertreemodellegendnode.h:110
QgsSymbolLegendNode::isScaleOK
bool isScaleOK(double scale) const override
Definition: qgslayertreemodellegendnode.h:310
QgsLayerTreeModelLegendNode::ItemContext::patchShape
QgsLegendPatchShape patchShape
The patch shape to render for the node.
Definition: qgslayertreemodellegendnode.h:192
QgsLayerTreeModel
Definition: qgslayertreemodel.h:53
QgsSymbol
Definition: qgssymbol.h:63
QgsDataDefinedSizeLegendNode
Definition: qgslayertreemodellegendnode.h:640
QgsLayerTreeModelLegendNode::LegendNodeRoles
LegendNodeRoles
Definition: qgslayertreemodellegendnode.h:63
QgsLegendPatchShape
Definition: qgslegendpatchshape.h:30
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:135
QgsLayerTreeModelLegendNode::userLabel
virtual QString userLabel() const
Definition: qgslayertreemodellegendnode.h:87
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:71
Q_NOWARN_DEPRECATED_POP
#define Q_NOWARN_DEPRECATED_POP
Definition: qgis.h:752
SIP_CONVERT_TO_SUBCLASS_CODE
#define SIP_CONVERT_TO_SUBCLASS_CODE(code)
Definition: qgis_sip.h:172
QgsTextFormat
Definition: qgstextformat.h:38
QgsLayerTreeModelLegendNode::ItemMetrics::labelSize
QSizeF labelSize
Definition: qgslayertreemodellegendnode.h:207
QgsLegendSymbolItem
Definition: qgslegendsymbolitem.h:36
qgsexpressioncontext.h
QgsLayerTreeLayer
Definition: qgslayertreelayer.h:43
QgsSymbolLegendNode::setTextOnSymbolTextFormat
void setTextOnSymbolTextFormat(const QgsTextFormat &format)
Sets format of text to be shown on top of the symbol.
Definition: qgslayertreemodellegendnode.h:377
qgis_sip.h
QgsSymbolLegendNode::setIconSize
void setIconSize(QSize sz)
Set the icon size.
Definition: qgslayertreemodellegendnode.h:318
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:365
QgsLegendSettings
The QgsLegendSettings class stores the appearance and layout settings for legend drawing with QgsLege...
Definition: qgslegendsettings.h:38
QgsWmsLegendNode
Definition: qgslayertreemodellegendnode.h:593
qgslegendpatchshape.h
QgsLayerTreeModelLegendNode::ItemContext
Definition: qgslayertreemodellegendnode.h:135
QgsLayerTreeModelLegendNode::setEmbeddedInParent
virtual void setEmbeddedInParent(bool embedded)
Definition: qgslayertreemodellegendnode.h:85
QgsLayerTreeModelLegendNode::ItemMetrics::symbolSize
QSizeF symbolSize
Definition: qgslayertreemodellegendnode.h:206
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:111
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:204
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:308
QgsImageLegendNode
Definition: qgslayertreemodellegendnode.h:529
QgsSymbolLegendNode::textOnSymbolLabel
QString textOnSymbolLabel() const
Returns label of text to be shown on top of the symbol.
Definition: qgslayertreemodellegendnode.h:359
QgsRasterSymbolLegendNode
Definition: qgslayertreemodellegendnode.h:559
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:95
QgsMapSettings
Definition: qgsmapsettings.h:86
QgsLayerTreeModelLegendNode::isScaleOK
virtual bool isScaleOK(double scale) const
Definition: qgslayertreemodellegendnode.h:128
QgsSymbolLegendNode
Definition: qgslayertreemodellegendnode.h:283
Q_NOWARN_DEPRECATED_PUSH
#define Q_NOWARN_DEPRECATED_PUSH
Definition: qgis.h:751
SIP_END
#define SIP_END
Definition: qgis_sip.h:189
QgsSymbolLegendNode::iconSize
QSize iconSize() const
Definition: qgslayertreemodellegendnode.h:320
SIP_TRANSFERTHIS
#define SIP_TRANSFERTHIS
Definition: qgis_sip.h:53
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
Definition: qgslayertreemodellegendnode.h:50