consistent stringification of sexagesimal angle and time values with and without units

This commit is contained in:
Mathew Sutcliffe
2013-08-28 20:42:31 +01:00
parent 7117545fd6
commit 611f1ecaeb
2 changed files with 21 additions and 11 deletions

View File

@@ -15,12 +15,14 @@ using BlackMisc::Math::CMath;
/* /*
* Rounded to QString * Rounded to QString
*/ */
QString CAngleUnit::makeRoundedQString(double value, int digits, bool i18n) const QString CAngleUnit::makeRoundedQStringWithUnit(double value, int digits, bool i18n) const
{ {
if (digits < 0) digits = this->getDisplayDigits(); if (digits < 0) digits = this->getDisplayDigits();
QString s; QString s;
if ((*this) == CAngleUnit::sexagesimalDeg()) if ((*this) == CAngleUnit::sexagesimalDeg())
{ {
digits -= 4;
Q_ASSERT(digits >= 0);
double de = CMath::trunc(value); double de = CMath::trunc(value);
double mi = CMath::trunc((value - de) * 100.0); double mi = CMath::trunc((value - de) * 100.0);
double se = CMath::trunc((value - de - mi / 100.0) * 1000000) / 100.0; double se = CMath::trunc((value - de - mi / 100.0) * 1000000) / 100.0;
@@ -30,6 +32,8 @@ QString CAngleUnit::makeRoundedQString(double value, int digits, bool i18n) cons
} }
else if ((*this) == CAngleUnit::sexagesimalDegMin()) else if ((*this) == CAngleUnit::sexagesimalDegMin())
{ {
digits -= 2;
Q_ASSERT(digits >= 0);
double de = CMath::trunc(value); double de = CMath::trunc(value);
double mi = CMath::trunc((value - de) * 100.0); double mi = CMath::trunc((value - de) * 100.0);
const char *fmt = value < 0 ? "-%L1 %L2" : "%L1 %L2"; const char *fmt = value < 0 ? "-%L1 %L2" : "%L1 %L2";
@@ -38,7 +42,7 @@ QString CAngleUnit::makeRoundedQString(double value, int digits, bool i18n) cons
} }
else else
{ {
s = this->CMeasurementUnit::makeRoundedQString(value, digits, i18n); s = this->CMeasurementUnit::makeRoundedQStringWithUnit(value, digits, i18n);
} }
return s; return s;
} }
@@ -46,12 +50,14 @@ QString CAngleUnit::makeRoundedQString(double value, int digits, bool i18n) cons
/* /*
* Rounded to QString * Rounded to QString
*/ */
QString CTimeUnit::makeRoundedQString(double value, int digits, bool i18n) const QString CTimeUnit::makeRoundedQStringWithUnit(double value, int digits, bool i18n) const
{ {
if (digits < 0) digits = this->getDisplayDigits(); if (digits < 0) digits = this->getDisplayDigits();
QString s; QString s;
if ((*this) == CTimeUnit::hms()) if ((*this) == CTimeUnit::hms())
{ {
digits -= 4;
Q_ASSERT(digits >= 0);
double hr = CMath::trunc(value); double hr = CMath::trunc(value);
double mi = CMath::trunc((value - hr) * 100.0); double mi = CMath::trunc((value - hr) * 100.0);
double se = CMath::trunc((value - hr - mi / 100.0) * 1000000) / 100.0; double se = CMath::trunc((value - hr - mi / 100.0) * 1000000) / 100.0;
@@ -61,6 +67,8 @@ QString CTimeUnit::makeRoundedQString(double value, int digits, bool i18n) const
} }
else if ((*this) == CTimeUnit::hrmin()) else if ((*this) == CTimeUnit::hrmin())
{ {
digits -= 2;
Q_ASSERT(digits >= 0);
double hr = CMath::trunc(value); double hr = CMath::trunc(value);
double mi = CMath::trunc((value - hr) * 100.0); double mi = CMath::trunc((value - hr) * 100.0);
const char *fmt = value < 0 ? "-%L1h%L2m" : "%L1h%L2m"; const char *fmt = value < 0 ? "-%L1h%L2m" : "%L1h%L2m";
@@ -69,6 +77,8 @@ QString CTimeUnit::makeRoundedQString(double value, int digits, bool i18n) const
} }
else if ((*this) == CTimeUnit::minsec()) else if ((*this) == CTimeUnit::minsec())
{ {
digits -= 2;
Q_ASSERT(digits >= 0);
double mi = CMath::trunc(value); double mi = CMath::trunc(value);
double se = CMath::trunc((value - mi) * 100.0); double se = CMath::trunc((value - mi) * 100.0);
const char *fmt = value < 0 ? "-%L2m%L3s" : "%L2m%L3s"; const char *fmt = value < 0 ? "-%L2m%L3s" : "%L2m%L3s";
@@ -77,7 +87,7 @@ QString CTimeUnit::makeRoundedQString(double value, int digits, bool i18n) const
} }
else else
{ {
s = this->CMeasurementUnit::makeRoundedQString(value, digits, i18n); s = this->CMeasurementUnit::makeRoundedQStringWithUnit(value, digits, i18n);
} }
return s; return s;
} }

View File

