Skip to content

Commit

Permalink
Cleaned up a bit, fixed area dialog also
Browse files Browse the repository at this point in the history
  • Loading branch information
homann committed Sep 4, 2012
1 parent 35cf2d6 commit b0e3d71
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 66 deletions.
85 changes: 30 additions & 55 deletions src/app/qgsmeasuredialog.cpp
Expand Up @@ -55,12 +55,6 @@ QgsMeasureDialog::QgsMeasureDialog( QgsMeasureTool* tool, Qt::WFlags f )
// but at least every time any canvas related settings changes
connect( mTool->canvas(), SIGNAL( mapCanvasRefreshed() ),
this, SLOT( updateSettings() ) );
// // Update when project wide transformation has changed
// connect( mTool->canvas()->mapRenderer(), SIGNAL( hasCrsTransformEnabled( bool ) ),
// this, SLOT( changeProjectionEnabledState() ) );
// // Update when project CRS has changed
// connect( mTool->canvas()->mapRenderer(), SIGNAL( destinationSrsChanged() ),
// this, SLOT( changeProjectionEnabledState() ) );

updateSettings();
}
Expand Down Expand Up @@ -113,9 +107,7 @@ void QgsMeasureDialog::updateSettings()

// clear interface
mTable->clear();
QTreeWidgetItem* item = new QTreeWidgetItem( QStringList( QString::number( 0, 'f', 1 ) ) );
item->setTextAlignment( 0, Qt::AlignRight );
mTable->addTopLevelItem( item );

mTotal = 0;
updateUi();

Expand All @@ -125,24 +117,16 @@ void QgsMeasureDialog::restart()
{
mTool->restart();

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


void QgsMeasureDialog::mousePress( QgsPoint &point )
{
if ( mTool->points().size() == 0 )
{
addPoint( point );
show();
}

show();
raise();
if ( ! mTool->done() )
{
Expand All @@ -152,25 +136,31 @@ void QgsMeasureDialog::mousePress( QgsPoint &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() > 1 )
if ( mMeasureArea && mTool->points().size() > 2 )
{
QList<QgsPoint> tmpPoints = mTool->points();
tmpPoints.append( point );
double area = mDa.measurePolygon( tmpPoints );
double area = mDa.measurePolygon( mTool->points() );
editTotal->setText( formatArea( area ) );
}
else if ( !mMeasureArea && mTool->points().size() > 0 )
else if ( !mMeasureArea && mTool->points().size() > 1 )
{
QgsPoint p1( mTool->points().last() ), p2( point );

int last = mTool->points().size() - 1;
QgsPoint p1 = mTool->points()[last];
QgsPoint p2 = mTool->points()[last-1];
double d = mDa.measureLine( p1, p2 );
editTotal->setText( formatDistance( mTotal + d ) );
QGis::UnitType myDisplayUnits;
// Ignore units
convertMeasurement( d, myDisplayUnits, false );

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

QGis::UnitType displayUnits;
// Meters or feet?
convertMeasurement( d, displayUnits, false );

// Set moving
QTreeWidgetItem *item = mTable->topLevelItem( mTable->topLevelItemCount() - 1 );
item->setText( 0, QLocale::system().toString( d, 'f', mDecimalPlaces ) );
QgsDebugMsg( QString( "Final result is %1" ).arg( item->text( 0 ) ) );
Expand All @@ -181,6 +171,7 @@ void QgsMeasureDialog::addPoint( QgsPoint &p )
{
Q_UNUSED( p );

QgsDebugMsg( "Entering" );
int numPoints = mTool->points().size();
if ( mMeasureArea && numPoints > 2 )
{
Expand All @@ -189,27 +180,12 @@ void QgsMeasureDialog::addPoint( QgsPoint &p )
}
else if ( !mMeasureArea && numPoints > 1 )
{
int last = numPoints - 2;

QgsPoint p1 = mTool->points()[last], p2 = mTool->points()[last+1];

double d = mDa.measureLine( p1, p2 );

mTotal += d;
editTotal->setText( formatDistance( mTotal ) );

QGis::UnitType myDisplayUnits;
// Ignore units
convertMeasurement( d, myDisplayUnits, false );

QTreeWidgetItem *item = mTable->topLevelItem( mTable->topLevelItemCount() - 1 );
item->setText( 0, QLocale::system().toString( d, 'f' ) );

item = new QTreeWidgetItem( QStringList( QLocale::system().toString( 0.0, 'f' ) ) );
QTreeWidgetItem * item = new QTreeWidgetItem( QStringList( QLocale::system().toString( 0.0, 'f', mDecimalPlaces ) ) );
item->setTextAlignment( 0, Qt::AlignRight );
mTable->addTopLevelItem( item );
mTable->scrollToItem( item );
}
QgsDebugMsg( "Exiting" );
}

void QgsMeasureDialog::on_buttonBox_rejected( void )
Expand Down Expand Up @@ -311,6 +287,7 @@ void QgsMeasureDialog::updateUi()
{
area = mDa.measurePolygon( mTool->points() );
}
mTable->hide(); // Hide the table, only show summary.
editTotal->setText( formatArea( area ) );
}
else
Expand All @@ -326,22 +303,20 @@ void QgsMeasureDialog::updateUi()
if ( !b )
{
double d = mDa.measureLine( p1, p2 );
mTotal += d;
editTotal->setText( formatDistance( mTotal ) );
QGis::UnitType myDisplayUnits;

convertMeasurement( d, myDisplayUnits, false );
QGis::UnitType dummyUnits;
convertMeasurement( d, dummyUnits, false );

QTreeWidgetItem *item = mTable->topLevelItem( mTable->topLevelItemCount() - 1 );
item->setText( 0, QLocale::system().toString( d, 'f', mDecimalPlaces ) );
item = new QTreeWidgetItem( QStringList( QLocale::system().toString( 0.0, 'f', mDecimalPlaces ) ) );
QTreeWidgetItem *item = new QTreeWidgetItem( QStringList( QLocale::system().toString( d , 'f', mDecimalPlaces ) ) );
item->setTextAlignment( 0, Qt::AlignRight );
mTable->addTopLevelItem( item );
mTable->scrollToItem( item );
}
p1 = p2;
b = false;
}
mTotal = mDa.measureLine( mTool->points() );
mTable->show(); // Show the table with items
editTotal->setText( formatDistance( mTotal ) );
}
}

