Cleaned up a bit, fixed area dialog also

This commit is contained in:
Magnus Homann 2012-08-27 14:13:58 +02:00
parent 54133eca44
commit 8b74b9e770
2 changed files with 49 additions and 66 deletions

View File

@ -55,12 +55,6 @@ QgsMeasureDialog::QgsMeasureDialog( QgsMeasureTool* tool, Qt::WFlags f )
// but at least every time any canvas related settings changes // but at least every time any canvas related settings changes
connect( mTool->canvas(), SIGNAL( mapCanvasRefreshed() ), connect( mTool->canvas(), SIGNAL( mapCanvasRefreshed() ),
this, SLOT( updateSettings() ) ); 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(); updateSettings();
} }
@ -113,9 +107,7 @@ void QgsMeasureDialog::updateSettings()
// clear interface // clear interface
mTable->clear(); mTable->clear();
QTreeWidgetItem* item = new QTreeWidgetItem( QStringList( QString::number( 0, 'f', 1 ) ) );
item->setTextAlignment( 0, Qt::AlignRight );
mTable->addTopLevelItem( item );
mTotal = 0; mTotal = 0;
updateUi(); updateUi();
@ -125,12 +117,7 @@ void QgsMeasureDialog::restart()
{ {
mTool->restart(); mTool->restart();
// Set one cell row where to update current distance
// If measuring area, the table doesn't get shown
mTable->clear(); mTable->clear();
QTreeWidgetItem* item = new QTreeWidgetItem( QStringList( QString::number( 0, 'f', 1 ) ) );
item->setTextAlignment( 0, Qt::AlignRight );
mTable->addTopLevelItem( item );
mTotal = 0.; mTotal = 0.;
updateUi(); updateUi();
} }
@ -138,11 +125,8 @@ void QgsMeasureDialog::restart()
void QgsMeasureDialog::mousePress( QgsPoint &point ) void QgsMeasureDialog::mousePress( QgsPoint &point )
{ {
if ( mTool->points().size() == 0 )
{ show();
addPoint( point );
show();
}
raise(); raise();
if ( ! mTool->done() ) if ( ! mTool->done() )
{ {
@ -152,25 +136,31 @@ void QgsMeasureDialog::mousePress( QgsPoint &point )
void QgsMeasureDialog::mouseMove( QgsPoint &point ) void QgsMeasureDialog::mouseMove( QgsPoint &point )
{ {
Q_UNUSED( point );
// show current distance/area while moving the point // show current distance/area while moving the point
// by creating a temporary copy of point array // by creating a temporary copy of point array
// and adding moving point at the end // and adding moving point at the end
if ( mMeasureArea && mTool->points().size() > 1 ) if ( mMeasureArea && mTool->points().size() > 2 )
{ {
QList<QgsPoint> tmpPoints = mTool->points(); double area = mDa.measurePolygon( mTool->points() );
tmpPoints.append( point );
double area = mDa.measurePolygon( tmpPoints );
editTotal->setText( formatArea( area ) ); 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 ); double d = mDa.measureLine( p1, p2 );
editTotal->setText( formatDistance( mTotal + d ) );
QGis::UnitType myDisplayUnits; mTotal = mDa.measureLine( mTool->points() );
// Ignore units editTotal->setText( formatDistance( mTotal ) );
convertMeasurement( d, myDisplayUnits, false );
QGis::UnitType displayUnits;
// Meters or feet?
convertMeasurement( d, displayUnits, false );
// Set moving
QTreeWidgetItem *item = mTable->topLevelItem( mTable->topLevelItemCount() - 1 ); QTreeWidgetItem *item = mTable->topLevelItem( mTable->topLevelItemCount() - 1 );
item->setText( 0, QLocale::system().toString( d, 'f', mDecimalPlaces ) ); item->setText( 0, QLocale::system().toString( d, 'f', mDecimalPlaces ) );
QgsDebugMsg( QString( "Final result is %1" ).arg( item->text( 0 ) ) ); QgsDebugMsg( QString( "Final result is %1" ).arg( item->text( 0 ) ) );
@ -181,6 +171,7 @@ void QgsMeasureDialog::addPoint( QgsPoint &p )
{ {
Q_UNUSED( p ); Q_UNUSED( p );
QgsDebugMsg( "Entering" );
int numPoints = mTool->points().size(); int numPoints = mTool->points().size();
if ( mMeasureArea && numPoints > 2 ) if ( mMeasureArea && numPoints > 2 )
{ {
@ -189,27 +180,12 @@ void QgsMeasureDialog::addPoint( QgsPoint &p )
} }
else if ( !mMeasureArea && numPoints > 1 ) else if ( !mMeasureArea && numPoints > 1 )
{ {
int last = numPoints - 2; QTreeWidgetItem * item = new QTreeWidgetItem( QStringList( QLocale::system().toString( 0.0, 'f', mDecimalPlaces ) ) );
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' ) ) );
item->setTextAlignment( 0, Qt::AlignRight ); item->setTextAlignment( 0, Qt::AlignRight );
mTable->addTopLevelItem( item ); mTable->addTopLevelItem( item );
mTable->scrollToItem( item ); mTable->scrollToItem( item );
} }
QgsDebugMsg( "Exiting" );
} }
void QgsMeasureDialog::on_buttonBox_rejected( void ) void QgsMeasureDialog::on_buttonBox_rejected( void )
@ -311,6 +287,7 @@ void QgsMeasureDialog::updateUi()
{ {
area = mDa.measurePolygon( mTool->points() ); area = mDa.measurePolygon( mTool->points() );
} }
mTable->hide(); // Hide the table, only show summary.
editTotal->setText( formatArea( area ) ); editTotal->setText( formatArea( area ) );
} }
else else
@ -326,15 +303,10 @@ void QgsMeasureDialog::updateUi()
if ( !b ) if ( !b )
{ {
double d = mDa.measureLine( p1, p2 ); double d = mDa.measureLine( p1, p2 );
mTotal += d; QGis::UnitType dummyUnits;
editTotal->setText( formatDistance( mTotal ) ); convertMeasurement( d, dummyUnits, false );
QGis::UnitType myDisplayUnits;
convertMeasurement( d, myDisplayUnits, false ); QTreeWidgetItem *item = new QTreeWidgetItem( QStringList( QLocale::system().toString( d , 'f', mDecimalPlaces ) ) );
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 ) ) );
item->setTextAlignment( 0, Qt::AlignRight ); item->setTextAlignment( 0, Qt::AlignRight );
mTable->addTopLevelItem( item ); mTable->addTopLevelItem( item );
mTable->scrollToItem( item ); mTable->scrollToItem( item );
@ -342,6 +314,9 @@ void QgsMeasureDialog::updateUi()
p1 = p2; p1 = p2;
b = false; b = false;
} }
mTotal = mDa.measureLine( mTool->points() );
mTable->show(); // Show the table with items
editTotal->setText( formatDistance( mTotal ) );
} }
} }

