24
24
25
25
QgsMapToolMoveLabel::QgsMapToolMoveLabel ( QgsMapCanvas *canvas )
26
26
: QgsMapToolLabel( canvas )
27
- , mClickOffsetX( 0 )
28
- , mClickOffsetY( 0 )
29
27
{
30
28
mToolName = tr ( " Move label" );
31
29
@@ -36,69 +34,6 @@ QgsMapToolMoveLabel::QgsMapToolMoveLabel( QgsMapCanvas *canvas )
36
34
mDiagramProperties << QgsDiagramLayerSettings::PositionY;
37
35
}
38
36
39
- void QgsMapToolMoveLabel::canvasPressEvent ( QgsMapMouseEvent *e )
40
- {
41
- deleteRubberBands ();
42
-
43
- QgsLabelPosition labelPos;
44
- if ( !labelAtPosition ( e, labelPos ) )
45
- {
46
- mCurrentLabel = LabelDetails ();
47
- return ;
48
- }
49
-
50
- mCurrentLabel = LabelDetails ( labelPos );
51
-
52
- QgsVectorLayer *vlayer = mCurrentLabel .layer ;
53
- if ( !vlayer )
54
- {
55
- return ;
56
- }
57
-
58
- int xCol = -1 , yCol = -1 ;
59
-
60
- if ( !mCurrentLabel .pos .isDiagram && !labelMoveable ( vlayer, mCurrentLabel .settings , xCol, yCol ) )
61
- {
62
- QgsPalIndexes indexes;
63
-
64
- if ( createAuxiliaryFields ( indexes ) )
65
- return ;
66
-
67
- if ( !labelMoveable ( vlayer, mCurrentLabel .settings , xCol, yCol ) )
68
- return ;
69
-
70
- xCol = indexes[ QgsPalLayerSettings::PositionX ];
71
- yCol = indexes[ QgsPalLayerSettings::PositionY ];
72
- }
73
- else if ( mCurrentLabel .pos .isDiagram && !diagramMoveable ( vlayer, xCol, yCol ) )
74
- {
75
- QgsDiagramIndexes indexes;
76
-
77
- if ( createAuxiliaryFields ( indexes ) )
78
- return ;
79
-
80
- if ( !diagramMoveable ( vlayer, xCol, yCol ) )
81
- return ;
82
-
83
- xCol = indexes[ QgsDiagramLayerSettings::PositionX ];
84
- yCol = indexes[ QgsDiagramLayerSettings::PositionY ];
85
- }
86
-
87
- if ( xCol >= 0 && yCol >= 0 )
88
- {
89
- mStartPointMapCoords = toMapCoordinates ( e->pos () );
90
- QgsPointXY referencePoint;
91
- if ( !currentLabelRotationPoint ( referencePoint, !currentLabelPreserveRotation (), false ) )
92
- {
93
- referencePoint.setX ( mCurrentLabel .pos .labelRect .xMinimum () );
94
- referencePoint.setY ( mCurrentLabel .pos .labelRect .yMinimum () );
95
- }
96
- mClickOffsetX = mStartPointMapCoords .x () - referencePoint.x ();
97
- mClickOffsetY = mStartPointMapCoords .y () - referencePoint.y ();
98
- createRubberBands ();
99
- }
100
- }
101
-
102
37
void QgsMapToolMoveLabel::canvasMoveEvent ( QgsMapMouseEvent *e )
103
38
{
104
39
if ( mLabelRubberBand )
@@ -119,80 +54,169 @@ void QgsMapToolMoveLabel::canvasReleaseEvent( QgsMapMouseEvent *e )
119
54
{
120
55
if ( !mLabelRubberBand )
121
56
{
122
- return ;
123
- }
57
+ if ( e-> button () != Qt::LeftButton )
58
+ return ;
124
59
125
- deleteRubberBands ();
60
+ // first click starts move
61
+ deleteRubberBands ();
126
62
127
- QgsVectorLayer *vlayer = mCurrentLabel .layer ;
128
- if ( !vlayer )
129
- {
130
- return ;
131
- }
63
+ QgsLabelPosition labelPos;
64
+ if ( !labelAtPosition ( e, labelPos ) )
65
+ {
66
+ mCurrentLabel = LabelDetails ();
67
+ return ;
68
+ }
132
69
133
- QgsPointXY releaseCoords = toMapCoordinates ( e->pos () );
134
- double xdiff = releaseCoords.x () - mStartPointMapCoords .x ();
135
- double ydiff = releaseCoords.y () - mStartPointMapCoords .y ();
70
+ mCurrentLabel = LabelDetails ( labelPos );
136
71
137
- int xCol, yCol;
138
- double xPosOrig, yPosOrig;
139
- bool xSuccess, ySuccess;
72
+ QgsVectorLayer *vlayer = mCurrentLabel .layer ;
73
+ if ( !vlayer )
74
+ {
75
+ return ;
76
+ }
140
77
141
- if ( !currentLabelDataDefinedPosition ( xPosOrig, xSuccess, yPosOrig, ySuccess, xCol, yCol ) )
142
- {
143
- return ;
144
- }
78
+ int xCol = -1 , yCol = -1 ;
145
79
146
- double xPosNew, yPosNew;
80
+ if ( !mCurrentLabel .pos .isDiagram && !labelMoveable ( vlayer, mCurrentLabel .settings , xCol, yCol ) )
81
+ {
82
+ QgsPalIndexes indexes;
147
83
148
- if ( !xSuccess || !ySuccess )
149
- {
150
- xPosNew = releaseCoords.x () - mClickOffsetX ;
151
- yPosNew = releaseCoords.y () - mClickOffsetY ;
152
- }
153
- else
154
- {
155
- // transform to map crs first, because xdiff,ydiff are in map coordinates
156
- const QgsMapSettings &ms = mCanvas ->mapSettings ();
157
- QgsPointXY transformedPoint = ms.layerToMapCoordinates ( vlayer, QgsPointXY ( xPosOrig, yPosOrig ) );
158
- xPosOrig = transformedPoint.x ();
159
- yPosOrig = transformedPoint.y ();
160
- xPosNew = xPosOrig + xdiff;
161
- yPosNew = yPosOrig + ydiff;
162
- }
84
+ if ( createAuxiliaryFields ( indexes ) )
85
+ return ;
163
86
164
- // transform back to layer crs
165
- if ( mCanvas )
166
- {
167
- const QgsMapSettings &s = mCanvas ->mapSettings ();
168
- QgsPointXY transformedPoint = s.mapToLayerCoordinates ( vlayer, QgsPointXY ( xPosNew, yPosNew ) );
169
- xPosNew = transformedPoint.x ();
170
- yPosNew = transformedPoint.y ();
171
- }
87
+ if ( !labelMoveable ( vlayer, mCurrentLabel .settings , xCol, yCol ) )
88
+ return ;
89
+
90
+ xCol = indexes[ QgsPalLayerSettings::PositionX ];
91
+ yCol = indexes[ QgsPalLayerSettings::PositionY ];
92
+ }
93
+ else if ( mCurrentLabel .pos .isDiagram && !diagramMoveable ( vlayer, xCol, yCol ) )
94
+ {
95
+ QgsDiagramIndexes indexes;
96
+
97
+ if ( createAuxiliaryFields ( indexes ) )
98
+ return ;
172
99
173
- vlayer->beginEditCommand ( tr ( " Moved label" ) + QStringLiteral ( " '%1'" ).arg ( currentLabelText ( 24 ) ) );
174
- vlayer->changeAttributeValue ( mCurrentLabel .pos .featureId , xCol, xPosNew );
175
- vlayer->changeAttributeValue ( mCurrentLabel .pos .featureId , yCol, yPosNew );
100
+ if ( !diagramMoveable ( vlayer, xCol, yCol ) )
101
+ return ;
102
+
103
+ xCol = indexes[ QgsDiagramLayerSettings::PositionX ];
104
+ yCol = indexes[ QgsDiagramLayerSettings::PositionY ];
105
+ }
176
106
177
- // set rotation to that of label, if data-defined and no rotation set yet
178
- // honor whether to preserve preexisting data on pin
179
- // must come after setting x and y positions
180
- if ( !mCurrentLabel .pos .isDiagram
181
- && !mCurrentLabel .pos .isPinned
182
- && !currentLabelPreserveRotation () )
107
+ if ( xCol >= 0 && yCol >= 0 )
108
+ {
109
+ mStartPointMapCoords = toMapCoordinates ( e->pos () );
110
+ QgsPointXY referencePoint;
111
+ if ( !currentLabelRotationPoint ( referencePoint, !currentLabelPreserveRotation (), false ) )
112
+ {
113
+ referencePoint.setX ( mCurrentLabel .pos .labelRect .xMinimum () );
114
+ referencePoint.setY ( mCurrentLabel .pos .labelRect .yMinimum () );
115
+ }
116
+ mClickOffsetX = mStartPointMapCoords .x () - referencePoint.x ();
117
+ mClickOffsetY = mStartPointMapCoords .y () - referencePoint.y ();
118
+ createRubberBands ();
119
+ }
120
+ }
121
+ else
183
122
{
184
- double defRot;
185
- bool rSuccess;
186
- int rCol;
187
- if ( currentLabelDataDefinedRotation ( defRot, rSuccess, rCol ) )
123
+ switch ( e->button () )
188
124
{
189
- double labelRot = mCurrentLabel .pos .rotation * 180 / M_PI;
190
- vlayer->changeAttributeValue ( mCurrentLabel .pos .featureId , rCol, labelRot );
125
+ case Qt::RightButton:
126
+ {
127
+ // right click is cancel
128
+ deleteRubberBands ();
129
+ return ;
130
+ }
131
+
132
+ case Qt::LeftButton:
133
+ {
134
+ // second click drops label
135
+ deleteRubberBands ();
136
+ QgsVectorLayer *vlayer = mCurrentLabel .layer ;
137
+ if ( !vlayer )
138
+ {
139
+ return ;
140
+ }
141
+
142
+ QgsPointXY releaseCoords = toMapCoordinates ( e->pos () );
143
+ double xdiff = releaseCoords.x () - mStartPointMapCoords .x ();
144
+ double ydiff = releaseCoords.y () - mStartPointMapCoords .y ();
145
+
146
+ int xCol, yCol;
147
+ double xPosOrig, yPosOrig;
148
+ bool xSuccess, ySuccess;
149
+
150
+ if ( !currentLabelDataDefinedPosition ( xPosOrig, xSuccess, yPosOrig, ySuccess, xCol, yCol ) )
151
+ {
152
+ return ;
153
+ }
154
+
155
+ double xPosNew, yPosNew;
156
+
157
+ if ( !xSuccess || !ySuccess )
158
+ {
159
+ xPosNew = releaseCoords.x () - mClickOffsetX ;
160
+ yPosNew = releaseCoords.y () - mClickOffsetY ;
161
+ }
162
+ else
163
+ {
164
+ // transform to map crs first, because xdiff,ydiff are in map coordinates
165
+ const QgsMapSettings &ms = mCanvas ->mapSettings ();
166
+ QgsPointXY transformedPoint = ms.layerToMapCoordinates ( vlayer, QgsPointXY ( xPosOrig, yPosOrig ) );
167
+ xPosOrig = transformedPoint.x ();
168
+ yPosOrig = transformedPoint.y ();
169
+ xPosNew = xPosOrig + xdiff;
170
+ yPosNew = yPosOrig + ydiff;
171
+ }
172
+
173
+ // transform back to layer crs
174
+ if ( mCanvas )
175
+ {
176
+ const QgsMapSettings &s = mCanvas ->mapSettings ();
177
+ QgsPointXY transformedPoint = s.mapToLayerCoordinates ( vlayer, QgsPointXY ( xPosNew, yPosNew ) );
178
+ xPosNew = transformedPoint.x ();
179
+ yPosNew = transformedPoint.y ();
180
+ }
181
+
182
+ vlayer->beginEditCommand ( tr ( " Moved label" ) + QStringLiteral ( " '%1'" ).arg ( currentLabelText ( 24 ) ) );
183
+ vlayer->changeAttributeValue ( mCurrentLabel .pos .featureId , xCol, xPosNew );
184
+ vlayer->changeAttributeValue ( mCurrentLabel .pos .featureId , yCol, yPosNew );
185
+
186
+ // set rotation to that of label, if data-defined and no rotation set yet
187
+ // honor whether to preserve preexisting data on pin
188
+ // must come after setting x and y positions
189
+ if ( !mCurrentLabel .pos .isDiagram
190
+ && !mCurrentLabel .pos .isPinned
191
+ && !currentLabelPreserveRotation () )
192
+ {
193
+ double defRot;
194
+ bool rSuccess;
195
+ int rCol;
196
+ if ( currentLabelDataDefinedRotation ( defRot, rSuccess, rCol ) )
197
+ {
198
+ double labelRot = mCurrentLabel .pos .rotation * 180 / M_PI;
199
+ vlayer->changeAttributeValue ( mCurrentLabel .pos .featureId , rCol, labelRot );
200
+ }
201
+ }
202
+ vlayer->endEditCommand ();
203
+
204
+ vlayer->triggerRepaint ();
205
+ break ;
206
+ }
207
+ default :
208
+ break ;
191
209
}
192
210
}
193
- vlayer-> endEditCommand ();
211
+ }
194
212
195
- vlayer->triggerRepaint ();
213
+ void QgsMapToolMoveLabel::keyReleaseEvent ( QKeyEvent *e )
214
+ {
215
+ if ( mLabelRubberBand && e->key () == Qt::Key_Escape )
216
+ {
217
+ // escape is cancel
218
+ deleteRubberBands ();
219
+ }
196
220
}
197
221
198
222
0 commit comments