15 * along with this program. If not, see <http://www.gnu.org/licenses/>. |
15 * along with this program. If not, see <http://www.gnu.org/licenses/>. |
16 */ |
16 */ |
17 #include "EditHop.h" |
17 #include "EditHop.h" |
18 #include "../ui/ui_EditHop.h" |
18 #include "../ui/ui_EditHop.h" |
19 #include "MainWindow.h" |
19 #include "MainWindow.h" |
|
20 #include "global.h" |
20 |
21 |
21 |
22 |
22 EditHop::EditHop(int id, QWidget *parent) : QDialog(parent), ui(new Ui::EditHop) |
23 EditHop::EditHop(int id, QWidget *parent) : QDialog(parent), ui(new Ui::EditHop) |
23 { |
24 { |
24 QSqlQuery query; |
25 QSqlQuery query; |
25 |
26 |
26 qDebug() << "EditHop record:" << id; |
27 qDebug() << "EditHop record:" << id; |
27 ui->setupUi(this); |
28 ui->setupUi(this); |
28 this->recno = id; |
29 this->recno = id; |
29 |
30 |
30 WindowTitle(); |
31 for (int i = 0; i < 3; i++) |
31 |
32 ui->typeEdit->addItem(QCoreApplication::translate("HopTypes", g_hop_types[i])); |
32 ui->typeEdit->addItem(tr("Bittering")); |
33 for (int i = 0; i < 7; i++) |
33 ui->typeEdit->addItem(tr("Aroma")); |
34 ui->formEdit->addItem(QCoreApplication::translate("HopForm", g_hop_forms[i])); |
34 ui->typeEdit->addItem(tr("Both")); |
|
35 |
|
36 ui->formEdit->addItem(tr("Pellet")); |
|
37 ui->formEdit->addItem(tr("Plug")); |
|
38 ui->formEdit->addItem(tr("Leaf")); |
|
39 ui->formEdit->addItem(tr("Leaf Wet")); /* Not in beerxml */ |
|
40 ui->formEdit->addItem(tr("Cryo")); /* Not in beerxml */ |
|
41 ui->formEdit->addItem(tr("Extract")); /* CO2 extract */ |
|
42 |
35 |
43 if (id >= 0) { |
36 if (id >= 0) { |
44 query.prepare("SELECT * FROM inventory_hops WHERE record = :recno"); |
37 query.prepare("SELECT * FROM inventory_hops WHERE record = :recno"); |
45 query.bindValue(":recno", id); |
38 query.bindValue(":recno", id); |
46 query.exec(); |
39 query.exec(); |
47 query.next(); |
40 query.next(); |
48 |
41 |
49 ui->nameEdit->setText(query.value(1).toString()); |
42 ui->nameEdit->setText(query.value("name").toString()); |
50 ui->alphaEdit->setValue(query.value(2).toDouble()); |
43 ui->alphaEdit->setValue(query.value("alpha").toDouble()); |
51 ui->betaEdit->setValue(query.value(3).toDouble()); |
44 ui->betaEdit->setValue(query.value("beta").toDouble()); |
52 ui->humuleneEdit->setValue(query.value(4).toDouble()); |
45 ui->humuleneEdit->setValue(query.value("humulene").toDouble()); |
53 ui->caryEdit->setValue(query.value(5).toDouble()); |
46 ui->caryEdit->setValue(query.value("caryophyllene").toDouble()); |
54 ui->cohumuloneEdit->setValue(query.value(6).toDouble()); |
47 ui->cohumuloneEdit->setValue(query.value("cohumulone").toDouble()); |
55 ui->myrceneEdit->setValue(query.value(7).toDouble()); |
48 ui->myrceneEdit->setValue(query.value("myrcene").toDouble()); |
56 ui->hsiEdit->setValue(query.value(8).toDouble()); |
49 ui->hsiEdit->setValue(query.value("hsi").toDouble()); |
57 ui->typeEdit->setCurrentIndex(query.value(9).toInt()); |
50 ui->typeEdit->setCurrentIndex(query.value("type").toInt()); |
58 ui->formEdit->setCurrentIndex(query.value(10).toInt()); |
51 ui->formEdit->setCurrentIndex(query.value("form").toInt()); |
59 ui->notesEdit->setPlainText(query.value(11).toString()); |
52 ui->notesEdit->setPlainText(query.value("notes").toString()); |
60 ui->originEdit->setText(query.value(12).toString()); |
53 ui->originEdit->setText(query.value("origin").toString()); |
61 ui->substitutesEdit->setText(query.value(13).toString()); |
54 ui->substitutesEdit->setText(query.value("substitutes").toString()); |
62 ui->alwaysEdit->setChecked(query.value(14).toInt() ? true:false); |
55 ui->alwaysEdit->setChecked(query.value("always_on_stock").toInt() ? true:false); |
63 ui->inventoryEdit->setValue(query.value(15).toDouble()); |
56 ui->inventoryEdit->setValue(query.value("inventory").toDouble()); |
64 ui->costEdit->setValue(query.value(16).toDouble()); |
57 ui->costEdit->setValue(query.value("cost").toDouble()); |
65 ui->valueEdit->setValue(query.value(15).toDouble() * query.value(16).toDouble()); |
58 ui->valueEdit->setValue(query.value("inventory").toDouble() * query.value("cost").toDouble()); |
66 if (query.value(17).toString().length() == 10) { |
59 if (query.value("production_date").toString().length() == 10) { |
67 ui->prodEdit->setDate(query.value(17).toDate()); |
60 ui->prodEdit->setDate(query.value("production_date").toDate()); |
68 } else { |
61 } else { |
69 ui->prodEdit->clear(); |
62 ui->prodEdit->clear(); |
70 } |
63 } |
71 if (query.value(18).toString().length() == 10) { |
64 if (query.value("tht_date").toString().length() == 10) { |
72 ui->thtEdit->setDate(query.value(18).toDate()); |
65 ui->thtEdit->setDate(query.value("tht_date").toDate()); |
73 } else { |
66 } else { |
74 ui->thtEdit->clear(); |
67 ui->thtEdit->clear(); |
75 } |
68 } |
76 ui->oilEdit->setValue(query.value(19).toDouble()); |
69 ui->oilEdit->setValue(query.value("total_oil").toDouble()); |
|
70 ui->utilisationEdit->setValue(query.value("utilisation").toDouble()); |
|
71 ui->bufactorEdit->setValue(query.value("bu_factor").toDouble()); |
77 } else { |
72 } else { |
78 /* Set some defaults */ |
73 /* Set some defaults */ |
79 ui->typeEdit->setCurrentIndex(0); |
74 ui->typeEdit->setCurrentIndex(0); |
80 ui->formEdit->setCurrentIndex(0); |
75 ui->formEdit->setCurrentIndex(0); |
|
76 ui->utilisationEdit->setValue(35.0); |
|
77 ui->bufactorEdit->setValue(1.0); |
81 ui->prodEdit->clear(); |
78 ui->prodEdit->clear(); |
82 ui->thtEdit->clear(); |
79 ui->thtEdit->clear(); |
83 } |
80 } |
84 connect(ui->nameEdit, &QLineEdit::textChanged, this, &EditHop::is_changed); |
81 connect(ui->nameEdit, &QLineEdit::textChanged, this, &EditHop::is_changed); |
85 connect(ui->alphaEdit, &QDoubleSpinBox::textChanged, this, &EditHop::is_changed); |
82 connect(ui->alphaEdit, &QDoubleSpinBox::textChanged, this, &EditHop::is_changed); |
102 connect(ui->prodButton2, SIGNAL(clicked()), this, SLOT(prod_date_clear())); |
99 connect(ui->prodButton2, SIGNAL(clicked()), this, SLOT(prod_date_clear())); |
103 connect(ui->thtEdit, &QDateEdit::dateChanged, this, &EditHop::is_changed); |
100 connect(ui->thtEdit, &QDateEdit::dateChanged, this, &EditHop::is_changed); |
104 connect(ui->thtButton1, SIGNAL(clicked()), this, SLOT(tht_date_today())); |
101 connect(ui->thtButton1, SIGNAL(clicked()), this, SLOT(tht_date_today())); |
105 connect(ui->thtButton2, SIGNAL(clicked()), this, SLOT(tht_date_clear())); |
102 connect(ui->thtButton2, SIGNAL(clicked()), this, SLOT(tht_date_clear())); |
106 connect(ui->oilEdit, &QDoubleSpinBox::textChanged, this, &EditHop::is_changed); |
103 connect(ui->oilEdit, &QDoubleSpinBox::textChanged, this, &EditHop::is_changed); |
|
104 connect(ui->utilisationEdit, &QDoubleSpinBox::textChanged, this, &EditHop::is_changed); |
|
105 connect(ui->bufactorEdit, &QDoubleSpinBox::textChanged, this, &EditHop::is_changed); |
107 |
106 |
108 ui->saveButton->setEnabled(false); |
107 ui->saveButton->setEnabled(false); |
109 ui->deleteButton->setEnabled((ui->inventoryEdit->value() == 0 && id >= 0) ? true:false); |
108 ui->deleteButton->setEnabled((ui->inventoryEdit->value() == 0 && id >= 0) ? true:false); |
|
109 |
|
110 WindowTitle(); |
110 } |
111 } |
111 |
112 |
112 |
113 |
113 EditHop::~EditHop() |
114 EditHop::~EditHop() |
114 { |
115 { |
152 return; |
160 return; |
153 } |
161 } |
154 |
162 |
155 if (this->textIsChanged) { |
163 if (this->textIsChanged) { |
156 if (this->recno == -1) { |
164 if (this->recno == -1) { |
157 query.prepare("INSERT INTO inventory_hops SET name=:name, alpha=:alpha, beta=:beta, " |
165 sql = "INSERT INTO inventory_hops SET "; |
|
166 } else { |
|
167 sql = "UPDATE inventory_hops SET "; |
|
168 } |
|
169 sql.append("name=:name, alpha=:alpha, beta=:beta, " |
158 "humulene=:humulene, caryophyllene=:cary, cohumulone=:cohumulone, myrcene=:myrcene, " |
170 "humulene=:humulene, caryophyllene=:cary, cohumulone=:cohumulone, myrcene=:myrcene, " |
159 "hsi=:hsi, type=:type, form=:form, notes=:notes, origin=:origin, substitutes=:substitutes, " |
171 "hsi=:hsi, type=:type, form=:form, notes=:notes, origin=:origin, substitutes=:substitutes, " |
160 "always_on_stock=:always, inventory=:inventory, cost=:cost, production_date=:prod, " |
172 "always_on_stock=:always, inventory=:inventory, cost=:cost, production_date=:prod, " |
161 "tht_date=:tht, total_oil=:oil, uuid = :uuid"); |
173 "tht_date=:tht, total_oil=:oil, utilisation=:utilisation, bu_factor=:bu_factor"); |
162 } else { |
174 if (this->recno == -1) { |
163 query.prepare("UPDATE inventory_hops SET name=:name, alpha=:alpha, beta=:beta, " |
175 sql.append(", uuid=:uuid"); |
164 "humulene=:humulene, caryophyllene=:cary, cohumulone=:cohumulone, myrcene=:myrcene, " |
176 } else { |
165 "hsi=:hsi, type=:type, form=:form, notes=:notes, origin=:origin, substitutes=:substitutes, " |
177 sql.append(" WHERE record = :recno"); |
166 "always_on_stock=:always, inventory=:inventory, cost=:cost, production_date=:prod, " |
|
167 "tht_date=:tht, total_oil=:oil WHERE record = :recno"); |
|
168 } |
178 } |
|
179 |
|
180 query.prepare(sql); |
169 query.bindValue(":name", ui->nameEdit->text()); |
181 query.bindValue(":name", ui->nameEdit->text()); |
170 query.bindValue(":alpha", QString("%1").arg(ui->alphaEdit->value(), 2, 'f', 1, '0')); |
182 query.bindValue(":alpha", QString("%1").arg(ui->alphaEdit->value(), 2, 'f', 1, '0')); |
171 query.bindValue(":beta", QString("%1").arg(ui->betaEdit->value(), 2, 'f', 1, '0')); |
183 query.bindValue(":beta", QString("%1").arg(ui->betaEdit->value(), 2, 'f', 1, '0')); |
172 query.bindValue(":humulene", QString("%1").arg(ui->humuleneEdit->value(), 2, 'f', 1, '0')); |
184 query.bindValue(":humulene", QString("%1").arg(ui->humuleneEdit->value(), 2, 'f', 1, '0')); |
173 query.bindValue(":cary", QString("%1").arg(ui->caryEdit->value(), 2, 'f', 1, '0')); |
185 query.bindValue(":cary", QString("%1").arg(ui->caryEdit->value(), 2, 'f', 1, '0')); |
184 query.bindValue(":cost", QString("%1").arg(ui->costEdit->value(), 3, 'f', 2, '0')); |
196 query.bindValue(":cost", QString("%1").arg(ui->costEdit->value(), 3, 'f', 2, '0')); |
185 /* Uses https://www.qtcentre.org/threads/17295-How-to-put-empty-value-in-QDateEdit */ |
197 /* Uses https://www.qtcentre.org/threads/17295-How-to-put-empty-value-in-QDateEdit */ |
186 query.bindValue(":prod", ui->prodEdit->nullDate()); |
198 query.bindValue(":prod", ui->prodEdit->nullDate()); |
187 query.bindValue(":tht", ui->thtEdit->nullDate()); |
199 query.bindValue(":tht", ui->thtEdit->nullDate()); |
188 query.bindValue(":oil", QString("%1").arg(ui->oilEdit->value(), 2, 'f', 1, '0')); |
200 query.bindValue(":oil", QString("%1").arg(ui->oilEdit->value(), 2, 'f', 1, '0')); |
|
201 query.bindValue(":utilisation", QString("%1").arg(ui->utilisationEdit->value(), 2, 'f', 1, '0')); |
|
202 query.bindValue(":bu_factor", QString("%1").arg(ui->bufactorEdit->value(), 2, 'f', 1, '0')); |
189 if (this->recno == -1) { |
203 if (this->recno == -1) { |
190 query.bindValue(":uuid", QUuid::createUuid().toString().mid(1, 36)); |
204 query.bindValue(":uuid", QUuid::createUuid().toString().mid(1, 36)); |
191 } else { |
205 } else { |
192 query.bindValue(":recno", this->recno); |
206 query.bindValue(":recno", this->recno); |
193 } |
207 } |