diff options
author | Dirk Hohndel <dirk@hohndel.org> | 2013-10-05 09:48:26 -0700 |
---|---|---|
committer | Dirk Hohndel <dirk@hohndel.org> | 2013-10-06 10:42:32 -0700 |
commit | 2c4ccc04bd384090917d7d17221012611426edb3 (patch) | |
tree | c32eec4864b1f613443f041e65b7d530a92c176f /qthelper.cpp | |
parent | 974ba1382dec543af0bab14c907e4f2012c27c05 (diff) | |
download | subsurface-2c4ccc04bd384090917d7d17221012611426edb3.tar.gz |
Parse GPS text in C++ and re-implement GPS changed check
This is SO MUCH easier then the convoluted and fragile C / glib code it
replaces.
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
Diffstat (limited to 'qthelper.cpp')
-rw-r--r-- | qthelper.cpp | 59 |
1 files changed, 56 insertions, 3 deletions
diff --git a/qthelper.cpp b/qthelper.cpp index 36d1d02c7..0af4dacea 100644 --- a/qthelper.cpp +++ b/qthelper.cpp @@ -1,4 +1,6 @@ #include "qthelper.h" +#include <QRegExp> +#include <QDebug> DiveComputerList::DiveComputerList() { @@ -83,14 +85,65 @@ QString weight_string(int weight_in_grams) if (get_units()->weight == units::KG) { int gr = weight_in_grams % 1000; int kg = weight_in_grams / 1000; - if (kg >= 20.0) { + if (kg >= 20.0) { str = QString("0"); - } else { + } else { str = QString("%1.%2").arg(kg).arg((unsigned)(gr) / 100); - } + } } else { double lbs = grams_to_lbs(weight_in_grams); str = QString("%1").arg(lbs, 0, 'f', lbs >= 40.0 ? 0 : 1 ); } return (str); } + +bool parseGpsText(const QString& gps_text, double *latitude, double *longitude) +{ + /* an empty string is interpreted as 0.0,0.0 and therefore "no gps location" */ + if (gps_text.trimmed() == "") { + *latitude = 0.0; + *longitude = 0.0; + return true; + } + QRegExp r("\\s*([SN])\\s*(\\d+)[" UTF8_DEGREE "\\s]+(\\d+)\\.(\\d+)[^EW]*([EW])\\s*(\\d+)[" UTF8_DEGREE "\\s]+(\\d+)\\.(\\d+)"); + if (r.indexIn(gps_text) != 8) { + // qDebug() << "Hemisphere" << r.cap(1) << "deg" << r.cap(2) << "min" << r.cap(3) << "decimal" << r.cap(4); + // qDebug() << "Hemisphere" << r.cap(5) << "deg" << r.cap(6) << "min" << r.cap(7) << "decimal" << r.cap(8); + *latitude = r.cap(2).toInt() + (r.cap(3) + QString(".") + r.cap(4)).toDouble() / 60.0; + *longitude = r.cap(6).toInt() + (r.cap(7) + QString(".") + r.cap(8)).toDouble() / 60.0; + if (r.cap(1) == "S") + *latitude *= -1.0; + if (r.cap(5) == "W") + *longitude *= -1.0; + // qDebug("%s -> %8.5f / %8.5f", gps_text.toLocal8Bit().data(), *latitude, *longitude); + return true; + } + return false; +} + +bool gpsHasChanged(struct dive *dive, struct dive *master, const QString& gps_text) +{ + double latitude, longitude; + int latudeg, longudeg; + + /* if we have a master and the dive's gps address is different from it, + * don't change the dive */ + if (master && (master->latitude.udeg != dive->latitude.udeg || + master->longitude.udeg != dive->longitude.udeg)) + return false; + + if (!parseGpsText(gps_text, &latitude, &longitude)) + return false; + + latudeg = rint(1000000 * latitude); + longudeg = rint(1000000 * longitude); + + /* if dive gps didn't change, nothing changed */ + if (dive->latitude.udeg == latudeg && dive->longitude.udeg == longudeg) + return false; + /* ok, update the dive and mark things changed */ + dive->latitude.udeg = latudeg; + dive->longitude.udeg = longudeg; + return true; + +} |