Skip to content
Permalink
Browse files
Fix for vertex editing bug where already changed geometries have not …
…been examined for point snapping

git-svn-id: http://svn.osgeo.org/qgis/trunk@5410 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
mhugent committed May 1, 2006
1 parent 90c10d1 commit 0910ad46efb4c757dc28ce59fd0604c68984c054
Showing with 83 additions and 19 deletions.
  1. +39 −9 src/gui/qgsmaptoolvertexedit.cpp
  2. +5 −0 src/gui/qgsmaptoolvertexedit.h
  3. +39 −10 src/gui/qgsvectorlayer.cpp
@@ -76,9 +76,13 @@ void QgsMapToolVertexEdit::canvasPressEvent(QMouseEvent * e)
std::cout << "QgsMapCanvas::mousePressEvent: QGis::AddVertex." << std::endl;
#endif

// TODO: Find nearest segment of the selected line, move that node to the mouse location
//Find nearest segment of the selected line, move that node to the mouse location
if (!snapSegmentWithContext(point))
return;
{
QMessageBox::warning(0, "Error", "Could not snap vertex. Have you set the tolerance?",
QMessageBox::Ok, Qt::NoButton);
return;
}

index = mSnappedBeforeVertex;

@@ -108,9 +112,13 @@ void QgsMapToolVertexEdit::canvasPressEvent(QMouseEvent * e)
// Find the closest line segment to the mouse position
// Then set up the rubber band to its endpoints

// TODO: Find nearest segment of the selected line, move that node to the mouse location
//Find nearest segment of the selected line, move that node to the mouse location
if (!snapVertexWithContext(point))
return;
{
QMessageBox::warning(0, "Error", "Could not snap vertex. Have you set the tolerance?",
QMessageBox::Ok, Qt::NoButton);
return;
}

#ifdef QGISDEBUG
qWarning("Creating rubber band for moveVertex");
@@ -150,7 +158,11 @@ void QgsMapToolVertexEdit::canvasPressEvent(QMouseEvent * e)

// TODO: Find nearest segment of the selected line, move that node to the mouse location
if (!snapVertexWithContext(point))
return;
{
QMessageBox::warning(0, "Error", "Could not snap vertex. Have you set the tolerance?",
QMessageBox::Ok, Qt::NoButton);
return;
}

// Get the point of the snapped-to vertex
mSnappedAtGeometry.vertexAt(x1, y1, mSnappedAtVertex);
@@ -219,8 +231,6 @@ bool QgsMapToolVertexEdit::snapVertexWithContext(QgsPoint& point)
if (!vlayer->snapVertexWithContext(point, atVertex, atFeatureId, atGeometry, tolerance()))
{
mSnappedAtFeatureId = -1;
QMessageBox::warning(0, "Error", "Could not snap vertex. Have you set the tolerance?",
QMessageBox::Ok, Qt::NoButton);
return FALSE;
}
else
@@ -237,6 +247,24 @@ bool QgsMapToolVertexEdit::snapVertexWithContext(QgsPoint& point)
}
}

bool QgsMapToolVertexEdit::snapVertex(QgsPoint& point, int exclFeatureId, int exclVertexNr)
{
QgsVectorLayer* vlayer = dynamic_cast<QgsVectorLayer*>(mCanvas->currentLayer());
if(vlayer)
{
QgsGeometryVertexIndex vIndex;
int snappedFeatureId;
QgsGeometry snappedGeometry;
//do the snapping to a copy point first
QgsPoint cpyPoint = point;
vlayer->snapVertexWithContext(cpyPoint, vIndex, snappedFeatureId, snappedGeometry, tolerance());
if(snappedFeatureId != exclFeatureId || vIndex.back() != exclVertexNr)
{
point = cpyPoint;
}
}
}