View File

@ -41,6 +41,8 @@ QgsMeasureTool::QgsMeasureTool( QgsMapCanvas* canvas, bool measureArea )
mCursor = QCursor( myCrossHairQPixmap, 8, 8 ); mCursor = QCursor( myCrossHairQPixmap, 8, 8 );
mDone = false; mDone = false;
// Append point we will move
mPoints.append( QgsPoint (0, 0) );
mDialog = new QgsMeasureDialog( this ); mDialog = new QgsMeasureDialog( this );
mSnapper.setMapCanvas( canvas ); mSnapper.setMapCanvas( canvas );
@ -96,6 +98,9 @@ void QgsMeasureTool::deactivate()
void QgsMeasureTool::restart() void QgsMeasureTool::restart()
{ {
mPoints.clear(); mPoints.clear();
// Append point we will move
mPoints.append( QgsPoint (0, 0) );
mRubberBand->reset( mMeasureArea ); mRubberBand->reset( mMeasureArea );
// re-read settings // re-read settings
@ -106,10 +111,6 @@ void QgsMeasureTool::restart()
} }
void QgsMeasureTool::updateSettings() void QgsMeasureTool::updateSettings()
{ {
QSettings settings; QSettings settings;
@ -128,10 +129,11 @@ void QgsMeasureTool::canvasPressEvent( QMouseEvent * e )
if ( e->button() == Qt::LeftButton ) if ( e->button() == Qt::LeftButton )
{ {
if ( mDone ) if ( mDone )
{
mDialog->restart(); mDialog->restart();
}
QgsPoint idPoint = snapPoint( e->pos() ); QgsPoint idPoint = snapPoint( e->pos() );
mDialog->mousePress( idPoint ); // mDialog->mousePress( idPoint );
} }
} }
@ -142,7 +144,13 @@ void QgsMeasureTool::canvasMoveEvent( QMouseEvent * e )
QgsPoint point = snapPoint( e->pos() ); QgsPoint point = snapPoint( e->pos() );
mRubberBand->movePoint( point ); mRubberBand->movePoint( point );
mDialog->mouseMove( point ); if( ! mPoints.isEmpty() )
{
// Update last point
mPoints.removeLast();
mPoints.append( point ) ;
mDialog->mouseMove( point );
}
} }
} }
@ -159,9 +167,8 @@ void QgsMeasureTool::canvasReleaseEvent( QMouseEvent * e )
} }
else else
{ {
// The figure is finished, store last point. // The figure is finished
mDone = true; mDone = true;
addPoint( point );
mDialog->show(); mDialog->show();
} }
} }
@ -178,14 +185,15 @@ void QgsMeasureTool::addPoint( QgsPoint &point )
{ {
QgsDebugMsg( "point=" + point.toString() ); QgsDebugMsg( "point=" + point.toString() );
int last = mPoints.size() - 1;
// don't add points with the same coordinates // 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; return;
QgsPoint pnt( point ); QgsPoint pnt( point );
// Append point that we will be moving.
mPoints.append( pnt ); mPoints.append( pnt );
mRubberBand->addPoint( point ); mRubberBand->addPoint( point );
if ( ! mDone ) if ( ! mDone )
{ {