src/EditHop.cpp

Sat, 30 Jul 2022 16:30:02 +0200

author
Michiel Broek <mbroek@mbse.eu>
date
Sat, 30 Jul 2022 16:30:02 +0200
changeset 380
8f5c03ed4321
parent 377
5c1f81c75bc4
child 385
09af9f46518f
permissions
-rw-r--r--

Global setup added hop utilisation defaults, these are used by the hops editor. In the hops editor, changing the hop form sets the new default utilisation. Hide and show the time edit entry instead of setting it read-only. The new defaults are loaded at program startup. Changed the global setup for the new utilisation fields and dropped the obsolete factor fields. The toIBU formula doesn't use my_factor_* variables anymore, hop differences now come from the utilisation field per hop. The global database is updated to the new defaults.

/**
 * EditHop.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 "EditHop.h"
#include "../ui/ui_EditHop.h"
#include "MainWindow.h"
#include "global.h"


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

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

    for (int i = 0; i < 3; i++)
	ui->typeEdit->addItem(QCoreApplication::translate("HopTypes", g_hop_types[i]));
    for (int i = 0; i < 7; i++)
	ui->formEdit->addItem(QCoreApplication::translate("HopForm", g_hop_forms[i]));

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

	ui->nameEdit->setText(query.value("name").toString());
	ui->alphaEdit->setValue(query.value("alpha").toDouble());
	ui->betaEdit->setValue(query.value("beta").toDouble());
	ui->humuleneEdit->setValue(query.value("humulene").toDouble());
	ui->caryEdit->setValue(query.value("caryophyllene").toDouble());
	ui->cohumuloneEdit->setValue(query.value("cohumulone").toDouble());
	ui->myrceneEdit->setValue(query.value("myrcene").toDouble());
	ui->hsiEdit->setValue(query.value("hsi").toDouble());
	ui->typeEdit->setCurrentIndex(query.value("type").toInt());
	ui->formEdit->setCurrentIndex(query.value("form").toInt());
	ui->notesEdit->setPlainText(query.value("notes").toString());
	ui->originEdit->setText(query.value("origin").toString());
	ui->substitutesEdit->setText(query.value("substitutes").toString());
	ui->alwaysEdit->setChecked(query.value("always_on_stock").toInt() ? true:false);
	ui->inventoryEdit->setValue(query.value("inventory").toDouble());
	ui->costEdit->setValue(query.value("cost").toDouble());
	ui->valueEdit->setValue(query.value("inventory").toDouble() * query.value("cost").toDouble());
	if (query.value("production_date").toString().length() == 10) {
            ui->prodEdit->setDate(query.value("production_date").toDate());
        } else {
            ui->prodEdit->clear();
        }
	if (query.value("tht_date").toString().length() == 10) {
	    ui->thtEdit->setDate(query.value("tht_date").toDate());
	} else {
	    ui->thtEdit->clear();
	}
	ui->oilEdit->setValue(query.value("total_oil").toDouble());
	ui->utilisationEdit->setValue(query.value("utilisation").toDouble());
	ui->bufactorEdit->setValue(query.value("bu_factor").toDouble());
    } else {
	/* Set some defaults */
	ui->typeEdit->setCurrentIndex(HOP_TYPE_BITTERING);
	ui->formEdit->setCurrentIndex(HOP_FORMS_PELLET);
	ui->utilisationEdit->setValue(my_ut_pellet);
	ui->bufactorEdit->setValue(1.0);
	ui->prodEdit->clear();
	ui->thtEdit->clear();
    }
    connect(ui->nameEdit, &QLineEdit::textChanged, this, &EditHop::is_changed);
    connect(ui->alphaEdit, &QDoubleSpinBox::textChanged, this, &EditHop::is_changed);
    connect(ui->betaEdit, &QDoubleSpinBox::textChanged, this, &EditHop::is_changed);
    connect(ui->humuleneEdit, &QDoubleSpinBox::textChanged, this, &EditHop::is_changed);
    connect(ui->caryEdit, &QDoubleSpinBox::textChanged, this, &EditHop::is_changed);
    connect(ui->cohumuloneEdit, &QDoubleSpinBox::textChanged, this, &EditHop::is_changed);
    connect(ui->myrceneEdit, &QDoubleSpinBox::textChanged, this, &EditHop::is_changed);
    connect(ui->hsiEdit, &QDoubleSpinBox::textChanged, this, &EditHop::is_changed);
    connect(ui->typeEdit, &QComboBox::currentTextChanged, this, &EditHop::is_changed);
    connect(ui->formEdit, QOverload<int>::of(&QComboBox::currentIndexChanged), this, &EditHop::form_changed);
    connect(ui->notesEdit, SIGNAL(textChanged()), this, SLOT(is_changed()));
    connect(ui->originEdit, &QLineEdit::textChanged, this, &EditHop::is_changed);
    connect(ui->substitutesEdit, &QLineEdit::textChanged, this, &EditHop::is_changed);
    connect(ui->alwaysEdit, &QCheckBox::stateChanged, this, &EditHop::is_changed);
    connect(ui->inventoryEdit, &QDoubleSpinBox::textChanged, this, &EditHop::is_changed);
    connect(ui->costEdit, &QDoubleSpinBox::textChanged, this, &EditHop::is_changed);
    connect(ui->prodEdit, &QDateEdit::dateChanged, this, &EditHop::is_changed);
    connect(ui->prodButton1, SIGNAL(clicked()), this, SLOT(prod_date_today()));
    connect(ui->prodButton2, SIGNAL(clicked()), this, SLOT(prod_date_clear()));
    connect(ui->thtEdit, &QDateEdit::dateChanged, this, &EditHop::is_changed);
    connect(ui->thtButton1, SIGNAL(clicked()), this, SLOT(tht_date_today()));
    connect(ui->thtButton2, SIGNAL(clicked()), this, SLOT(tht_date_clear()));
    connect(ui->oilEdit, &QDoubleSpinBox::textChanged, this, &EditHop::is_changed);
    connect(ui->utilisationEdit, &QDoubleSpinBox::textChanged, this, &EditHop::is_changed);
    connect(ui->bufactorEdit, &QDoubleSpinBox::textChanged, this, &EditHop::is_changed);

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

    WindowTitle();
}


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


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

    if (this->recno < 0) {
	txt = QString(tr("BMSapp - Add new hop"));
    } else {
	txt = QString(tr("BMSapp - Edit hop %1").arg(this->recno));
    }

    if (this->textIsChanged) {
	txt.append((QString(" **")));
    }
    setWindowTitle(txt);
}


