src/EditWater.cpp

changeset 29
76846c99f827
child 60
0d65238ebedc
equal deleted inserted replaced
28:93a70b1502ca 29:76846c99f827
1 /**
2 * EditWater.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 "EditWater.h"
18 #include "../ui/ui_EditWater.h"
19 #include "bmsapp.h"
20
21
22 EditWater::EditWater(int id, QWidget *parent) : QDialog(parent), ui(new Ui::EditWater)
23 {
24 QSqlQuery query;
25
26 qDebug() << "EditWater record:" << id;
27 ui->setupUi(this);
28 this->recno = id;
29
30 WindowTitle();
31
32 if (id >= 0) {
33 query.prepare("SELECT * FROM inventory_waters WHERE record = :recno");
34 query.bindValue(":recno", id);
35 query.exec();
36 query.next();
37
38 ui->nameEdit->setText(query.value(1).toString());
39 ui->unlimitedEdit->setChecked(query.value(2).toInt() ? true:false);
40 ui->caEdit->setValue(query.value(3).toDouble());
41 ui->hcoEdit->setValue(query.value(4).toDouble());
42 ui->so4Edit->setValue(query.value(5).toDouble());
43 ui->clEdit->setValue(query.value(6).toDouble());
44 ui->naEdit->setValue(query.value(7).toDouble());
45 ui->mgEdit->setValue(query.value(8).toDouble());
46 ui->phEdit->setValue(query.value(9).toDouble());
47 ui->notesEdit->setPlainText(query.value(10).toString());
48 ui->alkalinityEdit->setValue(query.value(11).toDouble());
49 ui->inventoryEdit->setValue(query.value(12).toDouble());
50 ui->costEdit->setValue(query.value(13).toDouble());
51 } else {
52 /* Set some defaults */
53 ui->phEdit->setValue(7.0);
54 }
55 WaterSet();
56 connect(ui->nameEdit, &QLineEdit::textChanged, this, &EditWater::is_changed);
57 connect(ui->unlimitedEdit, &QCheckBox::stateChanged, this, &EditWater::is_changed);
58 connect(ui->caEdit, &QDoubleSpinBox::textChanged, this, &EditWater::water_changed);
59 connect(ui->hcoEdit, &QDoubleSpinBox::textChanged, this, &EditWater::hco_changed);
60 connect(ui->so4Edit, &QDoubleSpinBox::textChanged, this, &EditWater::water_changed);
61 connect(ui->clEdit, &QDoubleSpinBox::textChanged, this, &EditWater::water_changed);
62 connect(ui->naEdit, &QDoubleSpinBox::textChanged, this, &EditWater::water_changed);
63 connect(ui->mgEdit, &QDoubleSpinBox::textChanged, this, &EditWater::water_changed);
64 connect(ui->phEdit, &QDoubleSpinBox::textChanged, this, &EditWater::water_changed);
65 connect(ui->alkalinityEdit, &QDoubleSpinBox::textChanged, this, &EditWater::alkalinity_changed);
66 connect(ui->notesEdit, SIGNAL(textChanged()), this, SLOT(is_changed()));
67 connect(ui->inventoryEdit, &QDoubleSpinBox::textChanged, this, &EditWater::is_changed);
68 connect(ui->costEdit, &QDoubleSpinBox::textChanged, this, &EditWater::is_changed);
69
70 ui->saveButton->setEnabled(false);
71 ui->deleteButton->setEnabled((ui->inventoryEdit->value() == 0 && id >= 0) ? true:false);
72 }
73
74
75 EditWater::~EditWater()
76 {
77 qDebug() << "EditWater done";
78 delete ui;
79 emit entry_changed();
80 }
81
82
83 /*
84 * Window header, mark any change with '**'
85 */
86 void EditWater::WindowTitle()
87 {
88 QString txt;
89
90 if (this->recno < 0) {
91 txt = QString(tr("BMSapp - Add new brewing water"));
92 } else {
93 txt = QString(tr("BMSapp - Edit brewing water %1").arg(this->recno));
94 }
95
96 if (this->textIsChanged) {
97 txt.append((QString(" **")));
98 }
99 setWindowTitle(txt);
100 }
101
102
103 void EditWater::on_saveButton_clicked()
104 {
105 QSqlQuery query;
106
107 /* If there are errors in the form, show a message and do "return;" */
108 if (ui->nameEdit->text().length() < 2) {
109 QMessageBox::warning(this, tr("Edit Water"), tr("Name empty or too short."));
110 return;
111 }
112
113 if (this->textIsChanged) {
114 if (this->recno == -1) {
115 query.prepare("INSERT INTO inventory_waters SET name=:name, unlimited_stock=:unlimited, calcium=:ca, "
116 "bicarbonate=:hco, sulfate=:so4, chloride=:cl, sodium=:na, magnesium=:mg, ph=:ph, notes=:notes, "
117 "total_alkalinity=:alkalinity, inventory=:inventory, cost=:cost, uuid = :uuid");
118 } else {
119 query.prepare("UPDATE inventory_waters SET name=:name, unlimited_stock=:unlimited, calcium=:ca, "
120 "bicarbonate=:hco, sulfate=:so4, chloride=:cl, sodium=:na, magnesium=:mg, ph=:ph, notes=:notes, "
121 "total_alkalinity=:alkalinity, inventory=:inventory, cost=:cost WHERE record = :recno");
122 }
123 query.bindValue(":name", ui->nameEdit->text());
124 query.bindValue(":unlimited", ui->unlimitedEdit->isChecked() ? 1:0);
125 query.bindValue(":ca", QString("%1").arg(ui->caEdit->value(), 2, 'f', 1, '0'));
126 query.bindValue(":hco", QString("%1").arg(ui->hcoEdit->value(), 2, 'f', 1, '0'));
127 query.bindValue(":so4", QString("%1").arg(ui->so4Edit->value(), 2, 'f', 1, '0'));
128 query.bindValue(":cl", QString("%1").arg(ui->clEdit->value(), 2, 'f', 1, '0'));
129 query.bindValue(":na", QString("%1").arg(ui->naEdit->value(), 2, 'f', 1, '0'));
130 query.bindValue(":mg", QString("%1").arg(ui->mgEdit->value(), 2, 'f', 1, '0'));
131 query.bindValue(":ph", QString("%1").arg(ui->phEdit->value(), 3, 'f', 2, '0'));
132 query.bindValue(":notes", ui->notesEdit->toPlainText());
133 query.bindValue(":alkalinity", QString("%1").arg(ui->alkalinityEdit->value(), 2, 'f', 1, '0'));
134 query.bindValue(":inventory", QString("%1").arg(ui->inventoryEdit->value(), 2, 'f', 1, '0'));
135 query.bindValue(":cost", QString("%1").arg(ui->costEdit->value(), 6, 'f', 5, '0'));
136 if (this->recno == -1) {
137 query.bindValue(":uuid", QUuid::createUuid().toString().mid(1, 36));
138 } else {
139 query.bindValue(":recno", this->recno);
140 }
141 query.exec();
142 if (query.lastError().isValid()) {
143 qDebug() << "EditWater" << query.lastError();
144 QMessageBox::warning(this, tr("Database error"),
145 tr("MySQL error: %1\n%2\n%3")
146 .arg(query.lastError().nativeErrorCode())
147 .arg(query.lastError().driverText())
148 .arg(query.lastError().databaseText()));
149 } else {
150 qDebug() << "EditWater Saved";
151 }
152 }
153
154 ui->saveButton->setEnabled(false);
155 this->textIsChanged = false;
156 WindowTitle();
157 }
158
159
160 void EditWater::on_deleteButton_clicked()
161 {
162 QSqlQuery query;
163
164 query.prepare("DELETE FROM inventory_waters WHERE record = :recno");
165 query.bindValue(":recno", this->recno);
166 query.exec();
167 if (query.lastError().isValid()) {
168 qDebug() << "EditWater" << query.lastError();
169 QMessageBox::warning(this, tr("Database error"),
170 tr("MySQL error: %1\n%2\n%3")
171 .arg(query.lastError().nativeErrorCode())
172 .arg(query.lastError().driverText())
173 .arg(query.lastError().databaseText()));
174 } else {
175 qDebug() << "EditWater Deleted" << this->recno;
176 }
177
178 this->close();
179 this->setResult(1);
180 }
181
182
183 void EditWater::WaterSet()
184 {
185 double cations, anions, balance;
186
187 cations = (ui->caEdit->value() / 20.039) + (ui->mgEdit->value() / 12.1525) + (ui->naEdit->value() / 22.989);
188 anions = (ui->hcoEdit->value() / 61.016) + (ui->so4Edit->value() / 48.031) + (ui->clEdit->value() / 35.4527);
189 balance = round((cations - anions) * 100) / 100;
190 ui->balanceEdit->setValue(balance);
191
192 if (balance <= 0.1 && balance >= -0.1) {
193 ui->balanceIcon->setPixmap(QPixmap(":icons/silk/tick.png"));
194 } else if (balance <= 0.5 && balance >= -0.5) {
195 ui->balanceIcon->setPixmap(QPixmap(":icons/silk/thumb_down.png"));
196 } else {
197 ui->balanceIcon->setPixmap(QPixmap(":icons/silk/error.png"));
198 }
199 }
200
201
202 void EditWater::is_changed()
203 {
204 ui->saveButton->setEnabled(true);
205 ui->deleteButton->setEnabled((ui->inventoryEdit->value() == 0 && this->recno >= 0) ? true:false);
206 this->textIsChanged = true;
207 WindowTitle();
208 }
209
210
211 void EditWater::water_changed()
212 {
213 WaterSet();
214 is_changed();
215 }
216
217
218 void EditWater::hco_changed()
219 {
220 ui->alkalinityEdit->setValue(ui->hcoEdit->value() * 50 / 61);
221 water_changed();
222 }
223
224
225 void EditWater::alkalinity_changed()
226 {
227 ui->hcoEdit->setValue(ui->alkalinityEdit->value() * 1.22);
228 water_changed();
229 }
230
231
232 void EditWater::on_quitButton_clicked()
233 {
234 if (this->textIsChanged) {
235 int rc = QMessageBox::warning(this, tr("Water changed"), tr("The water has been modified\n Save changes?"),
236 QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel, QMessageBox::Save);
237 switch (rc) {
238 case QMessageBox::Save:
239 on_saveButton_clicked();
240 break; /* Saved and then Quit */
241 case QMessageBox::Discard:
242 break; /* Quit without Save */
243 case QMessageBox::Cancel:
244 return; /* Return to the editor page */
245 }
246 }
247
248 this->close();
249 this->setResult(1);
250 }

mercurial