Quantum GIS API Documentation  1.7.5-Wroclaw
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator 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 <QPen>
21 #include <QPainter>
22 
24 {
25  setZValue( 90 );
26  show();
27 }
28 
30 {
31  QSet<QgsComposerItem*>::iterator itemIt = mItems.begin();
32  for ( ; itemIt != mItems.end(); ++itemIt )
33  {
34  if ( *itemIt )
35  {
36  mComposition->removeItem( *itemIt );
37  ( *itemIt )->setFlag( QGraphicsItem::ItemIsSelectable, true );
38  }
39  }
40 }
41 
43 {
44  if ( !item )
45  {
46  return;
47  }
48 
49  if ( mItems.contains( item ) )
50  {
51  return;
52  }
53  mItems.insert( item );
54  item->setSelected( false );
55  item->setFlag( QGraphicsItem::ItemIsSelectable, false ); //item in groups cannot be selected
56 
57  //update extent (which is in scene coordinates)
58  double minXItem = item->transform().dx();
59  double minYItem = item->transform().dy();
60  double maxXItem = minXItem + item->rect().width();
61  double maxYItem = minYItem + item->rect().height();
62 
63  if ( mSceneBoundingRectangle.isEmpty() ) //we add the first item
64  {
65  mSceneBoundingRectangle.setLeft( minXItem );
66  mSceneBoundingRectangle.setTop( minYItem );
67  mSceneBoundingRectangle.setRight( maxXItem );
68  mSceneBoundingRectangle.setBottom( maxYItem );
69  }
70 
71  else
72  {
73  if ( minXItem < mSceneBoundingRectangle.left() )
74  {
75  mSceneBoundingRectangle.setLeft( minXItem );
76  }
77  if ( minYItem < mSceneBoundingRectangle.top() )
78  {
79  mSceneBoundingRectangle.setTop( minYItem );
80  }
81  if ( maxXItem > mSceneBoundingRectangle.right() )
82  {
83  mSceneBoundingRectangle.setRight( maxXItem );
84  }
85  if ( maxYItem > mSceneBoundingRectangle.bottom() )
86  {
87  mSceneBoundingRectangle.setBottom( maxYItem );
88  }
89  }
90 
91  QgsComposerItem::setSceneRect( mSceneBoundingRectangle ); //call method of superclass to avoid repositioning of items
92 
93 }
94 
96 {
97  QSet<QgsComposerItem*>::iterator item_it = mItems.begin();
98  for ( ; item_it != mItems.end(); ++item_it )
99  {
100  ( *item_it )->setFlag( QGraphicsItem::ItemIsSelectable, true ); //enable item selection again
101  ( *item_it )->setSelected( true );
102  }
103  mItems.clear();
104 }
105 
106 void QgsComposerItemGroup::paint( QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget )
107 {
108  drawFrame( painter );
109  if ( isSelected() )
110  {
111  drawSelectionBoxes( painter );
112  }
113 }
114 
115 void QgsComposerItemGroup::setSceneRect( const QRectF& rectangle )
116 {
117  //calculate values between 0 and 1 for boundaries of all contained items, depending on their positions in the item group rectangle.
118  //then position the item boundaries in the new item group rect such that these values are the same
119  double xLeftCurrent = transform().dx();
120  double xRightCurrent = xLeftCurrent + rect().width();
121  double yTopCurrent = transform().dy();
122  double yBottomCurrent = yTopCurrent + rect().height();
123 
124  double xItemLeft, xItemRight, yItemTop, yItemBottom;
125  double xItemLeftNew, xItemRightNew, yItemTopNew, yItemBottomNew;
126  double xParamLeft, xParamRight, yParamTop, yParamBottom;
127 
128 
129  QSet<QgsComposerItem*>::iterator item_it = mItems.begin();
130  for ( ; item_it != mItems.end(); ++item_it )
131  {
132  xItemLeft = ( *item_it )->transform().dx();
133  xItemRight = xItemLeft + ( *item_it )->rect().width();
134  yItemTop = ( *item_it )->transform().dy();
135  yItemBottom = yItemTop + ( *item_it )->rect().height();
136 
137  xParamLeft = ( xItemLeft - xLeftCurrent ) / ( xRightCurrent - xLeftCurrent );
138  xParamRight = ( xItemRight - xLeftCurrent ) / ( xRightCurrent - xLeftCurrent );
139  yParamTop = ( yItemTop - yTopCurrent ) / ( yBottomCurrent - yTopCurrent );
140  yParamBottom = ( yItemBottom - yTopCurrent ) / ( yBottomCurrent - yTopCurrent );
141 
142  xItemLeftNew = xParamLeft * rectangle.right() + ( 1 - xParamLeft ) * rectangle.left();
143  xItemRightNew = xParamRight * rectangle.right() + ( 1 - xParamRight ) * rectangle.left();
144  yItemTopNew = yParamTop * rectangle.bottom() + ( 1 - yParamTop ) * rectangle.top();
145  yItemBottomNew = yParamBottom * rectangle.bottom() + ( 1 - yParamBottom ) * rectangle.top();
146 
147  ( *item_it )->setSceneRect( QRectF( xItemLeftNew, yItemTopNew, xItemRightNew - xItemLeftNew, yItemBottomNew - yItemTopNew ) );
148  }
149  QgsComposerItem::setSceneRect( rectangle );
150 }
151 
153 {
154  if ( !mComposition )
155  {
156  return;
157  }
158 
160  {
161  QPen newPen( pen() );
162  newPen.setStyle( Qt::DashLine );
163  newPen.setColor( QColor( 128, 128, 128, 128 ) );
164  p->setPen( newPen );
165  p->setRenderHint( QPainter::Antialiasing, true );
166  p->drawRect( QRectF( 0, 0, rect().width(), rect().height() ) );
167  }
168 }