void QgsMapToolVertexEdit::canvasReleaseEvent(QMouseEvent * e)
{
@@ -274,6 +302,8 @@ void QgsMapToolVertexEdit::canvasReleaseEvent(QMouseEvent * e)
if (mTool == AddVertex)
{

//snap to nearest vertex of vectorlayer
snapVertex(point, mSnappedAtFeatureId, mSnappedBeforeVertex.back());
#ifdef QGISDEBUG
std::cout << "QgsMapToolVertexEdit::canvasReleaseEvent: AddVertex." << std::endl;
#endif
@@ -282,19 +312,19 @@ void QgsMapToolVertexEdit::canvasReleaseEvent(QMouseEvent * e)
mRubberBand = 0;

// Add the new vertex
//todo: snap to nearest vertex of vectorlayer
vlayer->insertVertexBefore(point.x(), point.y(), mSnappedAtFeatureId, mSnappedBeforeVertex);
mCanvas->refresh();
}
else if (mTool == MoveVertex)
{
//snap to nearest vertex of vectorlayer
snapVertex(point, mSnappedAtFeatureId, mSnappedAtVertex.back());
#ifdef QGISDEBUG
std::cout << "QgsMapToolVertexEdit::canvasReleaseEvent: MoveVertex." << std::endl;
#endif

delete mRubberBand;
mRubberBand = 0;
//todo: snap to nearest vertex of vectorlayer
vlayer->moveVertexAt(point.x(), point.y(), mSnappedAtFeatureId, mSnappedAtVertex);
mCanvas->refresh();
}
@@ -61,7 +61,12 @@ class QgsMapToolVertexEdit : public QgsMapTool
//! current vertex edit tool
enum Tool mTool;

/**Searches the closest point within the project tolerance and setx mSnappedAtFeatureId and mSnappedAtVertex*/
bool snapVertexWithContext(QgsPoint& point);

/**Snaps a point (without setting mSnappedAtFeatureId and mSnappedAtVertex). Does not snap to the specified vertex,
because during dragging, a vertex should not be snapped to itself*/
bool snapVertex(QgsPoint& point, int exclFeatureId, int exclVertexNr);

bool snapSegmentWithContext(QgsPoint& point);

@@ -1679,7 +1679,7 @@ bool QgsVectorLayer::moveVertexAt(double x, double y, int atFeatureId,
if(mChangedGeometries.find(atFeatureId) == mChangedGeometries.end())
{
// first time this geometry has changed since last commit
if(!mCachedGeometries[atFeatureId])
if(mCachedGeometries.find(atFeatureId) == mCachedGeometries.end() || mCachedGeometries[atFeatureId] == 0)
{
return false;
}
@@ -2802,7 +2802,7 @@ bool QgsVectorLayer::snapVertexWithContext(QgsPoint& point, QgsGeometryVertexInd
}

QgsFeature* feature;
QgsPoint minDistSegPoint; // the closest point on the segment
QgsPoint minDistSegPoint; // the closest point
double testSqrDist; // the squared distance between 'point' and 'snappedFeature'

double minSqrDist = tolerance*tolerance; //current minimum distance
@@ -2830,6 +2830,8 @@ bool QgsVectorLayer::snapVertexWithContext(QgsPoint& point, QgsGeometryVertexInd
snappedFeatureId = feature->featureId();
snappedGeometry = *(feature->geometry());
vertexFound = true;
delete feature;
return true;
}
delete feature;
}
@@ -2855,14 +2857,27 @@ bool QgsVectorLayer::snapVertexWithContext(QgsPoint& point, QgsGeometryVertexInd
snappedFeatureId = (*iter)->featureId();
snappedGeometry = *((*iter)->geometry());
vertexFound = true;
return true;
}
}

if(!vertexFound)
//and also go through the changed geometries, because the spatial filter of the provider did not consider feature changes
for(std::map<int, QgsGeometry>::iterator it = mChangedGeometries.begin(); it != mChangedGeometries.end(); ++it)
{
return false;
minDistSegPoint = it->second.closestVertex(origPoint, atVertexTemp, testSqrDist);
if(testSqrDist < minSqrDist)
{
point = minDistSegPoint;
minSqrDist = testSqrDist;
atVertex = atVertexTemp;
snappedFeatureId = it->first;
snappedGeometry = it->second;
vertexFound = true;
return true;
}
}
return true;

return false;
}


@@ -2909,6 +2924,8 @@ QgsGeometry& snappedGeometry, double tolerance)
snappedFeatureId = feature->featureId();
snappedGeometry = *(feature->geometry());
segmentFound = true;
delete feature;
return true;
}
delete feature;
}
@@ -2935,14 +2952,26 @@ QgsGeometry& snappedGeometry, double tolerance)
snappedFeatureId = (*iter)->featureId();
snappedGeometry = *((*iter)->geometry());
segmentFound = true;
return true;
}
}

if(!segmentFound)
{
return false;
}
return true;
//and also go through the changed geometries, because the spatial filter of the provider did not consider feature changes
for(std::map<int, QgsGeometry>::iterator it = mChangedGeometries.begin(); it != mChangedGeometries.end(); ++it)
{
minDistSegPoint = it->second.closestSegmentWithContext(origPoint, beforeVertexTemp, testSqrDist);
if(testSqrDist < minSqrDist)
{
point = minDistSegPoint;
minSqrDist = testSqrDist;
beforeVertex = beforeVertexTemp;
snappedFeatureId = it->first;
snappedGeometry = it->second;
segmentFound = true;
return true;
}
}
return false;
}


0 comments on commit 0910ad4

Please sign in to comment.