Replace cylinder_is_used with is_cylinder_used

is_cylinder_used uses get_cylinder_index as underlaying function that
does the right thing with with respect on how to find the closest
matching cylinder, and handles both types of gaschange events correctly.

Signed-off-by: Anton Lundin <glance@acc.umu.se>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
This commit is contained in:
Anton Lundin 2014-07-17 17:19:19 +02:00 committed by Dirk Hohndel
parent 810880ea1d
commit 950638ec1c
6 changed files with 7 additions and 32 deletions

2
dive.c
View file

@ -448,7 +448,7 @@ void copy_cylinders(struct dive *s, struct dive *d, bool used_only)
if (!s || !d) if (!s || !d)
return; return;
for (i = 0; i < MAX_CYLINDERS; i++) for (i = 0; i < MAX_CYLINDERS; i++)
if (!used_only || cylinder_is_used(s, &s->cylinder[i])) if (!used_only || is_cylinder_used(s, i))
d->cylinder[i] = s->cylinder[i]; d->cylinder[i] = s->cylinder[i];
else else
memset(&d->cylinder[i], 0, sizeof(cylinder_t)); memset(&d->cylinder[i], 0, sizeof(cylinder_t));

2
dive.h
View file

@ -623,7 +623,7 @@ extern void renumber_dives(int start_nr, bool selected_only);
extern void copy_events(struct divecomputer *s, struct divecomputer *d); extern void copy_events(struct divecomputer *s, struct divecomputer *d);
extern void copy_cylinders(struct dive *s, struct dive *d, bool used_only); extern void copy_cylinders(struct dive *s, struct dive *d, bool used_only);
extern void copy_samples(struct divecomputer *s, struct divecomputer *d); extern void copy_samples(struct divecomputer *s, struct divecomputer *d);
extern bool cylinder_is_used(struct dive *d, cylinder_t *cyl); extern bool is_cylinder_used(struct dive *dive, int idx);
extern void fill_default_cylinder(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_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); 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 o2 = get_o2(&cyl->gasmix);
int he = get_he(&cyl->gasmix); int he = get_he(&cyl->gasmix);
if (!cylinder_is_used(dive, cyl)) if (!is_cylinder_used(dive, i))
continue; continue;
if (cylinder_none(cyl)) if (cylinder_none(cyl))
continue; continue;

View file

@ -73,31 +73,6 @@ bool cylinder_none(void *_data)
return cylinder_nodata(cyl) && cylinder_nosamples(cyl); 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;
}
void get_gas_string(const struct gasmix *gasmix, char *text, int len) void get_gas_string(const struct gasmix *gasmix, char *text, int len)
{ {
if (gasmix_is_air(gasmix)) if (gasmix_is_air(gasmix))

View file

@ -308,7 +308,7 @@ void CylindersModel::updateDive()
for (int i = 0; i < MAX_CYLINDERS; i++) { for (int i = 0; i < MAX_CYLINDERS; i++) {
if (!cylinder_none(&displayed_dive.cylinder[i]) && if (!cylinder_none(&displayed_dive.cylinder[i]) &&
(prefs.display_unused_tanks || (prefs.display_unused_tanks ||
cylinder_is_used(&displayed_dive, &displayed_dive.cylinder[i]) || is_cylinder_used(&displayed_dive, i) ||
displayed_dive.cylinder[i].manually_added)) displayed_dive.cylinder[i].manually_added))
rows = i + 1; rows = i + 1;
} }
@ -324,7 +324,7 @@ void CylindersModel::copyFromDive(dive *d)
return; return;
rows = 0; rows = 0;
for (int i = 0; i < MAX_CYLINDERS; i++) { for (int i = 0; i < MAX_CYLINDERS; i++) {
if (!cylinder_none(&d->cylinder[i]) && cylinder_is_used(d, &d->cylinder[i])) { if (!cylinder_none(&d->cylinder[i]) && is_cylinder_used(d, i)) {
rows = i + 1; rows = i + 1;
} }
} }
@ -360,7 +360,7 @@ void CylindersModel::remove(const QModelIndex &index)
((DivePlannerPointsModel::instance()->currentMode() != DivePlannerPointsModel::NOTHING && ((DivePlannerPointsModel::instance()->currentMode() != DivePlannerPointsModel::NOTHING &&
DivePlannerPointsModel::instance()->tankInUse(cyl->gasmix)) || DivePlannerPointsModel::instance()->tankInUse(cyl->gasmix)) ||
(DivePlannerPointsModel::instance()->currentMode() == DivePlannerPointsModel::NOTHING && (DivePlannerPointsModel::instance()->currentMode() == DivePlannerPointsModel::NOTHING &&
(cyl->manually_added || cylinder_is_used(&displayed_dive, cyl))))) { (cyl->manually_added || is_cylinder_used(&displayed_dive, index.row()))))) {
QMessageBox::warning(MainWindow::instance(), TITLE_OR_TEXT( QMessageBox::warning(MainWindow::instance(), TITLE_OR_TEXT(
tr("Cylinder cannot be removed"), tr("Cylinder cannot be removed"),
tr("This gas is in use. Only cylinders that are not used in the dive can be removed.")), tr("This gas is in use. Only cylinders that are not used in the dive can be removed.")),

View file

@ -292,7 +292,7 @@ void get_selected_dives_text(char *buffer, int size)
} }
} }
static bool is_cylinder_used(struct dive *dive, int idx) bool is_cylinder_used(struct dive *dive, int idx)
{ {
struct divecomputer *dc; struct divecomputer *dc;
bool firstGasExplicit = false; bool firstGasExplicit = false;