src/EditMisc.cpp

Thu, 18 Aug 2022 17:07:47 +0200

author
Michiel Broek <mbroek@mbse.eu>
date
Thu, 18 Aug 2022 17:07:47 +0200
changeset 398
49cf387e9070
parent 385
09af9f46518f
permissions
-rw-r--r--

After brewing show final IBU on the first tab.

/**
 * EditMisc.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 "EditMisc.h"
#include "../ui/ui_EditMisc.h"
#include "MainWindow.h"


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

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

    WindowTitle();

    ui->typeEdit->addItem(tr("Spice"));
    ui->typeEdit->addItem(tr("Herb"));
    ui->typeEdit->addItem(tr("Flavor"));
    ui->typeEdit->addItem(tr("Fining"));
    ui->typeEdit->addItem(tr("Water agent"));
    ui->typeEdit->addItem(tr("Yeast nutrient"));
    ui->typeEdit->addItem(tr("Other"));

    ui->useEdit->addItem(tr("Starter"));
    ui->useEdit->addItem(tr("Mash"));
    ui->useEdit->addItem(tr("Boil"));
    ui->useEdit->addItem(tr("Primary"));
    ui->useEdit->addItem(tr("Secondary"));
    ui->useEdit->addItem(tr("Bottling"));

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

	ui->nameEdit->setText(query.value("name").toString());
	ui->typeEdit->setCurrentIndex(query.value("type").toInt());
	ui->useEdit->setCurrentIndex(query.value("use_use").toInt());
	ui->timeEdit->setValue(query.value("time").toInt());
	ui->isweightEdit->setChecked(query.value("amount_is_weight").toInt() ? true:false);
	ui->useforEdit->setPlainText(query.value("use_for").toString());
	ui->notesEdit->setPlainText(query.value("notes").toString());
	ui->alwaysEdit->setChecked(query.value("always_on_stock").toInt() ? true:false);
	ui->inventoryEdit->setValue(query.value("inventory").toDouble() * 1000.0);
	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();
	}
    } else {
	/* Set some defaults */
	ui->typeEdit->setCurrentIndex(0);
	ui->useEdit->setCurrentIndex(0);
	ui->timeEdit->setValue(0);
	ui->prodEdit->clear();
	ui->thtEdit->clear();
    }
    UnitSet();
    TimeSet();
    connect(ui->nameEdit, &QLineEdit::textChanged, this, &EditMisc::is_changed);
    connect(ui->typeEdit, &QComboBox::currentTextChanged, this, &EditMisc::is_changed);
    connect(ui->useEdit, &QComboBox::currentTextChanged, this, &EditMisc::is_changed);
    connect(ui->timeEdit, &QSpinBox::textChanged, this, &EditMisc::time_changed);
    connect(ui->isweightEdit, &QCheckBox::stateChanged, this, &EditMisc::weight_changed);
    connect(ui->useforEdit, SIGNAL(textChanged()), this, SLOT(is_changed()));
    connect(ui->notesEdit, SIGNAL(textChanged()), this, SLOT(is_changed()));
    connect(ui->alwaysEdit, &QCheckBox::stateChanged, this, &EditMisc::is_changed);
    connect(ui->inventoryEdit, &QDoubleSpinBox::textChanged, this, &EditMisc::is_changed);
    connect(ui->costEdit, &QDoubleSpinBox::textChanged, this, &EditMisc::is_changed);
    connect(ui->prodEdit, &QDateEdit::dateChanged, this, &EditMisc::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, &EditMisc::is_changed);
    connect(ui->thtButton1, SIGNAL(clicked()), this, SLOT(tht_date_today()));
    connect(ui->thtButton2, SIGNAL(clicked()), this, SLOT(tht_date_clear()));

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


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


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

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

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


