QGIS API Documentation  3.10.0-A Coruña (6c816b4204)
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 
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 
137 //
138 // QgsLayoutItemMapItemStack
139 //
140 
142  : mMap( map )
143 {
144 
145 }
146 
148 {
149  removeItems();
150 }
151 
153 {
154  mItems.append( item );
155 }
156 
157 void QgsLayoutItemMapItemStack::removeItem( const QString &itemId )
158 {
159  for ( int i = mItems.size() - 1; i >= 0; --i )
160  {
161  if ( mItems.at( i )->id() == itemId )
162  {
163  delete mItems.takeAt( i );
164  return;
165  }
166  }
167 }
168 
169 void QgsLayoutItemMapItemStack::moveItemUp( const QString &itemId )
170 {
171  QgsLayoutItemMapItem *targetItem = item( itemId );
172  if ( !targetItem )
173  {
174  return;
175  }
176 
177  int index = mItems.indexOf( targetItem );
178  if ( index >= mItems.size() - 1 )
179  {
180  return;
181  }
182  mItems.swap( index, index + 1 );
183 }
184 
185 void QgsLayoutItemMapItemStack::moveItemDown( const QString &itemId )
186 {
187  QgsLayoutItemMapItem *targetItem = item( itemId );
188  if ( !targetItem )
189  {
190  return;
191  }
192 
193  int index = mItems.indexOf( targetItem );
194  if ( index < 1 )
195  {
196  return;
197  }
198  mItems.swap( index, index - 1 );
199 }
200 
202 {
203  for ( QgsLayoutItemMapItem *item : mItems )
204  {
205  if ( item->id() == itemId )
206  {
207  return item;
208  }
209  }
210 
211  return nullptr;
212 }
213 
215 {
216  if ( index < mItems.length() )
217  {
218  return mItems.at( index );
219  }
220 
221  return nullptr;
222 }
223 
225 {
226  return *mItems[idx];
227 }
228 
229 QList<QgsLayoutItemMapItem *> QgsLayoutItemMapItemStack::asList() const
230 {
231  QList< QgsLayoutItemMapItem * > list;
232  list.reserve( mItems.size() );
233  for ( QgsLayoutItemMapItem *item : mItems )
234  {
235  list.append( item );
236  }
237  return list;
238 }
239 
240 bool QgsLayoutItemMapItemStack::writeXml( QDomElement &elem, QDomDocument &doc, const QgsReadWriteContext &context ) const
241 {
242  //write item stack
243  for ( QgsLayoutItemMapItem *item : mItems )
244  {
245  item->writeXml( elem, doc, context );
246  }
247 
248  return true;
249 }
250 
252 {
253  for ( QgsLayoutItemMapItem *item : qgis::as_const( mItems ) )
254  {
256  }
257 }
258 
259 void QgsLayoutItemMapItemStack::drawItems( QPainter *painter, bool ignoreStacking )
260 {
261  if ( !painter )
262  {
263  return;
264  }
265 
266  for ( QgsLayoutItemMapItem *item : qgis::as_const( mItems ) )
267  {
268  switch ( item->stackingPosition() )
269  {
274  if ( !ignoreStacking )
275  break;
276 
279  item->draw( painter );
280  break;
281 
282  }
283  }
284 }
285 
287 {
288  for ( QgsLayoutItemMapItem *item : mItems )
289  {
290  if ( item->enabled() && item->usesAdvancedEffects() )
291  {
292  return true;
293  }
294  }
295  return false;
296 }
297 
299 {
300  for ( QgsLayoutItemMapItem *item : mItems )
301  {
302  if ( item->enabled() )
303  {
304  return true;
305  }
306  }
307  return false;
308 }
309 
311 {
312  qDeleteAll( mItems );
313  mItems.clear();
314 }
315 
316 
317 
The class is used as a container of context for various read/write operations on other objects...
Base class for all map layer types.
Definition: qgsmaplayer.h:79
QString mName
Friendly display name.
QgsExpressionContext createExpressionContext() const override
Creates an expression context relating to the objects&#39; current state.
_LayerRef< QgsMapLayer > QgsMapLayerRef
virtual void finalizeRestoreFromXml()
Called after all pending items have been restored from XML.
QgsLayoutItemMapItem(const QString &name, QgsLayoutItemMap *map)
Constructor for QgsLayoutItemMapItem, attached to the specified map.
StackingPosition
Item stacking position, specifies where the in the map&#39;s stack the item should be rendered...
bool readObjectPropertiesFromElement(const QDomElement &parentElement, const QDomDocument &document, const QgsReadWriteContext &context)
Sets object properties from a DOM element.
An item which is drawn inside a QgsLayoutItemMap, e.g., a grid or map overview.
virtual void finalizeRestoreFromXml()
Called after all pending items have been restored from XML.
TYPE * resolveWeakly(const QgsProject *project)
Resolves the map layer by attempting to find a matching layer in a project using a weak match...
Render above a specific map layer (see stackingLayer())
An interface for classes which can visit style entity (e.g.
Render below a specific map layer (see stackingLayer())
void removeItems()
Clears the item stack and deletes all QgsLayoutItemMapItems contained by the stack.
Render above all map layers, but below map labels.
QgsLayoutItemMap * mMap
Associated map.
QgsLayoutItemMapItemStack(QgsLayoutItemMap *map)
Constructor for QgsLayoutItemMapItemStack, attached to the specified map.
virtual bool usesAdvancedEffects() const
Returns true if the item is drawn using advanced effects, such as blend modes.
QgsExpressionContext createExpressionContext() const override
This method needs to be reimplemented in all classes which implement this interface and return an exp...
bool mEnabled
True if item is to be displayed on map.
Layout graphical items for displaying a map.
QString provider
Weak reference to layer provider.
QString layerId
Original layer ID.
const QgsLayout * layout() const
Returns the layout the object is attached to.
#define FALLTHROUGH
Definition: qgis.h:681
void setName(const QString &name)
Sets the friendly display name for the item.
StackingPosition mStackingPosition
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
void addItem(QgsLayoutItemMapItem *item)
Adds a new map item to the stack and takes ownership of the item.
QString name
Weak reference to layer name.
StackingPosition stackingPosition() const
Returns the item&#39;s stacking position, which specifies where the in the map&#39;s stack the item should be...
QgsExpressionContext createExpressionContext() const override
This method needs to be reimplemented in all classes which implement this interface and return an exp...
bool enabled() const
Returns whether the item will be drawn.
QgsMapLayer * stackingLayer() const
Returns the item&#39;s stacking layer, which specifies where the in the map&#39;s stack the item should be re...
virtual void draw(QPainter *painter)=0
Draws the item on to a destination painter.
void setLayer(TYPE *l)
Sets the reference to point to a specified layer.
QString source
Weak reference to layer public source.
void moveItemUp(const QString &itemId)
Moves an item which matching itemId up the stack, causing it to be rendered above other items...
void setStackingLayer(QgsMapLayer *layer)
Sets the item&#39;s stacking layer, which specifies where the in the map&#39;s stack the item should be rende...
QgsLayoutItemMapItem * item(int index) const
Returns a reference to the item at the specified index within the stack.
QgsLayoutItemMapItem & operator[](int index)
Returns a reference to an item at the specified index within the stack.
const QgsLayoutItemMap * map() const
Returns the layout item map for the item.
bool containsAdvancedEffects() const
Returns whether any items within the stack contain advanced effects, such as blending modes...
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 &#39;Layout...
bool hasEnabledItems() const
Returns true if the stack has any currently enabled items.
QList< QgsLayoutItemMapItem *> asList() const
Returns a list of QgsLayoutItemMapItems contained by the stack.
bool writeObjectPropertiesToElement(QDomElement &parentElement, QDomDocument &document, const QgsReadWriteContext &context) const
Stores object properties within an XML DOM element.
virtual bool accept(QgsStyleEntityVisitorInterface *visitor) const
Accepts the specified style entity visitor, causing it to visit all style entities associated with th...
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 setMap(QgsLayoutItemMap *map)
Sets the corresponding layout map for the item.
QgsProject * project() const
The project associated with the layout.
Definition: qgslayout.cpp:131
QString id() const
Returns the unique id for the map item.
A base class for objects which belong to a layout.
void drawItems(QPainter *painter, bool ignoreStacking=true)
Draws the items from the stack on a specified painter.
void removeItem(const QString &itemId)
Removes an item which matching itemId from the stack and deletes the corresponding QgsLayoutItemMapIt...
void moveItemDown(const QString &itemId)
Moves an item which matching itemId up the stack, causing it to be rendered above other items...
QList< QgsLayoutItemMapItem *> mItems
virtual void setEnabled(bool enabled)
Controls whether the item will be drawn.
TYPE * get() const
Returns a pointer to the layer, or nullptr if the reference has not yet been matched to a layer...
QString mUuid
Unique id.
QString name() const
Returns the friendly display name for the item.
Render above all map layers and labels.
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 &#39;LayoutMa...
Render below all map layers.