Skip to content

File tree

3 files changed

+58
-1
lines changed

3 files changed

+58
-1
lines changed
 

‎src/plugins/grass/qgsgrassedit.cpp

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1140,6 +1140,49 @@ void QgsGrassEdit::snap ( QgsPoint & point )
11401140
point.setY(y);
11411141
}
11421142

1143+
void QgsGrassEdit::snap ( QgsPoint & point, double startX, double startY )
1144+
{
1145+
double x = point.x();
1146+
double y = point.y();
1147+
1148+
double thresh = threshold();
1149+
1150+
// Start
1151+
double startDist = hypot ( x-startX, y-startY);
1152+
bool startIn = false;
1153+
if ( startDist <= thresh ) startIn = true;
1154+
1155+
// Nearest node
1156+
double nodeX, nodeY;
1157+
double nodeDist;
1158+
bool nodeIn = false;
1159+
int node = mProvider->findNode ( x, y, thresh );
1160+
1161+
if ( node > 0 )
1162+
{
1163+
mProvider->nodeCoor ( node, &nodeX, &nodeY );
1164+
nodeDist = hypot ( x-nodeX, y-nodeY);
1165+
nodeIn = true;
1166+
}
1167+
std::cerr << "startIn = " << startIn << std::endl;
1168+
std::cerr << "nodeIn = " << nodeIn << std::endl;
1169+
std::cerr << "startDist = " << startDist << std::endl;
1170+
std::cerr << "nodeDist = " << nodeDist << std::endl;
1171+
1172+
// Choose
1173+
if ( (startIn && !nodeIn) || (startIn && nodeIn && startDist < nodeDist) )
1174+
{
1175+
x = startX; y = startY;
1176+
}
1177+
else if ( (!startIn && nodeIn) || (startIn && nodeIn && startDist > nodeDist) )
1178+
{
1179+
x = nodeX; y = nodeY;
1180+
}
1181+
1182+
point.setX(x);
1183+
point.setY(y);
1184+
}
1185+
11431186
void QgsGrassEdit::newPoint(void) { startTool(QgsGrassEdit::NEW_POINT); }
11441187
void QgsGrassEdit::newLine(void) {
11451188
std::cerr << "QgsGrassEdit::newLine" << std::endl;

‎src/plugins/grass/qgsgrassedit.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,8 @@ public slots:
395395
/** Snap to nearest node in current threshold */
396396
void snap ( QgsPoint & point );
397397
void snap ( double *x, double *y);
398+
/** Snap point line considering line starting point */
399+
void snap ( QgsPoint & point, double startX, double startY);
398400

399401
/** Attributes */
400402
QgsGrassAttributes *mAttributes;

‎src/plugins/grass/qgsgrassedittools.cpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,20 @@ void QgsGrassEditNewLine::mouseClick(QgsPoint & point, Qt::ButtonState button)
151151
{
152152
switch ( button ) {
153153
case Qt::LeftButton:
154-
e->snap ( point );
154+
if ( e->mEditPoints->n_points > 2 )
155+
{
156+
e->snap ( point, e->mEditPoints->x[0], e->mEditPoints->y[0] );
157+
}
158+
else
159+
{
160+
e->snap ( point );
161+
}
155162
Vect_append_point ( e->mEditPoints, point.x(), point.y(), 0.0 );
163+
164+
// Draw
165+
Vect_reset_line ( e->mPoints );
166+
Vect_append_points ( e->mPoints, e->mEditPoints, GV_FORWARD );
167+
e->displayDynamic ( e->mPoints );
156168
break;
157169
case Qt::MidButton:
158170
if ( e->mEditPoints->n_points > 0 ) {

0 commit comments

Comments
 (0)
Please sign in to comment.