diff options
author | Berthold Stoeger <bstoeger@mail.tuwien.ac.at> | 2018-11-22 23:11:23 +0100 |
---|---|---|
committer | Dirk Hohndel <dirk@hohndel.org> | 2018-11-23 13:22:24 -0800 |
commit | 70897dd1b7d33f3d1f6b47acc587a7f33a176a04 (patch) | |
tree | d2304bd23c797d558b709dfa20d9a5afef2cd286 | |
parent | 68414531adeb68f7ad01c98bbac4d4d950409f02 (diff) | |
download | subsurface-70897dd1b7d33f3d1f6b47acc587a7f33a176a04.tar.gz |
Core: move is-single-day-trip and count-shown functions into core
These functionality was used by the desktop filter. To unify desktop
and mobile, move it into two new functions in divelist.c
Since one of them is the only caller of is_same_day() move that
likewise into divelist.c and make it of static linkage.
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
-rw-r--r-- | core/divelist.c | 36 | ||||
-rw-r--r-- | core/divelist.h | 2 | ||||
-rw-r--r-- | core/qthelper.cpp | 16 | ||||
-rw-r--r-- | core/qthelper.h | 1 | ||||
-rw-r--r-- | qt-models/divetripmodel.cpp | 10 |
5 files changed, 40 insertions, 25 deletions
diff --git a/core/divelist.c b/core/divelist.c index 1dc4e3d8a..ee7986466 100644 --- a/core/divelist.c +++ b/core/divelist.c @@ -47,6 +47,8 @@ * int find_next_visible_dive(timestamp_t when); * void clear_dive_file_data() * void clear_table(struct dive_table *table) + * bool trip_is_single_day(const struct dive_trip *trip) + * int trip_shown_dives(const struct dive_trip *trip) */ #include <unistd.h> #include <stdio.h> @@ -1875,3 +1877,37 @@ struct dive *find_next_visible_dive(timestamp_t when) return NULL; } + +static bool is_same_day(timestamp_t trip_when, timestamp_t dive_when) +{ + static timestamp_t twhen = (timestamp_t) 0; + static struct tm tmt; + struct tm tmd; + + utc_mkdate(dive_when, &tmd); + + if (twhen != trip_when) { + twhen = trip_when; + utc_mkdate(twhen, &tmt); + } + + return (tmd.tm_mday == tmt.tm_mday) && (tmd.tm_mon == tmt.tm_mon) && (tmd.tm_year == tmt.tm_year); +} + +bool trip_is_single_day(const struct dive_trip *trip) +{ + if (trip->dives.nr <= 1) + return true; + return is_same_day(trip->dives.dives[0]->when, + trip->dives.dives[trip->dives.nr - 1]->when); +} + +int trip_shown_dives(const struct dive_trip *trip) +{ + int res = 0; + for (int i = 0; i < trip->dives.nr; ++i) { + if (!trip->dives.dives[i]->hidden_by_filter) + res++; + } + return res; +} diff --git a/core/divelist.h b/core/divelist.h index 26218af05..11fa75f50 100644 --- a/core/divelist.h +++ b/core/divelist.h @@ -54,6 +54,8 @@ extern void set_dive_nr_for_current_dive(); extern timestamp_t get_surface_interval(timestamp_t when); extern void delete_dive_from_table(struct dive_table *table, int idx); extern struct dive *find_next_visible_dive(timestamp_t when); +extern bool trip_is_single_day(const struct dive_trip *trip); +extern int trip_shown_dives(const struct dive_trip *trip); int get_min_datafile_version(); void reset_min_datafile_version(); diff --git a/core/qthelper.cpp b/core/qthelper.cpp index 27d94e4f5..8fbaa31af 100644 --- a/core/qthelper.cpp +++ b/core/qthelper.cpp @@ -950,22 +950,6 @@ extern "C" char *get_current_date() return copy_qstring(current_date); } -bool is_same_day(timestamp_t trip_when, timestamp_t dive_when) -{ - static timestamp_t twhen = (timestamp_t) 0; - static struct tm tmt; - struct tm tmd; - - utc_mkdate(dive_when, &tmd); - - if (twhen != trip_when) { - twhen = trip_when; - utc_mkdate(twhen, &tmt); - } - - return (tmd.tm_mday == tmt.tm_mday) && (tmd.tm_mon == tmt.tm_mon) && (tmd.tm_year == tmt.tm_year); -} - QString get_trip_date_string(timestamp_t when, int nr, bool getday) { struct tm tm; diff --git a/core/qthelper.h b/core/qthelper.h index deb893634..3e90a1979 100644 --- a/core/qthelper.h +++ b/core/qthelper.h @@ -77,7 +77,6 @@ QString get_dive_duration_string(timestamp_t when, QString hoursText, QString mi QString get_dive_surfint_string(timestamp_t when, QString daysText, QString hoursText, QString minutesText, QString separator = " ", int maxdays = 4); QString get_dive_date_string(timestamp_t when); QString get_short_dive_date_string(timestamp_t when); -bool is_same_day (timestamp_t trip_when, timestamp_t dive_when); QString get_trip_date_string(timestamp_t when, int nr, bool getday); QString uiLanguage(QLocale *callerLoc); QLocale getLocale(); diff --git a/qt-models/divetripmodel.cpp b/qt-models/divetripmodel.cpp index 51284c41d..ae7641ba2 100644 --- a/qt-models/divetripmodel.cpp +++ b/qt-models/divetripmodel.cpp @@ -48,7 +48,6 @@ static QVariant dive_table_alignment(int column) QVariant DiveTripModel::tripData(const dive_trip *trip, int column, int role) { - bool oneDayTrip=true; if (role == TRIP_ROLE) return QVariant::fromValue(const_cast<dive_trip *>(trip)); // Not nice: casting away a const @@ -57,13 +56,8 @@ QVariant DiveTripModel::tripData(const dive_trip *trip, int column, int role) switch (column) { case DiveTripModel::NR: QString shownText; - int countShown = 0; - for (int i = 0; i < trip->dives.nr; ++i) { - struct dive *d = trip->dives.dives[i]; - if (!d->hidden_by_filter) - countShown++; - oneDayTrip &= is_same_day(trip_date(trip), d->when); - } + bool oneDayTrip = trip_is_single_day(trip); + int countShown = trip_shown_dives(trip); if (countShown < trip->dives.nr) shownText = tr("(%1 shown)").arg(countShown); if (!empty_string(trip->location)) |