QGIS API Documentation  3.14.0-Pi (9f7028fd23)
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 
66  QList<QgsLayerTreeModelLegendNode *> layerLegendNodes( QgsLayerTreeLayer *nodeLayer, bool skipNodeEmbeddedInParent = false ) const SIP_SKIP;
67 
72  void clearCachedData( QgsLayerTreeNode *node ) const;
73 
74  signals:
75 
80  void refreshLegend();
81 
82  private slots:
83 
88  void forceRefresh();
89 
90  private:
91 
96  QgsLayoutItemLegend *mLayoutLegend = nullptr;
97 
102  QString evaluateLayerExpressions( QgsLayerTreeLayer *nodeLayer ) const;
103 
104 };
105 
106 
107 
113 class CORE_EXPORT QgsLayoutItemLegend : public QgsLayoutItem
114 {
115  Q_OBJECT
116 
117  public:
118 
122  QgsLayoutItemLegend( QgsLayout *layout );
123 
129  static QgsLayoutItemLegend *create( QgsLayout *layout ) SIP_FACTORY;
130 
131  int type() const override;
132  QIcon icon() const override;
133  QgsLayoutItem::Flags itemFlags() const override;
134  //Overridden to show legend title
135  QString displayName() const override;
136 
140  void adjustBoxSize();
141 
148  void setResizeToContents( bool enabled );
149 
154  bool resizeToContents() const;
155 
159  QgsLegendModel *model() { return mLegendModel.get(); }
160 
166  void setAutoUpdateModel( bool autoUpdate );
167 
173  bool autoUpdateModel() const;
174 
179  void setLegendFilterByMapEnabled( bool enabled );
180 
185  bool legendFilterByMapEnabled() const { return mLegendFilterByMap; }
186 
192  void setLegendFilterOutAtlas( bool doFilter );
193 
198  bool legendFilterOutAtlas() const;
199 
204  void setTitle( const QString &title );
205 
210  QString title() const;
211 
216  Qt::AlignmentFlag titleAlignment() const;
217 
222  void setTitleAlignment( Qt::AlignmentFlag alignment );
223 
228 
232  QgsLegendStyle style( QgsLegendStyle::Style s ) const;
233 
237  void setStyle( QgsLegendStyle::Style component, const QgsLegendStyle &style );
238 
243  QFont styleFont( QgsLegendStyle::Style component ) const;
244 
249  void setStyleFont( QgsLegendStyle::Style component, const QFont &font );
250 
254  void setStyleMargin( QgsLegendStyle::Style component, double margin );
255 
259  void setStyleMargin( QgsLegendStyle::Style component, QgsLegendStyle::Side side, double margin );
260 
265  double lineSpacing() const;
266 
271  void setLineSpacing( double spacing );
272 
277  double boxSpace() const;
278 
283  void setBoxSpace( double space );
284 
289  double columnSpace() const;
290 
295  void setColumnSpace( double spacing );
296 
301  QColor fontColor() const;
302 
307  void setFontColor( const QColor &color );
308 
313  double symbolWidth() const;
314 
319  void setSymbolWidth( double width );
320 
329  void setSymbolAlignment( Qt::AlignmentFlag alignment );
330 
339  Qt::AlignmentFlag symbolAlignment() const;
340 
345  double symbolHeight() const;
346 
351  void setSymbolHeight( double height );
352 
357  double wmsLegendWidth() const;
358 
363  void setWmsLegendWidth( double width );
364 
369  double wmsLegendHeight() const;
370 
375  void setWmsLegendHeight( double height );
376 
381  void setWrapString( const QString &string );
382 
387  QString wrapString() const;
388 
393  int columnCount() const;
394 
399  void setColumnCount( int count );
400 
406  bool splitLayer() const;
407 
413  void setSplitLayer( bool enabled );
414 
419  bool equalColumnWidth() const;
420 
425  void setEqualColumnWidth( bool equalize );
426 
433  bool drawRasterStroke() const;
434 
442  void setDrawRasterStroke( bool enabled );
443 
451  QColor rasterStrokeColor() const;
452 
460  void setRasterStrokeColor( const QColor &color );
461 
469  double rasterStrokeWidth() const;
470 
478  void setRasterStrokeWidth( double width );
479 
484  void setLinkedMap( QgsLayoutItemMap *map );
485 
490  QgsLayoutItemMap *linkedMap() const { return mMap; }
491 
499  QString themeName() const;
500 
504  void updateLegend();
505 
509  void updateFilterByMap( bool redraw = true );
510 
514  const QgsLegendSettings &legendSettings() const { return mSettings; }
515 
516  void paint( QPainter *painter, const QStyleOptionGraphicsItem *itemStyle, QWidget *pWidget ) override;
517 
518  void finalizeRestoreFromXml() override;
519 
521  ExportLayerBehavior exportLayerBehavior() const override;
522  bool accept( QgsStyleEntityVisitorInterface *visitor ) const override;
523 
524  public slots:
525 
526  void refresh() override;
528 
529  protected:
530  void draw( QgsLayoutItemRenderContext &context ) override;
531  bool writePropertiesToElement( QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context ) const override;
532  bool readPropertiesFromElement( const QDomElement &element, const QDomDocument &document, const QgsReadWriteContext &context ) override;
533 
534  private slots:
535 
537  void invalidateCurrentMap();
538 
539  void updateFilterByMapAndRedraw();
540 
541 
543  void mapLayerStyleOverridesChanged();
545  void mapThemeChanged( const QString &theme );
546 
548  void onAtlasEnded();
549  void onAtlasFeature();
550 
551  void nodeCustomPropertyChanged( QgsLayerTreeNode *node, const QString &key );
552 
554  void clearLegendCachedData();
555 
556  private:
557  QgsLayoutItemLegend() = delete;
558 
560  void setCustomLayerTree( QgsLayerTree *rootGroup );
561 
562  void setupMapConnections( QgsLayoutItemMap *map, bool connect = true );
563 
564  void setModelStyleOverrides( const QMap<QString, QString> &overrides );
565 
566  std::unique_ptr< QgsLegendModel > mLegendModel;
567  std::unique_ptr< QgsLayerTreeGroup > mCustomLayerTree;
568 
569  QgsLegendSettings mSettings;
570 
571  QString mTitle;
572  int mColumnCount = 1;
573 
574  QString mMapUuid;
575  QgsLayoutItemMap *mMap = nullptr;
576 
577  bool mLegendFilterByMap = false;
578  bool mLegendFilterByExpression = false;
579 
581  bool mFilterOutAtlas = false;
582 
584  bool mFilterAskedForUpdate = false;
586  void doUpdateFilterByMap();
587 
588  bool mInAtlas = false;
589 
591  bool mInitialMapScaleCalculated = false;
592 
594  bool mForceResize = false;
595 
597  bool mSizeToContents = true;
598 
600  QString mThemeName;
601 
603 
604 };
605 
606 #endif // QGSLAYOUTITEMLEGEND_H
607 
QgsExpressionContext
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
Definition: qgsexpressioncontext.h:369
QgsLegendStyle::Style
Style
Component of legends which can be styled.
Definition: qgslegendstyle.h:53
QgsLayerTreeNode
Definition: qgslayertreenode.h:74
QgsLayoutItem::writePropertiesToElement
virtual bool writePropertiesToElement(QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context) const
Stores item state within an XML DOM element.
Definition: qgslayoutitem.cpp:1318
QgsReadWriteContext
Definition: qgsreadwritecontext.h:34
QgsLayoutItem::icon
virtual QIcon icon() const
Returns the item's icon.
Definition: qgslayoutitem.h:331
QgsLayoutItem::finalizeRestoreFromXml
virtual void finalizeRestoreFromXml()
Called after all pending items have been restored from XML.
Definition: qgslayoutitem.cpp:825
QgsLayoutItemRenderContext
Definition: qgslayoutitem.h:44
QgsStyleEntityVisitorInterface
Definition: qgsstyleentityvisitor.h:33
QgsLayoutItem::refreshDataDefinedProperty
virtual void refreshDataDefinedProperty(QgsLayoutObject::DataDefinedProperty property=QgsLayoutObject::AllProperties)
Refreshes a data defined property for the item by reevaluating the property's value and redrawing the...
Definition: qgslayoutitem.cpp:1082
QgsLayoutItemLegend::model
QgsLegendModel * model()
Returns the legend model.
Definition: qgslayoutitemlegend.h:159
QgsLayerTreeModel
Definition: qgslayertreemodel.h:53
QgsSymbol
Definition: qgssymbol.h:63
QgsLayoutItem::readPropertiesFromElement
virtual bool readPropertiesFromElement(const QDomElement &element, const QDomDocument &document, const QgsReadWriteContext &context)
Sets item state from a DOM element.
Definition: qgslayoutitem.cpp:1323
QgsLayoutItem::draw
virtual void draw(QgsLayoutItemRenderContext &context)=0
Draws the item's contents using the specified item render context.
SIP_FACTORY
#define SIP_FACTORY
Definition: qgis_sip.h:76
QgsLayoutItemLegend::legendSettings
const QgsLegendSettings & legendSettings() const
Returns the legend's renderer settings object.
Definition: qgslayoutitemlegend.h:514
QgsLayoutItem::type
int type() const override
Returns a unique graphics item type identifier.
Definition: qgslayoutitem.cpp:124
QgsLayoutItemLegend
Definition: qgslayoutitemlegend.h:113
QgsLayoutObject::AllProperties
@ AllProperties
All properties for item.
Definition: qgslayoutobject.h:135
qgsexpressioncontext.h
QgsLayoutItemLegend::legendFilterByMapEnabled
bool legendFilterByMapEnabled() const
Find out whether legend items are filtered to show just the ones visible in the associated map.
Definition: qgslayoutitemlegend.h:185
SIP_SKIP
#define SIP_SKIP
Definition: qgis_sip.h:126
QgsLayerTree
Definition: qgslayertree.h:32
QgsLayoutItem::exportLayerBehavior
virtual ExportLayerBehavior exportLayerBehavior() const
Returns the behavior of this item during exporting to layered exports (e.g.
Definition: qgslayoutitem.cpp:247
QgsLegendStyle::Side
Side
Margin sides.
Definition: qgslegendstyle.h:67
qgslayoutitem.h
QgsCompositionConverter
QgsCompositionConverter class converts a QGIS 2.x composition to a QGIS 3.x layout.
Definition: qgscompositionconverter.h:54
QgsLayoutItem::accept
virtual bool accept(QgsStyleEntityVisitorInterface *visitor) const
Accepts the specified style entity visitor, causing it to visit all style entities associated with th...
Definition: qgslayoutitem.cpp:1166
qgslayertreegroup.h
qgslegendsettings.h
QgsLayerTreeLayer
Definition: qgslayertreelayer.h:43
qgis_sip.h
QgsLegendSettings
The QgsLegendSettings class stores the appearance and layout settings for legend drawing with QgsLege...
Definition: qgslegendsettings.h:38
QgsLayoutItem::paint
void paint(QPainter *painter, const QStyleOptionGraphicsItem *itemStyle, QWidget *pWidget) override
Handles preparing a paint surface for the layout item and painting the item's content.
Definition: qgslayoutitem.cpp:285
QgsLayoutItemLegend::linkedMap
QgsLayoutItemMap * linkedMap() const
Returns the associated map.
Definition: qgslayoutitemlegend.h:490
QgsLayoutItem
Base class for graphical items within a QgsLayout.
Definition: qgslayoutitem.h:112
QgsLayoutItem::refresh
void refresh() override
Refreshes the item, causing a recalculation of any property overrides and recalculation of its positi...
Definition: qgslayoutitem.cpp:1172
QgsLayoutItemMap
Layout graphical items for displaying a map.
Definition: qgslayoutitemmap.h:39
QgsLayoutItem::itemFlags
virtual Flags itemFlags() const
Returns the item's flags, which indicate how the item behaves.
Definition: qgslayoutitem.cpp:129
QgsLayout
Base class for layouts, which can contain items such as maps, labels, scalebars, etc.
Definition: qgslayout.h:49
QgsLayoutItem::displayName
virtual QString displayName() const
Gets item display name.
Definition: qgslayoutitem.cpp:107
QgsLayerTreeModel::flags
Flags flags() const
Returns OR-ed combination of model flags.
Definition: qgslayertreemodel.cpp:1170
qgslayertreemodel.h
QgsLegendRenderer
The QgsLegendRenderer class handles automatic layout and rendering of legend. The content is given by...
Definition: qgslegendrenderer.h:46
QgsLayerTreeModel::layerLegendNodes
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...
Definition: qgslayertreemodel.cpp:1533
QgsLayerTreeModel::data
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const override
Definition: qgslayertreemodel.cpp:158
QgsLegendModel
Definition: qgslayoutitemlegend.h:43
SIP_TRANSFERTHIS
#define SIP_TRANSFERTHIS
Definition: qgis_sip.h:53
QgsLayoutItem::createExpressionContext
QgsExpressionContext createExpressionContext() const override
This method needs to be reimplemented in all classes which implement this interface and return an exp...
Definition: qgslayoutitem.cpp:1159
QgsLayoutObject::DataDefinedProperty
DataDefinedProperty
Data defined properties for different item types.
Definition: qgslayoutobject.h:132
QgsLegendStyle
Definition: qgslegendstyle.h:35