undo: pass dive as unique_ptr to addDive()

Before, a non-owning pointer was passed and the dive moved
away from the dive. Instead, let the caller decide if they
still want to keep a copy of the dive, or give up ownership:

In MainWindow and QMLManager new dives are generated, so
one might just as well give up ownership. In contrast,
the planner works on a copy (originally the infamous
"displayed_dive") and now moves the data manually.

This commit also removes duplicate code, by moving the
"create default dive" code from MainWindow and QMLManager
to struct dive.

Finally, determination of the "time zone offset" is not done
in POSIX, since we want to avoid calls form the core into
Qt.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2024-06-21 16:43:27 +02:00 committed by bstoeger
parent bdd5527005
commit 4a165980e7
13 changed files with 71 additions and 77 deletions

View file

@ -28,6 +28,8 @@
#include "trip.h"
#include "fulltext.h"
#include <time.h>
// For user visible text but still not translated
const char *divemode_text_ui[] = {
QT_TRANSLATE_NOOP("gettextFromC", "Open circuit"),
@ -39,8 +41,8 @@ const char *divemode_text_ui[] = {
// For writing/reading files.
const char *divemode_text[] = {"OC", "CCR", "PSCR", "Freedive"};
// It's the "manually added" divecomputer.
// Even for dives without divecomputer, we allocate a divecomputer structure.
// It's the "manually added" divecomputer.
dive::dive() : dcs(1)
{
id = dive_getUniqID();
@ -51,6 +53,22 @@ dive::dive(dive &&) = default;
dive &dive::operator=(const dive &) = default;
dive::~dive() = default;
// create a dive an hour from now with a default depth (15m/45ft) and duration (40 minutes)
// as a starting point for the user to edit
std::unique_ptr<dive> dive::default_dive()
{
auto d = std::make_unique<dive>();
d->when = time(nullptr) + gettimezoneoffset() + 3600;
d->dcs[0].duration.seconds = 40 * 60;
d->dcs[0].maxdepth.mm = M_OR_FT(15, 45);
d->dcs[0].meandepth.mm = M_OR_FT(13, 39); // this creates a resonable looking safety stop
make_manually_added_dive_dc(&d->dcs[0]);
fake_dc(&d->dcs[0]);
add_default_cylinder(d.get());
fixup_dive(d.get());
return d;
}
/*
* The legacy format for sample pressures has a single pressure
* for each sample that can have any sensor, plus a possible
@ -178,16 +196,6 @@ void copy_dive(const struct dive *s, struct dive *d)
invalidate_dive_cache(d);
}
/* make a clone of the source dive and clean out the source dive;
* this allows us to create a dive on the stack and then
* add it to the divelist. */
struct std::unique_ptr<dive> move_dive(struct dive *s)
{
auto d = std::make_unique<dive>();
std::swap(*s, *d);
return d;
}
#define CONDITIONAL_COPY_STRING(_component) \
if (what._component) \
d->_component = s->_component

View file

@ -78,6 +78,7 @@ struct dive {
dive(const dive &);
dive(dive &&);
dive &operator=(const dive &);
static std::unique_ptr<dive> default_dive();
timestamp_t endtime() const; /* maximum over divecomputers (with samples) */
duration_t totaltime() const; /* maximum over divecomputers (with samples) */
@ -172,7 +173,6 @@ extern bool subsurface_user_is_root();
extern void clear_dive(struct dive *dive);
extern void copy_dive(const struct dive *s, struct dive *d);
extern void selective_copy_dive(const struct dive *s, struct dive *d, struct dive_components what, bool clear);
extern struct std::unique_ptr<dive> move_dive(struct dive *s);
extern int legacy_format_o2pressures(const struct dive *dive, const struct divecomputer *dc);

View file

@ -692,15 +692,6 @@ QString getPrintingTemplatePathBundle()
return path;
}
int gettimezoneoffset()
{
QDateTime dt1, dt2;
dt1 = QDateTime::currentDateTime();
dt2 = dt1.toUTC();
dt1.setTimeSpec(Qt::UTC);
return dt2.secsTo(dt1);
}
QDateTime timestampToDateTime(timestamp_t when)
{
// Subsurface always uses "local time" as in "whatever was the local time at the location"

View file

@ -64,7 +64,6 @@ QString get_water_type_string(int salinity);
QString getSubsurfaceDataPath(QString folderToFind);
QString getPrintingTemplatePathUser();
QString getPrintingTemplatePathBundle();
int gettimezoneoffset();
QDateTime timestampToDateTime(timestamp_t when);
timestamp_t dateTimeToTimestamp(const QDateTime &t);
int parseDurationToSeconds(const QString &text);

View file

@ -10,6 +10,7 @@ extern timestamp_t utc_mktime(const struct tm *tm);
extern void utc_mkdate(timestamp_t, struct tm *tm);
extern int utc_year(timestamp_t timestamp);
extern int utc_weekday(timestamp_t timestamp);
extern int gettimezoneoffset();
/* parse and format date times of the form YYYY-MM-DD hh:mm:ss */
extern timestamp_t parse_datetime(const char *s); /* returns 0 on error */

View file

@ -225,3 +225,18 @@ const char *monthname(int mon)
};
return translate("gettextFromC", month_array[mon]);
}
int gettimezoneoffset()
{
time_t now = time(nullptr);
#ifdef WIN32
// Somewhat surprisingly, Windows doesn't have localtime_r (I thought this was POSIX?).
// Let's use the global timezone variable.
// Ultimately, use the portable C++20 API.
return static_cast<int>(-timezone);
#else
struct tm local;
localtime_r(&now, &local);
return local.tm_gmtoff;
#endif
}