Skip to content
Permalink
Browse files

Move handling of angular units to QgsUnitTypes, swap angle and distance

radio buttons to combo boxes in options dialog for consistency with
project properties
  • Loading branch information
nyalldawson committed Feb 15, 2016
1 parent dfdcec8 commit 6dc0b6910f3a9d41260ca4c3f81f64585d3be662
@@ -36,6 +36,18 @@ class QgsUnitTypes
UnknownAreaUnit, /*!< unknown areal unit */
};

//! Units of angles
enum AngleUnit
{
AngleDegrees = 0, /*!< degrees */
Radians, /*!< square kilometers */
Gon, /*!< gon/gradian */
MinutesOfArc, /*!< minutes of arc */
SecondsOfArc, /*!< seconds of arc */
Turn, /*!< turn/revolutions */
UnknownAngleUnit, /*!< unknown angle unit */
};

/** Returns the type for a distance unit.
*/
static DistanceUnitType unitType( QGis::UnitType unit );
@@ -120,5 +132,41 @@ class QgsUnitTypes
*/
static AreaUnit distanceToAreaUnit( QGis::UnitType distanceUnit );

// ANGULAR UNITS

/** Encodes an angular unit to a string.
* @param unit unit to encode
* @returns encoded string
* @see decodeAngleUnit()
*/
static QString encodeUnit( AngleUnit unit );

/** Decodes an angular unit from a string.
* @param string string to decode
* @param ok optional boolean, will be set to true if string was converted successfully
* @returns decoded units
* @see encodeUnit()
*/
static AngleUnit decodeAngleUnit( const QString& string, bool *ok = 0 );

/** Returns a translated string representing an angular unit.
* @param unit unit to convert to string
*/
static QString toString( AngleUnit unit );

/** Returns the conversion factor between the specified angular units.
* @param fromUnit angle unit to convert from
* @param toUnit angle unit to convert to
* @returns multiplication factor to convert between units
*/
static double fromUnitToUnitFactor( AngleUnit fromUnit, AngleUnit toUnit );

/** Returns an angle formatted as a friendly string.
* @param angle angle to format
* @param decimals number of decimal places to show
* @param unit unit of angle
* @returns formatted angle string
*/
static QString formatAngle( double angle, int decimals, AngleUnit unit );
};

@@ -16,6 +16,7 @@
#include "qgsdisplayangle.h"
#include "qgsmapcanvas.h"
#include "qgslogger.h"
#include "qgsunittypes.h"

#include <QSettings>
#include <cmath>
@@ -41,23 +42,7 @@ void QgsDisplayAngle::setValueInRadians( double value )
void QgsDisplayAngle::updateUi()
{
QSettings settings;
QString unitString = settings.value( "/qgis/measure/angleunits", "degrees" ).toString();
QgsUnitTypes::AngleUnit unit = QgsUnitTypes::decodeAngleUnit( settings.value( "/qgis/measure/angleunits", QgsUnitTypes::encodeUnit( QgsUnitTypes::AngleDegrees ) ).toString() );
int decimals = settings.value( "/qgis/measure/decimalplaces", "3" ).toInt();

if ( unitString == "degrees" )
{
mAngleLineEdit->setText( tr( "%1 degrees" ).arg( QLocale::system().toString( mValue * 180 / M_PI ),
'f', decimals ) );
}
else if ( unitString == "radians" )
{
mAngleLineEdit->setText( tr( "%1 radians" ).arg( QLocale::system().toString( mValue ),
'f', decimals ) );

}
else if ( unitString == "gon" )
{
mAngleLineEdit->setText( tr( "%1 gon" ).arg( QLocale::system().toString( mValue / M_PI * 200 ),
'f', decimals ) );
}
mAngleLineEdit->setText( QgsUnitTypes::formatAngle( mValue * QgsUnitTypes::fromUnitToUnitFactor( QgsUnitTypes::Radians, unit ), decimals, unit ) );
}
@@ -460,27 +460,17 @@ QgsOptions::QgsOptions( QWidget *parent, Qt::WindowFlags fl ) :
}

// Set the units for measuring
mDistanceUnitsComboBox->addItem( tr( "Meters" ), QGis::Meters );
mDistanceUnitsComboBox->addItem( tr( "Feet" ), QGis::Feet );
mDistanceUnitsComboBox->addItem( tr( "Nautical miles" ), QGis::NauticalMiles );
mDistanceUnitsComboBox->addItem( tr( "Degrees" ), QGis::Degrees );
mDistanceUnitsComboBox->addItem( tr( "Map units" ), QGis::UnknownUnit );

bool ok = false;
QGis::UnitType myDisplayUnits = QgsUnitTypes::decodeDistanceUnit( mSettings->value( "/qgis/measure/displayunits" ).toString(), &ok );
QGis::UnitType distanceUnits = QgsUnitTypes::decodeDistanceUnit( mSettings->value( "/qgis/measure/displayunits" ).toString(), &ok );
if ( !ok )
myDisplayUnits = QGis::Meters;

