src/EditHop.cpp

changeset 373
b02aca4e926c
parent 300
2a97905cb637
child 375
c21567bfd703
--- a/src/EditHop.cpp	Tue Jul 26 19:46:44 2022 +0200
+++ b/src/EditHop.cpp	Fri Jul 29 13:12:26 2022 +0200
@@ -17,6 +17,7 @@
 #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)
@@ -27,18 +28,10 @@
     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 */
-    ui->formEdit->addItem(tr("Extract"));	/* CO2 extract */
+    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");
@@ -46,38 +39,42 @@
 	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());
+	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(18).toString().length() == 10) {
-	    ui->thtEdit->setDate(query.value(18).toDate());
+	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(19).toDouble());
+	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(0);
 	ui->formEdit->setCurrentIndex(0);
+	ui->utilisationEdit->setValue(35.0);
+	ui->bufactorEdit->setValue(1.0);
 	ui->prodEdit->clear();
 	ui->thtEdit->clear();
     }
@@ -104,9 +101,13 @@
     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();
 }
 
 
@@ -135,12 +136,19 @@
 	txt.append((QString(" **")));
     }
     setWindowTitle(txt);
+
+    if (ui->formEdit->currentIndex() < HOP_FORMS_CO2EXTRACT) {
+        ui->inventoryEdit->setSuffix(tr(" Kg"));
+    } else {
+        ui->inventoryEdit->setSuffix(tr(" L"));
+    }
 }
 
 
 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) {
@@ -154,18 +162,22 @@
 
     if (this->textIsChanged) {
     	if (this->recno == -1) {
-    	    query.prepare("INSERT INTO inventory_hops SET name=:name, alpha=:alpha, beta=:beta, "
+	    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, 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");
+		"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'));
@@ -186,6 +198,8 @@
 	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 {

mercurial