src/EditWater.cpp

Sun, 12 Feb 2023 13:58:36 +0100

author
Michiel Broek <mbroek@mbse.eu>
date
Sun, 12 Feb 2023 13:58:36 +0100
changeset 494
49ac23d25f61
parent 385
09af9f46518f
permissions
-rw-r--r--

In monitor iSpindel: in the chart calculate the ranges, do't let the toolkit do that. Save the path for chart image download in the user settings. In the tooltip for the battery voltage line, also show the remaining battery capacity. In the monitor window show the battery capacity digit instead of allways 0. Updated the translations.

/**
 * 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 "global.h"
#include "Utils.h"
#include "MainWindow.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("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, QOverload<double>::of(&QDoubleSpinBox::valueChanged), this, &EditWater::calcium_changed);
    connect(ui->so4Edit, QOverload<double>::of(&QDoubleSpinBox::valueChanged), this, &EditWater::sulfate_changed);
    connect(ui->clEdit, QOverload<double>::of(&QDoubleSpinBox::valueChanged), this, &EditWater::chloride_changed);
    connect(ui->naEdit, QOverload<double>::of(&QDoubleSpinBox::valueChanged), this, &EditWater::sodium_changed);
    connect(ui->mgEdit, QOverload<double>::of(&QDoubleSpinBox::valueChanged), this, &EditWater::magnesium_changed);
    connect(ui->phEdit, QOverload<double>::of(&QDoubleSpinBox::valueChanged), this, &EditWater::ph_changed);
    connect(ui->alkalinityEdit, QOverload<double>::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, QOverload<double>::of(&QDoubleSpinBox::valueChanged), this, &EditWater::nitrate_changed);
}


EditWater::~EditWater()
{
    delete ui;
    emit entry_changed();
}


/*
 * Window header, mark any change with '**'
 */
void EditWater::WindowTitle()
{
    QString txt;

    if (recno < 0) {
	txt = QString(tr("BMSapp - Add new brewing water"));
    } else {
	txt = QString(tr("BMSapp - Edit brewing water %1").arg(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, nitrate=:no, 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, nitrate=:no WHERE record = :recno");
    	}
	query.bindValue(":name", ui->nameEdit->text());
	query.bindValue(":unlimited", ui->unlimitedEdit->isChecked() ? 1: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", 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", round(nitrate * 1000) / 1000);
	if (recno == -1) {
	    query.bindValue(":uuid", QUuid::createUuid().toString().mid(1, 36));
	} else {
	    query.bindValue(":recno", recno);
	}
	query.exec();
	if (query.lastError().isValid()) {
	    qWarning() << "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_cloneButton_clicked()
{
    QSqlQuery query;

    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, nitrate=:no, uuid = :uuid");

    query.bindValue(":name", ui->nameEdit->text() + " [copy]");
    query.bindValue(":unlimited", ui->unlimitedEdit->isChecked() ? 1: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", round(total_alkalinity * 1000) / 1000);
    query.bindValue(":inventory", QString("%1").arg(0, 2, 'f', 1, '0'));
    query.bindValue(":cost", QString("%1").arg(ui->costEdit->value(), 6, 'f', 5, '0'));
    query.bindValue(":no", round(nitrate * 1000) / 1000);
    query.bindValue(":uuid", QUuid::createUuid().toString().mid(1, 36));

    query.exec();
    if (query.lastError().isValid()) {
	qWarning() << "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";
    }
}


void EditWater::on_deleteButton_clicked()
{
    QSqlQuery query;

    int rc = QMessageBox::warning(this, tr("Delete water"), tr("Delete %1").arg(ui->nameEdit->text()),
                    QMessageBox::Yes | QMessageBox::No, QMessageBox::No);
    if (rc == QMessageBox::No)
        return;

    query.prepare("DELETE FROM inventory_waters WHERE record = :recno");
    query.bindValue(":recno", this->recno);
    query.exec();
    if (query.lastError().isValid()) {
	qWarning() << "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 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 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);
    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"));
    } 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::calcium_changed(double val)
{
    calcium = val;
    water_changed();
}


void EditWater::total_alkalinity_changed(double val)
{
    total_alkalinity = val;
    water_changed();
}


void EditWater::sulfate_changed(double val)
{
    sulfate = val;
    water_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)
{
    magnesium = val;
    water_changed();
}


void EditWater::ph_changed(double val)
{
    ph = val;
    water_changed();
}


void EditWater::nitrate_changed(double val)
{
    nitrate = val;
    water_changed();
}


void EditWater::on_quitButton_clicked()
{
    if (this->textIsChanged) {
	int rc = QMessageBox::warning(this, tr("Water changed"), tr("The brewing water has been modified. 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);
}

mercurial