Skip to content

Commit 088e58a

Browse files
authored
Merge pull request #5118 from nyalldawson/label_rotate
Fix rotate label tool
2 parents f0e53db + d1d5e6c commit 088e58a

File tree

4 files changed

+23
-25
lines changed

4 files changed

+23
-25
lines changed

src/app/qgsmaptoolrotatelabel.cpp

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ void QgsMapToolRotateLabel::canvasPressEvent( QgsMapMouseEvent *e )
7575

7676
if ( true )
7777
{
78-
mCurrentMouseAzimuth = azimuthToCCW( mRotationPoint.azimuth( toMapCoordinates( e->pos() ) ) );
78+
mCurrentMouseAzimuth = convertAzimuth( mRotationPoint.azimuth( toMapCoordinates( e->pos() ) ) );
7979

8080
bool hasRotationValue;
8181
int rotationCol;
@@ -91,8 +91,7 @@ void QgsMapToolRotateLabel::canvasPressEvent( QgsMapMouseEvent *e )
9191
mRotationPreviewBox = createRotationPreviewBox();
9292

9393
mRotationItem = new QgsPointRotationItem( mCanvas );
94-
mRotationItem->setOrientation( QgsPointRotationItem::Counterclockwise );
95-
mRotationItem->setSymbol( QgsApplication::getThemePixmap( QStringLiteral( "mActionRotatePointSymbols.svg" ) ).toImage() );
94+
mRotationItem->setOrientation( QgsPointRotationItem::Clockwise );
9695
mRotationItem->setPointLocation( mRotationPoint );
9796
mRotationItem->setSymbolRotation( mCurrentRotation );
9897
}
@@ -104,15 +103,17 @@ void QgsMapToolRotateLabel::canvasMoveEvent( QgsMapMouseEvent *e )
104103
if ( mLabelRubberBand )
105104
{
106105
QgsPointXY currentPoint = toMapCoordinates( e->pos() );
107-
double azimuth = azimuthToCCW( mRotationPoint.azimuth( currentPoint ) );
106+
double azimuth = convertAzimuth( mRotationPoint.azimuth( currentPoint ) );
108107
double azimuthDiff = azimuth - mCurrentMouseAzimuth;
109108
azimuthDiff = azimuthDiff > 180 ? azimuthDiff - 360 : azimuthDiff;
110109

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

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

117118
//if shift-modifier is pressed, round to 15 degrees
118119
int displayValue;
@@ -181,9 +182,10 @@ int QgsMapToolRotateLabel::roundTo15Degrees( double n )
181182
return ( m * 15 );
182183
}
183184

184-
double QgsMapToolRotateLabel::azimuthToCCW( double a )
185+
double QgsMapToolRotateLabel::convertAzimuth( double a )
185186
{
186-
return ( a > 0 ? 360 - a : -a );
187+
a -= 90; // convert from 0 = north to 0 = east
188+
return ( a <= -180.0 ? 360 + a : a );
187189
}
188190

189191
QgsRubberBand *QgsMapToolRotateLabel::createRotationPreviewBox()
@@ -218,15 +220,15 @@ void QgsMapToolRotateLabel::setRotationPreviewBox( double rotation )
218220

219221
for ( int i = 0; i < boxPoints.size(); ++i )
220222
{
221-
mRotationPreviewBox->addPoint( rotatePointCounterClockwise( boxPoints.at( i ), mRotationPoint, rotation ) );
223+
mRotationPreviewBox->addPoint( rotatePointClockwise( boxPoints.at( i ), mRotationPoint, rotation ) );
222224
}
223-
mRotationPreviewBox->addPoint( rotatePointCounterClockwise( boxPoints.at( 0 ), mRotationPoint, rotation ) );
225+
mRotationPreviewBox->addPoint( rotatePointClockwise( boxPoints.at( 0 ), mRotationPoint, rotation ) );
224226
mRotationPreviewBox->show();
225227
}
226228

227-
QgsPointXY QgsMapToolRotateLabel::rotatePointCounterClockwise( const QgsPointXY &input, const QgsPointXY &centerPoint, double degrees )
229+
QgsPointXY QgsMapToolRotateLabel::rotatePointClockwise( const QgsPointXY &input, const QgsPointXY &centerPoint, double degrees ) const
228230
{
229-
double rad = degrees / 180 * M_PI;
231+
double rad = -degrees / 180 * M_PI;
230232
double v1x = input.x() - centerPoint.x();
231233
double v1y = input.y() - centerPoint.y();
232234

src/app/qgsmaptoolrotatelabel.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,14 @@ class APP_EXPORT QgsMapToolRotateLabel: public QgsMapToolLabel
3737
protected:
3838

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

4343
QgsRubberBand *createRotationPreviewBox();
4444
void setRotationPreviewBox( double rotation );
4545

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

4949
double mStartRotation; //rotation value prior to start rotating
5050
double mCurrentRotation;

src/app/qgspointrotationitem.cpp

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,10 @@ QgsPointRotationItem::QgsPointRotationItem( QgsMapCanvas *canvas )
2525
//setup font
2626
mFont.setPointSize( 12 );
2727
mFont.setBold( true );
28-
}
29-
30-
QgsPointRotationItem::QgsPointRotationItem()
31-
: QgsMapCanvasItem( nullptr )
32-
, mOrientation( Clockwise )
33-
, mRotation( 0.0 )
34-
{
3528

29+
QImage im( 24, 24, QImage::Format_ARGB32 );
30+
im.fill( Qt::transparent );
31+
setSymbol( im );
3632
}
3733

3834
QgsPointRotationItem::~QgsPointRotationItem()

src/app/qgspointrotationitem.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ class APP_EXPORT QgsPointRotationItem: public QgsMapCanvasItem
5151
Orientation orientation() const { return mOrientation; }
5252

5353
private:
54-
QgsPointRotationItem();
54+
5555
//! Converts rotation into QPainter rotation considering mOrientation
5656
int painterRotation( int rotation ) const;
5757
//! Clockwise (default) or counterclockwise

0 commit comments

Comments
 (0)