Skip to content

Commit 88ce567

Browse files
author
mhugent
committed
Fix for bug #1096 Snapping to currently moved vertex should be suppressed
git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@8477 c8812cc2-4d05-0410-92ff-de0c093fc19c
1 parent 77dfd46 commit 88ce567

File tree

7 files changed

+63
-13
lines changed

7 files changed

+63
-13
lines changed

python/core/qgssnapper.sip

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public:
6363
@param startPoint the start point for snapping (in pixel coordinates)
6464
@param snappingResult the list where the results are inserted (everything in map coordinate system)
6565
@return 0 in case of success*/
66-
int snapPoint(const QPoint& startPoint, QList<QgsSnappingResult>& snappingResult);
66+
int snapPoint(const QPoint& startPoint, QList<QgsSnappingResult>& snappingResult, const QList<QgsPoint>& excludeList);
6767

6868
//setters
6969
void setLayersToSnap(const QList<QgsVectorLayer*>& layerList);

src/app/qgsmaptoolmovevertex.cpp

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ QgsMapToolMoveVertex::~QgsMapToolMoveVertex()
3434

3535
void QgsMapToolMoveVertex::canvasMoveEvent(QMouseEvent * e)
3636
{
37+
if(mRecentSnappingResults.size() < 1)
38+
{
39+
return ; //snapping not necessary
40+
}
41+
3742
//list of rubber bands, snapping results and point index to move
3843
//must have equal size
3944
int rbSize = mRubberBands.size();
@@ -51,7 +56,8 @@ void QgsMapToolMoveVertex::canvasMoveEvent(QMouseEvent * e)
5156
QList<QgsRubberBand*>::iterator rb_it = mRubberBands.begin();
5257

5358
QList<QgsSnappingResult> snapResults;
54-
if(mSnapper.snapToBackgroundLayers(e->pos(), snapResults) != 0)
59+
60+
if(mSnapper.snapToBackgroundLayers(e->pos(), snapResults, mExcludePoint) != 0)
5561
{
5662
return; //error, bail out
5763
}
@@ -119,7 +125,10 @@ void QgsMapToolMoveVertex::canvasPressEvent(QMouseEvent * e)
119125
mRubberBands.push_back(rb);
120126
}
121127

122-
//create rubber band list for snapping results
128+
if(mRecentSnappingResults.size() > 0)
129+
{
130+
mExcludePoint.push_back(mRecentSnappingResults.first().snappedVertex);
131+
}
123132
}
124133

125134
void QgsMapToolMoveVertex::canvasReleaseEvent(QMouseEvent * e)
@@ -138,7 +147,7 @@ void QgsMapToolMoveVertex::canvasReleaseEvent(QMouseEvent * e)
138147
QgsPoint snappedPointLayerCoord;
139148
QList<QgsSnappingResult> snapResults;
140149

141-
if(mSnapper.snapToBackgroundLayers(e->pos(), snapResults) != 0)
150+
if(mSnapper.snapToBackgroundLayers(e->pos(), snapResults, mExcludePoint) != 0)
142151
{
143152
//error
144153
}
@@ -171,6 +180,7 @@ void QgsMapToolMoveVertex::canvasReleaseEvent(QMouseEvent * e)
171180

172181
mRecentSnappingResults.clear();
173182
mRubberBandMovingPoints.clear();
183+
mExcludePoint.clear();
174184

175185
mCanvas->refresh();
176186
}

src/app/qgsmaptoolmovevertex.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ class QgsMapToolMoveVertex: public QgsMapToolVertexEdit
4646
that no point should be moved*/
4747
QList<int> mRubberBandMovingPoints;
4848

49+
/**The position of the vertex to move (in map coordinates) to exclude later from snapping*/
50+
QList<QgsPoint> mExcludePoint;
51+
4952
/**Deletes the rubber band pointers
5053
and clears mRubberBands*/
5154
void removeRubberBands();

