QGIS API Documentation  3.4.15-Madeira (e83d02e274)
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.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 
439 
440  public slots:
441 
442  void refresh() override;
444 
445  protected:
446  void draw( QgsLayoutItemRenderContext &context ) override;
447  bool writePropertiesToElement( QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context ) const override;
448  bool readPropertiesFromElement( const QDomElement &element, const QDomDocument &document, const QgsReadWriteContext &context ) override;
449 
450  private slots:
451 
453  void invalidateCurrentMap();
454 
455  void updateFilterByMapAndRedraw();
456 
457 
459  void mapLayerStyleOverridesChanged();
460 
462  void onAtlasEnded();
463  void onAtlasFeature();
464 
465  void nodeCustomPropertyChanged( QgsLayerTreeNode *node, const QString &key );
466 
467  private:
468  QgsLayoutItemLegend() = delete;
469 
471  void setCustomLayerTree( QgsLayerTree *rootGroup );
472 
473  void setupMapConnections( QgsLayoutItemMap *map, bool connect = true );
474 
475  std::unique_ptr< QgsLegendModel > mLegendModel;
476  std::unique_ptr< QgsLayerTreeGroup > mCustomLayerTree;
477 
478  QgsLegendSettings mSettings;
479 
480  QString mTitle;
481  int mColumnCount = 1;
482 
483  QString mMapUuid;
484  QgsLayoutItemMap *mMap = nullptr;
485 
486  bool mLegendFilterByMap = false;
487  bool mLegendFilterByExpression = false;
488 
490  bool mFilterOutAtlas = false;
491 
493  bool mFilterAskedForUpdate = false;
495  void doUpdateFilterByMap();
496 
497  bool mInAtlas = false;
498 
500  bool mInitialMapScaleCalculated = false;
501 
503  bool mForceResize = false;
504 
506  bool mSizeToContents = true;
507 
509 
510 };
511 
512 #endif // QGSLAYOUTITEMLEGEND_H
513 
The class is used as a container of context for various read/write operations on other objects...
Item model implementation based on layer tree model for layout legend.
virtual QIcon icon() const
Returns the item&#39;s icon.
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
virtual bool writePropertiesToElement(QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context) const
Stores item state within an XML DOM element.
Flags flags() const
Returns OR-ed combination of model flags.
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
QgsLayoutItemMap * linkedMap() const
Returns the associated map.
virtual bool readPropertiesFromElement(const QDomElement &element, const QDomDocument &document, const QgsReadWriteContext &context)
Sets item state from a DOM element.
virtual Flags itemFlags() const
Returns the item&#39;s flags, which indicate how the item behaves.
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.
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
Contains settings and helpers relating to a render of a QgsLayoutItem.
Definition: qgslayoutitem.h:43
virtual void finalizeRestoreFromXml()
Called after all pending items have been restored from XML.
bool legendFilterByMapEnabled() const
Find out whether legend items are filtered to show just the ones visible in the associated map...
QgsCompositionConverter class converts a QGIS 2.x composition to a QGIS 3.x layout.
const QgsLegendSettings & legendSettings() const
Returns the legend&#39;s renderer settings object.
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const override
virtual QString displayName() const
Gets item display name.
virtual void draw(QgsLayoutItemRenderContext &context)=0
Draws the item&#39;s contents using the specified item render context.
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.