|
1 /** |
|
2 * EditRecipe.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 "EditRecipe.h" |
|
18 #include "../ui/ui_EditRecipe.h" |
|
19 #include "MainWindow.h" |
|
20 |
|
21 |
|
22 EditRecipe::EditRecipe(int id, QWidget *parent) : QDialog(parent), ui(new Ui::EditRecipe) |
|
23 { |
|
24 QSqlQuery query; |
|
25 |
|
26 qDebug() << "EditRecipe record:" << id; |
|
27 ui->setupUi(this); |
|
28 this->recno = id; |
|
29 |
|
30 WindowTitle(); |
|
31 |
|
32 ui->typeEdit->addItem(tr("Extract")); |
|
33 ui->typeEdit->addItem(tr("Partial Mash")); |
|
34 ui->typeEdit->addItem(tr("All Grain")); |
|
35 |
|
36 if (id >= 0) { |
|
37 query.prepare("SELECT * FROM recipes WHERE record = :recno"); |
|
38 query.bindValue(":recno", id); |
|
39 query.exec(); |
|
40 query.next(); |
|
41 |
|
42 ui->lockedEdit->setChecked(query.value(2).toInt() ? true:false); |
|
43 // 3 st_name |
|
44 // 4 st_letter |
|
45 // 5 st_guide |
|
46 // 6 st_category |
|
47 // 7 st_category_number |
|
48 // 8 st_type |
|
49 // 9 st_og_min |
|
50 // 10 st_og_max |
|
51 // 11 st_fg_min |
|
52 // 12 st_fg_max |
|
53 // 13 st_ibu_min |
|
54 // 14 st_ibu_max |
|
55 // 15 st_color_min |
|
56 // 16 st_color_max |
|
57 // 17 st_carb_min |
|
58 // 18 st_carb_max |
|
59 // 19 st_abv_min |
|
60 // 20 st_abv_max |
|
61 ui->nameEdit->setText(query.value(21).toString()); |
|
62 ui->notesEdit->setPlainText(query.value(22).toString()); |
|
63 ui->typeEdit->setCurrentIndex(query.value(23).toInt()); |
|
64 ui->batch_sizeEdit->setValue(query.value(24).toDouble()); |
|
65 ui->boil_sizeEdit->setValue(query.value(25).toDouble()); |
|
66 ui->boil_timeEdit->setValue(query.value(26).toInt()); |
|
67 ui->efficiencyEdit->setValue(query.value(27).toDouble()); |
|
68 // 28 est_og |
|
69 // 29 est_fg |
|
70 // 30 est_abv |
|
71 // 31 est_color |
|
72 // 32 color_method |
|
73 // 33 est_ibu |
|
74 // 34 ibu_method |
|
75 // 35 est_carb |
|
76 // 36 sparge_temp |
|
77 // 37 sparge_ph |
|
78 // 38 sparge_volume |
|
79 // 39 sparge_source |
|
80 // 40 sparge_acid_type |
|
81 // 41 sparge_acid_perc |
|
82 // 42 sparge_acid_amount |
|
83 // 43 mash_ph |
|
84 // 44 mash_name |
|
85 // 45 calc_acid |
|
86 // 46 w1_name |
|
87 // 47 w1_amount |
|
88 // 48 w1_calcium |
|
89 // 49 w1_sulfate |
|
90 // 50 w1_chloride |
|
91 // 51 w1_sodium |
|
92 // 52 w1_magnesium |
|
93 // 53 w1_total_alkalinity |
|
94 // 54 w1_ph |
|
95 // 55 w1_cost |
|
96 // 56 w2_name |
|
97 // 57 w2_amount |
|
98 // 58 w2_calcium |
|
99 // 59 w2_sulfate |
|
100 // 60 w2_chloride |
|
101 // 61 w2_sodium |
|
102 // 62 w2_magnesium |
|
103 // 63 w2_total_alkalinity |
|
104 // 64 w2_ph |
|
105 // 65 w2_cost |
|
106 // 66 wg_amount |
|
107 // 67 wg_calcium |
|
108 // 68 wg_sulfate |
|
109 // 69 wg_chloride |
|
110 // 70 wg_sodium |
|
111 // 71 wg_magnesium |
|
112 // 72 wg_total_alkalinity |
|
113 // 73 wg_ph |
|
114 // 74 wb_calcium |
|
115 // 75 wb_sulfate |
|
116 // 76 wb_chloride |
|
117 // 77 wb_sodium |
|
118 // 78 wb_magnesium |
|
119 // 79 wb_total_alkalinity |
|
120 // 80 wb_ph |
|
121 // 81 wa_acid_name |
|
122 // 82 wa_acid_perc |
|
123 // 83 wa_base_name |
|
124 // 84 json_fermentables |
|
125 // 85 json_hops |
|
126 // 86 json_miscs |
|
127 // 87 json_yeasts |
|
128 // 88 json_mashs |
|
129 } else { |
|
130 /* Set some defaults */ |
|
131 } |
|
132 |
|
133 connect(ui->lockedEdit, &QCheckBox::stateChanged, this, &EditRecipe::is_changed); |
|
134 |
|
135 connect(ui->nameEdit, &QLineEdit::textChanged, this, &EditRecipe::is_changed); |
|
136 connect(ui->notesEdit, SIGNAL(textChanged()), this, SLOT(is_changed())); |
|
137 connect(ui->typeEdit, &QComboBox::currentTextChanged, this, &EditRecipe::is_changed); |
|
138 connect(ui->batch_sizeEdit, &QDoubleSpinBox::textChanged, this, &EditRecipe::is_changed); |
|
139 connect(ui->boil_sizeEdit, &QDoubleSpinBox::textChanged, this, &EditRecipe::is_changed); |
|
140 connect(ui->boil_timeEdit, &QSpinBox::textChanged, this, &EditRecipe::is_changed); |
|
141 connect(ui->efficiencyEdit, &QDoubleSpinBox::textChanged, this, &EditRecipe::is_changed); |
|
142 |
|
143 ui->saveButton->setEnabled(false); |
|
144 ui->deleteButton->setEnabled((id >= 0) ? true:false); |
|
145 } |
|
146 |
|
147 |
|
148 EditRecipe::~EditRecipe() |
|
149 { |
|
150 qDebug() << "EditRecipe done"; |
|
151 delete ui; |
|
152 emit entry_changed(); |
|
153 } |
|
154 |
|
155 |
|
156 /* |
|
157 * Window header, mark any change with '**' |
|
158 */ |
|
159 void EditRecipe::WindowTitle() |
|
160 { |
|
161 QString txt; |
|
162 |
|
163 if (this->recno < 0) { |
|
164 txt = QString(tr("BMSapp - Add new recipe")); |
|
165 } else { |
|
166 txt = QString(tr("BMSapp - Edit recipe %1").arg(this->recno)); |
|
167 } |
|
168 |
|
169 if (this->textIsChanged) { |
|
170 txt.append((QString(" **"))); |
|
171 } |
|
172 setWindowTitle(txt); |
|
173 } |
|
174 |
|
175 |
|
176 void EditRecipe::on_saveButton_clicked() |
|
177 { |
|
178 QSqlQuery query; |
|
179 |
|
180 /* If there are errors in the form, show a message and do "return;" */ |
|
181 // if (ui->nameEdit->text().length() < 2) { |
|
182 // QMessageBox::warning(this, tr("Edit Recipe"), tr("Name empty or too short.")); |
|
183 // return; |
|
184 // } |
|
185 |
|
186 if (this->textIsChanged) { |
|
187 if (this->recno == -1) { |
|
188 query.prepare("INSERT INTO recipes SET name=:name, " |
|
189 "uuid = :uuid"); |
|
190 } else { |
|
191 query.prepare("UPDATE recipes SET name=:name " |
|
192 " WHERE record = :recno"); |
|
193 } |
|
194 //query.bindValue(":name", ui->nameEdit->text()); |
|
195 //query.bindValue(":notes", ui->notesEdit->toPlainText()); |
|
196 if (this->recno == -1) { |
|
197 query.bindValue(":uuid", QUuid::createUuid().toString().mid(1, 36)); |
|
198 } else { |
|
199 query.bindValue(":recno", this->recno); |
|
200 } |
|
201 query.exec(); |
|
202 if (query.lastError().isValid()) { |
|
203 qDebug() << "EditRecipe" << query.lastError(); |
|
204 QMessageBox::warning(this, tr("Database error"), |
|
205 tr("MySQL error: %1\n%2\n%3") |
|
206 .arg(query.lastError().nativeErrorCode()) |
|
207 .arg(query.lastError().driverText()) |
|
208 .arg(query.lastError().databaseText())); |
|
209 } else { |
|
210 qDebug() << "EditRecipe Saved"; |
|
211 } |
|
212 } |
|
213 |
|
214 ui->saveButton->setEnabled(false); |
|
215 this->textIsChanged = false; |
|
216 WindowTitle(); |
|
217 } |
|
218 |
|
219 |
|
220 void EditRecipe::on_deleteButton_clicked() |
|
221 { |
|
222 QSqlQuery query; |
|
223 |
|
224 query.prepare("DELETE FROM recipes WHERE record = :recno"); |
|
225 query.bindValue(":recno", this->recno); |
|
226 query.exec(); |
|
227 if (query.lastError().isValid()) { |
|
228 qDebug() << "EditRecipe" << 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() << "EditRecipe Deleted" << this->recno; |
|
236 } |
|
237 |
|
238 this->close(); |
|
239 this->setResult(1); |
|
240 } |
|
241 |
|
242 |
|
243 void EditRecipe::is_changed() |
|
244 { |
|
245 ui->saveButton->setEnabled(true); |
|
246 ui->deleteButton->setEnabled((this->recno >= 0) ? true:false); |
|
247 this->textIsChanged = true; |
|
248 WindowTitle(); |
|
249 } |
|
250 |
|
251 |
|
252 void EditRecipe::time_changed() |
|
253 { |
|
254 is_changed(); |
|
255 } |
|
256 |
|
257 |
|
258 void EditRecipe::on_quitButton_clicked() |
|
259 { |
|
260 if (this->textIsChanged) { |
|
261 int rc = QMessageBox::warning(this, tr("Recipe changed"), tr("The ingredient has been modified. Save changes?"), |
|
262 QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel, QMessageBox::Save); |
|
263 switch (rc) { |
|
264 case QMessageBox::Save: |
|
265 on_saveButton_clicked(); |
|
266 break; /* Saved and then Quit */ |
|
267 case QMessageBox::Discard: |
|
268 break; /* Quit without Save */ |
|
269 case QMessageBox::Cancel: |
|
270 return; /* Return to the editor page */ |
|
271 } |
|
272 } |
|
273 |
|
274 this->close(); |
|
275 this->setResult(1); |
|
276 } |