Skip to content

Commit b22acec

Browse files
author
mhugent
committedMay 4, 2006
editing: show during mouse move where the point will be snapped to (by the expense of some performance)
git-svn-id: http://svn.osgeo.org/qgis/trunk@5417 c8812cc2-4d05-0410-92ff-de0c093fc19c
1 parent ef53c2f commit b22acec

File tree

4 files changed

+72
-27
lines changed

4 files changed

+72
-27
lines changed
 

‎src/core/qgsgeometry.cpp

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -313,30 +313,31 @@ QgsPoint QgsGeometry::closestVertex(const QgsPoint& point, QgsGeometryVertexInde
313313
{
314314
unsigned char* ptr=mGeometry+5;
315315
int* nlines=(int*)ptr;
316-
int* npoints;
316+
int* npoints = 0;
317317
ptr+=sizeof(int);
318318
for(int index=0;index<*nlines;++index)
319319
{
320-
npoints=(int*)ptr;
321-
ptr+=sizeof(int);
322-
for(int index2=0;index2<*npoints;++index2)
320+
ptr += (sizeof(int) + 1);
321+
npoints=(int*)ptr;
322+
ptr+=sizeof(int);
323+
for(int index2=0;index2<*npoints;++index2)
323324
{
324-
tempx=(double*)ptr;
325-
ptr+=sizeof(double);
326-
tempy=(double*)ptr;
327-
if(point.sqrDist(*tempx,*tempy)<actdist)
325+
tempx=(double*)ptr;
326+
ptr+=sizeof(double);
327+
tempy=(double*)ptr;
328+
ptr+=sizeof(double);
329+
if(point.sqrDist(*tempx,*tempy)<actdist)
328330
{
329-
x=*tempx;
330-
y=*tempy;
331-
actdist=point.sqrDist(*tempx,*tempy);
332-
vertexnr = vertexcounter;
331+
x=*tempx;
332+
y=*tempy;
333+
actdist=point.sqrDist(*tempx,*tempy);
334+
vertexnr = vertexcounter;
333335
}
334-
ptr+=sizeof(double);
335-
++vertexcounter;
336+
++vertexcounter;
336337
}
337338
}
338339
}
339-
break;
340+
break;
340341

341342
case QGis::WKBMultiPolygon:
342343
{
@@ -482,6 +483,7 @@ bool QgsGeometry::moveVertexAt(double x, double y, QgsGeometryVertexIndex atVert
482483
int pointindex = 0;
483484
for(int linenr = 0; linenr < *nrLines; ++linenr)
484485
{
486+
ptr += sizeof(int) + 1;
485487
nrPoints = (int*)ptr;
486488
ptr += sizeof(int);
487489
if(vertexnr >= pointindex && vertexnr < pointindex + (*nrPoints))
@@ -840,7 +842,6 @@ bool QgsGeometry::insertVertexBefore(double x, double y, QgsGeometryVertexIndex
840842
{
841843
case QGis::WKBPoint://cannot insert a vertex before another one on point types
842844
case QGis::WKBMultiPoint:
843-
break;
844845
case QGis::WKBLineString:
845846
{
846847
int* nPoints = (int*)ptr;
@@ -871,6 +872,9 @@ bool QgsGeometry::insertVertexBefore(double x, double y, QgsGeometryVertexIndex
871872
break;
872873
}
873874
case QGis::WKBMultiLineString:
875+
{
876+
break;
877+
}
874878
case QGis::WKBPolygon:
875879
{
876880
int* nRings = (int*)ptr;
@@ -1092,6 +1096,7 @@ bool QgsGeometry::vertexAt(double &x, double &y,
10921096
ptr += sizeof(int);
10931097
for(int linenr = 0; linenr < *nLines; ++linenr)
10941098
{
1099+
ptr += sizeof(int) + 1;
10951100
nPoints = (int*)ptr;
10961101
ptr += sizeof(int);
10971102
for(int pointnr = 0; pointnr < *nPoints; ++pointnr)
@@ -1271,6 +1276,7 @@ QgsPoint QgsGeometry::closestSegmentWithContext(QgsPoint& point,
12711276
int pointindex = 0;//global pointindex
12721277
for(int linenr = 0; linenr < *nLines; ++linenr)
12731278
{
1279+
ptr += sizeof(int) + 1;
12741280
nPoints = (int*)ptr;
12751281
ptr += sizeof(int);
12761282
prevx = 0;

‎src/gui/qgsmaptoolcapture.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -108,11 +108,10 @@ void QgsMapToolCapture::canvasReleaseEvent(QMouseEvent * e)
108108
if(vlayer->getDataProvider()->capabilities()&QgsVectorDataProvider::AddFeatures)
109109
{
110110
QgsFeature* f = new QgsFeature(0,"WKBPoint");
111-
111+
QgsPoint savePoint = maybeInversePoint(idPoint, "adding point");
112112
// snap point to points within the vector layer snapping tolerance
113-
vlayer->snapPoint(idPoint, tolerance);
114113
// project to layer's SRS
115-
QgsPoint savePoint = maybeInversePoint(idPoint, "adding point");
114+
vlayer->snapPoint(savePoint, tolerance);
116115

117116
// create geos geometry and attach it to feature
118117
int size=5+2*sizeof(double);
@@ -162,7 +161,6 @@ void QgsMapToolCapture::canvasReleaseEvent(QMouseEvent * e)
162161
QgsPoint digitisedPoint = toMapCoords(e->pos());
163162
vlayer->snapPoint(digitisedPoint, tolerance);
164163
mCaptureList.push_back(digitisedPoint);
165-
166164
mRubberBand->addPoint(digitisedPoint);
167165

168166
if (e->button() == Qt::LeftButton)
@@ -272,7 +270,11 @@ void QgsMapToolCapture::canvasMoveEvent(QMouseEvent * e)
272270
if (mCapturing)
273271
{
274272
// show the rubber-band from the last click
275-
mRubberBand->movePoint(toMapCoords(e->pos()));
273+
QgsVectorLayer *vlayer = dynamic_cast <QgsVectorLayer*>(mCanvas->currentLayer());
274+
double tolerance = QgsProject::instance()->readDoubleEntry("Digitizing","/Tolerance",0);
275+
QgsPoint rbpoint = toMapCoords(e->pos());
276+
vlayer->snapPoint(rbpoint, tolerance); //show snapping during dragging
277+
mRubberBand->movePoint(rbpoint);
276278
}
277279

278280
} // mouseMoveEvent

‎src/gui/qgsmaptoolvertexedit.cpp

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,19 @@ void QgsMapToolVertexEdit::canvasMoveEvent(QMouseEvent * e)
4747
{
4848
if (e->buttons() == Qt::LeftButton && (mTool == AddVertex || mTool == MoveVertex))
4949
{
50-
51-
#ifdef QGISDEBUG
52-
qWarning("Moving rubber band for moveVertex");
53-
#endif
54-
5550
int index = (mStartPointValid ? 1 : 0);
56-
mRubberBand->movePoint(index, toMapCoords(e->pos()));
51+
//snap to nearest vertex of vectorlayer
52+
QgsPoint rbpoint = toMapCoords(e->pos());
53+
if(mTool == AddVertex)
54+
{
55+
snapVertex(rbpoint, mSnappedAtFeatureId, mSnappedBeforeVertex.back());
56+
mRubberBand->movePoint(index, rbpoint);
57+
}
58+
else if(mTool == MoveVertex)
59+
{
60+
snapVertex(rbpoint, mSnappedAtFeatureId, mSnappedAtVertex.back());
61+
mRubberBand->movePoint(index, rbpoint);
62+
}
5763
}
5864

5965
}
@@ -304,6 +310,7 @@ void QgsMapToolVertexEdit::canvasReleaseEvent(QMouseEvent * e)
304310

305311
//snap to nearest vertex of vectorlayer
306312
snapVertex(point, mSnappedAtFeatureId, mSnappedBeforeVertex.back());
313+
307314
#ifdef QGISDEBUG
308315
std::cout << "QgsMapToolVertexEdit::canvasReleaseEvent: AddVertex." << std::endl;
309316
#endif
@@ -360,3 +367,28 @@ void QgsMapToolVertexEdit::deactivate()
360367
mRubberBand = 0;
361368
}
362369

370+
QgsPoint QgsMapToolVertexEdit::maybeInversePoint(QgsPoint point, const char whenmsg[])
371+
{
372+
QgsVectorLayer *vlayer = dynamic_cast <QgsVectorLayer*>(mCanvas->currentLayer());
373+
QgsPoint transformedPoint;
374+
375+
if( mCanvas->projectionsEnabled() )
376+
{
377+
// Do reverse transformation before saving. If possible!
378+
try
379+
{
380+
transformedPoint = vlayer->coordinateTransform()->transform(point, QgsCoordinateTransform::INVERSE);
381+
}
382+
catch(QgsCsException &cse)
383+
{
384+
//#ifdef QGISDEBUG
385+
std::cout << "Caught transform error when " << whenmsg <<"."
386+
<< "Setting untransformed values." << std::endl;
387+
//#endif
388+
// Transformation failed,. Bail out with original rectangle.
389+
return point;
390+
}
391+
return transformedPoint;
392+
}
393+
return point;
394+
}

‎src/gui/qgsmaptoolvertexedit.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,11 @@ class QgsMapToolVertexEdit : public QgsMapTool
5757

5858
//! returns tolerance in map coordinates
5959
double tolerance();
60+
61+
/** Helper function to inverse project a point if projections
62+
are enabled. Failsafe, returns the sent point if anything fails.
63+
@whenmsg is a part fo the error message. */
64+
QgsPoint maybeInversePoint(QgsPoint point, const char whenmsg[]);
6065

6166
//! current vertex edit tool
6267
enum Tool mTool;

0 commit comments

Comments
 (0)
Please sign in to comment.