Skip to content

Commit 793db5f

Browse files
committedAug 30, 2014
Allow removing last point while measuring via del/backspace keys (fix #10176)
1 parent acf1f70 commit 793db5f

File tree

4 files changed

+90
-1
lines changed

4 files changed

+90
-1
lines changed
 

‎src/app/qgsmeasuredialog.cpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ void QgsMeasureDialog::restart()
8989

9090
void QgsMeasureDialog::mouseMove( QgsPoint &point )
9191
{
92+
mLastMousePoint = point;
9293
// show current distance/area while moving the point
9394
// by creating a temporary copy of point array
9495
// and adding moving point at the end
@@ -145,6 +146,43 @@ void QgsMeasureDialog::addPoint( QgsPoint &p )
145146
QgsDebugMsg( "Exiting" );
146147
}
147148

149+
void QgsMeasureDialog::removeLastPoint()
150+
{
151+
int numPoints = mTool->points().size();
152+
if ( mMeasureArea )
153+
{
154+
if ( numPoints > 1 )
155+
{
156+
QList<QgsPoint> tmpPoints = mTool->points();
157+
tmpPoints.append( mLastMousePoint );
158+
double area = mDa.measurePolygon( tmpPoints );
159+
editTotal->setText( formatArea( area ) );
160+
}
161+
else
162+
{
163+
editTotal->setText( formatArea( 0 ) );
164+
}
165+
}
166+
else if ( !mMeasureArea && numPoints >= 1 )
167+
{
168+
//remove final row
169+
delete mTable->takeTopLevelItem( mTable->topLevelItemCount() - 1 );
170+
171+
QgsPoint p1( mTool->points().last() );
172+
double d = mDa.measureLine( p1, mLastMousePoint );
173+
174+
mTotal = mDa.measureLine( mTool->points() );
175+
editTotal->setText( formatDistance( mTotal + d ) );
176+
177+
QGis::UnitType displayUnits;
178+
// Meters or feet?
179+
convertMeasurement( d, displayUnits, false );
180+
181+
QTreeWidgetItem *item = mTable->topLevelItem( mTable->topLevelItemCount() - 1 );
182+
item->setText( 0, QLocale::system().toString( d, 'f', mDecimalPlaces ) );
183+
}
184+
}
185+
148186
void QgsMeasureDialog::on_buttonBox_rejected( void )
149187
{
150188
restart();

‎src/app/qgsmeasuredialog.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ class APP_EXPORT QgsMeasureDialog : public QDialog, private Ui::QgsMeasureBase
4747
//! Mose move
4848
void mouseMove( QgsPoint &point );
4949

50+
//! Remove last point
51+
void removeLastPoint();
52+
5053
public slots:
5154
//! Reject
5255
void on_buttonBox_rejected( void );
@@ -96,6 +99,8 @@ class APP_EXPORT QgsMeasureDialog : public QDialog, private Ui::QgsMeasureBase
9699

97100
//! pointer to measure tool which owns this dialog
98101
QgsMeasureTool* mTool;
102+
103+
QgsPoint mLastMousePoint;
99104
};
100105

101106
#endif

‎src/app/qgsmeasuretool.cpp

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,12 +167,54 @@ void QgsMeasureTool::canvasReleaseEvent( QMouseEvent * e )
167167
mDone = false;
168168
}
169169

170-
// we allways add the clicked point to the measuring feature
170+
// we always add the clicked point to the measuring feature
171171
addPoint( point );
172172
mDialog->show();
173173

174174
}
175175

176+
void QgsMeasureTool::undo()
177+
{
178+
if ( mRubberBand )
179+
{
180+
if ( mPoints.size() < 1 )
181+
{
182+
return;
183+
}
184+
185+
if ( mPoints.size() == 1 )
186+
{
187+
//removing first point, so restart everything
188+
restart();
189+
mDialog->restart();
190+
}
191+
else
192+
{
193+
//remove second last point from line band, and last point from points band
194+
mRubberBand->removePoint( -2, true );
195+
mRubberBandPoints->removePoint( -1, true );
196+
mPoints.removeLast();
197+
198+
mDialog->removeLastPoint();
199+
}
200+
201+
}
202+
}
203+
204+
void QgsMeasureTool::keyPressEvent( QKeyEvent* e )
205+
{
206+
if (( e->key() == Qt::Key_Backspace || e->key() == Qt::Key_Delete ) )
207+
{
208+
if ( !mDone )
209+
{
210+
undo();
211+
}
212+
213+
// Override default shortcut management in MapCanvas
214+
e->ignore();
215+
}
216+
}
217+
176218

177219
void QgsMeasureTool::addPoint( QgsPoint &point )
178220
{

‎src/app/qgsmeasuretool.h

100644100755
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ class APP_EXPORT QgsMeasureTool : public QgsMapTool
7070
//! called when map tool is being deactivated
7171
virtual void deactivate();
7272

73+
virtual void keyPressEvent( QKeyEvent* e );
74+
7375
public slots:
7476
//! updates the projections we're using
7577
void updateSettings();
@@ -102,6 +104,8 @@ class APP_EXPORT QgsMeasureTool : public QgsMapTool
102104
//@param p (pixel) coordinate
103105
QgsPoint snapPoint( const QPoint& p );
104106

107+
/**Removes the last vertex from mRubberBand*/
108+
void undo();
105109
};
106110

107111
#endif

0 commit comments

Comments
 (0)
Please sign in to comment.