QGIS API Documentation  2.18.21-Las Palmas (9fba24a)
qgscomposermapitem.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgscomposermapitem.cpp
3  -------------------
4  begin : September 2014
5  copyright : (C) 2014 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 "qgscomposermapitem.h"
19 #include "qgscomposermap.h"
20 #include <QUuid>
21 
23  : QgsComposerObject( map->composition() )
24  , mName( name )
25  , mComposerMap( map )
26  , mUuid( QUuid::createUuid().toString() )
27  , mEnabled( true )
28 {
29 
30 }
31 
33 {
34 
35 }
36 
38 {
39  Q_UNUSED( doc );
40  elem.setAttribute( "uuid", mUuid );
41  elem.setAttribute( "name", mName );
42  elem.setAttribute( "show", mEnabled );
43  return true;
44 }
45 
46 bool QgsComposerMapItem::readXML( const QDomElement &itemElem, const QDomDocument &doc )
47 {
48  Q_UNUSED( doc );
49  mUuid = itemElem.attribute( "uuid" );
50  mName = itemElem.attribute( "name" );
51  mEnabled = ( itemElem.attribute( "show", "0" ) != "0" );
52  return true;
53 }
54 
56 {
57  mComposerMap = map;
58 }
59 
60 //
61 // QgsComposerMapItemStack
62 //
63 
65  : mComposerMap( map )
66 {
67 
68 }
69 
71 {
72  removeItems();
73 }
74 
76 {
77  mItems.append( item );
78 }
79 
81 {
82  for ( int i = mItems.size() - 1; i >= 0; --i )
83  {
84  if ( mItems.at( i )->id() == itemId )
85  {
86  delete mItems.takeAt( i );
87  return;
88  }
89  }
90 }
91 
93 {
94  QgsComposerMapItem* targetItem = item( itemId );
95  if ( !targetItem )
96  {
97  return;
98  }
99 
100  int index = mItems.indexOf( targetItem );
101  if ( index >= mItems.size() - 1 )
102  {
103  return;
104  }
105  mItems.swap( index, index + 1 );
106 }
107 
109 {
110  QgsComposerMapItem* targetItem = item( itemId );
111  if ( !targetItem )
112  {
113  return;
114  }
115 
116  int index = mItems.indexOf( targetItem );
117  if ( index < 1 )
118  {
119  return;
120  }
121  mItems.swap( index, index - 1 );
122 }
123 
125 {
127  for ( ; it != mItems.constEnd(); ++it )
128  {
129  if (( *it )->id() == itemId )
130  {
131  return ( *it );
132  }
133  }
134 
135  return nullptr;
136 }
137 
139 {
141  for ( ; it != mItems.end(); ++it )
142  {
143  if (( *it )->id() == itemId )
144  {
145  return ( *it );
146  }
147  }
148 
149  return nullptr;
150 }
151 
153 {
154  if ( index < mItems.length() )
155  {
156  return mItems.at( index );
157  }
158 
159  return nullptr;
160 }
161 
163 {
164  return *mItems[idx];
165 }
166 
168 {
171  for ( ; it != mItems.end(); ++it )
172  {
173  list.append( *it );
174  }
175  return list;
176 }
177 
179 {
180  //write item stack
182  for ( ; itemIt != mItems.constEnd(); ++itemIt )
183  {
184  ( *itemIt )->writeXML( elem, doc );
185  }
186 
187  return true;
188 }
189 
191 {
192  if ( !painter )
193  {
194  return;
195  }
196 
198  for ( ; itemIt != mItems.constEnd(); ++itemIt )
199  {
200  ( *itemIt )->draw( painter );
201  }
202 }
203 
205 {
207  for ( ; it != mItems.constEnd(); ++it )
208  {
209  if (( *it )->enabled() && ( *it )->usesAdvancedEffects() )
210  {
211  return true;
212  }
213  }
214  return false;
215 }
216 
218 {
219  qDeleteAll( mItems );
220  mItems.clear();
221 }
222 
223 
224 
QgsComposerMapItem * item(const QString &itemId) const
Returns a reference to an item within the stack.
void clear()
static unsigned index
A base class for objects which belong to a map composition.
void addItem(QgsComposerMapItem *item)
Adds a new map item to the stack and takes ownership of the item.
QString attribute(const QString &name, const QString &defValue) const
int length() const
QgsComposerMapItem(const QString &name, QgsComposerMap *map)
Constructor for QgsComposerMapItem.
QString mName
Friendly display name.
const T & at(int i) const
void drawItems(QPainter *painter)
Draws the items from the stack on a specified painter.
T takeAt(int i)
An item which is drawn inside a QgsComposerMap, eg a grid or map overview.
int size() const
int indexOf(const T &value, int from) const
bool containsAdvancedEffects() const
Returns whether any items within the stack contain advanced effects, such as blending modes...
virtual bool writeXML(QDomElement &elem, QDomDocument &doc) const
Stores the state of the item stack in a DOM node.
void append(const T &value)
bool mEnabled
True if item is to be displayed on map.
QgsComposerMapItemStack(QgsComposerMap *map)
Constructor for QgsComposerMapItemStack.
void setAttribute(const QString &name, const QString &value)
void removeItem(const QString &itemId)
Removes an item from the stack and deletes the corresponding QgsComposerMapItem.
const QgsComposerMapItem * constItem(const QString &itemId) const
Returns a const reference to an item within the stack.
void moveItemUp(const QString &itemId)
Moves an item up the stack, causing it to be rendered above other items.
QgsComposerMapItem & operator[](int idx)
Returns a reference to an item within the stack.
QgsComposerMap * mComposerMap
Associated composer map.
Object representing map window.
void moveItemDown(const QString &itemId)
Moves an item up the stack, causing it to be rendered above other items.
iterator end()
QList< QgsComposerMapItem *> asList() const
Returns a list of QgsComposerMapItems contained by the stack.
QList< QgsComposerMapItem *> mItems
void swap(QList< T > &other)
QString mUuid
Unique id.
virtual bool readXML(const QDomElement &itemElem, const QDomDocument &doc) override
Sets map item state from a DOM document.
virtual void setComposerMap(QgsComposerMap *map)
Sets composer map for the item.
const_iterator constEnd() const
const_iterator constBegin() const
void removeItems()
Clears the item stack and deletes all QgsComposerMapItems contained by the stack. ...
virtual bool writeXML(QDomElement &elem, QDomDocument &doc) const override
Stores map item state in DOM element.
iterator begin()