void EditHop::on_saveButton_clicked()
{
    QSqlQuery query;
    QString sql = "";

    /* If there are errors in the form, show a message and do "return;" */
    if (ui->nameEdit->text().length() < 2) {
	QMessageBox::warning(this, tr("Edit Hop"), tr("Name empty or too short."));
	return;
    }
    if (ui->originEdit->text().length() < 2) {
        QMessageBox::warning(this, tr("Edit Hop"), tr("Origin empty or too short."));
        return;
    }

    if (this->textIsChanged) {
    	if (this->recno == -1) {
	    sql = "INSERT INTO inventory_hops SET ";
	} else {
	    sql = "UPDATE inventory_hops SET ";
	}
	sql.append("name=:name, alpha=:alpha, beta=:beta, "
		"humulene=:humulene, caryophyllene=:cary, cohumulone=:cohumulone, myrcene=:myrcene, "
		"hsi=:hsi, type=:type, form=:form, notes=:notes, origin=:origin, substitutes=:substitutes, "
		"always_on_stock=:always, inventory=:inventory, cost=:cost, production_date=:prod, "
		"tht_date=:tht, total_oil=:oil, utilisation=:utilisation, bu_factor=:bu_factor");
	if (this->recno == -1) {
	    sql.append(", uuid=:uuid");
	} else {
	    sql.append(" WHERE record = :recno");
    	}

	query.prepare(sql);
	query.bindValue(":name", ui->nameEdit->text());
	query.bindValue(":alpha", QString("%1").arg(ui->alphaEdit->value(), 2, 'f', 1, '0'));
	query.bindValue(":beta", QString("%1").arg(ui->betaEdit->value(), 2, 'f', 1, '0'));
	query.bindValue(":humulene", QString("%1").arg(ui->humuleneEdit->value(), 2, 'f', 1, '0'));
	query.bindValue(":cary", QString("%1").arg(ui->caryEdit->value(), 2, 'f', 1, '0'));
	query.bindValue(":cohumulone", QString("%1").arg(ui->cohumuloneEdit->value(), 2, 'f', 1, '0'));
	query.bindValue(":myrcene", QString("%1").arg(ui->myrceneEdit->value(), 2, 'f', 1, '0'));
	query.bindValue(":hsi", QString("%1").arg(ui->hsiEdit->value(), 2, 'f', 1, '0'));
	query.bindValue(":type", ui->typeEdit->currentIndex());
	query.bindValue(":form", ui->formEdit->currentIndex());
	query.bindValue(":notes", ui->notesEdit->toPlainText());
	query.bindValue(":origin", ui->originEdit->text());
	query.bindValue(":substitutes", ui->substitutesEdit->text());
	query.bindValue(":always", ui->alwaysEdit->isChecked() ? 1:0);
	query.bindValue(":inventory", QString("%1").arg(ui->inventoryEdit->value(), 5, 'f', 4, '0'));
	query.bindValue(":cost", QString("%1").arg(ui->costEdit->value(), 3, 'f', 2, '0'));
	/* Uses https://www.qtcentre.org/threads/17295-How-to-put-empty-value-in-QDateEdit */
	query.bindValue(":prod", ui->prodEdit->nullDate());
	query.bindValue(":tht", ui->thtEdit->nullDate());
	query.bindValue(":oil", QString("%1").arg(ui->oilEdit->value(), 2, 'f', 1, '0'));
	query.bindValue(":utilisation", QString("%1").arg(ui->utilisationEdit->value(), 2, 'f', 1, '0'));
	query.bindValue(":bu_factor", QString("%1").arg(ui->bufactorEdit->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() << "EditHop" << 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() << "EditHop Saved";
	}
    }

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


void EditHop::on_cloneButton_clicked()
{
    QSqlQuery query;

    QString sql = "INSERT INTO inventory_hops SET name=:name, alpha=:alpha, beta=:beta, "
                "humulene=:humulene, caryophyllene=:cary, cohumulone=:cohumulone, myrcene=:myrcene, "
                "hsi=:hsi, type=:type, form=:form, notes=:notes, origin=:origin, substitutes=:substitutes, "
                "always_on_stock=:always, inventory=:inventory, cost=:cost, production_date=:prod, "
                "tht_date=:tht, total_oil=:oil, utilisation=:utilisation, bu_factor=:bu_factor, uuid=:uuid";

    query.prepare(sql);
    query.bindValue(":name", ui->nameEdit->text() + " [copy]");
    query.bindValue(":alpha", QString("%1").arg(ui->alphaEdit->value(), 2, 'f', 1, '0'));
    query.bindValue(":beta", QString("%1").arg(ui->betaEdit->value(), 2, 'f', 1, '0'));
    query.bindValue(":humulene", QString("%1").arg(ui->humuleneEdit->value(), 2, 'f', 1, '0'));
    query.bindValue(":cary", QString("%1").arg(ui->caryEdit->value(), 2, 'f', 1, '0'));
    query.bindValue(":cohumulone", QString("%1").arg(ui->cohumuloneEdit->value(), 2, 'f', 1, '0'));
    query.bindValue(":myrcene", QString("%1").arg(ui->myrceneEdit->value(), 2, 'f', 1, '0'));
    query.bindValue(":hsi", QString("%1").arg(ui->hsiEdit->value(), 2, 'f', 1, '0'));
    query.bindValue(":type", ui->typeEdit->currentIndex());
    query.bindValue(":form", ui->formEdit->currentIndex());
    query.bindValue(":notes", ui->notesEdit->toPlainText());
    query.bindValue(":origin", ui->originEdit->text());
    query.bindValue(":substitutes", ui->substitutesEdit->text());
    query.bindValue(":always", ui->alwaysEdit->isChecked() ? 1:0);
    query.bindValue(":inventory", QString("%1").arg(0, 5, 'f', 4, '0'));
    query.bindValue(":cost", QString("%1").arg(ui->costEdit->value(), 3, 'f', 2, '0'));
    /* Uses https://www.qtcentre.org/threads/17295-How-to-put-empty-value-in-QDateEdit */
    query.bindValue(":prod", QDate());
    query.bindValue(":tht", QDate());
    query.bindValue(":oil", QString("%1").arg(ui->oilEdit->value(), 2, 'f', 1, '0'));
    query.bindValue(":utilisation", QString("%1").arg(ui->utilisationEdit->value(), 2, 'f', 1, '0'));
    query.bindValue(":bu_factor", QString("%1").arg(ui->bufactorEdit->value(), 2, 'f', 1, '0'));
    query.bindValue(":uuid", QUuid::createUuid().toString().mid(1, 36));
    query.exec();
    if (query.lastError().isValid()) {
        qDebug() << "EditHop" << 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() << "EditHop Saved";
    }

}


void EditHop::on_deleteButton_clicked()
{
    QSqlQuery query;

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

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


void EditHop::is_changed()
{
    ui->valueEdit->setValue(ui->inventoryEdit->value() * ui->costEdit->value());
    ui->saveButton->setEnabled(true);
    ui->deleteButton->setEnabled((ui->inventoryEdit->value() == 0 && this->recno >= 0) ? true:false);
    this->textIsChanged = true;
    WindowTitle();
}


void EditHop::form_changed(int val)
{
    switch (val) {
	case HOP_FORMS_PELLET:		ui->utilisationEdit->setValue(my_ut_pellet);		break;
	case HOP_FORMS_PLUG:		ui->utilisationEdit->setValue(my_ut_plug);		break;
	case HOP_FORMS_LEAF:		ui->utilisationEdit->setValue(my_ut_leaf);		break;
	case HOP_FORMS_LEAF_WET:	ui->utilisationEdit->setValue(my_ut_wethop);		break;
	case HOP_FORMS_CRYO:		ui->utilisationEdit->setValue(my_ut_t45);		break;
	case HOP_FORMS_CO2EXTRACT:	ui->utilisationEdit->setValue(my_ut_co2extract);	break;
    }
    is_changed();
}


void EditHop::on_quitButton_clicked()
{
    if (this->textIsChanged) {
	int rc = QMessageBox::warning(this, tr("Hop changed"), tr("This hop 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);
}


void EditHop::prod_date_clear()
{
    ui->prodEdit->setDate(QDate());
    is_changed();
}


void EditHop::prod_date_today()
{
    ui->prodEdit->setDate(QDate::currentDate());
    is_changed();
}


void EditHop::tht_date_clear()
{
    ui->thtEdit->setDate(QDate());
    is_changed();
}


void EditHop::tht_date_today()
{
    ui->thtEdit->setDate(QDate::currentDate());
    is_changed();
}

mercurial