Skip to content

Commit

Permalink
Apply patch #2936 (Units and decimal places of measure tools) from St…
Browse files Browse the repository at this point in the history
…efan Ziegler with small modifications

git-svn-id: http://svn.osgeo.org/qgis/trunk@14191 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
mhugent committed Sep 4, 2010
1 parent 69cd46b commit 505b766
Show file tree
Hide file tree
Showing 7 changed files with 118 additions and 46 deletions.
2 changes: 1 addition & 1 deletion python/core/qgsdistancearea.sip
Expand Up @@ -58,6 +58,6 @@ class QgsDistanceArea
//! compute bearing - in radians
double bearing(const QgsPoint& p1, const QgsPoint& p2);

static QString textUnit(double value, int decimals, QGis::UnitType u, bool isArea);
static QString textUnit( double value, int decimals, QGis::UnitType u, bool isArea, bool keepBaseUnit = false );

};
44 changes: 31 additions & 13 deletions src/app/qgsmeasuredialog.cpp
Expand Up @@ -51,6 +51,9 @@ QgsMeasureDialog::QgsMeasureDialog( QgsMeasureTool* tool, Qt::WFlags f )

//mTable->setHeaderLabels(QStringList() << tr("Segments (in meters)") << tr("Total") << tr("Azimuth") );

QSettings settings;


updateUi();
}

Expand Down Expand Up @@ -85,6 +88,9 @@ void QgsMeasureDialog::mousePress( QgsPoint &point )

void QgsMeasureDialog::mouseMove( QgsPoint &point )
{
QSettings settings;
int decimalPlaces = settings.value( "/qgis/measure/decimalplaces", "3" ).toInt();

// show current distance/area while moving the point
// by creating a temporary copy of point array
// and adding moving point at the end
Expand All @@ -93,29 +99,32 @@ void QgsMeasureDialog::mouseMove( QgsPoint &point )
QList<QgsPoint> tmpPoints = mTool->points();
tmpPoints.append( point );
double area = mTool->canvas()->mapRenderer()->distanceArea()->measurePolygon( tmpPoints );
editTotal->setText( formatArea( area ) );
editTotal->setText( formatArea( area, decimalPlaces ) );
}
else if ( !mMeasureArea && mTool->points().size() > 0 )
{
QgsPoint p1( mTool->points().last() ), p2( point );

double d = mTool->canvas()->mapRenderer()->distanceArea()->measureLine( p1, p2 );
editTotal->setText( formatDistance( mTotal + d ) );
editTotal->setText( formatDistance( mTotal + d, decimalPlaces ) );
QGis::UnitType myDisplayUnits;
// Ignore units
convertMeasurement( d, myDisplayUnits, false );
QTreeWidgetItem *item = mTable->topLevelItem( mTable->topLevelItemCount() - 1 );
item->setText( 0, QLocale::system().toString( d, 'f', 2 ) );
item->setText( 0, QLocale::system().toString( d, 'f', decimalPlaces ) );
}
}

void QgsMeasureDialog::addPoint( QgsPoint &point )
{
QSettings settings;
int decimalPlaces = settings.value( "/qgis/measure/decimalplaces", "3" ).toInt();

int numPoints = mTool->points().size();
if ( mMeasureArea && numPoints > 2 )
{
double area = mTool->canvas()->mapRenderer()->distanceArea()->measurePolygon( mTool->points() );
editTotal->setText( formatArea( area ) );
editTotal->setText( formatArea( area, decimalPlaces ) );
}
else if ( !mMeasureArea && numPoints > 1 )
{
Expand All @@ -126,16 +135,16 @@ void QgsMeasureDialog::addPoint( QgsPoint &point )
double d = mTool->canvas()->mapRenderer()->distanceArea()->measureLine( p1, p2 );

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

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

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

item = new QTreeWidgetItem( QStringList( QLocale::system().toString( 0.0, 'f', 2 ) ) );
item = new QTreeWidgetItem( QStringList( QLocale::system().toString( 0.0, 'f', decimalPlaces ) ) );
item->setTextAlignment( 0, Qt::AlignRight );
mTable->addTopLevelItem( item );
mTable->scrollToItem( item );
Expand Down Expand Up @@ -175,22 +184,31 @@ void QgsMeasureDialog::saveWindowLocation()
settings.setValue( key, height() );
}

QString QgsMeasureDialog::formatDistance( double distance )
QString QgsMeasureDialog::formatDistance( double distance, int decimalPlaces )
{
QSettings settings;
bool baseUnit = settings.value( "/qgis/measure/keepbaseunit", false ).toBool();

QGis::UnitType myDisplayUnits;
convertMeasurement( distance, myDisplayUnits, false );
return QgsDistanceArea::textUnit( distance, 2, myDisplayUnits, false );
return QgsDistanceArea::textUnit( distance, decimalPlaces, myDisplayUnits, false, baseUnit );
}

QString QgsMeasureDialog::formatArea( double area )
QString QgsMeasureDialog::formatArea( double area, int decimalPlaces )
{
QSettings settings;
bool baseUnit = settings.value( "/qgis/measure/keepbaseunit", false ).toBool();

QGis::UnitType myDisplayUnits;
convertMeasurement( area, myDisplayUnits, true );
return QgsDistanceArea::textUnit( area, 2, myDisplayUnits, true );
return QgsDistanceArea::textUnit( area, decimalPlaces, myDisplayUnits, true, baseUnit );
}

void QgsMeasureDialog::updateUi()
{
QSettings settings;
int decimalPlaces = settings.value( "/qgis/measure/decimalplaces", "3" ).toInt();

double dummy = 1.0;
QGis::UnitType myDisplayUnits;
// The dummy distance is ignored
Expand All @@ -216,12 +234,12 @@ void QgsMeasureDialog::updateUi()
if ( mMeasureArea )
{
mTable->hide();
editTotal->setText( formatArea( 0 ) );
editTotal->setText( formatArea( 0, decimalPlaces ) );
}
else
{
mTable->show();
editTotal->setText( formatDistance( 0 ) );
editTotal->setText( formatDistance( 0, decimalPlaces ) );
}

}
Expand Down
4 changes: 2 additions & 2 deletions src/app/qgsmeasuredialog.h
Expand Up @@ -66,10 +66,10 @@ class QgsMeasureDialog : public QDialog, private Ui::QgsMeasureBase
private:

//! formats distance to most appropriate units
QString formatDistance( double distance );
QString formatDistance( double distance, int decimalPlaces );

//! formats area to most appropriate units
QString formatArea( double area );
QString formatArea( double area, int decimalPlaces );

//! shows/hides table, shows correct units
void updateUi();
Expand Down
24 changes: 24 additions & 0 deletions src/app/qgsoptions.cpp
Expand Up @@ -202,6 +202,22 @@ QgsOptions::QgsOptions( QWidget *parent, Qt::WFlags fl ) :
mDegreesRadioButton->setChecked( true );
}

