Skip to content

Commit

Permalink
[composer] Fix item rotation so that rotation occurs around item center
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Dec 30, 2013
1 parent cea569d commit 41365d8
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/app/composer/qgscomposeritemwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -504,7 +504,7 @@ void QgsComposerItemWidget::on_mItemRotationSpinBox_valueChanged( double val )
if ( mItem )
{
mItem->beginCommand( tr( "Item rotation changed" ), QgsComposerMergeCommand::ItemRotation );
mItem->setItemRotation( val );
mItem->setItemRotation( val, true );
mItem->update();
mItem->endCommand();
}
Expand Down
20 changes: 15 additions & 5 deletions src/core/composer/qgscomposeritem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -486,8 +486,6 @@ void QgsComposerItem::setSceneRect( const QRectF& rectangle )

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

emit sizeChanged();
Expand Down Expand Up @@ -712,8 +710,21 @@ void QgsComposerItem::setRotation( double r )
setItemRotation( r );
}

void QgsComposerItem::setItemRotation( double r )
void QgsComposerItem::setItemRotation( double r, bool adjustPosition )
{
if ( adjustPosition )
{
//adjustPosition set, so shift the position of the item so that rotation occurs around item center
//create a line from the centrepoint of the rect() to its origin, in scene coordinates
QLineF refLine = QLineF( mapToScene( QPointF( rect().width() / 2.0, rect().height() / 2.0 ) ) , mapToScene( QPointF( 0 , 0 ) ) );
//rotate this line by the current rotation angle
refLine.setAngle( refLine.angle() - r + mItemRotation );
//get new end point of line - this is the new item position
QPointF rotatedReferencePoint = refLine.p2();
setPos( rotatedReferencePoint );
emit sizeChanged();
}

if ( r > 360 )
{
mItemRotation = (( int )r ) % 360;
Expand All @@ -723,8 +734,7 @@ void QgsComposerItem::setItemRotation( double r )
mItemRotation = r;
}

//rotate item around its centre point
setTransformOriginPoint( QPointF( rect().width() / 2.0, rect().height() / 2.0 ) );
setTransformOriginPoint( 0, 0 );
QGraphicsItem::setRotation( mItemRotation );

emit itemRotationChanged( r );
Expand Down
5 changes: 4 additions & 1 deletion src/core/composer/qgscomposeritem.h
Original file line number Diff line number Diff line change
Expand Up @@ -320,9 +320,12 @@ class CORE_EXPORT QgsComposerItem: public QObject, public QGraphicsRectItem
virtual void setRotation( double r );

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

void repaint();

Expand Down

0 comments on commit 41365d8

Please sign in to comment.