src/core/qgssnapper.cpp

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ QgsSnapper::~QgsSnapper()
3939

4040
}
4141

42-
int QgsSnapper::snapPoint(const QPoint& startPoint, QList<QgsSnappingResult>& snappingResult)
42+
int QgsSnapper::snapPoint(const QPoint& startPoint, QList<QgsSnappingResult>& snappingResult, const QList<QgsPoint>& excludePoints)
4343
{
4444
snappingResult.clear();
4545

@@ -84,6 +84,9 @@ int QgsSnapper::snapPoint(const QPoint& startPoint, QList<QgsSnappingResult>& sn
8484
snappingResultList.insert(sqrt(newResult.snappedVertex.sqrDist(mapCoordPoint)), newResult);
8585
}
8686
}
87+
88+
//excluded specific points from result
89+
cleanResultList(snappingResultList, excludePoints);
8790

8891
//evaluate results according to snap mode
8992
QMultiMap<double, QgsSnappingResult>::iterator evalIt = snappingResultList.begin();
@@ -137,3 +140,30 @@ void QgsSnapper::setSnapMode(QgsSnapper::SNAP_MODE snapMode)
137140
{
138141
mSnapMode = snapMode;
139142
}
143+
144+
void QgsSnapper::cleanResultList(QMultiMap<double, QgsSnappingResult>& list, const QList<QgsPoint>& excludeList) const
145+
{
146+
QgsPoint currentResultPoint;
147+
QgsSnappingResult currentSnappingResult;
148+
QList<double> keysToRemove;
149+
150+
QMultiMap<double, QgsSnappingResult>::iterator result_it = list.begin();
151+
for(; result_it != list.end(); ++result_it)
152+
{
153+
currentSnappingResult = result_it.value();
154+
if(currentSnappingResult.snappedVertexNr != -1)
155+
{
156+
currentResultPoint = currentSnappingResult.snappedVertex;
157+
if(excludeList.contains(currentResultPoint))
158+
{
159+
keysToRemove.push_back(result_it.key());
160+
}
161+
}
162+
}
163+
164+
QList<double>::const_iterator remove_it = keysToRemove.constBegin();
165+
for(; remove_it != keysToRemove.constEnd(); ++remove_it)
166+
{
167+
list.remove(*remove_it);
168+
}
169+
}

src/core/qgssnapper.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,9 @@ class CORE_EXPORT QgsSnapper
8181
/**Does the snapping operation
8282
@param startPoint the start point for snapping (in pixel coordinates)
8383
@param snappingResult the list where the results are inserted (everything in map coordinate system)
84+
@param excludePoints a list with (map coordinate) points that should be excluded in the snapping result. Useful e.g. for vertex moves where a vertex should not be snapped to its original position
8485
@return 0 in case of success*/
85-
int snapPoint(const QPoint& startPoint, QList<QgsSnappingResult>& snappingResult);
86+
int snapPoint(const QPoint& startPoint, QList<QgsSnappingResult>& snappingResult, const QList<QgsPoint>& excludePoints = QList<QgsPoint>());
8687

8788
//setters
8889
void setLayersToSnap(const QList<QgsVectorLayer*>& layerList);
@@ -93,6 +94,10 @@ class CORE_EXPORT QgsSnapper
9394
private:
9495
/**Don't use the default constructor*/
9596
QgsSnapper();
97+
98+
/**Removes the snapping results that contains points in exclude list*/
99+
void cleanResultList(QMultiMap<double, QgsSnappingResult>& list, const QList<QgsPoint>& excludeList) const;
100+
96101
/**The maprender object contains information about the output coordinate system
97102
of the map and about the relationship between pixel space and map space*/
98103
QgsMapRender* mMapRender;

src/gui/qgsmapcanvassnapper.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ void QgsMapCanvasSnapper::setMapCanvas(QgsMapCanvas* canvas)
5858
}
5959
}
6060

