Skip to content

Commit eaec7ac

Browse files
author
mhugent
committedNov 26, 2009
Keep shape and composer picture size during rotation. Adapt the item size instead
git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@12262 c8812cc2-4d05-0410-92ff-de0c093fc19c
1 parent 7e01bf9 commit eaec7ac

11 files changed

+187
-55
lines changed
 

‎python/core/qgscomposeritem.sip

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ class QgsComposerItem: QObject, QGraphicsRectItem
145145
double rotation() const;
146146

147147
public slots:
148-
void setRotation( double r);
148+
virtual void setRotation( double r);
149149

150150
protected:
151151

@@ -200,6 +200,13 @@ class QgsComposerItem: QObject, QGraphicsRectItem
200200
bool cornerPointOnRotatedAndScaledRect( double& x, double& y, double width, double height ) const;
201201
/**Returns a point on the line from startPoint to directionPoint that is a certain distance away from the starting point*/
202202
QPointF pointOnLineWithDistance( const QPointF& startPoint, const QPointF& directionPoint, double distance ) const;
203+
/**Calculates width / height of the bounding box of a rotated rectangle (mRotation)*/
204+
void sizeChangedByRotation(double& width, double& height);
205+
/**Rotates a point / vector
206+
@param angle rotation angle in degrees, counterclockwise
207+
@param x in/out: x coordinate before / after the rotation
208+
@param y in/out: y cooreinate before / after the rotation*/
209+
void rotate( double angle, double& x, double& y ) const;
203210

204211
signals:
205212
/**Is emitted on rotation change to notify north arrow pictures*/

‎python/core/qgscomposerpicture.sip

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ class QgsComposerPicture: QgsComposerItem
4141
/**True if the rotation is taken from a map item*/
4242
bool useRotationMap() const;
4343

44+
public slots:
45+
virtual void setRotation( double r );
46+
4447

4548
signals:
4649
/**Tell the configuration widget that the settings need to be updated*/

‎src/core/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@ composer/qgscomposerpicture.h
202202
composer/qgscomposerscalebar.h
203203
composer/qgscomposeritem.h
204204
composer/qgscomposeritemgroup.h
205+
composer/qgscomposershape.h
205206
composer/qgscomposition.h
206207
composer/qgslegendmodel.h
207208
symbology/qgsmarkercatalogue.h

‎src/core/composer/qgscomposeritem.cpp

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -948,3 +948,50 @@ QPointF QgsComposerItem::pointOnLineWithDistance( const QPointF& startPoint, con
948948
double scaleFactor = distance / length;
949949
return QPointF( startPoint.x() + dx * scaleFactor, startPoint.y() + dy * scaleFactor );
950950
}
951+
952+
void QgsComposerItem::sizeChangedByRotation( double& width, double& height )
953+
{
954+
if ( mRotation == 0.0 )
955+
{
956+
return;
957+
}
958+
959+
//vector to p1
960+
double x1 = -width / 2.0;
961+
double y1 = -height / 2.0;
962+
rotate( mRotation, x1, y1 );
963+
//vector to p2
964+
double x2 = width / 2.0;
965+
double y2 = -height / 2.0;
966+
rotate( mRotation, x2, y2 );
967+
//vector to p3
968+
double x3 = width / 2.0;
969+
double y3 = height / 2.0;
970+
rotate( mRotation, x3, y3 );
971+
//vector to p4
972+
double x4 = -width / 2.0;
973+
double y4 = height / 2.0;
974+
rotate( mRotation, x4, y4 );
975+
976+
//double midpoint
977+
QPointF midpoint( width / 2.0, height / 2.0 );
978+
979+
QPolygonF rotatedRectPoly;
980+
rotatedRectPoly << QPointF( midpoint.x() + x1, midpoint.y() + y1 );
981+
rotatedRectPoly << QPointF( midpoint.x() + x2, midpoint.y() + y2 );
982+
rotatedRectPoly << QPointF( midpoint.x() + x3, midpoint.y() + y3 );
983+
rotatedRectPoly << QPointF( midpoint.x() + x4, midpoint.y() + y4 );
984+
QRectF boundingRect = rotatedRectPoly.boundingRect();
985+
width = boundingRect.width();
986+
height = boundingRect.height();
987+
}
988+
989+
void QgsComposerItem::rotate( double angle, double& x, double& y ) const
990+
{
991+
double rotToRad = angle * M_PI / 180.0;
992+
double xRot, yRot;
993+
xRot = x * cos( rotToRad ) - y * sin( rotToRad );
994+
yRot = x * sin( rotToRad ) + y * cos( rotToRad );
995+
x = xRot;
996+
y = yRot;
997+
}

