QGIS API Documentation  3.4.15-Madeira (e83d02e274)
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 <QUuid>
21 
23  : QgsLayoutObject( map->layout() )
24  , mName( name )
25  , mMap( map )
26  , mUuid( QUuid::createUuid().toString() )
27  , mEnabled( true )
28 {
29 
30 }
31 
32 bool QgsLayoutItemMapItem::writeXml( QDomElement &element, QDomDocument &document, const QgsReadWriteContext & ) const
33 {
34  Q_UNUSED( document );
35  element.setAttribute( QStringLiteral( "uuid" ), mUuid );
36  element.setAttribute( QStringLiteral( "name" ), mName );
37  element.setAttribute( QStringLiteral( "show" ), mEnabled );
38  return true;
39 }
40 
41 bool QgsLayoutItemMapItem::readXml( const QDomElement &itemElem, const QDomDocument &doc, const QgsReadWriteContext & )
42 {
43  Q_UNUSED( doc );
44  mUuid = itemElem.attribute( QStringLiteral( "uuid" ) );
45  mName = itemElem.attribute( QStringLiteral( "name" ) );
46  mEnabled = ( itemElem.attribute( QStringLiteral( "show" ), QStringLiteral( "0" ) ) != QLatin1String( "0" ) );
47  return true;
48 }
49 
51 {
52 }
53 
55 {
56  mMap = map;
57 }
58 
60 {
61  return mMap;
62 }
63 
64 void QgsLayoutItemMapItem::setName( const QString &name )
65 {
66  mName = name;
67 }
68 
70 {
71  return mName;
72 }
73 
75 {
76  mEnabled = enabled;
77 }
78 
80 {
81  return mEnabled;
82 }
83 
85 {
86  return false;
87 }
88 
89 //
90 // QgsLayoutItemMapItemStack
91 //
92 
94  : mMap( map )
95 {
96 
97 }
98 
100 {
101  removeItems();
102 }
103 
105 {
106  mItems.append( item );
107 }
108 
109 void QgsLayoutItemMapItemStack::removeItem( const QString &itemId )
110 {
111  for ( int i = mItems.size() - 1; i >= 0; --i )
112  {
113  if ( mItems.at( i )->id() == itemId )
114  {
115  delete mItems.takeAt( i );
116  return;
117  }
118  }
119 }
120 
121 void QgsLayoutItemMapItemStack::moveItemUp( const QString &itemId )
122 {
123  QgsLayoutItemMapItem *targetItem = item( itemId );
124  if ( !targetItem )
125  {
126  return;
127  }
128 
129  int index = mItems.indexOf( targetItem );
130  if ( index >= mItems.size() - 1 )
131  {
132  return;
133  }
134  mItems.swap( index, index + 1 );
135 }
136 
137 void QgsLayoutItemMapItemStack::moveItemDown( const QString &itemId )
138 {
139  QgsLayoutItemMapItem *targetItem = item( itemId );
140  if ( !targetItem )
141  {
142  return;
143  }
144 
145  int index = mItems.indexOf( targetItem );
146  if ( index < 1 )
147  {
148  return;
149  }
150  mItems.swap( index, index - 1 );
151 }
152 
154 {
155  for ( QgsLayoutItemMapItem *item : mItems )
156  {
157  if ( item->id() == itemId )
158  {
159  return item;
160  }
161  }
162 
163  return nullptr;
164 }
165 
167 {
168  if ( index < mItems.length() )
169  {
170  return mItems.at( index );
171  }
172 
173  return nullptr;
174 }
175 
177 {
178  return *mItems[idx];
179 }
180 
181 QList<QgsLayoutItemMapItem *> QgsLayoutItemMapItemStack::asList() const
182 {
183  QList< QgsLayoutItemMapItem * > list;
184  list.reserve( mItems.size() );
185  for ( QgsLayoutItemMapItem *item : mItems )
186  {
187  list.append( item );
188  }
189  return list;
190 }
191 
192 bool QgsLayoutItemMapItemStack::writeXml( QDomElement &elem, QDomDocument &doc, const QgsReadWriteContext &context ) const
193 {
194  //write item stack
195  for ( QgsLayoutItemMapItem *item : mItems )
196  {
197  item->writeXml( elem, doc, context );
198  }
199 
200  return true;
201 }
202 
204 {
205  for ( QgsLayoutItemMapItem *item : qgis::as_const( mItems ) )
206  {
208  }
209 }
210 
211 void QgsLayoutItemMapItemStack::drawItems( QPainter *painter )
212 {
213  if ( !painter )
214  {
215  return;
216  }
217 
218  for ( QgsLayoutItemMapItem *item : qgis::as_const( mItems ) )
219  {
220  item->draw( painter );
221  }
222 }
223 
225 {
226  for ( QgsLayoutItemMapItem *item : mItems )
227  {
228  if ( item->enabled() && item->usesAdvancedEffects() )
229  {
230  return true;
231  }
232  }
233  return false;
234 }
235 
237 {
238  qDeleteAll( mItems );
239  mItems.clear();
240 }
241 
242 
243 
The class is used as a container of context for various read/write operations on other objects...
QString mName
Friendly display name.
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.
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...
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.
const QgsLayoutItemMap * map() const
Returns the layout item map for the item.
QList< QgsLayoutItemMapItem * > asList() const
Returns a list of QgsLayoutItemMapItems contained by the stack.
bool containsAdvancedEffects() const
Returns whether any items within the stack contain advanced effects, such as blending modes...
virtual bool usesAdvancedEffects() const
Returns true if the item is drawn using advanced effects, such as blend modes.
void removeItems()
Clears the item stack and deletes all QgsLayoutItemMapItems contained by the stack.
QgsLayoutItemMap * mMap
Associated map.
QgsLayoutItemMapItemStack(QgsLayoutItemMap *map)
Constructor for QgsLayoutItemMapItemStack, attached to the specified map.
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...
bool mEnabled
True if item is to be displayed on map.
Layout graphical items for displaying a map.
QString id() const
Returns the unique id for the map item.
void setName(const QString &name)
Sets the friendly display name for the item.
void addItem(QgsLayoutItemMapItem *item)
Adds a new map item to the stack and takes ownership of the item.
virtual void draw(QPainter *painter)=0
Draws the item on to a destination painter.
void moveItemUp(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.
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...
void setMap(QgsLayoutItemMap *map)
Sets the corresponding layout map for the item.
QString name() const
Returns the friendly display name for the item.
A base class for objects which belong to a layout.
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...
void drawItems(QPainter *painter)
Draws the items from the stack on a specified painter.
QList< QgsLayoutItemMapItem * > mItems
void setEnabled(bool enabled)
Controls whether the item will be drawn.
QString mUuid
Unique id.
bool enabled() const
Returns whether the item will be drawn.