summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar Berthold Stoeger <bstoeger@mail.tuwien.ac.at>2020-12-14 22:42:07 +0100
committerGravatar Dirk Hohndel <dirk@hohndel.org>2020-12-17 13:03:56 -0800
commitbf8261c0014253634eaddf7c0a9cf79d3a8b4123 (patch)
treee74ca215dbf25cf311e958875863da8037e0eee3
parentdc37ba7758a34fb9d5dd840d41902621030fec32 (diff)
downloadsubsurface-bf8261c0014253634eaddf7c0a9cf79d3a8b4123.tar.gz
core: create string-format.cpp source file
The mobile version of the list used string formatting functions defined in DiveObjectHelper and declared in mobilelistmodels.h. Very confusing. Move them to a separate source file where - in the long run - all the string-formatting functions, which are scattered all over the place, can be collected. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at> Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
-rw-r--r--Subsurface-mobile.pro2
-rw-r--r--core/CMakeLists.txt2
-rw-r--r--core/string-format.cpp145
-rw-r--r--core/string-format.h19
-rw-r--r--core/subsurface-qt/diveobjecthelper.cpp147
-rw-r--r--qt-models/divetripmodel.cpp9
-rw-r--r--qt-models/mobilelistmodel.h10
7 files changed, 178 insertions, 156 deletions
diff --git a/Subsurface-mobile.pro b/Subsurface-mobile.pro
index 2da1e5703..5641f422d 100644
--- a/Subsurface-mobile.pro
+++ b/Subsurface-mobile.pro
@@ -89,6 +89,7 @@ SOURCES += subsurface-mobile-main.cpp \
core/membuffer.c \
core/selection.cpp \
core/sha1.c \
+ core/string-format.cpp \
core/strtod.c \
core/tag.c \
core/taxonomy.c \
@@ -222,6 +223,7 @@ HEADERS += \
core/selection.h \
core/sha1.h \
core/strndup.h \
+ core/string-format.h \
core/subsurfacestartup.h \
core/subsurfacesysinfo.h \
core/taxonomy.h \
diff --git a/core/CMakeLists.txt b/core/CMakeLists.txt
index 1ed24019c..5680c5f6b 100644
--- a/core/CMakeLists.txt
+++ b/core/CMakeLists.txt
@@ -164,6 +164,8 @@ set(SUBSURFACE_CORE_LIB_SRCS
statistics.c
statistics.h
strndup.h
+ string-format.h
+ string-format.cpp
strtod.c
subsurface-string.h
subsurfacestartup.c
diff --git a/core/string-format.cpp b/core/string-format.cpp
new file mode 100644
index 000000000..331fb64ba
--- /dev/null
+++ b/core/string-format.cpp
@@ -0,0 +1,145 @@
+#include "string-format.h"
+#include "dive.h"
+#include "divesite.h"
+#include "qthelper.h"
+#include "subsurface-string.h"
+#include <QTextDocument>
+
+enum returnPressureSelector { START_PRESSURE, END_PRESSURE };
+
+static QString getPressures(const struct dive *dive, int i, enum returnPressureSelector ret)
+{
+ const cylinder_t *cyl = get_cylinder(dive, i);
+ QString fmt;
+ if (ret == START_PRESSURE) {
+ if (cyl->start.mbar)
+ fmt = get_pressure_string(cyl->start, true);
+ else if (cyl->sample_start.mbar)
+ fmt = get_pressure_string(cyl->sample_start, true);
+ }
+ if (ret == END_PRESSURE) {
+ if (cyl->end.mbar)
+ fmt = get_pressure_string(cyl->end, true);
+ else if(cyl->sample_end.mbar)
+ fmt = get_pressure_string(cyl->sample_end, true);
+ }
+ return fmt;
+}
+
+QString formatSac(const dive *d)
+{
+ if (!d->sac)
+ return QString();
+ const char *unit;
+ int decimal;
+ double value = get_volume_units(d->sac, &decimal, &unit);
+ return QString::number(value, 'f', decimal).append(unit);
+}
+
+QString formatNotes(const dive *d)
+{
+ QString tmp = d->notes ? QString::fromUtf8(d->notes) : QString();
+ if (is_dc_planner(&d->dc)) {
+ QTextDocument notes;
+ #define _NOTES_BR "&#92n"
+ tmp.replace("<thead>", "<thead>" _NOTES_BR)
+ .replace("<br>", "<br>" _NOTES_BR)
+ .replace("<br/>", "<br/>" _NOTES_BR)
+ .replace("<br />", "<br />" _NOTES_BR)
+ .replace("<tr>", "<tr>" _NOTES_BR)
+ .replace("</tr>", "</tr>" _NOTES_BR);
+ notes.setHtml(tmp);
+ tmp = notes.toPlainText();
+ tmp.replace(_NOTES_BR, "<br/>");
+ #undef _NOTES_BR
+ } else {
+ tmp.replace("\n", "<br/>");
+ }
+ return tmp;
+}
+
+QString format_gps_decimal(const dive *d)
+{
+ bool savep = prefs.coordinates_traditional;
+
+ prefs.coordinates_traditional = false;
+ QString val = d->dive_site ? printGPSCoords(&d->dive_site->location) : QString();
+ prefs.coordinates_traditional = savep;
+ return val;
+}
+
+QStringList formatGetCylinder(const dive *d)
+{
+ QStringList getCylinder;
+ for (int i = 0; i < d->cylinders.nr; i++) {
+ if (is_cylinder_used(d, i))
+ getCylinder << get_cylinder(d, i)->type.description;
+ }
+ return getCylinder;
+}
+
+QStringList formatStartPressure(const dive *d)
+{
+ QStringList startPressure;
+ for (int i = 0; i < d->cylinders.nr; i++) {
+ if (is_cylinder_used(d, i))
+ startPressure << getPressures(d, i, START_PRESSURE);
+ }
+ return startPressure;
+}
+
+QStringList formatEndPressure(const dive *d)
+{
+ QStringList endPressure;
+ for (int i = 0; i < d->cylinders.nr; i++) {
+ if (is_cylinder_used(d, i))
+ endPressure << getPressures(d, i, END_PRESSURE);
+ }
+ return endPressure;
+}
+
+QStringList formatFirstGas(const dive *d)
+{
+ QStringList gas;
+ for (int i = 0; i < d->cylinders.nr; i++) {
+ if (is_cylinder_used(d, i))
+ gas << get_gas_string(get_cylinder(d, i)->gasmix);
+ }
+ return gas;
+}
+
+// Add string to sorted QStringList, if it doesn't already exist and
+// it isn't the empty string.
+static void addStringToSortedList(QStringList &l, const char *s)
+{
+ if (empty_string(s))
+ return;
+
+ // Do a binary search for the string. lower_bound() returns an iterator
+ // to either the searched-for element or the next higher element if it
+ // doesn't exist.
+ QString qs(s);
+ auto it = std::lower_bound(l.begin(), l.end(), qs); // TODO: use locale-aware sorting
+ if (it != l.end() && *it == s)
+ return;
+
+ // Add new string at sorted position
+ l.insert(it, s);
+}
+
+QStringList formatFullCylinderList()
+{
+ QStringList cylinders;
+ struct dive *d;
+ int i = 0;
+ for_each_dive (i, d) {
+ for (int j = 0; j < d->cylinders.nr; j++)
+ addStringToSortedList(cylinders, get_cylinder(d, j)->type.description);
+ }
+
+ for (int ti = 0; ti < tank_info_table.nr; ti++)
+ addStringToSortedList(cylinders, tank_info_table.infos[ti].name);
+
+ return cylinders;
+}
+
diff --git a/core/string-format.h b/core/string-format.h
new file mode 100644
index 000000000..96a9638e4
--- /dev/null
+++ b/core/string-format.h
@@ -0,0 +1,19 @@
+// SPDX-License-Identifier: GPL-2.0
+// Various functions that format data into QStrings or QStringLists
+#ifndef STRING_FORMAT_H
+#define STRING_FORMAT_H
+
+#include <QStringList>
+
+struct dive;
+
+QString formatSac(const dive *d);
+QString formatNotes(const dive *d);
+QString format_gps_decimal(const dive *d);
+QStringList formatGetCylinder(const dive *d);
+QStringList formatStartPressure(const dive *d);
+QStringList formatEndPressure(const dive *d);
+QStringList formatFirstGas(const dive *d);
+QStringList formatFullCylinderList();
+
+#endif
diff --git a/core/subsurface-qt/diveobjecthelper.cpp b/core/subsurface-qt/diveobjecthelper.cpp
index 1fa91a301..8f081fa04 100644
--- a/core/subsurface-qt/diveobjecthelper.cpp
+++ b/core/subsurface-qt/diveobjecthelper.cpp
@@ -9,6 +9,7 @@
#include "core/divesite.h"
#include "core/trip.h"
#include "core/subsurface-string.h"
+#include "core/string-format.h"
#include "qt-models/tankinfomodel.h"
#if defined(DEBUG_DOH)
@@ -19,8 +20,6 @@ static int callCounter = 0;
#endif /* defined(DEBUG_DOH) */
-enum returnPressureSelector {START_PRESSURE, END_PRESSURE};
-
static QString getFormattedWeight(const struct dive *dive, int idx)
{
const weightsystem_t *weight = &dive->weightsystems.weightsystems[idx];
@@ -43,57 +42,6 @@ static QString getFormattedCylinder(const struct dive *dive, int idx)
return fmt;
}
-static QString getPressures(const struct dive *dive, int i, enum returnPressureSelector ret)
-{
- const cylinder_t *cyl = get_cylinder(dive, i);
- QString fmt;
- if (ret == START_PRESSURE) {
- if (cyl->start.mbar)
- fmt = get_pressure_string(cyl->start, true);
- else if (cyl->sample_start.mbar)
- fmt = get_pressure_string(cyl->sample_start, true);
- }
- if (ret == END_PRESSURE) {
- if (cyl->end.mbar)
- fmt = get_pressure_string(cyl->end, true);
- else if(cyl->sample_end.mbar)
- fmt = get_pressure_string(cyl->sample_end, true);
- }
- return fmt;
-}
-
-QString format_gps_decimal(const dive *d)
-{
- bool savep = prefs.coordinates_traditional;
-
- prefs.coordinates_traditional = false;
- QString val = d->dive_site ? printGPSCoords(&d->dive_site->location) : QString();
- prefs.coordinates_traditional = savep;
- return val;
-}
-
-QString formatNotes(const dive *d)
-{
- QString tmp = d->notes ? QString::fromUtf8(d->notes) : QString();
- if (is_dc_planner(&d->dc)) {
- QTextDocument notes;
- #define _NOTES_BR "&#92n"
- tmp.replace("<thead>", "<thead>" _NOTES_BR)
- .replace("<br>", "<br>" _NOTES_BR)
- .replace("<br/>", "<br/>" _NOTES_BR)
- .replace("<br />", "<br />" _NOTES_BR)
- .replace("<tr>", "<tr>" _NOTES_BR)
- .replace("</tr>", "</tr>" _NOTES_BR);
- notes.setHtml(tmp);
- tmp = notes.toPlainText();
- tmp.replace(_NOTES_BR, "<br/>");
- #undef _NOTES_BR
- } else {
- tmp.replace("\n", "<br/>");
- }
- return tmp;
-}
-
static QString formatGas(const dive *d)
{
/*WARNING: here should be the gastlist, returned
@@ -117,16 +65,6 @@ static QString formatGas(const dive *d)
return gases;
}
-QString formatSac(const dive *d)
-{
- if (!d->sac)
- return QString();
- const char *unit;
- int decimal;
- double value = get_volume_units(d->sac, &decimal, &unit);
- return QString::number(value, 'f', decimal).append(unit);
-}
-
static QString formatWeightList(const dive *d)
{
QString weights;
@@ -172,81 +110,6 @@ static QVector<CylinderObjectHelper> makeCylinderObjects(const dive *d)
return res;
}
-QStringList formatGetCylinder(const dive *d)
-{
- QStringList getCylinder;
- for (int i = 0; i < d->cylinders.nr; i++) {
- if (is_cylinder_used(d, i))
- getCylinder << get_cylinder(d, i)->type.description;
- }
- return getCylinder;
-}
-
-QStringList getStartPressure(const dive *d)
-{
- QStringList startPressure;
- for (int i = 0; i < d->cylinders.nr; i++) {
- if (is_cylinder_used(d, i))
- startPressure << getPressures(d, i, START_PRESSURE);
- }
- return startPressure;
-}
-
-QStringList getEndPressure(const dive *d)
-{
- QStringList endPressure;
- for (int i = 0; i < d->cylinders.nr; i++) {
- if (is_cylinder_used(d, i))
- endPressure << getPressures(d, i, END_PRESSURE);
- }
- return endPressure;
-}
-
-QStringList getFirstGas(const dive *d)
-{
- QStringList gas;
- for (int i = 0; i < d->cylinders.nr; i++) {
- if (is_cylinder_used(d, i))
- gas << get_gas_string(get_cylinder(d, i)->gasmix);
- }
- return gas;
-}
-
-// Add string to sorted QStringList, if it doesn't already exist and
-// it isn't the empty string.
-static void addStringToSortedList(QStringList &l, const char *s)
-{
- if (empty_string(s))
- return;
-
- // Do a binary search for the string. lower_bound() returns an iterator
- // to either the searched-for element or the next higher element if it
- // doesn't exist.
- QString qs(s);
- auto it = std::lower_bound(l.begin(), l.end(), qs); // TODO: use locale-aware sorting
- if (it != l.end() && *it == s)
- return;
-
- // Add new string at sorted position
- l.insert(it, s);
-}
-
-QStringList getFullCylinderList()
-{
- QStringList cylinders;
- struct dive *d;
- int i = 0;
- for_each_dive (i, d) {
- for (int j = 0; j < d->cylinders.nr; j++)
- addStringToSortedList(cylinders, get_cylinder(d, j)->type.description);
- }
-
- for (int ti = 0; ti < tank_info_table.nr; ti++)
- addStringToSortedList(cylinders, tank_info_table.infos[ti].name);
-
- return cylinders;
-}
-
QString formatDiveSalinity(const dive *d)
{
int salinity = get_dive_salinity(d);
@@ -299,9 +162,9 @@ DiveObjectHelper::DiveObjectHelper(const struct dive *d) :
otu(d->otu),
sumWeight(get_weight_string(weight_t { total_weight(d) }, true)),
getCylinder(formatGetCylinder(d)),
- startPressure(getStartPressure(d)),
- endPressure(getEndPressure(d)),
- firstGas(getFirstGas(d)),
+ startPressure(formatStartPressure(d)),
+ endPressure(formatEndPressure(d)),
+ firstGas(formatFirstGas(d)),
salinity(formatDiveSalinity(d)),
waterType(formatDiveWaterType(d))
{
@@ -342,5 +205,5 @@ QString DiveObjectHelper::time() const
QStringList DiveObjectHelper::cylinderList() const
{
- return getFullCylinderList();
+ return formatFullCylinderList();
}
diff --git a/qt-models/divetripmodel.cpp b/qt-models/divetripmodel.cpp
index 6b700a92e..52ad937f5 100644
--- a/qt-models/divetripmodel.cpp
+++ b/qt-models/divetripmodel.cpp
@@ -3,6 +3,7 @@
#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"
@@ -225,11 +226,11 @@ QVariant DiveTripModelBase::diveData(const struct dive *d, int column, int role)
case MobileListModel::DiveSiteRole: return QVariant::fromValue(d->dive_site);
case MobileListModel::CylinderRole: return formatGetCylinder(d).join(", ");
case MobileListModel::GetCylinderRole: return formatGetCylinder(d);
- case MobileListModel::CylinderListRole: return getFullCylinderList();
+ case MobileListModel::CylinderListRole: return formatFullCylinderList();
case MobileListModel::SingleWeightRole: return d->weightsystems.nr <= 1;
- case MobileListModel::StartPressureRole: return getStartPressure(d);
- case MobileListModel::EndPressureRole: return getEndPressure(d);
- case MobileListModel::FirstGasRole: return getFirstGas(d);
+ case MobileListModel::StartPressureRole: return formatStartPressure(d);
+ case MobileListModel::EndPressureRole: return formatEndPressure(d);
+ case MobileListModel::FirstGasRole: return formatFirstGas(d);
case MobileListModel::SelectedRole: return d->selected;
case MobileListModel::DiveInTripRole: return d->divetrip != NULL;
case MobileListModel::IsInvalidRole: return d->invalid;
diff --git a/qt-models/mobilelistmodel.h b/qt-models/mobilelistmodel.h
index e18481efc..5c03a02a7 100644
--- a/qt-models/mobilelistmodel.h
+++ b/qt-models/mobilelistmodel.h
@@ -186,14 +186,4 @@ private:
MobileSwipeModel sm;
};
-// Helper functions - these are actually defined in DiveObjectHelper.cpp. Why declare them here?
-QString formatSac(const dive *d);
-QString formatNotes(const dive *d);
-QString format_gps_decimal(const dive *d);
-QStringList formatGetCylinder(const dive *d);
-QStringList getStartPressure(const dive *d);
-QStringList getEndPressure(const dive *d);
-QStringList getFirstGas(const dive *d);
-QStringList getFullCylinderList();
-
#endif