Skip to content

Commit

Permalink
Fix rotate label tool results in inverted rotation
Browse files Browse the repository at this point in the history
Fix #17068
  • Loading branch information
nyalldawson committed Sep 4, 2017
1 parent fadfb35 commit 0f407e1
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 16 deletions.
27 changes: 15 additions & 12 deletions src/app/qgsmaptoolrotatelabel.cpp
Expand Up @@ -75,7 +75,7 @@ void QgsMapToolRotateLabel::canvasPressEvent( QgsMapMouseEvent *e )

if ( true )
{
mCurrentMouseAzimuth = azimuthToCCW( mRotationPoint.azimuth( toMapCoordinates( e->pos() ) ) );
mCurrentMouseAzimuth = convertAzimuth( mRotationPoint.azimuth( toMapCoordinates( e->pos() ) ) );

bool hasRotationValue;
int rotationCol;
Expand All @@ -91,7 +91,7 @@ void QgsMapToolRotateLabel::canvasPressEvent( QgsMapMouseEvent *e )
mRotationPreviewBox = createRotationPreviewBox();

mRotationItem = new QgsPointRotationItem( mCanvas );
mRotationItem->setOrientation( QgsPointRotationItem::Counterclockwise );
mRotationItem->setOrientation( QgsPointRotationItem::Clockwise );
mRotationItem->setSymbol( QgsApplication::getThemePixmap( QStringLiteral( "mActionRotatePointSymbols.svg" ) ).toImage() );
mRotationItem->setPointLocation( mRotationPoint );
mRotationItem->setSymbolRotation( mCurrentRotation );
Expand All @@ -104,15 +104,17 @@ void QgsMapToolRotateLabel::canvasMoveEvent( QgsMapMouseEvent *e )
if ( mLabelRubberBand )
{
QgsPointXY currentPoint = toMapCoordinates( e->pos() );
double azimuth = azimuthToCCW( mRotationPoint.azimuth( currentPoint ) );
double azimuth = convertAzimuth( mRotationPoint.azimuth( currentPoint ) );
double azimuthDiff = azimuth - mCurrentMouseAzimuth;
azimuthDiff = azimuthDiff > 180 ? azimuthDiff - 360 : azimuthDiff;

mCurrentRotation += azimuthDiff;
mCurrentRotation = mCurrentRotation - static_cast<float>( static_cast<int>( mCurrentRotation / 360 ) ) * 360; //mCurrentRotation % 360;
mCurrentRotation = mCurrentRotation < 0 ? 360 - mCurrentRotation : mCurrentRotation;
if ( mCurrentRotation >= 360 || mCurrentRotation <= -360 )
mCurrentRotation = std::fmod( mCurrentRotation, 360.0 );
if ( mCurrentRotation < 0 )
mCurrentRotation += 360.0;

mCurrentMouseAzimuth = azimuth - static_cast<float>( static_cast<int>( azimuth / 360 ) ) * 360;
mCurrentMouseAzimuth = std::fmod( azimuth, 360.0 );

//if shift-modifier is pressed, round to 15 degrees
int displayValue;
Expand Down Expand Up @@ -181,9 +183,10 @@ int QgsMapToolRotateLabel::roundTo15Degrees( double n )
return ( m * 15 );
}

double QgsMapToolRotateLabel::azimuthToCCW( double a )
double QgsMapToolRotateLabel::convertAzimuth( double a )
{
return ( a > 0 ? 360 - a : -a );
a -= 90; // convert from 0 = north to 0 = east
return ( a <= -180.0 ? 360 + a : a );
}

QgsRubberBand *QgsMapToolRotateLabel::createRotationPreviewBox()
Expand Down Expand Up @@ -218,15 +221,15 @@ void QgsMapToolRotateLabel::setRotationPreviewBox( double rotation )

for ( int i = 0; i < boxPoints.size(); ++i )
{
mRotationPreviewBox->addPoint( rotatePointCounterClockwise( boxPoints.at( i ), mRotationPoint, rotation ) );
mRotationPreviewBox->addPoint( rotatePointClockwise( boxPoints.at( i ), mRotationPoint, rotation ) );
}
mRotationPreviewBox->addPoint( rotatePointCounterClockwise( boxPoints.at( 0 ), mRotationPoint, rotation ) );
mRotationPreviewBox->addPoint( rotatePointClockwise( boxPoints.at( 0 ), mRotationPoint, rotation ) );
mRotationPreviewBox->show();
}

QgsPointXY QgsMapToolRotateLabel::rotatePointCounterClockwise( const QgsPointXY &input, const QgsPointXY &centerPoint, double degrees )
QgsPointXY QgsMapToolRotateLabel::rotatePointClockwise( const QgsPointXY &input, const QgsPointXY &centerPoint, double degrees ) const
{
double rad = degrees / 180 * M_PI;
double rad = -degrees / 180 * M_PI;
double v1x = input.x() - centerPoint.x();
double v1y = input.y() - centerPoint.y();

Expand Down
8 changes: 4 additions & 4 deletions src/app/qgsmaptoolrotatelabel.h
Expand Up @@ -37,14 +37,14 @@ class APP_EXPORT QgsMapToolRotateLabel: public QgsMapToolLabel
protected:

static int roundTo15Degrees( double n );
//! Converts azimuth value to counterclockwise 0 - 360
static double azimuthToCCW( double a );
//! Converts azimuth value so that 0 is corresponds to East
static double convertAzimuth( double a );

QgsRubberBand *createRotationPreviewBox();
void setRotationPreviewBox( double rotation );

//! Rotates input point counterclockwise around centerPoint
QgsPointXY rotatePointCounterClockwise( const QgsPointXY &input, const QgsPointXY &centerPoint, double degrees );
//! Rotates input point clockwise around centerPoint
QgsPointXY rotatePointClockwise( const QgsPointXY &input, const QgsPointXY &centerPoint, double degrees ) const;

double mStartRotation; //rotation value prior to start rotating
double mCurrentRotation;
Expand Down

0 comments on commit 0f407e1

Please sign in to comment.