QGIS API Documentation  2.8.2-Wien
 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 "qgscomposerutils.h"
21 #include "qgslogger.h"
22 #include "qgscomposermodel.h"
23 
24 #include <QPen>
25 #include <QPainter>
26 
28  : QgsComposerItem( c )
29 {
30  setZValue( 90 );
31  show();
32 }
33 
35 {
36  //loop through group members and remove them from the scene
37  QSet<QgsComposerItem*>::iterator itemIt = mItems.begin();
38  for ( ; itemIt != mItems.end(); ++itemIt )
39  {
40  if ( *itemIt )
41  {
42  //inform model that we are about to remove an item from the scene
43  mComposition->itemsModel()->setItemRemoved( *itemIt );
44  mComposition->removeItem( *itemIt );
45  ( *itemIt )->setIsGroupMember( false );
46  }
47  }
48 }
49 
51 {
52  if ( !item )
53  {
54  return;
55  }
56 
57  if ( mItems.contains( item ) )
58  {
59  return;
60  }
61 
62  connect( item, SIGNAL( destroyed() ), this, SLOT( itemDestroyed() ) );
63 
64  mItems.insert( item );
65  item->setSelected( false );
66  item->setIsGroupMember( true );
67 
68  //update extent
69  if ( mBoundingRectangle.isEmpty() ) //we add the first item
70  {
71  mBoundingRectangle = QRectF( 0, 0, item->rect().width(), item->rect().height() );
72  //call method of superclass to avoid repositioning of items
73  QgsComposerItem::setSceneRect( QRectF( item->pos().x(), item->pos().y(), item->rect().width(), item->rect().height() ) );
74 
75  if ( item->itemRotation() != 0 )
76  {
77  setItemRotation( item->itemRotation() );
78  }
79  }
80  else
81  {
82  if ( item->itemRotation() != itemRotation() )
83  {
84  //items have mixed rotation, so reset rotation of group
85  mBoundingRectangle = mapRectToScene( mBoundingRectangle );
86  setItemRotation( 0 );
87  mBoundingRectangle = mBoundingRectangle.united( item->mapRectToScene( item->rect() ) );
88  //call method of superclass to avoid repositioning of items
89  QgsComposerItem::setSceneRect( mBoundingRectangle );
90  }
91  else
92  {
93  //items have same rotation, so keep rotation of group
94  mBoundingRectangle = mBoundingRectangle.united( mapRectFromItem( item, item->rect() ) );
95  QPointF newPos = mapToScene( mBoundingRectangle.topLeft().x(), mBoundingRectangle.topLeft().y() );
96  mBoundingRectangle = QRectF( 0, 0, mBoundingRectangle.width(), mBoundingRectangle.height() );
97  QgsComposerItem::setSceneRect( QRectF( newPos.x(), newPos.y(), mBoundingRectangle.width(), mBoundingRectangle.height() ) );
98  }
99  }
100 
101 }
102 
104 {
105  QSet<QgsComposerItem*>::iterator item_it = mItems.begin();
106  for ( ; item_it != mItems.end(); ++item_it )
107  {
108  ( *item_it )->setIsGroupMember( false );
109  ( *item_it )->setSelected( true );
110  }
111  mItems.clear();
112 }
113 
115 {
116  mItems.remove( static_cast<QgsComposerItem*>( sender() ) );
117 }
118 
119 void QgsComposerItemGroup::paint( QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget )
120 {
121  Q_UNUSED( option );
122  Q_UNUSED( widget );
123  drawFrame( painter );
124  if ( isSelected() )
125  {
126  drawSelectionBoxes( painter );
127  }
128 }
129 
130 void QgsComposerItemGroup::setSceneRect( const QRectF& rectangle )
131 {
132  //resize all items in this group
133  //first calculate new group rectangle in current group coordsys
134  QPointF newOrigin = mapFromScene( rectangle.topLeft() );
135  QRectF newRect = QRectF( newOrigin.x(), newOrigin.y(), rectangle.width(), rectangle.height() );
136 
137  QSet<QgsComposerItem*>::iterator item_it = mItems.begin();
138  for ( ; item_it != mItems.end(); ++item_it )
139  {
140  //each item needs to be scaled relatively to the final size of the group
141  QRectF itemRect = mapRectFromItem(( *item_it ), ( *item_it )->rect() );
142  QgsComposerUtils::relativeResizeRect( itemRect, rect(), newRect );
143 
144  QPointF newPos = mapToScene( itemRect.topLeft() );
145  ( *item_it )->setSceneRect( QRectF( newPos.x(), newPos.y(), itemRect.width(), itemRect.height() ) );
146  }
147  //lastly, set new rect for group
148  QgsComposerItem::setSceneRect( rectangle );
149 }
150 
151 void QgsComposerItemGroup::setVisibility( const bool visible )
152 {
153  //also set visibility for all items within the group
154  QSet<QgsComposerItem*>::iterator item_it = mItems.begin();
155  for ( ; item_it != mItems.end(); ++item_it )
156  {
157  ( *item_it )->setVisibility( visible );
158  }
159  //lastly set visibility for group item itself
161 }
162 
164 {
165  if ( !mComposition )
166  {
167  return;
168  }
169 
171  {
172  QPen newPen( pen() );
173  newPen.setStyle( Qt::DashLine );
174  newPen.setColor( QColor( 128, 128, 128, 128 ) );
175  p->setPen( newPen );
176  p->setRenderHint( QPainter::Antialiasing, true );
177  p->drawRect( QRectF( 0, 0, rect().width(), rect().height() ) );
178  }
179 }
180 
181 bool QgsComposerItemGroup::writeXML( QDomElement& elem, QDomDocument & doc ) const
182 {
183  QDomElement group = doc.createElement( "ComposerItemGroup" );
184 
185  QSet<QgsComposerItem*>::const_iterator itemIt = mItems.begin();
186  for ( ; itemIt != mItems.end(); ++itemIt )
187  {
188  QDomElement item = doc.createElement( "ComposerItemGroupElement" );
189  item.setAttribute( "uuid", ( *itemIt )->uuid() );
190  group.appendChild( item );
191  }
192 
193  elem.appendChild( group );
194 
195  return _writeXML( group, doc );
196 }
197 
198 bool QgsComposerItemGroup::readXML( const QDomElement& itemElem, const QDomDocument& doc )
199 {
200  //restore general composer item properties
201  QDomNodeList composerItemList = itemElem.elementsByTagName( "ComposerItem" );
202  if ( composerItemList.size() > 0 )
203  {
204  QDomElement composerItemElem = composerItemList.at( 0 ).toElement();
205  _readXML( composerItemElem, doc );
206  }
207 
208  QList<QGraphicsItem *> items = mComposition->items();
209 
210  QDomNodeList elementNodes = itemElem.elementsByTagName( "ComposerItemGroupElement" );
211  for ( int i = 0; i < elementNodes.count(); ++i )
212  {
213  QDomNode elementNode = elementNodes.at( i );
214  if ( !elementNode.isElement() )
215  continue;
216 
217  QString uuid = elementNode.toElement().attribute( "uuid" );
218 
219  for ( QList<QGraphicsItem *>::iterator it = items.begin(); it != items.end(); ++it )
220  {
221  QgsComposerItem *item = dynamic_cast<QgsComposerItem *>( *it );
222  if ( item && ( item->mUuid == uuid || item->mTemplateUuid == uuid ) )
223  {
224  addItem( item );
225  break;
226  }
227  }
228  }
229 
230  return true;
231 }