mirror of
https://github.com/subsurface/subsurface.git
synced 2024-11-28 05:00:20 +00:00
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>
This commit is contained in:
parent
dc37ba7758
commit
bf8261c001
7 changed files with 178 additions and 156 deletions
|
@ -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 \
|
||||
|
|
|
@ -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
|
||||
|
|
145
core/string-format.cpp
Normal file
145
core/string-format.cpp
Normal file
|
@ -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 "\n"
|
||||
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;
|
||||
}
|
||||
|
19
core/string-format.h
Normal file
19
core/string-format.h
Normal file
|
@ -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
|
|
@ -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 "\n"
|
||||
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();
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue