QGIS API Documentation  3.8.0-Zanzibar (11aff65)
qgslayoutitemlegend.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgslayoutitemlegend.h
3  ---------------------
4  begin : October 2017
5  copyright : (C) 2017 by Nyall Dawson
6  email : nyall dot dawson at gmail dot com
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU General Public License as published by *
13  * the Free Software Foundation; either version 2 of the License, or *
14  * (at your option) any later version. *
15  * *
16  ***************************************************************************/
17 
18 #ifndef QGSLAYOUTITEMLEGEND_H
19 #define QGSLAYOUTITEMLEGEND_H
20 
21 #include "qgis_core.h"
22 #include "qgis_sip.h"
23 #include "qgslayoutitem.h"
24 #include "qgslayertreemodel.h"
25 #include "qgslegendsettings.h"
26 #include "qgslayertreegroup.h"
27 
28 class QgsLayerTreeModel;
29 class QgsSymbol;
30 class QgsLayoutItemMap;
31 class QgsLegendRenderer;
32 
41 class CORE_EXPORT QgsLegendModel : public QgsLayerTreeModel
42 {
43  Q_OBJECT
44 
45  public:
47  QgsLegendModel( QgsLayerTree *rootNode, QObject *parent SIP_TRANSFERTHIS = nullptr );
48 
49  QVariant data( const QModelIndex &index, int role ) const override;
50 
51  Qt::ItemFlags flags( const QModelIndex &index ) const override;
52 };
53 
54 
55 
61 class CORE_EXPORT QgsLayoutItemLegend : public QgsLayoutItem
62 {
63  Q_OBJECT
64 
65  public:
66 
70  QgsLayoutItemLegend( QgsLayout *layout );
71 
77  static QgsLayoutItemLegend *create( QgsLayout *layout ) SIP_FACTORY;
78 
79  int type() const override;
80  QIcon icon() const override;
81  QgsLayoutItem::Flags itemFlags() const override;
82  //Overridden to show legend title
83  QString displayName() const override;
84 
88  void adjustBoxSize();
89 
96  void setResizeToContents( bool enabled );
97 
102  bool resizeToContents() const;
103 
107  QgsLegendModel *model() { return mLegendModel.get(); }
108 
114  void setAutoUpdateModel( bool autoUpdate );
115 
121  bool autoUpdateModel() const;
122 
127  void setLegendFilterByMapEnabled( bool enabled );
128 
133  bool legendFilterByMapEnabled() const { return mLegendFilterByMap; }
134 
140  void setLegendFilterOutAtlas( bool doFilter );
141 
146  bool legendFilterOutAtlas() const;
147 
152  void setTitle( const QString &title );
153 
158  QString title() const;
159 
164  Qt::AlignmentFlag titleAlignment() const;
165 
170  void setTitleAlignment( Qt::AlignmentFlag alignment );
171 
176 
180  QgsLegendStyle style( QgsLegendStyle::Style s ) const;
181 
185  void setStyle( QgsLegendStyle::Style component, const QgsLegendStyle &style );
186 
191  QFont styleFont( QgsLegendStyle::Style component ) const;
192 
197  void setStyleFont( QgsLegendStyle::Style component, const QFont &font );
198 
202  void setStyleMargin( QgsLegendStyle::Style component, double margin );
203 
207  void setStyleMargin( QgsLegendStyle::Style component, QgsLegendStyle::Side side, double margin );
208 
213  double lineSpacing() const;
214 
219  void setLineSpacing( double spacing );
220 
225  double boxSpace() const;
226 
231  void setBoxSpace( double space );
232 
237  double columnSpace() const;
238 
243  void setColumnSpace( double spacing );
244 
249  QColor fontColor() const;
250 
255  void setFontColor( const QColor &color );
256 
261  double symbolWidth() const;
262 
267  void setSymbolWidth( double width );
268 
273  double symbolHeight() const;
274 
279  void setSymbolHeight( double height );
280 
285  double wmsLegendWidth() const;
286 
291  void setWmsLegendWidth( double width );
292 
297  double wmsLegendHeight() const;
298 
303  void setWmsLegendHeight( double height );
304 
309  void setWrapString( const QString &string );
310 
315  QString wrapString() const;
316 
321  int columnCount() const;
322 
327  void setColumnCount( int count );
328 
334  bool splitLayer() const;
335 
341  void setSplitLayer( bool enabled );
342 
347  bool equalColumnWidth() const;
348 
353  void setEqualColumnWidth( bool equalize );
354 
361  bool drawRasterStroke() const;
362 
370  void setDrawRasterStroke( bool enabled );
371 
379  QColor rasterStrokeColor() const;
380 
388  void setRasterStrokeColor( const QColor &color );
389 
397  double rasterStrokeWidth() const;
398 
406  void setRasterStrokeWidth( double width );
407 
412  void setLinkedMap( QgsLayoutItemMap *map );
413 
418  QgsLayoutItemMap *linkedMap() const { return mMap; }
419 
423  void updateLegend();
424 
428  void updateFilterByMap( bool redraw = true );
429 
433  const QgsLegendSettings &legendSettings() const { return mSettings; }
434 
435  void paint( QPainter *painter, const QStyleOptionGraphicsItem *itemStyle, QWidget *pWidget ) override;
436 
437  void finalizeRestoreFromXml() override;
438 
440 
441 
442  public slots:
443 
444  void refresh() override;
446 
447  protected:
448  void draw( QgsLayoutItemRenderContext &context ) override;
449  bool writePropertiesToElement( QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context ) const override;
450  bool readPropertiesFromElement( const QDomElement &element, const QDomDocument &document, const QgsReadWriteContext &context ) override;
451 
452  private slots:
453 
455  void invalidateCurrentMap();
456 
457  void updateFilterByMapAndRedraw();
458 
459 
461  void mapLayerStyleOverridesChanged();
462 
464  void onAtlasEnded();
465  void onAtlasFeature();
466 
467  void nodeCustomPropertyChanged( QgsLayerTreeNode *node, const QString &key );
468 
469  private:
470  QgsLayoutItemLegend() = delete;
471 
473  void setCustomLayerTree( QgsLayerTree *rootGroup );
474 
475  void setupMapConnections( QgsLayoutItemMap *map, bool connect = true );
476 
477  std::unique_ptr< QgsLegendModel > mLegendModel;
478  std::unique_ptr< QgsLayerTreeGroup > mCustomLayerTree;
479 
480  QgsLegendSettings mSettings;
481 
482  QString mTitle;
483  int mColumnCount = 1;
484 
485  QString mMapUuid;
486  QgsLayoutItemMap *mMap = nullptr;
487 
488  bool mLegendFilterByMap = false;
489  bool mLegendFilterByExpression = false;
490 
492  bool mFilterOutAtlas = false;
493 
495  bool mFilterAskedForUpdate = false;
497  void doUpdateFilterByMap();
498 
499  bool mInAtlas = false;
500 
502  bool mInitialMapScaleCalculated = false;
503 
505  bool mForceResize = false;
506 
508  bool mSizeToContents = true;
509 
511 
512 };
513 
514 #endif // QGSLAYOUTITEMLEGEND_H
515 
The class is used as a container of context for various read/write operations on other objects...
QgsExpressionContext createExpressionContext() const override
This method needs to be reimplemented in all classes which implement this interface and return an exp...
virtual QIcon icon() const
Returns the item&#39;s icon.
Item model implementation based on layer tree model for layout legend.
QgsLegendModel * model()
Returns the legend model.
Base class for graphical items within a QgsLayout.
#define SIP_TRANSFERTHIS
Definition: qgis_sip.h:46
int type() const override
Returns a unique graphics item type identifier.
Abstract base class for all rendered symbols.
Definition: qgssymbol.h:61
const QgsLegendSettings & legendSettings() const
Returns the legend&#39;s renderer settings object.
bool legendFilterByMapEnabled() const
Find out whether legend items are filtered to show just the ones visible in the associated map...
Composer legend components style.
virtual void refreshDataDefinedProperty(QgsLayoutObject::DataDefinedProperty property=QgsLayoutObject::AllProperties)
Refreshes a data defined property for the item by reevaluating the property&#39;s value and redrawing the...
The QgsLayerTreeModel class is model implementation for Qt item views framework.
Layout graphical items for displaying a map.
Namespace with helper functions for layer tree operations.
Definition: qgslayertree.h:32
virtual bool readPropertiesFromElement(const QDomElement &element, const QDomDocument &document, const QgsReadWriteContext &context)
Sets item state from a DOM element.
void paint(QPainter *painter, const QStyleOptionGraphicsItem *itemStyle, QWidget *pWidget) override
Handles preparing a paint surface for the layout item and painting the item&#39;s content.
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...
This class is a base class for nodes in a layer tree.
#define SIP_FACTORY
Definition: qgis_sip.h:69
Side
Margin side.
Base class for layouts, which can contain items such as maps, labels, scalebars, etc.
Definition: qgslayout.h:49
virtual Flags itemFlags() const
Returns the item&#39;s flags, which indicate how the item behaves.
Contains settings and helpers relating to a render of a QgsLayoutItem.
Definition: qgslayoutitem.h:44
virtual void finalizeRestoreFromXml()
Called after all pending items have been restored from XML.
virtual QString displayName() const
Gets item display name.
QgsCompositionConverter class converts a QGIS 2.x composition to a QGIS 3.x layout.
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const override
virtual bool writePropertiesToElement(QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context) const
Stores item state within an XML DOM element.
QgsLayoutItemMap * linkedMap() const
Returns the associated map.
virtual void draw(QgsLayoutItemRenderContext &context)=0
Draws the item&#39;s contents using the specified item render context.
Flags flags() const
Returns OR-ed combination of model flags.
A layout item subclass for map legends.
DataDefinedProperty
Data defined properties for different item types.
void refresh() override
Refreshes the item, causing a recalculation of any property overrides and recalculation of its positi...
The QgsLegendRenderer class handles automatic layout and rendering of legend.
All properties for item.