QGIS API Documentation  3.18.1-Zürich (202f1bf7e5)
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 
18 #include "qgslayoutitemmapitem.h"
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 
33 bool 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 
55 bool QgsLayoutItemMapItem::readXml( const QDomElement &itemElem, const QDomDocument &doc, const QgsReadWriteContext &context )
56 {
57  QgsLayoutObject::readObjectPropertiesFromElement( itemElem, doc, context );
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 
89 void QgsLayoutItemMapItem::setName( const QString &name )
90 {
91  mName = name;
92 }
93 
95 {
96  return mName;
97 }
98 
99 void QgsLayoutItemMapItem::setEnabled( const bool enabled )
100 {
101  mEnabled = enabled;
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 )
127  return mMap->createExpressionContext();
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 
162 void 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 
174 void 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 #if QT_VERSION < QT_VERSION_CHECK(5, 13, 0)
188  mItems.swap( index, index + 1 );
189 #else
190  mItems.swapItemsAt( index, index + 1 );
191 #endif
192 }
193 
194 void QgsLayoutItemMapItemStack::moveItemDown( const QString &itemId )
195 {
196  QgsLayoutItemMapItem *targetItem = item( itemId );
197  if ( !targetItem )
198  {
199  return;
200  }
201 
202  int index = mItems.indexOf( targetItem );
203  if ( index < 1 )
204  {
205  return;
206  }
207 #if QT_VERSION < QT_VERSION_CHECK(5, 13, 0)
208  mItems.swap( index, index - 1 );
209 #else
210  mItems.swapItemsAt( index, index - 1 );
211 #endif
212 }
213 
215 {
216  for ( QgsLayoutItemMapItem *item : mItems )
217  {
218  if ( item->id() == itemId )
219  {
220  return item;
221  }
222  }
223 
224  return nullptr;
225 }
226 
228 {
229  if ( index < mItems.length() )
230  {
231  return mItems.at( index );
232  }
233 
234  return nullptr;
235 }
236 
238 {
239  return *mItems[idx];
240 }
241 
242 QList<QgsLayoutItemMapItem *> QgsLayoutItemMapItemStack::asList() const
243 {
244  QList< QgsLayoutItemMapItem * > list;
245  list.reserve( mItems.size() );
246  for ( QgsLayoutItemMapItem *item : mItems )
247  {
248  list.append( item );
249  }
250  return list;
251 }
252 
253 bool QgsLayoutItemMapItemStack::writeXml( QDomElement &elem, QDomDocument &doc, const QgsReadWriteContext &context ) const
254 {
255  //write item stack
256  for ( QgsLayoutItemMapItem *item : mItems )
257  {
258  item->writeXml( elem, doc, context );
259  }
260 
261  return true;
262 }
263 
265 {
266  for ( QgsLayoutItemMapItem *item : qgis::as_const( mItems ) )
267  {
269  }
270 }
271 
272 void QgsLayoutItemMapItemStack::drawItems( QPainter *painter, bool ignoreStacking )
273 {
274  if ( !painter )
275  {
276  return;
277  }
278 
279  for ( QgsLayoutItemMapItem *item : qgis::as_const( mItems ) )
280  {
281  switch ( item->stackingPosition() )
282  {
287  if ( !ignoreStacking )
288  break;
289 
292  item->draw( painter );
293  break;
294 
295  }
296  }
297 }
298 
300 {
301  for ( QgsLayoutItemMapItem *item : mItems )
302  {
303  if ( item->enabled() && item->usesAdvancedEffects() )
304  {
305  return true;
306  }
307  }
308  return false;
309 }
310 
312 {
313  for ( QgsLayoutItemMapItem *item : mItems )
314  {
315  if ( item->enabled() )
316  {
317  return true;
318  }
319  }
320  return false;
321 }
322 
324 {
325  qDeleteAll( mItems );
326  mItems.clear();
327 }
328 
329 
330 
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:132
Base class for all map layer types.
Definition: qgsmaplayer.h:85
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:829
_LayerRef< QgsMapLayer > QgsMapLayerRef
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.
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.
void setLayer(TYPE *l)
Sets the reference to point to a specified layer.
QString layerId
Original layer ID.