QGIS API Documentation  2.8.6-Wien
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 #include "qgsrasterdataprovider.h" // for QgsImageFetcher dtor visibility
26 
27 class QgsLayerTreeLayer;
28 class QgsLayerTreeModel;
29 class QgsLegendSettings;
30 class QgsMapSettings;
31 class QgsSymbolV2;
32 
42 class CORE_EXPORT QgsLayerTreeModelLegendNode : public QObject
43 {
44  Q_OBJECT
45  public:
47 
49  {
50  RuleKeyRole = Qt::UserRole,
52  ParentRuleKeyRole
53  };
54 
56  QgsLayerTreeLayer* layerNode() const { return mLayerNode; }
57 
59  QgsLayerTreeModel* model() const;
60 
62  virtual Qt::ItemFlags flags() const;
63 
65  virtual QVariant data( int role ) const = 0;
66 
68  virtual bool setData( const QVariant& value, int role );
69 
70  virtual bool isEmbeddedInParent() const { return mEmbeddedInParent; }
71  virtual void setEmbeddedInParent( bool embedded ) { mEmbeddedInParent = embedded; }
72 
73  virtual QString userLabel() const { return mUserLabel; }
74  virtual void setUserLabel( const QString& userLabel ) { mUserLabel = userLabel; }
75 
76  virtual bool isScaleOK( double scale ) const { Q_UNUSED( scale ); return true; }
77 
80  virtual void invalidateMapBasedData() {}
81 
82  struct ItemContext
83  {
85  QPainter* painter;
87  QPointF point;
89  double labelXOffset;
90  };
91 
92  struct ItemMetrics
93  {
94  QSizeF symbolSize;
95  QSizeF labelSize;
96  };
97 
103  virtual ItemMetrics draw( const QgsLegendSettings& settings, ItemContext* ctx );
104 
112  virtual QSizeF drawSymbol( const QgsLegendSettings& settings, ItemContext* ctx, double itemHeight ) const;
113 
121  virtual QSizeF drawSymbolText( const QgsLegendSettings& settings, ItemContext* ctx, const QSizeF& symbolSize ) const;
122 
123  signals:
125  void dataChanged();
126 
127  protected:
129  explicit QgsLayerTreeModelLegendNode( QgsLayerTreeLayer* nodeL, QObject* parent = 0 );
130 
131  protected:
134  QString mUserLabel;
135 };
136 
137 #include "qgslegendsymbolitemv2.h"
138 
146 {
147  public:
148  QgsSymbolV2LegendNode( QgsLayerTreeLayer* nodeLayer, const QgsLegendSymbolItemV2& item, QObject* parent = 0 );
150 
151  virtual Qt::ItemFlags flags() const override;
152  virtual QVariant data( int role ) const override;
153  virtual bool setData( const QVariant& value, int role ) override;
154 
155  QSizeF drawSymbol( const QgsLegendSettings& settings, ItemContext* ctx, double itemHeight ) const override;
156 
157  virtual void setEmbeddedInParent( bool embedded ) override;
158 
159  void setUserLabel( const QString& userLabel ) override { mUserLabel = userLabel; updateLabel(); }
160 
161  virtual bool isScaleOK( double scale ) const override { return mItem.isScaleOK( scale ); }
162 
163  virtual void invalidateMapBasedData() override;
164 
165  private:
166  void updateLabel();
167 
168  private:
169  QgsLegendSymbolItemV2 mItem;
170  mutable QPixmap mPixmap; // cached symbol preview
171  QString mLabel;
172  bool mSymbolUsesMapUnits;
173 };
174 
175 
182 {
183  public:
184  QgsSimpleLegendNode( QgsLayerTreeLayer* nodeLayer, const QString& label, const QIcon& icon = QIcon(), QObject* parent = 0, const QString& key = QString() );
185 
186  virtual QVariant data( int role ) const override;
187 
188  private:
189  QString mLabel;
190  QString mId;
191  QIcon mIcon;
192  QString mKey;
193 };
194 
195 
202 {
203  public:
204  QgsImageLegendNode( QgsLayerTreeLayer* nodeLayer, const QImage& img, QObject* parent = 0 );
205 
206  virtual QVariant data( int role ) const override;
207 
208  QSizeF drawSymbol( const QgsLegendSettings& settings, ItemContext* ctx, double itemHeight ) const override;
209 
210  private:
211  QImage mImage;
212 };
213 
220 {
221  public:
222  QgsRasterSymbolLegendNode( QgsLayerTreeLayer* nodeLayer, const QColor& color, const QString& label, QObject* parent = 0 );
223 
224  virtual QVariant data( int role ) const override;
225 
226  QSizeF drawSymbol( const QgsLegendSettings& settings, ItemContext* ctx, double itemHeight ) const override;
227 
228  private:
229  QColor mColor;
230  QString mLabel;
231 };
232 
233 class QgsImageFetcher;
234 
241 {
242  Q_OBJECT
243 
244  public:
245  QgsWMSLegendNode( QgsLayerTreeLayer* nodeLayer, QObject* parent = 0 );
246 
247  virtual QVariant data( int role ) const override;
248 
249  virtual QSizeF drawSymbol( const QgsLegendSettings& settings, ItemContext* ctx, double itemHeight ) const override;
250 
251  virtual void invalidateMapBasedData() override;
252 
253  private slots:
254 
255  void getLegendGraphicFinished( const QImage& );
256  void getLegendGraphicErrored( const QString& );
257  void getLegendGraphicProgress( qint64, qint64 );
258 
259  private:
260 
261  // Lazily initializes mImage
262  const QImage& getLegendGraphic() const;
263 
264  QImage renderMessage( const QString& msg ) const;
265 
266  QImage mImage;
267 
268  bool mValid;
269 
270  mutable QScopedPointer<QgsImageFetcher> mFetcher;
271 };
272 
273 #endif // QGSLAYERTREEMODELLEGENDNODE_H
Implementation of legend node interface for displaying arbitrary raster image.
virtual bool isScaleOK(double scale) const override
Handles asynchronous download of images.
Implementation of legend node interface for displaying raster legend entries.
The QgsMapSettings class contains configuration for rendering of the map.
void setUserLabel(const QString &userLabel) override
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
Return pointer to the parent layer node.
Implementation of legend node interface for displaying arbitrary label with icon. ...
Implementation of legend node interface for displaying WMS legend entries.
virtual void setEmbeddedInParent(bool embedded)
QPointF point
Top-left corner of the legend item.
virtual bool isScaleOK(double scale) const
The QgsLegendRendererItem class is abstract interface for legend items returned from QgsMapLayerLegen...
for QgsSymbolV2LegendNode only - legacy rule key (void ptr, to be cast to QgsSymbolV2 ptr) ...
Implementation of legend node interface for displaying preview of vector symbols and their labels and...
double labelXOffset
offset from the left side where label should start
The class stores information about one class/rule of a vector layer renderer in a unified way that ca...
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.