QGIS API Documentation 3.41.0-Master (181b2f43d8e)
Loading...
Searching...
No Matches
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 "moc_qgslayoutitemmapitem.cpp"
20#include "qgslayoutitemmap.h"
21#include "qgslayout.h"
22#include <QUuid>
23
25 : QgsLayoutObject( map->layout() )
26 , mName( name )
27 , mMap( map )
28 , mUuid( QUuid::createUuid().toString() )
29 , mEnabled( true )
30{
31
32}
33
34bool QgsLayoutItemMapItem::writeXml( QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context ) const
35{
36 Q_UNUSED( document )
37
38 QgsLayoutObject::writeObjectPropertiesToElement( element, document, context );
39
40 element.setAttribute( QStringLiteral( "uuid" ), mUuid );
41 element.setAttribute( QStringLiteral( "name" ), mName );
42 element.setAttribute( QStringLiteral( "show" ), mEnabled );
43 element.setAttribute( QStringLiteral( "position" ), static_cast< int >( mStackingPosition ) );
44
45 if ( mStackingLayer )
46 {
47 element.setAttribute( QStringLiteral( "stackingLayer" ), mStackingLayer.layerId );
48 element.setAttribute( QStringLiteral( "stackingLayerName" ), mStackingLayer.name );
49 element.setAttribute( QStringLiteral( "stackingLayerSource" ), mStackingLayer.source );
50 element.setAttribute( QStringLiteral( "stackingLayerProvider" ), mStackingLayer.provider );
51 }
52
53 return true;
54}
55
56bool QgsLayoutItemMapItem::readXml( const QDomElement &itemElem, const QDomDocument &doc, const QgsReadWriteContext &context )
57{
59
60 mUuid = itemElem.attribute( QStringLiteral( "uuid" ) );
61 mName = itemElem.attribute( QStringLiteral( "name" ) );
62 mEnabled = ( itemElem.attribute( QStringLiteral( "show" ), QStringLiteral( "0" ) ) != QLatin1String( "0" ) );
63 mStackingPosition = static_cast< StackingPosition >( itemElem.attribute( QStringLiteral( "position" ), QString::number( QgsLayoutItemMapItem::StackBelowMapLabels ) ).toInt() );
64
65 const QString layerId = itemElem.attribute( QStringLiteral( "stackingLayer" ) );
66 const QString layerName = itemElem.attribute( QStringLiteral( "stackingLayerName" ) );
67 const QString layerSource = itemElem.attribute( QStringLiteral( "stackingLayerSource" ) );
68 const QString layerProvider = itemElem.attribute( QStringLiteral( "stackingLayerProvider" ) );
69 mStackingLayer = QgsMapLayerRef( layerId, layerName, layerSource, layerProvider );
70 if ( mMap && mMap->layout() )
72
73 return true;
74}
75
79
84
86{
87 return mMap;
88}
89
90void QgsLayoutItemMapItem::setName( const QString &name )
91{
92 mName = name;
93}
94
96{
97 return mName;
98}
99
100void QgsLayoutItemMapItem::setEnabled( const bool enabled )
101{
103}
104
106{
107 return mEnabled;
108}
109
111{
112 return false;
113}
114
119
124
132
134{
135 return true;
136}
137
139{
140 return nullptr;
141}
142
143//
144// QgsLayoutItemMapItemStack
145//
146
152
157
162
163void QgsLayoutItemMapItemStack::removeItem( const QString &itemId )
164{
165 for ( int i = mItems.size() - 1; i >= 0; --i )
166 {
167 if ( mItems.at( i )->id() == itemId )
168 {
169 delete mItems.takeAt( i );
170 return;
171 }
172 }
173}
174
175void QgsLayoutItemMapItemStack::moveItemUp( const QString &itemId )
176{
177 QgsLayoutItemMapItem *targetItem = item( itemId );
178 if ( !targetItem )
179 {
180 return;
181 }
182
183 int index = mItems.indexOf( targetItem );
184 if ( index >= mItems.size() - 1 )
185 {
186 return;
187 }
188 mItems.swapItemsAt( index, index + 1 );
189}
190
191void QgsLayoutItemMapItemStack::moveItemDown( const QString &itemId )
192{
193 QgsLayoutItemMapItem *targetItem = item( itemId );
194 if ( !targetItem )
195 {
196 return;
197 }
198
199 int index = mItems.indexOf( targetItem );
200 if ( index < 1 )
201 {
202 return;
203 }
204 mItems.swapItemsAt( index, index - 1 );
205}
206
208{
210 {
211 if ( item->id() == itemId )
212 {
213 return item;
214 }
215 }
216
217 return nullptr;
218}
219
221{
222 if ( index < mItems.length() )
223 {
224 return mItems.at( index );
225 }
226
227 return nullptr;
228}
229
234
235QList<QgsLayoutItemMapItem *> QgsLayoutItemMapItemStack::asList() const
236{
237 QList< QgsLayoutItemMapItem * > list;
238 list.reserve( mItems.size() );
240 {
241 list.append( item );
242 }
243 return list;
244}
245
246bool QgsLayoutItemMapItemStack::writeXml( QDomElement &elem, QDomDocument &doc, const QgsReadWriteContext &context ) const
247{
248 //write item stack
250 {
251 item->writeXml( elem, doc, context );
252 }
253
254 return true;
255}
256
258{
259 for ( QgsLayoutItemMapItem *item : std::as_const( mItems ) )
260 {
262 }
263}
264
265void QgsLayoutItemMapItemStack::drawItems( QPainter *painter, bool ignoreStacking )
266{
267 if ( !painter )
268 {
269 return;
270 }
271
272 for ( QgsLayoutItemMapItem *item : std::as_const( mItems ) )
273 {
274 switch ( item->stackingPosition() )
275 {
280 if ( !ignoreStacking )
281 break;
282
283 [[fallthrough]];
285 item->draw( painter );
286 break;
287
288 }
289 }
290}
291
293{
295 {
296 if ( item->enabled() && item->usesAdvancedEffects() )
297 {
298 return true;
299 }
300 }
301 return false;
302}
303
305{
307 {
308 if ( item->enabled() )
309 {
310 return true;
311 }
312 }
313 return false;
314}
315
317{
318 qDeleteAll( mItems );
319 mItems.clear();
320}
321
322
323
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.
Base class for all map layer types.
Definition qgsmaplayer.h:76
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.
_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.