Undo: implement undo of air and water temperature editing

Mostly trivial. Since now on editing the field is re-set, the
validation function becomes unnecessary.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2019-01-30 22:13:24 +01:00 committed by Dirk Hohndel
parent a12adf8e2a
commit de579c1a1a
6 changed files with 90 additions and 47 deletions

View file

@ -155,4 +155,14 @@ void editVisibility(const QVector<dive *> dives, int newValue, int oldValue)
execute(new EditVisibility(dives, newValue, oldValue)); execute(new EditVisibility(dives, newValue, oldValue));
} }
void editAirTemp(const QVector<dive *> dives, int newValue, int oldValue)
{
execute(new EditAirTemp(dives, newValue, oldValue));
}
void editWaterTemp(const QVector<dive *> dives, int newValue, int oldValue)
{
execute(new EditWaterTemp(dives, newValue, oldValue));
}
} // namespace Command } // namespace Command

View file

@ -57,6 +57,8 @@ void editSuit(const QVector<dive *> dives, const QString &newValue, const QStrin
void editMode(const QVector<dive *> dives, int index, int newValue, int oldValue); void editMode(const QVector<dive *> dives, int index, int newValue, int oldValue);
void editRating(const QVector<dive *> dives, int newValue, int oldValue); void editRating(const QVector<dive *> dives, int newValue, int oldValue);
void editVisibility(const QVector<dive *> dives, int newValue, int oldValue); void editVisibility(const QVector<dive *> dives, int newValue, int oldValue);
void editAirTemp(const QVector<dive *> dives, int newValue, int oldValue);
void editWaterTemp(const QVector<dive *> dives, int newValue, int oldValue);
} // namespace Command } // namespace Command

View file

@ -145,7 +145,7 @@ DiveField EditRating::fieldId() const
return DiveField::RATING; return DiveField::RATING;
} }
// ***** Visibility **** // ***** Visibility *****
void EditVisibility::set(struct dive *d, int value) const void EditVisibility::set(struct dive *d, int value) const
{ {
d->visibility = value; d->visibility = value;
@ -166,6 +166,48 @@ DiveField EditVisibility::fieldId() const
return DiveField::VISIBILITY; return DiveField::VISIBILITY;
} }
// ***** Air Temperature *****
void EditAirTemp::set(struct dive *d, int value) const
{
d->airtemp.mkelvin = value > 0 ? (uint32_t)value : 0u;
}
int EditAirTemp::data(struct dive *d) const
{
return (int)d->airtemp.mkelvin;
}
QString EditAirTemp::fieldName() const
{
return tr("air temperature");
}
DiveField EditAirTemp::fieldId() const
{
return DiveField::AIR_TEMP;
}
// ***** Water Temperature *****
void EditWaterTemp::set(struct dive *d, int value) const
{
d->watertemp.mkelvin = value > 0 ? (uint32_t)value : 0u;
}
int EditWaterTemp::data(struct dive *d) const
{
return (int)d->watertemp.mkelvin;
}
QString EditWaterTemp::fieldName() const
{
return tr("water temperature");
}
DiveField EditWaterTemp::fieldId() const
{
return DiveField::WATER_TEMP;
}
// ***** Mode ***** // ***** Mode *****
// Editing the dive mode has very peculiar semantics for historic reasons: // Editing the dive mode has very peculiar semantics for historic reasons:
// Since the dive-mode depends on the dive computer, the i-th dive computer // Since the dive-mode depends on the dive computer, the i-th dive computer

View file

