summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar Dirk Hohndel <dirk@hohndel.org>2013-10-05 09:48:26 -0700
committerGravatar Dirk Hohndel <dirk@hohndel.org>2013-10-06 10:42:32 -0700
commit2c4ccc04bd384090917d7d17221012611426edb3 (patch)
treec32eec4864b1f613443f041e65b7d530a92c176f
parent974ba1382dec543af0bab14c907e4f2012c27c05 (diff)
downloadsubsurface-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>
-rw-r--r--qt-ui/maintab.cpp8
-rw-r--r--qthelper.cpp59
-rw-r--r--qthelper.h1
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