From 45c706b7fc2b980c4a9ad1c53c2d7f47def391a5 Mon Sep 17 00:00:00 2001
From: "Robert C. Helling" <helling@atdotde.de>
Date: Wed, 25 Jun 2014 14:00:03 +0200
Subject: [PATCH] Make pO2 settings functional in planner

Spin boxes for pO2 are now hooked up to preference values. Adding new cylinders (or changing their fo2)
computes the MOD accordin to the current value of decopo2. Note that chaning the limits for deco pO2
does _not_ automatically update the swith depth of all cylinders as those might have been manually entered.

Furthermore, MOD has now to option of rounding to multiples of a given depth. That is used for the automatic
switch depth which are now always multiples of 3m (so that EAN50 is switched to at 21m rather than 22m).

Signed-off-by: Robert C. Helling <helling@atdotde.de>
---
 dive.h                   |  5 +++--
 equipment.c              |  2 +-
 profile.c                |  2 +-
 qt-ui/diveplanner.cpp    | 18 ++++++++++++++++--
 qt-ui/diveplanner.h      |  2 ++
 qt-ui/models.cpp         |  4 ++--
 qt-ui/models.h           |  2 +-
 qt-ui/plannerSettings.ui | 46 ++++++++++++++++++++++++++++------------------
 subsurfacestartup.c      |  4 +++-
 9 files changed, 57 insertions(+), 28 deletions(-)

diff --git a/dive.h b/dive.h
index a5c7606..f06791c 100644
--- a/dive.h
+++ b/dive.h
@@ -134,9 +134,10 @@ static inline int interpolate(int a, int b, int part, int whole)
 	return rint(x / whole);
 }
 
