QGIS API Documentation  3.9.0-Master (224899f119)
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 #include "qgsexpressioncontext.h"
28 
29 class QgsLayerTreeModel;
30 class QgsSymbol;
31 class QgsLayoutItemMap;
32 class QgsLegendRenderer;
34 
43 class CORE_EXPORT QgsLegendModel : public QgsLayerTreeModel
44 {
45  Q_OBJECT
46 
47  public:
49  QgsLegendModel( QgsLayerTree *rootNode, QObject *parent SIP_TRANSFERTHIS = nullptr, QgsLayoutItemLegend *layout = nullptr );
50 
52  QgsLegendModel( QgsLayerTree *rootNode, QgsLayoutItemLegend *layout );
53 
54  QVariant data( const QModelIndex &index, int role ) const override;
55 
56  Qt::ItemFlags flags( const QModelIndex &index ) const override;
57 
58  signals:
59 
64  void refreshLegend();
65 
66  private slots:
67 
72  void forceRefresh();
73 
74  private:
75 
83  QList<QgsLayerTreeModelLegendNode *> layerLegendNodes( QgsLayerTreeLayer *nodeLayer, bool skipNodeEmbeddedInParent = false ) const;
84 
89  QgsLayoutItemLegend *mLayoutLegend = nullptr;
90 
91 };
92 
93 
94 
100 class CORE_EXPORT QgsLayoutItemLegend : public QgsLayoutItem
101 {
102  Q_OBJECT
103 
104  public:
105 
109  QgsLayoutItemLegend( QgsLayout *layout );
110 
116  static QgsLayoutItemLegend *create( QgsLayout *layout ) SIP_FACTORY;
117 
118  int type() const override;
119  QIcon icon() const override;
120  QgsLayoutItem::Flags itemFlags() const override;
121  //Overridden to show legend title
122  QString displayName() const override;
123 
127  void adjustBoxSize();
128 
135  void setResizeToContents( bool enabled );
136 
141  bool resizeToContents() const;
142 
146  QgsLegendModel *model() { return mLegendModel.get(); }
147 
153  void setAutoUpdateModel( bool autoUpdate );
154 
160  bool autoUpdateModel() const;
161 
166  void setLegendFilterByMapEnabled( bool enabled );
167 
172  bool legendFilterByMapEnabled() const { return mLegendFilterByMap; }
173 
179  void setLegendFilterOutAtlas( bool doFilter );
180 
185  bool legendFilterOutAtlas() const;
186 
191  void setTitle( const QString &title );
192 
197  QString title() const;
198 
203  Qt::AlignmentFlag titleAlignment() const;
204 
209  void setTitleAlignment( Qt::AlignmentFlag alignment );
210 
215 
219  QgsLegendStyle style( QgsLegendStyle::Style s ) const;
220 
224  void setStyle( QgsLegendStyle::Style component, const QgsLegendStyle &style );
225 
230  QFont styleFont( QgsLegendStyle::Style component ) const;
231 
236  void setStyleFont( QgsLegendStyle::Style component, const QFont &font );
237 
241  void setStyleMargin( QgsLegendStyle::Style component, double margin );
242 
246  void setStyleMargin( QgsLegendStyle::Style component, QgsLegendStyle::Side side, double margin );
247 
252  double lineSpacing() const;
253 
258  void setLineSpacing( double spacing );
259 
264  double boxSpace() const;
265 
270  void setBoxSpace( double space );
271 
276  double columnSpace() const;
277 
282  void setColumnSpace( double spacing );
283 
288  QColor fontColor() const;
289 
294  void setFontColor( const QColor &color );
295 
300  double symbolWidth() const;
301 
306  void setSymbolWidth( double width );
307 
316  void setSymbolAlignment( Qt::AlignmentFlag alignment );
317 
326  Qt::AlignmentFlag symbolAlignment() const;
327 
332  double symbolHeight() const;
333 
338  void setSymbolHeight( double height );
339 
344  double wmsLegendWidth() const;
345 
350  void setWmsLegendWidth( double width );
351 
356  double wmsLegendHeight() const;
357 
362  void setWmsLegendHeight( double height );
363 
368  void setWrapString( const QString &string );
369 
374  QString wrapString() const;
375 
380  int columnCount() const;
381 
386  void setColumnCount( int count );
387 
393  bool splitLayer() const;
394 
400  void setSplitLayer( bool enabled );
401 
406  bool equalColumnWidth() const;
407 
412  void setEqualColumnWidth( bool equalize );
413 
420  bool drawRasterStroke() const;
421 
429  void setDrawRasterStroke( bool enabled );
430 
438  QColor rasterStrokeColor() const;
439 
447  void setRasterStrokeColor( const QColor &color );
448 
456  double rasterStrokeWidth() const;
457 
465  void setRasterStrokeWidth( double width );
466 
471  void setLinkedMap( QgsLayoutItemMap *map );
472 
477  QgsLayoutItemMap *linkedMap() const { return mMap; }
478 
482  void updateLegend();
483 
487  void updateFilterByMap( bool redraw = true );
488 
492  const QgsLegendSettings &legendSettings() const { return mSettings; }
493 
494  void paint( QPainter *painter, const QStyleOptionGraphicsItem *itemStyle, QWidget *pWidget ) override;
495 
496  void finalizeRestoreFromXml() override;
497 
499  ExportLayerBehavior exportLayerBehavior() const override;
500 
501  public slots:
502 
503  void refresh() override;
505 
506  protected:
507  void draw( QgsLayoutItemRenderContext &context ) override;
508  bool writePropertiesToElement( QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context ) const override;
509  bool readPropertiesFromElement( const QDomElement &element, const QDomDocument &document, const QgsReadWriteContext &context ) override;
510 
511  private slots:
512 
514  void invalidateCurrentMap();
515 
516  void updateFilterByMapAndRedraw();
517 
518 
520  void mapLayerStyleOverridesChanged();
521 
523  void onAtlasEnded();
524  void onAtlasFeature();
525 
526  void nodeCustomPropertyChanged( QgsLayerTreeNode *node, const QString &key );
527 
528 
529  private:
530  QgsLayoutItemLegend() = delete;
531 
533  void setCustomLayerTree( QgsLayerTree *rootGroup );
534 
535  void setupMapConnections( QgsLayoutItemMap *map, bool connect = true );
536 
537  std::unique_ptr< QgsLegendModel > mLegendModel;
538  std::unique_ptr< QgsLayerTreeGroup > mCustomLayerTree;
539 
540  QgsLegendSettings mSettings;
541 
542  QString mTitle;
543  int mColumnCount = 1;
544 
545  QString mMapUuid;
546  QgsLayoutItemMap *mMap = nullptr;
547 
548  bool mLegendFilterByMap = false;
549  bool mLegendFilterByExpression = false;
550 
552  bool mFilterOutAtlas = false;
553 
555  bool mFilterAskedForUpdate = false;
557  void doUpdateFilterByMap();
558 
559  bool mInAtlas = false;
560 
562  bool mInitialMapScaleCalculated = false;
563 
565  bool mForceResize = false;
566 
568  bool mSizeToContents = true;
569 
571 
572 };
573 
574 #endif // QGSLAYOUTITEMLEGEND_H
575 
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:53
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...
Contains detailed styling information relating to how a layout legend should be rendered.
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:76
Side
Margin sides.
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.
Style
Component of legends which can be styled.
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.
ExportLayerBehavior
Behavior of item when exporting to layered outputs.
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.
QList< QgsLayerTreeModelLegendNode * > layerLegendNodes(QgsLayerTreeLayer *nodeLayer, bool skipNodeEmbeddedInParent=false)
Returns filtered list of active legend nodes attached to a particular layer node (by default it retur...
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.
virtual ExportLayerBehavior exportLayerBehavior() const
Returns the behavior of this item during exporting to layered exports (e.g.
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...
Layer tree node points to a map layer.
The QgsLegendRenderer class handles automatic layout and rendering of legend.
All properties for item.