QGIS API Documentation  3.8.0-Zanzibar (11aff65)
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 
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  Q_OBJECT
51  public:
52 
54  {
55  RuleKeyRole = Qt::UserRole,
56  ParentRuleKeyRole
57  };
58 
60  QgsLayerTreeLayer *layerNode() const { return mLayerNode; }
61 
63  QgsLayerTreeModel *model() const;
64 
66  virtual Qt::ItemFlags flags() const;
67 
69  virtual QVariant data( int role ) const = 0;
70 
72  virtual bool setData( const QVariant &value, int role );
73 
74  virtual bool isEmbeddedInParent() const { return mEmbeddedInParent; }
75  virtual void setEmbeddedInParent( bool embedded ) { mEmbeddedInParent = embedded; }
76 
77  virtual QString userLabel() const { return mUserLabel; }
78  virtual void setUserLabel( const QString &userLabel ) { mUserLabel = userLabel; }
79 
80  virtual bool isScaleOK( double scale ) const { Q_UNUSED( scale ) return true; }
81 
85  virtual void invalidateMapBasedData() {}
86 
87  struct ItemContext
88  {
90  QgsRenderContext *context = nullptr;
92  QPainter *painter = nullptr;
94  QPointF point;
96  double labelXOffset;
97  };
98 
99  struct ItemMetrics
100  {
101  QSizeF symbolSize;
102  QSizeF labelSize;
103  };
104 
111  virtual ItemMetrics draw( const QgsLegendSettings &settings, ItemContext *ctx );
112 
121  void exportToJson( const QgsLegendSettings &settings, const QgsRenderContext &context, QJsonObject &json );
122 
130  virtual QSizeF drawSymbol( const QgsLegendSettings &settings, ItemContext *ctx, double itemHeight ) const;
131 
139  virtual void exportSymbolToJson( const QgsLegendSettings &settings, const QgsRenderContext &context, QJsonObject &json ) const;
140 
148  virtual QSizeF drawSymbolText( const QgsLegendSettings &settings, ItemContext *ctx, QSizeF symbolSize ) const;
149 
156  void exportSymbolTextToJson( const QgsLegendSettings &settings, QJsonObject &json ) const;
157 
158  signals:
160  void dataChanged();
161 
162  protected:
164  explicit QgsLayerTreeModelLegendNode( QgsLayerTreeLayer *nodeL, QObject *parent SIP_TRANSFERTHIS = nullptr );
165 
167  QgsRenderContext *createTemporaryRenderContext() const SIP_FACTORY;
168 
169  protected:
170  QgsLayerTreeLayer *mLayerNode = nullptr;
171  bool mEmbeddedInParent;
172  QString mUserLabel;
173 };
174 
175 #include "qgslegendsymbolitem.h"
176 #include "qgstextrenderer.h"
177 
186 {
187  Q_OBJECT
188 
189  public:
190 
197  QgsSymbolLegendNode( QgsLayerTreeLayer *nodeLayer, const QgsLegendSymbolItem &item, QObject *parent SIP_TRANSFERTHIS = nullptr );
198 
199  Qt::ItemFlags flags() const override;
200  QVariant data( int role ) const override;
201  bool setData( const QVariant &value, int role ) override;
202 
203  QSizeF drawSymbol( const QgsLegendSettings &settings, ItemContext *ctx, double itemHeight ) const override;
204 
205  void exportSymbolToJson( const QgsLegendSettings &settings, const QgsRenderContext &context, QJsonObject &json ) const override;
206 
207  void setEmbeddedInParent( bool embedded ) override;
208 
209  void setUserLabel( const QString &userLabel ) override { mUserLabel = userLabel; updateLabel(); }
210 
211  bool isScaleOK( double scale ) const override { return mItem.isScaleOK( scale ); }
212 
213  void invalidateMapBasedData() override;
214 
219  void setIconSize( QSize sz ) { mIconSize = sz; }
221  QSize iconSize() const { return mIconSize; }
222 
230  QSize minimumIconSize() const;
231 
238  QSize minimumIconSize( QgsRenderContext *context ) const;
239 
245  const QgsSymbol *symbol() const;
246 
254  void setSymbol( QgsSymbol *symbol SIP_TRANSFER );
255 
260  QString textOnSymbolLabel() const { return mTextOnSymbolLabel; }
261 
266  void setTextOnSymbolLabel( const QString &label ) { mTextOnSymbolLabel = label; }
267 
272  QgsTextFormat textOnSymbolTextFormat() const { return mTextOnSymbolTextFormat; }
273 
278  void setTextOnSymbolTextFormat( const QgsTextFormat &format ) { mTextOnSymbolTextFormat = format; }
279 
280  public slots:
281 
288  void checkAllItems();
289 
296  void uncheckAllItems();
297 
304  void toggleAllItems();
305 
306  private:
307  void updateLabel();
308 
309  private:
310  QgsLegendSymbolItem mItem;
311  mutable QPixmap mPixmap; // cached symbol preview
312  QString mLabel;
313  bool mSymbolUsesMapUnits;
314  QSize mIconSize;
315 
316  QString mTextOnSymbolLabel;
317  QgsTextFormat mTextOnSymbolTextFormat;
318 
319  // ident the symbol icon to make it look like a tree structure
320  static const int INDENT_SIZE = 20;
321 
326  void checkAll( bool state );
327 };
328 
329 
337 {
338  Q_OBJECT
339 
340  public:
341 
350  QgsSimpleLegendNode( QgsLayerTreeLayer *nodeLayer, const QString &label, const QIcon &icon = QIcon(), QObject *parent SIP_TRANSFERTHIS = nullptr, const QString &key = QString() );
351 
352  QVariant data( int role ) const override;
353 
354  private:
355  QString mLabel;
356  QString mId;
357  QIcon mIcon;
358  QString mKey;
359 };
360 
361 
369 {
370  Q_OBJECT
371 
372  public:
373 
380  QgsImageLegendNode( QgsLayerTreeLayer *nodeLayer, const QImage &img, QObject *parent SIP_TRANSFERTHIS = nullptr );
381 
382  QVariant data( int role ) const override;
383 
384  QSizeF drawSymbol( const QgsLegendSettings &settings, ItemContext *ctx, double itemHeight ) const override;
385 
386  void exportSymbolToJson( const QgsLegendSettings &settings, const QgsRenderContext &context, QJsonObject &json ) const override;
387 
388  private:
389  QImage mImage;
390 };
391 
399 {
400  Q_OBJECT
401 
402  public:
403 
411  QgsRasterSymbolLegendNode( QgsLayerTreeLayer *nodeLayer, const QColor &color, const QString &label, QObject *parent SIP_TRANSFERTHIS = nullptr );
412 
413  QVariant data( int role ) const override;
414 
415  QSizeF drawSymbol( const QgsLegendSettings &settings, ItemContext *ctx, double itemHeight ) const override;
416 
417  void exportSymbolToJson( const QgsLegendSettings &settings, const QgsRenderContext &context, QJsonObject &json ) const override;
418 
419  private:
420  QColor mColor;
421  QString mLabel;
422 };
423 
424 class QgsImageFetcher;
425 
433 {
434  Q_OBJECT
435 
436  public:
437 
443  QgsWmsLegendNode( QgsLayerTreeLayer *nodeLayer, QObject *parent SIP_TRANSFERTHIS = nullptr );
444 
445  QVariant data( int role ) const override;
446 
447  QSizeF drawSymbol( const QgsLegendSettings &settings, ItemContext *ctx, double itemHeight ) const override;
448 
449  void exportSymbolToJson( const QgsLegendSettings &settings, const QgsRenderContext &context, QJsonObject &json ) const override;
450 
451  void invalidateMapBasedData() override;
452 
453  private slots:
454 
455  void getLegendGraphicFinished( const QImage & );
456  void getLegendGraphicErrored( const QString & );
457  void getLegendGraphicProgress( qint64, qint64 );
458 
459  private:
460 
461  // Lazily initializes mImage
462  QImage getLegendGraphic() const;
463 
464  QImage renderMessage( const QString &msg ) const;
465 
466  QImage mImage;
467 
468  bool mValid;
469 
470  mutable std::unique_ptr<QgsImageFetcher> mFetcher;
471 };
472 
473 
480 {
481  Q_OBJECT
482 
483  public:
485  QgsDataDefinedSizeLegendNode( QgsLayerTreeLayer *nodeLayer, const QgsDataDefinedSizeLegend &settings, QObject *parent SIP_TRANSFERTHIS = nullptr );
486  ~QgsDataDefinedSizeLegendNode() override;
487 
488  QVariant data( int role ) const override;
489 
490  ItemMetrics draw( const QgsLegendSettings &settings, ItemContext *ctx ) override;
491 
492  private:
493  void cacheImage() const;
494  QgsDataDefinedSizeLegend *mSettings = nullptr;
495  mutable QImage mImage;
496 };
497 
498 #endif // QGSLAYERTREEMODELLEGENDNODE_H
Implementation of legend node interface for displaying arbitrary raster image.
#define SIP_TRANSFERTHIS
Definition: qgis_sip.h:46
Abstract base class for all rendered symbols.
Definition: qgssymbol.h:61
Handles asynchronous download of images.
void setIconSize(QSize sz)
Set the icon size.
Produces legend node with a marker symbol.
Implementation of legend node interface for displaying raster legend entries.
void setTextOnSymbolLabel(const QString &label)
Sets label of text to be shown on top of the symbol.
Implementation of legend node interface for displaying preview of vector symbols and their labels and...
QgsLayerTreeLayer * layerNode() const
Returns pointer to the parent layer node.
void setUserLabel(const QString &userLabel) override
Implementation of legend node interface for displaying WMS legend entries.
The QgsMapSettings class contains configuration for rendering of the map.
The QgsLayerTreeModel class is model implementation for Qt item views framework.
virtual bool isScaleOK(double scale) const
The QgsLegendSettings class stores the appearance and layout settings for legend drawing with QgsLege...
#define SIP_TRANSFER
Definition: qgis_sip.h:36
QString textOnSymbolLabel() const
Returns label of text to be shown on top of the symbol.
#define SIP_FACTORY
Definition: qgis_sip.h:69
Implementation of legend node interface for displaying arbitrary label with icon. ...
QgsTextFormat textOnSymbolTextFormat() const
Returns text format of the label to be shown on top of the symbol.
nlohmann::json json
Definition: qgsjsonutils.h:27
void setTextOnSymbolTextFormat(const QgsTextFormat &format)
Sets format of text to be shown on top of the symbol.
virtual void setEmbeddedInParent(bool embedded)
The class stores information about one class/rule of a vector layer renderer in a unified way that ca...
Contains information about the context of a rendering operation.
QPointF point
Top-left corner of the legend item.
bool isScaleOK(double scale) const override
The QgsLegendRendererItem class is abstract interface for legend items returned from QgsMapLayerLegen...
Container for all settings relating to text rendering.
double labelXOffset
offset from the left side where label should start
Object that keeps configuration of appearance of marker symbol&#39;s data-defined size in legend...
virtual void invalidateMapBasedData()
Notification from model that information from associated map view has changed.
virtual void setUserLabel(const QString &userLabel)
Layer tree node points to a map layer.