QGIS API Documentation  2.10.1-Pisa
 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 class QgsRenderContext;
33 
43 class CORE_EXPORT QgsLayerTreeModelLegendNode : public QObject
44 {
45  Q_OBJECT
46  public:
48 
50  {
51  RuleKeyRole = Qt::UserRole,
53  ParentRuleKeyRole
54  };
55 
57  QgsLayerTreeLayer* layerNode() const { return mLayerNode; }
58 
60  QgsLayerTreeModel* model() const;
61 
63  virtual Qt::ItemFlags flags() const;
64 
66  virtual QVariant data( int role ) const = 0;
67 
69  virtual bool setData( const QVariant& value, int role );
70 
71  virtual bool isEmbeddedInParent() const { return mEmbeddedInParent; }
72  virtual void setEmbeddedInParent( bool embedded ) { mEmbeddedInParent = embedded; }
73 
74  virtual QString userLabel() const { return mUserLabel; }
75  virtual void setUserLabel( const QString& userLabel ) { mUserLabel = userLabel; }
76 
77  virtual bool isScaleOK( double scale ) const { Q_UNUSED( scale ); return true; }
78 
81  virtual void invalidateMapBasedData() {}
82 
83  struct ItemContext
84  {
90  double labelXOffset;
91  };
92 
93  struct ItemMetrics
94  {
97  };
98 
104  virtual ItemMetrics draw( const QgsLegendSettings& settings, ItemContext* ctx );
105 
113  virtual QSizeF drawSymbol( const QgsLegendSettings& settings, ItemContext* ctx, double itemHeight ) const;
114 
122  virtual QSizeF drawSymbolText( const QgsLegendSettings& settings, ItemContext* ctx, const QSizeF& symbolSize ) const;
123 
124  signals:
126  void dataChanged();
127 
128  protected:
130  explicit QgsLayerTreeModelLegendNode( QgsLayerTreeLayer* nodeL, QObject* parent = 0 );
131 
132  protected:
136 };
137 
138 #include "qgslegendsymbolitemv2.h"
139 
147 {
148  public:
149  QgsSymbolV2LegendNode( QgsLayerTreeLayer* nodeLayer, const QgsLegendSymbolItemV2& item, QObject* parent = 0 );
151 
152  virtual Qt::ItemFlags flags() const override;
153  virtual QVariant data( int role ) const override;
154  virtual bool setData( const QVariant& value, int role ) override;
155 
156  QSizeF drawSymbol( const QgsLegendSettings& settings, ItemContext* ctx, double itemHeight ) const override;
157 
158  virtual void setEmbeddedInParent( bool embedded ) override;
159 
160  void setUserLabel( const QString& userLabel ) override { mUserLabel = userLabel; updateLabel(); }
161 
162  virtual bool isScaleOK( double scale ) const override { return mItem.isScaleOK( scale ); }
163 
164  virtual void invalidateMapBasedData() override;
165 
168  void setIconSize( const QSize& sz ) { mIconSize = sz; }
170  QSize iconSize() const { return mIconSize; }
171 
174  QSize minimumIconSize() const;
175 
176  private:
177  void updateLabel();
178 
179  private:
180  QgsLegendSymbolItemV2 mItem;
181  mutable QPixmap mPixmap; // cached symbol preview
182  QString mLabel;
183  bool mSymbolUsesMapUnits;
184  QSize mIconSize;
185 
186  // ident the symbol icon to make it look like a tree structure
187  static const int indentSize = 20;
188 
189  // return a temporary context or null if legendMapViewData are not valid
190  QgsRenderContext * createTemporaryRenderContext() const;
191 
192 };
193 
194 
201 {
202  public:
203  QgsSimpleLegendNode( QgsLayerTreeLayer* nodeLayer, const QString& label, const QIcon& icon = QIcon(), QObject* parent = 0, const QString& key = QString() );
204 
205  virtual QVariant data( int role ) const override;
206 
207  private:
208  QString mLabel;
209  QString mId;
210  QIcon mIcon;
211  QString mKey;
212 };
213 
214 
221 {
222  public:
223  QgsImageLegendNode( QgsLayerTreeLayer* nodeLayer, const QImage& img, QObject* parent = 0 );
224 
225  virtual QVariant data( int role ) const override;
226 
227  QSizeF drawSymbol( const QgsLegendSettings& settings, ItemContext* ctx, double itemHeight ) const override;
228 
229  private:
230  QImage mImage;
231 };
232 
239 {
240  public:
241  QgsRasterSymbolLegendNode( QgsLayerTreeLayer* nodeLayer, const QColor& color, const QString& label, QObject* parent = 0 );
242 
243  virtual QVariant data( int role ) const override;
244 
245  QSizeF drawSymbol( const QgsLegendSettings& settings, ItemContext* ctx, double itemHeight ) const override;
246 
247  private:
248  QColor mColor;
249  QString mLabel;
250 };
251 
252 class QgsImageFetcher;
253 
260 {
261  Q_OBJECT
262 
263  public:
264  QgsWMSLegendNode( QgsLayerTreeLayer* nodeLayer, QObject* parent = 0 );
265 
266  virtual QVariant data( int role ) const override;
267 
268  virtual QSizeF drawSymbol( const QgsLegendSettings& settings, ItemContext* ctx, double itemHeight ) const override;
269 
270  virtual void invalidateMapBasedData() override;
271 
272  private slots:
273 
274  void getLegendGraphicFinished( const QImage& );
275  void getLegendGraphicErrored( const QString& );
276  void getLegendGraphicProgress( qint64, qint64 );
277 
278  private:
279 
280  // Lazily initializes mImage
281  const QImage& getLegendGraphic() const;
282 
283  QImage renderMessage( const QString& msg ) const;
284 
285  QImage mImage;
286 
287  bool mValid;
288 
289  mutable QScopedPointer<QgsImageFetcher> mFetcher;
290 };
291 
292 #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.
virtual bool setData(const QVariant &value, int role)
Set some data associated with the item.
Implementation of legend node interface for displaying raster legend entries.
virtual Qt::ItemFlags flags() const
Return item flags associated with the item.
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.
virtual QSizeF drawSymbol(const QgsLegendSettings &settings, ItemContext *ctx, double itemHeight) const
Draws symbol on the left side of the item.
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)
Contains information about the context of a rendering operation.
QPointF point
Top-left corner of the legend item.
virtual bool isScaleOK(double scale) const
virtual QVariant data(int role) const =0
Return data associated with the item.
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...
void setIconSize(const QSize &sz)
Set the icon size.
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.
typedef ItemFlags