When deleting a cylinder, adapt gas changes

When we delete a cylinder the gas changes in that dive may have to be
adjusted. We didn't do this at all in the past. With this commit we should
be doing this right for a single dive that is being edited.

This does NOT handle multiple dives being edited at the same time (or more
specifically - if you have multiple dives selected and delete a cylinder,
the dives that had the same set of cylinders (other than the displayed
dive) will get that particular cylinder deleted, but won't have their gas
change events (and sensor data in the samples) adapted.

Possibly we should simply prohibit deleting cylinders when more than one
dive are selected.

See #834

Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
This commit is contained in:
Dirk Hohndel 2015-04-23 15:24:02 -07:00
parent bbfa2b655b
commit 415abeea66
4 changed files with 29 additions and 2 deletions

4
dive.c
View file

@ -1758,7 +1758,7 @@ static void add_initial_gaschange(struct dive *dive, struct divecomputer *dc)
add_gas_switch_event(dive, dc, 0, 0);
}
static void dc_cylinder_renumber(struct dive *dive, struct divecomputer *dc, int mapping[])
void dc_cylinder_renumber(struct dive *dive, struct divecomputer *dc, int mapping[])
{
int i;
struct event *ev;
@ -2259,7 +2259,7 @@ struct dive *try_to_merge(struct dive *a, struct dive *b, bool prefer_downloaded
return NULL;
}
static void free_events(struct event *ev)
void free_events(struct event *ev)
{
while (ev) {
struct event *next = ev->next;

2
dive.h
View file

@ -349,6 +349,7 @@ struct dive {
};
extern int get_cylinder_idx_by_use(struct dive *dive, enum cylinderuse cylinder_use_type);
extern void dc_cylinder_renumber(struct dive *dive, struct divecomputer *dc, int mapping[]);
/* when selectively copying dive information, which parts should be copied? */
struct dive_components {
@ -721,6 +722,7 @@ extern struct dive *merge_dives(struct dive *a, struct dive *b, int offset, bool
extern struct dive *try_to_merge(struct dive *a, struct dive *b, bool prefer_downloaded);
extern void renumber_dives(int start_nr, bool selected_only);
extern void copy_events(struct divecomputer *s, struct divecomputer *d);
extern void free_events(struct event *ev);
extern void copy_cylinders(struct dive *s, struct dive *d, bool used_only);
extern void copy_samples(struct divecomputer *s, struct divecomputer *d);
extern bool is_cylinder_used(struct dive *dive, int idx);

View file

@ -813,6 +813,16 @@ void MainTab::acceptChanges()
cd->cylinder[i] = displayed_dive.cylinder[i];
cd->cylinder[i].type.description = copy_string(displayed_dive.cylinder[i].type.description);
}
/* if cylinders changed we may have changed gas change events
* - so far this is ONLY supported for a single selected dive */
struct divecomputer *tdc = &current_dive->dc;
struct divecomputer *sdc = &displayed_dive.dc;
while(tdc && sdc) {
free_events(tdc->events);
copy_events(sdc, tdc);
tdc = tdc->next;
sdc = sdc->next;
}
do_replot = true;
}

View file

@ -388,6 +388,7 @@ Qt::ItemFlags CylindersModel::flags(const QModelIndex &index) const
void CylindersModel::remove(const QModelIndex &index)
{
int mapping[MAX_CYLINDERS];
if (index.column() != REMOVE) {
return;
}
@ -395,6 +396,7 @@ void CylindersModel::remove(const QModelIndex &index)
cylinder_t *cyl = &displayed_dive.cylinder[index.row()];
struct gasmix *mygas = &cyl->gasmix;
for (int i = 0; i < MAX_CYLINDERS; i++) {
mapping[i] = i;
if (i == index.row() || cylinder_none(&displayed_dive.cylinder[i]))
continue;
struct gasmix *gas2 = &displayed_dive.cylinder[i].gasmix;
@ -419,11 +421,24 @@ void CylindersModel::remove(const QModelIndex &index)
// as first gas
memmove(cyl, &displayed_dive.cylinder[same_gas], sizeof(*cyl));
remove_cylinder(&displayed_dive, same_gas);
mapping[same_gas] = 0;
for (int i = same_gas + 1; i < MAX_CYLINDERS; i++)
mapping[i] = i - 1;
} else {
remove_cylinder(&displayed_dive, index.row());
if (same_gas > index.row())
same_gas--;
mapping[index.row()] = same_gas;
for (int i = index.row() + 1; i < MAX_CYLINDERS; i++)
mapping[i] = i - 1;
}
changed = true;
endRemoveRows();
struct divecomputer *dc = &displayed_dive.dc;
while (dc) {
dc_cylinder_renumber(&displayed_dive, dc, mapping);
dc = dc->next;
}
}
WeightModel::WeightModel(QObject *parent) : CleanerTableModel(parent),