QGIS API Documentation  3.0.2-Girona (307d082)
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  for ( QgsLayoutItemMapItem *item : mItems )
185  {
186  list.append( item );
187  }
188  return list;
189 }
190 
191 bool QgsLayoutItemMapItemStack::writeXml( QDomElement &elem, QDomDocument &doc, const QgsReadWriteContext &context ) const
192 {
193  //write item stack
194  for ( QgsLayoutItemMapItem *item : mItems )
195  {
196  item->writeXml( elem, doc, context );
197  }
198 
199  return true;
200 }
201 
203 {
204  for ( QgsLayoutItemMapItem *item : qgis::as_const( mItems ) )
205  {
207  }
208 }
209 
210 void QgsLayoutItemMapItemStack::drawItems( QPainter *painter )
211 {
212  if ( !painter )
213  {
214  return;
215  }
216 
217  for ( QgsLayoutItemMapItem *item : qgis::as_const( mItems ) )
218  {
219  item->draw( painter );
220  }
221 }
222 
224 {
225  for ( QgsLayoutItemMapItem *item : mItems )
226  {
227  if ( item->enabled() && item->usesAdvancedEffects() )
228  {
229  return true;
230  }
231  }
232  return false;
233 }
234 
236 {
237  qDeleteAll( mItems );
238  mItems.clear();
239 }
240 
241 
242 
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.
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.
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 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.
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.
bool enabled() const
Returns whether the item will be drawn.
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.
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.
QString id() const
Returns the unique id for the map 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.
QString name() const
Returns the friendly display name for the item.
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...