summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar Dirk Hohndel <dirk@hohndel.org>2014-06-01 12:38:32 -0700
committerGravatar Dirk Hohndel <dirk@hohndel.org>2014-06-01 12:43:40 -0700
commitc539c8f861928c637f6b3e790b05e89914e2be8f (patch)
tree9728f8d08279bbe98fa8afdec73de9de1d55b66a
parent1a040134538b7733f3088ea34f101cfedecc2c64 (diff)
downloadsubsurface-c539c8f861928c637f6b3e790b05e89914e2be8f.tar.gz
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>
-rw-r--r--dive.c26
-rw-r--r--dive.h4
-rw-r--r--divelist.c2
-rw-r--r--equipment.c25
-rw-r--r--qt-ui/diveplanner.cpp6
-rw-r--r--qt-ui/models.cpp14
6 files changed, 39 insertions, 38 deletions
diff --git a/dive.c b/dive.c
index 6545fecab..aad4b07a5 100644
--- a/dive.c
+++ b/dive.c
@@ -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);
diff --git a/dive.h b/dive.h
index a2d9c5425..5861c9f09 100644
--- a/dive.h
+++ b/dive.h
@@ -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);
diff --git a/divelist.c b/divelist.c
index 421ea28c2..489982465 100644
--- a/divelist.c
+++ b/divelist.c
@@ -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;
diff --git a/equipment.c b/equipment.c
index b69155629..a67df68a4 100644
--- a/equipment.c
+++ b/equipment.c
@@ -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;
diff --git a/qt-ui/diveplanner.cpp b/qt-ui/diveplanner.cpp
index 252c0ffc3..1c6c05702 100644
--- a/qt-ui/diveplanner.cpp
+++ b/qt-ui/diveplanner.cpp
@@ -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;
}
}
}
diff --git a/qt-ui/models.cpp b/qt-ui/models.cpp
index 2228c8e5e..340276059 100644
--- a/qt-ui/models.cpp
+++ b/qt-ui/models.cpp
@@ -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,9 +315,8 @@ 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;
@@ -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,9 +357,10 @@ 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("Cylinder cannot be removed"),
tr("This gas in use. Only cylinders that are not used in the dive can be removed.")),
QMessageBox::Ok);
return;