Skip to content

Commit 41365d8

Browse files
committedDec 30, 2013
[composer] Fix item rotation so that rotation occurs around item center
1 parent cea569d commit 41365d8

File tree

3 files changed

+20
-7
lines changed

3 files changed

+20
-7
lines changed
 

‎src/app/composer/qgscomposeritemwidget.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -504,7 +504,7 @@ void QgsComposerItemWidget::on_mItemRotationSpinBox_valueChanged( double val )
504504
if ( mItem )
505505
{
506506
mItem->beginCommand( tr( "Item rotation changed" ), QgsComposerMergeCommand::ItemRotation );
507-
mItem->setItemRotation( val );
507+
mItem->setItemRotation( val, true );
508508
mItem->update();
509509
mItem->endCommand();
510510
}

‎src/core/composer/qgscomposeritem.cpp

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -486,8 +486,6 @@ void QgsComposerItem::setSceneRect( const QRectF& rectangle )
486486

487487
QRectF newRect( 0, 0, newWidth, newHeight );
488488
QGraphicsRectItem::setRect( newRect );
489-
//rotate item around its centre point
490-
setTransformOriginPoint( QPointF( rect().width() / 2.0, rect().height() / 2.0 ) );
491489
setPos( xTranslation, yTranslation );
492490

493491
emit sizeChanged();
@@ -712,8 +710,21 @@ void QgsComposerItem::setRotation( double r )
712710
setItemRotation( r );
713711
}
714712

715-
void QgsComposerItem::setItemRotation( double r )
713+
void QgsComposerItem::setItemRotation( double r, bool adjustPosition )
716714
{
715+
if ( adjustPosition )
716+
{
717+
//adjustPosition set, so shift the position of the item so that rotation occurs around item center
718+
//create a line from the centrepoint of the rect() to its origin, in scene coordinates
719+
QLineF refLine = QLineF( mapToScene( QPointF( rect().width() / 2.0, rect().height() / 2.0 ) ) , mapToScene( QPointF( 0 , 0 ) ) );
720+
//rotate this line by the current rotation angle
721+
refLine.setAngle( refLine.angle() - r + mItemRotation );
722+
//get new end point of line - this is the new item position
723+
QPointF rotatedReferencePoint = refLine.p2();
724+
setPos( rotatedReferencePoint );
725+
emit sizeChanged();
726+
}
727+
717728
if ( r > 360 )
718729
{
719730
mItemRotation = (( int )r ) % 360;
@@ -723,8 +734,7 @@ void QgsComposerItem::setItemRotation( double r )
723734
mItemRotation = r;
724735
}
725736

726-
//rotate item around its centre point
727-
setTransformOriginPoint( QPointF( rect().width() / 2.0, rect().height() / 2.0 ) );
737+
setTransformOriginPoint( 0, 0 );
728738
QGraphicsItem::setRotation( mItemRotation );
729739

730740
emit itemRotationChanged( r );

‎src/core/composer/qgscomposeritem.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -320,9 +320,12 @@ class CORE_EXPORT QgsComposerItem: public QObject, public QGraphicsRectItem
320320
virtual void setRotation( double r );
321321

322322
/**Sets the item rotation
323+
@param r item rotation in degrees
324+
@param adjustPosition set to true if item should be shifted so that rotation occurs
325+
around item center. If false, rotation occurs around item origin
323326
@note this method was added in version 2.1
324327
*/
325-
virtual void setItemRotation( double r );
328+
virtual void setItemRotation( double r, bool adjustPosition = false );
326329

327330
void repaint();
328331

0 commit comments

Comments
 (0)
Please sign in to comment.