From 793db5f53ff99891a297ee4c7dcda6861cc0a09e Mon Sep 17 00:00:00 2001 From: Nyall Dawson Date: Sat, 30 Aug 2014 21:47:13 +1000 Subject: [PATCH] Allow removing last point while measuring via del/backspace keys (fix #10176) --- src/app/qgsmeasuredialog.cpp | 38 +++++++++++++++++++++++++++++++ src/app/qgsmeasuredialog.h | 5 ++++ src/app/qgsmeasuretool.cpp | 44 +++++++++++++++++++++++++++++++++++- src/app/qgsmeasuretool.h | 4 ++++ 4 files changed, 90 insertions(+), 1 deletion(-) mode change 100644 => 100755 src/app/qgsmeasuretool.h diff --git a/src/app/qgsmeasuredialog.cpp b/src/app/qgsmeasuredialog.cpp index d65446cde4c..1e333a8f68f 100644 --- a/src/app/qgsmeasuredialog.cpp +++ b/src/app/qgsmeasuredialog.cpp @@ -89,6 +89,7 @@ void QgsMeasureDialog::restart() void QgsMeasureDialog::mouseMove( QgsPoint &point ) { + mLastMousePoint = point; // show current distance/area while moving the point // by creating a temporary copy of point array // and adding moving point at the end @@ -145,6 +146,43 @@ void QgsMeasureDialog::addPoint( QgsPoint &p ) QgsDebugMsg( "Exiting" ); } +void QgsMeasureDialog::removeLastPoint() +{ + int numPoints = mTool->points().size(); + if ( mMeasureArea ) + { + if ( numPoints > 1 ) + { + QList tmpPoints = mTool->points(); + tmpPoints.append( mLastMousePoint ); + double area = mDa.measurePolygon( tmpPoints ); + editTotal->setText( formatArea( area ) ); + } + else + { + editTotal->setText( formatArea( 0 ) ); + } + } + else if ( !mMeasureArea && numPoints >= 1 ) + { + //remove final row + delete mTable->takeTopLevelItem( mTable->topLevelItemCount() - 1 ); + + QgsPoint p1( mTool->points().last() ); + double d = mDa.measureLine( p1, mLastMousePoint ); + + mTotal = mDa.measureLine( mTool->points() ); + editTotal->setText( formatDistance( mTotal + d ) ); + + QGis::UnitType displayUnits; + // Meters or feet? + convertMeasurement( d, displayUnits, false ); + + QTreeWidgetItem *item = mTable->topLevelItem( mTable->topLevelItemCount() - 1 ); + item->setText( 0, QLocale::system().toString( d, 'f', mDecimalPlaces ) ); + } +} + void QgsMeasureDialog::on_buttonBox_rejected( void ) { restart(); diff --git a/src/app/qgsmeasuredialog.h b/src/app/qgsmeasuredialog.h index 629ebe67411..b3196877130 100644 --- a/src/app/qgsmeasuredialog.h +++ b/src/app/qgsmeasuredialog.h @@ -47,6 +47,9 @@ class APP_EXPORT QgsMeasureDialog : public QDialog, private Ui::QgsMeasureBase //! Mose move void mouseMove( QgsPoint &point ); + //! Remove last point + void removeLastPoint(); + public slots: //! Reject void on_buttonBox_rejected( void ); @@ -96,6 +99,8 @@ class APP_EXPORT QgsMeasureDialog : public QDialog, private Ui::QgsMeasureBase //! pointer to measure tool which owns this dialog QgsMeasureTool* mTool; + + QgsPoint mLastMousePoint; }; #endif diff --git a/src/app/qgsmeasuretool.cpp b/src/app/qgsmeasuretool.cpp index 85ef19a3b1a..f2d54529e93 100644 --- a/src/app/qgsmeasuretool.cpp +++ b/src/app/qgsmeasuretool.cpp @@ -167,12 +167,54 @@ void QgsMeasureTool::canvasReleaseEvent( QMouseEvent * e ) mDone = false; } - // we allways add the clicked point to the measuring feature + // we always add the clicked point to the measuring feature addPoint( point ); mDialog->show(); } +void QgsMeasureTool::undo() +{ + if ( mRubberBand ) + { + if ( mPoints.size() < 1 ) + { + return; + } + + if ( mPoints.size() == 1 ) + { + //removing first point, so restart everything + restart(); + mDialog->restart(); + } + else + { + //remove second last point from line band, and last point from points band + mRubberBand->removePoint( -2, true ); + mRubberBandPoints->removePoint( -1, true ); + mPoints.removeLast(); + + mDialog->removeLastPoint(); + } + + } +} + +void QgsMeasureTool::keyPressEvent( QKeyEvent* e ) +{ + if (( e->key() == Qt::Key_Backspace || e->key() == Qt::Key_Delete ) ) + { + if ( !mDone ) + { + undo(); + } + + // Override default shortcut management in MapCanvas + e->ignore(); + } +} + void QgsMeasureTool::addPoint( QgsPoint &point ) { diff --git a/src/app/qgsmeasuretool.h b/src/app/qgsmeasuretool.h old mode 100644 new mode 100755 index 4516541988a..12576275ad7 --- a/src/app/qgsmeasuretool.h +++ b/src/app/qgsmeasuretool.h @@ -70,6 +70,8 @@ class APP_EXPORT QgsMeasureTool : public QgsMapTool //! called when map tool is being deactivated virtual void deactivate(); + virtual void keyPressEvent( QKeyEvent* e ); + public slots: //! updates the projections we're using void updateSettings(); @@ -102,6 +104,8 @@ class APP_EXPORT QgsMeasureTool : public QgsMapTool //@param p (pixel) coordinate QgsPoint snapPoint( const QPoint& p ); + /**Removes the last vertex from mRubberBand*/ + void undo(); }; #endif