@ -83,6 +83,24 @@ public:
DiveField fieldId() const override; DiveField fieldId() const override;
}; };
class EditAirTemp : 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 EditWaterTemp : 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 EditMode : public EditBase<int> { class EditMode : public EditBase<int> {
int index; int index;
public: public:

View file

@ -342,6 +342,12 @@ void MainTab::divesEdited(const QVector<dive *> &, DiveField field)
return; return;
switch(field) { switch(field) {
case DiveField::AIR_TEMP:
ui.airtemp->setText(get_temperature_string(current_dive->airtemp, true));
break;
case DiveField::WATER_TEMP:
ui.watertemp->setText(get_temperature_string(current_dive->watertemp, true));
break;
case DiveField::RATING: case DiveField::RATING:
ui.rating->setCurrentStars(current_dive->rating); ui.rating->setCurrentStars(current_dive->rating);
break; break;
@ -804,11 +810,6 @@ void MainTab::acceptChanges()
struct dive *cd = current_dive; struct dive *cd = current_dive;
// now check if something has changed and if yes, edit the selected dives that // now check if something has changed and if yes, edit the selected dives that
// were identical with the master dive shown (and mark the divelist as changed) // were identical with the master dive shown (and mark the divelist as changed)
if (displayed_dive.airtemp.mkelvin != cd->airtemp.mkelvin)
MODIFY_DIVES(selectedDives, EDIT_VALUE(airtemp.mkelvin));
if (displayed_dive.watertemp.mkelvin != cd->watertemp.mkelvin)
MODIFY_DIVES(selectedDives, EDIT_VALUE(watertemp.mkelvin));
if (displayed_dive.dive_site != cd->dive_site) if (displayed_dive.dive_site != cd->dive_site)
MODIFY_DIVES(selectedDives, EDIT_VALUE(dive_site)); MODIFY_DIVES(selectedDives, EDIT_VALUE(dive_site));
@ -1091,13 +1092,12 @@ void MainTab::on_depth_textChanged(const QString &text)
MainWindow::instance()->graphics->plotDive(); MainWindow::instance()->graphics->plotDive();
} }
void MainTab::on_airtemp_textChanged(const QString &text) void MainTab::on_airtemp_editingFinished()
{ {
if (editMode == IGNORE || acceptingEdit == true) if (editMode == IGNORE || acceptingEdit == true || !current_dive)
return; return;
displayed_dive.airtemp.mkelvin = parseTemperatureToMkelvin(text); Command::editAirTemp(getSelectedDivesCurrentLast(),
markChangedWidget(ui.airtemp); parseTemperatureToMkelvin(ui.airtemp->text()), current_dive->airtemp.mkelvin);
validate_temp_field(ui.airtemp, text);
} }
void MainTab::divetype_Changed(int index) void MainTab::divetype_Changed(int index)
@ -1108,41 +1108,13 @@ void MainTab::divetype_Changed(int index)
get_dive_dc(current_dive, dc_number)->divemode); get_dive_dc(current_dive, dc_number)->divemode);
} }
void MainTab::on_watertemp_textChanged(const QString &text) void MainTab::on_watertemp_editingFinished()
{ {
if (editMode == IGNORE || acceptingEdit == true) if (editMode == IGNORE || acceptingEdit == true || !current_dive)
return; return;
displayed_dive.watertemp.mkelvin = parseTemperatureToMkelvin(text); Command::editWaterTemp(getSelectedDivesCurrentLast(),
markChangedWidget(ui.watertemp); parseTemperatureToMkelvin(ui.watertemp->text()),
validate_temp_field(ui.watertemp, text); current_dive->watertemp.mkelvin);
}
void MainTab::validate_temp_field(QLineEdit *tempField, const QString &text)
{
static bool missing_unit = false;
static bool missing_precision = false;
if (!text.contains(QRegExp("^[-+]{0,1}[0-9]+([,.][0-9]+){0,1}(°[CF]){0,1}$")) &&
!text.isEmpty() &&
!text.contains(QRegExp("^[-+]$"))) {
if (text.contains(QRegExp("^[-+]{0,1}[0-9]+([,.][0-9]+){0,1}(°)$")) && !missing_unit) {
if (!missing_unit) {
missing_unit = true;
return;
}
}
if (text.contains(QRegExp("^[-+]{0,1}[0-9]+([,.]){0,1}(°[CF]){0,1}$")) && !missing_precision) {
if (!missing_precision) {
missing_precision = true;
return;
}
}
QPalette p;
p.setBrush(QPalette::Base, QColor(Qt::red).lighter());
tempField->setPalette(p);
} else {
missing_unit = false;
missing_precision = false;
}
} }
void MainTab::on_dateEdit_dateChanged(const QDate &date) void MainTab::on_dateEdit_dateChanged(const QDate &date)

View file

@ -80,12 +80,11 @@ slots:
void on_diveTripLocation_textEdited(const QString& text); void on_diveTripLocation_textEdited(const QString& text);
void on_notes_textChanged(); void on_notes_textChanged();
void on_notes_editingFinished(); void on_notes_editingFinished();
void on_airtemp_textChanged(const QString &text); void on_airtemp_editingFinished();
void on_duration_textChanged(const QString &text); void on_duration_textChanged(const QString &text);
void on_depth_textChanged(const QString &text); void on_depth_textChanged(const QString &text);
void divetype_Changed(int); void divetype_Changed(int);
void on_watertemp_textChanged(const QString &text); void on_watertemp_editingFinished();
void validate_temp_field(QLineEdit *tempField, const QString &text);
void on_dateEdit_dateChanged(const QDate &date); void on_dateEdit_dateChanged(const QDate &date);
void on_timeEdit_timeChanged(const QTime & time); void on_timeEdit_timeChanged(const QTime & time);
void on_rating_valueChanged(int value); void on_rating_valueChanged(int value);