1 #include "CoordSymbol.h"
2 #include "FormatDegreesMinutesSecondsBase.h"
4 #include <QDoubleValidator>
10 const double DEGREES_TO_MINUTES = 60.0;
11 const double MINUTES_TO_SECONDS = 60.0;
12 const double DEGREES_TO_SECONDS = DEGREES_TO_MINUTES * MINUTES_TO_SECONDS;
13 const double MINUTES_TO_DEGREES = 1.0 / DEGREES_TO_MINUTES;
14 const double SECONDS_TO_DEGREES = 1.0 / (DEGREES_TO_MINUTES * MINUTES_TO_SECONDS);
20 FormatDegreesMinutesSecondsBase::~FormatDegreesMinutesSecondsBase()
26 LOG4CPP_INFO_S ((*mainCat)) <<
"FormatDegreesMinutesSecondsBase::formatOutputDegreesMinutesSeconds"
27 <<
" value=" << value;
30 bool negative = (value < 0);
32 int degrees = qFloor (value);
34 int minutes = value * DEGREES_TO_MINUTES;
35 value -= minutes * MINUTES_TO_DEGREES;
36 double seconds = value * DEGREES_TO_SECONDS;
37 degrees *= (negative ? -1.0 : 1.0);
39 return QString (
"%1%2 %3%4 %5%6")
41 .arg (QChar (COORD_SYMBOL_DEGREES))
43 .arg (QChar (COORD_SYMBOL_MINUTES_PRIME))
45 .arg (QChar (COORD_SYMBOL_SECONDS_DOUBLE_PRIME));
49 bool isNsHemisphere)
const
51 LOG4CPP_INFO_S ((*mainCat)) <<
"FormatDegreesMinutesSecondsBase::formatOutputDegreesMinutesSecondsNsew"
53 <<
" isNsHemisphere=" << (isNsHemisphere ?
"true" :
"false");
56 bool negative = (value < 0);
58 int degrees = qFloor (value);
60 int minutes = value * DEGREES_TO_MINUTES;
61 value -= minutes * MINUTES_TO_DEGREES;
62 double seconds = value * DEGREES_TO_SECONDS;
66 hemisphere = (negative ?
"S" :
"N");
68 hemisphere = (negative ?
"W" :
"E");
71 return QString (
"%1%2 %3%4 %5%6 %7")
73 .arg (QChar (COORD_SYMBOL_DEGREES))
75 .arg (QChar (COORD_SYMBOL_MINUTES_PRIME))
77 .arg (QChar (COORD_SYMBOL_SECONDS_DOUBLE_PRIME))
84 LOG4CPP_INFO_S ((*mainCat)) <<
"FormatDegreesMinutesSecondsBase::parseInput"
85 <<
" string=" << stringUntrimmed.toLatin1().data();
87 const QString
string = stringUntrimmed.trimmed ();
89 if (
string.isEmpty()) {
91 return QValidator::Intermediate;
95 QStringList fields =
string.split (QRegExp (
"\\s+"),
96 QString::SkipEmptyParts);
98 QString field0, field1, field2;
99 if (fields.count() == 0) {
100 return QValidator::Invalid;
102 field0 = fields.at(0);
103 if (fields.count() > 1) {
104 field1 = fields.at(1);
105 if (fields.count() > 2) {
106 field2 = fields.at(2);
107 if (fields.count() > 3) {
108 return QValidator::Invalid;
114 stripSymbols (field0,
121 QDoubleValidator valDegrees;
122 QDoubleValidator valMinutesOrSeconds;
123 valMinutesOrSeconds.setBottom (0);
125 double valueDegrees = 0, valueMinutes = 0, valueSeconds = 0;
128 QValidator::State state = valDegrees.validate (field0,
130 if (state == QValidator::Acceptable) {
132 valueDegrees = field0.toDouble();
134 if (fields.count() > 1) {
137 state = valMinutesOrSeconds.validate (field1,
139 if (state == QValidator::Acceptable) {
141 valueMinutes = field1.toDouble();
143 if (fields.count() > 2) {
146 state = valMinutesOrSeconds.validate (field2,
148 if (state == QValidator::Acceptable) {
150 valueSeconds = field2.toDouble();
158 if (state == QValidator::Acceptable) {
159 if (valueDegrees < 0) {
162 value = valueDegrees - valueMinutes * MINUTES_TO_DEGREES - valueSeconds * SECONDS_TO_DEGREES;
167 value = valueDegrees + valueMinutes * MINUTES_TO_DEGREES + valueSeconds * SECONDS_TO_DEGREES;
175 void FormatDegreesMinutesSecondsBase::stripSymbols (QString &field0,
177 QString &field2)
const
179 const int FIELD_WIDTH = 0, BASE_8 = 8, BASE_16 = 16;
182 QString strExpDegrees = QString (
".*\\0%1$")
183 .arg (COORD_SYMBOL_DEGREES, FIELD_WIDTH, BASE_8);
185 QRegExp regExpDegrees (strExpDegrees);
187 if (regExpDegrees.exactMatch (field0)) {
188 field0 = field0.left (field0.count() - 1);
192 QString strExpMinutes = QString (
".*[\\0%1\\x%2]$")
193 .arg (COORD_SYMBOL_MINUTES_APOSTROPHE, FIELD_WIDTH, BASE_8)
194 .arg (COORD_SYMBOL_MINUTES_PRIME, FIELD_WIDTH, BASE_16);
196 QRegExp regExpMinutes (strExpMinutes);
198 if (regExpMinutes.exactMatch (field1)) {
199 field1 = field1.left (field1.count() - 1);
203 QString strExpSeconds1Char = QString (
".*[\\x%1\\x%2]$")
204 .arg (COORD_SYMBOL_SECONDS_DOUBLE_PRIME, FIELD_WIDTH, BASE_16)
205 .arg (COORD_SYMBOL_SECONDS_QUOTATIONS, FIELD_WIDTH, BASE_16);
206 QString strExpSeconds2Chars = QString (
".*\\0%1\\0%2$")
207 .arg (COORD_SYMBOL_MINUTES_PRIME, FIELD_WIDTH, BASE_8)
208 .arg (COORD_SYMBOL_MINUTES_PRIME, FIELD_WIDTH, BASE_8);
210 QRegExp regExpSeconds1Char (strExpSeconds1Char), regExpSeconds2Chars (strExpSeconds2Chars);
212 if (regExpSeconds1Char.exactMatch (field2)) {
213 field2 = field2.left (field2.count() - 1);
215 if (regExpSeconds2Chars.exactMatch (field2)) {
216 field2 = field2.left (field2.count() - 2);