src/EditProfileWater.cpp

Thu, 18 Aug 2022 20:34:15 +0200

author
Michiel Broek <mbroek@mbse.eu>
date
Thu, 18 Aug 2022 20:34:15 +0200
changeset 401
583148eb6e01
parent 385
09af9f46518f
permissions
-rw-r--r--

Init est_carb field for new products.

/**
 * EditProfileWater.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 "EditProfileWater.h"
#include "../ui/ui_EditProfileWater.h"
#include "global.h"
#include "Utils.h"
#include "MainWindow.h"


EditProfileWater::EditProfileWater(int id, QWidget *parent) : QDialog(parent), ui(new Ui::EditProfileWater)
{
    QSqlQuery query;

    qDebug() << "EditProfileWater record:" << id;
    ui->setupUi(this);
    this->recno = id;

    WindowTitle();

    if (id >= 0) {
	query.prepare("SELECT * FROM profile_water WHERE record = :recno");
	query.bindValue(":recno", id);
	query.exec();
	query.next();

	ui->nameEdit->setText(query.value("name").toString());
	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());
    } else {
	/* Set some defaults */
	calcium = bicarbonate =  sulfate = chloride = 0;
	sodium = magnesium = total_alkalinity = 0;
	ph = 7.0;
	ui->phEdit->setValue(7.0);
    }
    ui->saveButton->setEnabled(false);
    ui->deleteButton->setEnabled((id >= 0) ? true:false);
    WaterSet();
    connect(ui->nameEdit, &QLineEdit::textChanged, this, &EditProfileWater::is_changed);
    connect(ui->caEdit, QOverload<double>::of(&QDoubleSpinBox::valueChanged), this, &EditProfileWater::calcium_changed);
    connect(ui->so4Edit, QOverload<double>::of(&QDoubleSpinBox::valueChanged), this, &EditProfileWater::sulfate_changed);
    connect(ui->clEdit, QOverload<double>::of(&QDoubleSpinBox::valueChanged), this, &EditProfileWater::chloride_changed);
    connect(ui->naEdit, QOverload<double>::of(&QDoubleSpinBox::valueChanged), this, &EditProfileWater::sodium_changed);
    connect(ui->mgEdit, QOverload<double>::of(&QDoubleSpinBox::valueChanged), this, &EditProfileWater::magnesium_changed);
    connect(ui->phEdit, QOverload<double>::of(&QDoubleSpinBox::valueChanged), this, &EditProfileWater::ph_changed);
    connect(ui->alkalinityEdit, QOverload<double>::of(&QDoubleSpinBox::valueChanged), this, &EditProfileWater::total_alkalinity_changed);
    connect(ui->notesEdit, SIGNAL(textChanged()), this, SLOT(is_changed()));
}


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


/*
 * Window header, mark any change with '**'
 */
void EditProfileWater::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 EditProfileWater::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 (recno == -1) {
    	    query.prepare("INSERT INTO profile_water SET name=:name, calcium=:ca, "
		"bicarbonate=:hco, sulfate=:so4, chloride=:cl, sodium=:na, magnesium=:mg, ph=:ph, notes=:notes, "
		"total_alkalinity=:alkalinity, uuid = :uuid");
    	} else {
	    query.prepare("UPDATE profile_water SET name=:name, calcium=:ca, "
		"bicarbonate=:hco, sulfate=:so4, chloride=:cl, sodium=:na, magnesium=:mg, ph=:ph, notes=:notes, "
                "total_alkalinity=:alkalinity WHERE record = :recno");
    	}
	query.bindValue(":name", ui->nameEdit->text());
	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);
	if (recno == -1) {
	    query.bindValue(":uuid", QUuid::createUuid().toString().mid(1, 36));
	} else {
	    query.bindValue(":recno", this->recno);
	}
	query.exec();
	if (query.lastError().isValid()) {
	    qWarning() << "EditProfileWater" << 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() << "EditProfileWater Saved";
	}
    }

    ui->saveButton->setEnabled(false);
    this->textIsChanged = false;
    WindowTitle();
}


void EditProfileWater::on_cloneButton_clicked()
{
    QSqlQuery query;

    query.prepare("INSERT INTO profile_water SET name=:name, calcium=:ca, "
                "bicarbonate=:hco, sulfate=:so4, chloride=:cl, sodium=:na, magnesium=:mg, ph=:ph, notes=:notes, "
                "total_alkalinity=:alkalinity, uuid = :uuid");

    query.bindValue(":name", ui->nameEdit->text() + " [copy]");
    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(":uuid", QUuid::createUuid().toString().mid(1, 36));

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


void EditProfileWater::on_deleteButton_clicked()
{
    QSqlQuery query;

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

    query.prepare("DELETE FROM profile_water WHERE record = :recno");
    query.bindValue(":recno", this->recno);
    query.exec();
    if (query.lastError().isValid()) {
	qWarning() << "EditProfileWater" << 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() << "EditProfileWater Deleted" << this->recno;
    }

    this->close();
    this->setResult(1);
}


void EditProfileWater::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);
    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 EditProfileWater::is_changed()
{
    ui->saveButton->setEnabled(true);
    ui->deleteButton->setEnabled((this->recno >= 0) ? true:false);
    this->textIsChanged = true;
    WindowTitle();
}


void EditProfileWater::water_changed()
{
    WaterSet();
    is_changed();
}


void EditProfileWater::calcium_changed(double val)
{
    calcium = val;
    water_changed();
}


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


void EditProfileWater::sulfate_changed(double val)
{
    sulfate = val;
    water_changed();
}


void EditProfileWater::chloride_changed(double val)
{
    chloride = val;
    water_changed();
}


void EditProfileWater::sodium_changed(double val)
{
    sodium = val;
    water_changed();
}


void EditProfileWater::magnesium_changed(double val)
{
    magnesium = val;
    water_changed();
}


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


void EditProfileWater::on_quitButton_clicked()
{
    if (this->textIsChanged) {
	int rc = QMessageBox::warning(this, tr("Water changed"), tr("This water profile 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