diff -r fcf222cabb98 -r 324a7268796b src/EditWater.cpp --- a/src/EditWater.cpp Tue Jul 12 16:01:49 2022 +0200 +++ b/src/EditWater.cpp Sat Jul 16 15:06:40 2022 +0200 @@ -16,6 +16,8 @@ */ #include "EditWater.h" #include "../ui/ui_EditWater.h" +#include "global.h" +#include "Utils.h" #include "MainWindow.h" @@ -35,42 +37,52 @@ query.exec(); query.next(); - ui->nameEdit->setText(query.value(1).toString()); - ui->unlimitedEdit->setChecked(query.value(2).toInt() ? true:false); - ui->caEdit->setValue(query.value(3).toDouble()); - ui->hcoEdit->setValue(query.value(4).toDouble()); - ui->so4Edit->setValue(query.value(5).toDouble()); - ui->clEdit->setValue(query.value(6).toDouble()); - ui->naEdit->setValue(query.value(7).toDouble()); - ui->mgEdit->setValue(query.value(8).toDouble()); - ui->phEdit->setValue(query.value(9).toDouble()); - ui->notesEdit->setPlainText(query.value(10).toString()); - ui->alkalinityEdit->setValue(query.value(11).toDouble()); - ui->inventoryEdit->setValue(query.value(12).toDouble()); - ui->costEdit->setValue(query.value(13).toDouble()); - ui->noEdit->setValue(query.value(15).toDouble()); + ui->nameEdit->setText(query.value("name").toString()); + ui->unlimitedEdit->setChecked(query.value("unlimited_stock").toInt() ? true:false); + calcium = query.value("calcium").toDouble(); + ui->caEdit->setValue(query.value("calcium").toDouble()); + bicarbonate = query.value("bicarbonate").toDouble(); + ui->hcoEdit->setValue(query.value("bicarbonate").toDouble()); + sulfate = query.value("sulfate").toDouble(); + ui->so4Edit->setValue(query.value("sulfate").toDouble()); + chloride = query.value("chloride").toDouble(); + ui->clEdit->setValue(query.value("chloride").toDouble()); + sodium = query.value("sodium").toDouble(); + ui->naEdit->setValue(query.value("sodium").toDouble()); + magnesium = query.value("magnesium").toDouble(); + ui->mgEdit->setValue(query.value("magnesium").toDouble()); + ph = query.value("ph").toDouble(); + ui->phEdit->setValue(query.value("ph").toDouble()); + ui->notesEdit->setPlainText(query.value("notes").toString()); + total_alkalinity = query.value("total_alkalinity").toDouble(); + ui->alkalinityEdit->setValue(query.value("total_alkalinity").toDouble()); + ui->inventoryEdit->setValue(query.value("inventory").toDouble()); + ui->costEdit->setValue(query.value("cost").toDouble()); + nitrate = query.value("nitrate").toDouble(); + ui->noEdit->setValue(query.value("nitrate").toDouble()); } else { /* Set some defaults */ + calcium = bicarbonate = sulfate = chloride = 0; + sodium = magnesium = total_alkalinity = nitrate = 0; + ph = 7.0; ui->phEdit->setValue(7.0); } + ui->saveButton->setEnabled(false); + ui->deleteButton->setEnabled((ui->inventoryEdit->value() == 0 && id >= 0) ? true:false); WaterSet(); connect(ui->nameEdit, &QLineEdit::textChanged, this, &EditWater::is_changed); connect(ui->unlimitedEdit, &QCheckBox::stateChanged, this, &EditWater::is_changed); - connect(ui->caEdit, &QDoubleSpinBox::textChanged, this, &EditWater::water_changed); - connect(ui->hcoEdit, &QDoubleSpinBox::textChanged, this, &EditWater::hco_changed); - connect(ui->so4Edit, &QDoubleSpinBox::textChanged, this, &EditWater::water_changed); - connect(ui->clEdit, &QDoubleSpinBox::textChanged, this, &EditWater::water_changed); - connect(ui->naEdit, &QDoubleSpinBox::textChanged, this, &EditWater::water_changed); - connect(ui->mgEdit, &QDoubleSpinBox::textChanged, this, &EditWater::water_changed); - connect(ui->phEdit, &QDoubleSpinBox::textChanged, this, &EditWater::water_changed); - connect(ui->alkalinityEdit, &QDoubleSpinBox::textChanged, this, &EditWater::alkalinity_changed); + connect(ui->caEdit, QOverload::of(&QDoubleSpinBox::valueChanged), this, &EditWater::calcium_changed); + connect(ui->so4Edit, QOverload::of(&QDoubleSpinBox::valueChanged), this, &EditWater::sulfate_changed); + connect(ui->clEdit, QOverload::of(&QDoubleSpinBox::valueChanged), this, &EditWater::chloride_changed); + connect(ui->naEdit, QOverload::of(&QDoubleSpinBox::valueChanged), this, &EditWater::sodium_changed); + connect(ui->mgEdit, QOverload::of(&QDoubleSpinBox::valueChanged), this, &EditWater::magnesium_changed); + connect(ui->phEdit, QOverload::of(&QDoubleSpinBox::valueChanged), this, &EditWater::ph_changed); + connect(ui->alkalinityEdit, QOverload::of(&QDoubleSpinBox::valueChanged), this, &EditWater::total_alkalinity_changed); connect(ui->notesEdit, SIGNAL(textChanged()), this, SLOT(is_changed())); connect(ui->inventoryEdit, &QDoubleSpinBox::textChanged, this, &EditWater::is_changed); connect(ui->costEdit, &QDoubleSpinBox::textChanged, this, &EditWater::is_changed); - connect(ui->noEdit, &QDoubleSpinBox::textChanged, this, &EditWater::water_changed); - - ui->saveButton->setEnabled(false); - ui->deleteButton->setEnabled((ui->inventoryEdit->value() == 0 && id >= 0) ? true:false); + connect(ui->noEdit, QOverload::of(&QDoubleSpinBox::valueChanged), this, &EditWater::nitrate_changed); } @@ -89,10 +101,10 @@ { QString txt; - if (this->recno < 0) { + if (recno < 0) { txt = QString(tr("BMSapp - Add new brewing water")); } else { - txt = QString(tr("BMSapp - Edit brewing water %1").arg(this->recno)); + txt = QString(tr("BMSapp - Edit brewing water %1").arg(recno)); } if (this->textIsChanged) { @@ -124,22 +136,22 @@ } query.bindValue(":name", ui->nameEdit->text()); query.bindValue(":unlimited", ui->unlimitedEdit->isChecked() ? 1:0); - query.bindValue(":ca", QString("%1").arg(ui->caEdit->value(), 2, 'f', 1, '0')); - query.bindValue(":hco", QString("%1").arg(ui->hcoEdit->value(), 2, 'f', 1, '0')); - query.bindValue(":so4", QString("%1").arg(ui->so4Edit->value(), 2, 'f', 1, '0')); - query.bindValue(":cl", QString("%1").arg(ui->clEdit->value(), 2, 'f', 1, '0')); - query.bindValue(":na", QString("%1").arg(ui->naEdit->value(), 2, 'f', 1, '0')); - query.bindValue(":mg", QString("%1").arg(ui->mgEdit->value(), 2, 'f', 1, '0')); - query.bindValue(":ph", QString("%1").arg(ui->phEdit->value(), 3, 'f', 2, '0')); + query.bindValue(":ca", round(calcium * 1000) / 1000); + query.bindValue(":hco", round(bicarbonate * 1000) / 1000); + query.bindValue(":so4", round(sulfate * 1000) / 1000); + query.bindValue(":cl", round(chloride * 1000) / 1000); + query.bindValue(":na", round(sodium * 1000) / 1000); + query.bindValue(":mg", round(magnesium * 1000) / 1000); + query.bindValue(":ph", round(ph * 1000) / 1000); query.bindValue(":notes", ui->notesEdit->toPlainText()); - query.bindValue(":alkalinity", QString("%1").arg(ui->alkalinityEdit->value(), 2, 'f', 1, '0')); + query.bindValue(":alkalinity", round(total_alkalinity * 1000) / 1000); query.bindValue(":inventory", QString("%1").arg(ui->inventoryEdit->value(), 2, 'f', 1, '0')); query.bindValue(":cost", QString("%1").arg(ui->costEdit->value(), 6, 'f', 5, '0')); - query.bindValue(":no", QString("%1").arg(ui->noEdit->value(), 3, 'f', 2, '0')); - if (this->recno == -1) { + query.bindValue(":no", round(nitrate * 1000) / 1000); + if (recno == -1) { query.bindValue(":uuid", QUuid::createUuid().toString().mid(1, 36)); } else { - query.bindValue(":recno", this->recno); + query.bindValue(":recno", recno); } query.exec(); if (query.lastError().isValid()) { @@ -185,18 +197,21 @@ void EditWater::WaterSet() { - double cations, anions, balance; + double CO3 = total_alkalinity * pow(10, ph - 10.33) / (1+2* pow(10, ph - 10.33)) * MMCO3 / (MMCaCO3 / 2); + double t_bicarbonate = Utils::Bicarbonate(total_alkalinity, ph); + qDebug() << t_bicarbonate << bicarbonate; + if ((round(t_bicarbonate * 1000) / 1000) != bicarbonate) + is_changed(); + bicarbonate = t_bicarbonate; + ui->hcoEdit->setValue(bicarbonate); - double CaCO3 = ui->hcoEdit->value() / 1.22; - double pH = ui->phEdit->value(); - double CO3 = ((CaCO3 / 50) / (1+(2* pow(10, pH - 10.33))) * pow(10, pH - 10.33)) * 60; - cations = (ui->caEdit->value() / 20.039) + (ui->mgEdit->value() / 12.1525) + (ui->naEdit->value() / 22.989); - anions = (ui->hcoEdit->value() / 61.016) + (CO3 / 30) + (ui->so4Edit->value() / 48.031) + - (ui->clEdit->value() / 35.4527) + (ui->noEdit->value() / 62.0049); - balance = round((cations - anions) * 100) / 100; + double cations = (2*calcium / MMCa) + (2*magnesium / MMMg) + (sodium / MMNa); + double anions = (bicarbonate / MMHCO3) + (2*CO3 / MMCO3) + (2*sulfate / MMSO4) + (chloride / MMCl) + (nitrate / MMNO3); + double balance = round((cations - anions) * 100) / 100; ui->balanceEdit->setValue(balance); ui->co3Edit->setValue(CO3); - //qDebug() << A13 << B13 << CO3; + ui->hardnessEdit->setValue(Utils::Hardness(calcium, magnesium)); + ui->raEdit->setValue(Utils::ResidualAlkalinity(total_alkalinity, calcium, magnesium)); if (balance <= 0.1 && balance >= -0.1) { ui->balanceIcon->setPixmap(QPixmap(":icons/silk/tick.png")); @@ -224,16 +239,58 @@ } -void EditWater::hco_changed() +void EditWater::calcium_changed(double val) +{ + calcium = val; + water_changed(); +} + + +void EditWater::total_alkalinity_changed(double val) { - ui->alkalinityEdit->setValue(ui->hcoEdit->value() * 50 / 61); + total_alkalinity = val; + water_changed(); +} + + +void EditWater::sulfate_changed(double val) +{ + sulfate = val; water_changed(); } -void EditWater::alkalinity_changed() +void EditWater::chloride_changed(double val) +{ + chloride = val; + water_changed(); +} + + +void EditWater::sodium_changed(double val) +{ + sodium = val; + water_changed(); +} + + +void EditWater::magnesium_changed(double val) { - ui->hcoEdit->setValue(ui->alkalinityEdit->value() * 1.22); + magnesium = val; + water_changed(); +} + + +void EditWater::ph_changed(double val) +{ + ph = val; + water_changed(); +} + + +void EditWater::nitrate_changed(double val) +{ + nitrate = val; water_changed(); }