QGIS API Documentation  2.8.2-Wien
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 );
185 
186  virtual QVariant data( int role ) const override;
187 
188  private:
189  QString mLabel;
190  QString mId;
191  QIcon mIcon;
192 };
193 
194 
201 {
202  public:
203  QgsImageLegendNode( QgsLayerTreeLayer* nodeLayer, const QImage& img, QObject* parent = 0 );
204 
205  virtual QVariant data( int role ) const override;
206 
207  QSizeF drawSymbol( const QgsLegendSettings& settings, ItemContext* ctx, double itemHeight ) const override;
208 
209  private:
210  QImage mImage;
211 };
212 
219 {
220  public:
221  QgsRasterSymbolLegendNode( QgsLayerTreeLayer* nodeLayer, const QColor& color, const QString& label, QObject* parent = 0 );
222 
223  virtual QVariant data( int role ) const override;
224 
225  QSizeF drawSymbol( const QgsLegendSettings& settings, ItemContext* ctx, double itemHeight ) const override;
226 
227  private:
228  QColor mColor;
229  QString mLabel;
230 };
231 
232 class QgsImageFetcher;
233 
240 {
241  Q_OBJECT
242 
243  public:
244  QgsWMSLegendNode( QgsLayerTreeLayer* nodeLayer, QObject* parent = 0 );
245 
246  virtual QVariant data( int role ) const override;
247 
248  virtual QSizeF drawSymbol( const QgsLegendSettings& settings, ItemContext* ctx, double itemHeight ) const override;
249 
250  virtual void invalidateMapBasedData() override;
251 
252  private slots:
253 
254  void getLegendGraphicFinished( const QImage& );
255  void getLegendGraphicErrored( const QString& );
256  void getLegendGraphicProgress( qint64, qint64 );
257 
258  private:
259 
260  // Lazily initializes mImage
261  const QImage& getLegendGraphic() const;
262 
263  QImage renderMessage( const QString& msg ) const;
264 
265  QImage mImage;
266 
267  bool mValid;
268 
269  mutable QScopedPointer<QgsImageFetcher> mFetcher;
270 };
271 
272 #endif // QGSLAYERTREEMODELLEGENDNODE_H