/
qgsmaptoolrotatelabel.cpp
246 lines (207 loc) · 7.36 KB
/
qgsmaptoolrotatelabel.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
/***************************************************************************
qgsmaptoolrotatelabel.cpp
-------------------------
begin : 2010-11-09
copyright : (C) 2010 by Marco Hugentobler
email : marco dot hugentobler at sourcepole dot ch
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "qgsmaptoolrotatelabel.h"
#include "qgsmapcanvas.h"
#include "qgsmaplayerregistry.h"
#include "qgspallabeling.h"
#include "qgspointrotationitem.h"
#include "qgsrubberband.h"
#include "qgsvectorlayer.h"
#include <QMouseEvent>
#include "qgisapp.h"
#include "qgsapplication.h"
QgsMapToolRotateLabel::QgsMapToolRotateLabel( QgsMapCanvas* canvas )
: QgsMapToolLabel( canvas )
, mStartRotation( 0.0 )
, mCurrentRotation( 0.0 )
, mCurrentMouseAzimuth( 0.0 )
, mRotationItem( 0 )
, mRotationPreviewBox( 0 )
, mCtrlPressed( false )
{
}
QgsMapToolRotateLabel::~QgsMapToolRotateLabel()
{
delete mRotationItem;
delete mRotationPreviewBox;
}
void QgsMapToolRotateLabel::canvasPressEvent( QMouseEvent *e )
{
deleteRubberBands();
if ( !labelAtPosition( e, mCurrentLabelPos ) )
{
return;
}
QgsVectorLayer* vlayer = dynamic_cast<QgsVectorLayer*>( QgsMapLayerRegistry::instance()->mapLayer( mCurrentLabelPos.layerID ) );
if ( !vlayer )
{
return;
}
// only rotate non-pinned OverPoint placements until other placements are supported in pal::Feature
bool labelSettingsOk;
QgsPalLayerSettings& layerSettings = currentLabelSettings( &labelSettingsOk );
if ( !mCurrentLabelPos.isPinned && labelSettingsOk
&& layerSettings.placement != QgsPalLayerSettings::OverPoint )
{
return;
}
// rotate unpinned labels (i.e. no hali/vali settings) as if hali/vali was Center/Half
if ( !rotationPoint( mRotationPoint, false, !mCurrentLabelPos.isPinned ) )
{
return;
}
int rotationCol;
if ( layerIsRotatable( vlayer, rotationCol ) )
{
mCurrentMouseAzimuth = azimuthToCCW( mRotationPoint.azimuth( toMapCoordinates( e->pos() ) ) );
bool hasRotationValue;
if ( dataDefinedRotation( vlayer, mCurrentLabelPos.featureId, mCurrentRotation, hasRotationValue, true ) )
{
if ( !hasRotationValue )
{
mCurrentRotation = 0;
}
mStartRotation = mCurrentRotation;
createRubberBands();
mRotationPreviewBox = createRotationPreviewBox();
mRotationItem = new QgsPointRotationItem( mCanvas );
mRotationItem->setOrientation( QgsPointRotationItem::Counterclockwise );
mRotationItem->setSymbol( QgsApplication::getThemePixmap( "mActionRotatePointSymbols.png" ).toImage() );
mRotationItem->setPointLocation( mRotationPoint );
mRotationItem->setSymbolRotation( mCurrentRotation );
}
}
}
void QgsMapToolRotateLabel::canvasMoveEvent( QMouseEvent *e )
{
if ( mLabelRubberBand )
{
QgsPoint currentPoint = toMapCoordinates( e->pos() );
double azimuth = azimuthToCCW( 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;
mCurrentMouseAzimuth = azimuth - static_cast<float>( static_cast<int>( azimuth / 360 ) ) * 360;
//if shift-modifier is pressed, round to 15 degrees
int displayValue;
if ( e->modifiers() & Qt::ControlModifier )
{
displayValue = roundTo15Degrees( mCurrentRotation );
mCtrlPressed = true;
}
else
{
displayValue = ( int )( mCurrentRotation );
mCtrlPressed = false;
}
if ( mRotationItem )
{
mRotationItem->setSymbolRotation( displayValue );
setRotationPreviewBox( displayValue - mStartRotation );
mRotationItem->update();
}
}
}
void QgsMapToolRotateLabel::canvasReleaseEvent( QMouseEvent *e )
{
Q_UNUSED( e );
if ( !mLabelRubberBand ) //no rubber band created (most likely because the current label cannot be rotated )
{
return;
}
deleteRubberBands();
delete mRotationItem;
mRotationItem = 0;
delete mRotationPreviewBox;
mRotationPreviewBox = 0;
QgsMapLayer* layer = QgsMapLayerRegistry::instance()->mapLayer( mCurrentLabelPos.layerID );
if ( !layer )
{
return;
}
QgsVectorLayer* vlayer = dynamic_cast<QgsVectorLayer*>( layer );
if ( !vlayer )
{
return;
}
int rotationCol;
if ( !layerIsRotatable( vlayer, rotationCol ) )
{
return;
}
double rotation = mCtrlPressed ? roundTo15Degrees( mCurrentRotation ) : mCurrentRotation;
if ( rotation == mStartRotation ) //mouse button pressed / released, but no rotation
{
return;
}
vlayer->beginEditCommand( tr( "Rotated label" ) + QString( " '%1'" ).arg( currentLabelText( 24 ) ) );
vlayer->changeAttributeValue( mCurrentLabelPos.featureId, rotationCol, rotation );
vlayer->endEditCommand();
mCanvas->refresh();
}
int QgsMapToolRotateLabel::roundTo15Degrees( double n )
{
int m = ( int )( n / 15.0 + 0.5 );
return ( m * 15 );
}
double QgsMapToolRotateLabel::azimuthToCCW( double a )
{
return ( a > 0 ? 360 - a : -a );
}
QgsRubberBand* QgsMapToolRotateLabel::createRotationPreviewBox()
{
delete mRotationPreviewBox;
QVector< QgsPoint > boxPoints = mCurrentLabelPos.cornerPoints;
if ( boxPoints.size() < 1 )
{
return 0;
}
mRotationPreviewBox = new QgsRubberBand( mCanvas, QGis::Line );
mRotationPreviewBox->setColor( QColor( 0, 0, 255, 65 ) );
mRotationPreviewBox->setWidth( 3 );
setRotationPreviewBox( mCurrentRotation - mStartRotation );
return mRotationPreviewBox;
}
void QgsMapToolRotateLabel::setRotationPreviewBox( double rotation )
{
if ( !mRotationPreviewBox )
{
return;
}
mRotationPreviewBox->reset();
QVector< QgsPoint > boxPoints = mCurrentLabelPos.cornerPoints;
if ( boxPoints.size() < 1 )
{
return;
}
for ( int i = 0; i < boxPoints.size(); ++i )
{
mRotationPreviewBox->addPoint( rotatePointCounterClockwise( boxPoints.at( i ), mRotationPoint, rotation ) );
}
mRotationPreviewBox->addPoint( rotatePointCounterClockwise( boxPoints.at( 0 ), mRotationPoint, rotation ) );
mRotationPreviewBox->show();
}
QgsPoint QgsMapToolRotateLabel::rotatePointCounterClockwise( const QgsPoint& input, const QgsPoint& centerPoint, double degrees )
{
double rad = degrees / 180 * M_PI;
double v1x = input.x() - centerPoint.x();
double v1y = input.y() - centerPoint.y();
double v2x = cos( rad ) * v1x - sin( rad ) * v1y;
double v2y = sin( rad ) * v1x + cos( rad ) * v1y;
return QgsPoint( centerPoint.x() + v2x, centerPoint.y() + v2y );
}