Skip to content

Commit 15f272f

Browse files
author
mhugent
committedSep 4, 2010
Apply patch #2936 (Units and decimal places of measure tools) from Stefan Ziegler with small modifications
git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@14191 c8812cc2-4d05-0410-92ff-de0c093fc19c
1 parent 8618b97 commit 15f272f

File tree

7 files changed

+118
-46
lines changed

7 files changed

+118
-46
lines changed
 

‎python/core/qgsdistancearea.sip

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,6 @@ class QgsDistanceArea
5858
//! compute bearing - in radians
5959
double bearing(const QgsPoint& p1, const QgsPoint& p2);
6060

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

6363
};

‎src/app/qgsmeasuredialog.cpp

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ QgsMeasureDialog::QgsMeasureDialog( QgsMeasureTool* tool, Qt::WFlags f )
5151

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

54+
QSettings settings;
55+
56+
5457
updateUi();
5558
}
5659

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

8689
void QgsMeasureDialog::mouseMove( QgsPoint &point )
8790
{
91+
QSettings settings;
92+
int decimalPlaces = settings.value( "/qgis/measure/decimalplaces", "3" ).toInt();
93+
8894
// show current distance/area while moving the point
8995
// by creating a temporary copy of point array
9096
// and adding moving point at the end
@@ -93,29 +99,32 @@ void QgsMeasureDialog::mouseMove( QgsPoint &point )
9399
QList<QgsPoint> tmpPoints = mTool->points();
94100
tmpPoints.append( point );
95101
double area = mTool->canvas()->mapRenderer()->distanceArea()->measurePolygon( tmpPoints );
96-
editTotal->setText( formatArea( area ) );
102+
editTotal->setText( formatArea( area, decimalPlaces ) );
97103
}
98104
else if ( !mMeasureArea && mTool->points().size() > 0 )
99105
{
100106
QgsPoint p1( mTool->points().last() ), p2( point );
101107

102108
double d = mTool->canvas()->mapRenderer()->distanceArea()->measureLine( p1, p2 );
103-
editTotal->setText( formatDistance( mTotal + d ) );
109+
editTotal->setText( formatDistance( mTotal + d, decimalPlaces ) );
104110
QGis::UnitType myDisplayUnits;
105111
// Ignore units
106112
convertMeasurement( d, myDisplayUnits, false );
107113
QTreeWidgetItem *item = mTable->topLevelItem( mTable->topLevelItemCount() - 1 );
108-
item->setText( 0, QLocale::system().toString( d, 'f', 2 ) );
114+
item->setText( 0, QLocale::system().toString( d, 'f', decimalPlaces ) );
109115
}
110116
}
111117

