QGIS API Documentation  2.2.0-Valmiera
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
qgscomposeritemgroup.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgscomposeritemgroup.cpp
3  ------------------------
4  begin : 2nd June 2008
5  copyright : (C) 2008 by Marco Hugentobler
6  email : marco dot hugentobler at karto dot baug dot ethz dot ch
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 "qgscomposeritemgroup.h"
19 #include "qgscomposition.h"
20 #include "qgslogger.h"
21 
22 #include <QPen>
23 #include <QPainter>
24 
26  : QgsComposerItem( c )
27 {
28  setZValue( 90 );
29  show();
30 }
31 
33 {
34  QSet<QgsComposerItem*>::iterator itemIt = mItems.begin();
35  for ( ; itemIt != mItems.end(); ++itemIt )
36  {
37  if ( *itemIt )
38  {
39  mComposition->removeItem( *itemIt );
40  ( *itemIt )->setFlag( QGraphicsItem::ItemIsSelectable, true );
41  }
42  }
43 }
44 
46 {
47  if ( !item )
48  {
49  return;
50  }
51 
52  if ( mItems.contains( item ) )
53  {
54  return;
55  }
56 
57  connect( item, SIGNAL( destroyed() ), this, SLOT( itemDestroyed() ) );
58 
59  mItems.insert( item );
60  item->setSelected( false );
61  item->setFlag( QGraphicsItem::ItemIsSelectable, false ); //item in groups cannot be selected
62 
63  //update extent
64  if ( mBoundingRectangle.isEmpty() ) //we add the first item
65  {
66  mBoundingRectangle = QRectF( 0, 0, item->rect().width(), item->rect().height() );
67  //call method of superclass to avoid repositioning of items
68  QgsComposerItem::setSceneRect( QRectF( item->pos().x(), item->pos().y(), item->rect().width(), item->rect().height() ) );
69 
70  if ( item->itemRotation() != 0 )
71  {
72  setItemRotation( item->itemRotation() );
73  }
74  }
75  else
76  {
77  if ( item->itemRotation() != itemRotation() )
78  {
79  //items have mixed rotation, so reset rotation of group
80  mBoundingRectangle = mapRectToScene( mBoundingRectangle );
81  setItemRotation( 0 );
82  mBoundingRectangle = mBoundingRectangle.united( item->mapRectToScene( item->rect() ) );
83  //call method of superclass to avoid repositioning of items
85  }
86  else
87  {
88  //items have same rotation, so keep rotation of group
89  mBoundingRectangle = mBoundingRectangle.united( mapRectFromItem( item, item->rect() ) );
90  QPointF newPos = mapToScene( mBoundingRectangle.topLeft().x(), mBoundingRectangle.topLeft().y() );
91  mBoundingRectangle = QRectF( 0, 0, mBoundingRectangle.width(), mBoundingRectangle.height() );
92  QgsComposerItem::setSceneRect( QRectF( newPos.x(), newPos.y(), mBoundingRectangle.width(), mBoundingRectangle.height() ) );
93  }
94  }
95 
96 }
97 
99 {
100  QSet<QgsComposerItem*>::iterator item_it = mItems.begin();
101  for ( ; item_it != mItems.end(); ++item_it )
102  {
103  ( *item_it )->setFlag( QGraphicsItem::ItemIsSelectable, true ); //enable item selection again
104  ( *item_it )->setSelected( true );
105  }
106  mItems.clear();
107 }
108 
110 {
111  mItems.remove( static_cast<QgsComposerItem*>( sender() ) );
112 }
113 
114 void QgsComposerItemGroup::paint( QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget )
115 {
116  Q_UNUSED( option );
117  Q_UNUSED( widget );
118  drawFrame( painter );
119  if ( isSelected() )
120  {
121  drawSelectionBoxes( painter );
122  }
123 }
124 
125 void QgsComposerItemGroup::setSceneRect( const QRectF& rectangle )
126 {
127  //resize all items in this group
128  //first calculate new group rectangle in current group coordsys
129  QPointF newOrigin = mapFromScene( rectangle.topLeft() );
130  QRectF newRect = QRectF( newOrigin.x(), newOrigin.y(), rectangle.width(), rectangle.height() );
131 
132  QSet<QgsComposerItem*>::iterator item_it = mItems.begin();
133  for ( ; item_it != mItems.end(); ++item_it )
134  {
135  //each item needs to be scaled relatively to the final size of the group
136  QRectF itemRect = mapRectFromItem(( *item_it ), ( *item_it )->rect() );
137  QgsComposition::relativeResizeRect( itemRect, rect(), newRect );
138 
139  QPointF newPos = mapToScene( itemRect.topLeft() );
140  ( *item_it )->setSceneRect( QRectF( newPos.x(), newPos.y(), itemRect.width(), itemRect.height() ) );
141  }
142  //lastly, set new rect for group
143  QgsComposerItem::setSceneRect( rectangle );
144 }
145 
147 {
148  if ( !mComposition )
149  {
150  return;
151  }
152 
154  {
155  QPen newPen( pen() );
156  newPen.setStyle( Qt::DashLine );
157  newPen.setColor( QColor( 128, 128, 128, 128 ) );
158  p->setPen( newPen );
159  p->setRenderHint( QPainter::Antialiasing, true );
160  p->drawRect( QRectF( 0, 0, rect().width(), rect().height() ) );
161  }
162 }
163 
164 bool QgsComposerItemGroup::writeXML( QDomElement& elem, QDomDocument & doc ) const
165 {
166  QDomElement group = doc.createElement( "ComposerItemGroup" );
167 
168  QSet<QgsComposerItem*>::const_iterator itemIt = mItems.begin();
169  for ( ; itemIt != mItems.end(); ++itemIt )
170  {
171  QDomElement item = doc.createElement( "ComposerItemGroupElement" );
172  item.setAttribute( "uuid", ( *itemIt )->uuid() );
173  group.appendChild( item );
174  }
175 
176  elem.appendChild( group );
177 
178  return _writeXML( group, doc );
179 }
180 
181 bool QgsComposerItemGroup::readXML( const QDomElement& itemElem, const QDomDocument& doc )
182 {
183  //restore general composer item properties
184  QDomNodeList composerItemList = itemElem.elementsByTagName( "ComposerItem" );
185  if ( composerItemList.size() > 0 )
186  {
187  QDomElement composerItemElem = composerItemList.at( 0 ).toElement();
188  _readXML( composerItemElem, doc );
189  }
190 
191  QList<QGraphicsItem *> items = mComposition->items();
192 
193  QDomNodeList elementNodes = itemElem.elementsByTagName( "ComposerItemGroupElement" );
194  for ( int i = 0; i < elementNodes.count(); ++i )
195  {
196  QDomNode elementNode = elementNodes.at( i );
197  if ( !elementNode.isElement() )
198  continue;
199 
200  QString uuid = elementNode.toElement().attribute( "uuid" );
201 
202  for ( QList<QGraphicsItem *>::iterator it = items.begin(); it != items.end(); ++it )
203  {
204  QgsComposerItem *item = dynamic_cast<QgsComposerItem *>( *it );
205  if ( item && ( item->mUuid == uuid || item->mTemplateUuid == uuid ) )
206  {
207  addItem( item );
208  break;
209  }
210  }
211  }
212 
213  return true;
214 }