src/EditProfileWater.cpp

Thu, 21 Apr 2022 17:22:01 +0200

author
Michiel Broek <mbroek@mbse.eu>
date
Thu, 21 Apr 2022 17:22:01 +0200
changeset 151
b5b2483f3a3f
parent 90
2396457a8167
child 353
324a7268796b
permissions
-rw-r--r--

New recipe, calculate the boil_size. Lot's of ignoreChanges removeals and where needed QSignalBlocker is used. Bottle priming calculation added. In fermentables editor block and release to100 settings only in mash to fermentation steps, bottle and kegging are ignored. Update the IBU slider after hop changes. Set the mash name when another mash profile is selected. Don't backup initial infuse amount if there was no mash table. A small cosmetic layout change on the mash tab.

/**
 * 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 "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(1).toString());
        ui->caEdit->setValue(query.value(2).toDouble());
	ui->hcoEdit->setValue(query.value(3).toDouble());
	ui->so4Edit->setValue(query.value(4).toDouble());
	ui->clEdit->setValue(query.value(5).toDouble());
	ui->naEdit->setValue(query.value(6).toDouble());
	ui->mgEdit->setValue(query.value(7).toDouble());
	ui->phEdit->setValue(query.value(8).toDouble());
	ui->notesEdit->setPlainText(query.value(9).toString());
	ui->alkalinityEdit->setValue(query.value(10).toDouble());
    } else {
	/* Set some defaults */
	ui->phEdit->setValue(7.0);
    }
    WaterSet();
    connect(ui->nameEdit, &QLineEdit::textChanged, this, &EditProfileWater::is_changed);
    connect(ui->caEdit, &QDoubleSpinBox::textChanged, this, &EditProfileWater::water_changed);
    connect(ui->hcoEdit, &QDoubleSpinBox::textChanged, this, &EditProfileWater::hco_changed);
    connect(ui->so4Edit, &QDoubleSpinBox::textChanged, this, &EditProfileWater::water_changed);
    connect(ui->clEdit, &QDoubleSpinBox::textChanged, this, &EditProfileWater::water_changed);
    connect(ui->naEdit, &QDoubleSpinBox::textChanged, this, &EditProfileWater::water_changed);
    connect(ui->mgEdit, &QDoubleSpinBox::textChanged, this, &EditProfileWater::water_changed);
    connect(ui->phEdit, &QDoubleSpinBox::textChanged, this, &EditProfileWater::water_changed);
    connect(ui->alkalinityEdit, &QDoubleSpinBox::textChanged, this, &EditProfileWater::alkalinity_changed);
    connect(ui->notesEdit, SIGNAL(textChanged()), this, SLOT(is_changed()));

    ui->saveButton->setEnabled(false);
    ui->deleteButton->setEnabled((id >= 0) ? true:false);
}


EditProfileWater::~EditProfileWater()
{
    qDebug() << "EditProfileWater done";
    delete ui;
    emit entry_changed();
}


/*
 * Window header, mark any change with '**'
 */
void EditProfileWater::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 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 (this->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", 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'));
	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() << "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_deleteButton_clicked()
{
    QSqlQuery query;

    query.prepare("DELETE FROM profile_water WHERE record = :recno");
    query.bindValue(":recno", this->recno);
    query.exec();
    if (query.lastError().isValid()) {
	qDebug() << "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 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 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::hco_changed()
{
    ui->alkalinityEdit->setValue(ui->hcoEdit->value() * 50 / 61);
    water_changed();
}


void EditProfileWater::alkalinity_changed()
{
    ui->hcoEdit->setValue(ui->alkalinityEdit->value() * 1.22);
    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