Convert the atmospheric pressure in the Information Tab to an editable field

The Information tab shows the atmospheric pressure. Make this value editable
and also ensure that changes to it are undo-able.

Signed-off-by: willemferguson <willemferguson@zoology.up.ac.za>
This commit is contained in:
willemferguson 2019-04-30 12:42:33 +02:00 committed by Dirk Hohndel
parent ca6aa38139
commit 1bdf00b2b4
19 changed files with 323 additions and 20 deletions

View file

@ -171,6 +171,11 @@ void editWaterTemp(int newValue, bool currentDiveOnly)
execute(new EditWaterTemp(newValue, currentDiveOnly));
}
void editAtmPress(int newValue, bool currentDiveOnly)
{
execute(new EditAtmPress(newValue, currentDiveOnly));
}
void editDepth(int newValue, bool currentDiveOnly)
{
execute(new EditDepth(newValue, currentDiveOnly));

View file

@ -61,6 +61,7 @@ void editRating(int newValue, bool currentDiveOnly);
void editVisibility(int newValue, bool currentDiveOnly);
void editAirTemp(int newValue, bool currentDiveOnly);
void editWaterTemp(int newValue, bool currentDiveOnly);
void editAtmPress(int newValue, bool currentDiveOnly);
void editDepth(int newValue, bool currentDiveOnly);
void editDuration(int newValue, bool currentDiveOnly);
void editDiveSite(struct dive_site *newValue, bool currentDiveOnly);

View file

@ -248,6 +248,27 @@ DiveField EditWaterTemp::fieldId() const
return DiveField::WATER_TEMP;
}
// ***** Atmospheric pressure *****
void EditAtmPress::set(struct dive *d, int value) const
{
d->surface_pressure.mbar = value > 0 ? (uint32_t)value : 0u;
}
int EditAtmPress::data(struct dive *d) const
{
return (int)d->surface_pressure.mbar;
}
QString EditAtmPress::fieldName() const
{
return tr("Atm. pressure");
}
DiveField EditAtmPress::fieldId() const
{
return DiveField::ATM_PRESS;
}
// ***** Duration *****
void EditDuration::set(struct dive *d, int value) const
{

View file

@ -102,6 +102,15 @@ public:
DiveField fieldId() const override;
};
class EditAtmPress : public EditBase<int> {
public:
using EditBase<int>::EditBase; // Use constructor of base class.
void set(struct dive *d, int value) const override;
int data(struct dive *d) const override;
QString fieldName() const override;
DiveField fieldId() const override;
};
class EditDuration : public EditBase<int> {
public:
using EditBase<int>::EditBase; // Use constructor of base class.

View file

@ -2,15 +2,24 @@
#include "TabDiveInformation.h"
#include "ui_TabDiveInformation.h"
#include "../tagwidget.h"
#include "core/units.h"
#include "core/dive.h"
#include "desktop-widgets/command.h"
#include <core/qthelper.h>
#include <core/statistics.h>
#include <core/display.h>
#define COMBO_CHANGED 0
#define TEXT_EDITED 1
TabDiveInformation::TabDiveInformation(QWidget *parent) : TabBase(parent), ui(new Ui::TabDiveInformation())
{
ui->setupUi(this);
connect(&diveListNotifier, &DiveListNotifier::divesChanged, this, &TabDiveInformation::divesChanged);
QStringList atmPressTypes { "mbar", get_depth_unit() ,"use dc"};
ui->atmPressType->insertItems(0, atmPressTypes);
pressTypeIndex = 0;
}
TabDiveInformation::~TabDiveInformation()
@ -32,7 +41,7 @@ void TabDiveInformation::clear()
ui->averageDepthText->clear();
ui->waterTemperatureText->clear();
ui->airTemperatureText->clear();
ui->airPressureText->clear();
ui->atmPressVal->clear();
ui->salinityText->clear();
}
@ -74,6 +83,17 @@ void TabDiveInformation::updateProfile()
" ", current_dive->dc.divemode == FREEDIVE));
ui->sacText->setText( mean[0] ? SACs : QString());
if (current_dive->surface_pressure.mbar == 0) {
ui->atmPressVal->clear(); // If no atm pressure for dive then clear text box
}
else {
ui->atmPressVal->setEnabled(true);
QString pressStr;
pressStr.sprintf("%d",current_dive->surface_pressure.mbar);
ui->atmPressVal->setText(pressStr); // else display atm pressure
}
}
// Update fields that depend on start of dive
@ -99,15 +119,15 @@ void TabDiveInformation::updateData()
ui->waterTemperatureText->setText(get_temperature_string(current_dive->watertemp, true));
ui->airTemperatureText->setText(get_temperature_string(current_dive->airtemp, true));
if (current_dive->surface_pressure.mbar) /* this is ALWAYS displayed in mbar */
ui->airPressureText->setText(QString("%1mbar").arg(current_dive->surface_pressure.mbar));
else
ui->airPressureText->clear();
if (current_dive->salinity)
ui->salinityText->setText(QString("%1g/").arg(current_dive->salinity / 10.0));
else
ui->salinityText->clear();
ui->atmPressType->setEditable(true);
ui->atmPressType->setItemText(1, get_depth_unit()); // Check for changes in depth unit (imperial/metric)
ui->atmPressType->setEditable(false);
ui->atmPressType->setCurrentIndex(0); // Set the atmospheric pressure combo box to mbar
}
// This function gets called if a field gets updated by an undo command.
@ -130,6 +150,9 @@ void TabDiveInformation::divesChanged(dive_trip *trip, const QVector<dive *> &di
case DiveField::WATER_TEMP:
ui->waterTemperatureText->setText(get_temperature_string(current_dive->watertemp, true));
break;
case DiveField::ATM_PRESS:
ui->atmPressVal->setText(ui->atmPressVal->text().sprintf("%d",current_dive->surface_pressure.mbar));
break;
case DiveField::DATETIME:
updateWhen();
break;
@ -137,3 +160,47 @@ void TabDiveInformation::divesChanged(dive_trip *trip, const QVector<dive *> &di
break;
}
}
void TabDiveInformation::on_atmPressType_currentIndexChanged(int index) { updateTextBox(COMBO_CHANGED); }
void TabDiveInformation::on_atmPressVal_editingFinished() { updateTextBox(TEXT_EDITED); }
void TabDiveInformation::updateTextBox(int event) // Either the text box has been edited or the pressure type has changed.
{ // Either way this gets a numeric value and puts it on the text box atmPressVal,
pressure_t atmpress = { 0 }; // then stores it in dive->surface_pressure.The undo stack for the text box content is
double altitudeVal; // maintained even though two independent events trigger saving the text box contents.
if (current_dive) {
switch (ui->atmPressType->currentIndex()) {
case 0: // If an atm pressure has been specified in mbar:
if (event == TEXT_EDITED) // this is only triggered by on_atmPressVal_editingFinished()
atmpress.mbar = ui->atmPressVal->text().toInt(); // use the specified mbar pressure
break;
case 1: // If an altitude has been specified:
if (event == TEXT_EDITED) { // this is only triggered by on_atmPressVal_editingFinished()
altitudeVal = (ui->atmPressVal->text().toFloat()); // get altitude from text box
if (prefs.units.length == units::FEET) // if altitude in feet
altitudeVal = feet_to_mm(altitudeVal); // imperial: convert altitude from feet to mm
else
altitudeVal = altitudeVal * 1000; // metric: convert altitude from meters to mm
atmpress.mbar = altitude_to_pressure((int32_t) altitudeVal); // convert altitude (mm) to pressure (mbar)
ui->atmPressVal->setText(ui->atmPressVal->text().sprintf("%d",atmpress.mbar));
ui->atmPressType->setCurrentIndex(0); // reset combobox to mbar
} else { // i.e. event == COMBO_CHANGED, that is, "m" or "ft" was selected from combobox
ui->atmPressVal->clear(); // Clear the text box so that altitude can be typed
}
break;
case 2: // i.e. event = COMBO_CHANGED, that is, the option "Use dc" was selected from combobox
atmpress = calculate_surface_pressure(current_dive); // re-calculate air pressure from dc data
ui->atmPressVal->setText(QString::number(atmpress.mbar)); // display it in text box
ui->atmPressType->setCurrentIndex(0); // reset combobox to mbar
break;
default:
atmpress.mbar = 1013; // This line should never execute
break;
}
if (atmpress.mbar)
Command::editAtmPress(atmpress.mbar, false); // and save the pressure for undo
}
}

