QGIS API Documentation 3.32.0-Lima (311a8cb8a6)
qgslayoutitemmapitem.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgslayoutitemmapitem.cpp
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
19#include "qgslayoutitemmap.h"
20#include "qgslayout.h"
21#include <QUuid>
22
24 : QgsLayoutObject( map->layout() )
25 , mName( name )
26 , mMap( map )
27 , mUuid( QUuid::createUuid().toString() )
28 , mEnabled( true )
29{
30
31}
32
33bool QgsLayoutItemMapItem::writeXml( QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context ) const
34{
35 Q_UNUSED( document )
36
37 QgsLayoutObject::writeObjectPropertiesToElement( element, document, context );
38
39 element.setAttribute( QStringLiteral( "uuid" ), mUuid );
40 element.setAttribute( QStringLiteral( "name" ), mName );
41 element.setAttribute( QStringLiteral( "show" ), mEnabled );
42 element.setAttribute( QStringLiteral( "position" ), static_cast< int >( mStackingPosition ) );
43
44 if ( mStackingLayer )
45 {
46 element.setAttribute( QStringLiteral( "stackingLayer" ), mStackingLayer.layerId );
47 element.setAttribute( QStringLiteral( "stackingLayerName" ), mStackingLayer.name );
48 element.setAttribute( QStringLiteral( "stackingLayerSource" ), mStackingLayer.source );
49 element.setAttribute( QStringLiteral( "stackingLayerProvider" ), mStackingLayer.provider );
50 }
51
52 return true;
53}
54
55bool QgsLayoutItemMapItem::readXml( const QDomElement &itemElem, const QDomDocument &doc, const QgsReadWriteContext &context )
56{
58
59 mUuid = itemElem.attribute( QStringLiteral( "uuid" ) );
60 mName = itemElem.attribute( QStringLiteral( "name" ) );
61 mEnabled = ( itemElem.attribute( QStringLiteral( "show" ), QStringLiteral( "0" ) ) != QLatin1String( "0" ) );
62 mStackingPosition = static_cast< StackingPosition >( itemElem.attribute( QStringLiteral( "position" ), QString::number( QgsLayoutItemMapItem::StackBelowMapLabels ) ).toInt() );
63
64 const QString layerId = itemElem.attribute( QStringLiteral( "stackingLayer" ) );
65 const QString layerName = itemElem.attribute( QStringLiteral( "stackingLayerName" ) );
66 const QString layerSource = itemElem.attribute( QStringLiteral( "stackingLayerSource" ) );
67 const QString layerProvider = itemElem.attribute( QStringLiteral( "stackingLayerProvider" ) );
68 mStackingLayer = QgsMapLayerRef( layerId, layerName, layerSource, layerProvider );
69 if ( mMap && mMap->layout() )
71
72 return true;
73}
74
76{
77}
78
80{
81 mMap = map;
82}
83
85{
86 return mMap;
87}
88
89void QgsLayoutItemMapItem::setName( const QString &name )
90{
91 mName = name;
92}
93
95{
96 return mName;
97}
98
99void QgsLayoutItemMapItem::setEnabled( const bool enabled )
100{
102}
103
105{
106 return mEnabled;
107}
108
110{
111 return false;
112}
113
115{
116 return mStackingLayer.get();
117}
118
120{
121 mStackingLayer.setLayer( layer );
122}
123
125{
126 if ( mMap )
128
130}
131
133{
134 return true;
135}
136
138{
139 return nullptr;
140}
141
142//
143// QgsLayoutItemMapItemStack
144//
145
147 : mMap( map )
148{
149
150}
151
153{
154 removeItems();
155}
156
158{
159 mItems.append( item );
160}
161
162void QgsLayoutItemMapItemStack::removeItem( const QString &itemId )
163{
164 for ( int i = mItems.size() - 1; i >= 0; --i )
165 {
166 if ( mItems.at( i )->id() == itemId )
167 {
168 delete mItems.takeAt( i );
169 return;
170 }
171 }
172}
173
174void QgsLayoutItemMapItemStack::moveItemUp( const QString &itemId )
175{
176 QgsLayoutItemMapItem *targetItem = item( itemId );
177 if ( !targetItem )
178 {
179 return;
180 }
181
182 int index = mItems.indexOf( targetItem );
183 if ( index >= mItems.size() - 1 )
184 {
185 return;
186 }
187 mItems.swapItemsAt( index, index + 1 );
188}
189
190void QgsLayoutItemMapItemStack::moveItemDown( const QString &itemId )
191{
192 QgsLayoutItemMapItem *targetItem = item( itemId );
193 if ( !targetItem )
194 {
195 return;
196 }
197
198 int index = mItems.indexOf( targetItem );
199 if ( index < 1 )
200 {
201 return;
202 }
203 mItems.swapItemsAt( index, index - 1 );
204}
205
207{
209 {
210 if ( item->id() == itemId )
211 {
212 return item;
213 }
214 }
215
216 return nullptr;
217}
218
220{
221 if ( index < mItems.length() )
222 {
223 return mItems.at( index );
224 }
225
226 return nullptr;
227}
228
230{
231 return *mItems[idx];
232}
233
234QList<QgsLayoutItemMapItem *> QgsLayoutItemMapItemStack::asList() const
235{
236 QList< QgsLayoutItemMapItem * > list;
237 list.reserve( mItems.size() );
239 {
240 list.append( item );
241 }
242 return list;
243}
244
245bool QgsLayoutItemMapItemStack::writeXml( QDomElement &elem, QDomDocument &doc, const QgsReadWriteContext &context ) const
246{
247 //write item stack
249 {
250 item->writeXml( elem, doc, context );
251 }
252
253 return true;
254}
255
257{
258 for ( QgsLayoutItemMapItem *item : std::as_const( mItems ) )
259 {
261 }
262}
263
264void QgsLayoutItemMapItemStack::drawItems( QPainter *painter, bool ignoreStacking )
265{
266 if ( !painter )
267 {
268 return;
269 }
270
271 for ( QgsLayoutItemMapItem *item : std::as_const( mItems ) )
272 {
273 switch ( item->stackingPosition() )
274 {
279 if ( !ignoreStacking )
280 break;
281
284 item->draw( painter );
285 break;
286
287 }
288 }
289}
290
292{
294 {
295 if ( item->enabled() && item->usesAdvancedEffects() )
296 {
297 return true;
298 }
299 }
300 return false;
301}
302
304{
306 {
307 if ( item->enabled() )
308 {
309 return true;
310 }
311 }
312 return false;
313}
314
316{
317 qDeleteAll( mItems );
318 mItems.clear();
319}
320
321
322
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
virtual bool writeXml(QDomElement &element, QDomDocument &doc, const QgsReadWriteContext &context) const
Stores the state of the item stack in a DOM node, where element is the DOM element corresponding to a...
void addItem(QgsLayoutItemMapItem *item)
Adds a new map item to the stack and takes ownership of the item.
virtual void finalizeRestoreFromXml()
Called after all pending items have been restored from XML.
bool containsAdvancedEffects() const
Returns whether any items within the stack contain advanced effects, such as blending modes.
void removeItem(const QString &itemId)
Removes an item which matching itemId from the stack and deletes the corresponding QgsLayoutItemMapIt...
QgsLayoutItemMapItem * item(int index) const
Returns a reference to the item at the specified index within the stack.
QList< QgsLayoutItemMapItem * > asList() const
Returns a list of QgsLayoutItemMapItems contained by the stack.
void drawItems(QPainter *painter, bool ignoreStacking=true)
Draws the items from the stack on a specified painter.
void moveItemUp(const QString &itemId)
Moves an item which matching itemId up the stack, causing it to be rendered above other items.
void removeItems()
Clears the item stack and deletes all QgsLayoutItemMapItems contained by the stack.
QgsLayoutItemMapItemStack(QgsLayoutItemMap *map)
Constructor for QgsLayoutItemMapItemStack, attached to the specified map.
QList< QgsLayoutItemMapItem * > mItems
void moveItemDown(const QString &itemId)
Moves an item which matching itemId up the stack, causing it to be rendered above other items.
QgsLayoutItemMapItem & operator[](int index)
Returns a reference to an item at the specified index within the stack.
bool hasEnabledItems() const
Returns true if the stack has any currently enabled items.
An item which is drawn inside a QgsLayoutItemMap, e.g., a grid or map overview.
void setMap(QgsLayoutItemMap *map)
Sets the corresponding layout map for the item.
QString id() const
Returns the unique id for the map item.
StackingPosition
Item stacking position, specifies where the in the map's stack the item should be rendered.
@ StackBelowMapLabels
Render above all map layers, but below map labels.
@ StackAboveMapLabels
Render above all map layers and labels.
@ StackBelowMapLayer
Render below a specific map layer (see stackingLayer())
@ StackAboveMapLayer
Render above a specific map layer (see stackingLayer())
@ StackBelowMap
Render below all map layers.
QgsLayoutItemMap * mMap
Associated map.
virtual bool readXml(const QDomElement &element, const QDomDocument &doc, const QgsReadWriteContext &context)
Sets the map item state from a DOM document, where element is the DOM node corresponding to a 'Layout...
void setStackingLayer(QgsMapLayer *layer)
Sets the item's stacking layer, which specifies where the in the map's stack the item should be rende...
StackingPosition mStackingPosition
virtual bool writeXml(QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context) const
Stores map item state in a DOM element, where element is the DOM element corresponding to a 'LayoutMa...
QString mName
Friendly display name.
virtual bool usesAdvancedEffects() const
Returns true if the item is drawn using advanced effects, such as blend modes.
bool mEnabled
True if item is to be displayed on map.
virtual void setEnabled(bool enabled)
Controls whether the item will be drawn.
StackingPosition stackingPosition() const
Returns the item's stacking position, which specifies where the in the map's stack the item should be...
QString name() const
Returns the friendly display name for the item.
virtual bool accept(QgsStyleEntityVisitorInterface *visitor) const
Accepts the specified style entity visitor, causing it to visit all style entities associated with th...
void setName(const QString &name)
Sets the friendly display name for the item.
QgsLayoutItemMapItem(const QString &name, QgsLayoutItemMap *map)
Constructor for QgsLayoutItemMapItem, attached to the specified map.
bool enabled() const
Returns whether the item will be drawn.
virtual void finalizeRestoreFromXml()
Called after all pending items have been restored from XML.
QgsExpressionContext createExpressionContext() const override
This method needs to be reimplemented in all classes which implement this interface and return an exp...
const QgsLayoutItemMap * map() const
Returns the layout item map for the item.
virtual void draw(QPainter *painter)=0
Draws the item on to a destination painter.
QgsMapLayer * stackingLayer() const
Returns the item's stacking layer, which specifies where the in the map's stack the item should be re...
virtual QgsMapLayer * mapLayer()
Returns the internal map layer used by this item, if available.
Layout graphical items for displaying a map.
QgsExpressionContext createExpressionContext() const override
This method needs to be reimplemented in all classes which implement this interface and return an exp...
A base class for objects which belong to a layout.
bool readObjectPropertiesFromElement(const QDomElement &parentElement, const QDomDocument &document, const QgsReadWriteContext &context)
Sets object properties from a DOM element.
const QgsLayout * layout() const
Returns the layout the object is attached to.
QgsExpressionContext createExpressionContext() const override
Creates an expression context relating to the objects' current state.
bool writeObjectPropertiesToElement(QDomElement &parentElement, QDomDocument &document, const QgsReadWriteContext &context) const
Stores object properties within an XML DOM element.
QgsProject * project() const
The project associated with the layout.
Definition: qgslayout.cpp:140
Base class for all map layer types.
Definition: qgsmaplayer.h:73
The class is used as a container of context for various read/write operations on other objects.
An interface for classes which can visit style entity (e.g.
#define FALLTHROUGH
Definition: qgis.h:4599
_LayerRef< QgsMapLayer > QgsMapLayerRef
TYPE * resolveWeakly(const QgsProject *project, MatchType matchType=MatchType::All)
Resolves the map layer by attempting to find a matching layer in a project using a weak match.
QString source
Weak reference to layer public source.
QString name
Weak reference to layer name.
TYPE * get() const
Returns a pointer to the layer, or nullptr if the reference has not yet been matched to a layer.
QString provider
Weak reference to layer provider.
void setLayer(TYPE *l)
Sets the reference to point to a specified layer.
QString layerId
Original layer ID.