src/EditProfileWater.cpp

changeset 353
324a7268796b
parent 90
2396457a8167
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 "EditProfileWater.h" 17 #include "EditProfileWater.h"
18 #include "../ui/ui_EditProfileWater.h" 18 #include "../ui/ui_EditProfileWater.h"
19 #include "global.h"
20 #include "Utils.h"
19 #include "MainWindow.h" 21 #include "MainWindow.h"
20 22
21 23
22 EditProfileWater::EditProfileWater(int id, QWidget *parent) : QDialog(parent), ui(new Ui::EditProfileWater) 24 EditProfileWater::EditProfileWater(int id, QWidget *parent) : QDialog(parent), ui(new Ui::EditProfileWater)
23 { 25 {
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 {
79 */ 90 */
80 void EditProfileWater::WindowTitle() 91 void EditProfileWater::WindowTitle()
81 { 92 {
82 QString txt; 93 QString txt;
83 94
84 if (this->recno < 0) { 95 if (recno < 0) {
85 txt = QString(tr("BMSapp - Add new brewing water")); 96 txt = QString(tr("BMSapp - Add new brewing water"));
86 } else { 97 } else {
87 txt = QString(tr("BMSapp - Edit brewing water %1").arg(this->recno)); 98 txt = QString(tr("BMSapp - Edit brewing water %1").arg(recno));
88 } 99 }
89 100
90 if (this->textIsChanged) { 101 if (this->textIsChanged) {
91 txt.append((QString(" **"))); 102 txt.append((QString(" **")));
92 } 103 }
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"));
204 WaterSet(); 224 WaterSet();
205 is_changed(); 225 is_changed();
206 } 226 }
207 227
208 228
209 void EditProfileWater::hco_changed() 229 void EditProfileWater::calcium_changed(double val)
210 { 230 {
211 ui->alkalinityEdit->setValue(ui->hcoEdit->value() * 50 / 61); 231 calcium = val;
212 water_changed(); 232 water_changed();
213 } 233 }
214 234
215 235
216 void EditProfileWater::alkalinity_changed() 236 void EditProfileWater::total_alkalinity_changed(double val)
217 { 237 {
218 ui->hcoEdit->setValue(ui->alkalinityEdit->value() * 1.22); 238 total_alkalinity = val;
239 water_changed();
240 }
241
242
243 void EditProfileWater::sulfate_changed(double val)
244 {
245 sulfate = val;
246 water_changed();
247 }
248
249
250 void EditProfileWater::chloride_changed(double val)
251 {
252 chloride = val;
253 water_changed();
254 }
255
256
257 void EditProfileWater::sodium_changed(double val)
258 {
259 sodium = val;
260 water_changed();
261 }
262
263
264 void EditProfileWater::magnesium_changed(double val)
265 {
266 magnesium = val;
267 water_changed();
268 }
269
270
271 void EditProfileWater::ph_changed(double val)
272 {
273 ph = val;
219 water_changed(); 274 water_changed();
220 } 275 }
221 276
222 277
223 void EditProfileWater::on_quitButton_clicked() 278 void EditProfileWater::on_quitButton_clicked()

mercurial