61-
int QgsMapCanvasSnapper::snapToCurrentLayer(const QPoint& p, QList<QgsSnappingResult>& results, QgsSnapper::SNAP_TO snap_to, double snappingTol)
61+
int QgsMapCanvasSnapper::snapToCurrentLayer(const QPoint& p, QList<QgsSnappingResult>& results, QgsSnapper::SNAP_TO snap_to, double snappingTol, const QList<QgsPoint>& excludePoints)
6262
{
6363
results.clear();
6464

@@ -112,7 +112,7 @@ int QgsMapCanvasSnapper::snapToCurrentLayer(const QPoint& p, QList<QgsSnappingRe
112112
mSnapper->setTolerances(toleranceList);
113113
mSnapper->setSnapToList(snapToList);
114114

115-
if(mSnapper->snapPoint(p, results) != 0)
115+
if(mSnapper->snapPoint(p, results, excludePoints) != 0)
116116
{
117117
return 4;
118118
}
@@ -125,7 +125,7 @@ int QgsMapCanvasSnapper::snapToCurrentLayer(const QPoint& p, QList<QgsSnappingRe
125125
}
126126
}
127127

128-
int QgsMapCanvasSnapper::snapToBackgroundLayers(const QPoint& p, QList<QgsSnappingResult>& results)
128+
int QgsMapCanvasSnapper::snapToBackgroundLayers(const QPoint& p, QList<QgsSnappingResult>& results, const QList<QgsPoint>& excludePoints)
129129
{
130130
results.clear();
131131

@@ -252,7 +252,7 @@ int QgsMapCanvasSnapper::snapToBackgroundLayers(const QPoint& p, QList<QgsSnappi
252252
mSnapper->setTolerances(toleranceDoubleList);
253253
mSnapper->setSnapToList(snapTo);
254254

255-
if(mSnapper->snapPoint(p, results) != 0)
255+
if(mSnapper->snapPoint(p, results, excludePoints) != 0)
256256
{
257257
return 4;
258258
}

src/gui/qgsmapcanvassnapper.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,16 +48,18 @@ class GUI_EXPORT QgsMapCanvasSnapper
4848
@param p start point of the snap (in pixel coordinates)
4949
@param results list to which the results are appended
5050
@param snap_to snap to vertex or to segment
51-
@param snappingTol snapping tolerance. -1 means that the search radius for vertex edits is taken*/
52-
int snapToCurrentLayer(const QPoint& p, QList<QgsSnappingResult>& results, QgsSnapper::SNAP_TO snap_to, double snappingTol = -1);
51+
@param snappingTol snapping tolerance. -1 means that the search radius for vertex edits is taken
52+
@param excludePoints a list with (map coordinate) points that should be excluded in the snapping result. Useful e.g. for vertex moves where a vertex should not be snapped to its original position*/
53+
int snapToCurrentLayer(const QPoint& p, QList<QgsSnappingResult>& results, QgsSnapper::SNAP_TO snap_to, double snappingTol = -1, const QList<QgsPoint>& excludePoints = QList<QgsPoint>());
5354
/**Snaps to the background layers. This method is usefull to align the features of the
5455
edited layers to those of other layers (as described in the project properties).
5556
Uses snap mode QgsSnapper::ONE_RESULT. Therefore, only the
5657
closest result is returned.
5758
@param p start point of the snap (in pixel coordinates)
5859
@param result snapped point
60+
@param excludePoints a list with (map coordinate) points that should be excluded in the snapping result. Useful e.g. for vertex moves where a vertex should not be snapped to its original position
5961
@return 0 in case of success*/
60-
int snapToBackgroundLayers(const QPoint& p, QList<QgsSnappingResult>& results);
62+
int snapToBackgroundLayers(const QPoint& p, QList<QgsSnappingResult>& results, const QList<QgsPoint>& excludePoints = QList<QgsPoint>());
6163

6264
void setMapCanvas(QgsMapCanvas* canvas);
6365

0 commit comments

Comments
 (0)