QGIS API Documentation  2.8.6-Wien
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 
37 bool QgsComposerMapItem::writeXML( QDomElement &elem, QDomDocument &doc ) const
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 
80 void QgsComposerMapItemStack::removeItem( const QString &itemId )
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 
92 void QgsComposerMapItemStack::moveItemUp( const QString &itemId )
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 
108 void QgsComposerMapItemStack::moveItemDown( const QString &itemId )
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 
124 const QgsComposerMapItem *QgsComposerMapItemStack::constItem( const QString &itemId ) const
125 {
126  QList< QgsComposerMapItem* >::const_iterator it = mItems.constBegin();
127  for ( ; it != mItems.constEnd(); ++it )
128  {
129  if (( *it )->id() == itemId )
130  {
131  return ( *it );
132  }
133  }
134 
135  return 0;
136 }
137 
138 QgsComposerMapItem *QgsComposerMapItemStack::item( const QString &itemId ) const
139 {
140  QList< QgsComposerMapItem* >::const_iterator it = mItems.begin();
141  for ( ; it != mItems.end(); ++it )
142  {
143  if (( *it )->id() == itemId )
144  {
145  return ( *it );
146  }
147  }
148 
149  return 0;
150 }
151 
153 {
154  if ( index < mItems.length() )
155  {
156  return mItems.at( index );
157  }
158 
159  return 0;
160 }
161 
163 {
164  return *mItems[idx];
165 }
166 
167 QList<QgsComposerMapItem *> QgsComposerMapItemStack::asList() const
168 {
169  QList< QgsComposerMapItem* > list;
170  QList< QgsComposerMapItem* >::const_iterator it = mItems.begin();
171  for ( ; it != mItems.end(); ++it )
172  {
173  list.append( *it );
174  }
175  return list;
176 }
177 
178 bool QgsComposerMapItemStack::writeXML( QDomElement &elem, QDomDocument &doc ) const
179 {
180  //write item stack
181  QList< QgsComposerMapItem* >::const_iterator itemIt = mItems.constBegin();
182  for ( ; itemIt != mItems.constEnd(); ++itemIt )
183  {
184  ( *itemIt )->writeXML( elem, doc );
185  }
186 
187  return true;
188 }
189 
190 void QgsComposerMapItemStack::drawItems( QPainter *painter )
191 {
192  if ( !painter )
193  {
194  return;
195  }
196 
197  QList< QgsComposerMapItem* >::const_iterator itemIt = mItems.constBegin();
198  for ( ; itemIt != mItems.constEnd(); ++itemIt )
199  {
200  ( *itemIt )->draw( painter );
201  }
202 }
203 
205 {
206  QList< QgsComposerMapItem* >::const_iterator it = mItems.constBegin();
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 
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.
bool containsAdvancedEffects() const
Returns whether any items within the stack contain advanced effects, such as blending modes...
QgsComposerMapItem(const QString &name, QgsComposerMap *map)
Constructor for QgsComposerMapItem.
QString mName
Friendly display name.
void drawItems(QPainter *painter)
Draws the items from the stack on a specified painter.
An item which is drawn inside a QgsComposerMap, eg a grid or map overview.
const QgsComposerMapItem * constItem(const QString &itemId) const
Returns a const reference to an item within the stack.
QList< QgsComposerMapItem * > asList() const
Returns a list of QgsComposerMapItems contained by the stack.
bool mEnabled
True if item is to be displayed on map.
QgsComposerMapItemStack(QgsComposerMap *map)
Constructor for QgsComposerMapItemStack.
void removeItem(const QString &itemId)
Removes an item from the stack and deletes the corresponding QgsComposerMapItem.
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.
QgsComposerMapItem * item(const QString &itemId) const
Returns a reference to an item within the stack.
Object representing map window.
void moveItemDown(const QString &itemId)
Moves an item up the stack, causing it to be rendered above other items.
QList< QgsComposerMapItem * > mItems
virtual bool writeXML(QDomElement &elem, QDomDocument &doc) const
Stores the state of the item stack in a DOM node.
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.
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.