Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
editing: show during mouse move where the point will be snapped to (b…
…y the expense of some performance)

git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@5417 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
mhugent committed May 4, 2006
1 parent 2326ff6 commit de5b6a3
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 27 deletions.
38 changes: 22 additions & 16 deletions src/core/qgsgeometry.cpp
Expand Up @@ -313,30 +313,31 @@ QgsPoint QgsGeometry::closestVertex(const QgsPoint& point, QgsGeometryVertexInde
{
unsigned char* ptr=mGeometry+5;
int* nlines=(int*)ptr;
int* npoints;
int* npoints = 0;
ptr+=sizeof(int);
for(int index=0;index<*nlines;++index)
{
npoints=(int*)ptr;
ptr+=sizeof(int);
for(int index2=0;index2<*npoints;++index2)
ptr += (sizeof(int) + 1);
npoints=(int*)ptr;
ptr+=sizeof(int);
for(int index2=0;index2<*npoints;++index2)
{
tempx=(double*)ptr;
ptr+=sizeof(double);
tempy=(double*)ptr;
if(point.sqrDist(*tempx,*tempy)<actdist)
tempx=(double*)ptr;
ptr+=sizeof(double);
tempy=(double*)ptr;
ptr+=sizeof(double);
if(point.sqrDist(*tempx,*tempy)<actdist)
{
x=*tempx;
y=*tempy;
actdist=point.sqrDist(*tempx,*tempy);
vertexnr = vertexcounter;
x=*tempx;
y=*tempy;
actdist=point.sqrDist(*tempx,*tempy);
vertexnr = vertexcounter;
}
ptr+=sizeof(double);
++vertexcounter;
++vertexcounter;
}
}
}
break;
break;

case QGis::WKBMultiPolygon:
{
Expand Down Expand Up @@ -482,6 +483,7 @@ bool QgsGeometry::moveVertexAt(double x, double y, QgsGeometryVertexIndex atVert
int pointindex = 0;
for(int linenr = 0; linenr < *nrLines; ++linenr)
{
ptr += sizeof(int) + 1;
nrPoints = (int*)ptr;
ptr += sizeof(int);
if(vertexnr >= pointindex && vertexnr < pointindex + (*nrPoints))
Expand Down Expand Up @@ -840,7 +842,6 @@ bool QgsGeometry::insertVertexBefore(double x, double y, QgsGeometryVertexIndex
{
case QGis::WKBPoint://cannot insert a vertex before another one on point types
case QGis::WKBMultiPoint:
break;
case QGis::WKBLineString:
{
int* nPoints = (int*)ptr;
Expand Down Expand Up @@ -871,6 +872,9 @@ bool QgsGeometry::insertVertexBefore(double x, double y, QgsGeometryVertexIndex
break;
}
case QGis::WKBMultiLineString:
{
break;
}
case QGis::WKBPolygon:
{
int* nRings = (int*)ptr;
Expand Down Expand Up @@ -1092,6 +1096,7 @@ bool QgsGeometry::vertexAt(double &x, double &y,
ptr += sizeof(int);
for(int linenr = 0; linenr < *nLines; ++linenr)
{
ptr += sizeof(int) + 1;
nPoints = (int*)ptr;
ptr += sizeof(int);
for(int pointnr = 0; pointnr < *nPoints; ++pointnr)
Expand Down Expand Up @@ -1271,6 +1276,7 @@ QgsPoint QgsGeometry::closestSegmentWithContext(QgsPoint& point,
int pointindex = 0;//global pointindex
for(int linenr = 0; linenr < *nLines; ++linenr)
{
ptr += sizeof(int) + 1;
nPoints = (int*)ptr;
ptr += sizeof(int);
prevx = 0;
Expand Down
12 changes: 7 additions & 5 deletions src/gui/qgsmaptoolcapture.cpp
Expand Up @@ -108,11 +108,10 @@ void QgsMapToolCapture::canvasReleaseEvent(QMouseEvent * e)
if(vlayer->getDataProvider()->capabilities()&QgsVectorDataProvider::AddFeatures)
{
QgsFeature* f = new QgsFeature(0,"WKBPoint");

QgsPoint savePoint = maybeInversePoint(idPoint, "adding point");
// snap point to points within the vector layer snapping tolerance
vlayer->snapPoint(idPoint, tolerance);
// project to layer's SRS
QgsPoint savePoint = maybeInversePoint(idPoint, "adding point");
vlayer->snapPoint(savePoint, tolerance);

// create geos geometry and attach it to feature
int size=5+2*sizeof(double);
Expand Down Expand Up @@ -162,7 +161,6 @@ void QgsMapToolCapture::canvasReleaseEvent(QMouseEvent * e)
QgsPoint digitisedPoint = toMapCoords(e->pos());
vlayer->snapPoint(digitisedPoint, tolerance);
mCaptureList.push_back(digitisedPoint);

mRubberBand->addPoint(digitisedPoint);

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

} // mouseMoveEvent
Expand Down
44 changes: 38 additions & 6 deletions src/gui/qgsmaptoolvertexedit.cpp
Expand Up @@ -47,13 +47,19 @@ void QgsMapToolVertexEdit::canvasMoveEvent(QMouseEvent * e)
{
if (e->buttons() == Qt::LeftButton && (mTool == AddVertex || mTool == MoveVertex))
{

#ifdef QGISDEBUG
qWarning("Moving rubber band for moveVertex");
#endif

int index = (mStartPointValid ? 1 : 0);
mRubberBand->movePoint(index, toMapCoords(e->pos()));
//snap to nearest vertex of vectorlayer
QgsPoint rbpoint = toMapCoords(e->pos());
if(mTool == AddVertex)
{
snapVertex(rbpoint, mSnappedAtFeatureId, mSnappedBeforeVertex.back());
mRubberBand->movePoint(index, rbpoint);
}
else if(mTool == MoveVertex)
{
snapVertex(rbpoint, mSnappedAtFeatureId, mSnappedAtVertex.back());
mRubberBand->movePoint(index, rbpoint);
}
}

}
Expand Down Expand Up @@ -304,6 +310,7 @@ void QgsMapToolVertexEdit::canvasReleaseEvent(QMouseEvent * e)

//snap to nearest vertex of vectorlayer
snapVertex(point, mSnappedAtFeatureId, mSnappedBeforeVertex.back());

#ifdef QGISDEBUG
std::cout << "QgsMapToolVertexEdit::canvasReleaseEvent: AddVertex." << std::endl;
#endif
Expand Down Expand Up @@ -360,3 +367,28 @@ void QgsMapToolVertexEdit::deactivate()
mRubberBand = 0;
}

QgsPoint QgsMapToolVertexEdit::maybeInversePoint(QgsPoint point, const char whenmsg[])
{
QgsVectorLayer *vlayer = dynamic_cast <QgsVectorLayer*>(mCanvas->currentLayer());
QgsPoint transformedPoint;

if( mCanvas->projectionsEnabled() )
{
// Do reverse transformation before saving. If possible!
try
{
transformedPoint = vlayer->coordinateTransform()->transform(point, QgsCoordinateTransform::INVERSE);
}
catch(QgsCsException &cse)
{
//#ifdef QGISDEBUG
std::cout << "Caught transform error when " << whenmsg <<"."
<< "Setting untransformed values." << std::endl;
//#endif
// Transformation failed,. Bail out with original rectangle.
return point;
}
return transformedPoint;
}
return point;
}
5 changes: 5 additions & 0 deletions src/gui/qgsmaptoolvertexedit.h
Expand Up @@ -57,6 +57,11 @@ class QgsMapToolVertexEdit : public QgsMapTool

//! returns tolerance in map coordinates
double tolerance();

/** Helper function to inverse project a point if projections
are enabled. Failsafe, returns the sent point if anything fails.
@whenmsg is a part fo the error message. */
QgsPoint maybeInversePoint(QgsPoint point, const char whenmsg[]);

//! current vertex edit tool
enum Tool mTool;
Expand Down

0 comments on commit de5b6a3

Please sign in to comment.