QGIS API Documentation 4.0.0-Norrköping (1ddcee3d0e4)
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
37bool QgsLayoutItemMapItem::writeXml( QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context ) const
38{
39 Q_UNUSED( document )
40
41 QgsLayoutObject::writeObjectPropertiesToElement( element, document, context );
42
43 element.setAttribute( u"uuid"_s, mUuid );
44 element.setAttribute( u"name"_s, mName );
45 element.setAttribute( u"show"_s, mEnabled );
46 element.setAttribute( u"position"_s, static_cast< int >( mStackingPosition ) );
47
48 if ( mStackingLayer )
49 {
50 element.setAttribute( u"stackingLayer"_s, mStackingLayer.layerId );
51 element.setAttribute( u"stackingLayerName"_s, mStackingLayer.name );
52 element.setAttribute( u"stackingLayerSource"_s, mStackingLayer.source );
53 element.setAttribute( u"stackingLayerProvider"_s, mStackingLayer.provider );
54 }
55
56 return true;
57}
58
59bool QgsLayoutItemMapItem::readXml( const QDomElement &itemElem, const QDomDocument &doc, const QgsReadWriteContext &context )
60{
62
63 mUuid = itemElem.attribute( u"uuid"_s );
64 mName = itemElem.attribute( u"name"_s );
65 mEnabled = ( itemElem.attribute( u"show"_s, u"0"_s ) != "0"_L1 );
66 mStackingPosition = static_cast< StackingPosition >( itemElem.attribute( u"position"_s, QString::number( QgsLayoutItemMapItem::StackBelowMapLabels ) ).toInt() );
67
68 const QString layerId = itemElem.attribute( u"stackingLayer"_s );
69 const QString layerName = itemElem.attribute( u"stackingLayerName"_s );
70 const QString layerSource = itemElem.attribute( u"stackingLayerSource"_s );
71 const QString layerProvider = itemElem.attribute( u"stackingLayerProvider"_s );
72 mStackingLayer = QgsMapLayerRef( layerId, layerName, layerSource, layerProvider );
73 if ( mMap && mMap->layout() )
74 mStackingLayer.resolveWeakly( mMap->layout()->project() );
75
76 return true;
77}
78
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
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 {
261 item->finalizeRestoreFromXml();
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
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}
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