src/EditWater.cpp

changeset 353
324a7268796b
parent 236
d0b1640ba951
child 385
09af9f46518f
equal deleted inserted replaced
352:fcf222cabb98 353:324a7268796b
14 * You should have received a copy of the GNU General Public License 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/>. 15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */ 16 */
17 #include "EditWater.h" 17 #include "EditWater.h"
18 #include "../ui/ui_EditWater.h" 18 #include "../ui/ui_EditWater.h"
19 #include "global.h"
20 #include "Utils.h"
19 #include "MainWindow.h" 21 #include "MainWindow.h"
20 22
21 23
22 EditWater::EditWater(int id, QWidget *parent) : QDialog(parent), ui(new Ui::EditWater) 24 EditWater::EditWater(int id, QWidget *parent) : QDialog(parent), ui(new Ui::EditWater)
23 { 25 {
33 query.prepare("SELECT * FROM inventory_waters WHERE record = :recno"); 35 query.prepare("SELECT * FROM inventory_waters WHERE record = :recno");
34 query.bindValue(":recno", id); 36 query.bindValue(":recno", id);
35 query.exec(); 37 query.exec();
36 query.next(); 38 query.next();
37 39
38 ui->nameEdit->setText(query.value(1).toString()); 40 ui->nameEdit->setText(query.value("name").toString());
39 ui->unlimitedEdit->setChecked(query.value(2).toInt() ? true:false); 41 ui->unlimitedEdit->setChecked(query.value("unlimited_stock").toInt() ? true:false);
40 ui->caEdit->setValue(query.value(3).toDouble()); 42 calcium = query.value("calcium").toDouble();
41 ui->hcoEdit->setValue(query.value(4).toDouble()); 43 ui->caEdit->setValue(query.value("calcium").toDouble());
42 ui->so4Edit->setValue(query.value(5).toDouble()); 44 bicarbonate = query.value("bicarbonate").toDouble();
43 ui->clEdit->setValue(query.value(6).toDouble()); 45 ui->hcoEdit->setValue(query.value("bicarbonate").toDouble());
44 ui->naEdit->setValue(query.value(7).toDouble()); 46 sulfate = query.value("sulfate").toDouble();
45 ui->mgEdit->setValue(query.value(8).toDouble()); 47 ui->so4Edit->setValue(query.value("sulfate").toDouble());
46 ui->phEdit->setValue(query.value(9).toDouble()); 48 chloride = query.value("chloride").toDouble();
47 ui->notesEdit->setPlainText(query.value(10).toString()); 49 ui->clEdit->setValue(query.value("chloride").toDouble());
48 ui->alkalinityEdit->setValue(query.value(11).toDouble()); 50 sodium = query.value("sodium").toDouble();
49 ui->inventoryEdit->setValue(query.value(12).toDouble()); 51 ui->naEdit->setValue(query.value("sodium").toDouble());
50 ui->costEdit->setValue(query.value(13).toDouble()); 52 magnesium = query.value("magnesium").toDouble();
51 ui->noEdit->setValue(query.value(15).toDouble()); 53 ui->mgEdit->setValue(query.value("magnesium").toDouble());
54 ph = query.value("ph").toDouble();
55 ui->phEdit->setValue(query.value("ph").toDouble());
56 ui->notesEdit->setPlainText(query.value("notes").toString());
57 total_alkalinity = query.value("total_alkalinity").toDouble();
58 ui->alkalinityEdit->setValue(query.value("total_alkalinity").toDouble());
59 ui->inventoryEdit->setValue(query.value("inventory").toDouble());
60 ui->costEdit->setValue(query.value("cost").toDouble());
61 nitrate = query.value("nitrate").toDouble();
62 ui->noEdit->setValue(query.value("nitrate").toDouble());
52 } else { 63 } else {
53 /* Set some defaults */ 64 /* Set some defaults */
65 calcium = bicarbonate = sulfate = chloride = 0;
66 sodium = magnesium = total_alkalinity = nitrate = 0;
67 ph = 7.0;
54 ui->phEdit->setValue(7.0); 68 ui->phEdit->setValue(7.0);
55 } 69 }
70 ui->saveButton->setEnabled(false);
71 ui->deleteButton->setEnabled((ui->inventoryEdit->value() == 0 && id >= 0) ? true:false);
56 WaterSet(); 72 WaterSet();
57 connect(ui->nameEdit, &QLineEdit::textChanged, this, &EditWater::is_changed); 73 connect(ui->nameEdit, &QLineEdit::textChanged, this, &EditWater::is_changed);
58 connect(ui->unlimitedEdit, &QCheckBox::stateChanged, this, &EditWater::is_changed); 74 connect(ui->unlimitedEdit, &QCheckBox::stateChanged, this, &EditWater::is_changed);
59 connect(ui->caEdit, &QDoubleSpinBox::textChanged, this, &EditWater::water_changed); 75 connect(ui->caEdit, QOverload<double>::of(&QDoubleSpinBox::valueChanged), this, &EditWater::calcium_changed);
60 connect(ui->hcoEdit, &QDoubleSpinBox::textChanged, this, &EditWater::hco_changed); 76 connect(ui->so4Edit, QOverload<double>::of(&QDoubleSpinBox::valueChanged), this, &EditWater::sulfate_changed);
61 connect(ui->so4Edit, &QDoubleSpinBox::textChanged, this, &EditWater::water_changed); 77 connect(ui->clEdit, QOverload<double>::of(&QDoubleSpinBox::valueChanged), this, &EditWater::chloride_changed);
62 connect(ui->clEdit, &QDoubleSpinBox::textChanged, this, &EditWater::water_changed); 78 connect(ui->naEdit, QOverload<double>::of(&QDoubleSpinBox::valueChanged), this, &EditWater::sodium_changed);
63 connect(ui->naEdit, &QDoubleSpinBox::textChanged, this, &EditWater::water_changed); 79 connect(ui->mgEdit, QOverload<double>::of(&QDoubleSpinBox::valueChanged), this, &EditWater::magnesium_changed);
64 connect(ui->mgEdit, &QDoubleSpinBox::textChanged, this, &EditWater::water_changed); 80 connect(ui->phEdit, QOverload<double>::of(&QDoubleSpinBox::valueChanged), this, &EditWater::ph_changed);
65 connect(ui->phEdit, &QDoubleSpinBox::textChanged, this, &EditWater::water_changed); 81 connect(ui->alkalinityEdit, QOverload<double>::of(&QDoubleSpinBox::valueChanged), this, &EditWater::total_alkalinity_changed);
66 connect(ui->alkalinityEdit, &QDoubleSpinBox::textChanged, this, &EditWater::alkalinity_changed);
67 connect(ui->notesEdit, SIGNAL(textChanged()), this, SLOT(is_changed())); 82 connect(ui->notesEdit, SIGNAL(textChanged()), this, SLOT(is_changed()));
68 connect(ui->inventoryEdit, &QDoubleSpinBox::textChanged, this, &EditWater::is_changed); 83 connect(ui->inventoryEdit, &QDoubleSpinBox::textChanged, this, &EditWater::is_changed);
69 connect(ui->costEdit, &QDoubleSpinBox::textChanged, this, &EditWater::is_changed); 84 connect(ui->costEdit, &QDoubleSpinBox::textChanged, this, &EditWater::is_changed);
70 connect(ui->noEdit, &QDoubleSpinBox::textChanged, this, &EditWater::water_changed); 85 connect(ui->noEdit, QOverload<double>::of(&QDoubleSpinBox::valueChanged), this, &EditWater::nitrate_changed);
71
72 ui->saveButton->setEnabled(false);
73 ui->deleteButton->setEnabled((ui->inventoryEdit->value() == 0 && id >= 0) ? true:false);
74 } 86 }
75 87
76 88
77 EditWater::~EditWater() 89 EditWater::~EditWater()
78 { 90 {
87 */ 99 */
88 void EditWater::WindowTitle() 100 void EditWater::WindowTitle()
89 { 101 {
90 QString txt; 102 QString txt;
91 103
92 if (this->recno < 0) { 104 if (recno < 0) {
93 txt = QString(tr("BMSapp - Add new brewing water")); 105 txt = QString(tr("BMSapp - Add new brewing water"));
94 } else { 106 } else {
95 txt = QString(tr("BMSapp - Edit brewing water %1").arg(this->recno)); 107 txt = QString(tr("BMSapp - Edit brewing water %1").arg(recno));
96 } 108 }
97 109
98 if (this->textIsChanged) { 110 if (this->textIsChanged) {
99 txt.append((QString(" **"))); 111 txt.append((QString(" **")));
100 } 112 }
122 "bicarbonate=:hco, sulfate=:so4, chloride=:cl, sodium=:na, magnesium=:mg, ph=:ph, notes=:notes, " 134 "bicarbonate=:hco, sulfate=:so4, chloride=:cl, sodium=:na, magnesium=:mg, ph=:ph, notes=:notes, "
123 "total_alkalinity=:alkalinity, inventory=:inventory, cost=:cost, nitrate=:no WHERE record = :recno"); 135 "total_alkalinity=:alkalinity, inventory=:inventory, cost=:cost, nitrate=:no WHERE record = :recno");
124 } 136 }
125 query.bindValue(":name", ui->nameEdit->text()); 137 query.bindValue(":name", ui->nameEdit->text());
126 query.bindValue(":unlimited", ui->unlimitedEdit->isChecked() ? 1:0); 138 query.bindValue(":unlimited", ui->unlimitedEdit->isChecked() ? 1:0);
127 query.bindValue(":ca", QString("%1").arg(ui->caEdit->value(), 2, 'f', 1, '0')); 139 query.bindValue(":ca", round(calcium * 1000) / 1000);
128 query.bindValue(":hco", QString("%1").arg(ui->hcoEdit->value(), 2, 'f', 1, '0')); 140 query.bindValue(":hco", round(bicarbonate * 1000) / 1000);
129 query.bindValue(":so4", QString("%1").arg(ui->so4Edit->value(), 2, 'f', 1, '0')); 141 query.bindValue(":so4", round(sulfate * 1000) / 1000);
130 query.bindValue(":cl", QString("%1").arg(ui->clEdit->value(), 2, 'f', 1, '0')); 142 query.bindValue(":cl", round(chloride * 1000) / 1000);
131 query.bindValue(":na", QString("%1").arg(ui->naEdit->value(), 2, 'f', 1, '0')); 143 query.bindValue(":na", round(sodium * 1000) / 1000);
132 query.bindValue(":mg", QString("%1").arg(ui->mgEdit->value(), 2, 'f', 1, '0')); 144 query.bindValue(":mg", round(magnesium * 1000) / 1000);
133 query.bindValue(":ph", QString("%1").arg(ui->phEdit->value(), 3, 'f', 2, '0')); 145 query.bindValue(":ph", round(ph * 1000) / 1000);
134 query.bindValue(":notes", ui->notesEdit->toPlainText()); 146 query.bindValue(":notes", ui->notesEdit->toPlainText());
135 query.bindValue(":alkalinity", QString("%1").arg(ui->alkalinityEdit->value(), 2, 'f', 1, '0')); 147 query.bindValue(":alkalinity", round(total_alkalinity * 1000) / 1000);
136 query.bindValue(":inventory", QString("%1").arg(ui->inventoryEdit->value(), 2, 'f', 1, '0')); 148 query.bindValue(":inventory", QString("%1").arg(ui->inventoryEdit->value(), 2, 'f', 1, '0'));
137 query.bindValue(":cost", QString("%1").arg(ui->costEdit->value(), 6, 'f', 5, '0')); 149 query.bindValue(":cost", QString("%1").arg(ui->costEdit->value(), 6, 'f', 5, '0'));
138 query.bindValue(":no", QString("%1").arg(ui->noEdit->value(), 3, 'f', 2, '0')); 150 query.bindValue(":no", round(nitrate * 1000) / 1000);
139 if (this->recno == -1) { 151 if (recno == -1) {
140 query.bindValue(":uuid", QUuid::createUuid().toString().mid(1, 36)); 152 query.bindValue(":uuid", QUuid::createUuid().toString().mid(1, 36));
141 } else { 153 } else {
142 query.bindValue(":recno", this->recno); 154 query.bindValue(":recno", recno);
143 } 155 }
144 query.exec(); 156 query.exec();
145 if (query.lastError().isValid()) { 157 if (query.lastError().isValid()) {
146 qDebug() << "EditWater" << query.lastError(); 158 qDebug() << "EditWater" << query.lastError();
147 QMessageBox::warning(this, tr("Database error"), 159 QMessageBox::warning(this, tr("Database error"),
183 } 195 }
184 196
185 197
186 void EditWater::WaterSet() 198 void EditWater::WaterSet()
187 { 199 {
188 double cations, anions, balance; 200 double CO3 = total_alkalinity * pow(10, ph - 10.33) / (1+2* pow(10, ph - 10.33)) * MMCO3 / (MMCaCO3 / 2);
189 201 double t_bicarbonate = Utils::Bicarbonate(total_alkalinity, ph);
190 double CaCO3 = ui->hcoEdit->value() / 1.22; 202 qDebug() << t_bicarbonate << bicarbonate;
191 double pH = ui->phEdit->value(); 203 if ((round(t_bicarbonate * 1000) / 1000) != bicarbonate)
192 double CO3 = ((CaCO3 / 50) / (1+(2* pow(10, pH - 10.33))) * pow(10, pH - 10.33)) * 60; 204 is_changed();
193 cations = (ui->caEdit->value() / 20.039) + (ui->mgEdit->value() / 12.1525) + (ui->naEdit->value() / 22.989); 205 bicarbonate = t_bicarbonate;
194 anions = (ui->hcoEdit->value() / 61.016) + (CO3 / 30) + (ui->so4Edit->value() / 48.031) + 206 ui->hcoEdit->setValue(bicarbonate);
195 (ui->clEdit->value() / 35.4527) + (ui->noEdit->value() / 62.0049); 207
196 balance = round((cations - anions) * 100) / 100; 208 double cations = (2*calcium / MMCa) + (2*magnesium / MMMg) + (sodium / MMNa);
209 double anions = (bicarbonate / MMHCO3) + (2*CO3 / MMCO3) + (2*sulfate / MMSO4) + (chloride / MMCl) + (nitrate / MMNO3);
210 double balance = round((cations - anions) * 100) / 100;
197 ui->balanceEdit->setValue(balance); 211 ui->balanceEdit->setValue(balance);
198 ui->co3Edit->setValue(CO3); 212 ui->co3Edit->setValue(CO3);
199 //qDebug() << A13 << B13 << CO3; 213 ui->hardnessEdit->setValue(Utils::Hardness(calcium, magnesium));
214 ui->raEdit->setValue(Utils::ResidualAlkalinity(total_alkalinity, calcium, magnesium));
200 215
201 if (balance <= 0.1 && balance >= -0.1) { 216 if (balance <= 0.1 && balance >= -0.1) {
202 ui->balanceIcon->setPixmap(QPixmap(":icons/silk/tick.png")); 217 ui->balanceIcon->setPixmap(QPixmap(":icons/silk/tick.png"));
203 } else if (balance <= 0.5 && balance >= -0.5) { 218 } else if (balance <= 0.5 && balance >= -0.5) {
204 ui->balanceIcon->setPixmap(QPixmap(":icons/silk/thumb_down.png")); 219 ui->balanceIcon->setPixmap(QPixmap(":icons/silk/thumb_down.png"));
222 WaterSet(); 237 WaterSet();
223 is_changed(); 238 is_changed();
224 } 239 }
225 240
226 241
227 void EditWater::hco_changed() 242 void EditWater::calcium_changed(double val)
228 { 243 {
229 ui->alkalinityEdit->setValue(ui->hcoEdit->value() * 50 / 61); 244 calcium = val;
230 water_changed(); 245 water_changed();
231 } 246 }
232 247
233 248
234 void EditWater::alkalinity_changed() 249 void EditWater::total_alkalinity_changed(double val)
235 { 250 {
236 ui->hcoEdit->setValue(ui->alkalinityEdit->value() * 1.22); 251 total_alkalinity = val;
252 water_changed();
253 }
254
255
256 void EditWater::sulfate_changed(double val)
257 {
258 sulfate = val;
259 water_changed();
260 }
261
262
263 void EditWater::chloride_changed(double val)
264 {
265 chloride = val;
266 water_changed();
267 }
268
269
270 void EditWater::sodium_changed(double val)
271 {
272 sodium = val;
273 water_changed();
274 }
275
276
277 void EditWater::magnesium_changed(double val)
278 {
279 magnesium = val;
280 water_changed();
281 }
282
283
284 void EditWater::ph_changed(double val)
285 {
286 ph = val;
287 water_changed();
288 }
289
290
291 void EditWater::nitrate_changed(double val)
292 {
293 nitrate = val;
237 water_changed(); 294 water_changed();
238 } 295 }
239 296
240 297
241 void EditWater::on_quitButton_clicked() 298 void EditWater::on_quitButton_clicked()

mercurial