if ( myDisplayUnits == QGis::Feet )
{
radFeet->setChecked( true );
}
else if ( myDisplayUnits == QGis::NauticalMiles )
{
radNautical->setChecked( true );
}
else if ( myDisplayUnits == QGis::Degrees )
{
radDegrees->setChecked( true );
}
else
{
radMeters->setChecked( true );
}
distanceUnits = QGis::Meters;
mDistanceUnitsComboBox->setCurrentIndex( mDistanceUnitsComboBox->findData( distanceUnits ) );

mAreaUnitsComboBox->addItem( tr( "Square meters" ), QgsUnitTypes::SquareMeters );
mAreaUnitsComboBox->addItem( tr( "Square kilometers" ), QgsUnitTypes::SquareKilometers );
@@ -498,24 +488,15 @@ QgsOptions::QgsOptions( QWidget *parent, Qt::WindowFlags fl ) :
areaUnits = QgsUnitTypes::SquareMeters;
mAreaUnitsComboBox->setCurrentIndex( mAreaUnitsComboBox->findData( areaUnits ) );

QButtonGroup* angleButtonGroup = new QButtonGroup( this );
angleButtonGroup->addButton( mDegreesRadioButton );
angleButtonGroup->addButton( mRadiansRadioButton );
angleButtonGroup->addButton( mGonRadioButton );
mAngleUnitsComboBox->addItem( tr( "Degrees" ), QgsUnitTypes::AngleDegrees );
mAngleUnitsComboBox->addItem( tr( "Radians" ), QgsUnitTypes::Radians );
mAngleUnitsComboBox->addItem( tr( "Gon/gradians" ), QgsUnitTypes::Gon );
mAngleUnitsComboBox->addItem( tr( "Minutes of arc" ), QgsUnitTypes::MinutesOfArc );
mAngleUnitsComboBox->addItem( tr( "Seconds of arc" ), QgsUnitTypes::SecondsOfArc );
mAngleUnitsComboBox->addItem( tr( "Turns/revolutions" ), QgsUnitTypes::Turn );

QString myAngleUnitsTxt = mSettings->value( "/qgis/measure/angleunits", "degrees" ).toString();
if ( myAngleUnitsTxt == "gon" )
{
mGonRadioButton->setChecked( true );
}
else if ( myAngleUnitsTxt == "radians" )
{
mRadiansRadioButton->setChecked( true );
}
else //degrees
{
mDegreesRadioButton->setChecked( true );
}
QgsUnitTypes::AngleUnit unit = QgsUnitTypes::decodeAngleUnit( mSettings->value( "/qgis/measure/angleunits", QgsUnitTypes::encodeUnit( QgsUnitTypes::AngleDegrees ) ).toString() );
mAngleUnitsComboBox->setCurrentIndex( mAngleUnitsComboBox->findData( unit ) );

// set decimal places of the measure tool
int decimalPlaces = mSettings->value( "/qgis/measure/decimalplaces", "3" ).toInt();
@@ -1262,36 +1243,16 @@ void QgsOptions::saveOptions()

mSettings->setValue( "/Projections/showDatumTransformDialog", chkShowDatumTransformDialog->isChecked() );

if ( radFeet->isChecked() )
{
mSettings->setValue( "/qgis/measure/displayunits", QgsUnitTypes::encodeUnit( QGis::Feet ) );
}
else if ( radNautical->isChecked() )
{
mSettings->setValue( "/qgis/measure/displayunits", QgsUnitTypes::encodeUnit( QGis::NauticalMiles ) );
}
else if ( radDegrees->isChecked() )
{
mSettings->setValue( "/qgis/measure/displayunits", QgsUnitTypes::encodeUnit( QGis::Degrees ) );
}
else
{
mSettings->setValue( "/qgis/measure/displayunits", QgsUnitTypes::encodeUnit( QGis::Meters ) );
}
//measurement settings

QGis::UnitType distanceUnit = static_cast< QGis::UnitType >( mDistanceUnitsComboBox->itemData( mDistanceUnitsComboBox->currentIndex() ).toInt() );
mSettings->setValue( "/qgis/measure/displayunits", QgsUnitTypes::encodeUnit( distanceUnit ) );

QgsUnitTypes::AreaUnit areaUnit = static_cast< QgsUnitTypes::AreaUnit >( mAreaUnitsComboBox->itemData( mAreaUnitsComboBox->currentIndex() ).toInt() );
mSettings->setValue( "/qgis/measure/areaunits", QgsUnitTypes::encodeUnit( areaUnit ) );

QString angleUnitString = "degrees";
if ( mRadiansRadioButton->isChecked() )
{
angleUnitString = "radians";
}
else if ( mGonRadioButton->isChecked() )
{
angleUnitString = "gon";
}
mSettings->setValue( "/qgis/measure/angleunits", angleUnitString );
QgsUnitTypes::AngleUnit angleUnit = static_cast< QgsUnitTypes::AngleUnit >( mAngleUnitsComboBox->itemData( mAngleUnitsComboBox->currentIndex() ).toInt() );
mSettings->setValue( "/qgis/measure/angleunits", QgsUnitTypes::encodeUnit( angleUnit ) );

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

0 comments on commit 6dc0b69

Please sign in to comment.
You can’t perform that action at this time.