|
1 /** |
|
2 * EditMisc.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 "EditMisc.h" |
|
18 #include "../ui/ui_EditMisc.h" |
|
19 #include "bmsapp.h" |
|
20 |
|
21 |
|
22 EditMisc::EditMisc(int id, QWidget *parent) : QDialog(parent), ui(new Ui::EditMisc) |
|
23 { |
|
24 QSqlQuery query; |
|
25 |
|
26 qDebug() << "EditMisc record:" << id; |
|
27 ui->setupUi(this); |
|
28 this->recno = id; |
|
29 |
|
30 WindowTitle(); |
|
31 |
|
32 ui->typeEdit->addItem(tr("Spice")); |
|
33 ui->typeEdit->addItem(tr("Herb")); |
|
34 ui->typeEdit->addItem(tr("Flavor")); |
|
35 ui->typeEdit->addItem(tr("Fining")); |
|
36 ui->typeEdit->addItem(tr("Water agent")); |
|
37 ui->typeEdit->addItem(tr("Yeast nutrient")); |
|
38 ui->typeEdit->addItem(tr("Other")); |
|
39 |
|
40 ui->useEdit->addItem(tr("Starter")); |
|
41 ui->useEdit->addItem(tr("Mash")); |
|
42 ui->useEdit->addItem(tr("Boil")); |
|
43 ui->useEdit->addItem(tr("Primary")); |
|
44 ui->useEdit->addItem(tr("Secondary")); |
|
45 ui->useEdit->addItem(tr("Bottling")); |
|
46 |
|
47 if (id >= 0) { |
|
48 query.prepare("SELECT * FROM inventory_miscs WHERE record = :recno"); |
|
49 query.bindValue(":recno", id); |
|
50 query.exec(); |
|
51 query.next(); |
|
52 |
|
53 ui->nameEdit->setText(query.value(1).toString()); |
|
54 ui->typeEdit->setCurrentIndex(query.value(2).toInt()); |
|
55 ui->useEdit->setCurrentIndex(query.value(3).toInt()); |
|
56 ui->timeEdit->setValue(query.value(4).toInt()); |
|
57 ui->isweightEdit->setChecked(query.value(5).toInt() ? true:false); |
|
58 ui->useforEdit->setPlainText(query.value(6).toString()); |
|
59 ui->notesEdit->setPlainText(query.value(7).toString()); |
|
60 ui->alwaysEdit->setChecked(query.value(8).toInt() ? true:false); |
|
61 ui->inventoryEdit->setValue(query.value(9).toDouble()); |
|
62 ui->costEdit->setValue(query.value(10).toDouble()); |
|
63 ui->valueEdit->setValue(query.value(9).toDouble() * query.value(10).toDouble()); |
|
64 if (query.value(11).toString().length() == 10) { |
|
65 ui->prodEdit->setDate(query.value(11).toDate()); |
|
66 } else { |
|
67 ui->prodEdit->clear(); |
|
68 } |
|
69 if (query.value(12).toString().length() == 10) { |
|
70 ui->thtEdit->setDate(query.value(12).toDate()); |
|
71 } else { |
|
72 ui->thtEdit->clear(); |
|
73 } |
|
74 } else { |
|
75 /* Set some defaults */ |
|
76 ui->typeEdit->setCurrentIndex(0); |
|
77 ui->useEdit->setCurrentIndex(0); |
|
78 ui->timeEdit->setValue(0); |
|
79 ui->prodEdit->clear(); |
|
80 ui->thtEdit->clear(); |
|
81 } |
|
82 TimeSet(); |
|
83 connect(ui->nameEdit, &QLineEdit::textChanged, this, &EditMisc::is_changed); |
|
84 connect(ui->typeEdit, &QComboBox::currentTextChanged, this, &EditMisc::is_changed); |
|
85 connect(ui->useEdit, &QComboBox::currentTextChanged, this, &EditMisc::is_changed); |
|
86 connect(ui->timeEdit, &QSpinBox::textChanged, this, &EditMisc::time_changed); |
|
87 connect(ui->isweightEdit, &QCheckBox::stateChanged, this, &EditMisc::is_changed); |
|
88 connect(ui->useforEdit, SIGNAL(textChanged()), this, SLOT(is_changed())); |
|
89 connect(ui->notesEdit, SIGNAL(textChanged()), this, SLOT(is_changed())); |
|
90 connect(ui->alwaysEdit, &QCheckBox::stateChanged, this, &EditMisc::is_changed); |
|
91 connect(ui->inventoryEdit, &QDoubleSpinBox::textChanged, this, &EditMisc::is_changed); |
|
92 connect(ui->costEdit, &QDoubleSpinBox::textChanged, this, &EditMisc::is_changed); |
|
93 connect(ui->prodEdit, &QDateEdit::dateChanged, this, &EditMisc::is_changed); |
|
94 connect(ui->thtEdit, &QDateEdit::dateChanged, this, &EditMisc::is_changed); |
|
95 |
|
96 ui->saveButton->setEnabled(false); |
|
97 ui->deleteButton->setEnabled((ui->inventoryEdit->value() == 0 && id >= 0) ? true:false); |
|
98 } |
|
99 |
|
100 |
|
101 EditMisc::~EditMisc() |
|
102 { |
|
103 qDebug() << "EditMisc done"; |
|
104 delete ui; |
|
105 emit entry_changed(); |
|
106 } |
|
107 |
|
108 |
|
109 /* |
|
110 * Window header, mark any change with '**' |
|
111 */ |
|
112 void EditMisc::WindowTitle() |
|
113 { |
|
114 QString txt; |
|
115 |
|
116 if (this->recno < 0) { |
|
117 txt = QString(tr("BMSapp - Add new misc ingredient")); |
|
118 } else { |
|
119 txt = QString(tr("BMSapp - Edit misc ingredient %1").arg(this->recno)); |
|
120 } |
|
121 |
|
122 if (this->textIsChanged) { |
|
123 txt.append((QString(" **"))); |
|
124 } |
|
125 setWindowTitle(txt); |
|
126 } |
|
127 |
|
128 |
|
129 void EditMisc::on_saveButton_clicked() |
|
130 { |
|
131 QSqlQuery query; |
|
132 |
|
133 /* If there are errors in the form, show a message and do "return;" */ |
|
134 if (ui->nameEdit->text().length() < 2) { |
|
135 QMessageBox::warning(this, tr("Edit Misc"), tr("Name empty or too short.")); |
|
136 return; |
|
137 } |
|
138 |
|
139 if (this->textIsChanged) { |
|
140 if (this->recno == -1) { |
|
141 query.prepare("INSERT INTO inventory_miscs SET name=:name, type=:type, use_use=:use, " |
|
142 "time=:time, amount_is_weight=:isweight, use_for=:usefor, notes=:notes, " |
|
143 "always_on_stock=:always, inventory=:inventory, cost=:cost, production_date=:prod, " |
|
144 "tht_date=:tht, uuid = :uuid"); |
|
145 } else { |
|
146 query.prepare("UPDATE inventory_miscs SET name=:name, type=:type, use_use=:use, " |
|
147 "time=:time, amount_is_weight=:isweight, use_for=:usefor, notes=:notes, " |
|
148 "always_on_stock=:always, inventory=:inventory, cost=:cost, production_date=:prod, " |
|
149 "tht_date=:tht WHERE record = :recno"); |
|
150 } |
|
151 query.bindValue(":name", ui->nameEdit->text()); |
|
152 query.bindValue(":type", ui->typeEdit->currentIndex()); |
|
153 query.bindValue(":use", ui->useEdit->currentIndex()); |
|
154 query.bindValue(":time", ui->timeEdit->value()); |
|
155 query.bindValue(":isweight", ui->isweightEdit->isChecked() ? 1:0); |
|
156 query.bindValue(":usefor", ui->useforEdit->toPlainText()); |
|
157 query.bindValue(":notes", ui->notesEdit->toPlainText()); |
|
158 query.bindValue(":always", ui->alwaysEdit->isChecked() ? 1:0); |
|
159 query.bindValue(":inventory", QString("%1").arg(ui->inventoryEdit->value() / 1000.0, 5, 'f', 4, '0')); |
|
160 query.bindValue(":cost", QString("%1").arg(ui->costEdit->value(), 3, 'f', 2, '0')); |
|
161 /* Uses https://www.qtcentre.org/threads/17295-How-to-put-empty-value-in-QDateEdit */ |
|
162 query.bindValue(":prod", ui->prodEdit->nullDate()); |
|
163 query.bindValue(":tht", ui->thtEdit->nullDate()); |
|
164 if (this->recno == -1) { |
|
165 query.bindValue(":uuid", QUuid::createUuid().toString().mid(1, 36)); |
|
166 } else { |
|
167 query.bindValue(":recno", this->recno); |
|
168 } |
|
169 query.exec(); |
|
170 if (query.lastError().isValid()) { |
|
171 qDebug() << "EditMisc" << query.lastError(); |
|
172 QMessageBox::warning(this, tr("Database error"), |
|
173 tr("MySQL error: %1\n%2\n%3") |
|
174 .arg(query.lastError().nativeErrorCode()) |
|
175 .arg(query.lastError().driverText()) |
|
176 .arg(query.lastError().databaseText())); |
|
177 } else { |
|
178 qDebug() << "EditMisc Saved"; |
|
179 } |
|
180 } |
|
181 |
|
182 ui->saveButton->setEnabled(false); |
|
183 this->textIsChanged = false; |
|
184 WindowTitle(); |
|
185 } |
|
186 |
|
187 |
|
188 void EditMisc::on_deleteButton_clicked() |
|
189 { |
|
190 QSqlQuery query; |
|
191 |
|
192 query.prepare("DELETE FROM inventory_miscs WHERE record = :recno"); |
|
193 query.bindValue(":recno", this->recno); |
|
194 query.exec(); |
|
195 if (query.lastError().isValid()) { |
|
196 qDebug() << "EditMisc" << 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() << "EditMisc Deleted" << this->recno; |
|
204 } |
|
205 |
|
206 this->close(); |
|
207 this->setResult(1); |
|
208 } |
|
209 |
|
210 |
|
211 void EditMisc::TimeSet() |
|
212 { |
|
213 int time = ui->timeEdit->value(); |
|
214 QString w = QString(""); |
|
215 |
|
216 if (time == 0) { |
|
217 ui->timeEdit->setSingleStep(1); |
|
218 |
|
219 } else if (time == 1) { |
|
220 w = QString("1 minute"); |
|
221 ui->timeEdit->setSingleStep(1); |
|
222 |
|
223 } else if (time < 180) { |
|
224 w = QString("%1 minutes").arg(time); |
|
225 ui->timeEdit->setSingleStep(1); |
|
226 |
|
227 } else if (time == 180) { |
|
228 w = QString("3 hours"); |
|
229 if (lasttime == 240) |
|
230 ui->timeEdit->setSingleStep(1); /* Going down */ |
|
231 else |
|
232 ui->timeEdit->setSingleStep(60); |
|
233 |
|
234 } else if (time < 1440) { |
|
235 w = QString("%1 hours").arg(time / 60); |
|
236 ui->timeEdit->setSingleStep(60); |
|
237 |
|
238 } else if (time == 1440) { |
|
239 w = QString("1 day"); |
|
240 if (lasttime == 2880) |
|
241 ui->timeEdit->setSingleStep(60); /* Going down */ |
|
242 else |
|
243 ui->timeEdit->setSingleStep(1440); |
|
244 |
|
245 } else { |
|
246 w = QString("%1 days").arg(time / 1440); |
|
247 ui->timeEdit->setSingleStep(1440); |
|
248 |
|
249 } |
|
250 ui->timeShow->setText(w); |
|
251 lasttime = time; |
|
252 } |
|
253 |
|
254 |
|
255 void EditMisc::is_changed() |
|
256 { |
|
257 ui->valueEdit->setValue(ui->inventoryEdit->value() * ui->costEdit->value()); |
|
258 ui->saveButton->setEnabled(true); |
|
259 ui->deleteButton->setEnabled((ui->inventoryEdit->value() == 0 && this->recno >= 0) ? true:false); |
|
260 this->textIsChanged = true; |
|
261 WindowTitle(); |
|
262 } |
|
263 |
|
264 |
|
265 void EditMisc::time_changed() |
|
266 { |
|
267 TimeSet(); |
|
268 is_changed(); |
|
269 } |
|
270 |
|
271 |
|
272 void EditMisc::on_quitButton_clicked() |
|
273 { |
|
274 if (this->textIsChanged) { |
|
275 int rc = QMessageBox::warning(this, tr("Misc changed"), tr("The ingredient has been modified\n Save changes?"), |
|
276 QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel, QMessageBox::Save); |
|
277 switch (rc) { |
|
278 case QMessageBox::Save: |
|
279 on_saveButton_clicked(); |
|
280 break; /* Saved and then Quit */ |
|
281 case QMessageBox::Discard: |
|
282 break; /* Quit without Save */ |
|
283 case QMessageBox::Cancel: |
|
284 return; /* Return to the editor page */ |
|
285 } |
|
286 } |
|
287 |
|
288 this->close(); |
|
289 this->setResult(1); |
|
290 } |