Dive site: show distance to current dive using extra data

Currently, in the dive-site selection widget the distance to
the dive site of the current dive is shown. Instead, use the
recently introduced dive_get_gps_location() function. Thus,
the actual GPS coordinates extracted by libdivecomputer are
used.

The function is only called when the current dive changes
and the location is stored in the item delegate.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2019-04-25 09:35:46 +02:00 committed by Dirk Hohndel
parent c7e1c40b0e
commit e76298a8a7
4 changed files with 20 additions and 8 deletions

View file

@ -441,10 +441,15 @@ QWidget *DoubleSpinBoxDelegate::createEditor(QWidget *parent, const QStyleOption
return w;
}
LocationFilterDelegate::LocationFilterDelegate(QObject*)
LocationFilterDelegate::LocationFilterDelegate(QObject *) : currentLocation({0, 0})
{
}
void LocationFilterDelegate::setCurrentLocation(location_t loc)
{
currentLocation = loc;
}
void LocationFilterDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &origIdx) const
{
QFont fontBigger = qApp->font();
@ -461,8 +466,7 @@ void LocationFilterDelegate::paint(QPainter *painter, const QStyleOptionViewItem
QIcon icon = index.data(Qt::DecorationRole).value<QIcon>();
struct dive_site *ds =
index.model()->data(index.model()->index(index.row(), LocationInformationModel::DIVESITE)).value<dive_site *>();
struct dive_site *currentDiveSite = current_dive ? get_dive_site_for_dive(current_dive) : nullptr;
bool currentDiveSiteHasGPS = currentDiveSite && dive_site_has_gps_location(currentDiveSite);
bool currentDiveHasGPS = has_location(&currentLocation);
//Special case: do not show name, but instead, show
if (index.row() < 2) {
diveSiteName = index.data().toString();
@ -487,13 +491,13 @@ void LocationFilterDelegate::paint(QPainter *painter, const QStyleOptionViewItem
if (bottomText.isEmpty())
bottomText = printGPSCoords(&ds->location);
if (dive_site_has_gps_location(ds) && currentDiveSiteHasGPS) {
if (dive_site_has_gps_location(ds) && currentDiveHasGPS) {
// so we are showing a completion and both the current dive site and the completion
// have a GPS fix... so let's show the distance
if (same_location(&ds->location, &currentDiveSite->location)) {
if (same_location(&ds->location, &currentLocation)) {
bottomText += tr(" (same GPS fix)");
} else {
int distanceMeters = get_distance(&ds->location, &currentDiveSite->location);
int distanceMeters = get_distance(&ds->location, &currentLocation);
QString distance = distance_string(distanceMeters);
int nr = nr_of_dives_at_dive_site(ds);
bottomText += tr(" (~%1 away").arg(distance);
@ -501,7 +505,7 @@ void LocationFilterDelegate::paint(QPainter *painter, const QStyleOptionViewItem
}
}
if (bottomText.isEmpty()) {
if (currentDiveSiteHasGPS)
if (currentDiveHasGPS)
bottomText = tr("(no existing GPS data, add GPS fix from this dive)");
else
bottomText = tr("(no GPS data)");