Skip to content
Permalink
Browse files

Merge pull request #1444 from avlisad/master

Improve mouse logic for measure tool. Fix #4628 Fix #10029
  • Loading branch information
NathanW2 committed Jun 12, 2014
2 parents ef70000 + 81593ed commit f1b5838392a4da8298a5848b2eed197ed15ecab8
Showing with 38 additions and 68 deletions.
  1. +20 −31 src/app/qgsmeasuredialog.cpp
  2. +0 −3 src/app/qgsmeasuredialog.h
  3. +18 −34 src/app/qgsmeasuretool.cpp
@@ -43,12 +43,6 @@ QgsMeasureDialog::QgsMeasureDialog( QgsMeasureTool* tool, Qt::WindowFlags f )
mMeasureArea = tool->measureArea();
mTotal = 0.;

// Set one cell row where to update current distance
// If measuring area, the table doesn't get shown
QTreeWidgetItem* item = new QTreeWidgetItem( QStringList( QString::number( 0, 'f', 1 ) ) );
item->setTextAlignment( 0, Qt::AlignRight );
mTable->addTopLevelItem( item );

updateSettings();
}

@@ -97,37 +91,24 @@ void QgsMeasureDialog::restart()
}


void QgsMeasureDialog::mousePress( QgsPoint &point )
{
show();
raise();
if ( ! mTool->done() )
{
mouseMove( point );
}
}

void QgsMeasureDialog::mouseMove( QgsPoint &point )
{
Q_UNUSED( point );

// show current distance/area while moving the point
// by creating a temporary copy of point array
// and adding moving point at the end
if ( mMeasureArea && mTool->points().size() > 2 )
if ( mMeasureArea && mTool->points().size() >= 2 )
{
double area = mDa.measurePolygon( mTool->points() );
QList<QgsPoint> tmpPoints = mTool->points();
tmpPoints.append( point );
double area = mDa.measurePolygon( tmpPoints );
editTotal->setText( formatArea( area ) );
}
else if ( !mMeasureArea && mTool->points().size() > 1 )
else if ( !mMeasureArea && mTool->points().size() >= 1 )
{
int last = mTool->points().size() - 1;
QgsPoint p1 = mTool->points()[last];
QgsPoint p2 = mTool->points()[last-1];
QgsPoint p1( mTool->points().last() ), p2( point );
double d = mDa.measureLine( p1, p2 );

mTotal = mDa.measureLine( mTool->points() );
editTotal->setText( formatDistance( mTotal ) );
editTotal->setText( formatDistance( mTotal + d ) );

QGis::UnitType displayUnits;
// Meters or feet?
@@ -150,12 +131,20 @@ void QgsMeasureDialog::addPoint( QgsPoint &p )
double area = mDa.measurePolygon( mTool->points() );
editTotal->setText( formatArea( area ) );
}
else if ( !mMeasureArea && numPoints > 1 )
else if ( !mMeasureArea && numPoints >= 1 )
{
QTreeWidgetItem * item = new QTreeWidgetItem( QStringList( QLocale::system().toString( 0.0, 'f', mDecimalPlaces ) ) );
item->setTextAlignment( 0, Qt::AlignRight );
mTable->addTopLevelItem( item );
mTable->scrollToItem( item );
if ( !mTool->done() )
{
QTreeWidgetItem * item = new QTreeWidgetItem( QStringList( QLocale::system().toString( 0.0, 'f', mDecimalPlaces ) ) );
item->setTextAlignment( 0, Qt::AlignRight );
mTable->addTopLevelItem( item );
mTable->scrollToItem( item );
}
if ( numPoints > 1)
{
mTotal = mDa.measureLine( mTool->points() );
editTotal->setText( formatDistance( mTotal ) );
}
}
QgsDebugMsg( "Exiting" );
}
@@ -47,9 +47,6 @@ class APP_EXPORT QgsMeasureDialog : public QDialog, private Ui::QgsMeasureBase
//! Mose move
void mouseMove( QgsPoint &point );

//! Mouse press
void mousePress( QgsPoint &point );

public slots:
//! Reject
void on_buttonBox_rejected( void );
@@ -136,16 +136,7 @@ void QgsMeasureTool::updateSettings()

void QgsMeasureTool::canvasPressEvent( QMouseEvent * e )
{
if ( e->button() == Qt::LeftButton )
{
if ( mDone )
{
mDialog->restart();
QgsPoint point = snapPoint( e->pos() );
addPoint( point );
mDone = false;
}
}
Q_UNUSED( e );
}

void QgsMeasureTool::canvasMoveEvent( QMouseEvent * e )
@@ -155,13 +146,7 @@ void QgsMeasureTool::canvasMoveEvent( QMouseEvent * e )
QgsPoint point = snapPoint( e->pos() );

mRubberBand->movePoint( point );
if ( ! mPoints.isEmpty() )
{
// Update last point
mPoints.removeLast();
mPoints.append( point ) ;
mDialog->mouseMove( point );
}
mDialog->mouseMove( point );
}
}

@@ -170,45 +155,44 @@ void QgsMeasureTool::canvasReleaseEvent( QMouseEvent * e )
{
QgsPoint point = snapPoint( e->pos() );

if ( e->button() == Qt::RightButton && ( e->buttons() & Qt::LeftButton ) == 0 ) // restart
if ( mDone ) // if we have stopped measuring any mouse click restart measuring
{
mDialog->restart();
}

if ( e->button() == Qt::RightButton ) // if we clicked the rigth button we stop measuring
{
if ( mDone )
{
mDialog->restart();
}
else
{
// The figure is finished
mDone = true;
mDialog->show();
}
mDone = true;
}
else if ( e->button() == Qt::LeftButton )
{
// Append point we will move
addPoint( point );
mDialog->show();
mDone = false;
}

// we allways add the clicked point to the measuring feature
addPoint( point );
mDialog->show();

}


void QgsMeasureTool::addPoint( QgsPoint &point )
{
QgsDebugMsg( "point=" + point.toString() );

int last = mPoints.size() - 1;
// don't add points with the same coordinates
if ( mPoints.size() > 1 && mPoints[ last ] == mPoints[ last - 1 ] )
if ( !mPoints.isEmpty() && mPoints.last() == point )
{
return;
}

QgsPoint pnt( point );
// Append point that we will be moving.
mPoints.append( pnt );

mRubberBand->addPoint( point );
mRubberBandPoints->addPoint( point );
if ( ! mDone )
if ( ! mDone ) // Prevent the insertion of a new item in segments measure table
{
mDialog->addPoint( point );
}

0 comments on commit f1b5838

Please sign in to comment.
You can’t perform that action at this time.