QGIS API Documentation  3.6.0-Noosa (5873452)
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 
120  virtual QSizeF drawSymbol( const QgsLegendSettings &settings, ItemContext *ctx, double itemHeight ) const;
121 
129  virtual QSizeF drawSymbolText( const QgsLegendSettings &settings, ItemContext *ctx, QSizeF symbolSize ) const;
130 
131  signals:
133  void dataChanged();
134 
135  protected:
137  explicit QgsLayerTreeModelLegendNode( QgsLayerTreeLayer *nodeL, QObject *parent SIP_TRANSFERTHIS = nullptr );
138 
140  QgsRenderContext *createTemporaryRenderContext() const SIP_FACTORY;
141 
142  protected:
143  QgsLayerTreeLayer *mLayerNode = nullptr;
144  bool mEmbeddedInParent;
145  QString mUserLabel;
146 };
147 
148 #include "qgslegendsymbolitem.h"
149 #include "qgstextrenderer.h"
150 
159 {
160  Q_OBJECT
161 
162  public:
163 
170  QgsSymbolLegendNode( QgsLayerTreeLayer *nodeLayer, const QgsLegendSymbolItem &item, QObject *parent SIP_TRANSFERTHIS = nullptr );
171 
172  Qt::ItemFlags flags() const override;
173  QVariant data( int role ) const override;
174  bool setData( const QVariant &value, int role ) override;
175 
176  QSizeF drawSymbol( const QgsLegendSettings &settings, ItemContext *ctx, double itemHeight ) const override;
177 
178  void setEmbeddedInParent( bool embedded ) override;
179 
180  void setUserLabel( const QString &userLabel ) override { mUserLabel = userLabel; updateLabel(); }
181 
182  bool isScaleOK( double scale ) const override { return mItem.isScaleOK( scale ); }
183 
184  void invalidateMapBasedData() override;
185 
190  void setIconSize( QSize sz ) { mIconSize = sz; }
192  QSize iconSize() const { return mIconSize; }
193 
201  QSize minimumIconSize() const;
202 
209  QSize minimumIconSize( QgsRenderContext *context ) const;
210 
216  const QgsSymbol *symbol() const;
217 
225  void setSymbol( QgsSymbol *symbol SIP_TRANSFER );
226 
231  QString textOnSymbolLabel() const { return mTextOnSymbolLabel; }
232 
237  void setTextOnSymbolLabel( const QString &label ) { mTextOnSymbolLabel = label; }
238 
243  QgsTextFormat textOnSymbolTextFormat() const { return mTextOnSymbolTextFormat; }
244 
249  void setTextOnSymbolTextFormat( const QgsTextFormat &format ) { mTextOnSymbolTextFormat = format; }
250 
251  public slots:
252 
259  void checkAllItems();
260 
267  void uncheckAllItems();
268 
275  void toggleAllItems();
276 
277  private:
278  void updateLabel();
279 
280  private:
281  QgsLegendSymbolItem mItem;
282  mutable QPixmap mPixmap; // cached symbol preview
283  QString mLabel;
284  bool mSymbolUsesMapUnits;
285  QSize mIconSize;
286 
287  QString mTextOnSymbolLabel;
288  QgsTextFormat mTextOnSymbolTextFormat;
289 
290  // ident the symbol icon to make it look like a tree structure
291  static const int INDENT_SIZE = 20;
292 
297  void checkAll( bool state );
298 };
299 
300 
308 {
309  Q_OBJECT
310 
311  public:
312 
321  QgsSimpleLegendNode( QgsLayerTreeLayer *nodeLayer, const QString &label, const QIcon &icon = QIcon(), QObject *parent SIP_TRANSFERTHIS = nullptr, const QString &key = QString() );
322 
323  QVariant data( int role ) const override;
324 
325  private:
326  QString mLabel;
327  QString mId;
328  QIcon mIcon;
329  QString mKey;
330 };
331 
332 
340 {
341  Q_OBJECT
342 
343  public:
344 
351  QgsImageLegendNode( QgsLayerTreeLayer *nodeLayer, const QImage &img, QObject *parent SIP_TRANSFERTHIS = nullptr );
352 
353  QVariant data( int role ) const override;
354 
355  QSizeF drawSymbol( const QgsLegendSettings &settings, ItemContext *ctx, double itemHeight ) const override;
356 
357  private:
358  QImage mImage;
359 };
360 
368 {
369  Q_OBJECT
370 
371  public:
372 
380  QgsRasterSymbolLegendNode( QgsLayerTreeLayer *nodeLayer, const QColor &color, const QString &label, 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  private:
387  QColor mColor;
388  QString mLabel;
389 };
390 
391 class QgsImageFetcher;
392 
400 {
401  Q_OBJECT
402 
403  public:
404 
410  QgsWmsLegendNode( QgsLayerTreeLayer *nodeLayer, QObject *parent SIP_TRANSFERTHIS = nullptr );
411 
412  QVariant data( int role ) const override;
413 
414  QSizeF drawSymbol( const QgsLegendSettings &settings, ItemContext *ctx, double itemHeight ) const override;
415 
416  void invalidateMapBasedData() override;
417 
418  private slots:
419 
420  void getLegendGraphicFinished( const QImage & );
421  void getLegendGraphicErrored( const QString & );
422  void getLegendGraphicProgress( qint64, qint64 );
423 
424  private:
425 
426  // Lazily initializes mImage
427  QImage getLegendGraphic() const;
428 
429  QImage renderMessage( const QString &msg ) const;
430 
431  QImage mImage;
432 
433  bool mValid;
434 
435  mutable std::unique_ptr<QgsImageFetcher> mFetcher;
436 };
437 
438 
445 {
446  Q_OBJECT
447 
448  public:
450  QgsDataDefinedSizeLegendNode( QgsLayerTreeLayer *nodeLayer, const QgsDataDefinedSizeLegend &settings, QObject *parent SIP_TRANSFERTHIS = nullptr );
451  ~QgsDataDefinedSizeLegendNode() override;
452 
453  QVariant data( int role ) const override;
454 
455  ItemMetrics draw( const QgsLegendSettings &settings, ItemContext *ctx ) override;
456 
457  private:
458  void cacheImage() const;
459  QgsDataDefinedSizeLegend *mSettings = nullptr;
460  mutable QImage mImage;
461 };
462 
463 #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.
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.