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>
This commit is contained in:
Dirk Hohndel 2013-10-05 09:48:26 -07:00
parent 974ba1382d
commit 2c4ccc04bd
3 changed files with 59 additions and 9 deletions

View file

@ -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 {

View file

@ -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;
}

View file

@ -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