From d1b8f1ca3d66983001b0e32e9d1b294e1f0bf055 Mon Sep 17 00:00:00 2001 From: Berthold Stoeger Date: Thu, 16 Dec 2021 23:09:39 +0100 Subject: [PATCH] formatting: move get_trip_title to string-format.h and split it To enable grouping by trip in the statistics module, split the get_trip_title() function in a version that appends a "(n dive(s)" string an one that doesn't. The statistics module doesn't want that added string, since it displays the number of dives in a different way. Also, move the functions to string-format.h, where these are collected. And rename them to camelCase. Yes, it's ugly, but consistent with most other C++ code in the code base. Signed-off-by: Berthold Stoeger --- core/qthelper.cpp | 21 +------------------ core/qthelper.h | 1 - core/string-format.cpp | 27 +++++++++++++++++++++++++ core/string-format.h | 3 +++ desktop-widgets/tripselectiondialog.cpp | 4 ++-- qt-models/divetripmodel.cpp | 4 ++-- 6 files changed, 35 insertions(+), 25 deletions(-) diff --git a/core/qthelper.cpp b/core/qthelper.cpp index 3e699d889..8a560f3d0 100644 --- a/core/qthelper.cpp +++ b/core/qthelper.cpp @@ -1,6 +1,7 @@ // SPDX-License-Identifier: GPL-2.0 #include "qthelper.h" #include "dive.h" +#include "divelist.h" #include "core/settings/qPrefLanguage.h" #include "core/settings/qPrefUpdateManager.h" #include "core/subsurface-qt/divelistnotifier.h" @@ -18,7 +19,6 @@ #include "picture.h" #include "selection.h" #include "tag.h" -#include "trip.h" #include "imagedownloader.h" #include "xmlparams.h" #include "core/git-access.h" // for CLOUD_HOST definitions @@ -1040,25 +1040,6 @@ extern "C" char *get_current_date() return copy_qstring(current_date); } -QString get_trip_string(const dive_trip *trip) -{ - if (!trip) - return QString(); - - int nr = trip->dives.nr; - timestamp_t when = trip_date(trip); - bool getday = trip_is_single_day(trip); - - QDateTime localTime = timestampToDateTime(when); - - QString prefix = !empty_string(trip->location) ? QString(trip->location) + ", " : QString(); - QString suffix = " " + gettextFromC::tr("(%n dive(s))", "", nr); - if (getday) - return prefix + loc.toString(localTime, prefs.date_format) + suffix; - else - return prefix + loc.toString(localTime, "MMM yyyy") + suffix; -} - static QMutex hashOfMutex; static QHash localFilenameOf; diff --git a/core/qthelper.h b/core/qthelper.h index 7ae14ed13..0da1fe37f 100644 --- a/core/qthelper.h +++ b/core/qthelper.h @@ -88,7 +88,6 @@ QString get_dive_date_string(timestamp_t when); QString get_first_dive_date_string(); QString get_last_dive_date_string(); QString get_short_dive_date_string(timestamp_t when); -QString get_trip_string(const dive_trip *trip); QString getUiLanguage(); void initUiLanguage(); QLocale getLocale(); diff --git a/core/string-format.cpp b/core/string-format.cpp index 0ae4e6366..0526d3653 100644 --- a/core/string-format.cpp +++ b/core/string-format.cpp @@ -3,10 +3,13 @@ #include "divesite.h" #include "qthelper.h" #include "subsurface-string.h" +#include "trip.h" #include +#include #include enum returnPressureSelector { START_PRESSURE, END_PRESSURE }; +static QLocale loc; static QString getPressures(const struct dive *dive, int i, enum returnPressureSelector ret) { @@ -273,3 +276,27 @@ QString formatDayOfWeek(int day) case 6: return gettextFromC::tr("Saturday"); } } + +QString formatTripTitle(const dive_trip *trip) +{ + if (!trip) + return QString(); + + timestamp_t when = trip_date(trip); + bool getday = trip_is_single_day(trip); + + QDateTime localTime = timestampToDateTime(when); + + QString prefix = !empty_string(trip->location) ? QString(trip->location) + ", " : QString(); + if (getday) + return prefix + loc.toString(localTime, prefs.date_format); + else + return prefix + loc.toString(localTime, "MMM yyyy"); +} + +QString formatTripTitleWithDives(const dive_trip *trip) +{ + int nr = trip->dives.nr; + return formatTripTitle(trip) + " " + + gettextFromC::tr("(%n dive(s))", "", nr); +} diff --git a/core/string-format.h b/core/string-format.h index dfe705344..67221b540 100644 --- a/core/string-format.h +++ b/core/string-format.h @@ -6,6 +6,7 @@ #include struct dive; +struct dive_trip; QString formatSac(const dive *d); QString formatNotes(const dive *d); @@ -26,5 +27,7 @@ QString formatDiveDate(const dive *d); QString formatDiveTime(const dive *d); QString formatDiveDateTime(const dive *d); QString formatDayOfWeek(int day); +QString formatTripTitle(const dive_trip *trip); +QString formatTripTitleWithDives(const dive_trip *trip); #endif diff --git a/desktop-widgets/tripselectiondialog.cpp b/desktop-widgets/tripselectiondialog.cpp index 2623fdc10..5b336b1b8 100644 --- a/desktop-widgets/tripselectiondialog.cpp +++ b/desktop-widgets/tripselectiondialog.cpp @@ -1,7 +1,7 @@ // SPDX-License-Identifier: GPL-2.0 #include "tripselectiondialog.h" +#include "core/string-format.h" #include "core/trip.h" -#include "core/qthelper.h" #include #include @@ -19,7 +19,7 @@ TripSelectionDialog::TripSelectionDialog(QWidget *parent) : QDialog(parent) QStringList list; list.reserve(trip_table.nr); for (int i = 0; i < trip_table.nr; ++i) - list.push_back(get_trip_string(trip_table.trips[i])); + list.push_back(formatTripTitleWithDives(trip_table.trips[i])); ui.trips->addItems(list); } diff --git a/qt-models/divetripmodel.cpp b/qt-models/divetripmodel.cpp index 939569522..af0e232d4 100644 --- a/qt-models/divetripmodel.cpp +++ b/qt-models/divetripmodel.cpp @@ -3,11 +3,11 @@ #include "core/divefilter.h" #ifdef SUBSURFACE_MOBILE #include "qt-models/mobilelistmodel.h" -#include "core/string-format.h" #endif #include "core/gettextfromc.h" #include "core/metrics.h" #include "core/selection.h" +#include "core/string-format.h" #include "core/trip.h" #include "core/qthelper.h" #include "core/divesite.h" @@ -123,7 +123,7 @@ QVariant DiveTripModelBase::tripData(const dive_trip *trip, int column, int role int countShown = trip_shown_dives(trip); if (countShown < trip->dives.nr) shownText = tr("(%1 shown)").arg(countShown); - return get_trip_string(trip) + " " + shownText; + return formatTripTitleWithDives(trip) + " " + shownText; } }