Expand Down
30 changes: 19 additions & 11 deletions src/app/qgsmeasuretool.cpp
Expand Up @@ -41,6 +41,8 @@ QgsMeasureTool::QgsMeasureTool( QgsMapCanvas* canvas, bool measureArea )
mCursor = QCursor( myCrossHairQPixmap, 8, 8 );

mDone = false;
// Append point we will move
mPoints.append( QgsPoint (0, 0) );

mDialog = new QgsMeasureDialog( this );
mSnapper.setMapCanvas( canvas );
Expand Down Expand Up @@ -96,6 +98,9 @@ void QgsMeasureTool::deactivate()
void QgsMeasureTool::restart()
{
mPoints.clear();
// Append point we will move
mPoints.append( QgsPoint (0, 0) );

mRubberBand->reset( mMeasureArea );

// re-read settings
Expand All @@ -106,10 +111,6 @@ void QgsMeasureTool::restart()

}





void QgsMeasureTool::updateSettings()
{
QSettings settings;
Expand All @@ -128,10 +129,11 @@ void QgsMeasureTool::canvasPressEvent( QMouseEvent * e )
if ( e->button() == Qt::LeftButton )
{
if ( mDone )
{
mDialog->restart();

}
QgsPoint idPoint = snapPoint( e->pos() );
mDialog->mousePress( idPoint );
// mDialog->mousePress( idPoint );
}
}

Expand All @@ -142,7 +144,13 @@ void QgsMeasureTool::canvasMoveEvent( QMouseEvent * e )
QgsPoint point = snapPoint( e->pos() );

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

Expand All @@ -159,9 +167,8 @@ void QgsMeasureTool::canvasReleaseEvent( QMouseEvent * e )
}
else
{
// The figure is finished, store last point.
// The figure is finished
mDone = true;
addPoint( point );
mDialog->show();
}
}
Expand All @@ -178,14 +185,15 @@ 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() > 0 && point == mPoints[0] )
if ( mPoints.size() > 1 && mPoints[ last ] == mPoints[ last - 1 ] )
return;

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


mRubberBand->addPoint( point );
if ( ! mDone )
{
Expand Down

0 comments on commit b0e3d71

Please sign in to comment.