|
1 /** |
|
2 * EditProfileStyle.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 "EditProfileStyle.h" |
|
18 #include "../ui/ui_EditProfileStyle.h" |
|
19 #include "bmsapp.h" |
|
20 |
|
21 |
|
22 EditProfileStyle::EditProfileStyle(int id, QWidget *parent) : QDialog(parent), ui(new Ui::EditProfileStyle) |
|
23 { |
|
24 QSqlQuery query; |
|
25 |
|
26 qDebug() << "EditProfileStyle 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("Mead")); |
|
35 ui->typeEdit->addItem(tr("Wheat")); |
|
36 ui->typeEdit->addItem(tr("Mixed")); |
|
37 ui->typeEdit->addItem(tr("Cider")); |
|
38 |
|
39 if (id >= 0) { |
|
40 query.prepare("SELECT * FROM profile_styles WHERE record = :recno"); |
|
41 query.bindValue(":recno", id); |
|
42 query.exec(); |
|
43 query.next(); |
|
44 |
|
45 ui->styleEdit->setText(query.value(1).toString()); |
|
46 ui->catEdit->setText(query.value(2).toString()); |
|
47 ui->catnrEdit->setValue(query.value(3).toInt()); |
|
48 ui->groupEdit->setText(query.value(4).toString()); |
|
49 ui->guideEdit->setText(query.value(5).toString()); |
|
50 ui->typeEdit->setCurrentIndex(query.value(6).toInt()); |
|
51 ui->ogminEdit->setValue(query.value(7).toDouble()); |
|
52 ui->ogmaxEdit->setValue(query.value(8).toDouble()); |
|
53 ui->fgminEdit->setValue(query.value(9).toDouble()); |
|
54 ui->fgmaxEdit->setValue(query.value(10).toDouble()); |
|
55 ui->ibuminEdit->setValue(query.value(11).toDouble()); |
|
56 ui->ibumaxEdit->setValue(query.value(12).toDouble()); |
|
57 ui->ebcminEdit->setValue(query.value(13).toDouble()); |
|
58 ui->ebcmaxEdit->setValue(query.value(14).toDouble()); |
|
59 ui->co2minEdit->setValue(query.value(15).toDouble()); |
|
60 ui->co2maxEdit->setValue(query.value(16).toDouble()); |
|
61 ui->abvminEdit->setValue(query.value(17).toDouble()); |
|
62 ui->abvmaxEdit->setValue(query.value(18).toDouble()); |
|
63 ui->notesEdit->setPlainText(query.value(19).toString()); |
|
64 ui->profileEdit->setPlainText(query.value(20).toString()); |
|
65 ui->ingredientsEdit->setPlainText(query.value(21).toString()); |
|
66 ui->examplesEdit->setPlainText(query.value(22).toString()); |
|
67 } else { |
|
68 /* Set some defaults */ |
|
69 ui->catnrEdit->setValue(0); |
|
70 ui->guideEdit->setText("BKG 2019"); |
|
71 ui->typeEdit->setCurrentIndex(0); |
|
72 ui->ogminEdit->setValue(1.030); |
|
73 ui->ogmaxEdit->setValue(1.060); |
|
74 ui->fgminEdit->setValue(1.005); |
|
75 ui->fgmaxEdit->setValue(1.010); |
|
76 ui->ibuminEdit->setValue(20); |
|
77 ui->ibumaxEdit->setValue(30); |
|
78 ui->ebcminEdit->setValue(52); |
|
79 ui->ebcmaxEdit->setValue(79); |
|
80 ui->co2minEdit->setValue(2.0); |
|
81 ui->co2maxEdit->setValue(2.5); |
|
82 ui->abvminEdit->setValue(4.0); |
|
83 ui->abvmaxEdit->setValue(5.0); |
|
84 } |
|
85 connect(ui->styleEdit, &QLineEdit::textChanged, this, &EditProfileStyle::is_changed); |
|
86 connect(ui->catEdit, &QLineEdit::textChanged, this, &EditProfileStyle::is_changed); |
|
87 connect(ui->catnrEdit, &QSpinBox::textChanged, this, &EditProfileStyle::is_changed); |
|
88 connect(ui->groupEdit, &QLineEdit::textChanged, this, &EditProfileStyle::is_changed); |
|
89 connect(ui->guideEdit, &QLineEdit::textChanged, this, &EditProfileStyle::is_changed); |
|
90 connect(ui->typeEdit, &QComboBox::currentTextChanged, this, &EditProfileStyle::is_changed); |
|
91 connect(ui->ogminEdit, &QDoubleSpinBox::textChanged, this, &EditProfileStyle::ogmin_changed); |
|
92 connect(ui->ogmaxEdit, &QDoubleSpinBox::textChanged, this, &EditProfileStyle::ogmax_changed); |
|
93 connect(ui->fgminEdit, &QDoubleSpinBox::textChanged, this, &EditProfileStyle::fgmin_changed); |
|
94 connect(ui->fgmaxEdit, &QDoubleSpinBox::textChanged, this, &EditProfileStyle::fgmax_changed); |
|
95 connect(ui->ibuminEdit, &QSpinBox::textChanged, this, &EditProfileStyle::ibumin_changed); |
|
96 connect(ui->ibumaxEdit, &QSpinBox::textChanged, this, &EditProfileStyle::ibumax_changed); |
|
97 connect(ui->ebcminEdit, &QSpinBox::textChanged, this, &EditProfileStyle::ebcmin_changed); |
|
98 connect(ui->ebcmaxEdit, &QSpinBox::textChanged, this, &EditProfileStyle::ebcmax_changed); |
|
99 connect(ui->co2minEdit, &QDoubleSpinBox::textChanged, this, &EditProfileStyle::co2min_changed); |
|
100 connect(ui->co2maxEdit, &QDoubleSpinBox::textChanged, this, &EditProfileStyle::co2max_changed); |
|
101 connect(ui->abvminEdit, &QDoubleSpinBox::textChanged, this, &EditProfileStyle::abvmin_changed); |
|
102 connect(ui->abvmaxEdit, &QDoubleSpinBox::textChanged, this, &EditProfileStyle::abvmax_changed); |
|
103 connect(ui->notesEdit, SIGNAL(textChanged()), this, SLOT(is_changed())); |
|
104 connect(ui->profileEdit, SIGNAL(textChanged()), this, SLOT(is_changed())); |
|
105 connect(ui->ingredientsEdit, SIGNAL(textChanged()), this, SLOT(is_changed())); |
|
106 connect(ui->examplesEdit, SIGNAL(textChanged()), this, SLOT(is_changed())); |
|
107 |
|
108 ui->saveButton->setEnabled(false); |
|
109 ui->deleteButton->setEnabled((id >= 0) ? true:false); |
|
110 } |
|
111 |
|
112 |
|
113 EditProfileStyle::~EditProfileStyle() |
|
114 { |
|
115 qDebug() << "EditProfileStyle done"; |
|
116 delete ui; |
|
117 emit entry_changed(); |
|
118 } |
|
119 |
|
120 |
|
121 /* |
|
122 * Window header, mark any change with '**' |
|
123 */ |
|
124 void EditProfileStyle::WindowTitle() |
|
125 { |
|
126 QString txt; |
|
127 |
|
128 if (this->recno < 0) { |
|
129 txt = QString(tr("BMSapp - Add new brewing water")); |
|
130 } else { |
|
131 txt = QString(tr("BMSapp - Edit brewing water %1").arg(this->recno)); |
|
132 } |
|
133 |
|
134 if (this->textIsChanged) { |
|
135 txt.append((QString(" **"))); |
|
136 } |
|
137 setWindowTitle(txt); |
|
138 } |
|
139 |
|
140 |
|
141 void EditProfileStyle::on_saveButton_clicked() |
|
142 { |
|
143 QSqlQuery query; |
|
144 |
|
145 /* If there are errors in the form, show a message and do "return;" */ |
|
146 if (ui->styleEdit->text().length() < 2) { |
|
147 QMessageBox::warning(this, tr("Edit Style"), tr("Beer style is empty or too short.")); |
|
148 return; |
|
149 } |
|
150 |
|
151 if (this->textIsChanged) { |
|
152 if (this->recno == -1) { |
|
153 query.prepare("INSERT INTO profile_styles SET name=:name, category=:category, " |
|
154 "category_number=:catnr, style_letter=:group, style_guide=:guide, type=:type, " |
|
155 "og_min=:og_min, og_max=:og_max, fg_min=:fg_min, fg_max=:fg_max, ibu_min=:ibu_min, " |
|
156 "ibu_max=:ibu_max, color_min=:ebc_min, color_max=:ebc_max, carb_min=:co2_min, " |
|
157 "carb_max=:co2_max, abv_min=:abv_min, abv_max=:abv_max, notes=:notes, " |
|
158 "profile=:profile, ingredients=:ingredients, examples=:examples, uuid=:uuid"); |
|
159 } else { |
|
160 query.prepare("UPDATE profile_styles SET name=:name, category=:category, " |
|
161 "category_number=:catnr, style_letter=:group, style_guide=:guide, type=:type, " |
|
162 "og_min=:og_min, og_max=:og_max, fg_min=:fg_min, fg_max=:fg_max, ibu_min=:ibu_min, " |
|
163 "ibu_max=:ibu_max, color_min=:ebc_min, color_max=:ebc_max, carb_min=:co2_min, " |
|
164 "carb_max=:co2_max, abv_min=:abv_min, abv_max=:abv_max, notes=:notes, " |
|
165 "profile=:profile, ingredients=:ingredients, examples=:examples WHERE record=:recno"); |
|
166 } |
|
167 query.bindValue(":name", ui->styleEdit->text()); |
|
168 query.bindValue(":category", ui->catEdit->text()); |
|
169 query.bindValue(":catnr", QString("%1").arg(ui->catnrEdit->value())); |
|
170 query.bindValue(":group", ui->groupEdit->text()); |
|
171 query.bindValue(":guide", ui->guideEdit->text()); |
|
172 query.bindValue(":type", ui->typeEdit->currentIndex()); |
|
173 query.bindValue(":og_min", QString("%1").arg(ui->ogminEdit->value(), 4, 'f', 3, '0')); |
|
174 query.bindValue(":og_max", QString("%1").arg(ui->ogmaxEdit->value(), 4, 'f', 3, '0')); |
|
175 query.bindValue(":fg_min", QString("%1").arg(ui->fgminEdit->value(), 4, 'f', 3, '0')); |
|
176 query.bindValue(":fg_max", QString("%1").arg(ui->fgmaxEdit->value(), 4, 'f', 3, '0')); |
|
177 query.bindValue(":ibu_min", QString("%1").arg(ui->ibuminEdit->value(), 1, 'f', 0, '0')); |
|
178 query.bindValue(":ibu_max", QString("%1").arg(ui->ibumaxEdit->value(), 1, 'f', 0, '0')); |
|
179 query.bindValue(":ebc_min", QString("%1").arg(ui->ebcminEdit->value(), 1, 'f', 0, '0')); |
|
180 query.bindValue(":ebc_max", QString("%1").arg(ui->ebcmaxEdit->value(), 1, 'f', 0, '0')); |
|
181 query.bindValue(":co2_min", QString("%1").arg(ui->co2minEdit->value(), 2, 'f', 1, '0')); |
|
182 query.bindValue(":co2_max", QString("%1").arg(ui->co2maxEdit->value(), 2, 'f', 1, '0')); |
|
183 query.bindValue(":abv_min", QString("%1").arg(ui->abvminEdit->value(), 2, 'f', 1, '0')); |
|
184 query.bindValue(":abv_max", QString("%1").arg(ui->abvmaxEdit->value(), 2, 'f', 1, '0')); |
|
185 query.bindValue(":notes", ui->notesEdit->toPlainText()); |
|
186 query.bindValue(":profile", ui->profileEdit->toPlainText()); |
|
187 query.bindValue(":ingredients", ui->ingredientsEdit->toPlainText()); |
|
188 query.bindValue(":examples", ui->examplesEdit->toPlainText()); |
|
189 if (this->recno == -1) { |
|
190 query.bindValue(":uuid", QUuid::createUuid().toString().mid(1, 36)); |
|
191 } else { |
|
192 query.bindValue(":recno", this->recno); |
|
193 } |
|
194 query.exec(); |
|
195 if (query.lastError().isValid()) { |
|
196 qDebug() << "EditProfileStyle" << query.lastError(); |
|
197 QMessageBox::warning(this, tr("Database error"), |
|
198 tr("MySQL error: %1\n%2\n%3") |
|
199 .arg(query.lastError().nativeErrorCode()) |
|
200 .arg(query.lastError().driverText()) |
|
201 .arg(query.lastError().databaseText())); |
|
202 } else { |
|
203 qDebug() << "EditProfileStyle Saved"; |
|
204 } |
|
205 } |
|
206 |
|
207 ui->saveButton->setEnabled(false); |
|
208 this->textIsChanged = false; |
|
209 WindowTitle(); |
|
210 } |
|
211 |
|
212 |
|
213 void EditProfileStyle::on_deleteButton_clicked() |
|
214 { |
|
215 QSqlQuery query; |
|
216 |
|
217 query.prepare("DELETE FROM profile_styles WHERE record = :recno"); |
|
218 query.bindValue(":recno", this->recno); |
|
219 query.exec(); |
|
220 if (query.lastError().isValid()) { |
|
221 qDebug() << "EditProfileStyle" << query.lastError(); |
|
222 QMessageBox::warning(this, tr("Database error"), |
|
223 tr("MySQL error: %1\n%2\n%3") |
|
224 .arg(query.lastError().nativeErrorCode()) |
|
225 .arg(query.lastError().driverText()) |
|
226 .arg(query.lastError().databaseText())); |
|
227 } else { |
|
228 qDebug() << "EditProfileStyle Deleted" << this->recno; |
|
229 } |
|
230 |
|
231 this->close(); |
|
232 this->setResult(1); |
|
233 } |
|
234 |
|
235 |
|
236 void EditProfileStyle::is_changed() |
|
237 { |
|
238 ui->saveButton->setEnabled(true); |
|
239 ui->deleteButton->setEnabled((this->recno >= 0) ? true:false); |
|
240 this->textIsChanged = true; |
|
241 WindowTitle(); |
|
242 } |
|
243 |
|
244 |
|
245 void EditProfileStyle::ogmin_changed() |
|
246 { |
|
247 if (ui->ogminEdit->value() > (ui->ogmaxEdit->value() - 0.001)) |
|
248 ui->ogmaxEdit->setValue(ui->ogminEdit->value() + 0.001); |
|
249 is_changed(); |
|
250 } |
|
251 |
|
252 |
|
253 void EditProfileStyle::ogmax_changed() |
|
254 { |
|
255 if (ui->ogmaxEdit->value() < (ui->ogminEdit->value() + 0.001)) |
|
256 ui->ogminEdit->setValue(ui->ogmaxEdit->value() - 0.001); |
|
257 is_changed(); |
|
258 } |
|
259 |
|
260 |
|
261 void EditProfileStyle::fgmin_changed() |
|
262 { |
|
263 if (ui->fgminEdit->value() > (ui->fgmaxEdit->value() - 0.001)) |
|
264 ui->fgmaxEdit->setValue(ui->fgminEdit->value() + 0.001); |
|
265 is_changed(); |
|
266 } |
|
267 |
|
268 |
|
269 void EditProfileStyle::fgmax_changed() |
|
270 { |
|
271 if (ui->fgmaxEdit->value() < (ui->fgminEdit->value() + 0.001)) |
|
272 ui->fgminEdit->setValue(ui->fgmaxEdit->value() - 0.001); |
|
273 is_changed(); |
|
274 } |
|
275 |
|
276 |
|
277 void EditProfileStyle::ibumin_changed() |
|
278 { |
|
279 if (ui->ibuminEdit->value() > (ui->ibumaxEdit->value() - 1)) |
|
280 ui->ibumaxEdit->setValue(ui->ibuminEdit->value() + 1); |
|
281 is_changed(); |
|
282 } |
|
283 |
|
284 |
|
285 void EditProfileStyle::ibumax_changed() |
|
286 { |
|
287 if (ui->ibumaxEdit->value() < (ui->ibuminEdit->value() + 1)) |
|
288 ui->ibuminEdit->setValue(ui->ibumaxEdit->value() - 1); |
|
289 is_changed(); |
|
290 } |
|
291 |
|
292 |
|
293 void EditProfileStyle::ebcmin_changed() |
|
294 { |
|
295 if (ui->ebcminEdit->value() > (ui->ebcmaxEdit->value() - 1)) |
|
296 ui->ebcmaxEdit->setValue(ui->ebcminEdit->value() + 1); |
|
297 is_changed(); |
|
298 } |
|
299 |
|
300 |
|
301 void EditProfileStyle::ebcmax_changed() |
|
302 { |
|
303 if (ui->ebcmaxEdit->value() < (ui->ebcminEdit->value() + 1)) |
|
304 ui->ebcminEdit->setValue(ui->ebcmaxEdit->value() - 1); |
|
305 is_changed(); |
|
306 } |
|
307 |
|
308 |
|
309 void EditProfileStyle::co2min_changed() |
|
310 { |
|
311 if (ui->co2minEdit->value() > (ui->co2maxEdit->value() - 0.1)) |
|
312 ui->co2maxEdit->setValue(ui->co2minEdit->value() + 0.1); |
|
313 is_changed(); |
|
314 } |
|
315 |
|
316 |
|
317 void EditProfileStyle::co2max_changed() |
|
318 { |
|
319 if (ui->co2maxEdit->value() < (ui->co2minEdit->value() + 0.1)) |
|
320 ui->co2minEdit->setValue(ui->co2maxEdit->value() - 0.1); |
|
321 is_changed(); |
|
322 } |
|
323 |
|
324 |
|
325 void EditProfileStyle::abvmin_changed() |
|
326 { |
|
327 if (ui->abvminEdit->value() > (ui->abvmaxEdit->value() - 0.1)) |
|
328 ui->abvmaxEdit->setValue(ui->abvminEdit->value() + 0.1); |
|
329 is_changed(); |
|
330 } |
|
331 |
|
332 |
|
333 void EditProfileStyle::abvmax_changed() |
|
334 { |
|
335 if (ui->abvmaxEdit->value() < (ui->abvminEdit->value() + 0.1)) |
|
336 ui->abvminEdit->setValue(ui->abvmaxEdit->value() - 0.1); |
|
337 is_changed(); |
|
338 } |
|
339 |
|
340 |
|
341 void EditProfileStyle::on_quitButton_clicked() |
|
342 { |
|
343 if (this->textIsChanged) { |
|
344 int rc = QMessageBox::warning(this, tr("Style changed"), tr("The style has been modified\n Save changes?"), |
|
345 QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel, QMessageBox::Save); |
|
346 switch (rc) { |
|
347 case QMessageBox::Save: |
|
348 on_saveButton_clicked(); |
|
349 break; /* Saved and then Quit */ |
|
350 case QMessageBox::Discard: |
|
351 break; /* Quit without Save */ |
|
352 case QMessageBox::Cancel: |
|
353 return; /* Return to the editor page */ |
|
354 } |
|
355 } |
|
356 |
|
357 this->close(); |
|
358 this->setResult(1); |
|
359 } |