Remove the .used member of the cylinder structure

Instead calculate this information on the fly, taking into account all
dive computers on the dive in questions.

There is one wrinkle to this - previously we abused the '.used' member to
make sure that a manually added cylinder didn't disappear the moment it
was added (think of the workflow: you add a cylinder, then you add a gas
change to that cylinder -> right after you add it it is unused and would
not be shown).

I am thinking that we might have to add the "manually_added" property to
the properties that we store in XML / git.

Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
This commit is contained in:
Dirk Hohndel 2014-06-01 12:38:32 -07:00
parent 1a04013453
commit c539c8f861
6 changed files with 39 additions and 38 deletions

26
dive.c
View file

@ -678,31 +678,6 @@ static struct event *find_previous_event(struct divecomputer *dc, struct event *
return previous;
}
/* mark all tanks that we switch to in this dive computer's data as used */
static void mark_used_tanks(struct dive *dive, struct divecomputer *dc)
{
struct event *ev = get_next_event(dc->events, "gaschange");
// unless there is a gas change in the first 30 seconds we can
// always mark the first cylinder as used
if (!ev || ev->time.seconds > 30)
dive->cylinder[0].used = true;
while (ev) {
int idx = get_cylinder_index(dive, ev);
dive->cylinder[idx].used = true;
ev = get_next_event(ev->next, "gaschange");
}
}
/* walk all divecomputers to find the unused tanks in this dive */
static void check_for_unused_tanks(struct dive *dive)
{
struct divecomputer *dc;
for_each_dc(dive, dc) {
mark_used_tanks(dive, dc);
}
}
static void fixup_surface_pressure(struct dive *dive)
{
struct divecomputer *dc;
@ -988,7 +963,6 @@ struct dive *fixup_dive(struct dive *dive)
fixup_duration(dive);
fixup_watertemp(dive);
fixup_airtemp(dive);
check_for_unused_tanks(dive);
for (i = 0; i < MAX_CYLINDERS; i++) {
cylinder_t *cyl = dive->cylinder + i;
add_cylinder_description(&cyl->type);

4
dive.h
View file

@ -59,7 +59,7 @@ typedef struct
struct gasmix gasmix;
pressure_t start, end, sample_start, sample_end;
depth_t depth;
bool used;
bool manually_added;
volume_t gas_used;
} cylinder_t;
@ -565,7 +565,7 @@ extern void renumber_dives(int start_nr, bool selected_only);
extern void copy_events(struct dive *s, struct dive *d);
extern void copy_cylinders(struct dive *s, struct dive *d);
extern void copy_samples(struct dive *s, struct dive *d);
extern bool cylinder_is_used(struct dive *d, cylinder_t *cyl);
extern void fill_default_cylinder(cylinder_t *cyl);
extern void add_gas_switch_event(struct dive *dive, struct divecomputer *dc, int time, int idx);
extern void add_event(struct divecomputer *dc, int time, int type, int flags, int value, const char *name);

View file

@ -116,7 +116,7 @@ void get_dive_gas(struct dive *dive, int *o2_p, int *he_p, int *o2low_p)
int o2 = get_o2(&cyl->gasmix);
int he = get_he(&cyl->gasmix);
if (!cyl->used)
if (cylinder_is_used(dive, cyl))
continue;
if (cylinder_none(cyl))
continue;

View file

@ -72,6 +72,31 @@ bool cylinder_none(void *_data)
return cylinder_nodata(cyl) && cylinder_nosamples(cyl);
}
/* look at all dive computers and figure out if this cylinder is used anywhere
* d has to be a valid dive (test before calling)
* cyl does not have to be a cylinder that is part of this dive structure */
bool cylinder_is_used(struct dive *d, cylinder_t *cyl)
{
struct divecomputer *dc = &d->dc;
bool same_as_first = gasmix_distance(&cyl->gasmix, &d->cylinder[0].gasmix) < 200;
while (dc) {
struct event *ev = get_next_event(dc->events, "gaschange");
if (same_as_first && (!ev || ev->time.seconds > 30)) {
// unless there is a gas change in the first 30 seconds we can
// always mark the first cylinder as used
return true;
}
while (ev) {
if (gasmix_distance(&cyl->gasmix, get_gasmix_from_event(ev)) < 200)
return true;
ev = get_next_event(ev->next, "gaschange");
}
dc = dc->next;
}
return false;
}
bool weightsystem_none(void *_data)
{
weightsystem_t *ws = _data;

View file

@ -120,8 +120,13 @@ void DivePlannerPointsModel::setupCylinders()
return;
if (stagingDive != current_dive) {
// we are planning a dive
if (current_dive) {
// take the cylinders from the selected dive as starting point
CylindersModel::instance()->copyFromDive(current_dive);
copy_cylinders(current_dive, stagingDive);
reset_cylinders(stagingDive);
return;
} else {
if (!same_string(prefs.default_cylinder, "")) {
fill_default_cylinder(&stagingDive->cylinder[0]);
@ -130,7 +135,6 @@ void DivePlannerPointsModel::setupCylinders()
stagingDive->cylinder[0].type.description = strdup(tr("unknown").toUtf8().constData());
stagingDive->cylinder[0].type.size.mliter = 11100;
stagingDive->cylinder[0].type.workingpressure.mbar = 207000;
stagingDive->cylinder[0].used = true;
}
}
}

View file

@ -286,8 +286,7 @@ void CylindersModel::add()
int row = rows;
fill_default_cylinder(&current->cylinder[row]);
// mark the cylinder as 'used' since it was manually added
current->cylinder[row].used = true;
current->cylinder[row].manually_added = true;
beginInsertRows(QModelIndex(), row, row);
rows++;
changed = true;
@ -316,10 +315,9 @@ void CylindersModel::setDive(dive *d)
rows = 0;
for (int i = 0; i < MAX_CYLINDERS; i++) {
if (!cylinder_none(&d->cylinder[i]) &&
(prefs.display_unused_tanks || d->cylinder[i].used)) {
(prefs.display_unused_tanks || cylinder_is_used(d, &d->cylinder[i]) || d->cylinder[i].manually_added))
rows = i + 1;
}
}
current = d;
changed = false;
if (rows > 0) {
@ -334,8 +332,7 @@ void CylindersModel::copyFromDive(dive *d)
return;
rows = 0;
for (int i = 0; i < MAX_CYLINDERS; i++) {
if (!cylinder_none(&d->cylinder[i]) &&
(prefs.display_unused_tanks || d->cylinder[i].used)) {
if (!cylinder_none(&d->cylinder[i]) && cylinder_is_used(d, &d->cylinder[i])) {
rows = i + 1;
}
}
@ -360,7 +357,8 @@ void CylindersModel::remove(const QModelIndex &index)
cylinder_t *cyl = &current->cylinder[index.row()];
if ((DivePlannerPointsModel::instance()->currentMode() != DivePlannerPointsModel::NOTHING &&
DivePlannerPointsModel::instance()->tankInUse(cyl->gasmix.o2.permille, cyl->gasmix.he.permille)) ||
(DivePlannerPointsModel::instance()->currentMode() == DivePlannerPointsModel::NOTHING && cyl->used)) {
(DivePlannerPointsModel::instance()->currentMode() == DivePlannerPointsModel::NOTHING &&
(cyl->manually_added || (current_dive && cylinder_is_used(current_dive, cyl))))) {
QMessageBox::warning(MainWindow::instance(), TITLE_OR_TEXT(
tr("Cylinder cannot be removed"),
tr("This gas in use. Only cylinders that are not used in the dive can be removed.")),