|
1 /** |
|
2 * EditEquipment.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 "EditEquipment.h" |
|
18 #include "../ui/ui_EditEquipment.h" |
|
19 #include "bmsapp.h" |
|
20 |
|
21 |
|
22 EditEquipment::EditEquipment(int id, QWidget *parent) : QDialog(parent), ui(new Ui::EditEquipment) |
|
23 { |
|
24 QSqlQuery query, query2; |
|
25 |
|
26 qDebug() << "EditEquipment record:" << id; |
|
27 ui->setupUi(this); |
|
28 this->recno = id; |
|
29 |
|
30 WindowTitle(); |
|
31 |
|
32 ui->tun_materialEdit->addItem(tr("Stainless Steel")); |
|
33 ui->tun_materialEdit->addItem(tr("Aluminium")); |
|
34 ui->tun_materialEdit->addItem(tr("Plastics")); |
|
35 ui->tun_materialEdit->addItem(tr("Copper")); |
|
36 |
|
37 if (id >= 0) { |
|
38 query.prepare("SELECT * FROM inventory_equipments WHERE record = :recno"); |
|
39 query.bindValue(":recno", id); |
|
40 query.exec(); |
|
41 query.next(); |
|
42 |
|
43 ui->nameEdit->setText(query.value(1).toString()); |
|
44 ui->boil_sizeEdit->setValue(query.value(2).toDouble()); |
|
45 ui->batch_sizeEdit->setValue(query.value(3).toDouble()); |
|
46 ui->tun_volumeEdit->setValue(query.value(4).toDouble()); |
|
47 ui->tun_weightEdit->setValue(query.value(5).toDouble()); |
|
48 ui->tun_specific_heatEdit->setValue(query.value(6).toDouble()); |
|
49 ui->tun_materialEdit->setCurrentIndex(query.value(7).toInt()); |
|
50 ui->tun_heightEdit->setValue(query.value(8).toDouble() * 100.0); |
|
51 ui->top_up_waterEdit->setValue(query.value(9).toDouble()); |
|
52 ui->chiller_lossEdit->setValue(query.value(10).toDouble()); |
|
53 ui->evap_rateEdit->setValue(query.value(11).toDouble()); |
|
54 ui->boil_timeEdit->setValue(query.value(12).toDouble()); |
|
55 ui->calcboilEdit->setChecked(query.value(13).toInt() ? true:false); |
|
56 ui->top_up_kettleEdit->setValue(query.value(14).toDouble()); |
|
57 ui->hopfactorEdit->setValue(query.value(15).toDouble()); |
|
58 ui->notesEdit->setPlainText(query.value(16).toString()); |
|
59 ui->lauter_volumeEdit->setValue(query.value(17).toDouble()); |
|
60 ui->lauter_heightEdit->setValue(query.value(18).toDouble() * 100.0); |
|
61 ui->lauter_deadspaceEdit->setValue(query.value(19).toDouble()); |
|
62 ui->kettle_volumeEdit->setValue(query.value(20).toDouble()); |
|
63 ui->kettle_heightEdit->setValue(query.value(21).toDouble() * 100.0); |
|
64 ui->mash_volumeEdit->setValue(query.value(22).toDouble()); |
|
65 ui->mash_maxEdit->setValue(query.value(23).toDouble()); |
|
66 ui->efficiencyEdit->setValue(query.value(24).toDouble()); |
|
67 /* |
|
68 * Now we have loaded this record, check if this equipment is |
|
69 * being used by a product. If so, make the name field read-only. |
|
70 */ |
|
71 query2.prepare("SELECT eq_name FROM products WHERE eq_name=:name"); |
|
72 query2.bindValue(":name", query.value(1).toString()); |
|
73 query2.exec(); |
|
74 inuse = query2.size(); |
|
75 qDebug() << "in use" << inuse; |
|
76 ui->nameEdit->setReadOnly(inuse > 0); |
|
77 } else { |
|
78 /* Set some defaults */ |
|
79 ui->boil_sizeEdit->setValue(18); |
|
80 ui->batch_sizeEdit->setValue(15.3); |
|
81 ui->tun_volumeEdit->setValue(20); |
|
82 ui->tun_weightEdit->setValue(2); |
|
83 ui->tun_specific_heatEdit->setValue(0.11); |
|
84 ui->tun_materialEdit->setCurrentIndex(0); |
|
85 ui->tun_heightEdit->setValue(20); |
|
86 ui->top_up_waterEdit->setValue(0); |
|
87 ui->chiller_lossEdit->setValue(0.5); |
|
88 ui->evap_rateEdit->setValue(1.8); |
|
89 ui->boil_timeEdit->setValue(90); |
|
90 ui->calcboilEdit->setChecked(true); |
|
91 ui->top_up_kettleEdit->setValue(0); |
|
92 ui->hopfactorEdit->setValue(100); |
|
93 ui->lauter_volumeEdit->setValue(20); |
|
94 ui->lauter_heightEdit->setValue(20); |
|
95 ui->lauter_deadspaceEdit->setValue(0.5); |
|
96 ui->kettle_volumeEdit->setValue(20); |
|
97 ui->kettle_heightEdit->setValue(20); |
|
98 ui->mash_volumeEdit->setValue(18); |
|
99 ui->mash_maxEdit->setValue(6); |
|
100 ui->efficiencyEdit->setValue(75); |
|
101 inuse = 0; |
|
102 } |
|
103 connect(ui->nameEdit, &QLineEdit::textChanged, this, &EditEquipment::is_changed); |
|
104 connect(ui->boil_sizeEdit, &QDoubleSpinBox::textChanged, this, &EditEquipment::is_changed); |
|
105 connect(ui->batch_sizeEdit, &QDoubleSpinBox::textChanged, this, &EditEquipment::is_changed); |
|
106 connect(ui->tun_volumeEdit, &QDoubleSpinBox::textChanged, this, &EditEquipment::is_changed); |
|
107 connect(ui->tun_weightEdit, &QDoubleSpinBox::textChanged, this, &EditEquipment::is_changed); |
|
108 connect(ui->tun_materialEdit, &QComboBox::currentTextChanged, this, &EditEquipment::material_changed); |
|
109 connect(ui->tun_heightEdit, &QDoubleSpinBox::textChanged, this, &EditEquipment::is_changed); |
|
110 connect(ui->top_up_waterEdit, &QDoubleSpinBox::textChanged, this, &EditEquipment::is_changed); |
|
111 connect(ui->chiller_lossEdit, &QDoubleSpinBox::textChanged, this, &EditEquipment::is_changed); |
|
112 connect(ui->evap_rateEdit, &QDoubleSpinBox::textChanged, this, &EditEquipment::is_changed); |
|
113 connect(ui->boil_timeEdit, &QDoubleSpinBox::textChanged, this, &EditEquipment::is_changed); |
|
114 connect(ui->calcboilEdit, &QCheckBox::stateChanged, this, &EditEquipment::is_changed); |
|
115 connect(ui->top_up_kettleEdit, &QDoubleSpinBox::textChanged, this, &EditEquipment::is_changed); |
|
116 connect(ui->hopfactorEdit, &QDoubleSpinBox::textChanged, this, &EditEquipment::is_changed); |
|
117 connect(ui->notesEdit, SIGNAL(textChanged()), this, SLOT(is_changed())); |
|
118 connect(ui->lauter_volumeEdit, &QDoubleSpinBox::textChanged, this, &EditEquipment::is_changed); |
|
119 connect(ui->lauter_heightEdit, &QDoubleSpinBox::textChanged, this, &EditEquipment::is_changed); |
|
120 connect(ui->lauter_deadspaceEdit, &QDoubleSpinBox::textChanged, this, &EditEquipment::is_changed); |
|
121 connect(ui->kettle_volumeEdit, &QDoubleSpinBox::textChanged, this, &EditEquipment::is_changed); |
|
122 connect(ui->kettle_heightEdit, &QDoubleSpinBox::textChanged, this, &EditEquipment::is_changed); |
|
123 connect(ui->mash_volumeEdit, &QDoubleSpinBox::textChanged, this, &EditEquipment::is_changed); |
|
124 connect(ui->mash_maxEdit, &QDoubleSpinBox::textChanged, this, &EditEquipment::is_changed); |
|
125 connect(ui->efficiencyEdit, &QDoubleSpinBox::textChanged, this, &EditEquipment::is_changed); |
|
126 |
|
127 calcBatchVolume(); |
|
128 ui->saveButton->setEnabled(false); |
|
129 ui->deleteButton->setEnabled((inuse == 0 && id >= 0) ? true:false); |
|
130 } |
|
131 |
|
132 |
|
133 EditEquipment::~EditEquipment() |
|
134 { |
|
135 qDebug() << "EditEquipment done"; |
|
136 delete ui; |
|
137 emit entry_changed(); |
|
138 } |
|
139 |
|
140 |
|
141 /* |
|
142 * Window header, mark any change with '**' |
|
143 */ |
|
144 void EditEquipment::WindowTitle() |
|
145 { |
|
146 QString txt; |
|
147 |
|
148 if (this->recno < 0) { |
|
149 txt = QString(tr("BMSapp - Add new equipment")); |
|
150 } else { |
|
151 txt = QString(tr("BMSapp - Edit equipment %1").arg(this->recno)); |
|
152 } |
|
153 |
|
154 if (this->textIsChanged) { |
|
155 txt.append((QString(" **"))); |
|
156 } |
|
157 setWindowTitle(txt); |
|
158 } |
|
159 |
|
160 |
|
161 void EditEquipment::on_saveButton_clicked() |
|
162 { |
|
163 QSqlQuery query; |
|
164 |
|
165 /* If there are errors in the form, show a message and do "return;" */ |
|
166 if (ui->nameEdit->text().length() < 2) { |
|
167 QMessageBox::warning(this, tr("Edit Equipment"), tr("Name empty or too short.")); |
|
168 return; |
|
169 } |
|
170 |
|
171 if (this->textIsChanged) { |
|
172 if (this->recno == -1) { |
|
173 query.prepare("INSERT INTO inventory_equipments SET name=:name, boil_size=:boil_size, " |
|
174 "batch_size=:batch_size, tun_volume=:tun_volume, tun_weight=:tun_weight, " |
|
175 "tun_specific_heat=:tun_specific_heat, tun_material=:tun_material, tun_height=:tun_height, " |
|
176 "top_up_water=:top_up_water, trub_chiller_loss=:chiller_loss, evap_rate=:evap_rate, " |
|
177 "boil_time=:boil_time, calc_boil_volume=:calcboil, top_up_kettle=:top_up_kettle, " |
|
178 "hop_utilization=:hopfactor, notes=:notes, lauter_volume=:lauter_volume, " |
|
179 "lauter_height=:lauter_height, lauter_deadspace=:lauter_deadspace, kettle_volume=:kettle_volume, " |
|
180 "kettle_height=:kettle_height, mash_volume=:mash_volume, mash_max=:mash_max, " |
|
181 "efficiency=:efficiency, uuid=:uuid"); |
|
182 } else { |
|
183 query.prepare("UPDATE inventory_equipments SET name=:name, boil_size=:boil_size, " |
|
184 "batch_size=:batch_size, tun_volume=:tun_volume, tun_weight=:tun_weight, " |
|
185 "tun_specific_heat=:tun_specific_heat, tun_material=:tun_material, tun_height=:tun_height, " |
|
186 "top_up_water=:top_up_water, trub_chiller_loss=:chiller_loss, evap_rate=:evap_rate, " |
|
187 "boil_time=:boil_time, calc_boil_volume=:calcboil, top_up_kettle=:top_up_kettle, " |
|
188 "hop_utilization=:hopfactor, notes=:notes, lauter_volume=:lauter_volume, " |
|
189 "lauter_height=:lauter_height, lauter_deadspace=:lauter_deadspace, kettle_volume=:kettle_volume, " |
|
190 "kettle_height=:kettle_height, mash_volume=:mash_volume, mash_max=:mash_max, " |
|
191 "efficiency=:efficiency WHERE record=:recno"); |
|
192 } |
|
193 query.bindValue(":name", ui->nameEdit->text()); |
|
194 query.bindValue(":boil_size", QString("%1").arg(ui->boil_sizeEdit->value(), 2, 'f', 1, '0')); |
|
195 query.bindValue(":batch_size", QString("%1").arg(ui->batch_sizeEdit->value(), 3, 'f', 2, '0')); |
|
196 query.bindValue(":tun_volume", QString("%1").arg(ui->tun_volumeEdit->value(), 2, 'f', 1, '0')); |
|
197 query.bindValue(":tun_weight", QString("%1").arg(ui->tun_weightEdit->value(), 2, 'f', 1, '0')); |
|
198 query.bindValue(":tun_specific_heat", QString("%1").arg(ui->tun_specific_heatEdit->value(), 4, 'f', 3, '0')); |
|
199 query.bindValue(":tun_material", ui->tun_materialEdit->currentIndex()); |
|
200 query.bindValue(":tun_height", QString("%1").arg(ui->tun_heightEdit->value() / 100, 4, 'f', 3, '0')); |
|
201 query.bindValue(":top_up_water", QString("%1").arg(ui->top_up_waterEdit->value(), 2, 'f', 1, '0')); |
|
202 query.bindValue(":chiller_loss", QString("%1").arg(ui->chiller_lossEdit->value(), 2, 'f', 1, '0')); |
|
203 query.bindValue(":evap_rate", QString("%1").arg(ui->evap_rateEdit->value(), 3, 'f', 2, '0')); |
|
204 query.bindValue(":boil_time", QString("%1").arg(ui->boil_timeEdit->value(), 1, 'f', 0, '0')); |
|
205 query.bindValue(":calcboil", ui->calcboilEdit->isChecked() ? 1:0); |
|
206 query.bindValue(":top_up_kettle", QString("%1").arg(ui->top_up_kettleEdit->value(), 2, 'f', 1, '0')); |
|
207 query.bindValue(":hopfactor", QString("%1").arg(ui->hopfactorEdit->value(), 1, 'f', 0, '0')); |
|
208 query.bindValue(":notes", ui->notesEdit->toPlainText()); |
|
209 query.bindValue(":lauter_volume", QString("%1").arg(ui->lauter_volumeEdit->value(), 2, 'f', 1, '0')); |
|
210 query.bindValue(":lauter_height", QString("%1").arg(ui->lauter_heightEdit->value() / 100, 4, 'f', 3, '0')); |
|
211 query.bindValue(":lauter_deadspace", QString("%1").arg(ui->lauter_deadspaceEdit->value(), 2, 'f', 1, '0')); |
|
212 query.bindValue(":kettle_volume", QString("%1").arg(ui->kettle_volumeEdit->value(), 2, 'f', 1, '0')); |
|
213 query.bindValue(":kettle_height", QString("%1").arg(ui->kettle_heightEdit->value() / 100, 4, 'f', 3, '0')); |
|
214 query.bindValue(":mash_volume", QString("%1").arg(ui->mash_volumeEdit->value(), 2, 'f', 1, '0')); |
|
215 query.bindValue(":mash_max", QString("%1").arg(ui->mash_maxEdit->value(), 2, 'f', 1, '0')); |
|
216 query.bindValue(":efficiency", QString("%1").arg(ui->efficiencyEdit->value(), 2, 'f', 1, '0')); |
|
217 if (this->recno == -1) { |
|
218 query.bindValue(":uuid", QUuid::createUuid().toString().mid(1, 36)); |
|
219 } else { |
|
220 query.bindValue(":recno", this->recno); |
|
221 } |
|
222 query.exec(); |
|
223 if (query.lastError().isValid()) { |
|
224 qDebug() << "EditEquipment" << query.lastError(); |
|
225 QMessageBox::warning(this, tr("Database error"), |
|
226 tr("MySQL error: %1\n%2\n%3") |
|
227 .arg(query.lastError().nativeErrorCode()) |
|
228 .arg(query.lastError().driverText()) |
|
229 .arg(query.lastError().databaseText())); |
|
230 } else { |
|
231 qDebug() << "EditEquipment Saved"; |
|
232 } |
|
233 } |
|
234 |
|
235 ui->saveButton->setEnabled(false); |
|
236 this->textIsChanged = false; |
|
237 WindowTitle(); |
|
238 } |
|
239 |
|
240 |
|
241 void EditEquipment::on_deleteButton_clicked() |
|
242 { |
|
243 QSqlQuery query; |
|
244 |
|
245 query.prepare("DELETE FROM inventory_equipments WHERE record = :recno"); |
|
246 query.bindValue(":recno", this->recno); |
|
247 query.exec(); |
|
248 if (query.lastError().isValid()) { |
|
249 qDebug() << "EditEquipment" << query.lastError(); |
|
250 QMessageBox::warning(this, tr("Database error"), |
|
251 tr("MySQL error: %1\n%2\n%3") |
|
252 .arg(query.lastError().nativeErrorCode()) |
|
253 .arg(query.lastError().driverText()) |
|
254 .arg(query.lastError().databaseText())); |
|
255 } else { |
|
256 qDebug() << "EditEquipment Deleted" << this->recno; |
|
257 } |
|
258 |
|
259 this->close(); |
|
260 this->setResult(1); |
|
261 } |
|
262 |
|
263 |
|
264 void EditEquipment::calcBatchVolume() |
|
265 { |
|
266 double batch = ui->boil_sizeEdit->value() - (ui->evap_rateEdit->value() * ui->boil_timeEdit->value() / 60) + ui->top_up_kettleEdit->value(); |
|
267 double fermenter = round(((batch / 1.04) + ui->top_up_waterEdit->value() - ui->chiller_lossEdit->value()) * 100) / 100.0; |
|
268 batch = round(batch * 100) / 100.0; |
|
269 ui->batch_sizeEdit->setValue(batch); |
|
270 ui->vol_fermenterEdit->setValue(fermenter); |
|
271 } |
|
272 |
|
273 |
|
274 void EditEquipment::is_changed() |
|
275 { |
|
276 calcBatchVolume(); |
|
277 ui->saveButton->setEnabled(true); |
|
278 ui->deleteButton->setEnabled((inuse == 0 && this->recno >= 0) ? true:false); |
|
279 this->textIsChanged = true; |
|
280 WindowTitle(); |
|
281 } |
|
282 |
|
283 |
|
284 void EditEquipment::material_changed() |
|
285 { |
|
286 switch (ui->tun_materialEdit->currentIndex()) { |
|
287 case 0: ui->tun_specific_heatEdit->setValue(0.11); break; |
|
288 case 1: ui->tun_specific_heatEdit->setValue(0.22); break; |
|
289 case 2: ui->tun_specific_heatEdit->setValue(0.46); break; |
|
290 case 3: ui->tun_specific_heatEdit->setValue(0.092); break; |
|
291 } |
|
292 is_changed(); |
|
293 } |
|
294 |
|
295 |
|
296 void EditEquipment::on_quitButton_clicked() |
|
297 { |
|
298 if (this->textIsChanged) { |
|
299 int rc = QMessageBox::warning(this, tr("Equipment changed"), tr("This equipment has been modified\n Save changes?"), |
|
300 QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel, QMessageBox::Save); |
|
301 switch (rc) { |
|
302 case QMessageBox::Save: |
|
303 on_saveButton_clicked(); |
|
304 break; /* Saved and then Quit */ |
|
305 case QMessageBox::Discard: |
|
306 break; /* Quit without Save */ |
|
307 case QMessageBox::Cancel: |
|
308 return; /* Return to the editor page */ |
|
309 } |
|
310 } |
|
311 |
|
312 this->close(); |
|
313 this->setResult(1); |
|
314 } |