112118
void QgsMeasureDialog::addPoint( QgsPoint &point )
113119
{
120+
QSettings settings;
121+
int decimalPlaces = settings.value( "/qgis/measure/decimalplaces", "3" ).toInt();
122+
114123
int numPoints = mTool->points().size();
115124
if ( mMeasureArea && numPoints > 2 )
116125
{
117126
double area = mTool->canvas()->mapRenderer()->distanceArea()->measurePolygon( mTool->points() );
118-
editTotal->setText( formatArea( area ) );
127+
editTotal->setText( formatArea( area, decimalPlaces ) );
119128
}
120129
else if ( !mMeasureArea && numPoints > 1 )
121130
{
@@ -126,16 +135,16 @@ void QgsMeasureDialog::addPoint( QgsPoint &point )
126135
double d = mTool->canvas()->mapRenderer()->distanceArea()->measureLine( p1, p2 );
127136

128137
mTotal += d;
129-
editTotal->setText( formatDistance( mTotal ) );
138+
editTotal->setText( formatDistance( mTotal, decimalPlaces ) );
130139

131140
QGis::UnitType myDisplayUnits;
132141
// Ignore units
133142
convertMeasurement( d, myDisplayUnits, false );
134143

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

138-
item = new QTreeWidgetItem( QStringList( QLocale::system().toString( 0.0, 'f', 2 ) ) );
147+
item = new QTreeWidgetItem( QStringList( QLocale::system().toString( 0.0, 'f', decimalPlaces ) ) );
139148
item->setTextAlignment( 0, Qt::AlignRight );
140149
mTable->addTopLevelItem( item );
141150
mTable->scrollToItem( item );
@@ -175,22 +184,31 @@ void QgsMeasureDialog::saveWindowLocation()
175184
settings.setValue( key, height() );
176185
}
177186

178-
QString QgsMeasureDialog::formatDistance( double distance )
187+
QString QgsMeasureDialog::formatDistance( double distance, int decimalPlaces )
179188
{
189+
QSettings settings;
190+
bool baseUnit = settings.value( "/qgis/measure/keepbaseunit", false ).toBool();
191+
180192
QGis::UnitType myDisplayUnits;
181193
convertMeasurement( distance, myDisplayUnits, false );
182-
return QgsDistanceArea::textUnit( distance, 2, myDisplayUnits, false );
194+
return QgsDistanceArea::textUnit( distance, decimalPlaces, myDisplayUnits, false, baseUnit );
183195
}
184196

185-
QString QgsMeasureDialog::formatArea( double area )
197+
QString QgsMeasureDialog::formatArea( double area, int decimalPlaces )
186198
{
199+
QSettings settings;
200+
bool baseUnit = settings.value( "/qgis/measure/keepbaseunit", false ).toBool();
201+
187202
QGis::UnitType myDisplayUnits;
188203
convertMeasurement( area, myDisplayUnits, true );
189-
return QgsDistanceArea::textUnit( area, 2, myDisplayUnits, true );
204+
return QgsDistanceArea::textUnit( area, decimalPlaces, myDisplayUnits, true, baseUnit );
190205
}
191206

192207
void QgsMeasureDialog::updateUi()
193208
{
209+
QSettings settings;
210+
int decimalPlaces = settings.value( "/qgis/measure/decimalplaces", "3" ).toInt();
211+
194212
double dummy = 1.0;
195213
QGis::UnitType myDisplayUnits;
196214
// The dummy distance is ignored
@@ -216,12 +234,12 @@ void QgsMeasureDialog::updateUi()
216234
if ( mMeasureArea )
217235
{
218236
mTable->hide();
219-
editTotal->setText( formatArea( 0 ) );
237+
editTotal->setText( formatArea( 0, decimalPlaces ) );
220238
}
221239
else
222240
{
223241
mTable->show();
224-
editTotal->setText( formatDistance( 0 ) );
242+
editTotal->setText( formatDistance( 0, decimalPlaces ) );
225243
}
226244

227245
}

‎src/app/qgsmeasuredialog.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,10 @@ class QgsMeasureDialog : public QDialog, private Ui::QgsMeasureBase
6666
private:
6767

6868
//! formats distance to most appropriate units
69-
QString formatDistance( double distance );
69+
QString formatDistance( double distance, int decimalPlaces );
7070

7171
//! formats area to most appropriate units
72-
QString formatArea( double area );
72+
QString formatArea( double area, int decimalPlaces );
7373

7474
//! shows/hides table, shows correct units
7575
void updateUi();

‎src/app/qgsoptions.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,22 @@ QgsOptions::QgsOptions( QWidget *parent, Qt::WFlags fl ) :
202202
mDegreesRadioButton->setChecked( true );
203203
}
204204

205+
// set decimal places of the measure tool
206+
int decimalPlaces = settings.value( "/qgis/measure/decimalplaces", "3" ).toInt();
207+
mDecimalPlacesSpinBox->setRange( 0, 12 );
208+
mDecimalPlacesSpinBox->setValue( decimalPlaces );
209+
210+
// set if base unit of measure tool should be changed
211+
bool baseUnit = settings.value( "qgis/measure/keepbaseunit", false ).toBool();
212+
if ( baseUnit == true )
213+
{
214+
mKeepBaseUnitCheckBox->setChecked( true );
215+
}
216+
else
217+
{
218+
mKeepBaseUnitCheckBox->setChecked( false );
219+
}
220+
205221

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

