Preserve events when editing dive in planner

The planner does not know about events except gas
changes. But if the dive comes from the log, we
should preserve the dive computer events. At least
those that happend before we started to delete
waypoints to let the planner take over.

Signed-off-by: Robert C. Helling <helling@atdotde.de>
This commit is contained in:
Robert C. Helling 2020-04-12 13:39:01 +02:00 committed by bstoeger
parent 1690ba7c0c
commit b50d5b63ad
4 changed files with 48 additions and 8 deletions

View file

@ -127,6 +127,11 @@ int event_is_gaschange(const struct event *ev)
ev->type == SAMPLE_EVENT_GASCHANGE2;
}
bool event_is_divemodechange(const struct event *ev)
{
return same_string(ev->name, "modechange");
}
struct event *create_event(unsigned int time, int type, int flags, int value, const char *name)
{
int gas_index = -1;
@ -573,6 +578,31 @@ void copy_events(const struct divecomputer *s, struct divecomputer *d)
*pev = NULL;
}
/* copies all events from all dive computers before a given time
this is used when editing a dive in the planner to preserve the events
of the old dive */
void copy_events_until(const struct dive *sd, struct dive *dd, int time)
{
if (!sd || !dd)
return;
const struct divecomputer *s = &sd->dc;
struct divecomputer *d = &dd->dc;
while (s && d) {
const struct event *ev;
ev = s->events;
while (ev != NULL) {
// Don't add events the planner knows about
if (ev->time.seconds < time && !event_is_gaschange(ev) && !event_is_divemodechange(ev))
add_event(d, ev->time.seconds, ev->type, ev->flags, ev->value, ev->name);
ev = ev->next;
}
s = s->next;
d = d->next;
}
}
int nr_cylinders(const struct dive *dive)
{
return dive->cylinders.nr;

View file

@ -352,6 +352,7 @@ extern struct dive *merge_dives(const struct dive *a, const struct dive *b, int
extern struct dive *try_to_merge(struct dive *a, struct dive *b, bool prefer_downloaded);
extern struct event *clone_event(const struct event *src_ev);
extern void copy_events(const struct divecomputer *s, struct divecomputer *d);
extern void copy_events_until(const struct dive *sd, struct dive *dd, int time);
extern void free_events(struct event *ev);
extern void copy_cylinders(const struct cylinder_table *s, struct cylinder_table *d);
extern void copy_used_cylinders(const struct dive *s, struct dive *d, bool used_only);

View file

@ -150,6 +150,7 @@ void DivePlannerPointsModel::loadFromDive(dive *d)
addStop(0, d->dc.duration.seconds,cylinderid, last_sp.mbar, true, current_divemode);
recalc = oldRec;
DiveTypeSelectionModel::instance()->repopulate();
preserved_until = d->duration;
emitDataChanged();
}
@ -844,10 +845,13 @@ void DivePlannerPointsModel::remove(const QModelIndex &index)
int i;
int rows = rowCount();
if (QApplication::keyboardModifiers() & Qt::ControlModifier) {
preserved_until.seconds = divepoints.at(index.row()).time;
beginRemoveRows(QModelIndex(), index.row(), rows - 1);
for (i = rows - 1; i >= index.row(); i--)
divepoints.remove(i);
} else {
if (index.row() == rows -1)
preserved_until.seconds = divepoints.at(rows - 1).time;
beginRemoveRows(QModelIndex(), index.row(), index.row());
divepoints.remove(index.row());
}
@ -916,6 +920,7 @@ void DivePlannerPointsModel::clear()
endRemoveRows();
}
cylinders.clear();
preserved_until.seconds = 0;
setRecalc(oldRecalc);
}
@ -1218,18 +1223,21 @@ void DivePlannerPointsModel::createPlan(bool replanCopy)
displayed_dive.divetrip = nullptr; // Should not be necessary, just in case!
#if not defined(SUBSURFACE_MOBILE) && not defined(SUBSURFACE_TESTING)
Command::addDive(&displayed_dive, autogroup, true);
#endif // SUBSURFACE_MOBILE SUBSURFACE_TESTING
} else if (replanCopy) {
// we were planning an old dive and save as a new dive
displayed_dive.id = dive_getUniqID(); // Things will break horribly if we create dives with the same id.
#if not defined(SUBSURFACE_MOBILE) && not defined(SUBSURFACE_TESTING)
Command::addDive(&displayed_dive, false, false);
#endif // SUBSURFACE_MOBILE SUBSURFACE_TESTING
} else {
// we were planning an old dive and rewrite the plan
copy_events_until(current_dive, &displayed_dive, preserved_until.seconds);
if (replanCopy) {
// we were planning an old dive and save as a new dive
displayed_dive.id = dive_getUniqID(); // Things will break horribly if we create dives with the same id.
#if not defined(SUBSURFACE_MOBILE) && not defined(SUBSURFACE_TESTING)
Command::replanDive(&displayed_dive);
Command::addDive(&displayed_dive, false, false);
#endif // SUBSURFACE_MOBILE SUBSURFACE_TESTING
} else {
// we were planning an old dive and rewrite the plan
#if not defined(SUBSURFACE_MOBILE) && not defined(SUBSURFACE_TESTING)
Command::replanDive(&displayed_dive);
#endif // SUBSURFACE_MOBILE SUBSURFACE_TESTING
}
}
// Remove and clean the diveplan, so we don't delete

View file

@ -136,6 +136,7 @@ private:
QDateTime startTime;
int instanceCounter = 0;
struct deco_state ds_after_previous_dives;
duration_t preserved_until;
};
#endif