‎src/core/composer/qgscomposeritem.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ class CORE_EXPORT QgsComposerItem: public QObject, public QGraphicsRectItem
172172
double rotation() const {return mRotation;}
173173

174174
public slots:
175-
void setRotation( double r );
175+
virtual void setRotation( double r );
176176

177177
protected:
178178

@@ -245,12 +245,21 @@ class CORE_EXPORT QgsComposerItem: public QObject, public QGraphicsRectItem
245245
@note: this function was introduced in version 1.2*/
246246
double horizontalViewScaleFactor() const;
247247

248+
//some utility functions
249+
248250
/**Calculates width and hight of the picture (in mm) such that it fits into the item frame with the given rotation*/
249251
bool imageSizeConsideringRotation( double& width, double& height ) const;
250252
/**Calculates corner point after rotation and scaling*/
251253
bool cornerPointOnRotatedAndScaledRect( double& x, double& y, double width, double height ) const;
252254
/**Returns a point on the line from startPoint to directionPoint that is a certain distance away from the starting point*/
253255
QPointF pointOnLineWithDistance( const QPointF& startPoint, const QPointF& directionPoint, double distance ) const;
256+
/**Calculates width / height of the bounding box of a rotated rectangle (mRotation)*/
257+
void sizeChangedByRotation( double& width, double& height );
258+
/**Rotates a point / vector
259+
@param angle rotation angle in degrees, counterclockwise
260+
@param x in/out: x coordinate before / after the rotation
261+
@param y in/out: y cooreinate before / after the rotation*/
262+
void rotate( double angle, double& x, double& y ) const;
254263

255264
signals:
256265
/**Is emitted on rotation change to notify north arrow pictures*/

‎src/core/composer/qgscomposermap.cpp

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1383,24 +1383,3 @@ QgsComposerMap::Border QgsComposerMap::borderForLineCoord( const QPointF& p ) co
13831383
return Bottom;
13841384
}
13851385
}
1386-
1387-
void QgsComposerMap::rotate( double angle, double& x, double& y ) const
1388-
{
1389-
double rotToRad = angle * M_PI / 180.0;
1390-
double xRot, yRot;
1391-
xRot = x * cos( rotToRad ) - y * sin( rotToRad );
1392-
yRot = x * sin( rotToRad ) + y * cos( rotToRad );
1393-
x = xRot;
1394-
y = yRot;
1395-
}
1396-
1397-
#if 0
1398-
QPointF QgsComposerMap::pointOnLineWithDistance( const QPointF& startPoint, const QPointF& directionPoint, double distance ) const
1399-
{
1400-
double dx = directionPoint.x() - startPoint.x();
1401-
double dy = directionPoint.y() - startPoint.y();
1402-
double length = sqrt( dx * dx + dy * dy );
1403-
double scaleFactor = distance / length;
1404-
return QPointF( startPoint.x() + dx * scaleFactor, startPoint.y() + dy * scaleFactor );
1405-
}
1406-
#endif //0

‎src/core/composer/qgscomposermap.h

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -381,15 +381,6 @@ class CORE_EXPORT QgsComposerMap : public QgsComposerItem
381381
QPointF mapToItemCoords( const QPointF& mapCoords ) const;
382382
/**Returns the item border of a point (in item coordinates)*/
383383
Border borderForLineCoord( const QPointF& p ) const;
384-
/**Rotates a point / vector
385-
@param angle rotation angle in degrees, counterclockwise
386-
@param x in/out: x coordinate before / after the rotation
387-
@param y in/out: y cooreinate before / after the rotation*/
388-
void rotate( double angle, double& x, double& y ) const;
389-
#if 0
390-
/**Returns a point on the line from startPoint to directionPoint that is a certain distance away from the starting point*/
391-
QPointF pointOnLineWithDistance( const QPointF& startPoint, const QPointF& directionPoint, double distance ) const;
392-
#endif //0
393384
};
394385

395386
#endif

‎src/core/composer/qgscomposerpicture.cpp

Lines changed: 59 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,12 @@
3030
QgsComposerPicture::QgsComposerPicture( QgsComposition *composition ): QgsComposerItem( composition ), mMode( Unknown ), \
3131
mSvgCacheUpToDate( false ), mCachedDpi( 0 ), mCachedRotation( 0 ), mCachedViewScaleFactor( -1 ), mRotationMap( 0 )
3232
{
33+
mPictureWidth = rect().width();
3334
}
3435

3536
QgsComposerPicture::QgsComposerPicture(): QgsComposerItem( 0 ), mMode( Unknown ), mSvgCacheUpToDate( false ), mCachedRotation( 0 ), mCachedViewScaleFactor( -1 ), mRotationMap( 0 )
3637
{
37-
38+
mPictureHeight = rect().height();
3839
}
3940