// set decimal places of the measure tool
int decimalPlaces = settings.value( "/qgis/measure/decimalplaces", "3" ).toInt();
mDecimalPlacesSpinBox->setRange( 0, 12 );
mDecimalPlacesSpinBox->setValue( decimalPlaces );

// set if base unit of measure tool should be changed
bool baseUnit = settings.value( "qgis/measure/keepbaseunit", false ).toBool();
if ( baseUnit == true )
{
mKeepBaseUnitCheckBox->setChecked( true );
}
else
{
mKeepBaseUnitCheckBox->setChecked( false );
}


// add the themes to the combo box on the option dialog
QDir myThemeDir( ":/images/themes/" );
Expand Down Expand Up @@ -580,6 +596,14 @@ void QgsOptions::saveOptions()
}
settings.setValue( "/qgis/measure/angleunits", angleUnitString );


int decimalPlaces = mDecimalPlacesSpinBox->value();
settings.setValue( "/qgis/measure/decimalplaces", decimalPlaces );

bool baseUnit = mKeepBaseUnitCheckBox->isChecked();
settings.setValue( "/qgis/measure/keepbaseunit", baseUnit );


//set the color for selections
QColor myColor = pbnSelectionColor->color();
settings.setValue( "/qgis/default_selection_color_red", myColor.red() );
Expand Down
36 changes: 21 additions & 15 deletions src/core/qgsdistancearea.cpp
Expand Up @@ -668,7 +668,7 @@ double QgsDistanceArea::computePolygonFlatArea( const QList<QgsPoint>& points )
return fabs( area ); // All areas are positive!
}

QString QgsDistanceArea::textUnit( double value, int decimals, QGis::UnitType u, bool isArea )
QString QgsDistanceArea::textUnit( double value, int decimals, QGis::UnitType u, bool isArea, bool keepBaseUnit )
{
QString unitLabel;

Expand All @@ -678,7 +678,11 @@ QString QgsDistanceArea::textUnit( double value, int decimals, QGis::UnitType u,
case QGis::Meters:
if ( isArea )
{
if ( fabs( value ) > 1000000.0 )
if ( keepBaseUnit )
{
unitLabel = QObject::tr( " m2" );
}
else if ( fabs( value ) > 1000000.0 )
{
unitLabel = QObject::tr( " km2" );
value = value / 1000000.0;
Expand All @@ -695,11 +699,9 @@ QString QgsDistanceArea::textUnit( double value, int decimals, QGis::UnitType u,
}
else
{
if ( fabs( value ) == 0.0 )
if ( keepBaseUnit || fabs( value ) == 0.0 )
{
// Special case for pretty printing.
unitLabel = QObject::tr( " m" );

}
else if ( fabs( value ) > 1000.0 )
{
Expand All @@ -725,29 +727,33 @@ QString QgsDistanceArea::textUnit( double value, int decimals, QGis::UnitType u,
case QGis::Feet:
if ( isArea )
{
if ( fabs( value ) > ( 528.0*528.0 ) )
if ( keepBaseUnit || fabs( value ) <= ( 528.0*528.0 ) )
{
unitLabel = QObject::tr( " sq mile" );
value = value / ( 5280.0 * 5280.0 );
unitLabel = QObject::tr( " sq ft" );
}
else
{
unitLabel = QObject::tr( " sq ft" );
unitLabel = QObject::tr( " sq mile" );
value = value / ( 5280.0 * 5280.0 );
}
}
else
{
if ( fabs( value ) > 528.0 )
{
unitLabel = QObject::tr( " mile" );
value = value / 5280.0;
}
else
if ( fabs( value ) <= 528.0 || keepBaseUnit )
{
if ( fabs( value ) == 1.0 )
{
unitLabel = QObject::tr( " foot" );
}
else
{
unitLabel = QObject::tr( " feet" );
}
}
else
{
unitLabel = QObject::tr( " mile" );
value = value / 5280.0;
}
}
break;
Expand Down
2 changes: 1 addition & 1 deletion src/core/qgsdistancearea.h
Expand Up @@ -85,7 +85,7 @@ class CORE_EXPORT QgsDistanceArea
//! compute bearing - in radians
double bearing( const QgsPoint& p1, const QgsPoint& p2 );

static QString textUnit( double value, int decimals, QGis::UnitType u, bool isArea );
static QString textUnit( double value, int decimals, QGis::UnitType u, bool isArea, bool keepBaseUnit = false );

protected:

Expand Down

0 comments on commit 505b766

Please sign in to comment.