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