@@ -227,7 +227,7 @@ public:
* \param i18n * \param i18n
* \return * \return
*/ */
virtual QString makeRoundedQString(double value, int digits = -1, bool i18n = false) const; virtual QString makeRoundedQStringWithUnit(double value, int digits = -1, bool i18n = false) const;
/*! /*!
* \brief Radians * \brief Radians
@@ -255,7 +255,7 @@ public:
*/ */
static const CAngleUnit &sexagesimalDeg() static const CAngleUnit &sexagesimalDeg()
{ {
static CAngleUnit deg(QT_TRANSLATE_NOOP("CMeasurementUnit", "degree, minute, second"), "DMS", new SubdivisionConverter2<60, 100, 60, 100>, 0); static CAngleUnit deg(QT_TRANSLATE_NOOP("CMeasurementUnit", "degree, minute, second"), "DMS", new SubdivisionConverter2<60, 100, 60, 100>, 4);
return deg; return deg;
(void)QT_TRANSLATE_NOOP("CMeasurementUnit", "%L1 %L2 %L3"); (void)QT_TRANSLATE_NOOP("CMeasurementUnit", "%L1 %L2 %L3");
(void)QT_TRANSLATE_NOOP("CMeasurementUnit", "-%L1 %L2 %L3"); (void)QT_TRANSLATE_NOOP("CMeasurementUnit", "-%L1 %L2 %L3");
@@ -267,7 +267,7 @@ public:
*/ */
static const CAngleUnit &sexagesimalDegMin() static const CAngleUnit &sexagesimalDegMin()
{ {
static CAngleUnit deg(QT_TRANSLATE_NOOP("CMeasurementUnit", "degree, minute"), "MinDec", new SubdivisionConverter<60, 100>, 2); static CAngleUnit deg(QT_TRANSLATE_NOOP("CMeasurementUnit", "degree, minute"), "MinDec", new SubdivisionConverter<60, 100>, 4);
return deg; return deg;
(void)QT_TRANSLATE_NOOP("CMeasurementUnit", "%L1 %L2"); (void)QT_TRANSLATE_NOOP("CMeasurementUnit", "%L1 %L2");
(void)QT_TRANSLATE_NOOP("CMeasurementUnit", "-%L1 %L2"); (void)QT_TRANSLATE_NOOP("CMeasurementUnit", "-%L1 %L2");
@@ -984,7 +984,7 @@ public:
* \param i18n * \param i18n
* \return * \return
*/ */
virtual QString makeRoundedQString(double value, int digits = -1, bool i18n = false) const; virtual QString makeRoundedQStringWithUnit(double value, int digits = -1, bool i18n = false) const;
/*! /*!
* \brief Second s * \brief Second s
@@ -1041,7 +1041,7 @@ public:
*/ */
static const CTimeUnit &hms() static const CTimeUnit &hms()
{ {
static CTimeUnit hms(QT_TRANSLATE_NOOP("CMeasurementUnit", "hour, minute, second"), "hms", new SubdivisionConverter2<60, 100, 60, 100>(3600), 0); static CTimeUnit hms(QT_TRANSLATE_NOOP("CMeasurementUnit", "hour, minute, second"), "hms", new SubdivisionConverter2<60, 100, 60, 100>(3600), 4);
return hms; return hms;
(void)QT_TRANSLATE_NOOP("CMeasurementUnit", "%L1h%L2m%L3s"); (void)QT_TRANSLATE_NOOP("CMeasurementUnit", "%L1h%L2m%L3s");
(void)QT_TRANSLATE_NOOP("CMeasurementUnit", "%-L1h%L2m%L3s"); (void)QT_TRANSLATE_NOOP("CMeasurementUnit", "%-L1h%L2m%L3s");
@@ -1052,7 +1052,7 @@ public:
*/ */
static const CTimeUnit &hrmin() static const CTimeUnit &hrmin()
{ {
static CTimeUnit hrmin(QT_TRANSLATE_NOOP("CMeasurementUnit", "hour, minute"), "hm", new SubdivisionConverter<60, 100>(3600), 1); static CTimeUnit hrmin(QT_TRANSLATE_NOOP("CMeasurementUnit", "hour, minute"), "hm", new SubdivisionConverter<60, 100>(3600), 3);
return hrmin; return hrmin;
(void)QT_TRANSLATE_NOOP("CMeasurementUnit", "%L1h%L2m"); (void)QT_TRANSLATE_NOOP("CMeasurementUnit", "%L1h%L2m");
(void)QT_TRANSLATE_NOOP("CMeasurementUnit", "-%L1h%L2m"); (void)QT_TRANSLATE_NOOP("CMeasurementUnit", "-%L1h%L2m");
@@ -1063,7 +1063,7 @@ public:
*/ */
static const CTimeUnit &minsec() static const CTimeUnit &minsec()
{ {
static CTimeUnit minsec(QT_TRANSLATE_NOOP("CMeasurementUnit", "minute, second"), "minsec", new SubdivisionConverter<60, 100>(60), 0); static CTimeUnit minsec(QT_TRANSLATE_NOOP("CMeasurementUnit", "minute, second"), "minsec", new SubdivisionConverter<60, 100>(60), 2);
return minsec; return minsec;
(void)QT_TRANSLATE_NOOP("CMeasurementUnit", "%L1m%L2s"); (void)QT_TRANSLATE_NOOP("CMeasurementUnit", "%L1m%L2s");
(void)QT_TRANSLATE_NOOP("CMeasurementUnit", "-%L1m%L2s"); (void)QT_TRANSLATE_NOOP("CMeasurementUnit", "-%L1m%L2s");