--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/EditWater.cpp Sat Feb 26 16:34:20 2022 +0100 @@ -0,0 +1,250 @@ +/** + * EditWater.cpp is part of bmsapp. + * + * bmsapp is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * bmsapp is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ +#include "EditWater.h" +#include "../ui/ui_EditWater.h" +#include "bmsapp.h" + + +EditWater::EditWater(int id, QWidget *parent) : QDialog(parent), ui(new Ui::EditWater) +{ + QSqlQuery query; + + qDebug() << "EditWater record:" << id; + ui->setupUi(this); + this->recno = id; + + WindowTitle(); + + if (id >= 0) { + query.prepare("SELECT * FROM inventory_waters WHERE record = :recno"); + query.bindValue(":recno", id); + 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()); + } else { + /* Set some defaults */ + ui->phEdit->setValue(7.0); + } + 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->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); + + ui->saveButton->setEnabled(false); + ui->deleteButton->setEnabled((ui->inventoryEdit->value() == 0 && id >= 0) ? true:false); +} + + +EditWater::~EditWater() +{ + qDebug() << "EditWater done"; + delete ui; + emit entry_changed(); +} + + +/* + * Window header, mark any change with '**' + */ +void EditWater::WindowTitle() +{ + QString txt; + + if (this->recno < 0) { + txt = QString(tr("BMSapp - Add new brewing water")); + } else { + txt = QString(tr("BMSapp - Edit brewing water %1").arg(this->recno)); + } + + if (this->textIsChanged) { + txt.append((QString(" **"))); + } + setWindowTitle(txt); +} + + +void EditWater::on_saveButton_clicked() +{ + QSqlQuery query; + + /* If there are errors in the form, show a message and do "return;" */ + if (ui->nameEdit->text().length() < 2) { + QMessageBox::warning(this, tr("Edit Water"), tr("Name empty or too short.")); + return; + } + + if (this->textIsChanged) { + if (this->recno == -1) { + query.prepare("INSERT INTO inventory_waters SET name=:name, unlimited_stock=:unlimited, calcium=:ca, " + "bicarbonate=:hco, sulfate=:so4, chloride=:cl, sodium=:na, magnesium=:mg, ph=:ph, notes=:notes, " + "total_alkalinity=:alkalinity, inventory=:inventory, cost=:cost, uuid = :uuid"); + } else { + query.prepare("UPDATE inventory_waters SET name=:name, unlimited_stock=:unlimited, calcium=:ca, " + "bicarbonate=:hco, sulfate=:so4, chloride=:cl, sodium=:na, magnesium=:mg, ph=:ph, notes=:notes, " + "total_alkalinity=:alkalinity, inventory=:inventory, cost=:cost WHERE record = :recno"); + } + 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(":notes", ui->notesEdit->toPlainText()); + query.bindValue(":alkalinity", QString("%1").arg(ui->alkalinityEdit->value(), 2, 'f', 1, '0')); + 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')); + if (this->recno == -1) { + query.bindValue(":uuid", QUuid::createUuid().toString().mid(1, 36)); + } else { + query.bindValue(":recno", this->recno); + } + query.exec(); + if (query.lastError().isValid()) { + qDebug() << "EditWater" << query.lastError(); + QMessageBox::warning(this, tr("Database error"), + tr("MySQL error: %1\n%2\n%3") + .arg(query.lastError().nativeErrorCode()) + .arg(query.lastError().driverText()) + .arg(query.lastError().databaseText())); + } else { + qDebug() << "EditWater Saved"; + } + } + + ui->saveButton->setEnabled(false); + this->textIsChanged = false; + WindowTitle(); +} + + +void EditWater::on_deleteButton_clicked() +{ + QSqlQuery query; + + query.prepare("DELETE FROM inventory_waters WHERE record = :recno"); + query.bindValue(":recno", this->recno); + query.exec(); + if (query.lastError().isValid()) { + qDebug() << "EditWater" << query.lastError(); + QMessageBox::warning(this, tr("Database error"), + tr("MySQL error: %1\n%2\n%3") + .arg(query.lastError().nativeErrorCode()) + .arg(query.lastError().driverText()) + .arg(query.lastError().databaseText())); + } else { + qDebug() << "EditWater Deleted" << this->recno; + } + + this->close(); + this->setResult(1); +} + + +void EditWater::WaterSet() +{ + double cations, anions, balance; + + cations = (ui->caEdit->value() / 20.039) + (ui->mgEdit->value() / 12.1525) + (ui->naEdit->value() / 22.989); + anions = (ui->hcoEdit->value() / 61.016) + (ui->so4Edit->value() / 48.031) + (ui->clEdit->value() / 35.4527); + balance = round((cations - anions) * 100) / 100; + ui->balanceEdit->setValue(balance); + + if (balance <= 0.1 && balance >= -0.1) { + ui->balanceIcon->setPixmap(QPixmap(":icons/silk/tick.png")); + } else if (balance <= 0.5 && balance >= -0.5) { + ui->balanceIcon->setPixmap(QPixmap(":icons/silk/thumb_down.png")); + } else { + ui->balanceIcon->setPixmap(QPixmap(":icons/silk/error.png")); + } +} + + +void EditWater::is_changed() +{ + ui->saveButton->setEnabled(true); + ui->deleteButton->setEnabled((ui->inventoryEdit->value() == 0 && this->recno >= 0) ? true:false); + this->textIsChanged = true; + WindowTitle(); +} + + +void EditWater::water_changed() +{ + WaterSet(); + is_changed(); +} + + +void EditWater::hco_changed() +{ + ui->alkalinityEdit->setValue(ui->hcoEdit->value() * 50 / 61); + water_changed(); +} + + +void EditWater::alkalinity_changed() +{ + ui->hcoEdit->setValue(ui->alkalinityEdit->value() * 1.22); + water_changed(); +} + + +void EditWater::on_quitButton_clicked() +{ + if (this->textIsChanged) { + int rc = QMessageBox::warning(this, tr("Water changed"), tr("The water has been modified\n Save changes?"), + QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel, QMessageBox::Save); + switch (rc) { + case QMessageBox::Save: + on_saveButton_clicked(); + break; /* Saved and then Quit */ + case QMessageBox::Discard: + break; /* Quit without Save */ + case QMessageBox::Cancel: + return; /* Return to the editor page */ + } + } + + this->close(); + this->setResult(1); +}