QGIS API Documentation  3.16.0-Hannover (43b64b13f3)
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 
QgsExpressionContext
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
Definition: qgsexpressioncontext.h:370
QgsLayoutItemMapItem::setEnabled
virtual void setEnabled(bool enabled)
Controls whether the item will be drawn.
Definition: qgslayoutitemmapitem.cpp:99
QgsLayoutObject::layout
const QgsLayout * layout() const
Returns the layout the object is attached to.
Definition: qgslayoutobject.cpp:126
QgsLayoutItemMapItem::StackAboveMapLayer
@ StackAboveMapLayer
Render above a specific map layer (see stackingLayer())
Definition: qgslayoutitemmapitem.h:44
QgsLayoutItemMapItemStack::finalizeRestoreFromXml
virtual void finalizeRestoreFromXml()
Called after all pending items have been restored from XML.
Definition: qgslayoutitemmapitem.cpp:264
QgsLayoutItemMapItem::setMap
void setMap(QgsLayoutItemMap *map)
Sets the corresponding layout map for the item.
Definition: qgslayoutitemmapitem.cpp:79
QgsLayoutItemMapItem::StackBelowMapLayer
@ StackBelowMapLayer
Render below a specific map layer (see stackingLayer())
Definition: qgslayoutitemmapitem.h:43
_LayerRef::resolveWeakly
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.
Definition: qgsmaplayerref.h:210
QgsReadWriteContext
The class is used as a container of context for various read/write operations on other objects.
Definition: qgsreadwritecontext.h:35
QgsLayoutItemMapItem::draw
virtual void draw(QPainter *painter)=0
Draws the item on to a destination painter.
QgsLayoutItemMapItem::createExpressionContext
QgsExpressionContext createExpressionContext() const override
This method needs to be reimplemented in all classes which implement this interface and return an exp...
Definition: qgslayoutitemmapitem.cpp:124
QgsLayoutItemMapItemStack::writeXml
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...
Definition: qgslayoutitemmapitem.cpp:253
QgsLayoutItemMapItem::mName
QString mName
Friendly display name.
Definition: qgslayoutitemmapitem.h:204
QgsLayoutItemMapItem::map
const QgsLayoutItemMap * map() const
Returns the layout item map for the item.
Definition: qgslayoutitemmapitem.cpp:84
QgsLayoutItemMapItem
An item which is drawn inside a QgsLayoutItemMap, e.g., a grid or map overview.
Definition: qgslayoutitemmapitem.h:34
QgsLayoutItemMapItem::setName
void setName(const QString &name)
Sets the friendly display name for the item.
Definition: qgslayoutitemmapitem.cpp:89
QgsStyleEntityVisitorInterface
An interface for classes which can visit style entity (e.g.
Definition: qgsstyleentityvisitor.h:34
QgsLayoutItemMap::createExpressionContext
QgsExpressionContext createExpressionContext() const override
This method needs to be reimplemented in all classes which implement this interface and return an exp...
Definition: qgslayoutitemmap.cpp:1632
QgsLayoutItemMapItem::id
QString id() const
Returns the unique id for the map item.
Definition: qgslayoutitemmapitem.h:100
QgsMapLayerRef
_LayerRef< QgsMapLayer > QgsMapLayerRef
Definition: qgsmaplayerref.h:294
FALLTHROUGH
#define FALLTHROUGH
Definition: qgis.h:828
QgsLayoutItemMapItemStack::removeItems
void removeItems()
Clears the item stack and deletes all QgsLayoutItemMapItems contained by the stack.
Definition: qgslayoutitemmapitem.cpp:323
QgsLayoutItemMapItem::StackBelowMap
@ StackBelowMap
Render below all map layers.
Definition: qgslayoutitemmapitem.h:42
_LayerRef::layerId
QString layerId
Original layer ID.
Definition: qgsmaplayerref.h:116
QgsLayoutItemMapItemStack::operator[]
QgsLayoutItemMapItem & operator[](int index)
Returns a reference to an item at the specified index within the stack.
Definition: qgslayoutitemmapitem.cpp:237
QgsLayoutItemMapItem::mEnabled
bool mEnabled
True if item is to be displayed on map.
Definition: qgslayoutitemmapitem.h:213
QgsLayoutItemMapItem::stackingLayer
QgsMapLayer * stackingLayer() const
Returns the item's stacking layer, which specifies where the in the map's stack the item should be re...
Definition: qgslayoutitemmapitem.cpp:114
QgsLayoutItemMapItem::QgsLayoutItemMapItem
QgsLayoutItemMapItem(const QString &name, QgsLayoutItemMap *map)
Constructor for QgsLayoutItemMapItem, attached to the specified map.
Definition: qgslayoutitemmapitem.cpp:23
_LayerRef::setLayer
void setLayer(TYPE *l)
Sets the reference to point to a specified layer.
Definition: qgsmaplayerref.h:77
_LayerRef::provider
QString provider
Weak reference to layer provider.
Definition: qgsmaplayerref.h:123
QgsLayoutItemMapItemStack::hasEnabledItems
bool hasEnabledItems() const
Returns true if the stack has any currently enabled items.
Definition: qgslayoutitemmapitem.cpp:311
QgsLayoutItemMapItem::mMap
QgsLayoutItemMap * mMap
Associated map.
Definition: qgslayoutitemmapitem.h:207
QgsLayoutItemMapItemStack::removeItem
void removeItem(const QString &itemId)
Removes an item which matching itemId from the stack and deletes the corresponding QgsLayoutItemMapIt...
Definition: qgslayoutitemmapitem.cpp:162
QgsLayoutItemMapItemStack::QgsLayoutItemMapItemStack
QgsLayoutItemMapItemStack(QgsLayoutItemMap *map)
Constructor for QgsLayoutItemMapItemStack, attached to the specified map.
Definition: qgslayoutitemmapitem.cpp:146
QgsLayoutItemMapItem::finalizeRestoreFromXml
virtual void finalizeRestoreFromXml()
Called after all pending items have been restored from XML.
Definition: qgslayoutitemmapitem.cpp:75
_LayerRef::name
QString name
Weak reference to layer name.
Definition: qgsmaplayerref.h:121
QgsLayoutItemMapItem::readXml
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...
Definition: qgslayoutitemmapitem.cpp:55
QgsLayoutItemMapItem::mapLayer
virtual QgsMapLayer * mapLayer()
Returns the internal map layer used by this item, if available.
Definition: qgslayoutitemmapitem.cpp:137
QgsLayoutItemMapItem::usesAdvancedEffects
virtual bool usesAdvancedEffects() const
Returns true if the item is drawn using advanced effects, such as blend modes.
Definition: qgslayoutitemmapitem.cpp:109
QgsLayoutItemMapItemStack::containsAdvancedEffects
bool containsAdvancedEffects() const
Returns whether any items within the stack contain advanced effects, such as blending modes.
Definition: qgslayoutitemmapitem.cpp:299
QgsLayoutItemMapItem::StackBelowMapLabels
@ StackBelowMapLabels
Render above all map layers, but below map labels.
Definition: qgslayoutitemmapitem.h:45
QgsLayoutItemMapItem::mUuid
QString mUuid
Unique id.
Definition: qgslayoutitemmapitem.h:210
QgsLayoutItemMapItemStack::drawItems
void drawItems(QPainter *painter, bool ignoreStacking=true)
Draws the items from the stack on a specified painter.
Definition: qgslayoutitemmapitem.cpp:272
qgslayout.h
QgsLayoutItemMapItemStack::asList
QList< QgsLayoutItemMapItem * > asList() const
Returns a list of QgsLayoutItemMapItems contained by the stack.
Definition: qgslayoutitemmapitem.cpp:242
QgsLayoutItemMapItemStack::moveItemDown
void moveItemDown(const QString &itemId)
Moves an item which matching itemId up the stack, causing it to be rendered above other items.
Definition: qgslayoutitemmapitem.cpp:194
QgsLayoutObject::createExpressionContext
QgsExpressionContext createExpressionContext() const override
Creates an expression context relating to the objects' current state.
Definition: qgslayoutobject.cpp:156
QgsLayoutItemMap
Layout graphical items for displaying a map.
Definition: qgslayoutitemmap.h:318
QgsLayoutItemMapItemStack::mItems
QList< QgsLayoutItemMapItem * > mItems
Definition: qgslayoutitemmapitem.h:353
QgsLayoutItemMapItemStack::~QgsLayoutItemMapItemStack
virtual ~QgsLayoutItemMapItemStack()
Definition: qgslayoutitemmapitem.cpp:152
QgsLayoutObject::readObjectPropertiesFromElement
bool readObjectPropertiesFromElement(const QDomElement &parentElement, const QDomDocument &document, const QgsReadWriteContext &context)
Sets object properties from a DOM element.
Definition: qgslayoutobject.cpp:189
QgsLayoutItemMapItem::stackingPosition
StackingPosition stackingPosition() const
Returns the item's stacking position, which specifies where the in the map's stack the item should be...
Definition: qgslayoutitemmapitem.h:140
QgsLayoutItemMapItem::StackAboveMapLabels
@ StackAboveMapLabels
Render above all map layers and labels.
Definition: qgslayoutitemmapitem.h:46
_LayerRef::source
QString source
Weak reference to layer public source.
Definition: qgsmaplayerref.h:119
QgsLayoutItemMapItem::StackingPosition
StackingPosition
Item stacking position, specifies where the in the map's stack the item should be rendered.
Definition: qgslayoutitemmapitem.h:41
QgsMapLayer
Base class for all map layer types.
Definition: qgsmaplayer.h:83
QgsLayoutItemMapItem::writeXml
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...
Definition: qgslayoutitemmapitem.cpp:33
qgslayoutitemmapitem.h
QgsLayoutItemMapItem::mStackingPosition
StackingPosition mStackingPosition
Definition: qgslayoutitemmapitem.h:215
QgsLayoutItemMapItem::mStackingLayer
QgsMapLayerRef mStackingLayer
Definition: qgslayoutitemmapitem.h:217
QgsLayoutItemMapItemStack::moveItemUp
void moveItemUp(const QString &itemId)
Moves an item which matching itemId up the stack, causing it to be rendered above other items.
Definition: qgslayoutitemmapitem.cpp:174
QgsLayoutItemMapItemStack::item
QgsLayoutItemMapItem * item(int index) const
Returns a reference to the item at the specified index within the stack.
Definition: qgslayoutitemmapitem.cpp:227
QgsLayoutItemMapItem::name
QString name() const
Returns the friendly display name for the item.
Definition: qgslayoutitemmapitem.cpp:94
QgsLayout::project
QgsProject * project() const
The project associated with the layout.
Definition: qgslayout.cpp:132
QgsLayoutItemMapItemStack::addItem
void addItem(QgsLayoutItemMapItem *item)
Adds a new map item to the stack and takes ownership of the item.
Definition: qgslayoutitemmapitem.cpp:157
QgsLayoutItemMapItem::setStackingLayer
void setStackingLayer(QgsMapLayer *layer)
Sets the item's stacking layer, which specifies where the in the map's stack the item should be rende...
Definition: qgslayoutitemmapitem.cpp:119
QgsLayoutItemMapItem::accept
virtual bool accept(QgsStyleEntityVisitorInterface *visitor) const
Accepts the specified style entity visitor, causing it to visit all style entities associated with th...
Definition: qgslayoutitemmapitem.cpp:132
QgsLayoutObject::writeObjectPropertiesToElement
bool writeObjectPropertiesToElement(QDomElement &parentElement, QDomDocument &document, const QgsReadWriteContext &context) const
Stores object properties within an XML DOM element.
Definition: qgslayoutobject.cpp:168
QgsLayoutItemMapItem::enabled
bool enabled() const
Returns whether the item will be drawn.
Definition: qgslayoutitemmapitem.cpp:104
QgsLayoutObject
A base class for objects which belong to a layout.
Definition: qgslayoutobject.h:40
_LayerRef::get
TYPE * get() const
Returns a pointer to the layer, or nullptr if the reference has not yet been matched to a layer.
Definition: qgsmaplayerref.h:107
qgslayoutitemmap.h