summaryrefslogtreecommitdiffstats
path: root/desktop-widgets/locationinformation.cpp
diff options
context:
space:
mode:
authorGravatar Berthold Stoeger <bstoeger@mail.tuwien.ac.at>2019-04-25 00:26:48 +0200
committerGravatar Dirk Hohndel <dirk@hohndel.org>2019-05-11 12:35:11 -0700
commitc7e1c40b0e9ff1c9152f06b7f1c134f232297fe7 (patch)
tree78d2116b217d64518176d23e2b435afa9983eed2 /desktop-widgets/locationinformation.cpp
parentbab7033ccba8840eb661feed0750e60404d06fc0 (diff)
downloadsubsurface-c7e1c40b0e9ff1c9152f06b7f1c134f232297fe7.tar.gz
Dive site: sort by distance to current dive
When presenting the list of dive sites on the dive-info tab, sort the dive sites by distance to the current dive. The idea is that when the user wants to select a dive site, close dive sites should be prioritized. The location of the dive is determined with the dive_get_gps_location() function introduced in the previous commit. This actual GPS data get precedence over the currently set dive site for that dive. On change of dive, the current location is updated in the DiveLocationFilterProxyModel so that a potentially expensive search for GPS data is not repeated for every comparison. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
Diffstat (limited to 'desktop-widgets/locationinformation.cpp')
-rw-r--r--desktop-widgets/locationinformation.cpp22
1 files changed, 20 insertions, 2 deletions
diff --git a/desktop-widgets/locationinformation.cpp b/desktop-widgets/locationinformation.cpp
index 825f440ba..a57c6fb30 100644
--- a/desktop-widgets/locationinformation.cpp
+++ b/desktop-widgets/locationinformation.cpp
@@ -252,7 +252,7 @@ void LocationInformationWidget::reverseGeocode()
Command::editDiveSiteTaxonomy(diveSite, taxonomy);
}
-DiveLocationFilterProxyModel::DiveLocationFilterProxyModel(QObject *)
+DiveLocationFilterProxyModel::DiveLocationFilterProxyModel(QObject *) : currentLocation({0, 0})
{
}
@@ -263,6 +263,12 @@ void DiveLocationFilterProxyModel::setFilter(const QString &filterIn)
sort(LocationInformationModel::NAME);
}
+void DiveLocationFilterProxyModel::setCurrentLocation(location_t loc)
+{
+ currentLocation = loc;
+ sort(LocationInformationModel::NAME);
+}
+
bool DiveLocationFilterProxyModel::filterAcceptsRow(int source_row, const QModelIndex&) const
{
// We don't want to show the first two entries (add dive site with that name)
@@ -282,6 +288,14 @@ bool DiveLocationFilterProxyModel::lessThan(const QModelIndex &source_left, cons
// The first two entries are special - we never want to change their order
if (source_left.row() <= 1 || source_right.row() <= 1)
return source_left.row() < source_right.row();
+
+ // If there is a current location, sort by that - otherwise use the provided column
+ if (has_location(&currentLocation)) {
+ // The dive sites are -2 because of the first two items.
+ struct dive_site *ds1 = get_dive_site(source_left.row() - 2, &dive_site_table);
+ struct dive_site *ds2 = get_dive_site(source_right.row() - 2, &dive_site_table);
+ return get_distance(&ds1->location, &currentLocation) < get_distance(&ds2->location, &currentLocation);
+ }
return source_left.data().toString().compare(source_right.data().toString(), Qt::CaseInsensitive) < 0;
}
@@ -543,8 +557,9 @@ void DiveLocationLineEdit::fixPopupPosition()
}
}
-void DiveLocationLineEdit::setCurrentDiveSite(struct dive_site *ds)
+void DiveLocationLineEdit::setCurrentDiveSite(struct dive *d)
{
+ struct dive_site *ds = get_dive_site_for_dive(d);
currDs = ds;
if (!currDs) {
currType = NO_DIVE_SITE;
@@ -552,6 +567,9 @@ void DiveLocationLineEdit::setCurrentDiveSite(struct dive_site *ds)
} else {
setText(ds->name);
}
+
+ location_t currentLocation = d ? dive_get_gps_location(d) : location_t{0, 0};
+ proxy->setCurrentLocation(currentLocation);
}
void DiveLocationLineEdit::showPopup()