|
1 /** |
|
2 * EditFermentable.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 "EditFermentable.h" |
|
18 #include "../ui/ui_EditFermentable.h" |
|
19 #include "bmsapp.h" |
|
20 |
|
21 |
|
22 EditFermentable::EditFermentable(int id, QWidget *parent) : QDialog(parent), ui(new Ui::EditFermentable) |
|
23 { |
|
24 QSqlQuery query; |
|
25 |
|
26 qDebug() << "EditFermentable record:" << id; |
|
27 ui->setupUi(this); |
|
28 this->recno = id; |
|
29 |
|
30 WindowTitle(); |
|
31 |
|
32 ui->typeEdit->addItem(tr("Grain")); |
|
33 ui->typeEdit->addItem(tr("Sugar")); |
|
34 ui->typeEdit->addItem(tr("Extract")); |
|
35 ui->typeEdit->addItem(tr("Dry extract")); |
|
36 ui->typeEdit->addItem(tr("Adjunct")); |
|
37 |
|
38 ui->graintypeEdit->addItem(tr("Base")); |
|
39 ui->graintypeEdit->addItem(tr("Roast")); |
|
40 ui->graintypeEdit->addItem(tr("Crystal")); |
|
41 ui->graintypeEdit->addItem(tr("Kilned")); |
|
42 ui->graintypeEdit->addItem(tr("Sour Malt")); |
|
43 ui->graintypeEdit->addItem(tr("Special")); |
|
44 ui->graintypeEdit->addItem(tr("No malt")); |
|
45 |
|
46 ui->addedEdit->addItem(tr("Mash")); |
|
47 ui->addedEdit->addItem(tr("Boil")); |
|
48 ui->addedEdit->addItem(tr("Fermentation")); |
|
49 ui->addedEdit->addItem(tr("Lagering")); |
|
50 ui->addedEdit->addItem(tr("Bottle")); |
|
51 ui->addedEdit->addItem(tr("Kegs")); |
|
52 |
|
53 if (id >= 0) { |
|
54 query.prepare("SELECT * FROM inventory_fermentables WHERE record = :recno"); |
|
55 query.bindValue(":recno", id); |
|
56 query.exec(); |
|
57 query.next(); |
|
58 |
|
59 ui->nameEdit->setText(query.value(1).toString()); |
|
60 ui->notesEdit->setPlainText(query.value(8).toString()); |
|
61 ui->originEdit->setText(query.value(6).toString()); |
|
62 ui->supplierEdit->setText(query.value(7).toString()); |
|
63 ui->typeEdit->setCurrentIndex(query.value(2).toInt()); |
|
64 ui->graintypeEdit->setCurrentIndex(query.value(20).toInt()); |
|
65 ui->maxinbatchEdit->setValue(query.value(14).toDouble()); |
|
66 ui->mashEdit->setChecked(query.value(16).toInt() ? true:false); |
|
67 ui->addafterEdit->setChecked(query.value(5).toInt() ? true:false); |
|
68 ui->addedEdit->setCurrentIndex(query.value(17).toInt()); |
|
69 ui->alwaysEdit->setChecked(query.value(5).toInt() ? true:false); |
|
70 ui->inventoryEdit->setValue(query.value(21).toDouble()); |
|
71 ui->costEdit->setValue(query.value(22).toDouble()); |
|
72 ui->valueEdit->setValue(query.value(21).toDouble() * query.value(22).toDouble()); |
|
73 ui->yieldEdit->setValue(query.value(3).toDouble()); |
|
74 ui->colorEdit->setValue(query.value(4).toDouble()); |
|
75 ui->moistureEdit->setValue(query.value(10).toDouble()); |
|
76 ui->coarseEdit->setValue(query.value(9).toDouble()); |
|
77 ui->proteinEdit->setValue(query.value(12).toDouble()); |
|
78 ui->dissolvedEdit->setValue(query.value(13).toDouble()); |
|
79 ui->diastaticEdit->setValue(Utils::lintner_to_kolbach(query.value(11).toDouble())); |
|
80 ui->diphEdit->setValue(query.value(18).toDouble()); |
|
81 ui->acidphEdit->setValue(query.value(19).toDouble()); |
|
82 if (query.value(23).toString().length() == 10) { |
|
83 ui->prodEdit->setDate(query.value(23).toDate()); |
|
84 } else { |
|
85 ui->prodEdit->clear(); |
|
86 } |
|
87 if (query.value(24).toString().length() == 10) { |
|
88 ui->thtEdit->setDate(query.value(24).toDate()); |
|
89 } else { |
|
90 ui->thtEdit->clear(); |
|
91 } |
|
92 } else { |
|
93 /* Set some defaults */ |
|
94 ui->typeEdit->setCurrentIndex(0); |
|
95 ui->graintypeEdit->setCurrentIndex(0); |
|
96 ui->maxinbatchEdit->setValue(100); |
|
97 ui->mashEdit->setChecked(true); |
|
98 ui->addedEdit->setCurrentIndex(0); |
|
99 ui->yieldEdit->setValue(80); |
|
100 ui->colorEdit->setValue(3); |
|
101 ui->coarseEdit->setValue(3); |
|
102 ui->moistureEdit->setValue(4); |
|
103 } |
|
104 connect(ui->nameEdit, &QLineEdit::textChanged, this, &EditFermentable::is_changed); |
|
105 connect(ui->notesEdit, SIGNAL(textChanged()), this, SLOT(is_changed())); |
|
106 connect(ui->originEdit, &QLineEdit::textChanged, this, &EditFermentable::is_changed); |
|
107 connect(ui->supplierEdit, &QLineEdit::textChanged, this, &EditFermentable::is_changed); |
|
108 connect(ui->typeEdit, &QComboBox::currentTextChanged, this, &EditFermentable::is_changed); |
|
109 connect(ui->graintypeEdit, &QComboBox::currentTextChanged, this, &EditFermentable::is_changed); |
|
110 connect(ui->maxinbatchEdit, &QDoubleSpinBox::textChanged, this, &EditFermentable::is_changed); |
|
111 connect(ui->mashEdit, &QCheckBox::stateChanged, this, &EditFermentable::is_changed); |
|
112 connect(ui->addafterEdit, &QCheckBox::stateChanged, this, &EditFermentable::is_changed); |
|
113 connect(ui->addedEdit, &QComboBox::currentTextChanged, this, &EditFermentable::is_changed); |
|
114 connect(ui->alwaysEdit, &QCheckBox::stateChanged, this, &EditFermentable::is_changed); |
|
115 connect(ui->inventoryEdit, &QDoubleSpinBox::textChanged, this, &EditFermentable::is_changed); |
|
116 connect(ui->costEdit, &QDoubleSpinBox::textChanged, this, &EditFermentable::is_changed); |
|
117 connect(ui->yieldEdit, &QDoubleSpinBox::textChanged, this, &EditFermentable::is_changed); |
|
118 connect(ui->colorEdit, &QDoubleSpinBox::textChanged, this, &EditFermentable::is_changed); |
|
119 connect(ui->moistureEdit, &QDoubleSpinBox::textChanged, this, &EditFermentable::is_changed); |
|
120 connect(ui->coarseEdit, &QDoubleSpinBox::textChanged, this, &EditFermentable::is_changed); |
|
121 connect(ui->proteinEdit, &QDoubleSpinBox::textChanged, this, &EditFermentable::is_changed); |
|
122 connect(ui->dissolvedEdit, &QDoubleSpinBox::textChanged, this, &EditFermentable::is_changed); |
|
123 connect(ui->diastaticEdit, &QDoubleSpinBox::textChanged, this, &EditFermentable::is_changed); |
|
124 connect(ui->diphEdit, &QDoubleSpinBox::textChanged, this, &EditFermentable::is_changed); |
|
125 connect(ui->acidphEdit, &QDoubleSpinBox::textChanged, this, &EditFermentable::is_changed); |
|
126 connect(ui->prodEdit, &QDateEdit::dateChanged, this, &EditFermentable::is_changed); |
|
127 connect(ui->thtEdit, &QDateEdit::dateChanged, this, &EditFermentable::is_changed); |
|
128 |
|
129 ui->saveButton->setEnabled(false); |
|
130 ui->deleteButton->setEnabled((ui->inventoryEdit->value() == 0 && id >= 0) ? true:false); |
|
131 } |
|
132 |
|
133 |
|
134 EditFermentable::~EditFermentable() |
|
135 { |
|
136 qDebug() << "EditFermentable done"; |
|
137 delete ui; |
|
138 emit entry_changed(); |
|
139 } |
|
140 |
|
141 |
|
142 /* |
|
143 * Window header, mark any change with '**' |
|
144 */ |
|
145 void EditFermentable::WindowTitle() |
|
146 { |
|
147 QString txt; |
|
148 |
|
149 if (this->recno < 0) { |
|
150 txt = QString(tr("BMSapp - Add new fermentable")); |
|
151 } else { |
|
152 txt = QString(tr("BMSapp - Edit fermentable %1").arg(this->recno)); |
|
153 } |
|
154 |
|
155 if (this->textIsChanged) { |
|
156 txt.append((QString(" **"))); |
|
157 } |
|
158 setWindowTitle(txt); |
|
159 } |
|
160 |
|
161 |
|
162 void EditFermentable::on_saveButton_clicked() |
|
163 { |
|
164 QSqlQuery query; |
|
165 |
|
166 /* If there are errors in the form, show a message and do "return;" */ |
|
167 if (ui->nameEdit->text().length() < 2) { |
|
168 QMessageBox::warning(this, tr("Edit Fermentable"), tr("Name empty or too short.")); |
|
169 return; |
|
170 } |
|
171 if (ui->originEdit->text().length() < 2) { |
|
172 QMessageBox::warning(this, tr("Edit Fermentable"), tr("Origin empty or too short.")); |
|
173 return; |
|
174 } |
|
175 if (ui->supplierEdit->text().length() < 2) { |
|
176 QMessageBox::warning(this, tr("Edit Fermentable"), tr("Supplier empty or too short.")); |
|
177 return; |
|
178 } |
|
179 |
|
180 if (this->textIsChanged) { |
|
181 if (this->recno == -1) { |
|
182 query.prepare("INSERT INTO inventory_fermentables SET name=:name, type=:type, yield=:yield, color=:color, " |
|
183 "add_after_boil=:addafter, origin=:origin, supplier=:supplier, notes=:notes, coarse_fine_diff=:coarse, " |
|
184 "moisture=:moisture, diastatic_power=:diastatic, protein=:protein, dissolved_protein=:dissolved, " |
|
185 "max_in_batch=:maxinbatch, recommend_mash=:mash, added=:added, always_on_stock=:always, di_ph=:diph, " |
|
186 "acid_to_ph_57=:acidph, graintype=:graintype, inventory=:inventory, cost=:cost, production_date=:prod, " |
|
187 "tht_date=:tht, uuid = :uuid"); |
|
188 } else { |
|
189 query.prepare("UPDATE inventory_fermentables SET name=:name, type=:type, yield=:yield, color=:color, " |
|
190 "add_after_boil=:addafter, origin=:origin, supplier=:supplier, notes=:notes, coarse_fine_diff=:coarse, " |
|
191 "moisture=:moisture, diastatic_power=:diastatic, protein=:protein, dissolved_protein=:dissolved, " |
|
192 "max_in_batch=:maxinbatch, recommend_mash=:mash, added=:added, always_on_stock=:always, di_ph=:diph, " |
|
193 "acid_to_ph_57=:acidph, graintype=:graintype, inventory=:inventory, cost=:cost, production_date=:prod, " |
|
194 "tht_date=:tht WHERE record = :recno"); |
|
195 } |
|
196 query.bindValue(":name", ui->nameEdit->text()); |
|
197 query.bindValue(":type", ui->typeEdit->currentIndex()); |
|
198 query.bindValue(":yield", QString("%1").arg(ui->yieldEdit->value(), 2, 'f', 1, '0')); |
|
199 query.bindValue(":color", QString("%1").arg(ui->colorEdit->value(), 1, 'f', 0, '0')); |
|
200 query.bindValue(":addafter", ui->addafterEdit->isChecked() ? 1:0); |
|
201 query.bindValue(":origin", ui->originEdit->text()); |
|
202 query.bindValue(":supplier", ui->supplierEdit->text()); |
|
203 query.bindValue(":notes", ui->notesEdit->toPlainText()); |
|
204 query.bindValue(":coarse", QString("%1").arg(ui->coarseEdit->value(), 2, 'f', 1, '0')); |
|
205 query.bindValue(":moisture", QString("%1").arg(ui->moistureEdit->value(), 2, 'f', 1, '0')); |
|
206 query.bindValue(":diastatic", Utils::kolbach_to_lintner(ui->diastaticEdit->value())); |
|
207 query.bindValue(":protein", QString("%1").arg(ui->proteinEdit->value(), 2, 'f', 1, '0')); |
|
208 query.bindValue(":dissolved", QString("%1").arg(ui->dissolvedEdit->value(), 2, 'f', 1, '0')); |
|
209 query.bindValue(":maxinbatch", QString("%1").arg(ui->maxinbatchEdit->value(), 2, 'f', 1, '0')); |
|
210 query.bindValue(":mash", ui->mashEdit->isChecked() ? 1:0); |
|
211 query.bindValue(":added", ui->addedEdit->currentIndex()); |
|
212 query.bindValue(":always", ui->alwaysEdit->isChecked() ? 1:0); |
|
213 query.bindValue(":diph", QString("%1").arg(ui->diphEdit->value(), 3, 'f', 2, '0')); |
|
214 query.bindValue(":acidph", QString("%1").arg(ui->acidphEdit->value(), 6, 'f', 5, '0')); |
|
215 query.bindValue(":graintype", ui->graintypeEdit->currentIndex()); |
|
216 query.bindValue(":inventory", QString("%1").arg(ui->inventoryEdit->value(), 4, 'f', 3, '0')); |
|
217 query.bindValue(":cost", QString("%1").arg(ui->costEdit->value(), 3, 'f', 2, '0')); |
|
218 /* Uses https://www.qtcentre.org/threads/17295-How-to-put-empty-value-in-QDateEdit */ |
|
219 query.bindValue(":prod", ui->prodEdit->nullDate()); |
|
220 query.bindValue(":tht", ui->thtEdit->nullDate()); |
|
221 if (this->recno == -1) { |
|
222 query.bindValue(":uuid", QUuid::createUuid().toString().mid(1, 36)); |
|
223 } else { |
|
224 query.bindValue(":recno", this->recno); |
|
225 } |
|
226 query.exec(); |
|
227 if (query.lastError().isValid()) { |
|
228 qDebug() << "EditFermentable" << query.lastError(); |
|
229 QMessageBox::warning(this, tr("Database error"), |
|
230 tr("MySQL error: %1\n%2\n%3") |
|
231 .arg(query.lastError().nativeErrorCode()) |
|
232 .arg(query.lastError().driverText()) |
|
233 .arg(query.lastError().databaseText())); |
|
234 } else { |
|
235 qDebug() << "EditFermentable Saved"; |
|
236 } |
|
237 } |
|
238 |
|
239 ui->saveButton->setEnabled(false); |
|
240 this->textIsChanged = false; |
|
241 WindowTitle(); |
|
242 } |
|
243 |
|
244 |
|
245 void EditFermentable::on_deleteButton_clicked() |
|
246 { |
|
247 QSqlQuery query; |
|
248 |
|
249 query.prepare("DELETE FROM inventory_fermentables WHERE record = :recno"); |
|
250 query.bindValue(":recno", this->recno); |
|
251 query.exec(); |
|
252 if (query.lastError().isValid()) { |
|
253 qDebug() << "EditFermentable" << query.lastError(); |
|
254 QMessageBox::warning(this, tr("Database error"), |
|
255 tr("MySQL error: %1\n%2\n%3") |
|
256 .arg(query.lastError().nativeErrorCode()) |
|
257 .arg(query.lastError().driverText()) |
|
258 .arg(query.lastError().databaseText())); |
|
259 } else { |
|
260 qDebug() << "EditFermentable Deleted" << this->recno; |
|
261 } |
|
262 |
|
263 this->close(); |
|
264 this->setResult(1); |
|
265 } |
|
266 |
|
267 |
|
268 void EditFermentable::is_changed() |
|
269 { |
|
270 ui->valueEdit->setValue(ui->inventoryEdit->value() * ui->costEdit->value()); |
|
271 ui->saveButton->setEnabled(true); |
|
272 ui->deleteButton->setEnabled((ui->inventoryEdit->value() == 0 && this->recno >= 0) ? true:false); |
|
273 this->textIsChanged = true; |
|
274 WindowTitle(); |
|
275 } |
|
276 |
|
277 |
|
278 void EditFermentable::on_quitButton_clicked() |
|
279 { |
|
280 if (this->textIsChanged) { |
|
281 int rc = QMessageBox::warning(this, tr("Fermentable changed"), tr("The fermentable has been modified\n Save changes?"), |
|
282 QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel, QMessageBox::Save); |
|
283 switch (rc) { |
|
284 case QMessageBox::Save: |
|
285 on_saveButton_clicked(); |
|
286 break; /* Saved and then Quit */ |
|
287 case QMessageBox::Discard: |
|
288 break; /* Quit without Save */ |
|
289 case QMessageBox::Cancel: |
|
290 return; /* Return to the editor page */ |
|
291 } |
|
292 } |
|
293 |
|
294 this->close(); |
|
295 this->setResult(1); |
|
296 } |