Skip to content

Commit

Permalink
Allow removing last point while measuring via del/backspace keys (fix #…
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Aug 30, 2014
1 parent acf1f70 commit 793db5f
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 1 deletion.
38 changes: 38 additions & 0 deletions src/app/qgsmeasuredialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<QgsPoint> 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();
Expand Down
5 changes: 5 additions & 0 deletions src/app/qgsmeasuredialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down Expand Up @@ -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
44 changes: 43 additions & 1 deletion src/app/qgsmeasuretool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 )
{
Expand Down
4 changes: 4 additions & 0 deletions src/app/qgsmeasuretool.h
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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

0 comments on commit 793db5f

Please sign in to comment.