7 #include "CoordSymbol.h" 8 #include "FormatDegreesMinutesSecondsBase.h" 10 #include <QDoubleValidator> 13 #include <QStringList> 16 const double DEGREES_TO_MINUTES = 60.0;
17 const double MINUTES_TO_SECONDS = 60.0;
18 const double DEGREES_TO_SECONDS = DEGREES_TO_MINUTES * MINUTES_TO_SECONDS;
19 const double MINUTES_TO_DEGREES = 1.0 / DEGREES_TO_MINUTES;
20 const double SECONDS_TO_DEGREES = 1.0 / (DEGREES_TO_MINUTES * MINUTES_TO_SECONDS);
26 FormatDegreesMinutesSecondsBase::~FormatDegreesMinutesSecondsBase()
32 LOG4CPP_INFO_S ((*mainCat)) <<
"FormatDegreesMinutesSecondsBase::formatOutputDegreesMinutesSeconds" 33 <<
" value=" << value;
36 bool negative = (value < 0);
38 int degrees = qFloor (value);
40 int minutes = value * DEGREES_TO_MINUTES;
41 value -= minutes * MINUTES_TO_DEGREES;
42 double seconds = value * DEGREES_TO_SECONDS;
43 degrees *= (negative ? -1.0 : 1.0);
45 return QString (
"%1%2 %3%4 %5%6")
47 .arg (QChar (COORD_SYMBOL_DEGREES))
49 .arg (QChar (COORD_SYMBOL_MINUTES_PRIME))
51 .arg (QChar (COORD_SYMBOL_SECONDS_DOUBLE_PRIME));
55 bool isNsHemisphere)
const 57 LOG4CPP_INFO_S ((*mainCat)) <<
"FormatDegreesMinutesSecondsBase::formatOutputDegreesMinutesSecondsNsew" 59 <<
" isNsHemisphere=" << (isNsHemisphere ?
"true" :
"false");
62 bool negative = (value < 0);
64 int degrees = qFloor (value);
66 int minutes = value * DEGREES_TO_MINUTES;
67 value -= minutes * MINUTES_TO_DEGREES;
68 double seconds = value * DEGREES_TO_SECONDS;
72 hemisphere = (negative ?
"S" :
"N");
74 hemisphere = (negative ?
"W" :
"E");
77 return QString (
"%1%2 %3%4 %5%6 %7")
79 .arg (QChar (COORD_SYMBOL_DEGREES))
81 .arg (QChar (COORD_SYMBOL_MINUTES_PRIME))
83 .arg (QChar (COORD_SYMBOL_SECONDS_DOUBLE_PRIME))
90 LOG4CPP_INFO_S ((*mainCat)) <<
"FormatDegreesMinutesSecondsBase::parseInput" 91 <<
" string=" << stringUntrimmed.toLatin1().data();
93 const QString
string = stringUntrimmed.trimmed ();
95 if (
string.isEmpty()) {
97 return QValidator::Intermediate;
101 QStringList fields =
string.split (QRegExp (
"\\s+"),
102 QString::SkipEmptyParts);
104 QString field0, field1, field2;
105 if (fields.count() == 0) {
106 return QValidator::Invalid;
108 field0 = fields.at(0);
109 if (fields.count() > 1) {
110 field1 = fields.at(1);
111 if (fields.count() > 2) {
112 field2 = fields.at(2);
113 if (fields.count() > 3) {
114 return QValidator::Invalid;
120 stripSymbols (field0,
127 QDoubleValidator valDegrees;
128 QDoubleValidator valMinutesOrSeconds;
129 valMinutesOrSeconds.setBottom (0);
131 double valueDegrees = 0, valueMinutes = 0, valueSeconds = 0;
134 QValidator::State state = valDegrees.validate (field0,
136 if (state == QValidator::Acceptable) {
138 valueDegrees = field0.toDouble();
140 if (fields.count() > 1) {
143 state = valMinutesOrSeconds.validate (field1,
145 if (state == QValidator::Acceptable) {
147 valueMinutes = field1.toDouble();
149 if (fields.count() > 2) {
152 state = valMinutesOrSeconds.validate (field2,
154 if (state == QValidator::Acceptable) {
156 valueSeconds = field2.toDouble();
164 if (state == QValidator::Acceptable) {
165 if (valueDegrees < 0) {
168 value = valueDegrees - valueMinutes * MINUTES_TO_DEGREES - valueSeconds * SECONDS_TO_DEGREES;
173 value = valueDegrees + valueMinutes * MINUTES_TO_DEGREES + valueSeconds * SECONDS_TO_DEGREES;
181 void FormatDegreesMinutesSecondsBase::stripSymbols (QString &field0,
183 QString &field2)
const 185 const int FIELD_WIDTH = 0, BASE_8 = 8, BASE_16 = 16;
188 QString strExpDegrees = QString (
".*\\0%1$")
189 .arg (COORD_SYMBOL_DEGREES, FIELD_WIDTH, BASE_8);
191 QRegExp regExpDegrees (strExpDegrees);
193 if (regExpDegrees.exactMatch (field0)) {
194 field0 = field0.left (field0.count() - 1);
198 QString strExpMinutes = QString (
".*[\\0%1\\x%2]$")
199 .arg (COORD_SYMBOL_MINUTES_APOSTROPHE, FIELD_WIDTH, BASE_8)
200 .arg (COORD_SYMBOL_MINUTES_PRIME, FIELD_WIDTH, BASE_16);
202 QRegExp regExpMinutes (strExpMinutes);
204 if (regExpMinutes.exactMatch (field1)) {
205 field1 = field1.left (field1.count() - 1);
209 QString strExpSeconds1Char = QString (
".*[\\x%1\\x%2]$")
210 .arg (COORD_SYMBOL_SECONDS_DOUBLE_PRIME, FIELD_WIDTH, BASE_16)
211 .arg (COORD_SYMBOL_SECONDS_QUOTATIONS, FIELD_WIDTH, BASE_16);
212 QString strExpSeconds2Chars = QString (
".*\\0%1\\0%2$")
213 .arg (COORD_SYMBOL_MINUTES_PRIME, FIELD_WIDTH, BASE_8)
214 .arg (COORD_SYMBOL_MINUTES_PRIME, FIELD_WIDTH, BASE_8);
216 QRegExp regExpSeconds1Char (strExpSeconds1Char), regExpSeconds2Chars (strExpSeconds2Chars);
218 if (regExpSeconds1Char.exactMatch (field2)) {
219 field2 = field2.left (field2.count() - 1);
221 if (regExpSeconds2Chars.exactMatch (field2)) {
222 field2 = field2.left (field2.count() - 2);