Skip to content

Commit

Permalink
Merge pull request #5118 from nyalldawson/label_rotate
Browse files Browse the repository at this point in the history
Fix rotate label tool
  • Loading branch information
nyalldawson authored Sep 4, 2017
2 parents f0e53db + d1d5e6c commit 088e58a
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 25 deletions.
28 changes: 15 additions & 13 deletions src/app/qgsmaptoolrotatelabel.cpp
Original file line number Diff line number Diff line change
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,8 +91,7 @@ void QgsMapToolRotateLabel::canvasPressEvent( QgsMapMouseEvent *e )
mRotationPreviewBox = createRotationPreviewBox();

mRotationItem = new QgsPointRotationItem( mCanvas );
mRotationItem->setOrientation( QgsPointRotationItem::Counterclockwise );
mRotationItem->setSymbol( QgsApplication::getThemePixmap( QStringLiteral( "mActionRotatePointSymbols.svg" ) ).toImage() );
mRotationItem->setOrientation( QgsPointRotationItem::Clockwise );
mRotationItem->setPointLocation( mRotationPoint );
mRotationItem->setSymbolRotation( mCurrentRotation );
}
Expand All @@ -104,15 +103,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 +182,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 +220,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
Original file line number Diff line number Diff line change
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
10 changes: 3 additions & 7 deletions src/app/qgspointrotationitem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,10 @@ QgsPointRotationItem::QgsPointRotationItem( QgsMapCanvas *canvas )
//setup font
mFont.setPointSize( 12 );
mFont.setBold( true );
}

QgsPointRotationItem::QgsPointRotationItem()
: QgsMapCanvasItem( nullptr )
, mOrientation( Clockwise )
, mRotation( 0.0 )
{

QImage im( 24, 24, QImage::Format_ARGB32 );
im.fill( Qt::transparent );
setSymbol( im );
}

QgsPointRotationItem::~QgsPointRotationItem()
Expand Down
2 changes: 1 addition & 1 deletion src/app/qgspointrotationitem.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class APP_EXPORT QgsPointRotationItem: public QgsMapCanvasItem
Orientation orientation() const { return mOrientation; }

private:
QgsPointRotationItem();

//! Converts rotation into QPainter rotation considering mOrientation
int painterRotation( int rotation ) const;
//! Clockwise (default) or counterclockwise
Expand Down

0 comments on commit 088e58a

Please sign in to comment.