00001
00002
00003
00004
00005
00006
00007 #include "CoordSymbol.h"
00008 #include "FormatDegreesMinutesSecondsBase.h"
00009 #include "Logger.h"
00010 #include <QDoubleValidator>
00011 #include <qmath.h>
00012 #include <QRegExp>
00013 #include <QStringList>
00014 #include <QValidator>
00015
00016 const double DEGREES_TO_MINUTES = 60.0;
00017 const double MINUTES_TO_SECONDS = 60.0;
00018 const double DEGREES_TO_SECONDS = DEGREES_TO_MINUTES * MINUTES_TO_SECONDS;
00019 const double MINUTES_TO_DEGREES = 1.0 / DEGREES_TO_MINUTES;
00020 const double SECONDS_TO_DEGREES = 1.0 / (DEGREES_TO_MINUTES * MINUTES_TO_SECONDS);
00021
00022 FormatDegreesMinutesSecondsBase::FormatDegreesMinutesSecondsBase()
00023 {
00024 }
00025
00026 FormatDegreesMinutesSecondsBase::~FormatDegreesMinutesSecondsBase()
00027 {
00028 }
00029
00030 QString FormatDegreesMinutesSecondsBase::formatOutputDegreesMinutesSeconds (double value) const
00031 {
00032 LOG4CPP_INFO_S ((*mainCat)) << "FormatDegreesMinutesSecondsBase::formatOutputDegreesMinutesSeconds"
00033 << " value=" << value;
00034
00035
00036 bool negative = (value < 0);
00037 value = qAbs (value);
00038 int degrees = qFloor (value);
00039 value -= degrees;
00040 int minutes = value * DEGREES_TO_MINUTES;
00041 value -= minutes * MINUTES_TO_DEGREES;
00042 double seconds = value * DEGREES_TO_SECONDS;
00043 degrees *= (negative ? -1.0 : 1.0);
00044
00045 return QString ("%1%2 %3%4 %5%6")
00046 .arg (degrees)
00047 .arg (QChar (COORD_SYMBOL_DEGREES))
00048 .arg (minutes)
00049 .arg (QChar (COORD_SYMBOL_MINUTES_PRIME))
00050 .arg (seconds)
00051 .arg (QChar (COORD_SYMBOL_SECONDS_DOUBLE_PRIME));
00052 }
00053
00054 QString FormatDegreesMinutesSecondsBase::formatOutputDegreesMinutesSecondsNsew (double value,
00055 bool isNsHemisphere) const
00056 {
00057 LOG4CPP_INFO_S ((*mainCat)) << "FormatDegreesMinutesSecondsBase::formatOutputDegreesMinutesSecondsNsew"
00058 << " value=" << value
00059 << " isNsHemisphere=" << (isNsHemisphere ? "true" : "false");
00060
00061
00062 bool negative = (value < 0);
00063 value = qAbs (value);
00064 int degrees = qFloor (value);
00065 value -= degrees;
00066 int minutes = value * DEGREES_TO_MINUTES;
00067 value -= minutes * MINUTES_TO_DEGREES;
00068 double seconds = value * DEGREES_TO_SECONDS;
00069
00070 QString hemisphere;
00071 if (isNsHemisphere) {
00072 hemisphere = (negative ? "S" : "N");
00073 } else {
00074 hemisphere = (negative ? "W" : "E");
00075 }
00076
00077 return QString ("%1%2 %3%4 %5%6 %7")
00078 .arg (degrees)
00079 .arg (QChar (COORD_SYMBOL_DEGREES))
00080 .arg (minutes)
00081 .arg (QChar (COORD_SYMBOL_MINUTES_PRIME))
00082 .arg (seconds)
00083 .arg (QChar (COORD_SYMBOL_SECONDS_DOUBLE_PRIME))
00084 .arg (hemisphere);
00085 }
00086
00087 QValidator::State FormatDegreesMinutesSecondsBase::parseInput (const QString &stringUntrimmed,
00088 double &value) const
00089 {
00090 LOG4CPP_INFO_S ((*mainCat)) << "FormatDegreesMinutesSecondsBase::parseInput"
00091 << " string=" << stringUntrimmed.toLatin1().data();
00092
00093 const QString string = stringUntrimmed.trimmed ();
00094
00095 if (string.isEmpty()) {
00096
00097 return QValidator::Intermediate;
00098 }
00099
00100
00101 QStringList fields = string.split (QRegExp ("\\s+"),
00102 QString::SkipEmptyParts);
00103
00104 QString field0, field1, field2;
00105 if (fields.count() == 0) {
00106 return QValidator::Invalid;
00107 } else {
00108 field0 = fields.at(0);
00109 if (fields.count() > 1) {
00110 field1 = fields.at(1);
00111 if (fields.count() > 2) {
00112 field2 = fields.at(2);
00113 if (fields.count() > 3) {
00114 return QValidator::Invalid;
00115 }
00116 }
00117 }
00118 }
00119
00120 stripSymbols (field0,
00121 field1,
00122 field2);
00123
00124 int pos;
00125
00126
00127 QDoubleValidator valDegrees;
00128 QDoubleValidator valMinutesOrSeconds;
00129 valMinutesOrSeconds.setBottom (0);
00130
00131 double valueDegrees = 0, valueMinutes = 0, valueSeconds = 0;
00132
00133
00134 QValidator::State state = valDegrees.validate (field0,
00135 pos);
00136 if (state == QValidator::Acceptable) {
00137
00138 valueDegrees = field0.toDouble();
00139
00140 if (fields.count() > 1) {
00141
00142
00143 state = valMinutesOrSeconds.validate (field1,
00144 pos);
00145 if (state == QValidator::Acceptable) {
00146
00147 valueMinutes = field1.toDouble();
00148
00149 if (fields.count() > 2) {
00150
00151
00152 state = valMinutesOrSeconds.validate (field2,
00153 pos);
00154 if (state == QValidator::Acceptable) {
00155
00156 valueSeconds = field2.toDouble();
00157
00158 }
00159 }
00160 }
00161 }
00162 }
00163
00164 if (state == QValidator::Acceptable) {
00165 if (valueDegrees < 0) {
00166
00167
00168 value = valueDegrees - valueMinutes * MINUTES_TO_DEGREES - valueSeconds * SECONDS_TO_DEGREES;
00169
00170 } else {
00171
00172
00173 value = valueDegrees + valueMinutes * MINUTES_TO_DEGREES + valueSeconds * SECONDS_TO_DEGREES;
00174
00175 }
00176 }
00177
00178 return state;
00179 }
00180
00181 void FormatDegreesMinutesSecondsBase::stripSymbols (QString &field0,
00182 QString &field1,
00183 QString &field2) const
00184 {
00185 const int FIELD_WIDTH = 0, BASE_8 = 8, BASE_16 = 16;
00186
00187
00188 QString strExpDegrees = QString (".*\\0%1$")
00189 .arg (COORD_SYMBOL_DEGREES, FIELD_WIDTH, BASE_8);
00190
00191 QRegExp regExpDegrees (strExpDegrees);
00192
00193 if (regExpDegrees.exactMatch (field0)) {
00194 field0 = field0.left (field0.count() - 1);
00195 }
00196
00197
00198 QString strExpMinutes = QString (".*[\\0%1\\x%2]$")
00199 .arg (COORD_SYMBOL_MINUTES_APOSTROPHE, FIELD_WIDTH, BASE_8)
00200 .arg (COORD_SYMBOL_MINUTES_PRIME, FIELD_WIDTH, BASE_16);
00201
00202 QRegExp regExpMinutes (strExpMinutes);
00203
00204 if (regExpMinutes.exactMatch (field1)) {
00205 field1 = field1.left (field1.count() - 1);
00206 }
00207
00208
00209 QString strExpSeconds1Char = QString (".*[\\x%1\\x%2]$")
00210 .arg (COORD_SYMBOL_SECONDS_DOUBLE_PRIME, FIELD_WIDTH, BASE_16)
00211 .arg (COORD_SYMBOL_SECONDS_QUOTATIONS, FIELD_WIDTH, BASE_16);
00212 QString strExpSeconds2Chars = QString (".*\\0%1\\0%2$")
00213 .arg (COORD_SYMBOL_MINUTES_PRIME, FIELD_WIDTH, BASE_8)
00214 .arg (COORD_SYMBOL_MINUTES_PRIME, FIELD_WIDTH, BASE_8);
00215
00216 QRegExp regExpSeconds1Char (strExpSeconds1Char), regExpSeconds2Chars (strExpSeconds2Chars);
00217
00218 if (regExpSeconds1Char.exactMatch (field2)) {
00219 field2 = field2.left (field2.count() - 1);
00220 }
00221 if (regExpSeconds2Chars.exactMatch (field2)) {
00222 field2 = field2.left (field2.count() - 2);
00223 }
00224 }