void EditMisc::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 Misc"), tr("Name empty or too short."));
	return;
    }

    if (this->textIsChanged) {
    	if (this->recno == -1) {
    	    query.prepare("INSERT INTO inventory_miscs SET name=:name, type=:type, use_use=:use, "
		"time=:time, amount_is_weight=:isweight, use_for=:usefor, notes=:notes, "
		"always_on_stock=:always, inventory=:inventory, cost=:cost, production_date=:prod, "
		"tht_date=:tht, uuid = :uuid");
    	} else {
	    query.prepare("UPDATE inventory_miscs SET name=:name, type=:type, use_use=:use, "
		"time=:time, amount_is_weight=:isweight, use_for=:usefor, notes=:notes, "
		"always_on_stock=:always, inventory=:inventory, cost=:cost, production_date=:prod, "
                "tht_date=:tht WHERE record = :recno");
    	}
	query.bindValue(":name", ui->nameEdit->text());
	query.bindValue(":type", ui->typeEdit->currentIndex());
	query.bindValue(":use", ui->useEdit->currentIndex());
	query.bindValue(":time", ui->timeEdit->value());
	query.bindValue(":isweight", ui->isweightEdit->isChecked() ? 1:0);
	query.bindValue(":usefor", ui->useforEdit->toPlainText());
	query.bindValue(":notes", ui->notesEdit->toPlainText());
	query.bindValue(":always", ui->alwaysEdit->isChecked() ? 1:0);
	query.bindValue(":inventory", QString("%1").arg(ui->inventoryEdit->value() / 1000.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", ui->prodEdit->nullDate());
	query.bindValue(":tht", ui->thtEdit->nullDate());
	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()) {
	    qWarning() << "EditMisc" << 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() << "EditMisc Saved";
	}
    }

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


void EditMisc::on_cloneButton_clicked()
{
    QSqlQuery query;

    query.prepare("INSERT INTO inventory_miscs SET name=:name, type=:type, use_use=:use, "
                "time=:time, amount_is_weight=:isweight, use_for=:usefor, notes=:notes, "
                "always_on_stock=:always, inventory=:inventory, cost=:cost, production_date=:prod, "
                "tht_date=:tht, uuid = :uuid");

    query.bindValue(":name", ui->nameEdit->text() + " [copy]");
    query.bindValue(":type", ui->typeEdit->currentIndex());
    query.bindValue(":use", ui->useEdit->currentIndex());
    query.bindValue(":time", ui->timeEdit->value());
    query.bindValue(":isweight", ui->isweightEdit->isChecked() ? 1:0);
    query.bindValue(":usefor", ui->useforEdit->toPlainText());
    query.bindValue(":notes", ui->notesEdit->toPlainText());
    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'));
    query.bindValue(":prod", QDate());
    query.bindValue(":tht", QDate());
    query.bindValue(":uuid", QUuid::createUuid().toString().mid(1, 36));

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


void EditMisc::on_deleteButton_clicked()
{
    QSqlQuery query;

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

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

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


void EditMisc::UnitSet()
{
    if (ui->isweightEdit->isChecked()) {
	ui->inventoryEdit->setSuffix(tr(" gr"));
    } else {
	ui->inventoryEdit->setSuffix(tr(" ml"));
    }
}


void EditMisc::TimeSet()
{
    int	time = ui->timeEdit->value();
    QString w = QString("");

    if (time == 0) {
	ui->timeEdit->setSingleStep(1);

    } else if (time == 1) {
	w = QString("1 minute");
	ui->timeEdit->setSingleStep(1);

    } else if (time < 180) {
	w = QString("%1 minutes").arg(time);
	ui->timeEdit->setSingleStep(1);

    } else if (time == 180) {
	w = QString("3 hours");
	if (lasttime == 240)
	    ui->timeEdit->setSingleStep(1);	/* Going down */
	else
	    ui->timeEdit->setSingleStep(60);

    } else if (time < 1440) {
	w = QString("%1 hours").arg(time / 60);
	ui->timeEdit->setSingleStep(60);

    } else if (time == 1440) {
	w = QString("1 day");
	if (lasttime == 2880)
	    ui->timeEdit->setSingleStep(60);	/* Going down */
	else
	    ui->timeEdit->setSingleStep(1440);

    } else {
	w = QString("%1 days").arg(time / 1440);
	ui->timeEdit->setSingleStep(1440);

    }
    ui->timeShow->setText(w);
    lasttime = time;
}


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


void EditMisc::weight_changed()
{
    UnitSet();
    is_changed();
}


void EditMisc::time_changed()
{
    TimeSet();
    is_changed();
}


void EditMisc::on_quitButton_clicked()
{
    if (this->textIsChanged) {
	int rc = QMessageBox::warning(this, tr("Misc changed"), tr("The ingredient 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 EditMisc::prod_date_clear()
{
    ui->prodEdit->setDate(QDate());
    is_changed();
}


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


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


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

mercurial