src/EditMisc.cpp

Wed, 06 Apr 2022 20:26:47 +0200

author
Michiel Broek <mbroek@mbse.eu>
date
Wed, 06 Apr 2022 20:26:47 +0200
changeset 110
224be4d9f8eb
parent 90
2396457a8167
child 292
b6ec2e275736
permissions
-rw-r--r--

Fermentables are loaded in the main recipe record in a QList. The refresh table function does the sorting, on added moment (mash first), amount and finally color of the malt. Removed the old hidden table columns. Do all manipulation on the recipe record with QList arrays. The ferment_amount_changed() slot is complete. The whole recipe editor is going in the right direction.

/**
 * 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(1).toString());
	ui->typeEdit->setCurrentIndex(query.value(2).toInt());
	ui->useEdit->setCurrentIndex(query.value(3).toInt());
	ui->timeEdit->setValue(query.value(4).toInt());
	ui->isweightEdit->setChecked(query.value(5).toInt() ? true:false);
	ui->useforEdit->setPlainText(query.value(6).toString());
	ui->notesEdit->setPlainText(query.value(7).toString());
	ui->alwaysEdit->setChecked(query.value(8).toInt() ? true:false);
	ui->inventoryEdit->setValue(query.value(9).toDouble());
	ui->costEdit->setValue(query.value(10).toDouble());
	ui->valueEdit->setValue(query.value(9).toDouble() * query.value(10).toDouble());
	if (query.value(11).toString().length() == 10) {
            ui->prodEdit->setDate(query.value(11).toDate());
        } else {
            ui->prodEdit->clear();
        }
	if (query.value(12).toString().length() == 10) {
	    ui->thtEdit->setDate(query.value(12).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();
    }
    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::is_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->thtEdit, &QDateEdit::dateChanged, this, &EditMisc::is_changed);

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


EditMisc::~EditMisc()
{
    qDebug() << "EditMisc done";
    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()) {
	    qDebug() << "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_deleteButton_clicked()
{
    QSqlQuery query;

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

mercurial