QGIS API Documentation  3.10.0-A Coruña (6c816b4204)
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_sip.h"
28 
29 #include "qgsrasterdataprovider.h" // for QgsImageFetcher dtor visibility
30 #include "qgsexpressioncontext.h"
31 
32 class QgsLayerTreeLayer;
33 class QgsLayerTreeModel;
34 class QgsLegendSettings;
35 class QgsMapSettings;
36 class QgsSymbol;
37 class QgsRenderContext;
38 
49 class CORE_EXPORT QgsLayerTreeModelLegendNode : public QObject
50 {
51  Q_OBJECT
52 #ifdef SIP_RUN
54  if ( qobject_cast<QgsSymbolLegendNode *> ( sipCpp ) )
55  sipType = sipType_QgsSymbolLegendNode;
56  else
57  sipType = 0;
58  SIP_END
59 #endif
60  public:
61 
63  {
64  RuleKeyRole = Qt::UserRole,
65  ParentRuleKeyRole
66  };
67 
69  QgsLayerTreeLayer *layerNode() const { return mLayerNode; }
70 
72  QgsLayerTreeModel *model() const;
73 
75  virtual Qt::ItemFlags flags() const;
76 
78  virtual QVariant data( int role ) const = 0;
79 
81  virtual bool setData( const QVariant &value, int role );
82 
83  virtual bool isEmbeddedInParent() const { return mEmbeddedInParent; }
84  virtual void setEmbeddedInParent( bool embedded ) { mEmbeddedInParent = embedded; }
85 
86  virtual QString userLabel() const { return mUserLabel; }
87  virtual void setUserLabel( const QString &userLabel ) { mUserLabel = userLabel; }
88 
89  virtual bool isScaleOK( double scale ) const { Q_UNUSED( scale ) return true; }
90 
94  virtual void invalidateMapBasedData() {}
95 
96  struct ItemContext
97  {
98  Q_NOWARN_DEPRECATED_PUSH //because of deprecated members
99  ItemContext() = default;
101 
103  QgsRenderContext *context = nullptr;
105  QPainter *painter = nullptr;
106 
111  Q_DECL_DEPRECATED QPointF point;
112 
117  Q_DECL_DEPRECATED double labelXOffset = 0.0;
118 
123  double top = 0.0;
124 
131  double columnLeft = 0.0;
132 
139  double columnRight = 0.0;
140 
146  double maxSiblingSymbolWidth = 0.0;
147 
148  };
149 
150  struct ItemMetrics
151  {
152  QSizeF symbolSize;
153  QSizeF labelSize;
154  };
155 
162  virtual ItemMetrics draw( const QgsLegendSettings &settings, ItemContext *ctx );
163 
172  void exportToJson( const QgsLegendSettings &settings, const QgsRenderContext &context, QJsonObject &json );
173 
181  virtual QSizeF drawSymbol( const QgsLegendSettings &settings, ItemContext *ctx, double itemHeight ) const;
182 
190  virtual void exportSymbolToJson( const QgsLegendSettings &settings, const QgsRenderContext &context, QJsonObject &json ) const;
191 
199  virtual QSizeF drawSymbolText( const QgsLegendSettings &settings, ItemContext *ctx, QSizeF symbolSize ) const;
200 
207  void exportSymbolTextToJson( const QgsLegendSettings &settings, QJsonObject &json ) const;
208 
209  signals:
211  void dataChanged();
212 
213  protected:
215  explicit QgsLayerTreeModelLegendNode( QgsLayerTreeLayer *nodeL, QObject *parent SIP_TRANSFERTHIS = nullptr );
216 
218  QgsRenderContext *createTemporaryRenderContext() const SIP_FACTORY;
219 
220  protected:
221  QgsLayerTreeLayer *mLayerNode = nullptr;
222  bool mEmbeddedInParent;
223  QString mUserLabel;
224 };
225 
226 #include "qgslegendsymbolitem.h"
227 #include "qgstextrenderer.h"
228 
237 {
238  Q_OBJECT
239 
240 
241  public:
242 
249  QgsSymbolLegendNode( QgsLayerTreeLayer *nodeLayer, const QgsLegendSymbolItem &item, QObject *parent SIP_TRANSFERTHIS = nullptr );
250 
251  Qt::ItemFlags flags() const override;
252  QVariant data( int role ) const override;
253  bool setData( const QVariant &value, int role ) override;
254 
255  QSizeF drawSymbol( const QgsLegendSettings &settings, ItemContext *ctx, double itemHeight ) const override;
256 
257  void exportSymbolToJson( const QgsLegendSettings &settings, const QgsRenderContext &context, QJsonObject &json ) const override;
258 
259  void setEmbeddedInParent( bool embedded ) override;
260 
261  void setUserLabel( const QString &userLabel ) override { mUserLabel = userLabel; updateLabel(); }
262 
263  bool isScaleOK( double scale ) const override { return mItem.isScaleOK( scale ); }
264 
265  void invalidateMapBasedData() override;
266 
271  void setIconSize( QSize sz ) { mIconSize = sz; }
273  QSize iconSize() const { return mIconSize; }
274 
282  QSize minimumIconSize() const;
283 
290  QSize minimumIconSize( QgsRenderContext *context ) const;
291 
297  const QgsSymbol *symbol() const;
298 
306  void setSymbol( QgsSymbol *symbol SIP_TRANSFER );
307 
312  QString textOnSymbolLabel() const { return mTextOnSymbolLabel; }
313 
318  void setTextOnSymbolLabel( const QString &label ) { mTextOnSymbolLabel = label; }
319 
324  QgsTextFormat textOnSymbolTextFormat() const { return mTextOnSymbolTextFormat; }
325 
330  void setTextOnSymbolTextFormat( const QgsTextFormat &format ) { mTextOnSymbolTextFormat = format; }
331 
336  QString symbolLabel() const;
337 
344  QString evaluateLabel( const QgsExpressionContext &context = QgsExpressionContext(), const QString &label = QString() );
345 
346  public slots:
347 
354  void checkAllItems();
355 
362  void uncheckAllItems();
363 
370  void toggleAllItems();
371 
372  private:
373  void updateLabel();
374 
375  private:
376  QgsLegendSymbolItem mItem;
377  mutable QPixmap mPixmap; // cached symbol preview
378  QString mLabel;
379  bool mSymbolUsesMapUnits;
380  QSize mIconSize;
381 
382  QString mTextOnSymbolLabel;
383  QgsTextFormat mTextOnSymbolTextFormat;
384 
385  // ident the symbol icon to make it look like a tree structure
386  static const int INDENT_SIZE = 20;
387 
392  QgsExpressionContextScope *createSymbolScope() const SIP_FACTORY;
393 
398  void checkAll( bool state );
399 };
400 
401 
409 {
410  Q_OBJECT
411 
412  public:
413 
422  QgsSimpleLegendNode( QgsLayerTreeLayer *nodeLayer, const QString &label, const QIcon &icon = QIcon(), QObject *parent SIP_TRANSFERTHIS = nullptr, const QString &key = QString() );
423 
424  QVariant data( int role ) const override;
425 
426  private:
427  QString mLabel;
428  QString mId;
429  QIcon mIcon;
430  QString mKey;
431 };
432 
433 
441 {
442  Q_OBJECT
443 
444  public:
445 
452  QgsImageLegendNode( QgsLayerTreeLayer *nodeLayer, const QImage &img, QObject *parent SIP_TRANSFERTHIS = nullptr );
453 
454  QVariant data( int role ) const override;
455 
456  QSizeF drawSymbol( const QgsLegendSettings &settings, ItemContext *ctx, double itemHeight ) const override;
457 
458  void exportSymbolToJson( const QgsLegendSettings &settings, const QgsRenderContext &context, QJsonObject &json ) const override;
459 
460  private:
461  QImage mImage;
462 };
463 
471 {
472  Q_OBJECT
473 
474  public:
475 
483  QgsRasterSymbolLegendNode( QgsLayerTreeLayer *nodeLayer, const QColor &color, const QString &label, QObject *parent SIP_TRANSFERTHIS = nullptr );
484 
485  QVariant data( int role ) const override;
486 
487  QSizeF drawSymbol( const QgsLegendSettings &settings, ItemContext *ctx, double itemHeight ) const override;
488 
489  void exportSymbolToJson( const QgsLegendSettings &settings, const QgsRenderContext &context, QJsonObject &json ) const override;
490 
491  private:
492  QColor mColor;
493  QString mLabel;
494 };
495 
496 class QgsImageFetcher;
497 
505 {
506  Q_OBJECT
507 
508  public:
509 
515  QgsWmsLegendNode( QgsLayerTreeLayer *nodeLayer, QObject *parent SIP_TRANSFERTHIS = nullptr );
516 
517  QVariant data( int role ) const override;
518 
519  QSizeF drawSymbol( const QgsLegendSettings &settings, ItemContext *ctx, double itemHeight ) const override;
520 
521  void exportSymbolToJson( const QgsLegendSettings &settings, const QgsRenderContext &context, QJsonObject &json ) const override;
522 
523  void invalidateMapBasedData() override;
524 
525  private slots:
526 
527  void getLegendGraphicFinished( const QImage & );
528  void getLegendGraphicErrored( const QString & );
529  void getLegendGraphicProgress( qint64, qint64 );
530 
531  private:
532 
533  // Lazily initializes mImage
534  QImage getLegendGraphic() const;
535 
536  QImage renderMessage( const QString &msg ) const;
537 
538  QImage mImage;
539 
540  bool mValid;
541 
542  mutable std::unique_ptr<QgsImageFetcher> mFetcher;
543 };
544 
545 
552 {
553  Q_OBJECT
554 
555  public:
557  QgsDataDefinedSizeLegendNode( QgsLayerTreeLayer *nodeLayer, const QgsDataDefinedSizeLegend &settings, QObject *parent SIP_TRANSFERTHIS = nullptr );
558  ~QgsDataDefinedSizeLegendNode() override;
559 
560  QVariant data( int role ) const override;
561 
562  ItemMetrics draw( const QgsLegendSettings &settings, ItemContext *ctx ) override;
563 
564  private:
565  void cacheImage() const;
566  QgsDataDefinedSizeLegend *mSettings = nullptr;
567  mutable QImage mImage;
568 };
569 
570 #endif // QGSLAYERTREEMODELLEGENDNODE_H
Implementation of legend node interface for displaying arbitrary raster image.
#define SIP_TRANSFERTHIS
Definition: qgis_sip.h:53
Abstract base class for all rendered symbols.
Definition: qgssymbol.h:61
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.
#define Q_NOWARN_DEPRECATED_PUSH
Definition: qgis.h:649
void setTextOnSymbolLabel(const QString &label)
Sets label of text to be shown on top of the symbol.
Implementation of legend node interface for displaying preview of vector symbols and their labels and...
QgsLayerTreeLayer * layerNode() const
Returns 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
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
The QgsLegendSettings class stores the appearance and layout settings for legend drawing with QgsLege...
#define SIP_TRANSFER
Definition: qgis_sip.h:36
#define SIP_END
Definition: qgis_sip.h:189
QString textOnSymbolLabel() const
Returns label of text to be shown on top of the symbol.
#define SIP_FACTORY
Definition: qgis_sip.h:76
Implementation of legend node interface for displaying arbitrary label with icon. ...
Single scope for storing variables and functions for use within a QgsExpressionContext.
QgsTextFormat textOnSymbolTextFormat() const
Returns text format of the label to be shown on top of the symbol.
void setTextOnSymbolTextFormat(const QgsTextFormat &format)
Sets format of text to be shown on top of the symbol.
#define Q_NOWARN_DEPRECATED_POP
Definition: qgis.h:650
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.
bool isScaleOK(double scale) const override
#define SIP_CONVERT_TO_SUBCLASS_CODE(code)
Definition: qgis_sip.h:172
The QgsLegendRendererItem class is abstract interface for legend items returned from QgsMapLayerLegen...
Q_DECL_DEPRECATED QPointF point
Top-left corner of the legend item.
Container for all settings relating to text rendering.
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.