aboutsummaryrefslogtreecommitdiffstats
path: root/qt-models/diveplannermodel.cpp
diff options
context:
space:
mode:
authorGravatar Berthold Stoeger <bstoeger@mail.tuwien.ac.at>2019-08-04 18:44:57 +0200
committerGravatar Dirk Hohndel <dirk@hohndel.org>2019-11-09 19:19:04 +0100
commit7c9f46acd202121e67557bb634961ef17a9f6c1f (patch)
treef21b6ff42a930cc97c31db9f20c03f1613e70291 /qt-models/diveplannermodel.cpp
parentcd4f66014f7b5269dff9e088c9d7d38241c03156 (diff)
downloadsubsurface-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 'qt-models/diveplannermodel.cpp')
-rw-r--r--qt-models/diveplannermodel.cpp45
1 files changed, 24 insertions, 21 deletions
diff --git a/qt-models/diveplannermodel.cpp b/qt-models/diveplannermodel.cpp
index 537bf278f..8d19afcfb 100644
--- a/qt-models/diveplannermodel.cpp
+++ b/qt-models/diveplannermodel.cpp
@@ -152,29 +152,32 @@ void DivePlannerPointsModel::loadFromDive(dive *d)
// setup the cylinder widget accordingly
void DivePlannerPointsModel::setupCylinders()
{
- int i;
+ clear_cylinder_table(&displayed_dive.cylinders);
if (mode == PLAN && current_dive) {
// take the displayed cylinders from the selected dive as starting point
- CylindersModel::instance()->copyFromDive(current_dive);
- copy_cylinders(current_dive, &displayed_dive, !prefs.display_unused_tanks);
+ copy_used_cylinders(current_dive, &displayed_dive, !prefs.display_unused_tanks);
reset_cylinders(&displayed_dive, true);
- for (i = 0; i < MAX_CYLINDERS; i++)
- if (!cylinder_none(&(displayed_dive.cylinder[i])))
- return; // We have at least one cylinder
+ if (displayed_dive.cylinders.nr > 0) {
+ CylindersModel::instance()->updateDive();
+ return; // We have at least one cylinder
+ }
}
if (!empty_string(prefs.default_cylinder)) {
- fill_default_cylinder(&displayed_dive, 0);
- displayed_dive.cylinder[0].start = displayed_dive.cylinder[0].type.workingpressure;
- }
- if (cylinder_none(&displayed_dive.cylinder[0])) {
+ cylinder_t cyl = { 0 };
+ fill_default_cylinder(&displayed_dive, &cyl);
+ cyl.start = cyl.type.workingpressure;
+ add_to_cylinder_table(&displayed_dive.cylinders, 0, cyl);
+ } else {
+ cylinder_t cyl = { 0 };
// roughly an AL80
- displayed_dive.cylinder[0].type.description = copy_qstring(tr("unknown"));
- displayed_dive.cylinder[0].type.size.mliter = 11100;
- displayed_dive.cylinder[0].type.workingpressure.mbar = 207000;
+ cyl.type.description = copy_qstring(tr("unknown"));
+ cyl.type.size.mliter = 11100;
+ cyl.type.workingpressure.mbar = 207000;
+ add_to_cylinder_table(&displayed_dive.cylinders, 0, cyl);
}
reset_cylinders(&displayed_dive, false);
- CylindersModel::instance()->copyFromDive(&displayed_dive);
+ CylindersModel::instance()->updateDive();
}
// Update the dive's maximum depth. Returns true if max. depth changed
@@ -265,13 +268,13 @@ QVariant DivePlannerPointsModel::data(const QModelIndex &index, int role) const
case GAS:
/* Check if we have the same gasmix two or more times
* If yes return more verbose string */
- int same_gas = same_gasmix_cylinder(&displayed_dive.cylinder[p.cylinderid], p.cylinderid, &displayed_dive, true);
+ int same_gas = same_gasmix_cylinder(&displayed_dive.cylinders.cylinders[p.cylinderid], p.cylinderid, &displayed_dive, true);
if (same_gas == -1)
- return get_gas_string(displayed_dive.cylinder[p.cylinderid].gasmix);
+ return get_gas_string(displayed_dive.cylinders.cylinders[p.cylinderid].gasmix);
else
- return get_gas_string(displayed_dive.cylinder[p.cylinderid].gasmix) +
+ return get_gas_string(displayed_dive.cylinders.cylinders[p.cylinderid].gasmix) +
QString(" (%1 %2 ").arg(tr("cyl.")).arg(p.cylinderid + 1) +
- displayed_dive.cylinder[p.cylinderid].type.description + ")";
+ displayed_dive.cylinders.cylinders[p.cylinderid].type.description + ")";
}
} else if (role == Qt::DecorationRole) {
switch (index.column()) {
@@ -333,7 +336,7 @@ bool DivePlannerPointsModel::setData(const QModelIndex &index, const QVariant &v
p.setpoint = po2;
} break;
case GAS:
- if (value.toInt() >= 0 && value.toInt() < MAX_CYLINDERS)
+ if (value.toInt() >= 0)
p.cylinderid = value.toInt();
/* Did we change the start (dp 0) cylinder to another cylinderid than 0? */
if (value.toInt() != 0 && index.row() == 0)
@@ -890,8 +893,8 @@ void DivePlannerPointsModel::createTemporaryPlan()
// what does the cache do???
struct deco_state *cache = NULL;
struct divedatapoint *dp = NULL;
- for (int i = 0; i < MAX_CYLINDERS; i++) {
- cylinder_t *cyl = &displayed_dive.cylinder[i];
+ for (int i = 0; i < displayed_dive.cylinders.nr; i++) {
+ cylinder_t *cyl = &displayed_dive.cylinders.cylinders[i];
if (cyl->depth.mm && cyl->cylinder_use != NOT_USED) {
dp = create_dp(0, cyl->depth.mm, i, 0);
if (diveplan.dp) {