src/EditYeast.cpp

changeset 25
a9da2744609e
child 26
dfc4df8f9632
equal deleted inserted replaced
24:684c6e74cc1b 25:a9da2744609e
1 /**
2 * EditYeast.cpp is part of bmsapp.
3 *
4 * bmsapp is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * bmsapp is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17 #include "EditYeast.h"
18 #include "../ui/ui_EditYeast.h"
19 #include "bmsapp.h"
20
21
22 EditYeast::EditYeast(int id, QWidget *parent) : QDialog(parent), ui(new Ui::EditYeast)
23 {
24 QSqlQuery query;
25
26 qDebug() << "EditYeast record:" << id;
27 ui->setupUi(this);
28 this->recno = id;
29
30 WindowTitle();
31
32 ui->typeEdit->addItem(tr("Lager"));
33 ui->typeEdit->addItem(tr("Ale"));
34 ui->typeEdit->addItem(tr("Wheat"));
35 ui->typeEdit->addItem(tr("Wine"));
36 ui->typeEdit->addItem(tr("Champagne"));
37 ui->typeEdit->addItem(tr("Brett"));
38 ui->typeEdit->addItem(tr("Kveik"));
39 ui->typeEdit->addItem(tr("Hybrid"));
40
41 ui->formEdit->addItem(tr("Liquid"));
42 ui->formEdit->addItem(tr("Dry"));
43 ui->formEdit->addItem(tr("Slant"));
44 ui->formEdit->addItem(tr("Culture"));
45 ui->formEdit->addItem(tr("Frozen"));
46 ui->formEdit->addItem(tr("Bottle"));
47 ui->formEdit->addItem(tr("Dried"));
48
49 ui->flocEdit->addItem(tr("Low"));
50 ui->flocEdit->addItem(tr("Medium"));
51 ui->flocEdit->addItem(tr("High"));
52 ui->flocEdit->addItem(tr("Very high"));
53
54 if (id >= 0) {
55 query.prepare("SELECT * FROM inventory_yeasts WHERE record = :recno");
56 query.bindValue(":recno", id);
57 query.exec();
58 query.next();
59
60 ui->nameEdit->setText(query.value(1).toString());
61 ui->typeEdit->setCurrentIndex(query.value(2).toInt());
62 ui->formEdit->setCurrentIndex(query.value(3).toInt());
63 ui->laboratoryEdit->setText(query.value(4).toString());
64 ui->productidEdit->setText(query.value(5).toString());
65 ui->temploEdit->setValue(query.value(6).toDouble());
66 ui->temphiEdit->setValue(query.value(7).toDouble());
67 ui->flocEdit->setCurrentIndex(query.value(8).toInt());
68 ui->attEdit->setValue(query.value(9).toDouble());
69 ui->toleranceEdit->setValue(query.value(10).toDouble());
70 ui->notesEdit->setPlainText(query.value(11).toString());
71 ui->bestforEdit->setPlainText(query.value(12).toString());
72 ui->reuseEdit->setValue(query.value(13).toInt());
73 if (query.value(3).toInt() == 0)
74 ui->inventoryEdit->setValue(query.value(14).toDouble()); /* Liquid */
75 else
76 ui->inventoryEdit->setValue(query.value(14).toDouble() * 1000);
77 ui->costEdit->setValue(query.value(15).toDouble());
78 SetForm(query.value(3).toInt());
79 if (query.value(16).toString().length() == 10) {
80 ui->prodEdit->setDate(query.value(16).toDate());
81 } else {
82 ui->prodEdit->clear();
83 }
84 if (query.value(17).toString().length() == 10) {
85 ui->thtEdit->setDate(query.value(17).toDate());
86 } else {
87 ui->thtEdit->clear();
88 }
89 ui->cellsEdit->setValue(query.value(18).toDouble() / 1000000000);
90 ui->sta1Edit->setChecked(query.value(19).toInt() ? true:false);
91 ui->bacteriaEdit->setChecked(query.value(20).toInt() ? true:false);
92 ui->harvesttopEdit->setChecked(query.value(21).toInt() ? true:false);
93 ui->harvesttimeEdit->setValue(query.value(22).toInt());
94 ui->pitchtempEdit->setValue(query.value(23).toDouble());
95 ui->pofEdit->setChecked(query.value(24).toInt() ? true:false);
96 ui->yeastbankEdit->setText(query.value(26).toString());
97 ui->grhlloEdit->setValue(query.value(27).toInt());
98 ui->sgloEdit->setValue(query.value(28).toDouble());
99 ui->grhlhiEdit->setValue(query.value(29).toInt());
100 ui->sghiEdit->setValue(query.value(30).toDouble());
101 } else {
102 /* Set some defaults */
103 ui->typeEdit->setCurrentIndex(1);
104 ui->formEdit->setCurrentIndex(1);
105 SetForm(1);
106 ui->temploEdit->setValue(18.0);
107 ui->temphiEdit->setValue(22.0);
108 ui->attEdit->setValue(77.0);
109 ui->reuseEdit->setValue(10);
110 ui->grhlloEdit->setValue(50);
111 ui->sgloEdit->setValue(1.050);
112 ui->grhlhiEdit->setValue(80);
113 ui->sghiEdit->setValue(1.080);
114 ui->prodEdit->clear();
115 ui->thtEdit->clear();
116 }
117 connect(ui->nameEdit, &QLineEdit::textChanged, this, &EditYeast::is_changed);
118 connect(ui->typeEdit, &QComboBox::currentTextChanged, this, &EditYeast::is_changed);
119 connect(ui->formEdit, &QComboBox::currentTextChanged, this, &EditYeast::is_changed);
120 connect(ui->laboratoryEdit, &QLineEdit::textChanged, this, &EditYeast::is_changed);
121 connect(ui->productidEdit, &QLineEdit::textChanged, this, &EditYeast::is_changed);
122 connect(ui->temploEdit, &QDoubleSpinBox::textChanged, this, &EditYeast::temp_lo_changed);
123 connect(ui->temphiEdit, &QDoubleSpinBox::textChanged, this, &EditYeast::temp_hi_changed);
124 connect(ui->flocEdit, &QComboBox::currentTextChanged, this, &EditYeast::is_changed);
125 connect(ui->attEdit, &QDoubleSpinBox::textChanged, this, &EditYeast::is_changed);
126 connect(ui->toleranceEdit, &QDoubleSpinBox::textChanged, this, &EditYeast::is_changed);
127 connect(ui->notesEdit, SIGNAL(textChanged()), this, SLOT(is_changed()));
128 connect(ui->bestforEdit, SIGNAL(textChanged()), this, SLOT(is_changed()));
129 connect(ui->reuseEdit, &QSpinBox::textChanged, this, &EditYeast::is_changed);
130 connect(ui->inventoryEdit, &QDoubleSpinBox::textChanged, this, &EditYeast::is_changed);
131 connect(ui->costEdit, &QDoubleSpinBox::textChanged, this, &EditYeast::is_changed);
132 connect(ui->prodEdit, &QDateEdit::dateChanged, this, &EditYeast::is_changed);
133 connect(ui->thtEdit, &QDateEdit::dateChanged, this, &EditYeast::is_changed);
134 connect(ui->cellsEdit, &QDoubleSpinBox::textChanged, this, &EditYeast::is_changed);
135 connect(ui->sta1Edit, &QCheckBox::stateChanged, this, &EditYeast::is_changed);
136 connect(ui->bacteriaEdit, &QCheckBox::stateChanged, this, &EditYeast::is_changed);
137 connect(ui->harvesttopEdit, &QCheckBox::stateChanged, this, &EditYeast::is_changed);
138 connect(ui->harvesttimeEdit, &QSpinBox::textChanged, this, &EditYeast::is_changed);
139 connect(ui->pitchtempEdit, &QDoubleSpinBox::textChanged, this, &EditYeast::is_changed);
140 connect(ui->pofEdit, &QCheckBox::stateChanged, this, &EditYeast::is_changed);
141 connect(ui->yeastbankEdit, &QLineEdit::textChanged, this, &EditYeast::is_changed);
142 connect(ui->grhlloEdit, &QSpinBox::textChanged, this, &EditYeast::is_changed);
143 connect(ui->sgloEdit, &QDoubleSpinBox::textChanged, this, &EditYeast::is_changed);
144 connect(ui->grhlhiEdit, &QSpinBox::textChanged, this, &EditYeast::is_changed);
145 connect(ui->sghiEdit, &QDoubleSpinBox::textChanged, this, &EditYeast::is_changed);
146
147 ui->saveButton->setEnabled(false);
148 ui->deleteButton->setEnabled((ui->inventoryEdit->value() == 0 && id >= 0) ? true:false);
149 }
150
151
152 EditYeast::~EditYeast()
153 {
154 qDebug() << "EditYeast done";
155 delete ui;
156 emit entry_changed();
157 }
158
159
160 void EditYeast::SetForm(int form)
161 {
162 if (form == 0) {
163 /*
164 * Liquid yeast
165 */
166 ui->costLabel->setText(tr("Price per pack:"));
167 ui->inventoryLabel->setText(tr("Inventory Packs:"));
168 ui->cellsLabel->setText(tr("Billion cells/pack:"));
169 ui->inventoryEdit->setDecimals(0);
170 ui->inventoryEdit->setSingleStep(1.0);
171 ui->valueEdit->setValue(ui->inventoryEdit->value() * ui->costEdit->value());
172 } else if (form == 1 || form == 6) {
173 /*
174 * Dry or dried yeast
175 */
176 ui->costLabel->setText(tr("Price per Kg:"));
177 ui->inventoryLabel->setText(tr("Inventory gram:"));
178 ui->cellsLabel->setText(tr("Billion cells/gram:"));
179 ui->inventoryEdit->setDecimals(1);
180 ui->inventoryEdit->setSingleStep(0.5);
181 ui->valueEdit->setValue((ui->inventoryEdit->value() / 1000) * ui->costEdit->value());
182 } else {
183 /*
184 * All others
185 */
186 ui->costLabel->setText(tr("Price per litre:"));
187 ui->inventoryLabel->setText(tr("Inventory ml:"));
188 ui->cellsLabel->setText(tr("Billion cells/ml:"));
189 ui->inventoryEdit->setDecimals(1);
190 ui->inventoryEdit->setSingleStep(0.5);
191 ui->valueEdit->setValue((ui->inventoryEdit->value() / 1000) * ui->costEdit->value());
192 }
193 }
194
195
196
197 /*
198 * Window header, mark any change with '**'
199 */
200 void EditYeast::WindowTitle()
201 {
202 QString txt;
203
204 if (this->recno < 0) {
205 txt = QString(tr("BMSapp - Add new hop"));
206 } else {
207 txt = QString(tr("BMSapp - Edit hop %1").arg(this->recno));
208 }
209
210 if (this->textIsChanged) {
211 txt.append((QString(" **")));
212 }
213 setWindowTitle(txt);
214 }
215
216
217 void EditYeast::on_saveButton_clicked()
218 {
219 QSqlQuery query;
220
221 /* If there are errors in the form, show a message and do "return;" */
222 if (ui->nameEdit->text().length() < 2) {
223 QMessageBox::warning(this, tr("Edit Yeast"), tr("Name empty or too short."));
224 return;
225 }
226 if (ui->laboratoryEdit->text().length() < 2) {
227 QMessageBox::warning(this, tr("Edit Yeast"), tr("Laboratory empty or too short."));
228 return;
229 }
230
231 if (this->textIsChanged) {
232 if (this->recno == -1) {
233 query.prepare("INSERT INTO inventory_yeasts SET name=:name, type=:type, form=:form, "
234 "laboratory=:laboratory, product_id=:productid, min_temperature=:templo, max_temperature=:temphi, "
235 "flocculation=:floc, attenuation=:att, tolerance=:tolerance, notes=:notes, best_for=:bestfor, "
236 "max_reuse=:reuse, inventory=:inventory, cost=:cost, production_date=:prod, tht_date=:tht, "
237 "cells=:cells, sta1=:sta1, bacteria=:bacteria, harvest_top=:harvesttop, harvest_time=:harvesttime, "
238 "pitch_temperature=:pitchtemp, pofpos=:pof, short_desc=:yeastbank, gr_hl_lo=:grhllo, sg_lo=:sglo, "
239 "gr_hl_hi=:grhlhi, sg_hi=:sghi, uuid = :uuid");
240 } else {
241 query.prepare("UPDATE inventory_yeasts SET name=:name, type=:type, form=:form, "
242 "laboratory=:laboratory, product_id=:productid, min_temperature=:templo, max_temperature=:temphi, "
243 "flocculation=:floc, attenuation=:att, tolerance=:tolerance, notes=:notes, best_for=:bestfor, "
244 "max_reuse=:reuse, inventory=:inventory, cost=:cost, production_date=:prod, tht_date=:tht, "
245 "cells=:cells, sta1=:sta1, bacteria=:bacteria, harvest_top=:harvesttop, harvest_time=:harvesttime, "
246 "pitch_temperature=:pitchtemp, pofpos=:pof, short_desc=:yeastbank, gr_hl_lo=:grhllo, sg_lo=:sglo, "
247 "gr_hl_hi=:grhlhi, sg_hi=:sghi WHERE record = :recno");
248 }
249 query.bindValue(":name", ui->nameEdit->text());
250 query.bindValue(":type", ui->typeEdit->currentIndex());
251 query.bindValue(":form", ui->formEdit->currentIndex());
252 query.bindValue(":laboratory", ui->laboratoryEdit->text());
253 query.bindValue(":productid", ui->productidEdit->text());
254 query.bindValue(":templo", QString("%1").arg(ui->temploEdit->value(), 2, 'f', 1, '0'));
255 query.bindValue(":temphi", QString("%1").arg(ui->temphiEdit->value(), 2, 'f', 1, '0'));
256 query.bindValue(":floc", ui->flocEdit->currentIndex());
257 query.bindValue(":att", QString("%1").arg(ui->attEdit->value(), 2, 'f', 1, '0'));
258 query.bindValue(":tolerance", QString("%1").arg(ui->toleranceEdit->value(), 2, 'f', 1, '0'));
259 query.bindValue(":notes", ui->notesEdit->toPlainText());
260 query.bindValue(":bestfor", ui->bestforEdit->toPlainText());
261 query.bindValue(":reuse", ui->reuseEdit->value());
262 if (ui->formEdit->currentIndex() == 0)
263 query.bindValue(":inventory", QString("%1").arg(ui->inventoryEdit->value(), 5, 'f', 4, '0'));
264 else
265 query.bindValue(":inventory", QString("%1").arg(ui->inventoryEdit->value() / 1000, 5, 'f', 4, '0'));
266 query.bindValue(":cost", QString("%1").arg(ui->costEdit->value(), 3, 'f', 2, '0'));
267 /* Uses https://www.qtcentre.org/threads/17295-How-to-put-empty-value-in-QDateEdit */
268 query.bindValue(":prod", ui->prodEdit->nullDate());
269 query.bindValue(":tht", ui->thtEdit->nullDate());
270 query.bindValue(":cells", QString("%1").arg(ui->cellsEdit->value() * 1000000000, 1, 'f', 0, '0'));
271 query.bindValue(":sta1", ui->sta1Edit->isChecked() ? 1:0);
272 query.bindValue(":bacteria", ui->bacteriaEdit->isChecked() ? 1:0);
273 query.bindValue(":harvesttop", ui->harvesttopEdit->isChecked() ? 1:0);
274 query.bindValue(":harvesttime", ui->harvesttimeEdit->value());
275 query.bindValue(":pitchtemp", QString("%1").arg(ui->pitchtempEdit->value(), 2, 'f', 1, '0'));
276 query.bindValue(":pof", ui->pofEdit->isChecked() ? 1:0);
277 query.bindValue(":yeastbank", ui->yeastbankEdit->text());
278 query.bindValue(":grhllo", QString("%1").arg(ui->grhlloEdit->value(), 2, 'f', 1, '0'));
279 query.bindValue(":sglo", ui->sgloEdit->value());
280 query.bindValue(":grhlhi", QString("%1").arg(ui->grhlhiEdit->value(), 2, 'f', 1, '0'));
281 query.bindValue(":sghi", ui->sghiEdit->value());
282 if (this->recno == -1) {
283 query.bindValue(":uuid", QUuid::createUuid().toString().mid(1, 36));
284 } else {
285 query.bindValue(":recno", this->recno);
286 }
287 query.exec();
288 if (query.lastError().isValid()) {
289 qDebug() << "EditYeast" << query.lastError();
290 QMessageBox::warning(this, tr("Database error"),
291 tr("MySQL error: %1\n%2\n%3")
292 .arg(query.lastError().nativeErrorCode())
293 .arg(query.lastError().driverText())
294 .arg(query.lastError().databaseText()));
295 } else {
296 qDebug() << "EditYeast Saved";
297 }
298 }
299
300 ui->saveButton->setEnabled(false);
301 this->textIsChanged = false;
302 WindowTitle();
303 }
304
305
306 void EditYeast::on_deleteButton_clicked()
307 {
308 QSqlQuery query;
309
310 query.prepare("DELETE FROM inventory_yeasts WHERE record = :recno");
311 query.bindValue(":recno", this->recno);
312 query.exec();
313 if (query.lastError().isValid()) {
314 qDebug() << "EditYeast" << query.lastError();
315 QMessageBox::warning(this, tr("Database error"),
316 tr("MySQL error: %1\n%2\n%3")
317 .arg(query.lastError().nativeErrorCode())
318 .arg(query.lastError().driverText())
319 .arg(query.lastError().databaseText()));
320 } else {
321 qDebug() << "EditYeast Deleted" << this->recno;
322 }
323
324 this->close();
325 this->setResult(1);
326 }
327
328
329 void EditYeast::is_changed()
330 {
331 SetForm(ui->formEdit->currentIndex());
332 ui->saveButton->setEnabled(true);
333 ui->deleteButton->setEnabled((ui->inventoryEdit->value() == 0 && this->recno >= 0) ? true:false);
334 this->textIsChanged = true;
335 WindowTitle();
336 }
337
338
339 void EditYeast::temp_lo_changed()
340 {
341 if (ui->temploEdit->value() > (ui->temphiEdit->value() - 1))
342 ui->temphiEdit->setValue(ui->temploEdit->value() + 1);
343 is_changed();
344 }
345
346
347 void EditYeast::temp_hi_changed()
348 {
349 if (ui->temphiEdit->value() < (ui->temploEdit->value() + 1))
350 ui->temploEdit->setValue(ui->temphiEdit->value() - 1);
351 is_changed();
352 }
353
354
355 void EditYeast::on_quitButton_clicked()
356 {
357 if (this->textIsChanged) {
358 int rc = QMessageBox::warning(this, tr("Yeast changed"), tr("The yeast has been modified\n Save changes?"),
359 QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel, QMessageBox::Save);
360 switch (rc) {
361 case QMessageBox::Save:
362 on_saveButton_clicked();
363 break; /* Saved and then Quit */
364 case QMessageBox::Discard:
365 break; /* Quit without Save */
366 case QMessageBox::Cancel:
367 return; /* Return to the editor page */
368 }
369 }
370
371 this->close();
372 this->setResult(1);
373 }

mercurial