src/EditRecipe.cpp

changeset 110
224be4d9f8eb
parent 109
1ce50e72a6b1
child 111
04f5a7c5a1dc
equal deleted inserted replaced
109:1ce50e72a6b1 110:224be4d9f8eb
18 #include "EditRecipe.h" 18 #include "EditRecipe.h"
19 #include "../ui/ui_EditRecipe.h" 19 #include "../ui/ui_EditRecipe.h"
20 #include "Utils.h" 20 #include "Utils.h"
21 21
22 22
23
23 EditRecipe::EditRecipe(int id, QWidget *parent) : QDialog(parent), ui(new Ui::EditRecipe) 24 EditRecipe::EditRecipe(int id, QWidget *parent) : QDialog(parent), ui(new Ui::EditRecipe)
24 { 25 {
25 QSqlQuery query; 26 QSqlQuery query;
26 27
27 qDebug() << "EditRecipe record:" << id; 28 qDebug() << "EditRecipe record:" << id;
28 recipe = new Recipe; 29 recipe = new Recipe;
29 ui->setupUi(this); 30 ui->setupUi(this);
30 recipe->fermentables_records = recipe->hops_records = recipe->misc_records = recipe->yeasts_records = recipe->mashs_records = 0;
31 recipe->fermentables_current = recipe->hops_current = recipe->misc_current = recipe->yeasts_current = recipe->mashs_current = -1; 31 recipe->fermentables_current = recipe->hops_current = recipe->misc_current = recipe->yeasts_current = recipe->mashs_current = -1;
32 recipe->fermentables_use100 = false; 32 recipe->fermentables_use100 = false;
33 this->recno = id; 33 this->recno = id;
34 34
35 WindowTitle(); 35 WindowTitle();
152 152
153 QJsonParseError parseError; 153 QJsonParseError parseError;
154 const auto& f_json = query.value(84).toString(); 154 const auto& f_json = query.value(84).toString();
155 if (!f_json.trimmed().isEmpty()) { 155 if (!f_json.trimmed().isEmpty()) {
156 const auto& formattedJson = QString("%1").arg(f_json); 156 const auto& formattedJson = QString("%1").arg(f_json);
157 this->fermentables = QJsonDocument::fromJson(formattedJson.toUtf8(), &parseError); 157 QJsonDocument fermentables = QJsonDocument::fromJson(formattedJson.toUtf8(), &parseError);
158 if (parseError.error != QJsonParseError::NoError) 158 if (parseError.error != QJsonParseError::NoError) {
159 qDebug() << "Parse error: " << parseError.errorString() << "at" << parseError.offset ; 159 qDebug() << "Parse error: " << parseError.errorString() << "at" << parseError.offset ;
160 } else if (fermentables.isArray()) {
161 for (int i = 0; i < fermentables.array().size(); i++) {
162 QJsonObject obj = fermentables.array().at(i).toObject();
163 Fermentables f;
164 f.f_name = obj["f_name"].toString();
165 f.f_origin = obj["f_origin"].toString();
166 f.f_supplier = obj["f_supplier"].toString();
167 f.f_amount = obj["f_amount"].toDouble();
168 f.f_cost = obj["f_cost"].toDouble();
169 f.f_type = obj["f_type"].toInt();
170 f.f_yield = obj["f_yield"].toDouble();
171 f.f_color = obj["f_color"].toDouble();
172 f.f_coarse_fine_diff = obj["f_coarse_fine_diff"].toDouble();
173 f.f_moisture = obj["f_moisture"].toDouble();
174 f.f_diastatic_power = obj["f_diastatic_power"].toDouble();
175 f.f_protein = obj["f_protein"].toDouble();
176 f.f_dissolved_protein = obj["f_dissolved_protein"].toDouble();
177 f.f_max_in_batch = obj["f_max_in_batch"].toDouble();
178 f.f_graintype = obj["f_graintype"].toInt();
179 f.f_added = obj["f_added"].toInt();
180 f.f_recommend_mash = obj["f_recommend_mash"].toInt() ? true:false;
181 f.f_add_after_boil = obj["f_add_after_boil"].toInt() ? true:false;
182 f.f_adjust_to_total_100 = obj["f_adjust_to_total_100"].toInt() ? true:false;
183 f.f_percentage = obj["f_percentage"].toDouble();
184 f.f_di_ph = obj["f_di_ph"].toDouble();
185 f.f_acid_to_ph_57 = obj["f_acid_to_ph_57"].toDouble();
186 if (f.f_adjust_to_total_100)
187 recipe->fermentables_use100 = true;
188 recipe->fermentables.append(f);
189 }
190 qDebug() << "fermentables" << recipe->fermentables.size();
191 }
160 } else { 192 } else {
161 qDebug() << "empty fermentables"; 193 qDebug() << "empty fermentables";
162 } 194 }
163 195
164 const auto& h_json = query.value(85).toString(); 196 const auto& h_json = query.value(85).toString();
193 225
194 const auto& ma_json = query.value(88).toString(); 226 const auto& ma_json = query.value(88).toString();
195 if (!ma_json.trimmed().isEmpty()) { 227 if (!ma_json.trimmed().isEmpty()) {
196 const auto& formattedJson = QString("%1").arg(ma_json); 228 const auto& formattedJson = QString("%1").arg(ma_json);
197 QJsonDocument mashs = QJsonDocument::fromJson(formattedJson.toUtf8(), &parseError); 229 QJsonDocument mashs = QJsonDocument::fromJson(formattedJson.toUtf8(), &parseError);
198 if (parseError.error != QJsonParseError::NoError) 230 if (parseError.error != QJsonParseError::NoError) {
199 qDebug() << "Parse error: " << parseError.errorString() << "at" << parseError.offset ; 231 qDebug() << "Parse error: " << parseError.errorString() << "at" << parseError.offset ;
200 232 } else if (mashs.isArray()) {
201 if (mashs.isArray()) {
202 for (int i = 0; i < mashs.array().size(); i++) { 233 for (int i = 0; i < mashs.array().size(); i++) {
203 QJsonObject obj = mashs.array().at(i).toObject(); 234 QJsonObject obj = mashs.array().at(i).toObject();
204 235 // qDebug() << i << obj;
205 qDebug() << i << obj;
206 Mashs m; 236 Mashs m;
207 m.step_name = obj["step_name"].toString(); 237 m.step_name = obj["step_name"].toString();
208 m.step_type = obj["step_type"].toInt(); 238 m.step_type = obj["step_type"].toInt();
209 m.step_volume = obj["step_volume"].toDouble(); 239 m.step_volume = obj["step_volume"].toDouble();
210 m.step_infuse_amount = obj["step_infuse_amount"].toDouble(); 240 m.step_infuse_amount = obj["step_infuse_amount"].toDouble();
218 } 248 }
219 } 249 }
220 } else { 250 } else {
221 qDebug() << "empty mashs"; 251 qDebug() << "empty mashs";
222 } 252 }
223 recipe->mashs_records = recipe->mashs.size(); 253 qDebug() << "mashs" << recipe->mashs.size();
224
225 // qDebug() << query.value(88).toString();
226 // if (recipe->mashs.size()) {
227 qDebug() << recipe->mashs.size();
228 // }
229 //qDebug() << recipe->mashs;
230 254
231 } else { 255 } else {
232 /* Set some defaults */ 256 /* Set some defaults */
233 recipe->locked = false; 257 recipe->locked = false;
234 recipe->st_name = ""; 258 recipe->st_name = "";
296 recipe->wa_acid_name = 0; 320 recipe->wa_acid_name = 0;
297 recipe->wa_acid_perc = 80; 321 recipe->wa_acid_perc = 80;
298 recipe->wa_base_name = 0; 322 recipe->wa_base_name = 0;
299 323
300 const auto& formattedJson = QString("[]"); 324 const auto& formattedJson = QString("[]");
301 this->fermentables = QJsonDocument::fromJson(formattedJson.toUtf8());
302 this->hops = QJsonDocument::fromJson(formattedJson.toUtf8()); 325 this->hops = QJsonDocument::fromJson(formattedJson.toUtf8());
303 this->miscs = QJsonDocument::fromJson(formattedJson.toUtf8()); 326 this->miscs = QJsonDocument::fromJson(formattedJson.toUtf8());
304 this->yeasts = QJsonDocument::fromJson(formattedJson.toUtf8()); 327 this->yeasts = QJsonDocument::fromJson(formattedJson.toUtf8());
305 } 328 }
306 329
386 connect(ui->est_og2Edit, &QDoubleSpinBox::textChanged, this, &EditRecipe::is_changed); 409 connect(ui->est_og2Edit, &QDoubleSpinBox::textChanged, this, &EditRecipe::is_changed);
387 connect(ui->perc_mashShow, &QProgressBar::valueChanged, this, &EditRecipe::on_perc_mash_valueChanged); 410 connect(ui->perc_mashShow, &QProgressBar::valueChanged, this, &EditRecipe::on_perc_mash_valueChanged);
388 connect(ui->perc_sugarsShow, &QProgressBar::valueChanged, this, &EditRecipe::on_perc_sugars_valueChanged); 411 connect(ui->perc_sugarsShow, &QProgressBar::valueChanged, this, &EditRecipe::on_perc_sugars_valueChanged);
389 connect(ui->perc_caraShow, &QProgressBar::valueChanged, this, &EditRecipe::on_perc_cara_valueChanged); 412 connect(ui->perc_caraShow, &QProgressBar::valueChanged, this, &EditRecipe::on_perc_cara_valueChanged);
390 connect(ui->lintnerShow, &QProgressBar::valueChanged, this, &EditRecipe::on_lintner_valueChanged); 413 connect(ui->lintnerShow, &QProgressBar::valueChanged, this, &EditRecipe::on_lintner_valueChanged);
391 connect(ui->fermentablesTable, SIGNAL(cellChanged(int, int)), this, SLOT(cell_Fermentable_changed(int, int)));
392 414
393 // All signals from tab "Hops" 415 // All signals from tab "Hops"
394 // connect(ui->hopsTable, SIGNAL(cellChanged(int, int)), this, SLOT(cell_Changed(int, int))); 416 // connect(ui->hopsTable, SIGNAL(cellChanged(int, int)), this, SLOT(cell_Changed(int, int)));
395 417
396 // All signals from tab "Miscs" 418 // All signals from tab "Miscs"
415 { 437 {
416 qDebug() << "EditRecipe done start"; 438 qDebug() << "EditRecipe done start";
417 delete ui; 439 delete ui;
418 emit entry_changed(); 440 emit entry_changed();
419 qDebug() << "EditRecipe done final"; 441 qDebug() << "EditRecipe done final";
442 }
443
444
445 bool EditRecipe::ferment_sort_test(const Fermentables &D1, const Fermentables &D2)
446 {
447 return (D1.f_added <= D2.f_added) && (D1.f_amount >= D2.f_amount) && (D1.f_color < D2.f_color);
420 } 448 }
421 449
422 450
423 void EditRecipe::refreshFermentables() 451 void EditRecipe::refreshFermentables()
424 { 452 {
425 QString w; 453 QString w;
426 QWidget* pWidget; 454 QWidget* pWidget;
427 QHBoxLayout* pLayout; 455 QHBoxLayout* pLayout;
428 QTableWidgetItem *item; 456 QTableWidgetItem *item;
429 QLabel *label; 457 QLabel *label;
430 double d; 458
431 int j; 459 qDebug() << "refreshFermentables" << recipe->fermentables.size();
432 int total = 0; 460 std::sort(recipe->fermentables.begin(), recipe->fermentables.end(), ferment_sort_test);
433 461
434 qDebug() << "refreshFermentables" << this->fermentables << this->fermentables.isArray() << this->fermentables.array().size(); 462 /*
435 /* During filling the table turn off the cellChanged signal because every cell that is filled 463 * During filling the table turn off the cellChanged signal because every cell that is filled
436 * triggers the cellChanged signal. The QTableWidget has no better signal to use. */ 464 * triggers the cellChanged signal. The QTableWidget has no better signal to use.
465 */
437 this->ignoreChanges = true; 466 this->ignoreChanges = true;
438 467
439 const QStringList labels({tr("Supplier"), tr("Fermentable"), tr("EBC"), tr("Type"), tr("Graintype"), tr("When"), tr("Yield"), 468 const QStringList labels({tr("Supplier"), tr("Fermentable"), tr("EBC"), tr("Type"), tr("Graintype"), tr("When"), tr("Yield"),
440 tr("Amount"), tr("Procent"), tr("100%"), tr("Delete"), tr("Edit") }); 469 tr("Amount"), tr("Procent"), tr("100%"), tr("Delete"), tr("Edit") });
441 ui->fermentablesTable->setColumnCount(30); 470 ui->fermentablesTable->setColumnCount(12);
442 ui->fermentablesTable->setColumnWidth(0, 150); /* Supplier */ 471 ui->fermentablesTable->setColumnWidth(0, 150); /* Supplier */
443 ui->fermentablesTable->setColumnWidth(1, 225); /* Fermentable */ 472 ui->fermentablesTable->setColumnWidth(1, 225); /* Fermentable */
444 ui->fermentablesTable->setColumnWidth(2, 50); /* Color */ 473 ui->fermentablesTable->setColumnWidth(2, 50); /* Color */
445 ui->fermentablesTable->setColumnWidth(3, 75); /* Type */ 474 ui->fermentablesTable->setColumnWidth(3, 75); /* Type */
446 ui->fermentablesTable->setColumnWidth(4, 75); /* Graintype */ 475 ui->fermentablesTable->setColumnWidth(4, 75); /* Graintype */
449 ui->fermentablesTable->setColumnWidth(7, 90); /* Amount */ 478 ui->fermentablesTable->setColumnWidth(7, 90); /* Amount */
450 ui->fermentablesTable->setColumnWidth(8, 60); /* Procent */ 479 ui->fermentablesTable->setColumnWidth(8, 60); /* Procent */
451 ui->fermentablesTable->setColumnWidth(9, 50); /* 100% */ 480 ui->fermentablesTable->setColumnWidth(9, 50); /* 100% */
452 ui->fermentablesTable->setColumnWidth(10, 80); /* Delete */ 481 ui->fermentablesTable->setColumnWidth(10, 80); /* Delete */
453 ui->fermentablesTable->setColumnWidth(11, 80); /* Edit */ 482 ui->fermentablesTable->setColumnWidth(11, 80); /* Edit */
454 for (int i = 12; i < 30; i++)
455 ui->fermentablesTable->setColumnHidden(i, true);
456 ui->fermentablesTable->setHorizontalHeaderLabels(labels); 483 ui->fermentablesTable->setHorizontalHeaderLabels(labels);
457 ui->fermentablesTable->verticalHeader()->hide(); 484 ui->fermentablesTable->verticalHeader()->hide();
458 ui->fermentablesTable->setRowCount(this->fermentables.array().size()); 485 ui->fermentablesTable->setRowCount(recipe->fermentables.size());
459 486
460 if (this->fermentables.isArray()) { 487 for (int i = 0; i < recipe->fermentables.size(); i++) {
461 for (int i = 0; i < this->fermentables.array().size(); i++) { 488
462 QJsonObject obj = this->fermentables.array().at(i).toObject(); 489 ui->fermentablesTable->setItem(i, 0, new QTableWidgetItem(recipe->fermentables.at(i).f_supplier));
463 490 ui->fermentablesTable->setItem(i, 1, new QTableWidgetItem(recipe->fermentables.at(i).f_name));
464 ui->fermentablesTable->setItem(i, 0, new QTableWidgetItem(obj["f_supplier"].toString())); 491
465 ui->fermentablesTable->setItem(i, 1, new QTableWidgetItem(obj["f_name"].toString())); 492 w = QString("%1").arg(recipe->fermentables.at(i).f_color, 1, 'f', 0, '0');
466 493 item = new QTableWidgetItem(w);
467 w = QString("%1").arg(obj["f_color"].toDouble(), 1, 'f', 0, '0'); 494 item->setTextAlignment(Qt::AlignRight|Qt::AlignVCenter);
468 item = new QTableWidgetItem(w); 495 ui->fermentablesTable->setItem(i, 2, item);
469 item->setTextAlignment(Qt::AlignRight|Qt::AlignVCenter); 496
470 ui->fermentablesTable->setItem(i, 2, item); 497 item = new QTableWidgetItem(f_types[recipe->fermentables.at(i).f_type]);
471 498 item->setTextAlignment(Qt::AlignCenter|Qt::AlignVCenter);
472 item = new QTableWidgetItem(f_types[obj["f_type"].toInt()]); 499 ui->fermentablesTable->setItem(i, 3, item);
473 item->setTextAlignment(Qt::AlignCenter|Qt::AlignVCenter); 500
474 ui->fermentablesTable->setItem(i, 3, item); 501 item = new QTableWidgetItem(f_graintypes[recipe->fermentables.at(i).f_graintype]);
475 502 item->setTextAlignment(Qt::AlignCenter|Qt::AlignVCenter);
476 item = new QTableWidgetItem(f_graintypes[obj["f_graintype"].toInt()]); 503 ui->fermentablesTable->setItem(i, 4, item);
477 item->setTextAlignment(Qt::AlignCenter|Qt::AlignVCenter); 504
478 ui->fermentablesTable->setItem(i, 4, item); 505 item = new QTableWidgetItem(f_added[recipe->fermentables.at(i).f_added]);
479 506 item->setTextAlignment(Qt::AlignCenter|Qt::AlignVCenter);
480 item = new QTableWidgetItem(f_added[obj["f_added"].toInt()]); 507 ui->fermentablesTable->setItem(i, 5, item);
481 item->setTextAlignment(Qt::AlignCenter|Qt::AlignVCenter); 508
482 ui->fermentablesTable->setItem(i, 5, item); 509 item = new QTableWidgetItem(QString("%1%").arg(recipe->fermentables.at(i).f_yield, 2, 'f', 1, '0'));
483 510 item->setTextAlignment(Qt::AlignRight|Qt::AlignVCenter);
484 item = new QTableWidgetItem(QString("%1%").arg(obj["f_yield"].toDouble(), 2, 'f', 1, '0')); 511 ui->fermentablesTable->setItem(i, 6, item);
485 item->setTextAlignment(Qt::AlignRight|Qt::AlignVCenter); 512
486 ui->fermentablesTable->setItem(i, 6, item); 513 item = new QTableWidgetItem(QString("%1 Kg").arg(recipe->fermentables.at(i).f_amount, 4, 'f', 3, '0'));
487 514 item->setTextAlignment(Qt::AlignRight|Qt::AlignVCenter);
488 item = new QTableWidgetItem(QString("%1 Kg").arg(obj["f_amount"].toDouble(), 4, 'f', 3, '0')); 515 ui->fermentablesTable->setItem(i, 7, item);
489 item->setTextAlignment(Qt::AlignRight|Qt::AlignVCenter); 516
490 ui->fermentablesTable->setItem(i, 7, item); 517 item = new QTableWidgetItem(QString("%1%").arg(recipe->fermentables.at(i).f_percentage, 2, 'f', 1, '0'));
491 518 item->setTextAlignment(Qt::AlignRight|Qt::AlignVCenter);
492 item = new QTableWidgetItem(QString("%1%").arg(obj["f_percentage"].toDouble(), 2, 'f', 1, '0')); 519 ui->fermentablesTable->setItem(i, 8, item);
493 item->setTextAlignment(Qt::AlignRight|Qt::AlignVCenter); 520
494 ui->fermentablesTable->setItem(i, 8, item); 521 if (recipe->fermentables.at(i).f_adjust_to_total_100) {
495
496 if (obj["f_adjust_to_total_100"].toInt()) {
497 pWidget = new QWidget(); 522 pWidget = new QWidget();
498 label = new QLabel; 523 label = new QLabel;
499 label->setPixmap(QPixmap(":icons/silk/tick.png")); 524 label->setPixmap(QPixmap(":icons/silk/tick.png"));
500 pLayout = new QHBoxLayout(pWidget); 525 pLayout = new QHBoxLayout(pWidget);
501 pLayout->addWidget(label); 526 pLayout->addWidget(label);
502 pLayout->setAlignment(Qt::AlignCenter); 527 pLayout->setAlignment(Qt::AlignCenter);
503 pLayout->setContentsMargins(0, 0, 0, 0); 528 pLayout->setContentsMargins(0, 0, 0, 0);
504 pWidget->setLayout(pLayout); 529 pWidget->setLayout(pLayout);
505 ui->fermentablesTable->setCellWidget(i, 9, pWidget); 530 ui->fermentablesTable->setCellWidget(i, 9, pWidget);
506 } else { 531 } else {
507 ui->fermentablesTable->removeCellWidget(i, 9); 532 ui->fermentablesTable->removeCellWidget(i, 9);
508 }
509
510 /* Add the Delete row button */
511 pWidget = new QWidget();
512 QPushButton* btn_dele = new QPushButton();
513 btn_dele->setObjectName(QString("%1").arg(i)); /* Send row with the button */
514 btn_dele->setText(tr("Delete"));
515 connect(btn_dele, SIGNAL(clicked()), this, SLOT(on_deleteFermentRow_clicked()));
516 pLayout = new QHBoxLayout(pWidget);
517 pLayout->addWidget(btn_dele);
518 pLayout->setContentsMargins(5, 0, 5, 0);
519 pWidget->setLayout(pLayout);
520 ui->fermentablesTable->setCellWidget(i, 10, pWidget);
521
522 pWidget = new QWidget();
523 QPushButton* btn_edit = new QPushButton();
524 btn_edit->setObjectName(QString("%1").arg(i)); /* Send row with the button */
525 btn_edit->setText(tr("Edit"));
526 connect(btn_edit, SIGNAL(clicked()), this, SLOT(on_editFermentRow_clicked()));
527 pLayout = new QHBoxLayout(pWidget);
528 pLayout->addWidget(btn_edit);
529 pLayout->setContentsMargins(5, 0, 5, 0);
530 pWidget->setLayout(pLayout);
531 ui->fermentablesTable->setCellWidget(i, 11, pWidget);
532
533 /* Hidden columns */
534 ui->fermentablesTable->setItem(i, 12, new QTableWidgetItem(QString("%1").arg(obj["f_acid_to_ph_57"].toDouble(), 6, 'f', 5, '0')));
535 ui->fermentablesTable->setItem(i, 13, new QTableWidgetItem(QString("%1").arg(obj["f_add_after_boil"].toInt(), 1, 'f', 0, '0')));
536 ui->fermentablesTable->setItem(i, 14, new QTableWidgetItem(QString("%1").arg(obj["f_coarse_fine_diff"].toDouble(), 2, 'f', 1, '0')));
537 ui->fermentablesTable->setItem(i, 15, new QTableWidgetItem(QString("%1").arg(obj["f_cost"].toDouble(), 3, 'f', 2, '0')));
538 ui->fermentablesTable->setItem(i, 16, new QTableWidgetItem(QString("%1").arg(obj["f_di_ph"].toDouble(), 6, 'f', 5, '0')));
539 ui->fermentablesTable->setItem(i, 17, new QTableWidgetItem(QString("%1").arg(obj["f_diastatic_power"].toDouble(), 6, 'f', 5, '0')));
540 ui->fermentablesTable->setItem(i, 18, new QTableWidgetItem(QString("%1").arg(obj["f_dissolved_protein"].toDouble(), 2, 'f', 1, '0')));
541 ui->fermentablesTable->setItem(i, 19, new QTableWidgetItem(QString("%1").arg(obj["f_moisture"].toDouble(), 2, 'f', 1, '0')));
542 ui->fermentablesTable->setItem(i, 20, new QTableWidgetItem(obj["f_origin"].toString()));
543 ui->fermentablesTable->setItem(i, 21, new QTableWidgetItem(QString("%1").arg(obj["f_protein"].toDouble(), 2, 'f', 1, '0')));
544 ui->fermentablesTable->setItem(i, 22, new QTableWidgetItem(QString("%1").arg(obj["f_recommend_mash"].toInt(), 1, 'f', 0, '0')));
545 /* Again these because the visible ones have human readable strings added. */
546 ui->fermentablesTable->setItem(i, 23, new QTableWidgetItem(QString("%1").arg(obj["f_amount"].toDouble(), 4, 'f', 3, '0')));
547 ui->fermentablesTable->setItem(i, 24, new QTableWidgetItem(QString("%1").arg(obj["f_percentage"].toDouble(), 2, 'f', 1, '0')));
548 ui->fermentablesTable->setItem(i, 25, new QTableWidgetItem(QString("%1").arg(obj["f_yield"].toDouble(), 2, 'f', 1, '0')));
549 ui->fermentablesTable->setItem(i, 26, new QTableWidgetItem(QString("%1").arg(obj["f_max_in_batch"].toDouble(), 2, 'f', 1, '0')));
550 ui->fermentablesTable->setItem(i, 27, new QTableWidgetItem(QString("%1").arg(obj["f_type"].toDouble(), 1, 'f', 0, '0')));
551 ui->fermentablesTable->setItem(i, 28, new QTableWidgetItem(QString("%1").arg(obj["f_graintype"].toDouble(), 1, 'f', 0, '0')));
552 ui->fermentablesTable->setItem(i, 29, new QTableWidgetItem(QString("%1").arg(obj["f_added"].toDouble(), 1, 'f', 0, '0')));
553 } 533 }
554 } 534
555 535 /* Add the Delete row button */
536 pWidget = new QWidget();
537 QPushButton* btn_dele = new QPushButton();
538 btn_dele->setObjectName(QString("%1").arg(i)); /* Send row with the button */
539 btn_dele->setText(tr("Delete"));
540 connect(btn_dele, SIGNAL(clicked()), this, SLOT(on_deleteFermentRow_clicked()));
541 pLayout = new QHBoxLayout(pWidget);
542 pLayout->addWidget(btn_dele);
543 pLayout->setContentsMargins(5, 0, 5, 0);
544 pWidget->setLayout(pLayout);
545 ui->fermentablesTable->setCellWidget(i, 10, pWidget);
546
547 pWidget = new QWidget();
548 QPushButton* btn_edit = new QPushButton();
549 btn_edit->setObjectName(QString("%1").arg(i)); /* Send row with the button */
550 btn_edit->setText(tr("Edit"));
551 connect(btn_edit, SIGNAL(clicked()), this, SLOT(on_editFermentRow_clicked()));
552 pLayout = new QHBoxLayout(pWidget);
553 pLayout->addWidget(btn_edit);
554 pLayout->setContentsMargins(5, 0, 5, 0);
555 pWidget->setLayout(pLayout);
556 ui->fermentablesTable->setCellWidget(i, 11, pWidget);
557 }
556 this->ignoreChanges = false; 558 this->ignoreChanges = false;
557 } 559 }
558 560
559 561
560 void EditRecipe::refreshHops() 562 void EditRecipe::refreshHops()
605 double colorh = 0; // Colors ebc * vol * kt 607 double colorh = 0; // Colors ebc * vol * kt
606 double colorn = 0; // Colors ebc * pt * pct 608 double colorn = 0; // Colors ebc * pt * pct
607 QJsonObject obj; 609 QJsonObject obj;
608 610
609 qDebug() << "calcFermentables()"; 611 qDebug() << "calcFermentables()";
610 use_to100 = false;
611 612
612 /* 613 /*
613 * Get average mashtemp and mashtime from the Mash schedule. 614 * Get average mashtemp and mashtime from the Mash schedule.
614 * It is possible that the schedule is not (yet) present. 615 * It is possible that the schedule is not (yet) present.
615 */ 616 */
627 qDebug() << " mash time" << mashtime << "temp" << mashtemp << "infuse" << mashinfuse; 628 qDebug() << " mash time" << mashtime << "temp" << mashtemp << "infuse" << mashinfuse;
628 } else { 629 } else {
629 qDebug() << " no mash schedule"; 630 qDebug() << " no mash schedule";
630 } 631 }
631 632
632 if (this->fermentables.array().size() < 1) { 633 if (recipe->fermentables.size() < 1) {
633 qDebug() << " no fermentables, return."; 634 qDebug() << " no fermentables, return.";
634 return; 635 return;
635 } 636 }
636 637 qDebug() << " adjust to 100" << recipe->fermentables_use100;
637 for (i = 0; i < this->fermentables.array().size(); i++) { 638
638 obj = this->fermentables.array().at(i).toObject(); 639 for (i = 0; i < recipe->fermentables.size(); i++) {
639 if (obj["f_adjust_to_total_100"].toInt()) 640 if (recipe->fermentables.at(i).f_type == 1 && recipe->fermentables.at(i).f_added < 4) // Sugars
640 use_to100 = true; 641 psugar += recipe->fermentables.at(i).f_percentage;
641 if (obj["f_type"].toInt() == 1 && obj["f_added"].toInt() < 4) // Sugars 642 if (recipe->fermentables.at(i).f_graintype == 2 && recipe->fermentables.at(i).f_added < 4) // Crystal/Cara
642 psugar += obj["f_percentage"].toDouble(); 643 pcara += recipe->fermentables.at(i).f_percentage;
643 if (obj["f_graintype"].toInt() == 2 && obj["f_added"].toInt() < 4) // Crystal/Cara 644 d = recipe->fermentables.at(i).f_amount * (recipe->fermentables.at(i).f_yield / 100) * (1 - recipe->fermentables.at(i).f_moisture / 100);
644 pcara += obj["f_percentage"].toDouble(); 645 if (recipe->fermentables.at(i).f_added == 0) { // Mash
645 d = obj["f_amount"].toDouble() * (obj["f_yield"].toDouble() / 100) * (1 - obj["f_moisture"].toDouble() / 100);
646 if (obj["f_added"].toInt() == 0) { // Mash
647 if (mvol > 0) { // If mash volume is known 646 if (mvol > 0) { // If mash volume is known
648 mvol += obj["f_amount"].toDouble() * obj["f_moisture"].toDouble() / 100; 647 mvol += recipe->fermentables.at(i).f_amount * recipe->fermentables.at(i).f_moisture / 100;
649 s += d; 648 s += d;
650 } 649 }
651 d = ui->efficiencyEdit->value() / 100 * d; 650 d = ui->efficiencyEdit->value() / 100 * d;
652 sugarsm += d; 651 sugarsm += d;
653 mashkg += obj["f_amount"].toDouble(); 652 mashkg += recipe->fermentables.at(i).f_amount;
654 } 653 }
655 if (obj["f_added"].toInt() == 0 || obj["f_added"].toInt() == 1) // Mash or boil 654 if (recipe->fermentables.at(i).f_added == 0 || recipe->fermentables.at(i).f_added == 1) // Mash or boil
656 sugarsf += d; 655 sugarsf += d;
657 if (obj["f_added"].toInt() == 2 || obj["f_added"].toInt() == 3) { // Fermentation or lagering 656 if (recipe->fermentables.at(i).f_added == 2 || recipe->fermentables.at(i).f_added == 3) { // Fermentation or lagering
658 x = (obj["f_yield"].toDouble() / 100) * (1 - obj["f_moisture"].toDouble() / 100); 657 x = (recipe->fermentables.at(i).f_yield / 100) * (1 - recipe->fermentables.at(i).f_moisture / 100);
659 addedS += obj["f_amount"].toDouble() * x; 658 addedS += recipe->fermentables.at(i).f_amount * x;
660 addedmass += obj["f_amount"].toDouble(); 659 addedmass += recipe->fermentables.at(i).f_amount;
661 vol += (x * sugardensity + (1 - x) * 1) * obj["f_amount"].toDouble(); 660 vol += (x * sugardensity + (1 - x) * 1) * recipe->fermentables.at(i).f_amount;
662 } 661 }
663 if (obj["f_added"].toInt() == 0 && (obj["f_type"].toInt() == 0 || obj["f_type"].toInt() == 4) && obj["f_color"].toDouble() < 50) { 662 if (recipe->fermentables.at(i).f_added == 0 && (recipe->fermentables.at(i).f_type == 0 || recipe->fermentables.at(i).f_type == 4) && recipe->fermentables.at(i).f_color < 50) {
664 lintner += obj["f_diastatic_power"].toDouble() * obj["f_amount"].toDouble(); 663 lintner += recipe->fermentables.at(i).f_diastatic_power * recipe->fermentables.at(i).f_amount;
665 } 664 }
666 if (obj["f_added"].toInt() < 4) { 665 if (recipe->fermentables.at(i).f_added < 4) {
667 colort += obj["f_amount"].toDouble() * Utils::ebc_to_srm(obj["f_color"].toDouble()); 666 colort += recipe->fermentables.at(i).f_amount * Utils::ebc_to_srm(recipe->fermentables.at(i).f_color);
668 colorh += obj["f_amount"].toDouble() * obj["f_color"].toDouble() * Utils::get_kt(obj["f_color"].toDouble()); 667 colorh += recipe->fermentables.at(i).f_amount * recipe->fermentables.at(i).f_color * Utils::get_kt(recipe->fermentables.at(i).f_color);
669 colorn += (obj["f_percentage"].toDouble() / 100) * obj["f_color"].toDouble(); // For 8.6 Pt wort. 668 colorn += (recipe->fermentables.at(i).f_percentage / 100) * recipe->fermentables.at(i).f_color; // For 8.6 Pt wort.
670 } 669 }
671 } 670 }
672 qDebug() << " colort" << colort << "colorh" << colorh << "colorn" << colorn; 671 qDebug() << " colort" << colort << "colorh" << colorh << "colorn" << colorn;
673 qDebug() << " psugar" << psugar << "pcara" << pcara << "mvol" << mvol; 672 qDebug() << " psugar" << psugar << "pcara" << pcara << "mvol" << mvol;
674 qDebug() << " sugarsf" << sugarsf << "sugarsm" << sugarsm; 673 qDebug() << " sugarsf" << sugarsf << "sugarsm" << sugarsm;
752 * Take the alcohol and sugar parts and then combine. 751 * Take the alcohol and sugar parts and then combine.
753 */ 752 */
754 double alc = 1881.22 * fg * (og - fg) / (1.775 - og); 753 double alc = 1881.22 * fg * (og - fg) / (1.775 - og);
755 double sug = 3550 * fg * (0.1808 * og + 0.8192 * fg - 1.0004); 754 double sug = 3550 * fg * (0.1808 * og + 0.8192 * fg - 1.0004);
756 ui->calEdit->setValue(round((alc + sug) / (12 * 0.0295735296))); 755 ui->calEdit->setValue(round((alc + sug) / (12 * 0.0295735296)));
757
758 // If to_100 then make all amount fields t/o and percent fields r/w
759
760 } 756 }
761 757
762 758
763 void EditRecipe::on_perc_mash_valueChanged(int value) 759 void EditRecipe::on_perc_mash_valueChanged(int value)
764 { 760 {
1001 calcFermentables(); 997 calcFermentables();
1002 is_changed(); 998 is_changed();
1003 } 999 }
1004 1000
1005 1001
1002 /* TODO: move this code to MySQL save */
1006 void EditRecipe::fermentable_Json() 1003 void EditRecipe::fermentable_Json()
1007 { 1004 {
1008 QTableWidgetItem *item; 1005 QTableWidgetItem *item;
1009 QJsonArray array; 1006 QJsonArray array;
1010 1007
1011 qDebug() << "fermentable_Json()"; 1008 qDebug() << "fermentable_Json()";
1012 ui->fermentablesTable->sortItems(23, Qt::DescendingOrder); // Sort on amount. 1009
1013 qDebug() << "fermentable_Json() 1"; 1010 for (int i = 0; i < recipe->fermentables.size(); i++) {
1014 1011
1015 for (int i = 0; i < ui->fermentablesTable->rowCount(); i++) {
1016
1017 qDebug() << "fermentable_Json() 2" << i;
1018 QJsonObject obj; 1012 QJsonObject obj;
1019 item = ui->fermentablesTable->item(i, 12); 1013 obj.insert("f_acid_to_ph_57", recipe->fermentables.at(i).f_acid_to_ph_57);
1020 obj.insert("f_acid_to_ph_57", item->text().toDouble()); 1014 obj.insert("f_add_after_boil", recipe->fermentables.at(i).f_add_after_boil);
1021 item = ui->fermentablesTable->item(i, 13); 1015 obj.insert("f_added", recipe->fermentables.at(i).f_added);
1022 obj.insert("f_add_after_boil", item->text().toDouble()); 1016 obj.insert("f_adjust_to_total_100", recipe->fermentables.at(i).f_adjust_to_total_100);
1023 item = ui->fermentablesTable->item(i, 29); 1017 obj.insert("f_amount", recipe->fermentables.at(i).f_added);
1024 obj.insert("f_added", item->text().toDouble()); 1018 obj.insert("f_coarse_fine_diff", recipe->fermentables.at(i).f_coarse_fine_diff);
1025 item = ui->fermentablesTable->item(i, 9); 1019 obj.insert("f_color", recipe->fermentables.at(i).f_color);
1026 obj.insert("f_adjust_to_total_100", item->text().toDouble()); 1020 obj.insert("f_cost", recipe->fermentables.at(i).f_cost);
1027 item = ui->fermentablesTable->item(i, 23); 1021 obj.insert("f_di_ph", recipe->fermentables.at(i).f_di_ph);
1028 obj.insert("f_amount", item->text().toDouble()); 1022 obj.insert("f_diastatic_power", recipe->fermentables.at(i).f_diastatic_power);
1029 item = ui->fermentablesTable->item(i, 14); 1023 obj.insert("f_dissolved_protein", recipe->fermentables.at(i).f_dissolved_protein);
1030 obj.insert("f_coarse_fine_diff", item->text().toDouble()); 1024 obj.insert("f_graintype", recipe->fermentables.at(i).f_graintype);
1031 item = ui->fermentablesTable->item(i, 2); 1025 obj.insert("f_max_in_batch", recipe->fermentables.at(i).f_max_in_batch);
1032 obj.insert("f_color", item->text().toDouble()); 1026 obj.insert("f_moisture", recipe->fermentables.at(i).f_moisture);
1033 item = ui->fermentablesTable->item(i, 15); 1027 obj.insert("f_name", recipe->fermentables.at(i).f_name);
1034 obj.insert("f_cost", item->text().toDouble()); 1028 obj.insert("f_origin", recipe->fermentables.at(i).f_origin);
1035 item = ui->fermentablesTable->item(i, 16); 1029 obj.insert("f_percentage", recipe->fermentables.at(i).f_percentage);
1036 obj.insert("f_di_ph", item->text().toDouble()); 1030 obj.insert("f_protein", recipe->fermentables.at(i).f_protein);
1037 item = ui->fermentablesTable->item(i, 17); 1031 obj.insert("f_recommend_mash", recipe->fermentables.at(i).f_recommend_mash);
1038 obj.insert("f_diastatic_power", item->text().toDouble()); 1032 obj.insert("f_supplier", recipe->fermentables.at(i).f_supplier);
1039 item = ui->fermentablesTable->item(i, 18); 1033 obj.insert("f_type", recipe->fermentables.at(i).f_type);
1040 obj.insert("f_dissolved_protein", item->text().toDouble()); 1034 obj.insert("f_yield", recipe->fermentables.at(i).f_yield);
1041 item = ui->fermentablesTable->item(i, 28);
1042 obj.insert("f_graintype", item->text().toDouble());
1043 item = ui->fermentablesTable->item(i, 26);
1044 obj.insert("f_max_in_batch", item->text().toDouble());
1045 item = ui->fermentablesTable->item(i, 19);
1046 obj.insert("f_moisture", item->text().toDouble());
1047 item = ui->fermentablesTable->item(i, 1);
1048 obj.insert("f_name", item->text());
1049 item = ui->fermentablesTable->item(i, 20);
1050 obj.insert("f_origin", item->text());
1051 item = ui->fermentablesTable->item(i, 24);
1052 obj.insert("f_percentage", item->text().toDouble());
1053 item = ui->fermentablesTable->item(i, 21);
1054 obj.insert("f_protein", item->text().toDouble());
1055 item = ui->fermentablesTable->item(i, 22);
1056 obj.insert("f_recommend_mash", item->text().toDouble());
1057 item = ui->fermentablesTable->item(i, 0);
1058 obj.insert("f_supplier", item->text());
1059 item = ui->fermentablesTable->item(i, 27);
1060 obj.insert("f_type", item->text().toDouble());
1061 item = ui->fermentablesTable->item(i, 25);
1062 obj.insert("f_yield", item->text().toDouble());
1063 qDebug() << "fermentable_Json" << i << obj; 1035 qDebug() << "fermentable_Json" << i << obj;
1064 array.append(obj); /* Append this object */ 1036 array.append(obj); /* Append this object */
1065 } 1037 }
1066 1038
1067 qDebug() << array; 1039 qDebug() << array;
1068 /* Copy to the global array and refresh */ 1040 /* Copy to the global array and refresh */
1069 this->fermentables.setArray(array); 1041 // this->fermentables.setArray(array);
1070 is_changed();
1071 emit refreshAll();
1072 }
1073
1074
1075 void EditRecipe::cell_Fermentable_changed(int nRow, int nCol)
1076 {
1077 if (this->ignoreChanges)
1078 return;
1079
1080 qDebug() << "Cell at row " + QString::number(nRow) + " column " + QString::number(nCol) + " was changed.";
1081
1082 // TODO: some checks and auto fixes.
1083 // make_Json();
1084 } 1042 }
1085 1043
1086 1044
1087 void EditRecipe::on_deleteFermentRow_clicked() 1045 void EditRecipe::on_deleteFermentRow_clicked()
1088 { 1046 {
1089 QPushButton *pb = qobject_cast<QPushButton *>(QObject::sender()); 1047 QPushButton *pb = qobject_cast<QPushButton *>(QObject::sender());
1090 int row = pb->objectName().toInt(); 1048 int row = pb->objectName().toInt();
1091 qDebug() << "Delete fermentable row" << row; 1049 qDebug() << "Delete fermentable row" << row;
1092 1050
1093 QJsonObject obj = this->fermentables.array().at(row).toObject(); 1051 int rc = QMessageBox::warning(this, tr("Delete fermentable"), tr("Delete %1").arg(recipe->fermentables.at(row).f_name),
1094 qDebug() << obj;
1095
1096 int rc = QMessageBox::warning(this, tr("Delete fermentable"), tr("Delete %1").arg(obj["f_name"].toString()),
1097 QMessageBox::Yes | QMessageBox::No, QMessageBox::No); 1052 QMessageBox::Yes | QMessageBox::No, QMessageBox::No);
1098 if (rc == QMessageBox::No) 1053 if (rc == QMessageBox::No)
1099 return; 1054 return;
1100 1055
1101 ui->fermentablesTable->removeRow(row); 1056 ui->fermentablesTable->removeRow(row);
1102 // recalculate percentages 1057 // recalculate percentages
1103 fermentable_Json(); 1058 // fermentable_Json();
1104 } 1059 }
1105 1060
1106 1061
1107 void EditRecipe::ferment_amount_changed(double val) 1062 void EditRecipe::ferment_amount_changed(double val)
1108 { 1063 {
1109 QTableWidgetItem *item; 1064 QTableWidgetItem *item;
1110 double total = 0; 1065 double total = 0, perc;
1066
1067 if (recipe->fermentables_use100)
1068 return;
1111 1069
1112 qDebug() << "ferment_amount_changed()" << editrow << val; 1070 qDebug() << "ferment_amount_changed()" << editrow << val;
1113
1114 this->ignoreChanges = true; 1071 this->ignoreChanges = true;
1115 1072
1073 recipe->fermentables[editrow].f_amount = val;
1116 item = new QTableWidgetItem(QString("%1 Kg").arg(val, 4, 'f', 3, '0')); 1074 item = new QTableWidgetItem(QString("%1 Kg").arg(val, 4, 'f', 3, '0'));
1117 item->setTextAlignment(Qt::AlignRight|Qt::AlignVCenter); 1075 item->setTextAlignment(Qt::AlignRight|Qt::AlignVCenter);
1118 ui->fermentablesTable->setItem(editrow, 7, item); 1076 ui->fermentablesTable->setItem(editrow, 7, item);
1119 1077
1120 item = new QTableWidgetItem(QString("%1").arg(val, 4, 'f', 3, '0')); 1078 for (int i = 0; i < recipe->fermentables.size(); i++)
1121 ui->fermentablesTable->setItem(editrow, 23, item); 1079 total += recipe->fermentables.at(i).f_amount;
1122 1080
1123 for (int i = 0; i < ui->fermentablesTable->rowCount(); i++) { 1081 /*
1124 item = ui->fermentablesTable->item(i, 23); 1082 * Recalculate the percentages
1125 total += item->text().toDouble(); 1083 */
1126 } 1084 for (int i = 0; i < recipe->fermentables.size(); i++) {
1127 qDebug() << "total weight now" << total; 1085 perc = recipe->fermentables.at(i).f_amount / total * 100;
1128 1086 recipe->fermentables[i].f_percentage = perc;
1129 // for (int i = 0; i < ui->fermentablesTable->rowCount(); i++) { 1087 item = new QTableWidgetItem(QString("%1%").arg(perc, 2, 'f', 1, '0'));
1130 // item = ui->fermentablesTable->item(i, 23); 1088 item->setTextAlignment(Qt::AlignRight|Qt::AlignVCenter);
1131 // total += item->text().toDouble(); 1089 ui->fermentablesTable->setItem(i, 8, item);
1132 // } 1090 if (i == editrow)
1133 1091 this->pctEdit->setValue(perc);
1092 }
1134 this->ignoreChanges = false; 1093 this->ignoreChanges = false;
1135
1136 // fermentable_Json();
1137 } 1094 }
1138 1095
1139 void EditRecipe::ferment_pct_changed(double val) 1096 void EditRecipe::ferment_pct_changed(double val)
1140 { 1097 {
1098 if (! recipe->fermentables_use100)
1099 return;
1100
1141 qDebug() << "ferment_pct_changed()" << val; 1101 qDebug() << "ferment_pct_changed()" << val;
1142 } 1102 }
1143 1103
1144 1104
1145 void EditRecipe::on_editFermentRow_clicked() 1105 void EditRecipe::on_editFermentRow_clicked()
1146 { 1106 {
1147 QPushButton *pb = qobject_cast<QPushButton *>(QObject::sender()); 1107 QPushButton *pb = qobject_cast<QPushButton *>(QObject::sender());
1148 editrow = pb->objectName().toInt(); 1108 editrow = pb->objectName().toInt();
1149 qDebug() << "Edit fermentable row" << editrow; 1109 qDebug() << "Edit fermentable row" << editrow;
1150 work = this->fermentables.array().at(editrow).toObject(); 1110 Fermentables backup = recipe->fermentables.at(editrow);
1151 backup = this->fermentables.array().at(editrow).toObject();
1152
1153 qDebug() << work;
1154 1111
1155 QDialog* dialog = new QDialog(this); 1112 QDialog* dialog = new QDialog(this);
1156 dialog->resize(738, 287); 1113 dialog->resize(738, 287);
1157 QDialogButtonBox *buttonBox = new QDialogButtonBox(dialog); 1114 QDialogButtonBox *buttonBox = new QDialogButtonBox(dialog);
1158 buttonBox->setObjectName(QString::fromUtf8("buttonBox")); 1115 buttonBox->setObjectName(QString::fromUtf8("buttonBox"));
1210 selectEdit = new QComboBox(dialog); 1167 selectEdit = new QComboBox(dialog);
1211 selectEdit->setObjectName(QString::fromUtf8("selectEdit")); 1168 selectEdit->setObjectName(QString::fromUtf8("selectEdit"));
1212 selectEdit->setGeometry(QRect(160, 70, 251, 23)); 1169 selectEdit->setGeometry(QRect(160, 70, 251, 23));
1213 nameEdit = new QLineEdit(dialog); 1170 nameEdit = new QLineEdit(dialog);
1214 nameEdit->setObjectName(QString::fromUtf8("nameEdit")); 1171 nameEdit->setObjectName(QString::fromUtf8("nameEdit"));
1215 nameEdit->setText(work["f_name"].toString()); 1172 nameEdit->setText(recipe->fermentables.at(editrow).f_name);
1216 nameEdit->setGeometry(QRect(160, 10, 511, 23)); 1173 nameEdit->setGeometry(QRect(160, 10, 511, 23));
1217 nameEdit->setReadOnly(true); 1174 nameEdit->setReadOnly(true);
1218 supplierEdit = new QLineEdit(dialog); 1175 supplierEdit = new QLineEdit(dialog);
1219 supplierEdit->setObjectName(QString::fromUtf8("supplierEdit")); 1176 supplierEdit->setObjectName(QString::fromUtf8("supplierEdit"));
1220 supplierEdit->setText(work["f_supplier"].toString()); 1177 supplierEdit->setText(recipe->fermentables.at(editrow).f_supplier);
1221 supplierEdit->setGeometry(QRect(160, 40, 511, 23)); 1178 supplierEdit->setGeometry(QRect(160, 40, 511, 23));
1222 supplierEdit->setReadOnly(true); 1179 supplierEdit->setReadOnly(true);
1223 amountEdit = new QDoubleSpinBox(dialog); 1180 amountEdit = new QDoubleSpinBox(dialog);
1224 amountEdit->setObjectName(QString::fromUtf8("amountEdit")); 1181 amountEdit->setObjectName(QString::fromUtf8("amountEdit"));
1225 amountEdit->setGeometry(QRect(160, 100, 121, 24)); 1182 amountEdit->setGeometry(QRect(160, 100, 121, 24));
1226 amountEdit->setAlignment(Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter); 1183 amountEdit->setAlignment(Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter);
1227 amountEdit->setAccelerated(true); 1184 amountEdit->setAccelerated(true);
1228 amountEdit->setDecimals(3); 1185 amountEdit->setDecimals(3);
1229 amountEdit->setReadOnly(use_to100); 1186 amountEdit->setReadOnly(recipe->fermentables_use100);
1230 amountEdit->setMaximum(100000.0); 1187 amountEdit->setMaximum(100000.0);
1231 amountEdit->setSingleStep(0.0010); 1188 amountEdit->setSingleStep(0.0010);
1232 amountEdit->setValue(work["f_amount"].toDouble()); 1189 amountEdit->setValue(recipe->fermentables.at(editrow).f_amount);
1233 connect(amountEdit, QOverload<double>::of(&QDoubleSpinBox::valueChanged), this, &EditRecipe::ferment_amount_changed); 1190 connect(amountEdit, QOverload<double>::of(&QDoubleSpinBox::valueChanged), this, &EditRecipe::ferment_amount_changed);
1234 1191
1235 pctEdit = new QDoubleSpinBox(dialog); 1192 pctEdit = new QDoubleSpinBox(dialog);
1236 pctEdit->setObjectName(QString::fromUtf8("pctEdit")); 1193 pctEdit->setObjectName(QString::fromUtf8("pctEdit"));
1237 pctEdit->setGeometry(QRect(160, 130, 121, 24)); 1194 pctEdit->setGeometry(QRect(160, 130, 121, 24));
1238 pctEdit->setAlignment(Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter); 1195 pctEdit->setAlignment(Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter);
1239 pctEdit->setAccelerated(true); 1196 pctEdit->setAccelerated(true);
1240 pctEdit->setDecimals(1); 1197 pctEdit->setDecimals(1);
1241 if (use_to100) { 1198 if (recipe->fermentables_use100) {
1242 if (work["f_adjust_to_total_100"].toInt()) 1199 if (recipe->fermentables.at(editrow).f_adjust_to_total_100)
1243 pctEdit->setReadOnly(true); 1200 pctEdit->setReadOnly(true);
1244 else 1201 else
1245 pctEdit->setReadOnly(false); 1202 pctEdit->setReadOnly(false);
1246 } else { 1203 } else {
1247 pctEdit->setReadOnly(true); 1204 pctEdit->setReadOnly(true);
1248 } 1205 }
1249 pctEdit->setMaximum(100.0); 1206 pctEdit->setMaximum(100.0);
1250 pctEdit->setSingleStep(0.1); 1207 pctEdit->setSingleStep(0.1);
1251 pctEdit->setValue(work["f_percentage"].toDouble()); 1208 pctEdit->setValue(recipe->fermentables.at(editrow).f_percentage);
1252 connect(pctEdit, QOverload<double>::of(&QDoubleSpinBox::valueChanged), this, &EditRecipe::ferment_pct_changed); 1209 connect(pctEdit, QOverload<double>::of(&QDoubleSpinBox::valueChanged), this, &EditRecipe::ferment_pct_changed);
1253 1210
1254 addedEdit = new QComboBox(dialog); 1211 addedEdit = new QComboBox(dialog);
1255 addedEdit->setObjectName(QString::fromUtf8("addedEdit")); 1212 addedEdit->setObjectName(QString::fromUtf8("addedEdit"));
1256 addedEdit->setGeometry(QRect(160, 190, 161, 23)); 1213 addedEdit->setGeometry(QRect(160, 190, 161, 23));
1258 addedEdit->addItem(tr("Boil")); 1215 addedEdit->addItem(tr("Boil"));
1259 addedEdit->addItem(tr("Fermentation")); 1216 addedEdit->addItem(tr("Fermentation"));
1260 addedEdit->addItem(tr("Lagering")); 1217 addedEdit->addItem(tr("Lagering"));
1261 addedEdit->addItem(tr("Bottle")); 1218 addedEdit->addItem(tr("Bottle"));
1262 addedEdit->addItem(tr("Kegs")); 1219 addedEdit->addItem(tr("Kegs"));
1263 addedEdit->setCurrentIndex(work["f_added"].toInt()); 1220 addedEdit->setCurrentIndex(recipe->fermentables.at(editrow).f_added);
1264 1221
1265 to100Edit = new QCheckBox(dialog); 1222 to100Edit = new QCheckBox(dialog);
1266 to100Edit->setObjectName(QString::fromUtf8("to100Edit")); 1223 to100Edit->setObjectName(QString::fromUtf8("to100Edit"));
1267 to100Edit->setGeometry(QRect(160, 160, 85, 21)); 1224 to100Edit->setGeometry(QRect(160, 160, 85, 21));
1268 to100Edit->setChecked(work["f_adjust_to_total_100"].toInt() ? true:false); 1225 to100Edit->setChecked(recipe->fermentables.at(editrow).f_adjust_to_total_100);
1269 1226
1270 instockEdit = new QCheckBox(dialog); 1227 instockEdit = new QCheckBox(dialog);
1271 instockEdit->setObjectName(QString::fromUtf8("instockEdit")); 1228 instockEdit->setObjectName(QString::fromUtf8("instockEdit"));
1272 instockEdit->setGeometry(QRect(550, 70, 85, 21)); 1229 instockEdit->setGeometry(QRect(550, 70, 85, 21));
1273 1230
1276 maxEdit->setGeometry(QRect(550, 130, 121, 24)); 1233 maxEdit->setGeometry(QRect(550, 130, 121, 24));
1277 maxEdit->setAlignment(Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter); 1234 maxEdit->setAlignment(Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter);
1278 maxEdit->setReadOnly(true); 1235 maxEdit->setReadOnly(true);
1279 maxEdit->setButtonSymbols(QAbstractSpinBox::NoButtons); 1236 maxEdit->setButtonSymbols(QAbstractSpinBox::NoButtons);
1280 maxEdit->setDecimals(1); 1237 maxEdit->setDecimals(1);
1281 maxEdit->setValue(work["f_max_in_batch"].toDouble()); 1238 maxEdit->setValue(recipe->fermentables.at(editrow).f_max_in_batch);
1282 1239
1283 connect(buttonBox, SIGNAL(rejected()), dialog, SLOT(reject())); 1240 connect(buttonBox, SIGNAL(rejected()), dialog, SLOT(reject()));
1284 connect(buttonBox, SIGNAL(accepted()), dialog, SLOT(accept())); 1241 connect(buttonBox, SIGNAL(accepted()), dialog, SLOT(accept()));
1285 dialog->exec(); 1242 dialog->exec();
1286 if (dialog->result() == QDialog::Rejected) { 1243 if (dialog->result() == QDialog::Rejected) {
1287 qDebug() << "rejected"; 1244 qDebug() << "rejected";
1288 // restore fermentbackup 1245 // restore fermentbackup
1246 // recalculate percentages
1289 } else { 1247 } else {
1290 qDebug() << "accepted"; 1248 qDebug() << "accepted";
1291 // fermentrow to final 1249 // fermentrow to final
1292 1250
1293 //fermentable_Json(); segfault ! 1251 //fermentable_Json(); segfault !
1294 } 1252 }
1295 1253
1296 // disconnect 1254 // disconnect
1297 // return 1255 // return
1256 emit refreshAll();
1298 } 1257 }
1299 1258
1300 1259
1301 void EditRecipe::on_quitButton_clicked() 1260 void EditRecipe::on_quitButton_clicked()
1302 { 1261 {

mercurial