|
17 | 17 |
|
18 | 18 | #include "qgscomposeritemgroup.h"
|
19 | 19 | #include "qgscomposition.h"
|
| 20 | +#include "qgslogger.h" |
20 | 21 |
|
21 | 22 | #include <QPen>
|
22 | 23 | #include <QPainter>
|
@@ -59,40 +60,39 @@ void QgsComposerItemGroup::addItem( QgsComposerItem* item )
|
59 | 60 | item->setSelected( false );
|
60 | 61 | item->setFlag( QGraphicsItem::ItemIsSelectable, false ); //item in groups cannot be selected
|
61 | 62 |
|
62 |
| - //update extent (which is in scene coordinates) |
63 |
| - double minXItem = item->pos().x(); |
64 |
| - double minYItem = item->pos().y(); |
65 |
| - double maxXItem = minXItem + item->rect().width(); |
66 |
| - double maxYItem = minYItem + item->rect().height(); |
67 |
| - |
68 |
| - if ( mSceneBoundingRectangle.isEmpty() ) //we add the first item |
| 63 | + //update extent |
| 64 | + if ( mBoundingRectangle.isEmpty() ) //we add the first item |
69 | 65 | {
|
70 |
| - mSceneBoundingRectangle.setLeft( minXItem ); |
71 |
| - mSceneBoundingRectangle.setTop( minYItem ); |
72 |
| - mSceneBoundingRectangle.setRight( maxXItem ); |
73 |
| - mSceneBoundingRectangle.setBottom( maxYItem ); |
| 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 | 74 | }
|
75 | 75 | else
|
76 | 76 | {
|
77 |
| - if ( minXItem < mSceneBoundingRectangle.left() ) |
78 |
| - { |
79 |
| - mSceneBoundingRectangle.setLeft( minXItem ); |
80 |
| - } |
81 |
| - if ( minYItem < mSceneBoundingRectangle.top() ) |
| 77 | + if ( item->itemRotation() != itemRotation() ) |
82 | 78 | {
|
83 |
| - mSceneBoundingRectangle.setTop( minYItem ); |
| 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 |
| 84 | + QgsComposerItem::setSceneRect( mBoundingRectangle ); |
84 | 85 | }
|
85 |
| - if ( maxXItem > mSceneBoundingRectangle.right() ) |
| 86 | + else |
86 | 87 | {
|
87 |
| - mSceneBoundingRectangle.setRight( maxXItem ); |
88 |
| - } |
89 |
| - if ( maxYItem > mSceneBoundingRectangle.bottom() ) |
90 |
| - { |
91 |
| - mSceneBoundingRectangle.setBottom( maxYItem ); |
| 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() ) ); |
92 | 93 | }
|
93 | 94 | }
|
94 | 95 |
|
95 |
| - QgsComposerItem::setSceneRect( mSceneBoundingRectangle ); //call method of superclass to avoid repositioning of items |
96 | 96 | }
|
97 | 97 |
|
98 | 98 | void QgsComposerItemGroup::removeItems()
|
@@ -124,38 +124,22 @@ void QgsComposerItemGroup::paint( QPainter * painter, const QStyleOptionGraphics
|
124 | 124 |
|
125 | 125 | void QgsComposerItemGroup::setSceneRect( const QRectF& rectangle )
|
126 | 126 | {
|
127 |
| - //calculate values between 0 and 1 for boundaries of all contained items, depending on their positions in the item group rectangle. |
128 |
| - //then position the item boundaries in the new item group rect such that these values are the same |
129 |
| - double xLeftCurrent = pos().x(); |
130 |
| - double xRightCurrent = xLeftCurrent + rect().width(); |
131 |
| - double yTopCurrent = pos().y(); |
132 |
| - double yBottomCurrent = yTopCurrent + rect().height(); |
133 |
| - |
134 |
| - double xItemLeft, xItemRight, yItemTop, yItemBottom; |
135 |
| - double xItemLeftNew, xItemRightNew, yItemTopNew, yItemBottomNew; |
136 |
| - double xParamLeft, xParamRight, yParamTop, yParamBottom; |
137 |
| - |
| 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() ); |
138 | 131 |
|
139 | 132 | QSet<QgsComposerItem*>::iterator item_it = mItems.begin();
|
140 | 133 | for ( ; item_it != mItems.end(); ++item_it )
|
141 | 134 | {
|
142 |
| - xItemLeft = ( *item_it )->pos().x(); |
143 |
| - xItemRight = xItemLeft + ( *item_it )->rect().width(); |
144 |
| - yItemTop = ( *item_it )->pos().y(); |
145 |
| - yItemBottom = yItemTop + ( *item_it )->rect().height(); |
146 |
| - |
147 |
| - xParamLeft = ( xItemLeft - xLeftCurrent ) / ( xRightCurrent - xLeftCurrent ); |
148 |
| - xParamRight = ( xItemRight - xLeftCurrent ) / ( xRightCurrent - xLeftCurrent ); |
149 |
| - yParamTop = ( yItemTop - yTopCurrent ) / ( yBottomCurrent - yTopCurrent ); |
150 |
| - yParamBottom = ( yItemBottom - yTopCurrent ) / ( yBottomCurrent - yTopCurrent ); |
151 |
| - |
152 |
| - xItemLeftNew = xParamLeft * rectangle.right() + ( 1 - xParamLeft ) * rectangle.left(); |
153 |
| - xItemRightNew = xParamRight * rectangle.right() + ( 1 - xParamRight ) * rectangle.left(); |
154 |
| - yItemTopNew = yParamTop * rectangle.bottom() + ( 1 - yParamTop ) * rectangle.top(); |
155 |
| - yItemBottomNew = yParamBottom * rectangle.bottom() + ( 1 - yParamBottom ) * rectangle.top(); |
156 |
| - |
157 |
| - ( *item_it )->setSceneRect( QRectF( xItemLeftNew, yItemTopNew, xItemRightNew - xItemLeftNew, yItemBottomNew - yItemTopNew ) ); |
| 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() ) ); |
158 | 141 | }
|
| 142 | + //lastly, set new rect for group |
159 | 143 | QgsComposerItem::setSceneRect( rectangle );
|
160 | 144 | }
|
161 | 145 |
|
|
0 commit comments