diff options
author | Dirk Hohndel <dirk@hohndel.org> | 2013-11-13 21:45:54 +0900 |
---|---|---|
committer | Dirk Hohndel <dirk@hohndel.org> | 2013-11-13 21:56:22 +0900 |
commit | d7fb6853a30d430d73df9d1b076ea194ddeacac2 (patch) | |
tree | a2f33260f6d8bff8c30a8c5cdc7944682aa1fdce | |
parent | aa76d3592361ec7b07a8fc15fd466bcd392629ec (diff) | |
download | subsurface-d7fb6853a30d430d73df9d1b076ea194ddeacac2.tar.gz |
Connect changes in the tanks with the dive that is being added
- you can no longer delete a tank when its gas is in use
- therefore you can no longer delete the last tank
- when you change the gas mix of a tank, the corresponding segments in the
dive change as well
- when changing gas for a segment the correct available gases are offered
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
-rw-r--r-- | qt-ui/diveplanner.cpp | 60 | ||||
-rw-r--r-- | qt-ui/diveplanner.h | 6 | ||||
-rw-r--r-- | qt-ui/models.cpp | 19 |
3 files changed, 83 insertions, 2 deletions
diff --git a/qt-ui/diveplanner.cpp b/qt-ui/diveplanner.cpp index 45360c85c..65a358489 100644 --- a/qt-ui/diveplanner.cpp +++ b/qt-ui/diveplanner.cpp @@ -1263,6 +1263,64 @@ DivePlannerPointsModel::Mode DivePlannerPointsModel::currentMode() const return mode; } +QList<QPair<int, int> > DivePlannerPointsModel::collectGases(struct dive *d) +{ + QList<QPair<int, int> > l; + for (int i = 0; i < MAX_CYLINDERS; i++) { + cylinder_t *cyl = &d->cylinder[i]; + if (!cylinder_nodata(cyl)) + l.push_back(QPair<int, int>(cyl->gasmix.o2.permille, cyl->gasmix.he.permille)); + } + return l; +} +void DivePlannerPointsModel::rememberTanks() +{ + oldGases = collectGases(stagingDive); +} + +bool DivePlannerPointsModel::tankInUse(int o2, int he) +{ + for (int j = 0; j < rowCount(); j++) { + divedatapoint& p = divepoints[j]; + if (p.time == 0) // special entries that hold the available gases + continue; + if ((p.o2 == o2 && p.he == he) || + (is_air(p.o2, p.he) && is_air(o2, he))) + return true; + } + return false; +} + +void DivePlannerPointsModel::tanksUpdated() +{ + if (mode == ADD) { + // we don't know exactly what changed - what we care about is + // "did a gas change on us". So we look through the diveplan to + // see if there is a gas that is now missing and if there is, we + // replace it with the matching new gas. + QList<QPair<int,int> > gases = collectGases(stagingDive); + if (gases.length() == oldGases.length()) { + // either nothing relevant changed, or exactly ONE gasmix changed + for (int i = 0; i < gases.length(); i++) { + if (gases.at(i) != oldGases.at(i)) { + for (int j = 0; j < rowCount(); j++) { + divedatapoint& p = divepoints[j]; + int o2 = oldGases.at(i).first; + int he = oldGases.at(i).second; + if (p.o2 == o2 && p.he == he || + (is_air(p.o2, p.he) && (is_air(o2, he) || (o2 == 0 && he == 0)))) { + p.o2 = gases.at(i).first; + p.he = gases.at(i).second; + } + } + break; + } + } + } + emit dataChanged(createIndex(0, 0), createIndex(rowCount()-1, COLUMNS-1)); + } +} + void DivePlannerPointsModel::clear() { if (mode == ADD) { @@ -1312,8 +1370,8 @@ void DivePlannerPointsModel::createTemporaryPlan() #endif plan(&diveplan, &cache, &tempDive, isPlanner(), &errorString); if (mode == ADD) { + // copy the samples and events, but don't overwrite the cylinders copy_samples(tempDive, current_dive); - copy_cylinders(tempDive, current_dive); copy_events(tempDive, current_dive); } #if DEBUG_PLAN diff --git a/qt-ui/diveplanner.h b/qt-ui/diveplanner.h index f22112d85..fcb679f89 100644 --- a/qt-ui/diveplanner.h +++ b/qt-ui/diveplanner.h @@ -34,6 +34,9 @@ public: void createSimpleDive(); void clear(); Mode currentMode() const; + void tanksUpdated(); + void rememberTanks(); + bool tankInUse(int o2, int he); /** * @return the row number. */ @@ -42,6 +45,8 @@ public: int size(); struct diveplan getDiveplan(); QStringList &getGasList(); + QList<QPair<int, int> > collectGases(dive *d); + public slots: int addStop(int meters = 0, int minutes = 0, int o2 = 0, int he = 0, int ccpoint = 0 ); void addCylinder_clicked(); @@ -73,6 +78,7 @@ private: void deleteTemporaryPlan(struct divedatapoint *dp); QVector<sample> backupSamples; // For editing added dives. struct dive *stagingDive; + QList<QPair<int, int> > oldGases; }; class Button : public QObject, public QGraphicsRectItem { diff --git a/qt-ui/models.cpp b/qt-ui/models.cpp index 2a9208389..51faa0670 100644 --- a/qt-ui/models.cpp +++ b/qt-ui/models.cpp @@ -5,6 +5,8 @@ * */ #include "models.h" +#include "diveplanner.h" +#include "mainwindow.h" #include "../helpers.h" #include "../dive.h" #include "../device.h" @@ -17,6 +19,7 @@ #include <QBrush> #include <QFont> #include <QIcon> +#include <QMessageBox> QFont defaultModelFont() { @@ -196,6 +199,10 @@ void CylindersModel::passInData(const QModelIndex& index, const QVariant& value) bool CylindersModel::setData(const QModelIndex& index, const QVariant& value, int role) { + bool addDiveMode = DivePlannerPointsModel::instance()->currentMode() != DivePlannerPointsModel::NOTHING; + if (addDiveMode) + DivePlannerPointsModel::instance()->rememberTanks(); + cylinder_t *cyl = cylinderAt(index); switch(index.column()) { case TYPE: @@ -296,6 +303,8 @@ bool CylindersModel::setData(const QModelIndex& index, const QVariant& value, in } } dataChanged(index, index); + if (addDiveMode) + DivePlannerPointsModel::instance()->tanksUpdated(); return true; } @@ -311,7 +320,7 @@ void CylindersModel::add() } int row = rows; - + fill_default_cylinder(¤t->cylinder[row]); beginInsertRows(QModelIndex(), row, row); rows++; changed = true; @@ -366,6 +375,14 @@ void CylindersModel::remove(const QModelIndex& index) if (index.column() != REMOVE) { return; } + cylinder_t *cyl = ¤t->cylinder[index.row()]; + if (DivePlannerPointsModel::instance()->tankInUse(cyl->gasmix.o2.permille, cyl->gasmix.he.permille)) { + QMessageBox::warning(mainWindow(), + 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; + } beginRemoveRows(QModelIndex(), index.row(), index.row()); // yah, know, ugly. rows--; remove_cylinder(current, index.row()); |