2016-11-02 17:35:26 +01:00
|
|
|
#include <QApplication>
|
2017-11-26 18:54:03 +01:00
|
|
|
#include "common/coordinates.h"
|
2016-11-02 17:35:26 +01:00
|
|
|
#include "format.h"
|
|
|
|
|
2018-02-11 20:39:39 +01:00
|
|
|
|
|
|
|
static QString deg2DMS(double val)
|
|
|
|
{
|
|
|
|
int deg = val;
|
|
|
|
double r1 = val - deg;
|
|
|
|
|
|
|
|
int min = r1 * 60.0;
|
|
|
|
double r2 = r1 - (min / 60.0);
|
|
|
|
|
|
|
|
double sec = r2 * 3600.0;
|
|
|
|
|
|
|
|
return QString("%1°%2'%3\"").arg(deg).arg(min, 2, 10, QChar('0'))
|
|
|
|
.arg(sec, 4, 'f', 1, QChar('0'));
|
|
|
|
}
|
|
|
|
|
2018-02-11 20:49:26 +01:00
|
|
|
static QString deg2DMM(double val)
|
2018-02-11 20:39:39 +01:00
|
|
|
{
|
|
|
|
int deg = val;
|
|
|
|
double r1 = val - deg;
|
|
|
|
|
|
|
|
double min = r1 * 60.0;
|
|
|
|
|
|
|
|
return QString("%1°%2'").arg(deg).arg(min, 6, 'f', 3, QChar('0'));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-09-29 11:43:09 +02:00
|
|
|
QString Format::timeSpan(qreal time, bool full)
|
2016-11-02 17:35:26 +01:00
|
|
|
{
|
|
|
|
unsigned h, m, s;
|
|
|
|
|
|
|
|
h = time / 3600;
|
|
|
|
m = (time - (h * 3600)) / 60;
|
|
|
|
s = time - (h * 3600) - (m * 60);
|
|
|
|
|
2017-09-29 11:43:09 +02:00
|
|
|
if (full || h)
|
|
|
|
return QString("%1:%2:%3").arg(h, 2, 10, QChar('0'))
|
|
|
|
.arg(m, 2, 10, QChar('0')).arg(s, 2, 10, QChar('0'));
|
|
|
|
else
|
|
|
|
return QString("%1:%2").arg(m, 2, 10, QChar('0'))
|
|
|
|
.arg(s, 2, 10, QChar('0'));
|
2016-11-02 17:35:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
QString Format::distance(qreal value, Units units)
|
|
|
|
{
|
2018-09-09 18:46:43 +02:00
|
|
|
QLocale l(QLocale::system());
|
|
|
|
|
2016-11-02 17:35:26 +01:00
|
|
|
if (units == Imperial) {
|
|
|
|
if (value < MIINM)
|
2018-09-09 18:46:43 +02:00
|
|
|
return l.toString(value * M2FT, 'f', 0) + UNIT_SPACE
|
2016-11-02 17:35:26 +01:00
|
|
|
+ qApp->translate("Format", "ft");
|
|
|
|
else
|
2018-09-09 18:46:43 +02:00
|
|
|
return l.toString(value * M2MI, 'f', 1) + UNIT_SPACE
|
2016-11-02 17:35:26 +01:00
|
|
|
+ qApp->translate("Format", "mi");
|
2018-02-11 23:51:57 +01:00
|
|
|
} else if (units == Nautical) {
|
|
|
|
if (value < NMIINM)
|
2018-09-09 18:46:43 +02:00
|
|
|
return l.toString(value * M2FT, 'f', 0) + UNIT_SPACE
|
2018-02-11 23:51:57 +01:00
|
|
|
+ qApp->translate("Format", "ft");
|
|
|
|
else
|
2018-09-09 18:46:43 +02:00
|
|
|
return l.toString(value * M2NMI, 'f', 1) + UNIT_SPACE
|
2018-02-11 23:51:57 +01:00
|
|
|
+ qApp->translate("Format", "nmi");
|
2016-11-02 17:35:26 +01:00
|
|
|
} else {
|
|
|
|
if (value < KMINM)
|
2018-09-09 18:46:43 +02:00
|
|
|
return l.toString(value, 'f', 0) + UNIT_SPACE
|
2016-11-02 17:35:26 +01:00
|
|
|
+ qApp->translate("Format", "m");
|
|
|
|
else
|
2018-09-09 18:46:43 +02:00
|
|
|
return l.toString(value * M2KM, 'f', 1) + UNIT_SPACE
|
2016-11-02 17:35:26 +01:00
|
|
|
+ qApp->translate("Format", "km");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QString Format::elevation(qreal value, Units units)
|
|
|
|
{
|
2018-09-09 18:46:43 +02:00
|
|
|
QLocale l(QLocale::system());
|
|
|
|
|
2016-11-02 17:35:26 +01:00
|
|
|
if (units == Metric)
|
2018-09-09 18:46:43 +02:00
|
|
|
return l.toString(qRound(value)) + UNIT_SPACE
|
2016-11-02 17:35:26 +01:00
|
|
|
+ qApp->translate("Format", "m");
|
|
|
|
else
|
2018-09-09 18:46:43 +02:00
|
|
|
return l.toString(qRound(value * M2FT)) + UNIT_SPACE
|
2016-11-02 17:35:26 +01:00
|
|
|
+ qApp->translate("Format", "ft");
|
|
|
|
}
|
|
|
|
|
2018-02-11 20:39:39 +01:00
|
|
|
QString Format::coordinates(const Coordinates &value, CoordinatesFormat type)
|
2016-11-02 17:35:26 +01:00
|
|
|
{
|
|
|
|
QChar yH = (value.lat() < 0) ? 'S' : 'N';
|
|
|
|
QChar xH = (value.lon() < 0) ? 'W' : 'E';
|
|
|
|
|
2018-02-11 20:39:39 +01:00
|
|
|
switch (type) {
|
|
|
|
case DegreesMinutes:
|
2018-02-11 20:49:26 +01:00
|
|
|
return deg2DMM(qAbs(value.lat())) + yH + "," + QChar(0x00A0)
|
|
|
|
+ deg2DMM(qAbs(value.lon())) + xH;
|
2018-02-11 20:39:39 +01:00
|
|
|
break;
|
|
|
|
case DMS:
|
|
|
|
return deg2DMS(qAbs(value.lat())) + yH + "," + QChar(0x00A0)
|
|
|
|
+ deg2DMS(qAbs(value.lon())) + xH;
|
|
|
|
break;
|
|
|
|
default:
|
2018-09-09 18:46:43 +02:00
|
|
|
QLocale l(QLocale::system());
|
|
|
|
return l.toString(qAbs(value.lat()), 'f', 5) + yH + ","
|
|
|
|
+ QChar(0x00A0) + l.toString(qAbs(value.lon()), 'f', 5) + xH;
|
2018-02-11 20:39:39 +01:00
|
|
|
}
|
2016-11-02 17:35:26 +01:00
|
|
|
}
|