/
qgsvertextool.h
497 lines (365 loc) · 19.1 KB
/
qgsvertextool.h
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
/***************************************************************************
qgsvertextool.h
--------------------------------------
Date : February 2017
Copyright : (C) 2017 by Martin Dobias
Email : wonder dot sk at gmail dot com
***************************************************************************
* *
* 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. *
* *
***************************************************************************/
#ifndef QGSVERTEXTOOL_H
#define QGSVERTEXTOOL_H
#include <memory>
#include <QPointer>
#include "qgis_app.h"
#include "qgsmaptooladvanceddigitizing.h"
#include "qgsgeometry.h"
#include "qgspointlocator.h"
class QRubberBand;
class QgsGeometryValidator;
class QgsVertexEditor;
class QgsLockedFeature;
class QgsSnapIndicator;
class QgsVertexMarker;
//! helper structure for a vertex being dragged
struct Vertex
{
Vertex( QgsVectorLayer *layer, QgsFeatureId fid, int vertexId )
: layer( layer )
, fid( fid )
, vertexId( vertexId ) {}
bool operator==( const Vertex &other ) const
{
return layer == other.layer && fid == other.fid && vertexId == other.vertexId;
}
bool operator!=( const Vertex &other ) const
{
return !operator==( other );
}
QgsVectorLayer *layer = nullptr;
QgsFeatureId fid;
int vertexId;
};
//! qHash implementation - we use Vertex in QSet
uint qHash( const Vertex &v );
class APP_EXPORT QgsVertexTool : public QgsMapToolAdvancedDigitizing
{
Q_OBJECT
public:
enum VertexToolMode
{
ActiveLayer,
AllLayers
};
Q_ENUM( VertexToolMode )
QgsVertexTool( QgsMapCanvas *canvas, QgsAdvancedDigitizingDockWidget *cadDock, VertexToolMode mode = QgsVertexTool::AllLayers );
//! Cleanup canvas items we have created
~QgsVertexTool() override;
void cadCanvasPressEvent( QgsMapMouseEvent *e ) override;
void cadCanvasReleaseEvent( QgsMapMouseEvent *e ) override;
void cadCanvasMoveEvent( QgsMapMouseEvent *e ) override;
//! Start addition of a new vertex on double-click
void canvasDoubleClickEvent( QgsMapMouseEvent *e ) override;
void activate() override;
void deactivate() override;
void keyPressEvent( QKeyEvent *e ) override;
QgsGeometry cachedGeometry( const QgsVectorLayer *layer, QgsFeatureId fid );
//! Toggle the vertex editor
void showVertexEditor(); //#spellok
//! Update vertex editor to show feature from the given match
void updateVertexEditor( QgsVectorLayer *layer, QgsFeatureId fid );
private slots:
//! update geometry of our feature
void onCachedGeometryChanged( QgsFeatureId fid, const QgsGeometry &geom );
void onCachedGeometryDeleted( QgsFeatureId fid );
void clearGeometryCache();
void deleteVertexEditorSelection();
void validationErrorFound( const QgsGeometry::Error &e );
void validationFinished();
void startRangeVertexSelection();
void cleanEditor( QgsFeatureId id );
void lockedFeatureSelectionChanged();
private:
void buildDragBandsForVertices( const QSet<Vertex> &movingVertices, const QgsPointXY &dragVertexMapPoint );
void addDragBand( const QgsPointXY &v1, const QgsPointXY &v2 );
void addDragStraightBand( QgsVectorLayer *layer, QgsPointXY v0, QgsPointXY v1, bool moving0, bool moving1, const QgsPointXY &mapPoint );
void addDragCircularBand( QgsVectorLayer *layer, QgsPointXY v0, QgsPointXY v1, QgsPointXY v2, bool moving0, bool moving1, bool moving2, const QgsPointXY &mapPoint );
void moveDragBands( const QgsPointXY &mapPoint );
void clearDragBands();
void mouseMoveDraggingVertex( QgsMapMouseEvent *e );
void mouseMoveDraggingEdge( QgsMapMouseEvent *e );
void removeTemporaryRubberBands();
void cleanupVertexEditor();
void cleanupLockedFeature();
/**
* Temporarily override snapping config and snap to vertices and edges
of any editable vector layer, to allow selection of vertex for editing
(if snapped to edge, it would offer creation of a new vertex there).
*/
QgsPointLocator::Match snapToEditableLayer( QgsMapMouseEvent *e );
/**
* Tries to find a match in polygon interiors. This is useful for mouse move
* events to keep features highlighted to see their area.
*/
QgsPointLocator::Match snapToPolygonInterior( QgsMapMouseEvent *e );
/**
* Returns a list of all matches at the given map point. That is a concatenation
* of all vertex, edge and area matches (vertex/edge matches using standard search tolerance).
* Layer is only searched if it is editable.
*/
QList<QgsPointLocator::Match> findEditableLayerMatches( const QgsPointXY &mapPoint, QgsVectorLayer *layer );
/**
* Returns a set of all matches at the given map point from all editable layers (respecting the mode).
* The set does not contain only the closest match from each layer, but all matches in the standard
* vertex search tolerance. It also includes area matches.
*/
QSet<QPair<QgsVectorLayer *, QgsFeatureId> > findAllEditableFeatures( const QgsPointXY &mapPoint );
/**
* Implements behavior for mouse right-click to select a feature for editing (and in case of multiple
* features in one place, repeated right-clicks will cycle through the features).
*/
void tryToSelectFeature( QgsMapMouseEvent *e );
//! check whether we are still close to the mEndpointMarker
bool isNearEndpointMarker( const QgsPointXY &mapPoint );
bool isMatchAtEndpoint( const QgsPointLocator::Match &match );
QgsPointXY positionForEndpointMarker( const QgsPointLocator::Match &match );
void mouseMoveNotDragging( QgsMapMouseEvent *e );
QgsGeometry cachedGeometryForVertex( const Vertex &vertex );
void startDragging( QgsMapMouseEvent *e );
void startDraggingMoveVertex( const QgsPointLocator::Match &m );
//! Gets list of matches of all vertices of a layer exactly snapped to a map point
QList<QgsPointLocator::Match> layerVerticesSnappedToPoint( QgsVectorLayer *layer, const QgsPointXY &mapPoint );
//! Gets list of matches of all segments of a layer coincident with the given segment
QList<QgsPointLocator::Match> layerSegmentsSnappedToSegment( QgsVectorLayer *layer, const QgsPointXY &mapPoint1, const QgsPointXY &mapPoint2 );
void startDraggingAddVertex( const QgsPointLocator::Match &m );
void startDraggingAddVertexAtEndpoint( const QgsPointXY &mapPoint );
void startDraggingEdge( const QgsPointLocator::Match &m, const QgsPointXY &mapPoint );
void stopDragging();
QgsPoint matchToLayerPoint( const QgsVectorLayer *destLayer, const QgsPointXY &mapPoint, const QgsPointLocator::Match *match );
//! Finish moving of an edge
void moveEdge( const QgsPointXY &mapPoint );
void moveVertex( const QgsPointXY &mapPoint, const QgsPointLocator::Match *mapPointMatch );
void deleteVertex();
typedef QHash<QgsVectorLayer *, QHash<QgsFeatureId, QgsGeometry> > VertexEdits;
void addExtraVerticesToEdits( VertexEdits &edits, const QgsPointXY &mapPoint, QgsVectorLayer *dragLayer = nullptr, const QgsPoint &layerPoint = QgsPoint() );
void addExtraSegmentsToEdits( QgsVertexTool::VertexEdits &edits, const QgsPointXY &mapPoint, QgsVectorLayer *dragLayer, const QgsPoint &layerPoint );
void applyEditsToLayers( VertexEdits &edits );
/**
* For the given set of vertices (possibly from multiple layers) find any another vertices that are coincident with
* them and not yet included in the set. This is used for topological editing to find all vertices that should be moved.
*/
QSet<Vertex> findCoincidentVertices( const QSet<Vertex> &vertices );
/**
* For the given set of vertices, prepare mDraggingExtraVertices and mDraggingExtraVerticesOffset arrays.
* The parameters anchorPoint and anchorLayer are used to calculate offsets.
*/
void buildExtraVertices( const QSet<Vertex> &vertices, const QgsPointXY &anchorPoint, QgsVectorLayer *anchorLayer );
//! Returns a list of canvas layers filtered to just editable spatial vector layers
QList<QgsVectorLayer *> editableVectorLayers();
enum HighlightMode
{
ModeReset, //!< Reset any current selection
ModeAdd, //!< Add to current selection
ModeSubtract, //!< Remove from current selection
};
void setHighlightedVertices( const QList<Vertex> &listVertices, HighlightMode mode = ModeReset );
void setHighlightedVerticesVisible( bool visible );
//! Allow moving back and forth selected vertex within a feature
void highlightAdjacentVertex( double offset );
//! Initialize the rubberband for vertex selection using a rectangle
void initSelectionRubberBand();
//! Update the rubberband for vertex selection using a rectangle
void updateSelectionRubberBand( QgsMapMouseEvent *e );
void stopSelectionRubberBand();
/**
* Using a given edge match and original map point, find out
* center of the edge and whether we are close enough to the center
*/
bool matchEdgeCenterTest( const QgsPointLocator::Match &m, const QgsPointXY &mapPoint, QgsPointXY *edgeCenterPtr = nullptr );
//! Run validation on a geometry (in a background thread)
void validateGeometry( QgsVectorLayer *layer, QgsFeatureId featureId );
//! Makes sure that the vertex is visible in map canvas
void zoomToVertex( const Vertex &vertex );
//! Returns a list of vertices between the two given vertex indices (including those)
QList<Vertex> verticesInRange( QgsVectorLayer *layer, QgsFeatureId fid, int vertexId0, int vertexId1, bool longWay );
void updateFeatureBand( const QgsPointLocator::Match &m );
//! Updates vertex band based on the current match
void updateVertexBand( const QgsPointLocator::Match &m );
//! Handles mouse press event when in range selection method
void rangeMethodPressEvent( QgsMapMouseEvent *e );
//! Handles mouse release event when in range selection method
void rangeMethodReleaseEvent( QgsMapMouseEvent *e );
//! Handles mouse move event when in range selection method
void rangeMethodMoveEvent( QgsMapMouseEvent *e );
void stopRangeVertexSelection();
//! update the highlight of vertices from the locked feature
void updateLockedFeatureVertices();
private:
// members used for temporary highlight of stuff
//! marker of a snap match (if any) when dragging a vertex
std::unique_ptr<QgsSnapIndicator> mSnapIndicator;
/**
* marker in the middle of an edge while pointer is close to a vertex and not dragging anything
* (highlighting point that can be clicked to add a new vertex)
*/
QgsVertexMarker *mEdgeCenterMarker = nullptr;
//! rubber band for highlight of a whole feature on mouse over and not dragging anything
QgsRubberBand *mFeatureBand = nullptr;
//! rubber band for highlight of all vertices of a feature on mouse over and not dragging anything, also used for locked feature vertices
QgsRubberBand *mFeatureBandMarkers = nullptr;
//! source layer for mFeatureBand (null if mFeatureBand is null)
const QgsVectorLayer *mFeatureBandLayer = nullptr;
//! source feature id for mFeatureBand (zero if mFeatureBand is null)
QgsFeatureId mFeatureBandFid = 0;
//! highlight of a vertex while mouse pointer is close to a vertex and not dragging anything
QgsRubberBand *mVertexBand = nullptr;
//! highlight of an edge while mouse pointer is close to an edge and not dragging anything
QgsRubberBand *mEdgeBand = nullptr;
//! highlight of locked feature vertices (but not selected)
QList<QgsVertexMarker *> mLockedFeatureVerticesMarkers;
// members for dragging operation
enum DraggingVertexType
{
NotDragging,
MovingVertex,
AddingVertex,
AddingEndpoint,
};
/**
* markers for points used only for moving standalone point geoetry
* (there are no adjacent vertices so it is not used in mDragBands)
*/
QList<QgsVertexMarker *> mDragPointMarkers;
/**
* companion array to mDragPointMarkers: for each marker it keeps offset
* (in map units) from the position of the main vertex
*/
QList<QgsVector> mDragPointMarkersOffset;
//! structure to keep information about a rubber band used for dragging of a straight line segment
struct StraightBand
{
QgsRubberBand *band = nullptr; //!< Pointer to the actual rubber band
QgsPointXY p0, p1; //!< What are the original positions of points (in map units)
bool moving0, moving1; //!< Which points of the band are moving with mouse cursor
QgsVector offset0, offset1; //!< If the point is moving, what is the offset from the mouse cursor
};
//! structure to keep information about a rubber band used for dragging of a circular segment
struct CircularBand
{
QgsRubberBand *band = nullptr; //!< Pointer to the actual rubber band
QgsPointXY p0, p1, p2; //!< What are the original positions of points (in map units)
bool moving0, moving1, moving2; //!< Which points of the band are moving with mouse cursor
QgsVector offset0, offset1, offset2; //!< If the point is moving, what is the offset from the mouse cursor
//! update geometry of the rubber band band on the current mouse cursor position (in map units)
void updateRubberBand( const QgsPointXY &mapPoint );
};
//! list of active straight line rubber bands
QList<StraightBand> mDragStraightBands;
//! list of active rubber bands for circular segments
QList<CircularBand> mDragCircularBands;
//! instance of Vertex that is being currently moved or nothing
std::unique_ptr<Vertex> mDraggingVertex;
//! whether moving a vertex or adding one
DraggingVertexType mDraggingVertexType = NotDragging;
//! whether we are currently dragging an edge
bool mDraggingEdge = false;
/**
* list of Vertex instances of further vertices that are dragged together with
* the main vertex (mDraggingVertex) - either topologically connected points
* (if topo editing is allowed) or the ones coming from the highlight
*/
QList<Vertex> mDraggingExtraVertices;
/**
* companion array to mDraggingExtraVertices: for each vertex in mDraggingExtraVertices
* this is offset (in units of the layer) of the vertex from the position of the main
* vertex (mDraggingVertex)
*/
QList<QgsVector> mDraggingExtraVerticesOffset;
/**
* list of Vertex instances identifying segments (by their first vertex index) that should
* also get a new vertex: this is used for topo editing when adding a vertex to existing segment
*/
QList<Vertex> mDraggingExtraSegments;
// members for selection handling
//! list of Vertex instances of vertices that are selected
QList<Vertex> mSelectedVertices;
//! list of vertex markers
QList<QgsVertexMarker *> mSelectedVerticesMarkers;
// members for rectangle for selection
//! the rubberband for rectangle selection visualization
std::unique_ptr<QgsRubberBand> mSelectionRubberBand;
//! Initial point (in screen coordinates) when using rectangle selection
std::unique_ptr<QPoint> mSelectionRubberBandStartPos;
// members for addition of vertices at the end of a curve
//! Vertex instance or None
std::unique_ptr<Vertex> mMouseAtEndpoint;
//! QgsPointXY or None (can't get center from QgsVertexMarker)
std::unique_ptr<QgsPointXY> mEndpointMarkerCenter;
/**
* marker shown near the end of a curve to suggest that the user
* may add a new vertex at the end of the curve
*/
QgsVertexMarker *mEndpointMarker = nullptr;
/**
* keeps information about previously used snap match
* to stick with the same highlighted feature next time if there are more options
*/
std::unique_ptr<QgsPointLocator::Match> mLastSnap;
/**
* When double-clicking to add a new vertex, this member keeps the snap
* match from "press" event used to be used in following "release" event
*/
std::unique_ptr<QgsPointLocator::Match> mNewVertexFromDoubleClick;
//! Geometry cache for fast access to geometries (coordinates are in their layer's CRS)
QHash<const QgsVectorLayer *, QHash<QgsFeatureId, QgsGeometry> > mCache;
// support for vertex editor
//! Locked feature for the vertex editor
std::unique_ptr<QgsLockedFeature> mLockedFeature;
//! Dock widget which allows editing vertices
QPointer<QgsVertexEditor> mVertexEditor;
/**
* Data structure that stores alternative features to the currently selected (locked) feature.
* This is used when user clicks with right mouse button multiple times in one location
* to easily switch to the desired feature.
*/
struct LockedFeatureAlternatives
{
QPoint screenPoint;
QList< QPair<QgsVectorLayer *, QgsFeatureId> > alternatives;
int index = -1;
};
//! Keeps information about other possible features to select with right click. Null if no info is currently held.
std::unique_ptr<LockedFeatureAlternatives> mLockedFeatureAlternatives;
// support for validation of geometries
//! data structure for validation of one geometry of a vector layer
struct GeometryValidation
{
QgsVertexTool *tool = nullptr; //!< Pointer to the parent vertex tool (for connections / canvas)
QgsVectorLayer *layer = nullptr; //!< Pointer to the layer of the validated geometry (for reporojection)
QgsGeometryValidator *validator = nullptr; //!< Object that does validation. Non-null if active
QList<QgsVertexMarker *> errorMarkers; //!< Markers created by validation
QString errors; //!< Full error text from validation
void start( QgsGeometry &geom, QgsVertexTool *tool, QgsVectorLayer *l ); //!< Start validation
void addError( QgsGeometry::Error e ); //!< Add another error to the validation
void cleanup(); //!< Delete everything
};
//! data structure to keep validation details
QHash< QPair<QgsVectorLayer *, QgsFeatureId>, GeometryValidation> mValidations;
//! Enumeration of methods for selection of vertices
enum VertexSelectionMethod
{
SelectionNormal, //!< Default selection: clicking vertex starts move, ctrl+click selects vertex, dragging rectangle select multiple vertices
SelectionRange, //!< Range selection: clicking selects start vertex, next click select final vertex, vertices in the range get selected
};
//! Current vertex selection method
VertexSelectionMethod mSelectionMethod = SelectionNormal;
//! Starting vertex when using range selection (null if not yet selected)
std::unique_ptr<Vertex> mRangeSelectionFirstVertex;
VertexToolMode mMode = AllLayers;
};
#endif // QGSVERTEXTOOL_H