|
1 /** |
|
2 * EditHop.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 "EditHop.h" |
|
18 #include "../ui/ui_EditHop.h" |
|
19 #include "bmsapp.h" |
|
20 |
|
21 |
|
22 EditHop::EditHop(int id, QWidget *parent) : QDialog(parent), ui(new Ui::EditHop) |
|
23 { |
|
24 QSqlQuery query; |
|
25 |
|
26 qDebug() << "EditHop record:" << id; |
|
27 ui->setupUi(this); |
|
28 this->recno = id; |
|
29 |
|
30 WindowTitle(); |
|
31 |
|
32 ui->typeEdit->addItem(tr("Bittering")); |
|
33 ui->typeEdit->addItem(tr("Aroma")); |
|
34 ui->typeEdit->addItem(tr("Both")); |
|
35 |
|
36 ui->formEdit->addItem(tr("Pellet")); |
|
37 ui->formEdit->addItem(tr("Plug")); |
|
38 ui->formEdit->addItem(tr("Leaf")); |
|
39 ui->formEdit->addItem(tr("Leaf Wet")); /* Not in beerxml */ |
|
40 ui->formEdit->addItem(tr("Cryo")); /* Not in beerxml */ |
|
41 |
|
42 if (id >= 0) { |
|
43 query.prepare("SELECT * FROM inventory_hops WHERE record = :recno"); |
|
44 query.bindValue(":recno", id); |
|
45 query.exec(); |
|
46 query.next(); |
|
47 |
|
48 ui->nameEdit->setText(query.value(1).toString()); |
|
49 ui->alphaEdit->setValue(query.value(2).toDouble()); |
|
50 ui->betaEdit->setValue(query.value(3).toDouble()); |
|
51 ui->humuleneEdit->setValue(query.value(4).toDouble()); |
|
52 ui->caryEdit->setValue(query.value(5).toDouble()); |
|
53 ui->cohumuloneEdit->setValue(query.value(6).toDouble()); |
|
54 ui->myrceneEdit->setValue(query.value(7).toDouble()); |
|
55 ui->hsiEdit->setValue(query.value(8).toDouble()); |
|
56 ui->typeEdit->setCurrentIndex(query.value(9).toInt()); |
|
57 ui->formEdit->setCurrentIndex(query.value(10).toInt()); |
|
58 ui->notesEdit->setPlainText(query.value(11).toString()); |
|
59 ui->originEdit->setText(query.value(12).toString()); |
|
60 ui->substitutesEdit->setText(query.value(13).toString()); |
|
61 ui->alwaysEdit->setChecked(query.value(14).toInt() ? true:false); |
|
62 ui->inventoryEdit->setValue(query.value(15).toDouble()); |
|
63 ui->costEdit->setValue(query.value(16).toDouble()); |
|
64 ui->valueEdit->setValue(query.value(15).toDouble() * query.value(16).toDouble()); |
|
65 if (query.value(17).toString().length() == 10) { |
|
66 ui->prodEdit->setDate(query.value(17).toDate()); |
|
67 } else { |
|
68 ui->prodEdit->clear(); |
|
69 } |
|
70 if (query.value(18).toString().length() == 10) { |
|
71 ui->thtEdit->setDate(query.value(18).toDate()); |
|
72 } else { |
|
73 ui->thtEdit->clear(); |
|
74 } |
|
75 ui->oilEdit->setValue(query.value(19).toDouble()); |
|
76 } else { |
|
77 /* Set some defaults */ |
|
78 ui->typeEdit->setCurrentIndex(0); |
|
79 ui->formEdit->setCurrentIndex(0); |
|
80 ui->prodEdit->clear(); |
|
81 ui->thtEdit->clear(); |
|
82 } |
|
83 connect(ui->nameEdit, &QLineEdit::textChanged, this, &EditHop::is_changed); |
|
84 connect(ui->alphaEdit, &QDoubleSpinBox::textChanged, this, &EditHop::is_changed); |
|
85 connect(ui->betaEdit, &QDoubleSpinBox::textChanged, this, &EditHop::is_changed); |
|
86 connect(ui->humuleneEdit, &QDoubleSpinBox::textChanged, this, &EditHop::is_changed); |
|
87 connect(ui->caryEdit, &QDoubleSpinBox::textChanged, this, &EditHop::is_changed); |
|
88 connect(ui->cohumuloneEdit, &QDoubleSpinBox::textChanged, this, &EditHop::is_changed); |
|
89 connect(ui->myrceneEdit, &QDoubleSpinBox::textChanged, this, &EditHop::is_changed); |
|
90 connect(ui->hsiEdit, &QDoubleSpinBox::textChanged, this, &EditHop::is_changed); |
|
91 connect(ui->typeEdit, &QComboBox::currentTextChanged, this, &EditHop::is_changed); |
|
92 connect(ui->formEdit, &QComboBox::currentTextChanged, this, &EditHop::is_changed); |
|
93 connect(ui->notesEdit, SIGNAL(textChanged()), this, SLOT(is_changed())); |
|
94 connect(ui->originEdit, &QLineEdit::textChanged, this, &EditHop::is_changed); |
|
95 connect(ui->substitutesEdit, &QLineEdit::textChanged, this, &EditHop::is_changed); |
|
96 connect(ui->alwaysEdit, &QCheckBox::stateChanged, this, &EditHop::is_changed); |
|
97 connect(ui->inventoryEdit, &QDoubleSpinBox::textChanged, this, &EditHop::is_changed); |
|
98 connect(ui->costEdit, &QDoubleSpinBox::textChanged, this, &EditHop::is_changed); |
|
99 connect(ui->prodEdit, &QDateEdit::dateChanged, this, &EditHop::is_changed); |
|
100 connect(ui->thtEdit, &QDateEdit::dateChanged, this, &EditHop::is_changed); |
|
101 connect(ui->oilEdit, &QDoubleSpinBox::textChanged, this, &EditHop::is_changed); |
|
102 |
|
103 ui->saveButton->setEnabled(false); |
|
104 ui->deleteButton->setEnabled((ui->inventoryEdit->value() == 0 && id >= 0) ? true:false); |
|
105 } |
|
106 |
|
107 |
|
108 EditHop::~EditHop() |
|
109 { |
|
110 qDebug() << "EditHop done"; |
|
111 delete ui; |
|
112 emit entry_changed(); |
|
113 } |
|
114 |
|
115 |
|
116 /* |
|
117 * Window header, mark any change with '**' |
|
118 */ |
|
119 void EditHop::WindowTitle() |
|
120 { |
|
121 QString txt; |
|
122 |
|
123 if (this->recno < 0) { |
|
124 txt = QString(tr("BMSapp - Add new hop")); |
|
125 } else { |
|
126 txt = QString(tr("BMSapp - Edit hop %1").arg(this->recno)); |
|
127 } |
|
128 |
|
129 if (this->textIsChanged) { |
|
130 txt.append((QString(" **"))); |
|
131 } |
|
132 setWindowTitle(txt); |
|
133 } |
|
134 |
|
135 |
|
136 void EditHop::on_saveButton_clicked() |
|
137 { |
|
138 QSqlQuery query; |
|
139 |
|
140 /* If there are errors in the form, show a message and do "return;" */ |
|
141 if (ui->nameEdit->text().length() < 2) { |
|
142 QMessageBox::warning(this, tr("Edit Hop"), tr("Name empty or too short.")); |
|
143 return; |
|
144 } |
|
145 if (ui->originEdit->text().length() < 2) { |
|
146 QMessageBox::warning(this, tr("Edit Hop"), tr("Origin empty or too short.")); |
|
147 return; |
|
148 } |
|
149 |
|
150 if (this->textIsChanged) { |
|
151 if (this->recno == -1) { |
|
152 query.prepare("INSERT INTO inventory_hops SET name=:name, alpha=:alpha, beta=:beta, " |
|
153 "humulene=:humulene, caryophyllene=:cary, cohumulone=:cohumulone, myrcene=:myrcene, " |
|
154 "hsi=:hsi, type=:type, form=:form, notes=:notes, origin=:origin, substitutes=:substitutes, " |
|
155 "always_on_stock=:always, inventory=:inventory, cost=:cost, production_date=:prod, " |
|
156 "tht_date=:tht, total_oil=:oil, uuid = :uuid"); |
|
157 } else { |
|
158 query.prepare("UPDATE inventory_hops SET name=:name, alpha=:alpha, beta=:beta, " |
|
159 "humulene=:humulene, caryophyllene=:cary, cohumulone=:cohumulone, myrcene=:myrcene, " |
|
160 "hsi=:hsi, type=:type, form=:form, notes=:notes, origin=:origin, substitutes=:substitutes, " |
|
161 "always_on_stock=:always, inventory=:inventory, cost=:cost, production_date=:prod, " |
|
162 "tht_date=:tht, total_oil=:oil WHERE record = :recno"); |
|
163 } |
|
164 query.bindValue(":name", ui->nameEdit->text()); |
|
165 query.bindValue(":alpha", QString("%1").arg(ui->alphaEdit->value(), 2, 'f', 1, '0')); |
|
166 query.bindValue(":beta", QString("%1").arg(ui->betaEdit->value(), 2, 'f', 1, '0')); |
|
167 query.bindValue(":humulene", QString("%1").arg(ui->humuleneEdit->value(), 2, 'f', 1, '0')); |
|
168 query.bindValue(":cary", QString("%1").arg(ui->caryEdit->value(), 2, 'f', 1, '0')); |
|
169 query.bindValue(":cohumulone", QString("%1").arg(ui->cohumuloneEdit->value(), 2, 'f', 1, '0')); |
|
170 query.bindValue(":myrcene", QString("%1").arg(ui->myrceneEdit->value(), 2, 'f', 1, '0')); |
|
171 query.bindValue(":hsi", QString("%1").arg(ui->hsiEdit->value(), 2, 'f', 1, '0')); |
|
172 query.bindValue(":type", ui->typeEdit->currentIndex()); |
|
173 query.bindValue(":form", ui->formEdit->currentIndex()); |
|
174 query.bindValue(":notes", ui->notesEdit->toPlainText()); |
|
175 query.bindValue(":origin", ui->originEdit->text()); |
|
176 query.bindValue(":substitutes", ui->substitutesEdit->text()); |
|
177 query.bindValue(":always", ui->alwaysEdit->isChecked() ? 1:0); |
|
178 query.bindValue(":inventory", QString("%1").arg(ui->inventoryEdit->value(), 5, 'f', 4, '0')); |
|
179 query.bindValue(":cost", QString("%1").arg(ui->costEdit->value(), 3, 'f', 2, '0')); |
|
180 /* Uses https://www.qtcentre.org/threads/17295-How-to-put-empty-value-in-QDateEdit */ |
|
181 query.bindValue(":prod", ui->prodEdit->nullDate()); |
|
182 query.bindValue(":tht", ui->thtEdit->nullDate()); |
|
183 query.bindValue(":oil", QString("%1").arg(ui->oilEdit->value(), 2, 'f', 1, '0')); |
|
184 if (this->recno == -1) { |
|
185 query.bindValue(":uuid", QUuid::createUuid().toString().mid(1, 36)); |
|
186 } else { |
|
187 query.bindValue(":recno", this->recno); |
|
188 } |
|
189 query.exec(); |
|
190 if (query.lastError().isValid()) { |
|
191 qDebug() << "EditHop" << query.lastError(); |
|
192 QMessageBox::warning(this, tr("Database error"), |
|
193 tr("MySQL error: %1\n%2\n%3") |
|
194 .arg(query.lastError().nativeErrorCode()) |
|
195 .arg(query.lastError().driverText()) |
|
196 .arg(query.lastError().databaseText())); |
|
197 } else { |
|
198 qDebug() << "EditHop Saved"; |
|
199 } |
|
200 } |
|
201 |
|
202 ui->saveButton->setEnabled(false); |
|
203 this->textIsChanged = false; |
|
204 WindowTitle(); |
|
205 } |
|
206 |
|
207 |
|
208 void EditHop::on_deleteButton_clicked() |
|
209 { |
|
210 QSqlQuery query; |
|
211 |
|
212 query.prepare("DELETE FROM inventory_hops WHERE record = :recno"); |
|
213 query.bindValue(":recno", this->recno); |
|
214 query.exec(); |
|
215 if (query.lastError().isValid()) { |
|
216 qDebug() << "EditHop" << query.lastError(); |
|
217 QMessageBox::warning(this, tr("Database error"), |
|
218 tr("MySQL error: %1\n%2\n%3") |
|
219 .arg(query.lastError().nativeErrorCode()) |
|
220 .arg(query.lastError().driverText()) |
|
221 .arg(query.lastError().databaseText())); |
|
222 } else { |
|
223 qDebug() << "EditHop Deleted" << this->recno; |
|
224 } |
|
225 |
|
226 this->close(); |
|
227 this->setResult(1); |
|
228 } |
|
229 |
|
230 |
|
231 void EditHop::is_changed() |
|
232 { |
|
233 ui->valueEdit->setValue(ui->inventoryEdit->value() * ui->costEdit->value()); |
|
234 ui->saveButton->setEnabled(true); |
|
235 ui->deleteButton->setEnabled((ui->inventoryEdit->value() == 0 && this->recno >= 0) ? true:false); |
|
236 this->textIsChanged = true; |
|
237 WindowTitle(); |
|
238 } |
|
239 |
|
240 |
|
241 void EditHop::on_quitButton_clicked() |
|
242 { |
|
243 if (this->textIsChanged) { |
|
244 int rc = QMessageBox::warning(this, tr("Hop changed"), tr("The fermentable has been modified\n Save changes?"), |
|
245 QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel, QMessageBox::Save); |
|
246 switch (rc) { |
|
247 case QMessageBox::Save: |
|
248 on_saveButton_clicked(); |
|
249 break; /* Saved and then Quit */ |
|
250 case QMessageBox::Discard: |
|
251 break; /* Quit without Save */ |
|
252 case QMessageBox::Cancel: |
|
253 return; /* Return to the editor page */ |
|
254 } |
|
255 } |
|
256 |
|
257 this->close(); |
|
258 this->setResult(1); |
|
259 } |