Skip to content
Permalink
Browse files

Merge pull request #5118 from nyalldawson/label_rotate

Fix rotate label tool
  • Loading branch information
nyalldawson committed Sep 4, 2017
2 parents f0e53db + d1d5e6c commit 088e58aac6ec2ccc87954fa1f6a218370040f950
@@ -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;
@@ -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 );
}
@@ -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;
@@ -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()
@@ -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();

@@ -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;
@@ -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()
@@ -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

0 comments on commit 088e58a

Please sign in to comment.
You can’t perform that action at this time.