QGIS API Documentation  3.8.0-Zanzibar (11aff65)
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 & ) const
34 {
35  Q_UNUSED( document )
36  element.setAttribute( QStringLiteral( "uuid" ), mUuid );
37  element.setAttribute( QStringLiteral( "name" ), mName );
38  element.setAttribute( QStringLiteral( "show" ), mEnabled );
39  element.setAttribute( QStringLiteral( "position" ), static_cast< int >( mStackingPosition ) );
40 
41  if ( mStackingLayer )
42  {
43  element.setAttribute( QStringLiteral( "stackingLayer" ), mStackingLayer.layerId );
44  element.setAttribute( QStringLiteral( "stackingLayerName" ), mStackingLayer.name );
45  element.setAttribute( QStringLiteral( "stackingLayerSource" ), mStackingLayer.source );
46  element.setAttribute( QStringLiteral( "stackingLayerProvider" ), mStackingLayer.provider );
47  }
48 
49  return true;
50 }
51 
52 bool QgsLayoutItemMapItem::readXml( const QDomElement &itemElem, const QDomDocument &doc, const QgsReadWriteContext & )
53 {
54  Q_UNUSED( doc )
55  mUuid = itemElem.attribute( QStringLiteral( "uuid" ) );
56  mName = itemElem.attribute( QStringLiteral( "name" ) );
57  mEnabled = ( itemElem.attribute( QStringLiteral( "show" ), QStringLiteral( "0" ) ) != QLatin1String( "0" ) );
58  mStackingPosition = static_cast< StackingPosition >( itemElem.attribute( QStringLiteral( "position" ), QString::number( QgsLayoutItemMapItem::StackBelowMapLabels ) ).toInt() );
59 
60  const QString layerId = itemElem.attribute( QStringLiteral( "stackingLayer" ) );
61  const QString layerName = itemElem.attribute( QStringLiteral( "stackingLayerName" ) );
62  const QString layerSource = itemElem.attribute( QStringLiteral( "stackingLayerSource" ) );
63  const QString layerProvider = itemElem.attribute( QStringLiteral( "stackingLayerProvider" ) );
64  mStackingLayer = QgsMapLayerRef( layerId, layerName, layerSource, layerProvider );
65  if ( mMap && mMap->layout() )
67 
68  return true;
69 }
70 
72 {
73 }
74 
76 {
77  mMap = map;
78 }
79 
81 {
82  return mMap;
83 }
84 
85 void QgsLayoutItemMapItem::setName( const QString &name )
86 {
87  mName = name;
88 }
89 
91 {
92  return mName;
93 }
94 
96 {
97  mEnabled = enabled;
98 }
99 
101 {
102  return mEnabled;
103 }
104 
106 {
107  return false;
108 }
109 
111 {
112  return mStackingLayer.get();
113 }
114 
116 {
117  mStackingLayer.setLayer( layer );
118 }
119 
120 //
121 // QgsLayoutItemMapItemStack
122 //
123 
125  : mMap( map )
126 {
127 
128 }
129 
131 {
132  removeItems();
133 }
134 
136 {
137  mItems.append( item );
138 }
139 
140 void QgsLayoutItemMapItemStack::removeItem( const QString &itemId )
141 {
142  for ( int i = mItems.size() - 1; i >= 0; --i )
143  {
144  if ( mItems.at( i )->id() == itemId )
145  {
146  delete mItems.takeAt( i );
147  return;
148  }
149  }
150 }
151 
152 void QgsLayoutItemMapItemStack::moveItemUp( const QString &itemId )
153 {
154  QgsLayoutItemMapItem *targetItem = item( itemId );
155  if ( !targetItem )
156  {
157  return;
158  }
159 
160  int index = mItems.indexOf( targetItem );
161  if ( index >= mItems.size() - 1 )
162  {
163  return;
164  }
165  mItems.swap( index, index + 1 );
166 }
167 
168 void QgsLayoutItemMapItemStack::moveItemDown( const QString &itemId )
169 {
170  QgsLayoutItemMapItem *targetItem = item( itemId );
171  if ( !targetItem )
172  {
173  return;
174  }
175 
176  int index = mItems.indexOf( targetItem );
177  if ( index < 1 )
178  {
179  return;
180  }
181  mItems.swap( index, index - 1 );
182 }
183 
185 {
186  for ( QgsLayoutItemMapItem *item : mItems )
187  {
188  if ( item->id() == itemId )
189  {
190  return item;
191  }
192  }
193 
194  return nullptr;
195 }
196 
198 {
199  if ( index < mItems.length() )
200  {
201  return mItems.at( index );
202  }
203 
204  return nullptr;
205 }
206 
208 {
209  return *mItems[idx];
210 }
211 
212 QList<QgsLayoutItemMapItem *> QgsLayoutItemMapItemStack::asList() const
213 {
214  QList< QgsLayoutItemMapItem * > list;
215  list.reserve( mItems.size() );
216  for ( QgsLayoutItemMapItem *item : mItems )
217  {
218  list.append( item );
219  }
220  return list;
221 }
222 
223 bool QgsLayoutItemMapItemStack::writeXml( QDomElement &elem, QDomDocument &doc, const QgsReadWriteContext &context ) const
224 {
225  //write item stack
226  for ( QgsLayoutItemMapItem *item : mItems )
227  {
228  item->writeXml( elem, doc, context );
229  }
230 
231  return true;
232 }
233 
235 {
236  for ( QgsLayoutItemMapItem *item : qgis::as_const( mItems ) )
237  {
239  }
240 }
241 
242 void QgsLayoutItemMapItemStack::drawItems( QPainter *painter, bool ignoreStacking )
243 {
244  if ( !painter )
245  {
246  return;
247  }
248 
249  for ( QgsLayoutItemMapItem *item : qgis::as_const( mItems ) )
250  {
251  switch ( item->stackingPosition() )
252  {
257  if ( !ignoreStacking )
258  break;
259 
262  item->draw( painter );
263  break;
264 
265  }
266  }
267 }
268 
270 {
271  for ( QgsLayoutItemMapItem *item : mItems )
272  {
273  if ( item->enabled() && item->usesAdvancedEffects() )
274  {
275  return true;
276  }
277  }
278  return false;
279 }
280 
282 {
283  qDeleteAll( mItems );
284  mItems.clear();
285 }
286 
287 
288 
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:78
QString mName
Friendly display name.
_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...
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())
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.
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:656
void setName(const QString &name)
Sets the friendly display name for the item.
StackingPosition mStackingPosition
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...
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 & 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...
QList< QgsLayoutItemMapItem *> asList() const
Returns a list of QgsLayoutItemMapItems contained by the stack.
QgsLayoutItemMapItem * item(const QString &itemId) const
Returns a reference to an item which matching itemId within the stack.
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:130
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
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.