src/EditHop.cpp

changeset 24
684c6e74cc1b
child 60
0d65238ebedc
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/EditHop.cpp	Sun Feb 20 20:22:49 2022 +0100
@@ -0,0 +1,259 @@
+/**
+ * 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 "bmsapp.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;
+
+    WindowTitle();
+
+    ui->typeEdit->addItem(tr("Bittering"));
+    ui->typeEdit->addItem(tr("Aroma"));
+    ui->typeEdit->addItem(tr("Both"));
+
+    ui->formEdit->addItem(tr("Pellet"));
+    ui->formEdit->addItem(tr("Plug"));
+    ui->formEdit->addItem(tr("Leaf"));
+    ui->formEdit->addItem(tr("Leaf Wet"));	/* Not in beerxml */
+    ui->formEdit->addItem(tr("Cryo"));		/* Not in beerxml */
+
+    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(1).toString());
+	ui->alphaEdit->setValue(query.value(2).toDouble());
+	ui->betaEdit->setValue(query.value(3).toDouble());
+	ui->humuleneEdit->setValue(query.value(4).toDouble());
+	ui->caryEdit->setValue(query.value(5).toDouble());
+	ui->cohumuloneEdit->setValue(query.value(6).toDouble());
+	ui->myrceneEdit->setValue(query.value(7).toDouble());
+	ui->hsiEdit->setValue(query.value(8).toDouble());
+	ui->typeEdit->setCurrentIndex(query.value(9).toInt());
+	ui->formEdit->setCurrentIndex(query.value(10).toInt());
+	ui->notesEdit->setPlainText(query.value(11).toString());
+	ui->originEdit->setText(query.value(12).toString());
+	ui->substitutesEdit->setText(query.value(13).toString());
+	ui->alwaysEdit->setChecked(query.value(14).toInt() ? true:false);
+	ui->inventoryEdit->setValue(query.value(15).toDouble());
+	ui->costEdit->setValue(query.value(16).toDouble());
+	ui->valueEdit->setValue(query.value(15).toDouble() * query.value(16).toDouble());
+	if (query.value(17).toString().length() == 10) {
+            ui->prodEdit->setDate(query.value(17).toDate());
+        } else {
+            ui->prodEdit->clear();
+        }
+	if (query.value(18).toString().length() == 10) {
+	    ui->thtEdit->setDate(query.value(18).toDate());
+	} else {
+	    ui->thtEdit->clear();
+	}
+	ui->oilEdit->setValue(query.value(19).toDouble());
+    } else {
+	/* Set some defaults */
+	ui->typeEdit->setCurrentIndex(0);
+	ui->formEdit->setCurrentIndex(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, &QComboBox::currentTextChanged, this, &EditHop::is_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->thtEdit, &QDateEdit::dateChanged, this, &EditHop::is_changed);
+    connect(ui->oilEdit, &QDoubleSpinBox::textChanged, this, &EditHop::is_changed);
+
+    ui->saveButton->setEnabled(false);
+    ui->deleteButton->setEnabled((ui->inventoryEdit->value() == 0 && id >= 0) ? true:false);
+}
+
+
+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;
+
+    /* 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) {
+    	    query.prepare("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, uuid = :uuid");
+    	} else {
+	    query.prepare("UPDATE 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 WHERE record = :recno");
+    	}
+	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'));
+	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_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::on_quitButton_clicked()
+{
+    if (this->textIsChanged) {
+	int rc = QMessageBox::warning(this, tr("Hop changed"), tr("The fermentable has been modified\n 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