QGIS API Documentation 3.99.0-Master (d270888f95f)
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
20#include "qgslayout.h"
21#include "qgslayoutitemmap.h"
22
23#include <QString>
24#include <QUuid>
25
26#include "moc_qgslayoutitemmapitem.cpp"
27
28using namespace Qt::StringLiterals;
29
32 , mName( name )
33 , mMap( map )
34 , mUuid( QUuid::createUuid().toString() )
35{
36
37}
38
39bool QgsLayoutItemMapItem::writeXml( QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context ) const
40{
41 Q_UNUSED( document )
42
43 QgsLayoutObject::writeObjectPropertiesToElement( element, document, context );
44
45 element.setAttribute( u"uuid"_s, mUuid );
46 element.setAttribute( u"name"_s, mName );
47 element.setAttribute( u"show"_s, mEnabled );
48 element.setAttribute( u"position"_s, static_cast< int >( mStackingPosition ) );
49
50 if ( mStackingLayer )
51 {
52 element.setAttribute( u"stackingLayer"_s, mStackingLayer.layerId );
53 element.setAttribute( u"stackingLayerName"_s, mStackingLayer.name );
54 element.setAttribute( u"stackingLayerSource"_s, mStackingLayer.source );
55 element.setAttribute( u"stackingLayerProvider"_s, mStackingLayer.provider );
56 }
57
58 return true;
59}
60
61bool QgsLayoutItemMapItem::readXml( const QDomElement &itemElem, const QDomDocument &doc, const QgsReadWriteContext &context )
62{
64
65 mUuid = itemElem.attribute( u"uuid"_s );
66 mName = itemElem.attribute( u"name"_s );
67 mEnabled = ( itemElem.attribute( u"show"_s, u"0"_s ) != "0"_L1 );
68 mStackingPosition = static_cast< StackingPosition >( itemElem.attribute( u"position"_s, QString::number( QgsLayoutItemMapItem::StackBelowMapLabels ) ).toInt() );
69
70 const QString layerId = itemElem.attribute( u"stackingLayer"_s );
71 const QString layerName = itemElem.attribute( u"stackingLayerName"_s );
72 const QString layerSource = itemElem.attribute( u"stackingLayerSource"_s );
73 const QString layerProvider = itemElem.attribute( u"stackingLayerProvider"_s );
74 mStackingLayer = QgsMapLayerRef( layerId, layerName, layerSource, layerProvider );
75 if ( mMap && mMap->layout() )
76 mStackingLayer.resolveWeakly( mMap->layout()->project() );
77
78 return true;
79}
80
84
89
91{
92 return mMap;
93}
94
95void QgsLayoutItemMapItem::setName( const QString &name )
96{
97 mName = name;
98}
99
101{
102 return mName;
103}
104
106{
108}
109
111{
112 return mEnabled;
113}
114
116{
117 return false;
118}
119
124
126{
127 mStackingLayer.setLayer( layer );
128}
129
131{
132 if ( mMap )
133 return mMap->createExpressionContext();
134
136}
137
139{
140 return true;
141}
142
144{
145 return nullptr;
146}
147
148//
149// QgsLayoutItemMapItemStack
150//
151
157
162
167
168void QgsLayoutItemMapItemStack::removeItem( const QString &itemId )
169{
170 for ( int i = mItems.size() - 1; i >= 0; --i )
171 {
172 if ( mItems.at( i )->id() == itemId )
173 {
174 delete mItems.takeAt( i );
175 return;
176 }
177 }
178}
179
180void QgsLayoutItemMapItemStack::moveItemUp( const QString &itemId )
181{
182 QgsLayoutItemMapItem *targetItem = item( itemId );
183 if ( !targetItem )
184 {
185 return;
186 }
187
188 int index = mItems.indexOf( targetItem );
189 if ( index >= mItems.size() - 1 )
190 {
191 return;
192 }
193 mItems.swapItemsAt( index, index + 1 );
194}
195
196void QgsLayoutItemMapItemStack::moveItemDown( const QString &itemId )
197{
198 QgsLayoutItemMapItem *targetItem = item( itemId );
199 if ( !targetItem )
200 {
201 return;
202 }
203
204 int index = mItems.indexOf( targetItem );
205 if ( index < 1 )
206 {
207 return;
208 }
209 mItems.swapItemsAt( index, index - 1 );
210}
211
213{
215 {
216 if ( item->id() == itemId )
217 {
218 return item;
219 }
220 }
221
222 return nullptr;
223}
224
226{
227 if ( index < mItems.length() )
228 {
229 return mItems.at( index );
230 }
231
232 return nullptr;
233}
234
239
240QList<QgsLayoutItemMapItem *> QgsLayoutItemMapItemStack::asList() const
241{
242 QList< QgsLayoutItemMapItem * > list;
243 list.reserve( mItems.size() );
245 {
246 list.append( item );
247 }
248 return list;
249}
250
251bool QgsLayoutItemMapItemStack::writeXml( QDomElement &elem, QDomDocument &doc, const QgsReadWriteContext &context ) const
252{
253 //write item stack
255 {
256 item->writeXml( elem, doc, context );
257 }
258
259 return true;
260}
261
263{
264 for ( QgsLayoutItemMapItem *item : std::as_const( mItems ) )
265 {
266 item->finalizeRestoreFromXml();
267 }
268}
269
270void QgsLayoutItemMapItemStack::drawItems( QPainter *painter, bool ignoreStacking )
271{
272 if ( !painter )
273 {
274 return;
275 }
276
277 for ( QgsLayoutItemMapItem *item : std::as_const( mItems ) )
278 {
279 switch ( item->stackingPosition() )
280 {
285 if ( !ignoreStacking )
286 break;
287
288 [[fallthrough]];
290 item->draw( painter );
291 break;
292
293 }
294 }
295}
296
298{
300 {
301 if ( item->enabled() && item->usesAdvancedEffects() )
302 {
303 return true;
304 }
305 }
306 return false;
307}
308
310{
312 {
313 if ( item->enabled() )
314 {
315 return true;
316 }
317 }
318 return false;
319}
320
322{
323 qDeleteAll( mItems );
324 mItems.clear();
325}
326
327
328
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.
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.
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.
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.
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.
QgsLayoutObject(QgsLayout *layout)
Constructor for QgsLayoutObject, with the specified parent layout.
bool writeObjectPropertiesToElement(QDomElement &parentElement, QDomDocument &document, const QgsReadWriteContext &context) const
Stores object properties within an XML DOM element.
Base class for all map layer types.
Definition qgsmaplayer.h:83
A container for the context for various read/write operations on objects.
An interface for classes which can visit style entity (e.g.
_LayerRef< QgsMapLayer > QgsMapLayerRef