QGIS API Documentation 3.99.0-Master (2fe06baccd8)
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 <QUuid>
24
25#include "moc_qgslayoutitemmapitem.cpp"
26
29 , mName( name )
30 , mMap( map )
31 , mUuid( QUuid::createUuid().toString() )
32{
33
34}
35
36bool QgsLayoutItemMapItem::writeXml( QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context ) const
37{
38 Q_UNUSED( document )
39
40 QgsLayoutObject::writeObjectPropertiesToElement( element, document, context );
41
42 element.setAttribute( QStringLiteral( "uuid" ), mUuid );
43 element.setAttribute( QStringLiteral( "name" ), mName );
44 element.setAttribute( QStringLiteral( "show" ), mEnabled );
45 element.setAttribute( QStringLiteral( "position" ), static_cast< int >( mStackingPosition ) );
46
47 if ( mStackingLayer )
48 {
49 element.setAttribute( QStringLiteral( "stackingLayer" ), mStackingLayer.layerId );
50 element.setAttribute( QStringLiteral( "stackingLayerName" ), mStackingLayer.name );
51 element.setAttribute( QStringLiteral( "stackingLayerSource" ), mStackingLayer.source );
52 element.setAttribute( QStringLiteral( "stackingLayerProvider" ), mStackingLayer.provider );
53 }
54
55 return true;
56}
57
58bool QgsLayoutItemMapItem::readXml( const QDomElement &itemElem, const QDomDocument &doc, const QgsReadWriteContext &context )
59{
61
62 mUuid = itemElem.attribute( QStringLiteral( "uuid" ) );
63 mName = itemElem.attribute( QStringLiteral( "name" ) );
64 mEnabled = ( itemElem.attribute( QStringLiteral( "show" ), QStringLiteral( "0" ) ) != QLatin1String( "0" ) );
65 mStackingPosition = static_cast< StackingPosition >( itemElem.attribute( QStringLiteral( "position" ), QString::number( QgsLayoutItemMapItem::StackBelowMapLabels ) ).toInt() );
66
67 const QString layerId = itemElem.attribute( QStringLiteral( "stackingLayer" ) );
68 const QString layerName = itemElem.attribute( QStringLiteral( "stackingLayerName" ) );
69 const QString layerSource = itemElem.attribute( QStringLiteral( "stackingLayerSource" ) );
70 const QString layerProvider = itemElem.attribute( QStringLiteral( "stackingLayerProvider" ) );
71 mStackingLayer = QgsMapLayerRef( layerId, layerName, layerSource, layerProvider );
72 if ( mMap && mMap->layout() )
73 mStackingLayer.resolveWeakly( mMap->layout()->project() );
74
75 return true;
76}
77
81
86
88{
89 return mMap;
90}
91
92void QgsLayoutItemMapItem::setName( const QString &name )
93{
94 mName = name;
95}
96
98{
99 return mName;
100}
101
103{
105}
106
108{
109 return mEnabled;
110}
111
113{
114 return false;
115}
116
121
123{
124 mStackingLayer.setLayer( layer );
125}
126
128{
129 if ( mMap )
130 return mMap->createExpressionContext();
131
133}
134
136{
137 return true;
138}
139
141{
142 return nullptr;
143}
144
145//
146// QgsLayoutItemMapItemStack
147//
148
154
159
164
165void QgsLayoutItemMapItemStack::removeItem( const QString &itemId )
166{
167 for ( int i = mItems.size() - 1; i >= 0; --i )
168 {
169 if ( mItems.at( i )->id() == itemId )
170 {
171 delete mItems.takeAt( i );
172 return;
173 }
174 }
175}
176
177void QgsLayoutItemMapItemStack::moveItemUp( const QString &itemId )
178{
179 QgsLayoutItemMapItem *targetItem = item( itemId );
180 if ( !targetItem )
181 {
182 return;
183 }
184
185 int index = mItems.indexOf( targetItem );
186 if ( index >= mItems.size() - 1 )
187 {
188 return;
189 }
190 mItems.swapItemsAt( index, index + 1 );
191}
192
193void QgsLayoutItemMapItemStack::moveItemDown( const QString &itemId )
194{
195 QgsLayoutItemMapItem *targetItem = item( itemId );
196 if ( !targetItem )
197 {
198 return;
199 }
200
201 int index = mItems.indexOf( targetItem );
202 if ( index < 1 )
203 {
204 return;
205 }
206 mItems.swapItemsAt( index, index - 1 );
207}
208
210{
212 {
213 if ( item->id() == itemId )
214 {
215 return item;
216 }
217 }
218
219 return nullptr;
220}
221
223{
224 if ( index < mItems.length() )
225 {
226 return mItems.at( index );
227 }
228
229 return nullptr;
230}
231
236
237QList<QgsLayoutItemMapItem *> QgsLayoutItemMapItemStack::asList() const
238{
239 QList< QgsLayoutItemMapItem * > list;
240 list.reserve( mItems.size() );
242 {
243 list.append( item );
244 }
245 return list;
246}
247
248bool QgsLayoutItemMapItemStack::writeXml( QDomElement &elem, QDomDocument &doc, const QgsReadWriteContext &context ) const
249{
250 //write item stack
252 {
253 item->writeXml( elem, doc, context );
254 }
255
256 return true;
257}
258
260{
261 for ( QgsLayoutItemMapItem *item : std::as_const( mItems ) )
262 {
263 item->finalizeRestoreFromXml();
264 }
265}
266
267void QgsLayoutItemMapItemStack::drawItems( QPainter *painter, bool ignoreStacking )
268{
269 if ( !painter )
270 {
271 return;
272 }
273
274 for ( QgsLayoutItemMapItem *item : std::as_const( mItems ) )
275 {
276 switch ( item->stackingPosition() )
277 {
282 if ( !ignoreStacking )
283 break;
284
285 [[fallthrough]];
287 item->draw( painter );
288 break;
289
290 }
291 }
292}
293
295{
297 {
298 if ( item->enabled() && item->usesAdvancedEffects() )
299 {
300 return true;
301 }
302 }
303 return false;
304}
305
307{
309 {
310 if ( item->enabled() )
311 {
312 return true;
313 }
314 }
315 return false;
316}
317
319{
320 qDeleteAll( mItems );
321 mItems.clear();
322}
323
324
325
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:80
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