diff options
-rw-r--r-- | qt-ui/maintab.cpp | 8 | ||||
-rw-r--r-- | qthelper.cpp | 59 | ||||
-rw-r--r-- | qthelper.h | 1 |
3 files changed, 59 insertions, 9 deletions
diff --git a/qt-ui/maintab.cpp b/qt-ui/maintab.cpp index b19638b69..fd40bed86 100644 --- a/qt-ui/maintab.cpp +++ b/qt-ui/maintab.cpp @@ -14,6 +14,7 @@ #include "globe.h" #include "completionmodels.h" #include "diveplanner.h" +#include "qthelper.h" #include <QLabel> #include <QCompleter> @@ -695,13 +696,8 @@ void MainTab::on_notes_textChanged() void MainTab::on_coordinates_textChanged(const QString& text) { - QByteArray textByteArray = text.toLocal8Bit(); bool gpsChanged = FALSE; - // EDIT_SELECTED_DIVES(gpsChanged |= gps_changed(mydive, NULL, textByteArray.data())); - // FIXME - // FIXME - // FIXME - // FIXME + EDIT_SELECTED_DIVES(gpsChanged |= gpsHasChanged(mydive, NULL, text)); if (gpsChanged) { markChangedWidget(ui.coordinates); } else { 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; + +} diff --git a/qthelper.h b/qthelper.h index 3ac83b5c4..0bb7c19f1 100644 --- a/qthelper.h +++ b/qthelper.h @@ -34,5 +34,6 @@ public: }; QString weight_string(int weight_in_grams); +bool gpsHasChanged(struct dive* dive, struct dive *master, const QString &gps_text); #endif // QTHELPER_H |