QGIS API Documentation  2.18.21-Las Palmas (9fba24a)
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, QSizeF symbolSize ) const;
123 
124  signals:
126  void dataChanged();
127 
128  protected:
130  explicit QgsLayerTreeModelLegendNode( QgsLayerTreeLayer* nodeL, QObject* parent = nullptr );
131 
132  protected:
136 };
137 
138 #include "qgslegendsymbolitemv2.h"
139 
147 {
148  Q_OBJECT
149 
150  public:
151  QgsSymbolV2LegendNode( QgsLayerTreeLayer* nodeLayer, const QgsLegendSymbolItemV2& item, QObject* parent = nullptr );
153 
154  virtual Qt::ItemFlags flags() const override;
155  virtual QVariant data( int role ) const override;
156  virtual bool setData( const QVariant& value, int role ) override;
157 
158  QSizeF drawSymbol( const QgsLegendSettings& settings, ItemContext* ctx, double itemHeight ) const override;
159 
160  virtual void setEmbeddedInParent( bool embedded ) override;
161 
162  void setUserLabel( const QString& userLabel ) override { mUserLabel = userLabel; updateLabel(); }
163 
164  virtual bool isScaleOK( double scale ) const override { return mItem.isScaleOK( scale ); }
165 
166  virtual void invalidateMapBasedData() override;
167 
170  void setIconSize( QSize sz ) { mIconSize = sz; }
172  QSize iconSize() const { return mIconSize; }
173 
176  QSize minimumIconSize() const;
177 
182  const QgsSymbolV2* symbol() const;
183 
190  void setSymbol( QgsSymbolV2* symbol );
191 
192  public slots:
193 
198  void checkAllItems();
199 
204  void uncheckAllItems();
205 
206  private:
207  void updateLabel();
208 
209  private:
210  QgsLegendSymbolItemV2 mItem;
211  mutable QPixmap mPixmap; // cached symbol preview
212  QString mLabel;
213  bool mSymbolUsesMapUnits;
214  QSize mIconSize;
215 
216  // ident the symbol icon to make it look like a tree structure
217  static const int indentSize = 20;
218 
219  // return a temporary context or null if legendMapViewData are not valid
220  QgsRenderContext * createTemporaryRenderContext() const;
221 
225  void checkAll( bool state );
226 };
227 
228 
235 {
236  Q_OBJECT
237 
238  public:
239  QgsSimpleLegendNode( QgsLayerTreeLayer* nodeLayer, const QString& label, const QIcon& icon = QIcon(), QObject* parent = nullptr, const QString& key = QString() );
240 
241  virtual QVariant data( int role ) const override;
242 
243  private:
244  QString mLabel;
245  QString mId;
246  QIcon mIcon;
247  QString mKey;
248 };
249 
250 
257 {
258  Q_OBJECT
259 
260  public:
261  QgsImageLegendNode( QgsLayerTreeLayer* nodeLayer, const QImage& img, QObject* parent = nullptr );
262 
263  virtual QVariant data( int role ) const override;
264 
265  QSizeF drawSymbol( const QgsLegendSettings& settings, ItemContext* ctx, double itemHeight ) const override;
266 
267  private:
268  QImage mImage;
269 };
270 
277 {
278  Q_OBJECT
279 
280  public:
281  QgsRasterSymbolLegendNode( QgsLayerTreeLayer* nodeLayer, const QColor& color, const QString& label, QObject* parent = nullptr );
282 
283  virtual QVariant data( int role ) const override;
284 
285  QSizeF drawSymbol( const QgsLegendSettings& settings, ItemContext* ctx, double itemHeight ) const override;
286 
287  private:
288  QColor mColor;
289  QString mLabel;
290 };
291 
292 class QgsImageFetcher;
293 
300 {
301  Q_OBJECT
302 
303  public:
304  QgsWMSLegendNode( QgsLayerTreeLayer* nodeLayer, QObject* parent = nullptr );
305 
306  virtual QVariant data( int role ) const override;
307 
308  virtual QSizeF drawSymbol( const QgsLegendSettings& settings, ItemContext* ctx, double itemHeight ) const override;
309 
310  virtual void invalidateMapBasedData() override;
311 
312  private slots:
313 
314  void getLegendGraphicFinished( const QImage& );
315  void getLegendGraphicErrored( const QString& );
316  void getLegendGraphicProgress( qint64, qint64 );
317 
318  private:
319 
320  // Lazily initializes mImage
321  const QImage& getLegendGraphic() const;
322 
323  QImage renderMessage( const QString& msg ) const;
324 
325  QImage mImage;
326 
327  bool mValid;
328 
329  mutable QScopedPointer<QgsImageFetcher> mFetcher;
330 };
331 
332 #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.
QgsLayerTreeLayer * layerNode() const
Return pointer to the parent layer node.
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 bool isScaleOK(double scale) const
The QgsLegendSettings class stores the appearance and layout settings for legend drawing with QgsLege...
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.
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...
void setIconSize(QSize sz)
Set the icon size.
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