4041
QgsComposerPicture::~QgsComposerPicture()
@@ -61,8 +62,8 @@ void QgsComposerPicture::paint( QPainter* painter, const QStyleOptionGraphicsIte
6162

6263
if ( mMode != Unknown )
6364
{
64-
double rectPixelWidth = rect().width() * newDpi / 25.4;
65-
double rectPixelHeight = rect().height() * newDpi / 25.4;
65+
double rectPixelWidth = /*rect().width()*/mPictureWidth * newDpi / 25.4;
66+
double rectPixelHeight = /*rect().height()*/ mPictureHeight * newDpi / 25.4;
6667
QRectF boundRect;
6768
if ( mMode == SVG )
6869
{
@@ -75,11 +76,8 @@ void QgsComposerPicture::paint( QPainter* painter, const QStyleOptionGraphicsIte
7576

7677
double boundRectWidthMM = boundRect.width() / newDpi * 25.4;
7778
double boundRectHeightMM = boundRect.height() / newDpi * 25.4;
78-
double rotatedBoundImageWidth = boundRect.width();
79-
double rotatedBoundImageHeight = boundRect.height();
80-
imageSizeConsideringRotation( rotatedBoundImageWidth, rotatedBoundImageHeight );
81-
double rotatedBoundImageWidthMM = rotatedBoundImageWidth / newDpi * 25.4;
82-
double rotatedBoundImageHeightMM = rotatedBoundImageHeight / newDpi * 25.4;
79+
double boundImageWidth = boundRect.width();
80+
double boundImageHeight = boundRect.height();
8381

8482
if ( mMode == SVG )
8583
{
@@ -88,20 +86,20 @@ void QgsComposerPicture::paint( QPainter* painter, const QStyleOptionGraphicsIte
8886
//make nicer preview
8987
if ( mComposition && mComposition->plotStyle() == QgsComposition::Preview )
9088
{
91-
rotatedBoundImageWidth *= std::min( viewScaleFactor, 10.0 );
92-
rotatedBoundImageHeight *= std::min( viewScaleFactor, 10.0 );
89+
boundImageWidth *= std::min( viewScaleFactor, 10.0 );
90+
boundImageHeight *= std::min( viewScaleFactor, 10.0 );
9391
}
94-
mImage = QImage( rotatedBoundImageWidth, rotatedBoundImageHeight, QImage::Format_ARGB32 );
92+
mImage = QImage( boundImageWidth, boundImageHeight, QImage::Format_ARGB32 );
9593
updateImageFromSvg();
9694
}
9795
}
9896

9997
painter->save();
100-
painter->translate( boundRectWidthMM / 2.0, boundRectHeightMM / 2.0 );
98+
painter->translate( rect().width() / 2.0, rect().height() / 2.0 );
10199
painter->rotate( mRotation );
102-
painter->translate( -rotatedBoundImageWidthMM / 2.0, -rotatedBoundImageHeightMM / 2.0 );
100+
painter->translate( -boundRectWidthMM / 2.0, -boundRectHeightMM / 2.0 );
103101

104-
painter->drawImage( QRectF( 0, 0, rotatedBoundImageWidthMM, rotatedBoundImageHeightMM ), mImage, QRectF( 0, 0, mImage.width(), mImage.height() ) );
102+
painter->drawImage( QRectF( 0, 0, boundRectWidthMM, boundRectHeightMM ), mImage, QRectF( 0, 0, mImage.width(), mImage.height() ) );
105103

106104
painter->restore();
107105
}
@@ -183,6 +181,24 @@ QRectF QgsComposerPicture::boundedImageRect( double deviceWidth, double deviceHe
183181
}
184182
}
185183

184+
QRectF QgsComposerPicture::boundedSVGRect( double deviceWidth, double deviceHeight )
185+
{
186+
double imageToSvgRatio;
187+
if ( deviceWidth / mDefaultSvgSize.width() > deviceHeight / mDefaultSvgSize.height() )
188+
{
189+
imageToSvgRatio = deviceHeight / mDefaultSvgSize.height();
190+
double width = mDefaultSvgSize.width() * imageToSvgRatio;
191+
return QRectF( 0, 0, width, deviceHeight );
192+
}
193+
else
194+
{
195+
imageToSvgRatio = deviceWidth / mDefaultSvgSize.width();
196+
double height = mDefaultSvgSize.height() * imageToSvgRatio;
197+
return QRectF( 0, 0, deviceWidth, height );
198+
}
199+
}
200+
201+
#if 0
186202
QRectF QgsComposerPicture::boundedSVGRect( double deviceWidth, double deviceHeight )
187203
{
188204
double imageToSvgRatio;
@@ -199,6 +215,7 @@ QRectF QgsComposerPicture::boundedSVGRect( double deviceWidth, double deviceHeig
199215
return QRectF( 0, 0, width, deviceHeight );
200216
}
201217
}
218+
#endif //0
202219

203220
void QgsComposerPicture::updateImageFromSvg()
204221
{
@@ -216,9 +233,32 @@ void QgsComposerPicture::setSceneRect( const QRectF& rectangle )
216233
{
217234
mSvgCacheUpToDate = false;
218235
QgsComposerItem::setSceneRect( rectangle );
236+
237+
//consider to change size of the shape if the rectangle changes width and/or height
238+
double newPictureWidth = rectangle.width();
239+
double newPictureHeight = rectangle.height();
240+
imageSizeConsideringRotation( newPictureWidth, newPictureHeight );
241+
mPictureWidth = newPictureWidth;
242+
mPictureHeight = newPictureHeight;
243+
219244
emit settingsChanged();
220245
}
221246

247+
void QgsComposerPicture::setRotation( double r )
248+
{
249+
//adapt rectangle size
250+
double width = mPictureWidth;
251+
double height = mPictureHeight;
252+
sizeChangedByRotation( width, height );
253+
254+
//adapt scene rect to have the same center and the new width / height
255+
double x = transform().dx() + rect().width() / 2.0 - width / 2.0;
256+
double y = transform().dy() + rect().height() / 2.0 - height / 2.0;
257+
QgsComposerItem::setSceneRect( QRectF( x, y, width, height ) );
258+
259+
QgsComposerItem::setRotation( r );
260+
}
261+
222262
void QgsComposerPicture::setRotationMap( int composerMapId )
223263
{
224264
if ( !mComposition )
@@ -260,6 +300,8 @@ bool QgsComposerPicture::writeXML( QDomElement& elem, QDomDocument & doc ) const
260300
}
261301
QDomElement composerPictureElem = doc.createElement( "ComposerPicture" );
262302
composerPictureElem.setAttribute( "file", QgsProject::instance()->writePath( mSourceFile.fileName() ) );
303+
composerPictureElem.setAttribute( "pictureWidth", mPictureWidth );
304+
composerPictureElem.setAttribute( "pictureHeight", mPictureHeight );
263305
if ( !mRotationMap )
264306
{
265307
composerPictureElem.setAttribute( "mapId", -1 );
@@ -281,6 +323,9 @@ bool QgsComposerPicture::readXML( const QDomElement& itemElem, const QDomDocumen
281323
return false;
282324
}
283325

326+
mPictureWidth = itemElem.attribute( "pictureWidth", "10" ).toDouble();
327+
mPictureHeight = itemElem.attribute( "pictureHeight", "10" ).toDouble();
328+
284329
QDomNodeList composerItemList = itemElem.elementsByTagName( "ComposerItem" );
285330
if ( composerItemList.size() > 0 )
286331
{

‎src/core/composer/qgscomposerpicture.h

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class CORE_EXPORT QgsComposerPicture: public QgsComposerItem
3939
QString pictureFile() const;
4040

4141
/**Sets this items bound in scene coordinates such that 1 item size units
42-
corresponds to 1 scene size unit*/
42+
corresponds to 1 scene size unit and resizes the svg symbol / image*/
4343
void setSceneRect( const QRectF& rectangle );
4444

4545
/** stores state in Dom node
@@ -60,6 +60,10 @@ class CORE_EXPORT QgsComposerPicture: public QgsComposerItem
6060
/**True if the rotation is taken from a map item*/
6161
bool useRotationMap() const {return mRotationMap;}
6262

63+
public slots:
64+
/**Sets the rotation and adapts the item rect*/
65+
virtual void setRotation( double r );
66+
6367
private:
6468

6569
enum Mode //SVG or raster graphic format
@@ -94,7 +98,10 @@ class CORE_EXPORT QgsComposerPicture: public QgsComposerItem
9498
QSize mDefaultSvgSize;
9599
/**Map that sets the rotation (or 0 if this picture uses map independent rotation)*/
96100
const QgsComposerMap* mRotationMap;
97-
101+
/**Width of the picture (in mm)*/
102+
double mPictureWidth;
103+
/**Height of the picture (in mm)*/
104+
double mPictureHeight;
98105

99106
signals:
100107
/**Tell the configuration widget that the settings need to be updated*/

0 commit comments

Comments
 (0)
Please sign in to comment.