diff options
author | Berthold Stoeger <bstoeger@mail.tuwien.ac.at> | 2019-08-04 18:44:57 +0200 |
---|---|---|
committer | Dirk Hohndel <dirk@hohndel.org> | 2019-11-09 19:19:04 +0100 |
commit | 7c9f46acd202121e67557bb634961ef17a9f6c1f (patch) | |
tree | f21b6ff42a930cc97c31db9f20c03f1613e70291 /desktop-widgets | |
parent | cd4f66014f7b5269dff9e088c9d7d38241c03156 (diff) | |
download | subsurface-7c9f46acd202121e67557bb634961ef17a9f6c1f.tar.gz |
Core: remove MAX_CYLINDERS restriction
Instead of using fixed size arrays, use a new cylinder_table structure.
The code copies the weightsystem code, but is significantly more complex
because cylinders are such an integral part of the core.
Two functions to access the cylinders were added:
get_cylinder() and get_or_create_cylinder()
The former does a simple array access and supposes that the cylinder
exists. The latter is used by the parser(s) and if a cylinder with
the given id does not exist, cylinders up to that id are generated.
One point will make C programmers cringe: the cylinder structure is
passed by value. This is due to the way the table-macros work. A
refactoring of the table macros is planned. It has to be noted that
the size of a cylinder_t is 64 bytes, i.e. 8 long words on a 64-bit
architecture, so passing on the stack is probably not even significantly
slower than passing as reference.
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
Diffstat (limited to 'desktop-widgets')
-rw-r--r-- | desktop-widgets/command_edit.cpp | 45 | ||||
-rw-r--r-- | desktop-widgets/command_edit.h | 4 | ||||
-rw-r--r-- | desktop-widgets/divelogexportdialog.cpp | 24 | ||||
-rw-r--r-- | desktop-widgets/simplewidgets.cpp | 4 | ||||
-rw-r--r-- | desktop-widgets/tab-widgets/TabDiveEquipment.cpp | 33 | ||||
-rw-r--r-- | desktop-widgets/tab-widgets/TabDiveInformation.cpp | 8 | ||||
-rw-r--r-- | desktop-widgets/tab-widgets/TabDiveStatistics.cpp | 6 | ||||
-rw-r--r-- | desktop-widgets/tab-widgets/maintab.cpp | 13 |
8 files changed, 55 insertions, 82 deletions
diff --git a/desktop-widgets/command_edit.cpp b/desktop-widgets/command_edit.cpp index 95665722d..97fc05c95 100644 --- a/desktop-widgets/command_edit.cpp +++ b/desktop-widgets/command_edit.cpp @@ -666,18 +666,6 @@ DiveField EditDiveMaster::fieldId() const return DiveField::DIVEMASTER; } -// Helper function to copy cylinders. This supposes that the destination -// cylinder is uninitialized. I.e. the old description is not freed! -static void copy_cylinder(const cylinder_t &s, cylinder_t &d) -{ - d.type.description = copy_string(s.type.description); - d.type.size = s.type.size; - d.type.workingpressure = s.type.workingpressure; - d.gasmix = s.gasmix; - d.cylinder_use = s.cylinder_use; - d.depth = s.depth; -} - static void swapCandQString(QString &q, char *&c) { QString tmp(c); @@ -687,10 +675,9 @@ static void swapCandQString(QString &q, char *&c) } PasteState::PasteState(dive *dIn, const dive *data, dive_components what) : d(dIn), - tags(nullptr), - cylinders(MAX_CYLINDERS) + tags(nullptr) { - memset(&cylinders[0], 0, sizeof(cylinders)); + memset(&cylinders, 0, sizeof(cylinders)); memset(&weightsystems, 0, sizeof(weightsystems)); if (what.notes) notes = data->notes; @@ -708,10 +695,8 @@ PasteState::PasteState(dive *dIn, const dive *data, dive_components what) : d(dI divesite = data->dive_site; if (what.tags) tags = taglist_copy(data->tag_list); - if (what.cylinders) { - for (int i = 0; i < MAX_CYLINDERS; ++i) - copy_cylinder(data->cylinder[i], cylinders[i]); - } + if (what.cylinders) + copy_cylinders(&data->cylinders, &cylinders); if (what.weights) copy_weights(&data->weightsystems, &weightsystems); } @@ -719,8 +704,7 @@ PasteState::PasteState(dive *dIn, const dive *data, dive_components what) : d(dI PasteState::~PasteState() { taglist_free(tags); - for (cylinder_t &c: cylinders) - free((void *)c.type.description); + clear_cylinder_table(&cylinders); clear_weightsystem_table(&weightsystems); free(weightsystems.weightsystems); } @@ -743,10 +727,8 @@ void PasteState::swap(dive_components what) std::swap(divesite, d->dive_site); if (what.tags) std::swap(tags, d->tag_list); - if (what.cylinders) { - for (int i = 0; i < MAX_CYLINDERS; ++i) - std::swap(cylinders[i], d->cylinder[i]); - } + if (what.cylinders) + std::swap(cylinders, d->cylinders); if (what.weights) std::swap(weightsystems, d->weightsystems); } @@ -836,7 +818,7 @@ ReplanDive::ReplanDive(dive *source) : d(current_dive), dc({ 0 }), notes(nullptr) { - memset(&cylinders[0], 0, sizeof(cylinders)); + memset(&cylinders, 0, sizeof(cylinders)); if (!d) return; @@ -849,7 +831,7 @@ ReplanDive::ReplanDive(dive *source) : d(current_dive), surface_pressure = source->surface_pressure; // This resets the dive computers and cylinders of the source dive, avoiding deep copies. - std::swap(source->cylinder, cylinders); + std::swap(source->cylinders, cylinders); std::swap(source->dc, dc); setText(tr("Replan dive")); @@ -857,8 +839,7 @@ ReplanDive::ReplanDive(dive *source) : d(current_dive), ReplanDive::~ReplanDive() { - for (cylinder_t &c: cylinders) - free((void *)c.type.description); + clear_cylinder_table(&cylinders); free_dive_dcs(&dc); free(notes); } @@ -873,7 +854,7 @@ void ReplanDive::undo() std::swap(d->when, when); std::swap(d->maxdepth, maxdepth); std::swap(d->meandepth, meandepth); - std::swap(d->cylinder, cylinders); + std::swap(d->cylinders, cylinders); std::swap(d->dc, dc); std::swap(d->notes, notes); std::swap(d->surface_pressure, surface_pressure); @@ -882,9 +863,11 @@ void ReplanDive::undo() fixup_dive(d); QVector<dive *> divesToNotify = { d }; + // Note that we have to emit cylindersReset before divesChanged, because the divesChanged + // updates the DivePlotDataModel, which is out-of-sync and gets confused. + emit diveListNotifier.cylindersReset(divesToNotify); emit diveListNotifier.divesChanged(divesToNotify, DiveField::DATETIME | DiveField::DURATION | DiveField::DEPTH | DiveField::MODE | DiveField::NOTES | DiveField::SALINITY | DiveField::ATM_PRESS); - emit diveListNotifier.cylindersReset(divesToNotify); } // Redo and undo do the same diff --git a/desktop-widgets/command_edit.h b/desktop-widgets/command_edit.h index 489c51046..45fde9e59 100644 --- a/desktop-widgets/command_edit.h +++ b/desktop-widgets/command_edit.h @@ -246,7 +246,7 @@ struct PasteState { int rating; int visibility; tag_entry *tags; - std::vector<cylinder_t> cylinders; + struct cylinder_table cylinders; struct weightsystem_table weightsystems; PasteState(dive *d, const dive *data, dive_components what); // Read data from dive data for dive d @@ -273,7 +273,7 @@ class ReplanDive : public Base { // Exchange these data with current dive timestamp_t when; depth_t maxdepth, meandepth; - cylinder_t cylinders[MAX_CYLINDERS]; + struct cylinder_table cylinders; struct divecomputer dc; char *notes; pressure_t surface_pressure; diff --git a/desktop-widgets/divelogexportdialog.cpp b/desktop-widgets/divelogexportdialog.cpp index 8f192c39e..e093819dd 100644 --- a/desktop-widgets/divelogexportdialog.cpp +++ b/desktop-widgets/divelogexportdialog.cpp @@ -444,17 +444,17 @@ void DiveLogExportDialog::export_TeX(const char *filename, const bool selected_o // Print cylinder data put_format(&buf, "\n%% Gas use information:\n"); qty_cyl = 0; - for (i = 0; i < MAX_CYLINDERS; i++){ - - if (is_cylinder_used(dive, i) || (prefs.display_unused_tanks && dive->cylinder[i].type.description)){ - put_format(&buf, "\\def\\%scyl%cdescription{%s}\n", ssrf, 'a' + i, dive->cylinder[i].type.description); - put_format(&buf, "\\def\\%scyl%cgasname{%s}\n", ssrf, 'a' + i, gasname(dive->cylinder[i].gasmix)); - put_format(&buf, "\\def\\%scyl%cmixO2{%.1f\\%%}\n", ssrf, 'a' + i, get_o2(dive->cylinder[i].gasmix)/10.0); - put_format(&buf, "\\def\\%scyl%cmixHe{%.1f\\%%}\n", ssrf, 'a' + i, get_he(dive->cylinder[i].gasmix)/10.0); - put_format(&buf, "\\def\\%scyl%cmixN2{%.1f\\%%}\n", ssrf, 'a' + i, (100.0 - (get_o2(dive->cylinder[i].gasmix)/10.0) - (get_he(dive->cylinder[i].gasmix)/10.0))); - delta_p.mbar += dive->cylinder[i].start.mbar - dive->cylinder[i].end.mbar; - put_format(&buf, "\\def\\%scyl%cstartpress{%.1f\\%spressureunit}\n", ssrf, 'a' + i, get_pressure_units(dive->cylinder[i].start.mbar, &unit)/1.0, ssrf); - put_format(&buf, "\\def\\%scyl%cendpress{%.1f\\%spressureunit}\n", ssrf, 'a' + i, get_pressure_units(dive->cylinder[i].end.mbar, &unit)/1.0, ssrf); + for (i = 0; i < dive->cylinders.nr; i++){ + const cylinder_t &cyl = dive->cylinders.cylinders[i]; + if (is_cylinder_used(dive, i) || (prefs.display_unused_tanks && cyl.type.description)){ + put_format(&buf, "\\def\\%scyl%cdescription{%s}\n", ssrf, 'a' + i, cyl.type.description); + put_format(&buf, "\\def\\%scyl%cgasname{%s}\n", ssrf, 'a' + i, gasname(cyl.gasmix)); + put_format(&buf, "\\def\\%scyl%cmixO2{%.1f\\%%}\n", ssrf, 'a' + i, get_o2(cyl.gasmix)/10.0); + put_format(&buf, "\\def\\%scyl%cmixHe{%.1f\\%%}\n", ssrf, 'a' + i, get_he(cyl.gasmix)/10.0); + put_format(&buf, "\\def\\%scyl%cmixN2{%.1f\\%%}\n", ssrf, 'a' + i, (100.0 - (get_o2(cyl.gasmix)/10.0) - (get_he(cyl.gasmix)/10.0))); + delta_p.mbar += cyl.start.mbar - cyl.end.mbar; + put_format(&buf, "\\def\\%scyl%cstartpress{%.1f\\%spressureunit}\n", ssrf, 'a' + i, get_pressure_units(cyl.start.mbar, &unit)/1.0, ssrf); + put_format(&buf, "\\def\\%scyl%cendpress{%.1f\\%spressureunit}\n", ssrf, 'a' + i, get_pressure_units(cyl.end.mbar, &unit)/1.0, ssrf); qty_cyl += 1; } else { put_format(&buf, "\\def\\%scyl%cdescription{}\n", ssrf, 'a' + i); @@ -462,7 +462,7 @@ void DiveLogExportDialog::export_TeX(const char *filename, const bool selected_o put_format(&buf, "\\def\\%scyl%cmixO2{}\n", ssrf, 'a' + i); put_format(&buf, "\\def\\%scyl%cmixHe{}\n", ssrf, 'a' + i); put_format(&buf, "\\def\\%scyl%cmixN2{}\n", ssrf, 'a' + i); - delta_p.mbar += dive->cylinder[i].start.mbar - dive->cylinder[i].end.mbar; + delta_p.mbar += cyl.start.mbar - cyl.end.mbar; put_format(&buf, "\\def\\%scyl%cstartpress{}\n", ssrf, 'a' + i); put_format(&buf, "\\def\\%scyl%cendpress{}\n", ssrf, 'a' + i); qty_cyl += 1; diff --git a/desktop-widgets/simplewidgets.cpp b/desktop-widgets/simplewidgets.cpp index 11af56bef..c49a9886b 100644 --- a/desktop-widgets/simplewidgets.cpp +++ b/desktop-widgets/simplewidgets.cpp @@ -510,9 +510,9 @@ void DiveComponentSelection::buttonClicked(QAbstractButton *button) if (what->cylinders) { int cyl; text << tr("Cylinders:\n"); - for (cyl = 0; cyl < MAX_CYLINDERS; cyl++) { + for (cyl = 0; cyl < displayed_dive.cylinders.nr; cyl++) { if (is_cylinder_used(&displayed_dive, cyl)) - text << displayed_dive.cylinder[cyl].type.description << " " << gasname(displayed_dive.cylinder[cyl].gasmix) << "\n"; + text << displayed_dive.cylinders.cylinders[cyl].type.description << " " << gasname(displayed_dive.cylinders.cylinders[cyl].gasmix) << "\n"; } } if (what->weights) { diff --git a/desktop-widgets/tab-widgets/TabDiveEquipment.cpp b/desktop-widgets/tab-widgets/TabDiveEquipment.cpp index af4dcbd44..6f43fc5a2 100644 --- a/desktop-widgets/tab-widgets/TabDiveEquipment.cpp +++ b/desktop-widgets/tab-widgets/TabDiveEquipment.cpp @@ -207,9 +207,10 @@ static QVector<dive *> getSelectedDivesCurrentLast() return res; } -// TODO: This is only a temporary function until undo of weightsystems is implemented. +// TODO: This are only temporary functions until undo of weightsystems and cylinders is implemented. // Therefore it is not worth putting it in a header. extern bool weightsystems_equal(const dive *d1, const dive *d2); +extern bool cylinders_equal(const dive *d1, const dive *d2); void TabDiveEquipment::acceptChanges() { @@ -227,31 +228,13 @@ void TabDiveEquipment::acceptChanges() if (cylindersModel->changed) { mark_divelist_changed(true); MODIFY_DIVES(selectedDives, - for (int i = 0; i < MAX_CYLINDERS; i++) { - if (mydive != cd) { - if (same_string(mydive->cylinder[i].type.description, cd->cylinder[i].type.description)) { - // if we started out with the same cylinder description (for multi-edit) or if we do copt & paste - // make sure that we have the same cylinder type and copy the gasmix, but DON'T copy the start - // and end pressures (those are per dive after all) - if (!same_string(mydive->cylinder[i].type.description, displayed_dive.cylinder[i].type.description)) { - free((void*)mydive->cylinder[i].type.description); - mydive->cylinder[i].type.description = copy_string(displayed_dive.cylinder[i].type.description); - } - mydive->cylinder[i].type.size = displayed_dive.cylinder[i].type.size; - mydive->cylinder[i].type.workingpressure = displayed_dive.cylinder[i].type.workingpressure; - mydive->cylinder[i].gasmix = displayed_dive.cylinder[i].gasmix; - mydive->cylinder[i].cylinder_use = displayed_dive.cylinder[i].cylinder_use; - mydive->cylinder[i].depth = displayed_dive.cylinder[i].depth; - } - } - } + // if we started out with the same cylinder description (for multi-edit) or if we do copt & paste + // make sure that we have the same cylinder type and copy the gasmix, but DON'T copy the start + // and end pressures (those are per dive after all) + if (cylinders_equal(mydive, cd) && mydive != cd) + copy_cylinder_types(&displayed_dive, cd); + copy_cylinders(&displayed_dive.cylinders, &cd->cylinders); ); - for (int i = 0; i < MAX_CYLINDERS; i++) { - // copy the cylinder but make sure we have our own copy of the strings - free((void*)cd->cylinder[i].type.description); - 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 * and sensor idx in samples as well * - so far this is ONLY supported for a single selected dive */ diff --git a/desktop-widgets/tab-widgets/TabDiveInformation.cpp b/desktop-widgets/tab-widgets/TabDiveInformation.cpp index d5ce7d035..afa427bdc 100644 --- a/desktop-widgets/tab-widgets/TabDiveInformation.cpp +++ b/desktop-widgets/tab-widgets/TabDiveInformation.cpp @@ -53,20 +53,20 @@ void TabDiveInformation::updateProfile() ui->maximumDepthText->setText(get_depth_string(current_dive->maxdepth, true)); ui->averageDepthText->setText(get_depth_string(current_dive->meandepth, true)); - volume_t *gases = get_gas_used(current_dive); + volume_t *gases = get_gas_used(current_dive); QString volumes; - std::vector<int> mean(MAX_CYLINDERS), duration(MAX_CYLINDERS); + std::vector<int> mean(current_dive->cylinders.nr), duration(current_dive->cylinders.nr); per_cylinder_mean_depth(current_dive, select_dc(current_dive), &mean[0], &duration[0]); volume_t sac; QString gaslist, SACs, separator; - for (int i = 0; i < MAX_CYLINDERS; i++) { + for (int i = 0; i < current_dive->cylinders.nr; i++) { if (!is_cylinder_used(current_dive, i)) continue; gaslist.append(separator); volumes.append(separator); SACs.append(separator); separator = "\n"; - gaslist.append(gasname(current_dive->cylinder[i].gasmix)); + gaslist.append(gasname(current_dive->cylinders.cylinders[i].gasmix)); if (!gases[i].mliter) continue; volumes.append(get_volume_string(gases[i], true)); diff --git a/desktop-widgets/tab-widgets/TabDiveStatistics.cpp b/desktop-widgets/tab-widgets/TabDiveStatistics.cpp index ee090c6bc..f3c5f5807 100644 --- a/desktop-widgets/tab-widgets/TabDiveStatistics.cpp +++ b/desktop-widgets/tab-widgets/TabDiveStatistics.cpp @@ -118,16 +118,12 @@ void TabDiveStatistics::updateData() QVector<QPair<QString, int> > gasUsed = selectedDivesGasUsed(); QString gasUsedString; volume_t vol; - for (int j = 0; j < MAX_CYLINDERS; j++) { - if (gasUsed.isEmpty()) - break; + while (!gasUsed.isEmpty()) { QPair<QString, int> gasPair = gasUsed.last(); gasUsed.pop_back(); vol.mliter = gasPair.second; gasUsedString.append(gasPair.first).append(": ").append(get_volume_string(vol, true)).append("\n"); } - if (!gasUsed.isEmpty()) - gasUsedString.append("..."); volume_t o2_tot = {}, he_tot = {}; selected_dives_gas_parts(&o2_tot, &he_tot); diff --git a/desktop-widgets/tab-widgets/maintab.cpp b/desktop-widgets/tab-widgets/maintab.cpp index 3aea3fe39..4d3d7397d 100644 --- a/desktop-widgets/tab-widgets/maintab.cpp +++ b/desktop-widgets/tab-widgets/maintab.cpp @@ -651,13 +651,24 @@ bool weightsystems_equal(const dive *d1, const dive *d2) return true; } +bool cylinders_equal(const dive *d1, const dive *d2) +{ + if (d1->cylinders.nr != d2->cylinders.nr) + return false; + for (int i = 0; i < d1->cylinders.nr; ++i) { + if (!same_cylinder(d1->cylinders.cylinders[i], d2->cylinders.cylinders[i])) + return false; + } + return true; +} + void MainTab::rejectChanges() { EditMode lastMode = editMode; if (lastMode != NONE && current_dive && (modified || - memcmp(¤t_dive->cylinder[0], &displayed_dive.cylinder[0], sizeof(cylinder_t) * MAX_CYLINDERS) || + !cylinders_equal(current_dive, &displayed_dive) || !weightsystems_equal(current_dive, &displayed_dive))) { if (QMessageBox::warning(MainWindow::instance(), TITLE_OR_TEXT(tr("Discard the changes?"), tr("You are about to discard your changes.")), |