Skip to content
Permalink
Browse files
Apply patch #2936 (Units and decimal places of measure tools) from St…
…efan Ziegler with small modifications

git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@14191 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
mhugent committed Sep 4, 2010
1 parent 8618b97 commit 15f272f4a37c58ce9ec943aeb1fb6739b96aa8fb
Showing with 118 additions and 46 deletions.
  1. +1 −1 python/core/qgsdistancearea.sip
  2. +31 −13 src/app/qgsmeasuredialog.cpp
  3. +2 −2 src/app/qgsmeasuredialog.h
  4. +24 −0 src/app/qgsoptions.cpp
  5. +21 −15 src/core/qgsdistancearea.cpp
  6. +1 −1 src/core/qgsdistancearea.h
  7. +38 −14 src/ui/qgsoptionsbase.ui
@@ -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 );

};
@@ -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();
}

@@ -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
@@ -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 )
{
@@ -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 );
@@ -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
@@ -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 ) );
}

}
@@ -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();
@@ -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/" );
@@ -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() );
@@ -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;

@@ -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;
@@ -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 )
{
@@ -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;
@@ -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:

Loading

0 comments on commit 15f272f

Please sign in to comment.