View file

@ -18,10 +18,14 @@ public:
void clear() override;
private slots:
void divesChanged(dive_trip *trip, const QVector<dive *> &dives, DiveField field);
void on_atmPressVal_editingFinished();
void on_atmPressType_currentIndexChanged(int index);
private:
Ui::TabDiveInformation *ui;
void updateProfile();
void updateWhen();
int pressTypeIndex;
void updateTextBox(int event);
};
#endif

View file

@ -224,25 +224,37 @@
</layout>
</widget>
</item>
<item row="2" column="2">
<item row="2" column="2" colspan="1">
<widget class="QGroupBox" name="groupBox_10">
<property name="title">
<string>Air pressure</string>
<string>Atm. pressure</string>
</property>
<property name="alignment">
<set>Qt::AlignHCenter</set>
</property>
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Maximum">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<layout class="QHBoxLayout" name="diveInfoAirPressureLayout">
<item>
<widget class="QLabel" name="airPressureText">
<property name="text">
<string/>
</property>
<property name="alignment">
<set>Qt::AlignCenter</set>
<widget class="QLineEdit" name="atmPressVal">
<property name="readOnly">
<bool>false</bool>
</property>
</widget>
</item>
<item>
<widget class="QComboBox" name="atmPressType">
</widget>
</item>
</layout>
</widget>
</item>
<item row="3" column="2">
<widget class="QGroupBox" name="groupBox_9">
<property name="title">