-static inline depth_t gas_mod(struct gasmix *mix, pressure_t po2_limit) {
+/* MOD rounded to multiples of roundto mm */
+static inline depth_t gas_mod(struct gasmix *mix, pressure_t po2_limit, int roundto) {
 	depth_t depth;
-	depth.mm = po2_limit.mbar * 1000 / get_o2(mix) * 10 - 10000;
+	depth.mm = ((po2_limit.mbar * 1000 / get_o2(mix) * 10 - 10000) / roundto) * roundto;
 	return depth;
 }
 
diff --git a/equipment.c b/equipment.c
index 0201680..6d09e6f 100644
--- a/equipment.c
+++ b/equipment.c
@@ -241,7 +241,7 @@ void reset_cylinders(struct dive *dive)
 		if (cylinder_none(cyl))
 			continue;
 		if (cyl->depth.mm == 0) /* if the gas doesn't give a mod, assume conservative pO2 */
-			cyl->depth = gas_mod(&cyl->gasmix, pO2);
+			cyl->depth = gas_mod(&cyl->gasmix, pO2, 3000);
 		if (cyl->type.workingpressure.mbar)
 			cyl->start.mbar = cyl->end.mbar = cyl->type.workingpressure.mbar;
 		cyl->gas_used.mliter = 0;
diff --git a/profile.c b/profile.c
index ef0cb79..a0bde3c 100644
--- a/profile.c
+++ b/profile.c
@@ -1163,7 +1163,7 @@ static void calculate_gas_information_new(struct dive *dive, struct plot_info *p
 		 * END takes O₂ + N₂ (air) into account ("Narcotic" for trimix dives)
 		 * EAD just uses N₂ ("Air" for nitrox dives) */
 		pressure_t modpO2 = { .mbar = (int) (prefs.modpO2 * 1000) };
-		entry->mod = (double) gas_mod(&dive->cylinder[cylinderindex].gasmix, modpO2).mm;
+		entry->mod = (double) gas_mod(&dive->cylinder[cylinderindex].gasmix, modpO2, 1).mm;
 		entry->end = (entry->depth + 10000) * (1000 - fhe) / 1000.0 - 10000;
 		entry->ead = (entry->depth + 10000) * (1000 - fo2 - fhe) / (double)N2_IN_AIR - 10000;
 		entry->eadd = (entry->depth + 10000) *
diff --git a/qt-ui/diveplanner.cpp b/qt-ui/diveplanner.cpp
index 249b037..5b47863 100644
--- a/qt-ui/diveplanner.cpp
+++ b/qt-ui/diveplanner.cpp
@@ -363,6 +363,8 @@ PlannerSettingsWidget::PlannerSettingsWidget(QWidget *parent, Qt::WindowFlags f)
 	connect(ui.ascRateLast6m, SIGNAL(valueChanged(int)), plannerModel, SLOT(emitDataChanged()));
 	connect(ui.descRate, SIGNAL(valueChanged(int)), this, SLOT(setDescRate()));
 	connect(ui.descRate, SIGNAL(valueChanged(int)), plannerModel, SLOT(emitDataChanged()));
+	connect(ui.bottompo2, SIGNAL(valueChanged(double)), this, SLOT(setBottomPo2(double)));
+	connect(ui.decopo2, SIGNAL(valueChanged(double)), this, SLOT(setDecoPo2(double)));
 
 	setMinimumWidth(0);
 	setMinimumHeight(0);
@@ -413,6 +415,17 @@ void PlannerSettingsWidget::setDescRate(int rate)
 	prefs.descrate = rate * UNIT_FACTOR;
 }
 
+void PlannerSettingsWidget::setBottomPo2(double po2)
+{
+	prefs.bottompo2 = (int) (po2 * 1000.0);
+}
+
+void PlannerSettingsWidget::setDecoPo2(double po2)
+{
+	prefs.decopo2 = (int) (po2 * 1000.0);
+}
+
+
 void DivePlannerPointsModel::setPlanMode(Mode m)
 {
 	mode = m;
@@ -678,8 +691,9 @@ bool DivePlannerPointsModel::addGas(struct gasmix mix)
 			/* The depth to change to that gas is given by the depth where its pO₂ is 1.6 bar.
 			 * The user should be able to change this depth manually. */
 			pressure_t modpO2;
-			modpO2.mbar = 1600;
-			cyl->depth = gas_mod(&mix, modpO2);
+			modpO2.mbar = prefs.decopo2;
+			cyl->depth = gas_mod(&mix, modpO2, 3000);
+
 			CylindersModel::instance()->setDive(stagingDive);
 			return true;
 		}
diff --git a/qt-ui/diveplanner.h b/qt-ui/diveplanner.h
index 9f6052b..5955a5a 100644
--- a/qt-ui/diveplanner.h
+++ b/qt-ui/diveplanner.h
@@ -164,6 +164,8 @@ slots:
 	void setAscRateStops(int rate);
 	void setAscRateLast6m(int rate);
 	void setDescRate(int rate);
+	void setBottomPo2(double po2);
+	void setDecoPo2(double po2);
 
 private:
 	Ui::plannerSettingsWidget ui;
diff --git a/qt-ui/models.cpp b/qt-ui/models.cpp
index 167f548..a5a1f5b 100644
--- a/qt-ui/models.cpp
+++ b/qt-ui/models.cpp
@@ -250,8 +250,8 @@ bool CylindersModel::setData(const QModelIndex &index, const QVariant &value, in
 		if (CHANGED()) {
 			cyl->gasmix.o2 = string_to_fraction(vString.toUtf8().data());
 			pressure_t modpO2;
-			modpO2.mbar = 1600;
-			cyl->depth = gas_mod(&cyl->gasmix, modpO2);
+			modpO2.mbar = prefs.decopo2;
+			cyl->depth = gas_mod(&cyl->gasmix, modpO2, 3000);
 			changed = true;
 		}
 		break;
diff --git a/qt-ui/models.h b/qt-ui/models.h
index 17be8cb..3064265 100644
--- a/qt-ui/models.h
+++ b/qt-ui/models.h
@@ -116,7 +116,6 @@ public:
 	void passInData(const QModelIndex &index, const QVariant &value);
 	void add();
 	void clear();
-	void update();
 	void setDive(struct dive *d);
 	void copyFromDive(struct dive *d);
 	cylinder_t *cylinderAt(const QModelIndex &index);
@@ -125,6 +124,7 @@ public:
 public
 slots:
 	void remove(const QModelIndex &index);
+	void update();
 
 private:
 	struct dive *current;
diff --git a/qt-ui/plannerSettings.ui b/qt-ui/plannerSettings.ui
index 519f3ea..57814ea 100644
--- a/qt-ui/plannerSettings.ui
+++ b/qt-ui/plannerSettings.ui
@@ -72,16 +72,6 @@
             </property>
            </widget>
           </item>
-          <item row="1" column="1">
-           <widget class="QPlainTextEdit" name="bottompo2">
-            <property name="maximumSize">
-             <size>
-              <width>16777215</width>
-              <height>20</height>
-             </size>
-            </property>
-           </widget>
-          </item>
           <item row="2" column="0">
            <widget class="QLabel" name="label_7">
             <property name="text">
@@ -90,12 +80,34 @@
            </widget>
           </item>
           <item row="2" column="1">
-           <widget class="QPlainTextEdit" name="decopo2">
-            <property name="maximumSize">
-             <size>
-              <width>16777215</width>
-              <height>20</height>
-             </size>
+           <widget class="QDoubleSpinBox" name="decopo2">
+            <property name="suffix">
+             <string>bar</string>
+            </property>
+            <property name="maximum">
+             <double>2.000000000000000</double>
+            </property>
+            <property name="singleStep">
+             <double>0.100000000000000</double>
+            </property>
+            <property name="value">
+             <double>1.600000000000000</double>
+            </property>
+           </widget>
+          </item>
+          <item row="1" column="1">
+           <widget class="QDoubleSpinBox" name="bottompo2">
+            <property name="suffix">
+             <string>bar</string>
+            </property>
+            <property name="maximum">
+             <double>2.000000000000000</double>
+            </property>
+            <property name="singleStep">
+             <double>0.100000000000000</double>
+            </property>
+            <property name="value">
+             <double>1.400000000000000</double>
             </property>
            </widget>
           </item>
@@ -299,8 +311,6 @@
   <tabstop>display_transitions</tabstop>
   <tabstop>lastStop</tabstop>
   <tabstop>backgasBreaks</tabstop>
-  <tabstop>bottompo2</tabstop>
-  <tabstop>decopo2</tabstop>
   <tabstop>descRate</tabstop>
  </tabstops>
  <resources/>
diff --git a/subsurfacestartup.c b/subsurfacestartup.c
index 463aea6..8d8d848 100644
--- a/subsurfacestartup.c
+++ b/subsurfacestartup.c
@@ -37,7 +37,9 @@ struct preferences default_prefs = {
 	.ascrate50 = 6000 / 60,
 	.ascratestops = 6000 / 60,
 	.ascratelast6m = 1000 / 60,
-	.descrate = 18000 / 60
+	.descrate = 18000 / 60,
+	.bottompo2 = 1400,
+	.decopo2 = 1600
 };
 
 int run_survey;
-- 
1.8.5.2 (Apple Git-48)

