QGIS API Documentation  3.4.15-Madeira (e83d02e274)
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.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  QPainter *painter = nullptr;
92  QPointF point;
94  double labelXOffset;
95  };
96 
97  struct ItemMetrics
98  {
99  QSizeF symbolSize;
100  QSizeF labelSize;
101  };
102 
109  virtual ItemMetrics draw( const QgsLegendSettings &settings, ItemContext *ctx );
110 
118  virtual QSizeF drawSymbol( const QgsLegendSettings &settings, ItemContext *ctx, double itemHeight ) const;
119 
127  virtual QSizeF drawSymbolText( const QgsLegendSettings &settings, ItemContext *ctx, QSizeF symbolSize ) const;
128 
129  signals:
131  void dataChanged();
132 
133  protected:
135  explicit QgsLayerTreeModelLegendNode( QgsLayerTreeLayer *nodeL, QObject *parent SIP_TRANSFERTHIS = nullptr );
136 
138  QgsRenderContext *createTemporaryRenderContext() const SIP_FACTORY;
139 
140  protected:
141  QgsLayerTreeLayer *mLayerNode = nullptr;
142  bool mEmbeddedInParent;
143  QString mUserLabel;
144 };
145 
146 #include "qgslegendsymbolitem.h"
147 #include "qgstextrenderer.h"
148 
157 {
158  Q_OBJECT
159 
160  public:
161 
168  QgsSymbolLegendNode( QgsLayerTreeLayer *nodeLayer, const QgsLegendSymbolItem &item, QObject *parent SIP_TRANSFERTHIS = nullptr );
169 
170  Qt::ItemFlags flags() const override;
171  QVariant data( int role ) const override;
172  bool setData( const QVariant &value, int role ) override;
173 
174  QSizeF drawSymbol( const QgsLegendSettings &settings, ItemContext *ctx, double itemHeight ) const override;
175 
176  void setEmbeddedInParent( bool embedded ) override;
177 
178  void setUserLabel( const QString &userLabel ) override { mUserLabel = userLabel; updateLabel(); }
179 
180  bool isScaleOK( double scale ) const override { return mItem.isScaleOK( scale ); }
181 
182  void invalidateMapBasedData() override;
183 
188  void setIconSize( QSize sz ) { mIconSize = sz; }
190  QSize iconSize() const { return mIconSize; }
191 
199  QSize minimumIconSize() const;
200 
207  QSize minimumIconSize( QgsRenderContext *context ) const;
208 
214  const QgsSymbol *symbol() const;
215 
223  void setSymbol( QgsSymbol *symbol );
224 
229  QString textOnSymbolLabel() const { return mTextOnSymbolLabel; }
230 
235  void setTextOnSymbolLabel( const QString &label ) { mTextOnSymbolLabel = label; }
236 
241  QgsTextFormat textOnSymbolTextFormat() const { return mTextOnSymbolTextFormat; }
242 
247  void setTextOnSymbolTextFormat( const QgsTextFormat &format ) { mTextOnSymbolTextFormat = format; }
248 
249  public slots:
250 
256  void checkAllItems();
257 
263  void uncheckAllItems();
264 
265  private:
266  void updateLabel();
267 
268  private:
269  QgsLegendSymbolItem mItem;
270  mutable QPixmap mPixmap; // cached symbol preview
271  QString mLabel;
272  bool mSymbolUsesMapUnits;
273  QSize mIconSize;
274 
275  QString mTextOnSymbolLabel;
276  QgsTextFormat mTextOnSymbolTextFormat;
277 
278  // ident the symbol icon to make it look like a tree structure
279  static const int INDENT_SIZE = 20;
280 
285  void checkAll( bool state );
286 };
287 
288 
296 {
297  Q_OBJECT
298 
299  public:
300 
309  QgsSimpleLegendNode( QgsLayerTreeLayer *nodeLayer, const QString &label, const QIcon &icon = QIcon(), QObject *parent SIP_TRANSFERTHIS = nullptr, const QString &key = QString() );
310 
311  QVariant data( int role ) const override;
312 
313  private:
314  QString mLabel;
315  QString mId;
316  QIcon mIcon;
317  QString mKey;
318 };
319 
320 
328 {
329  Q_OBJECT
330 
331  public:
332 
339  QgsImageLegendNode( QgsLayerTreeLayer *nodeLayer, const QImage &img, QObject *parent SIP_TRANSFERTHIS = nullptr );
340 
341  QVariant data( int role ) const override;
342 
343  QSizeF drawSymbol( const QgsLegendSettings &settings, ItemContext *ctx, double itemHeight ) const override;
344 
345  private:
346  QImage mImage;
347 };
348 
356 {
357  Q_OBJECT
358 
359  public:
360 
368  QgsRasterSymbolLegendNode( QgsLayerTreeLayer *nodeLayer, const QColor &color, const QString &label, QObject *parent SIP_TRANSFERTHIS = nullptr );
369 
370  QVariant data( int role ) const override;
371 
372  QSizeF drawSymbol( const QgsLegendSettings &settings, ItemContext *ctx, double itemHeight ) const override;
373 
374  private:
375  QColor mColor;
376  QString mLabel;
377 };
378 
379 class QgsImageFetcher;
380 
388 {
389  Q_OBJECT
390 
391  public:
392 
398  QgsWmsLegendNode( QgsLayerTreeLayer *nodeLayer, QObject *parent SIP_TRANSFERTHIS = nullptr );
399 
400  QVariant data( int role ) const override;
401 
402  QSizeF drawSymbol( const QgsLegendSettings &settings, ItemContext *ctx, double itemHeight ) const override;
403 
404  void invalidateMapBasedData() override;
405 
406  private slots:
407 
408  void getLegendGraphicFinished( const QImage & );
409  void getLegendGraphicErrored( const QString & );
410  void getLegendGraphicProgress( qint64, qint64 );
411 
412  private:
413 
414  // Lazily initializes mImage
415  QImage getLegendGraphic() const;
416 
417  QImage renderMessage( const QString &msg ) const;
418 
419  QImage mImage;
420 
421  bool mValid;
422 
423  mutable std::unique_ptr<QgsImageFetcher> mFetcher;
424 };
425 
426 
433 {
434  Q_OBJECT
435 
436  public:
438  QgsDataDefinedSizeLegendNode( QgsLayerTreeLayer *nodeLayer, const QgsDataDefinedSizeLegend &settings, QObject *parent SIP_TRANSFERTHIS = nullptr );
439  ~QgsDataDefinedSizeLegendNode() override;
440 
441  QVariant data( int role ) const override;
442 
443  ItemMetrics draw( const QgsLegendSettings &settings, ItemContext *ctx ) override;
444 
445  private:
446  void cacheImage() const;
447  QgsDataDefinedSizeLegend *mSettings = nullptr;
448  mutable QImage mImage;
449 };
450 
451 #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.
QgsTextFormat textOnSymbolTextFormat() const
Returns text format of the label 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.
Implementation of legend node interface for displaying preview of vector symbols and their labels and...
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.
The QgsLegendSettings class stores the appearance and layout settings for legend drawing with QgsLege...
QgsLayerTreeLayer * layerNode() const
Returns pointer to the parent layer node.
#define SIP_FACTORY
Definition: qgis_sip.h:69
Implementation of legend node interface for displaying arbitrary label with icon. ...
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
virtual bool isScaleOK(double scale) const
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...
QString textOnSymbolLabel() const
Returns label of text to be shown on top of the symbol.
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.