40#include <QDomDocument>
47 , mLegendModel( new
QgsLegendModel( layout->project()->layerTreeRoot(), this ) )
50 connect( &
layout->atlasComposition(), &QgsAtlasComposition::renderEnded,
this, &QgsLayoutItemLegend::onAtlasEnded );
53 mTitle = mSettings.
title();
71 QgsLayoutItem::refresh();
74 clearLegendCachedData();
103 if ( mFilterAskedForUpdate )
105 mFilterAskedForUpdate =
false;
106 doUpdateFilterByMap();
109 const int dpi = painter->device()->logicalDpiX();
110 const double dotsPerMM = dpi / 25.4;
128 const QSizeF mapSizePixels = QSizeF( mMap->rect().width() * dotsPerMM, mMap->rect().height() * dotsPerMM );
138 mInitialMapScaleCalculated =
true;
141 legendRenderer.
setLegendSize( mForceResize && mSizeToContents ? QSize() : rect().size() );
143 const QPointF oldPos = pos();
146 if ( mSizeToContents )
151 const QSizeF size = legendRenderer.
minimumSize( &context );
154 mForceResize =
false;
160 else if ( size.height() > rect().height() || size.width() > rect().width() )
163 QSizeF targetSize = rect().size();
164 if ( size.height() > targetSize.height() )
165 targetSize.setHeight( size.height() );
166 if ( size.width() > targetSize.width() )
167 targetSize.setWidth( size.width() );
181 painter->translate( pos() - oldPos );
188 if ( !mMapUuid.isEmpty() )
190 setLinkedMap( qobject_cast< QgsLayoutItemMap * >(
mLayout->itemByUuid( mMapUuid,
true ) ) );
197 clearLegendCachedData();
215 painter->setPen( QPen( QColor( 0, 0, 0 ) ) );
217 if ( !mSizeToContents )
220 const QRectF thisPaintRect = QRectF( 0, 0, rect().width(), rect().height() );
221 painter->setClipRect( thisPaintRect );
243 if ( !mSizeToContents )
246 if ( !mInitialMapScaleCalculated )
259 const QSizeF size = legendRenderer.
minimumSize( &context );
260 QgsDebugMsgLevel( QStringLiteral(
"width = %1 height = %2" ).arg( size.width() ).arg( size.height() ), 2 );
261 if ( size.isValid() )
271 mSizeToContents = enabled;
276 return mSizeToContents;
279void QgsLayoutItemLegend::setCustomLayerTree(
QgsLayerTree *rootGroup )
281 mLegendModel->setRootGroup( rootGroup ? rootGroup : (
mLayout ?
mLayout->project()->layerTreeRoot() :
nullptr ) );
283 mCustomLayerTree.reset( rootGroup );
292 setCustomLayerTree( autoUpdate ?
nullptr :
mLayout->project()->layerTreeRoot()->clone() );
297void QgsLayoutItemLegend::nodeCustomPropertyChanged(
QgsLayerTreeNode *,
const QString &key )
299 if ( key == QLatin1String(
"cached_name" ) )
312 return !mCustomLayerTree;
317 if ( mLegendFilterByMap == enabled )
320 mLegendFilterByMap = enabled;
329 if (
mLayout &&
id().isEmpty() )
332 mLayout->itemsModel()->updateItemDisplayName(
this );
352 return mSettings.
rstyle( s );
357 return mSettings.
style( s );
587 legendElem.setAttribute( QStringLiteral(
"title" ), mTitle );
588 legendElem.setAttribute( QStringLiteral(
"titleAlignment" ), QString::number(
static_cast< int >( mSettings.
titleAlignment() ) ) );
589 legendElem.setAttribute( QStringLiteral(
"columnCount" ), QString::number( mColumnCount ) );
590 legendElem.setAttribute( QStringLiteral(
"splitLayer" ), QString::number( mSettings.
splitLayer() ) );
591 legendElem.setAttribute( QStringLiteral(
"equalColumnWidth" ), QString::number( mSettings.
equalColumnWidth() ) );
593 legendElem.setAttribute( QStringLiteral(
"boxSpace" ), QString::number( mSettings.
boxSpace() ) );
594 legendElem.setAttribute( QStringLiteral(
"columnSpace" ), QString::number( mSettings.
columnSpace() ) );
596 legendElem.setAttribute( QStringLiteral(
"symbolWidth" ), QString::number( mSettings.
symbolSize().width() ) );
597 legendElem.setAttribute( QStringLiteral(
"symbolHeight" ), QString::number( mSettings.
symbolSize().height() ) );
598 legendElem.setAttribute( QStringLiteral(
"maxSymbolSize" ), QString::number( mSettings.
maximumSymbolSize() ) );
599 legendElem.setAttribute( QStringLiteral(
"minSymbolSize" ), QString::number( mSettings.
minimumSymbolSize() ) );
601 legendElem.setAttribute( QStringLiteral(
"symbolAlignment" ), mSettings.
symbolAlignment() );
603 legendElem.setAttribute( QStringLiteral(
"symbolAlignment" ), mSettings.
symbolAlignment() );
605 legendElem.setAttribute( QStringLiteral(
"rasterBorder" ), mSettings.
drawRasterStroke() );
607 legendElem.setAttribute( QStringLiteral(
"rasterBorderWidth" ), QString::number( mSettings.
rasterStrokeWidth() ) );
609 legendElem.setAttribute( QStringLiteral(
"wmsLegendWidth" ), QString::number( mSettings.
wmsLegendSize().width() ) );
610 legendElem.setAttribute( QStringLiteral(
"wmsLegendHeight" ), QString::number( mSettings.
wmsLegendSize().height() ) );
611 legendElem.setAttribute( QStringLiteral(
"wrapChar" ), mSettings.
wrapChar() );
613 legendElem.setAttribute( QStringLiteral(
"resizeToContents" ), mSizeToContents );
617 legendElem.setAttribute( QStringLiteral(
"map_uuid" ), mMap->
uuid() );
620 QDomElement legendStyles = doc.createElement( QStringLiteral(
"styles" ) );
621 legendElem.appendChild( legendStyles );
629 if ( mCustomLayerTree )
632 mCustomLayerTree->writeXml( legendElem, context );
635 if ( mLegendFilterByMap )
637 legendElem.setAttribute( QStringLiteral(
"legendFilterByMap" ), QStringLiteral(
"1" ) );
639 legendElem.setAttribute( QStringLiteral(
"legendFilterByAtlas" ), mFilterOutAtlas ? QStringLiteral(
"1" ) : QStringLiteral(
"0" ) );
647 mTitle = itemElem.attribute( QStringLiteral(
"title" ) );
649 if ( !itemElem.attribute( QStringLiteral(
"titleAlignment" ) ).isEmpty() )
651 mSettings.
setTitleAlignment(
static_cast< Qt::AlignmentFlag
>( itemElem.attribute( QStringLiteral(
"titleAlignment" ) ).toInt() ) );
653 int colCount = itemElem.attribute( QStringLiteral(
"columnCount" ), QStringLiteral(
"1" ) ).toInt();
654 if ( colCount < 1 ) colCount = 1;
655 mColumnCount = colCount;
657 mSettings.
setSplitLayer( itemElem.attribute( QStringLiteral(
"splitLayer" ), QStringLiteral(
"0" ) ).toInt() == 1 );
658 mSettings.
setEqualColumnWidth( itemElem.attribute( QStringLiteral(
"equalColumnWidth" ), QStringLiteral(
"0" ) ).toInt() == 1 );
660 const QDomNodeList stylesNodeList = itemElem.elementsByTagName( QStringLiteral(
"styles" ) );
661 if ( !stylesNodeList.isEmpty() )
663 const QDomNode stylesNode = stylesNodeList.at( 0 );
664 for (
int i = 0; i < stylesNode.childNodes().size(); i++ )
666 const QDomElement styleElem = stylesNode.childNodes().at( i ).toElement();
669 const QString name = styleElem.attribute( QStringLiteral(
"name" ) );
682 if ( itemElem.hasAttribute( QStringLiteral(
"fontColor" ) ) )
685 fontClr.setNamedColor( itemElem.attribute( QStringLiteral(
"fontColor" ), QStringLiteral(
"#000000" ) ) );
693 mSettings.
setBoxSpace( itemElem.attribute( QStringLiteral(
"boxSpace" ), QStringLiteral(
"2.0" ) ).toDouble() );
694 mSettings.
setColumnSpace( itemElem.attribute( QStringLiteral(
"columnSpace" ), QStringLiteral(
"2.0" ) ).toDouble() );
696 mSettings.
setSymbolSize( QSizeF( itemElem.attribute( QStringLiteral(
"symbolWidth" ), QStringLiteral(
"7.0" ) ).toDouble(), itemElem.attribute( QStringLiteral(
"symbolHeight" ), QStringLiteral(
"14.0" ) ).toDouble() ) );
697 mSettings.
setSymbolAlignment(
static_cast< Qt::AlignmentFlag
>( itemElem.attribute( QStringLiteral(
"symbolAlignment" ), QString::number( Qt::AlignLeft ) ).toInt() ) );
699 mSettings.
setMaximumSymbolSize( itemElem.attribute( QStringLiteral(
"maxSymbolSize" ), QStringLiteral(
"0.0" ) ).toDouble() );
700 mSettings.
setMinimumSymbolSize( itemElem.attribute( QStringLiteral(
"minSymbolSize" ), QStringLiteral(
"0.0" ) ).toDouble() );
702 mSettings.
setWmsLegendSize( QSizeF( itemElem.attribute( QStringLiteral(
"wmsLegendWidth" ), QStringLiteral(
"50" ) ).toDouble(), itemElem.attribute( QStringLiteral(
"wmsLegendHeight" ), QStringLiteral(
"25" ) ).toDouble() ) );
704 if ( itemElem.hasAttribute( QStringLiteral(
"lineSpacing" ) ) )
706 const double spacing = itemElem.attribute( QStringLiteral(
"lineSpacing" ), QStringLiteral(
"1.0" ) ).toDouble();
730 mSettings.
setDrawRasterStroke( itemElem.attribute( QStringLiteral(
"rasterBorder" ), QStringLiteral(
"1" ) ) != QLatin1String(
"0" ) );
732 mSettings.
setRasterStrokeWidth( itemElem.attribute( QStringLiteral(
"rasterBorderWidth" ), QStringLiteral(
"0" ) ).toDouble() );
734 mSettings.
setWrapChar( itemElem.attribute( QStringLiteral(
"wrapChar" ) ) );
736 mSizeToContents = itemElem.attribute( QStringLiteral(
"resizeToContents" ), QStringLiteral(
"1" ) ) != QLatin1String(
"0" );
739 mLegendFilterByMap = itemElem.attribute( QStringLiteral(
"legendFilterByMap" ), QStringLiteral(
"0" ) ).toInt();
742 if ( !itemElem.attribute( QStringLiteral(
"map_uuid" ) ).isEmpty() )
744 mMapUuid = itemElem.attribute( QStringLiteral(
"map_uuid" ) );
747 setupMapConnections( mMap,
false );
750 mFilterOutAtlas = itemElem.attribute( QStringLiteral(
"legendFilterByAtlas" ), QStringLiteral(
"0" ) ).toInt();
753 QDomElement layerTreeElem = itemElem.firstChildElement( QStringLiteral(
"layer-tree" ) );
754 if ( layerTreeElem.isNull() )
755 layerTreeElem = itemElem.firstChildElement( QStringLiteral(
"layer-tree-group" ) );
757 if ( !layerTreeElem.isNull() )
761 tree->resolveReferences(
mLayout->project(),
true );
762 setCustomLayerTree( tree.release() );
765 setCustomLayerTree(
nullptr );
772 if ( !
id().isEmpty() )
778 QString text = mSettings.
title();
779 if ( text.isEmpty() )
781 return tr(
"<Legend>" );
783 if ( text.length() > 25 )
785 return tr(
"%1…" ).arg( text.left( 25 ) );
794void QgsLayoutItemLegend::setupMapConnections(
QgsLayoutItemMap *map,
bool connectSlots )
801 disconnect( map, &QObject::destroyed,
this, &QgsLayoutItemLegend::invalidateCurrentMap );
810 connect( map, &QObject::destroyed,
this, &QgsLayoutItemLegend::invalidateCurrentMap );
826 setupMapConnections( mMap,
false );
833 setupMapConnections( mMap,
true );
840void QgsLayoutItemLegend::invalidateCurrentMap()
849 bool forceUpdate =
false;
865 if ( ok && cols >= 0 )
881void QgsLayoutItemLegend::updateFilterByMapAndRedraw()
886void QgsLayoutItemLegend::setModelStyleOverrides(
const QMap<QString, QString> &overrides )
888 mLegendModel->setLayerStyleOverrides( overrides );
889 const QList< QgsLayerTreeLayer * > layers = mLegendModel->rootGroup()->findLayers();
891 mLegendModel->refreshLayerLegend( nodeLayer );
895void QgsLayoutItemLegend::clearLegendCachedData()
900 mLegendModel->clearCachedData( node );
904 const QList< QgsLayerTreeNode * > children = group->
children();
907 clearNodeCache( child );
912 clearNodeCache( mLegendModel->rootGroup() );
915void QgsLayoutItemLegend::mapLayerStyleOverridesChanged()
921 if ( mLegendFilterByMap )
937void QgsLayoutItemLegend::mapThemeChanged(
const QString &theme )
939 if ( mThemeName == theme )
945 if ( mLegendFilterByMap )
953 if ( mThemeName.isEmpty() )
955 setModelStyleOverrides( QMap<QString, QString>() );
960 const QMap<QString, QString> overrides =
mLayout->project()->mapThemeCollection()->mapThemeStyleOverrides( mThemeName );
961 setModelStyleOverrides( overrides );
975 mFilterAskedForUpdate =
true;
981void QgsLayoutItemLegend::doUpdateFilterByMap()
985 if ( !mThemeName.isEmpty() )
988 const QMap<QString, QString> overrides =
mLayout->project()->mapThemeCollection()->mapThemeStyleOverrides( mThemeName );
989 mLegendModel->setLayerStyleOverrides( overrides );
997 mLegendModel->setLayerStyleOverrides( QMap<QString, QString>() );
1002 if ( mMap && ( mLegendFilterByMap || filterByExpression || mInAtlas ) )
1004 const double dpi =
mLayout->renderContext().dpi();
1008 QSizeF size( requestRectangle.
width(), requestRectangle.
height() );
1016 filterPolygon =
mLayout->reportContext().currentGeometry( mMap->
crs() );
1018 mLegendModel->setLegendFilter( &ms, mInAtlas || mLegendFilterByMap, filterPolygon,
true );
1021 mLegendModel->setLegendFilterByMap(
nullptr );
1023 clearLegendCachedData();
1024 mForceResize =
true;
1034 mFilterOutAtlas = doFilter;
1039 return mFilterOutAtlas;
1042void QgsLayoutItemLegend::onAtlasFeature()
1046 mInAtlas = mFilterOutAtlas;
1050void QgsLayoutItemLegend::onAtlasEnded()
1090 const QList<QgsLayerTreeNode *> childNodes = group->
children();
1096 if ( !visit( nodeGroup ) )
1108 const QList<QgsLayerTreeModelLegendNode *> legendNodes = mLegendModel->layerLegendNodes( nodeLayer );
1113 if ( !symbolNode->patchShape().isNull() )
1125 return visit( mLegendModel->rootGroup( ) );
1133 , mLayoutLegend( layout )
1142 , mLayoutLegend( layout )
1155 if ( nodeLayer && ( role == Qt::DisplayRole || role == Qt::EditRole ) )
1157 QString name = node->
customProperty( QStringLiteral(
"cached_name" ) ).toString();
1158 if ( !name.isEmpty() )
1164 name = nodeLayer->
customProperty( QStringLiteral(
"legend/title-label" ) ).toString();
1165 if ( name.isEmpty() )
1166 name = nodeLayer->
name();
1167 if ( name.isEmpty() )
1168 name = node->
customProperty( QStringLiteral(
"legend/title-label" ) ).toString();
1169 if ( name.isEmpty() )
1170 name = node->
name();
1171 if ( nodeLayer->
customProperty( QStringLiteral(
"showFeatureCount" ), 0 ).toInt() )
1175 name += QStringLiteral(
" [%1]" ).arg( vlayer->
featureCount() );
1181 const bool evaluate = ( vlayer && !nodeLayer->
labelExpression().isEmpty() ) || name.contains(
"[%" );
1192 if ( mLayoutLegend )
1195 const QList<QgsLayerTreeModelLegendNode *> legendnodes =
layerLegendNodes( nodeLayer,
false );
1196 if ( legendnodes.count() > 1 )
1201 symnode->evaluateLabel( expressionContext );
1204 else if (
QgsSymbolLegendNode *symnode = qobject_cast<QgsSymbolLegendNode *>( legendnodes.first() ) )
1205 name = symnode->evaluateLabel( expressionContext );
1224 if ( !
mLegend.contains( nodeLayer ) )
1225 return QList<QgsLayerTreeModelLegendNode *>();
1228 QList<QgsLayerTreeModelLegendNode *> lst(
data.activeNodes );
1229 if ( !skipNodeEmbeddedInParent &&
data.embeddedNodeInParent )
1230 lst.prepend(
data.embeddedNodeInParent );
1239void QgsLegendModel::forceRefresh()
int valueAsInt(int key, const QgsExpressionContext &context, int defaultValue=0, bool *ok=nullptr) const
Calculates the current value of the property with the specified key and interprets it as an integer.
QString valueAsString(int key, const QgsExpressionContext &context, const QString &defaultString=QString(), bool *ok=nullptr) const
Calculates the current value of the property with the specified key and interprets it as a string.
static QIcon getThemeIcon(const QString &name, const QColor &fillColor=QColor(), const QColor &strokeColor=QColor())
Helper to get a theme icon.
Single scope for storing variables and functions for use within a QgsExpressionContext.
void addVariable(const QgsExpressionContextScope::StaticVariable &variable)
Adds a variable into the context scope.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
QgsExpressionContextScope * popScope()
Removes the last scope from the expression context and return it.
void appendScope(QgsExpressionContextScope *scope)
Appends a scope to the end of the context.
void appendScopes(const QList< QgsExpressionContextScope * > &scopes)
Appends a list of scopes to the end of the context.
A geometry is the spatial representation of a feature.
Layer tree group node serves as a container for layers and further groups.
Layer tree node points to a map layer.
QString labelExpression() const
Returns the expression member of the LayerTreeNode.
QgsLegendPatchShape patchShape() const
Returns the symbol patch shape to use when rendering the legend node symbol.
QString name() const override
Returns the layer's name.
QgsMapLayer * layer() const
Returns the map layer associated with this node.
The QgsLegendRendererItem class is abstract interface for legend items returned from QgsMapLayerLegen...
The QgsLayerTreeModel class is model implementation for Qt item views framework.
Flags flags() const
Returns OR-ed combination of model flags.
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const override
void setFlag(Flag f, bool on=true)
Enable or disable a model flag.
QHash< QgsLayerTreeLayer *, LayerLegendData > mLegend
Per layer data about layer's legend nodes.
QgsLayerTreeNode * index2node(const QModelIndex &index) const
Returns layer tree node for given index.
QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const override
static QgsLayerTreeModelLegendNode * index2legendNode(const QModelIndex &index)
Returns legend node for given index.
@ AllowNodeReorder
Allow reordering with drag'n'drop.
@ AllowLegendChangeState
Allow check boxes for legend nodes (if supported by layer's legend)
This class is a base class for nodes in a layer tree.
void setCustomProperty(const QString &key, const QVariant &value)
Sets a custom property for the node. Properties are stored in a map and saved in project file.
QList< QgsLayerTreeNode * > children()
Gets list of children of the node. Children are owned by the parent.
void removeCustomProperty(const QString &key)
Remove a custom property from layer. Properties are stored in a map and saved in project file.
QVariant customProperty(const QString &key, const QVariant &defaultValue=QVariant()) const
Read a custom property from layer. Properties are stored in a map and saved in project file.
virtual QString name() const =0
Returns name of the node.
void customPropertyChanged(QgsLayerTreeNode *node, const QString &key)
Emitted when a custom property of a node within the tree has been changed or removed.
static bool hasLegendFilterExpression(const QgsLayerTreeGroup &group)
Test if one of the layers in a group has an expression filter.
Namespace with helper functions for layer tree operations.
static QgsLayerTreeLayer * toLayer(QgsLayerTreeNode *node)
Cast node to a layer.
static bool isLayer(const QgsLayerTreeNode *node)
Check whether the node is a valid layer node.
static bool isGroup(QgsLayerTreeNode *node)
Check whether the node is a valid group node.
static QgsLayerTree * readXml(QDomElement &element, const QgsReadWriteContext &context)
Load the layer tree from an XML element.
static QgsLayerTreeGroup * toGroup(QgsLayerTreeNode *node)
Cast node to a group.
A layout item subclass for map legends.
bool autoUpdateModel() const
Returns whether the legend content should auto update to reflect changes in the project's layer tree.
QgsExpressionContext createExpressionContext() const override
This method needs to be reimplemented in all classes which implement this interface and return an exp...
void setStyleMargin(QgsLegendStyle::Style component, double margin)
Set the margin for a legend component.
bool accept(QgsStyleEntityVisitorInterface *visitor) const override
Accepts the specified style entity visitor, causing it to visit all style entities associated with th...
QString title() const
Returns the legend title.
void setSplitLayer(bool enabled)
Sets whether the legend items from a single layer can be split over multiple columns.
void refreshDataDefinedProperty(QgsLayoutObject::DataDefinedProperty property=QgsLayoutObject::AllProperties) override
void adjustBoxSize()
Sets the legend's item bounds to fit the whole legend content.
double wmsLegendWidth() const
Returns the WMS legend width.
void setColumnSpace(double spacing)
Sets the legend column spacing.
void setBoxSpace(double space)
Sets the legend box space.
bool splitLayer() const
Returns whether the legend items from a single layer can be split over multiple columns.
double symbolHeight() const
Returns the legend symbol height.
void updateFilterByMap(bool redraw=true)
Updates the legend content when filtered by map.
void setEqualColumnWidth(bool equalize)
Sets whether column widths should be equalized.
void setDrawRasterStroke(bool enabled)
Sets whether a stroke will be drawn around raster symbol items.
Q_DECL_DEPRECATED QFont styleFont(QgsLegendStyle::Style component) const
Returns the font settings for a legend component.
static QgsLayoutItemLegend * create(QgsLayout *layout)
Returns a new legend item for the specified layout.
void setLegendFilterOutAtlas(bool doFilter)
When set to true, during an atlas rendering, it will filter out legend elements where features are ou...
void setSymbolWidth(double width)
Sets the legend symbol width.
QString wrapString() const
Returns the legend text wrapping string.
bool resizeToContents() const
Returns whether the legend should automatically resize to fit its contents.
void setResizeToContents(bool enabled)
Sets whether the legend should automatically resize to fit its contents.
void updateLegend()
Updates the model and all legend entries.
QgsLayoutItemLegend(QgsLayout *layout)
Constructor for QgsLayoutItemLegend, with the specified parent layout.
void setLinkedMap(QgsLayoutItemMap *map)
Sets the map to associate with the legend.
bool writePropertiesToElement(QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context) const override
Stores item state within an XML DOM element.
void setSymbolAlignment(Qt::AlignmentFlag alignment)
Sets the alignment for placement of legend symbols.
QgsLegendStyle & rstyle(QgsLegendStyle::Style s)
Returns reference to modifiable legend style.
Q_DECL_DEPRECATED QColor fontColor() const
Returns the legend font color.
Qt::AlignmentFlag symbolAlignment() const
Returns the alignment for placement of legend symbols.
double maximumSymbolSize() const
Returns the maximum symbol size (in mm).
void setWmsLegendWidth(double width)
Sets the WMS legend width.
void setTitle(const QString &title)
Sets the legend title.
Q_DECL_DEPRECATED void setFontColor(const QColor &color)
Sets the legend font color.
double boxSpace() const
Returns the legend box space.
void setRasterStrokeColor(const QColor &color)
Sets the stroke color for the stroke drawn around raster symbol items.
QString displayName() const override
Gets item display name.
void paint(QPainter *painter, const QStyleOptionGraphicsItem *itemStyle, QWidget *pWidget) override
double symbolWidth() const
Returns the legend symbol width.
QColor rasterStrokeColor() const
Returns the stroke color for the stroke drawn around raster symbol items.
bool drawRasterStroke() const
Returns whether a stroke will be drawn around raster symbol items.
void draw(QgsLayoutItemRenderContext &context) override
Draws the item's contents using the specified item render context.
void finalizeRestoreFromXml() override
Called after all pending items have been restored from XML.
int type() const override
bool readPropertiesFromElement(const QDomElement &element, const QDomDocument &document, const QgsReadWriteContext &context) override
Sets item state from a DOM element.
bool legendFilterOutAtlas() const
Returns whether to filter out legend elements outside of the current atlas feature.
void setStyle(QgsLegendStyle::Style component, const QgsLegendStyle &style)
Sets the style of component to style for the legend.
ExportLayerBehavior exportLayerBehavior() const override
Returns the behavior of this item during exporting to layered exports (e.g.
Q_DECL_DEPRECATED void setLineSpacing(double spacing)
Sets the spacing in-between multiple lines.
double columnSpace() const
Returns the legend column spacing.
QgsLayoutItem::Flags itemFlags() const override
Returns the item's flags, which indicate how the item behaves.
Qt::AlignmentFlag titleAlignment() const
Returns the alignment of the legend title.
void setLegendFilterByMapEnabled(bool enabled)
Set whether legend items should be filtered to show just the ones visible in the associated map.
void setSymbolHeight(double height)
Sets the legend symbol height.
void setMinimumSymbolSize(double size)
Set the minimum symbol size for symbol (in millimeters).
void setAutoUpdateModel(bool autoUpdate)
Sets whether the legend content should auto update to reflect changes in the project's layer tree.
void setMaximumSymbolSize(double size)
Set the maximum symbol size for symbol (in millimeters).
QIcon icon() const override
Returns the item's icon.
double wmsLegendHeight() const
Returns the WMS legend height.
void setWmsLegendHeight(double height)
Sets the WMS legend height.
void setRasterStrokeWidth(double width)
Sets the stroke width for the stroke drawn around raster symbol items.
double minimumSymbolSize() const
Returns the minimum symbol size (in mm).
double rasterStrokeWidth() const
Returns the stroke width (in layout units) for the stroke drawn around raster symbol items.
Q_DECL_DEPRECATED double lineSpacing() const
Returns the spacing in-between lines in layout units.
Q_DECL_DEPRECATED void setStyleFont(QgsLegendStyle::Style component, const QFont &font)
Sets the style font for a legend component.
QgsLegendStyle style(QgsLegendStyle::Style s) const
Returns legend style.
void setTitleAlignment(Qt::AlignmentFlag alignment)
Sets the alignment of the legend title.
void setWrapString(const QString &string)
Sets the legend text wrapping string.
QString themeName() const
Returns the name of the theme currently linked to the legend.
void setColumnCount(int count)
Sets the legend column count.
bool equalColumnWidth() const
Returns whether column widths should be equalized.
int columnCount() const
Returns the legend column count.
bool legendFilterByMapEnabled() const
Find out whether legend items are filtered to show just the ones visible in the associated map.
Layout graphical items for displaying a map.
void extentChanged()
Emitted when the map's extent changes.
QgsMapSettings mapSettings(const QgsRectangle &extent, QSizeF size, double dpi, bool includeLayerSettings) const
Returns map settings that will be used for drawing of the map.
void layerStyleOverridesChanged()
Emitted when layer style overrides are changed... a means to let associated legend items know they sh...
void mapRotationChanged(double newRotation)
Emitted when the map's rotation changes.
QgsRectangle requestedExtent() const
Calculates the extent to request and the yShift of the top-left point in case of rotation.
QMap< QString, QString > layerStyleOverrides() const
Returns stored overrides of styles for layers.
QgsExpressionContext createExpressionContext() const override
This method needs to be reimplemented in all classes which implement this interface and return an exp...
double mapUnitsToLayoutUnits() const
Returns the conversion factor from map units to layout units.
void themeChanged(const QString &theme)
Emitted when the map's associated theme is changed.
QgsRectangle extent() const
Returns the current map extent.
QgsCoordinateReferenceSystem crs() const
Returns coordinate reference system used for rendering the map.
@ LayoutLegend
Legend item.
Contains settings and helpers relating to a render of a QgsLayoutItem.
QgsRenderContext & renderContext()
Returns a reference to the context's render context.
Base class for graphical items within a QgsLayout.
QgsLayoutSize sizeWithUnits() const
Returns the item's current size, including units.
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.
virtual void redraw()
Triggers a redraw (update) of the item.
QgsExpressionContext createExpressionContext() const override
This method needs to be reimplemented in all classes which implement this interface and return an exp...
virtual void attemptResize(const QgsLayoutSize &size, bool includesFrame=false)
Attempts to resize the item to a specified target size.
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...
@ FlagOverridesPaint
Item overrides the default layout item painting method.
virtual QString uuid() const
Returns the item identification string.
QString id() const
Returns the item's ID name.
ExportLayerBehavior
Behavior of item when exporting to layered outputs.
@ MustPlaceInOwnLayer
Item must be placed in its own individual layer.
void refresh() override
Refreshes the item, causing a recalculation of any property overrides and recalculation of its positi...
QgsPropertyCollection mDataDefinedProperties
const QgsLayout * layout() const
Returns the layout the object is attached to.
void changed()
Emitted when the object's properties change.
QPointer< QgsLayout > mLayout
DataDefinedProperty
Data defined properties for different item types.
@ LegendTitle
Legend title.
@ AllProperties
All properties for item.
@ LegendColumnCount
Legend column count.
@ FlagUseAdvancedEffects
Enable advanced effects such as blend modes.
This class provides a method of storing sizes, consisting of a width and height, for use in QGIS layo...
static QgsRenderContext createRenderContextForLayout(QgsLayout *layout, QPainter *painter, double dpi=-1)
Creates a render context suitable for the specified layout and painter destination.
static QgsRenderContext createRenderContextForMap(QgsLayoutItemMap *map, QPainter *painter, double dpi=-1)
Creates a render context suitable for the specified layout map and painter destination.
Base class for layouts, which can contain items such as maps, labels, scalebars, etc.
Item model implementation based on layer tree model for layout legend.
void clearCachedData(QgsLayerTreeNode *node) const
Clears any previously cached data for the specified node.
void refreshLegend()
Emitted to refresh the legend.
QgsLegendModel(QgsLayerTree *rootNode, QObject *parent=nullptr, QgsLayoutItemLegend *layout=nullptr)
Construct the model based on the given layer tree.
QVariant data(const QModelIndex &index, int role) const override
QList< QgsLayerTreeModelLegendNode * > layerLegendNodes(QgsLayerTreeLayer *nodeLayer, bool skipNodeEmbeddedInParent=false) const
Returns filtered list of active legend nodes attached to a particular layer node (by default it retur...
bool isNull() const
Returns true if the patch shape is a null QgsLegendPatchShape, which indicates that the default legen...
The QgsLegendRenderer class handles automatic layout and rendering of legend.
QSizeF minimumSize(QgsRenderContext *renderContext=nullptr)
Runs the layout algorithm and returns the minimum size required for the legend.
void setLegendSize(QSizeF s)
Sets the preferred resulting legend size.
Q_DECL_DEPRECATED void drawLegend(QPainter *painter)
Draws the legend with given painter.
void setSymbolAlignment(Qt::AlignmentFlag alignment)
Sets the alignment for placement of legend symbols.
QString wrapChar() const
Returns the string used as a wrapping character.
Q_DECL_DEPRECATED void setFontColor(const QColor &c)
Sets the font color used for legend items.
void setWrapChar(const QString &t)
Sets a string to use as a wrapping character.
void setRasterStrokeColor(const QColor &color)
Sets the stroke color for the stroke drawn around raster symbol items.
void setStyle(QgsLegendStyle::Style s, const QgsLegendStyle &style)
Sets the style for a legend component.
void setColumnSpace(double s)
Sets the margin space between adjacent columns (in millimeters).
Q_DECL_DEPRECATED void setMapScale(double scale)
Sets the legend map scale.
QgsLegendStyle style(QgsLegendStyle::Style s) const
Returns the style for a legend component.
void setTitle(const QString &t)
Sets the title for the legend, which will be rendered above all legend items.
bool drawRasterStroke() const
Returns whether a stroke will be drawn around raster symbol items.
void setDrawRasterStroke(bool enabled)
Sets whether a stroke will be drawn around raster symbol items.
QSizeF wmsLegendSize() const
Returns the size (in millimeters) of WMS legend graphics shown in the legend.
double minimumSymbolSize() const
Returns the minimum symbol size (in mm).
double rasterStrokeWidth() const
Returns the stroke width (in millimeters) for the stroke drawn around raster symbol items.
Q_DECL_DEPRECATED void setLineSpacing(double s)
Sets the line spacing to use between lines of legend text.
void setColumnCount(int c)
Sets the desired minimum number of columns to show in the legend.
void setTitleAlignment(Qt::AlignmentFlag alignment)
Sets the alignment of the legend title.
Q_DECL_DEPRECATED void setMmPerMapUnit(double mmPerMapUnit)
Q_DECL_DEPRECATED void setDpi(int dpi)
Qt::AlignmentFlag titleAlignment() const
Returns the alignment of the legend title.
QSizeF symbolSize() const
Returns the default symbol size (in millimeters) used for legend items.
Q_DECL_DEPRECATED QColor fontColor() const
Returns the font color used for legend items.
double maximumSymbolSize() const
Returns the maximum symbol size (in mm).
QString title() const
Returns the title for the legend, which will be rendered above all legend items.
QColor rasterStrokeColor() const
Returns the stroke color for the stroke drawn around raster symbol items.
Q_DECL_DEPRECATED double lineSpacing() const
Returns the line spacing to use between lines of legend text.
Q_DECL_DEPRECATED void setUseAdvancedEffects(bool use)
void setSplitLayer(bool s)
Sets whether layer components can be split over multiple columns.
double columnSpace() const
Returns the margin space between adjacent columns (in millimeters).
QgsLegendStyle & rstyle(QgsLegendStyle::Style s)
Returns modifiable reference to the style for a legend component.
void setEqualColumnWidth(bool s)
Sets whether all columns should have equal widths.
void setBoxSpace(double s)
Sets the legend box space (in millimeters), which is the empty margin around the inside of the legend...
double boxSpace() const
Returns the legend box space (in millimeters), which is the empty margin around the inside of the leg...
void setMaximumSymbolSize(double size)
Set the maximum symbol size for symbol (in millimeters).
bool splitLayer() const
Returns true if layer components can be split over multiple columns.
void setMinimumSymbolSize(double size)
Set the minimum symbol size for symbol (in millimeters).
void setRasterStrokeWidth(double width)
Sets the stroke width for the stroke drawn around raster symbol items.
Qt::AlignmentFlag symbolAlignment() const
Returns the alignment for placement of legend symbols.
bool equalColumnWidth() const
Returns true if all columns should have equal widths.
void setSymbolSize(QSizeF s)
Sets the default symbol size (in millimeters) used for legend items.
void setWmsLegendSize(QSizeF s)
Sets the desired size (in millimeters) of WMS legend graphics shown in the legend.
Contains detailed styling information relating to how a layout legend should be rendered.
Q_DECL_DEPRECATED QFont font() const
Returns the font used for rendering this legend component.
QgsTextFormat & textFormat()
Returns the text format used for rendering this legend component.
void setMargin(Side side, double margin)
Sets the margin (in mm) for the specified side of the component.
void readXml(const QDomElement &elem, const QDomDocument &doc, const QgsReadWriteContext &context=QgsReadWriteContext())
Reads the component's style definition from an XML element.
Style
Component of legends which can be styled.
@ Group
Legend group title.
@ Symbol
Symbol icon (excluding label)
@ Subgroup
Legend subgroup title.
@ SymbolLabel
Symbol label (excluding icon)
Q_DECL_DEPRECATED void setFont(const QFont &font)
Sets the font used for rendering this legend component.
void writeXml(const QString &name, QDomElement &elem, QDomDocument &doc, const QgsReadWriteContext &context=QgsReadWriteContext()) const
Writes the component's style definition to an XML element.
void setTextFormat(const QgsTextFormat &format)
Sets the text format used for rendering this legend component.
The QgsMapSettings class contains configuration for rendering of the map.
double scale() const
Returns the calculated map scale.
void projectColorsChanged()
Emitted whenever the project's color scheme has been changed.
The class is used as a container of context for various read/write operations on other objects.
A rectangle specified with double values.
double height() const SIP_HOLDGIL
Returns the height of the rectangle.
double width() const SIP_HOLDGIL
Returns the width of the rectangle.
Contains information about the context of a rendering operation.
double scaleFactor() const
Returns the scaling factor for the render to convert painter units to physical sizes.
QPainter * painter()
Returns the destination QPainter for the render operation.
QgsExpressionContext & expressionContext()
Gets the expression context.
Scoped object for saving and restoring a QPainter object's state.
An interface for classes which can visit style entity (e.g.
virtual bool visit(const QgsStyleEntityVisitorInterface::StyleLeaf &entity)
Called when the visitor will visit a style entity.
A legend patch shape entity for QgsStyle databases.
static QColor decodeColor(const QString &str)
static QString encodeColor(const QColor &color)
Implementation of legend node interface for displaying preview of vector symbols and their labels and...
Container for all settings relating to text rendering.
void setColor(const QColor &color)
Sets the color that text will be rendered in.
void setLineHeightUnit(Qgis::RenderUnit unit)
Sets the unit for the line height for text.
double size() const
Returns the size for rendered text.
void setLineHeight(double height)
Sets the line height for text.
Represents a vector layer which manages a vector based data sets.
QgsVectorLayerFeatureCounter * countSymbolFeatures(bool storeSymbolFids=false)
Count features for symbols.
long long featureCount(const QString &legendKey) const
Number of features rendered with specified legend key.
void symbolFeatureCountMapChanged()
Emitted when the feature count for symbols on this layer has been recalculated.
QgsLayerTreeModelLegendNode * legendNode(const QString &rule, QgsLayerTreeModel &model)
As part of the API refactoring and improvements which landed in the Processing API was substantially reworked from the x version This was done in order to allow much of the underlying Processing framework to be ported into c
#define Q_NOWARN_DEPRECATED_POP
#define Q_NOWARN_DEPRECATED_PUSH
#define QgsDebugMsgLevel(str, level)
Single variable definition for use within a QgsExpressionContextScope.
Structure that stores all data associated with one map layer.
Contains information relating to the style entity currently being visited.