33 query.prepare("SELECT * FROM profile_water WHERE record = :recno"); |
35 query.prepare("SELECT * FROM profile_water 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->caEdit->setValue(query.value(2).toDouble()); |
41 calcium = query.value("calcium").toDouble(); |
40 ui->hcoEdit->setValue(query.value(3).toDouble()); |
42 ui->caEdit->setValue(query.value("calcium").toDouble()); |
41 ui->so4Edit->setValue(query.value(4).toDouble()); |
43 bicarbonate = query.value("bicarbonate").toDouble(); |
42 ui->clEdit->setValue(query.value(5).toDouble()); |
44 ui->hcoEdit->setValue(query.value("bicarbonate").toDouble()); |
43 ui->naEdit->setValue(query.value(6).toDouble()); |
45 sulfate = query.value("sulfate").toDouble(); |
44 ui->mgEdit->setValue(query.value(7).toDouble()); |
46 ui->so4Edit->setValue(query.value("sulfate").toDouble()); |
45 ui->phEdit->setValue(query.value(8).toDouble()); |
47 chloride = query.value("chloride").toDouble(); |
46 ui->notesEdit->setPlainText(query.value(9).toString()); |
48 ui->clEdit->setValue(query.value("chloride").toDouble()); |
47 ui->alkalinityEdit->setValue(query.value(10).toDouble()); |
49 sodium = query.value("sodium").toDouble(); |
|
50 ui->naEdit->setValue(query.value("sodium").toDouble()); |
|
51 magnesium = query.value("magnesium").toDouble(); |
|
52 ui->mgEdit->setValue(query.value("magnesium").toDouble()); |
|
53 ph = query.value("ph").toDouble(); |
|
54 ui->phEdit->setValue(query.value("ph").toDouble()); |
|
55 ui->notesEdit->setPlainText(query.value("notes").toString()); |
|
56 total_alkalinity = query.value("total_alkalinity").toDouble(); |
|
57 ui->alkalinityEdit->setValue(query.value("total_alkalinity").toDouble()); |
48 } else { |
58 } else { |
49 /* Set some defaults */ |
59 /* Set some defaults */ |
|
60 calcium = bicarbonate = sulfate = chloride = 0; |
|
61 sodium = magnesium = total_alkalinity = 0; |
|
62 ph = 7.0; |
50 ui->phEdit->setValue(7.0); |
63 ui->phEdit->setValue(7.0); |
51 } |
64 } |
|
65 ui->saveButton->setEnabled(false); |
|
66 ui->deleteButton->setEnabled((id >= 0) ? true:false); |
52 WaterSet(); |
67 WaterSet(); |
53 connect(ui->nameEdit, &QLineEdit::textChanged, this, &EditProfileWater::is_changed); |
68 connect(ui->nameEdit, &QLineEdit::textChanged, this, &EditProfileWater::is_changed); |
54 connect(ui->caEdit, &QDoubleSpinBox::textChanged, this, &EditProfileWater::water_changed); |
69 connect(ui->caEdit, QOverload<double>::of(&QDoubleSpinBox::valueChanged), this, &EditProfileWater::calcium_changed); |
55 connect(ui->hcoEdit, &QDoubleSpinBox::textChanged, this, &EditProfileWater::hco_changed); |
70 connect(ui->so4Edit, QOverload<double>::of(&QDoubleSpinBox::valueChanged), this, &EditProfileWater::sulfate_changed); |
56 connect(ui->so4Edit, &QDoubleSpinBox::textChanged, this, &EditProfileWater::water_changed); |
71 connect(ui->clEdit, QOverload<double>::of(&QDoubleSpinBox::valueChanged), this, &EditProfileWater::chloride_changed); |
57 connect(ui->clEdit, &QDoubleSpinBox::textChanged, this, &EditProfileWater::water_changed); |
72 connect(ui->naEdit, QOverload<double>::of(&QDoubleSpinBox::valueChanged), this, &EditProfileWater::sodium_changed); |
58 connect(ui->naEdit, &QDoubleSpinBox::textChanged, this, &EditProfileWater::water_changed); |
73 connect(ui->mgEdit, QOverload<double>::of(&QDoubleSpinBox::valueChanged), this, &EditProfileWater::magnesium_changed); |
59 connect(ui->mgEdit, &QDoubleSpinBox::textChanged, this, &EditProfileWater::water_changed); |
74 connect(ui->phEdit, QOverload<double>::of(&QDoubleSpinBox::valueChanged), this, &EditProfileWater::ph_changed); |
60 connect(ui->phEdit, &QDoubleSpinBox::textChanged, this, &EditProfileWater::water_changed); |
75 connect(ui->alkalinityEdit, QOverload<double>::of(&QDoubleSpinBox::valueChanged), this, &EditProfileWater::total_alkalinity_changed); |
61 connect(ui->alkalinityEdit, &QDoubleSpinBox::textChanged, this, &EditProfileWater::alkalinity_changed); |
|
62 connect(ui->notesEdit, SIGNAL(textChanged()), this, SLOT(is_changed())); |
76 connect(ui->notesEdit, SIGNAL(textChanged()), this, SLOT(is_changed())); |
63 |
|
64 ui->saveButton->setEnabled(false); |
|
65 ui->deleteButton->setEnabled((id >= 0) ? true:false); |
|
66 } |
77 } |
67 |
78 |
68 |
79 |
69 EditProfileWater::~EditProfileWater() |
80 EditProfileWater::~EditProfileWater() |
70 { |
81 { |
103 QMessageBox::warning(this, tr("Edit Water"), tr("Name empty or too short.")); |
114 QMessageBox::warning(this, tr("Edit Water"), tr("Name empty or too short.")); |
104 return; |
115 return; |
105 } |
116 } |
106 |
117 |
107 if (this->textIsChanged) { |
118 if (this->textIsChanged) { |
108 if (this->recno == -1) { |
119 if (recno == -1) { |
109 query.prepare("INSERT INTO profile_water SET name=:name, calcium=:ca, " |
120 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, " |
121 "bicarbonate=:hco, sulfate=:so4, chloride=:cl, sodium=:na, magnesium=:mg, ph=:ph, notes=:notes, " |
111 "total_alkalinity=:alkalinity, uuid = :uuid"); |
122 "total_alkalinity=:alkalinity, uuid = :uuid"); |
112 } else { |
123 } else { |
113 query.prepare("UPDATE profile_water SET name=:name, calcium=:ca, " |
124 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, " |
125 "bicarbonate=:hco, sulfate=:so4, chloride=:cl, sodium=:na, magnesium=:mg, ph=:ph, notes=:notes, " |
115 "total_alkalinity=:alkalinity WHERE record = :recno"); |
126 "total_alkalinity=:alkalinity WHERE record = :recno"); |
116 } |
127 } |
117 query.bindValue(":name", ui->nameEdit->text()); |
128 query.bindValue(":name", ui->nameEdit->text()); |
118 query.bindValue(":ca", QString("%1").arg(ui->caEdit->value(), 2, 'f', 1, '0')); |
129 query.bindValue(":ca", round(calcium * 1000) / 1000); |
119 query.bindValue(":hco", QString("%1").arg(ui->hcoEdit->value(), 2, 'f', 1, '0')); |
130 query.bindValue(":hco", round(bicarbonate * 1000) / 1000); |
120 query.bindValue(":so4", QString("%1").arg(ui->so4Edit->value(), 2, 'f', 1, '0')); |
131 query.bindValue(":so4", round(sulfate * 1000) / 1000); |
121 query.bindValue(":cl", QString("%1").arg(ui->clEdit->value(), 2, 'f', 1, '0')); |
132 query.bindValue(":cl", round(chloride * 1000) / 1000); |
122 query.bindValue(":na", QString("%1").arg(ui->naEdit->value(), 2, 'f', 1, '0')); |
133 query.bindValue(":na", round(sodium * 1000) / 1000); |
123 query.bindValue(":mg", QString("%1").arg(ui->mgEdit->value(), 2, 'f', 1, '0')); |
134 query.bindValue(":mg", round(magnesium * 1000) / 1000); |
124 query.bindValue(":ph", QString("%1").arg(ui->phEdit->value(), 3, 'f', 2, '0')); |
135 query.bindValue(":ph", round(ph * 1000) / 1000); |
125 query.bindValue(":notes", ui->notesEdit->toPlainText()); |
136 query.bindValue(":notes", ui->notesEdit->toPlainText()); |
126 query.bindValue(":alkalinity", QString("%1").arg(ui->alkalinityEdit->value(), 2, 'f', 1, '0')); |
137 query.bindValue(":alkalinity", round(total_alkalinity * 1000) / 1000); |
127 if (this->recno == -1) { |
138 if (recno == -1) { |
128 query.bindValue(":uuid", QUuid::createUuid().toString().mid(1, 36)); |
139 query.bindValue(":uuid", QUuid::createUuid().toString().mid(1, 36)); |
129 } else { |
140 } else { |
130 query.bindValue(":recno", this->recno); |
141 query.bindValue(":recno", this->recno); |
131 } |
142 } |
132 query.exec(); |
143 query.exec(); |
171 } |
182 } |
172 |
183 |
173 |
184 |
174 void EditProfileWater::WaterSet() |
185 void EditProfileWater::WaterSet() |
175 { |
186 { |
176 double cations, anions, balance; |
187 double CO3 = total_alkalinity * pow(10, ph - 10.33) / (1+2* pow(10, ph - 10.33)) * MMCO3 / (MMCaCO3 / 2); |
177 |
188 double t_bicarbonate = Utils::Bicarbonate(total_alkalinity, ph); |
178 cations = (ui->caEdit->value() / 20.039) + (ui->mgEdit->value() / 12.1525) + (ui->naEdit->value() / 22.989); |
189 qDebug() << t_bicarbonate << bicarbonate; |
179 anions = (ui->hcoEdit->value() / 61.016) + (ui->so4Edit->value() / 48.031) + (ui->clEdit->value() / 35.4527); |
190 if ((round(t_bicarbonate * 1000) / 1000) != bicarbonate) |
180 balance = round((cations - anions) * 100) / 100; |
191 is_changed(); |
|
192 bicarbonate = t_bicarbonate; |
|
193 ui->hcoEdit->setValue(bicarbonate); |
|
194 |
|
195 double cations = (2*calcium / MMCa) + (2*magnesium / MMMg) + (sodium / MMNa); |
|
196 double anions = (bicarbonate / MMHCO3) + (2*CO3 / MMCO3) + (2*sulfate / MMSO4) + (chloride / MMCl); |
|
197 double balance = round((cations - anions) * 100) / 100; |
181 ui->balanceEdit->setValue(balance); |
198 ui->balanceEdit->setValue(balance); |
|
199 ui->co3Edit->setValue(CO3); |
|
200 ui->hardnessEdit->setValue(Utils::Hardness(calcium, magnesium)); |
|
201 ui->raEdit->setValue(Utils::ResidualAlkalinity(total_alkalinity, calcium, magnesium)); |
182 |
202 |
183 if (balance <= 0.1 && balance >= -0.1) { |
203 if (balance <= 0.1 && balance >= -0.1) { |
184 ui->balanceIcon->setPixmap(QPixmap(":icons/silk/tick.png")); |
204 ui->balanceIcon->setPixmap(QPixmap(":icons/silk/tick.png")); |
185 } else if (balance <= 0.5 && balance >= -0.5) { |
205 } else if (balance <= 0.5 && balance >= -0.5) { |
186 ui->balanceIcon->setPixmap(QPixmap(":icons/silk/thumb_down.png")); |
206 ui->balanceIcon->setPixmap(QPixmap(":icons/silk/thumb_down.png")); |