Planner: bring sanity to the SAC rate handling

The old implementation was... let's call it creative.
This tries to actually get things right instead of using magic.
Don't pretend that double values are ints.

Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
This commit is contained in:
Dirk Hohndel 2014-08-19 11:13:55 -05:00
parent 150676ce3d
commit 6ed189f32c
5 changed files with 61 additions and 48 deletions

9
dive.c
View file

@ -188,15 +188,12 @@ double get_volume_units(unsigned int ml, int *frac, const char **units)
return vol; return vol;
} }
int units_to_sac(int volume) int units_to_sac(double volume)
{ {
if(get_units()->volume == CUFT) if(get_units()->volume == CUFT)
if (volume < 10) return rint(cuft_to_l(volume) * 1000.0);
return cuft_to_l(volume) * 100;
else
return cuft_to_l(volume) * 10;
else else
return volume * 1000; return rint(volume * 1000);
} }
unsigned int units_to_depth(double depth) unsigned int units_to_depth(double depth)

2
dive.h
View file

@ -99,7 +99,7 @@ extern double get_weight_units(unsigned int grams, int *frac, const char **units
extern double get_vertical_speed_units(unsigned int mms, int *frac, const char **units); extern double get_vertical_speed_units(unsigned int mms, int *frac, const char **units);
extern unsigned int units_to_depth(double depth); extern unsigned int units_to_depth(double depth);
extern int units_to_sac(int volume); extern int units_to_sac(double volume);
/* Volume in mliter of a cylinder at pressure 'p' */ /* Volume in mliter of a cylinder at pressure 'p' */
extern int gas_volume(cylinder_t *cyl, pressure_t p); extern int gas_volume(cylinder_t *cyl, pressure_t p);

View file

@ -350,12 +350,12 @@ void DivePlannerWidget::heightChanged(const int height)
plannerModel->setSurfacePressure(pressure); plannerModel->setSurfacePressure(pressure);
} }
void PlannerSettingsWidget::bottomSacChanged(const int bottomSac) void PlannerSettingsWidget::bottomSacChanged(const double bottomSac)
{ {
plannerModel->setBottomSac(bottomSac); plannerModel->setBottomSac(bottomSac);
} }
void PlannerSettingsWidget::decoSacChanged(const int decosac) void PlannerSettingsWidget::decoSacChanged(const double decosac)
{ {
plannerModel->setDecoSac(decosac); plannerModel->setDecoSac(decosac);
} }
@ -408,16 +408,14 @@ PlannerSettingsWidget::PlannerSettingsWidget(QWidget *parent, Qt::WindowFlags f)
connect(ui.bottompo2, SIGNAL(valueChanged(double)), this, SLOT(setBottomPo2(double))); connect(ui.bottompo2, SIGNAL(valueChanged(double)), this, SLOT(setBottomPo2(double)));
connect(ui.decopo2, SIGNAL(valueChanged(double)), this, SLOT(setDecoPo2(double))); connect(ui.decopo2, SIGNAL(valueChanged(double)), this, SLOT(setDecoPo2(double)));
connect(ui.drop_stone_mode, SIGNAL(toggled(bool)), plannerModel, SLOT(setDropStoneMode(bool))); connect(ui.drop_stone_mode, SIGNAL(toggled(bool)), plannerModel, SLOT(setDropStoneMode(bool)));
connect(ui.bottomSAC, SIGNAL(valueChanged(int)), this, SLOT(bottomSacChanged(int))); connect(ui.bottomSAC, SIGNAL(valueChanged(double)), this, SLOT(bottomSacChanged(double)));
connect(ui.decoStopSAC, SIGNAL(valueChanged(int)), this, SLOT(decoSacChanged(int))); connect(ui.decoStopSAC, SIGNAL(valueChanged(double)), this, SLOT(decoSacChanged(double)));
connect(ui.gfhigh, SIGNAL(valueChanged(int)), plannerModel, SLOT(setGFHigh(int))); connect(ui.gfhigh, SIGNAL(valueChanged(int)), plannerModel, SLOT(setGFHigh(int)));
connect(ui.gflow, SIGNAL(valueChanged(int)), plannerModel, SLOT(setGFLow(int))); connect(ui.gflow, SIGNAL(valueChanged(int)), plannerModel, SLOT(setGFLow(int)));
connect(ui.gfhigh, SIGNAL(editingFinished()), plannerModel, SLOT(triggerGFHigh())); connect(ui.gfhigh, SIGNAL(editingFinished()), plannerModel, SLOT(triggerGFHigh()));
connect(ui.gflow, SIGNAL(editingFinished()), plannerModel, SLOT(triggerGFLow())); connect(ui.gflow, SIGNAL(editingFinished()), plannerModel, SLOT(triggerGFLow()));
connect(ui.backgasBreaks, SIGNAL(toggled(bool)), this, SLOT(setBackgasBreaks(bool))); connect(ui.backgasBreaks, SIGNAL(toggled(bool)), this, SLOT(setBackgasBreaks(bool)));
settingsChanged();
ui.bottomSAC->setValue(rint(get_volume_units(prefs.bottomsac, NULL, NULL)));
ui.decoStopSAC->setValue(rint(get_volume_units(prefs.decosac, NULL, NULL)));
ui.gflow->setValue(prefs.gflow); ui.gflow->setValue(prefs.gflow);
ui.gfhigh->setValue(prefs.gfhigh); ui.gfhigh->setValue(prefs.gfhigh);
@ -455,6 +453,9 @@ PlannerSettingsWidget::~PlannerSettingsWidget()
void PlannerSettingsWidget::settingsChanged() void PlannerSettingsWidget::settingsChanged()
{ {
QString vs; QString vs;
// don't recurse into setting the value from the ui when setting the ui from the value
ui.bottomSAC->blockSignals(true);
ui.decoStopSAC->blockSignals(true);
if (get_units()->length == units::FEET) { if (get_units()->length == units::FEET) {
vs.append(tr("ft/min")); vs.append(tr("ft/min"));
ui.lastStop->setText(tr("Last stop at 20ft")); ui.lastStop->setText(tr("Last stop at 20ft"));
@ -469,18 +470,24 @@ void PlannerSettingsWidget::settingsChanged()
if(get_units()->volume == units::CUFT) { if(get_units()->volume == units::CUFT) {
ui.bottomSAC->setSuffix(tr("cuft/min")); ui.bottomSAC->setSuffix(tr("cuft/min"));
ui.decoStopSAC->setSuffix(tr("cuft/min")); ui.decoStopSAC->setSuffix(tr("cuft/min"));
ui.bottomSAC->setPrefix("."); ui.bottomSAC->setDecimals(2);
ui.decoStopSAC->setPrefix("."); ui.bottomSAC->setSingleStep(0.1);
ui.bottomSAC->setValue(rint(ml_to_cuft(prefs.bottomsac) * 100.0)); ui.decoStopSAC->setDecimals(2);
ui.decoStopSAC->setValue(rint(ml_to_cuft(prefs.decosac) * 100.0)); ui.decoStopSAC->setSingleStep(0.1);
ui.bottomSAC->setValue(ml_to_cuft(prefs.bottomsac));
ui.decoStopSAC->setValue(ml_to_cuft(prefs.decosac));
} else { } else {
ui.bottomSAC->setSuffix(tr("/min")); ui.bottomSAC->setSuffix(tr("/min"));
ui.decoStopSAC->setSuffix(tr("/min")); ui.decoStopSAC->setSuffix(tr("/min"));
ui.bottomSAC->setPrefix(""); ui.bottomSAC->setDecimals(0);
ui.decoStopSAC->setPrefix(""); ui.bottomSAC->setSingleStep(1);
ui.bottomSAC->setValue(rint((double) prefs.bottomsac / 1000.0)); ui.decoStopSAC->setDecimals(0);
ui.decoStopSAC->setValue(rint((double) prefs.decosac / 1000.0)); ui.decoStopSAC->setSingleStep(1);
ui.bottomSAC->setValue((double) prefs.bottomsac / 1000.0);
ui.decoStopSAC->setValue((double) prefs.decosac / 1000.0);
} }
ui.bottomSAC->blockSignals(false);
ui.decoStopSAC->blockSignals(false);
updateUnitsUI(); updateUnitsUI();
ui.ascRate75->setSuffix(vs); ui.ascRate75->setSuffix(vs);
ui.ascRate50->setSuffix(vs); ui.ascRate50->setSuffix(vs);
@ -703,18 +710,15 @@ void DivePlannerPointsModel::emitDataChanged()
emit dataChanged(createIndex(0, 0), createIndex(rowCount() - 1, COLUMNS - 1)); emit dataChanged(createIndex(0, 0), createIndex(rowCount() - 1, COLUMNS - 1));
} }
void DivePlannerPointsModel::setBottomSac(int sac) void DivePlannerPointsModel::setBottomSac(double sac)
{ {
volume_t newSAC; diveplan.bottomsac = units_to_sac(sac);
newSAC.mliter = units_to_sac(sac);
diveplan.bottomsac = newSAC.mliter;
prefs.bottomsac = diveplan.bottomsac; prefs.bottomsac = diveplan.bottomsac;
emit dataChanged(createIndex(0, 0), createIndex(rowCount() - 1, COLUMNS - 1)); emit dataChanged(createIndex(0, 0), createIndex(rowCount() - 1, COLUMNS - 1));
} }
void DivePlannerPointsModel::setDecoSac(int sac) void DivePlannerPointsModel::setDecoSac(double sac)
{ {
volume_t newSAC;
diveplan.decosac = units_to_sac(sac); diveplan.decosac = units_to_sac(sac);
prefs.decosac = diveplan.decosac; prefs.decosac = diveplan.decosac;
emit dataChanged(createIndex(0, 0), createIndex(rowCount() - 1, COLUMNS - 1)); emit dataChanged(createIndex(0, 0), createIndex(rowCount() - 1, COLUMNS - 1));

View file

@ -70,8 +70,8 @@ slots:
void triggerGFLow(); void triggerGFLow();
void setSurfacePressure(int pressure); void setSurfacePressure(int pressure);
int getSurfacePressure(); int getSurfacePressure();
void setBottomSac(int sac); void setBottomSac(double sac);
void setDecoSac(int sac); void setDecoSac(double sac);
void setStartTime(const QTime &t); void setStartTime(const QTime &t);
void setStartDate(const QDate &date); void setStartDate(const QDate &date);
void setLastStop6m(bool value); void setLastStop6m(bool value);
@ -160,8 +160,8 @@ public
slots: slots:
void settingsChanged(); void settingsChanged();
void atmPressureChanged(const QString &pressure); void atmPressureChanged(const QString &pressure);
void bottomSacChanged(const int bottomSac); void bottomSacChanged(const double bottomSac);
void decoSacChanged(const int decosac); void decoSacChanged(const double decosac);
void printDecoPlan(); void printDecoPlan();
void setAscRate75(int rate); void setAscRate75(int rate);
void setAscRate50(int rate); void setAscRate50(int rate);

View file

@ -30,8 +30,8 @@
<rect> <rect>
<x>0</x> <x>0</x>
<y>0</y> <y>0</y>
<width>1078</width> <width>1084</width>
<height>418</height> <height>424</height>
</rect> </rect>
</property> </property>
<layout class="QHBoxLayout" name="horizontalLayout"> <layout class="QHBoxLayout" name="horizontalLayout">
@ -367,13 +367,6 @@
</property> </property>
</widget> </widget>
</item> </item>
<item row="0" column="1">
<widget class="QSpinBox" name="bottomSAC">
<property name="suffix">
<string>/min</string>
</property>
</widget>
</item>
<item row="1" column="0"> <item row="1" column="0">
<widget class="QLabel" name="label_14"> <widget class="QLabel" name="label_14">
<property name="text"> <property name="text">
@ -381,13 +374,6 @@
</property> </property>
</widget> </widget>
</item> </item>
<item row="1" column="1">
<widget class="QSpinBox" name="decoStopSAC">
<property name="suffix">
<string>/min</string>
</property>
</widget>
</item>
<item row="2" column="0"> <item row="2" column="0">
<widget class="QLabel" name="label_6"> <widget class="QLabel" name="label_6">
<property name="text"> <property name="text">
@ -447,6 +433,32 @@
</property> </property>
</spacer> </spacer>
</item> </item>
<item row="0" column="1">
<widget class="QDoubleSpinBox" name="bottomSAC">
<property name="suffix">
<string>/min</string>
</property>
<property name="decimals">
<number>0</number>
</property>
<property name="maximum">
<double>99.000000000000000</double>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QDoubleSpinBox" name="decoStopSAC">
<property name="suffix">
<string>/min</string>
</property>
<property name="decimals">
<number>0</number>
</property>
<property name="maximum">
<double>99.000000000000000</double>
</property>
</widget>
</item>
</layout> </layout>
</widget> </widget>
</item> </item>