QGIS API Documentation  3.0.2-Girona (307d082)
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 
156 {
157  Q_OBJECT
158 
159  public:
160 
167  QgsSymbolLegendNode( QgsLayerTreeLayer *nodeLayer, const QgsLegendSymbolItem &item, QObject *parent SIP_TRANSFERTHIS = nullptr );
168 
169  Qt::ItemFlags flags() const override;
170  QVariant data( int role ) const override;
171  bool setData( const QVariant &value, int role ) override;
172 
173  QSizeF drawSymbol( const QgsLegendSettings &settings, ItemContext *ctx, double itemHeight ) const override;
174 
175  void setEmbeddedInParent( bool embedded ) override;
176 
177  void setUserLabel( const QString &userLabel ) override { mUserLabel = userLabel; updateLabel(); }
178 
179  bool isScaleOK( double scale ) const override { return mItem.isScaleOK( scale ); }
180 
181  void invalidateMapBasedData() override;
182 
187  void setIconSize( QSize sz ) { mIconSize = sz; }
189  QSize iconSize() const { return mIconSize; }
190 
198  QSize minimumIconSize() const;
199 
206  QSize minimumIconSize( QgsRenderContext *context ) const;
207 
213  const QgsSymbol *symbol() const;
214 
222  void setSymbol( QgsSymbol *symbol );
223 
224  public slots:
225 
231  void checkAllItems();
232 
238  void uncheckAllItems();
239 
240  private:
241  void updateLabel();
242 
243  private:
244  QgsLegendSymbolItem mItem;
245  mutable QPixmap mPixmap; // cached symbol preview
246  QString mLabel;
247  bool mSymbolUsesMapUnits;
248  QSize mIconSize;
249 
250  // ident the symbol icon to make it look like a tree structure
251  static const int INDENT_SIZE = 20;
252 
257  void checkAll( bool state );
258 };
259 
260 
268 {
269  Q_OBJECT
270 
271  public:
272 
281  QgsSimpleLegendNode( QgsLayerTreeLayer *nodeLayer, const QString &label, const QIcon &icon = QIcon(), QObject *parent SIP_TRANSFERTHIS = nullptr, const QString &key = QString() );
282 
283  QVariant data( int role ) const override;
284 
285  private:
286  QString mLabel;
287  QString mId;
288  QIcon mIcon;
289  QString mKey;
290 };
291 
292 
300 {
301  Q_OBJECT
302 
303  public:
304 
311  QgsImageLegendNode( QgsLayerTreeLayer *nodeLayer, const QImage &img, QObject *parent SIP_TRANSFERTHIS = nullptr );
312 
313  QVariant data( int role ) const override;
314 
315  QSizeF drawSymbol( const QgsLegendSettings &settings, ItemContext *ctx, double itemHeight ) const override;
316 
317  private:
318  QImage mImage;
319 };
320 
328 {
329  Q_OBJECT
330 
331  public:
332 
340  QgsRasterSymbolLegendNode( QgsLayerTreeLayer *nodeLayer, const QColor &color, const QString &label, QObject *parent SIP_TRANSFERTHIS = nullptr );
341 
342  QVariant data( int role ) const override;
343 
344  QSizeF drawSymbol( const QgsLegendSettings &settings, ItemContext *ctx, double itemHeight ) const override;
345 
346  private:
347  QColor mColor;
348  QString mLabel;
349 };
350 
351 class QgsImageFetcher;
352 
360 {
361  Q_OBJECT
362 
363  public:
364 
370  QgsWmsLegendNode( QgsLayerTreeLayer *nodeLayer, QObject *parent SIP_TRANSFERTHIS = nullptr );
371 
372  QVariant data( int role ) const override;
373 
374  QSizeF drawSymbol( const QgsLegendSettings &settings, ItemContext *ctx, double itemHeight ) const override;
375 
376  void invalidateMapBasedData() override;
377 
378  private slots:
379 
380  void getLegendGraphicFinished( const QImage & );
381  void getLegendGraphicErrored( const QString & );
382  void getLegendGraphicProgress( qint64, qint64 );
383 
384  private:
385 
386  // Lazily initializes mImage
387  QImage getLegendGraphic() const;
388 
389  QImage renderMessage( const QString &msg ) const;
390 
391  QImage mImage;
392 
393  bool mValid;
394 
395  mutable std::unique_ptr<QgsImageFetcher> mFetcher;
396 };
397 
398 
405 {
406  Q_OBJECT
407 
408  public:
410  QgsDataDefinedSizeLegendNode( QgsLayerTreeLayer *nodeLayer, const QgsDataDefinedSizeLegend &settings, QObject *parent SIP_TRANSFERTHIS = nullptr );
411  ~QgsDataDefinedSizeLegendNode() override;
412 
413  QVariant data( int role ) const override;
414 
415  ItemMetrics draw( const QgsLegendSettings &settings, ItemContext *ctx ) override;
416 
417  private:
418  void cacheImage() const;
419  QgsDataDefinedSizeLegend *mSettings = nullptr;
420  mutable QImage mImage;
421 };
422 
423 #endif // QGSLAYERTREEMODELLEGENDNODE_H
Implementation of legend node interface for displaying arbitrary raster image.
#define SIP_TRANSFERTHIS
Definition: qgis_sip.h:46
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.
Implementation of legend node interface for displaying preview of vector symbols and their labels and...
QgsLayerTreeLayer * layerNode() const
Return 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_FACTORY
Definition: qgis_sip.h:69
Implementation of legend node interface for displaying arbitrary label with icon. ...
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...
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.