599+
600+
int decimalPlaces = mDecimalPlacesSpinBox->value();
601+
settings.setValue( "/qgis/measure/decimalplaces", decimalPlaces );
602+
603+
bool baseUnit = mKeepBaseUnitCheckBox->isChecked();
604+
settings.setValue( "/qgis/measure/keepbaseunit", baseUnit );
605+
606+
583607
//set the color for selections
584608
QColor myColor = pbnSelectionColor->color();
585609
settings.setValue( "/qgis/default_selection_color_red", myColor.red() );

‎src/core/qgsdistancearea.cpp

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -668,7 +668,7 @@ double QgsDistanceArea::computePolygonFlatArea( const QList<QgsPoint>& points )
668668
return fabs( area ); // All areas are positive!
669669
}
670670

671-
QString QgsDistanceArea::textUnit( double value, int decimals, QGis::UnitType u, bool isArea )
671+
QString QgsDistanceArea::textUnit( double value, int decimals, QGis::UnitType u, bool isArea, bool keepBaseUnit )
672672
{
673673
QString unitLabel;
674674

@@ -678,7 +678,11 @@ QString QgsDistanceArea::textUnit( double value, int decimals, QGis::UnitType u,
678678
case QGis::Meters:
679679
if ( isArea )
680680
{
681-
if ( fabs( value ) > 1000000.0 )
681+
if ( keepBaseUnit )
682+
{
683+
unitLabel = QObject::tr( " m2" );
684+
}
685+
else if ( fabs( value ) > 1000000.0 )
682686
{
683687
unitLabel = QObject::tr( " km2" );
684688
value = value / 1000000.0;
@@ -695,11 +699,9 @@ QString QgsDistanceArea::textUnit( double value, int decimals, QGis::UnitType u,
695699
}
696700
else
697701
{
698-
if ( fabs( value ) == 0.0 )
702+
if ( keepBaseUnit || fabs( value ) == 0.0 )
699703
{
700-
// Special case for pretty printing.
701704
unitLabel = QObject::tr( " m" );
702-
703705
}
704706
else if ( fabs( value ) > 1000.0 )
705707
{
@@ -725,29 +727,33 @@ QString QgsDistanceArea::textUnit( double value, int decimals, QGis::UnitType u,
725727
case QGis::Feet:
726728
if ( isArea )
727729
{
728-
if ( fabs( value ) > ( 528.0*528.0 ) )
730+
if ( keepBaseUnit || fabs( value ) <= ( 528.0*528.0 ) )
729731
{
730-
unitLabel = QObject::tr( " sq mile" );
731-
value = value / ( 5280.0 * 5280.0 );
732+
unitLabel = QObject::tr( " sq ft" );
732733
}
733734
else
734735
{
735-
unitLabel = QObject::tr( " sq ft" );
736+
unitLabel = QObject::tr( " sq mile" );
737+
value = value / ( 5280.0 * 5280.0 );
736738
}
737739
}
738740
else
739741
{
740-
if ( fabs( value ) > 528.0 )
741-
{
742-
unitLabel = QObject::tr( " mile" );
743-
value = value / 5280.0;
744-
}
745-
else
742+
if ( fabs( value ) <= 528.0 || keepBaseUnit )
746743
{
747744
if ( fabs( value ) == 1.0 )
745+
{
748746
unitLabel = QObject::tr( " foot" );
747+
}
749748
else
749+
{
750750
unitLabel = QObject::tr( " feet" );
751+
}
752+
}
753+
else
754+
{
755+
unitLabel = QObject::tr( " mile" );
756+
value = value / 5280.0;
751757
}
752758
}
753759
break;

‎src/core/qgsdistancearea.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ class CORE_EXPORT QgsDistanceArea
8585
//! compute bearing - in radians
8686
double bearing( const QgsPoint& p1, const QgsPoint& p2 );
8787

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

9090
protected:
9191

0 commit comments

Comments
 (0)
Please sign in to comment.