src/EditRecipeTab2.cpp

changeset 151
b5b2483f3a3f
parent 150
fd568cc1dd0e
child 152
58e4ce7dd217
equal deleted inserted replaced
150:fd568cc1dd0e 151:b5b2483f3a3f
19 19
20 20
21 21
22 bool EditRecipe::ferment_sort_test(const Fermentables &D1, const Fermentables &D2) 22 bool EditRecipe::ferment_sort_test(const Fermentables &D1, const Fermentables &D2)
23 { 23 {
24 return (D1.f_added <= D2.f_added) && (D1.f_amount >= D2.f_amount) && (D1.f_color < D2.f_color); 24 if (D1.f_added > D2.f_added)
25 return false;
26 if (D1.f_added < D2.f_added)
27 return true;
28 return (D1.f_amount >= D2.f_amount) && (D1.f_color < D2.f_color);
25 } 29 }
26 30
27 31
28 void EditRecipe::to100Fermentables(int row) 32 void EditRecipe::to100Fermentables(int row)
29 { 33 {
50 QHBoxLayout* pLayout; 54 QHBoxLayout* pLayout;
51 QTableWidgetItem *item; 55 QTableWidgetItem *item;
52 56
53 qDebug() << "refreshFermentables" << recipe->fermentables.size(); 57 qDebug() << "refreshFermentables" << recipe->fermentables.size();
54 std::sort(recipe->fermentables.begin(), recipe->fermentables.end(), ferment_sort_test); 58 std::sort(recipe->fermentables.begin(), recipe->fermentables.end(), ferment_sort_test);
55
56 /*
57 * During filling the table turn off the cellChanged signal because every cell that is filled
58 * triggers the cellChanged signal. The QTableWidget has no better signal to use.
59 */
60 this->ignoreChanges = true;
61 59
62 const QStringList labels({tr("Supplier"), tr("Fermentable"), tr("EBC"), tr("Type"), tr("Graintype"), tr("When"), tr("Yield"), 60 const QStringList labels({tr("Supplier"), tr("Fermentable"), tr("EBC"), tr("Type"), tr("Graintype"), tr("When"), tr("Yield"),
63 tr("Amount"), tr("Procent"), tr("100%"), tr("Delete"), tr("Edit") }); 61 tr("Amount"), tr("Procent"), tr("100%"), tr("Delete"), tr("Edit") });
64 ui->fermentablesTable->setColumnCount(12); 62 ui->fermentablesTable->setColumnCount(12);
65 ui->fermentablesTable->setColumnWidth(0, 150); /* Supplier */ 63 ui->fermentablesTable->setColumnWidth(0, 150); /* Supplier */
139 pLayout->addWidget(btn_edit); 137 pLayout->addWidget(btn_edit);
140 pLayout->setContentsMargins(5, 0, 5, 0); 138 pLayout->setContentsMargins(5, 0, 5, 0);
141 pWidget->setLayout(pLayout); 139 pWidget->setLayout(pLayout);
142 ui->fermentablesTable->setCellWidget(i, 11, pWidget); 140 ui->fermentablesTable->setCellWidget(i, 11, pWidget);
143 } 141 }
144 this->ignoreChanges = false;
145 } 142 }
146 143
147 144
148 void EditRecipe::calcFermentables() 145 void EditRecipe::calcFermentables()
149 { 146 {
183 mvol = mashinfuse; 180 mvol = mashinfuse;
184 qDebug() << " mash time" << mashtime << "temp" << mashtemp << "infuse" << mashinfuse; 181 qDebug() << " mash time" << mashtime << "temp" << mashtemp << "infuse" << mashinfuse;
185 } else { 182 } else {
186 qDebug() << " no mash schedule"; 183 qDebug() << " no mash schedule";
187 } 184 }
185
186 const QSignalBlocker blocker1(ui->est_ogEdit);
187 const QSignalBlocker blocker2(ui->est_og2Edit);
188 188
189 if (recipe->fermentables.size() < 1) { 189 if (recipe->fermentables.size() < 1) {
190 qDebug() << " no fermentables, return."; 190 qDebug() << " no fermentables, return.";
191 recipe->est_og = 0.980; 191 recipe->est_og = 0.980;
192 ui->est_ogEdit->setValue(0.980); 192 ui->est_ogEdit->setValue(0.980);
344 * Take the alcohol and sugar parts and then combine. 344 * Take the alcohol and sugar parts and then combine.
345 */ 345 */
346 double alc = 1881.22 * fg * (og - fg) / (1.775 - og); 346 double alc = 1881.22 * fg * (og - fg) / (1.775 - og);
347 double sug = 3550 * fg * (0.1808 * og + 0.8192 * fg - 1.0004); 347 double sug = 3550 * fg * (0.1808 * og + 0.8192 * fg - 1.0004);
348 ui->calEdit->setValue(round((alc + sug) / (12 * 0.0295735296))); 348 ui->calEdit->setValue(round((alc + sug) / (12 * 0.0295735296)));
349
350 // Bottle priming
351 double priming_total = 0;
352 for (i = 0; i < recipe->fermentables.size(); i++) {
353 if (recipe->fermentables.at(i).f_added == 4) {
354 priming_total += ((recipe->fermentables.at(i).f_yield / 100) * (1 - recipe->fermentables.at(i).f_moisture / 100)) *
355 recipe->fermentables.at(i).f_amount;
356 qDebug() << " priming" << recipe->fermentables.at(i).f_amount << "total" << priming_total;
357 }
358 }
359 double grl = priming_total * 1000.0 * (1 / recipe->batch_size);
360 double volco2 = grl * 0.510;
361 qDebug() << " priming gr/l" << grl << "volco2" << volco2;
362
363 if (volco2 > 0) {
364 recipe->est_carb = volco2;
365 ui->est_carbEdit->setValue(recipe->est_carb);
366 ui->est_carbShow->setValue(recipe->est_carb);
367 }
349 } 368 }
350 369
351 370
352 void EditRecipe::calcFermentablesFromOG(double og) 371 void EditRecipe::calcFermentablesFromOG(double og)
353 { 372 {
473 int rc = QMessageBox::warning(this, tr("Delete fermentable"), tr("Delete %1").arg(recipe->fermentables.at(row).f_name), 492 int rc = QMessageBox::warning(this, tr("Delete fermentable"), tr("Delete %1").arg(recipe->fermentables.at(row).f_name),
474 QMessageBox::Yes | QMessageBox::No, QMessageBox::No); 493 QMessageBox::Yes | QMessageBox::No, QMessageBox::No);
475 if (rc == QMessageBox::No) 494 if (rc == QMessageBox::No)
476 return; 495 return;
477 496
478 this->ignoreChanges = true; 497 // this->ignoreChanges = true;
479 recipe->fermentables.removeAt(row); 498 recipe->fermentables.removeAt(row);
480 499
481 /* 500 /*
482 * Recalculate the percentages on the rows left. 501 * Recalculate the percentages on the rows left.
483 */ 502 */
487 total += recipe->fermentables.at(i).f_amount; 506 total += recipe->fermentables.at(i).f_amount;
488 for (int i = 0; i < recipe->fermentables.size(); i++) 507 for (int i = 0; i < recipe->fermentables.size(); i++)
489 if (recipe->fermentables.at(i).f_added < 4) 508 if (recipe->fermentables.at(i).f_added < 4)
490 recipe->fermentables[i].f_percentage = recipe->fermentables.at(i).f_amount / total * 100; 509 recipe->fermentables[i].f_percentage = recipe->fermentables.at(i).f_amount / total * 100;
491 510
492 this->ignoreChanges = false; 511 // this->ignoreChanges = false;
493 is_changed(); 512 is_changed();
494 emit refreshAll(); 513 emit refreshAll();
495 } 514 }
496 515
497 516
498 void EditRecipe::ferment_amount_changed(double val) 517 void EditRecipe::ferment_amount_changed(double val)
499 { 518 {
500 QTableWidgetItem *item; 519 QTableWidgetItem *item;
501 double total = 0, perc; 520 double total = 0, perc;
502 521
503 if (recipe->fermentables_use100) 522 if (recipe->fermentables_use100 && recipe->fermentables.at(recipe->fermentables_row).f_added < 4)
504 return; 523 return;
505 524
506 qDebug() << "ferment_amount_changed()" << recipe->fermentables_row << val; 525 qDebug() << "ferment_amount_changed()" << recipe->fermentables_row << val;
507 this->ignoreChanges = true; 526 // this->ignoreChanges = true;
508 527
509 recipe->fermentables[recipe->fermentables_row].f_amount = val; 528 recipe->fermentables[recipe->fermentables_row].f_amount = val;
510 item = new QTableWidgetItem(QString("%1 Kg").arg(val, 4, 'f', 3, '0')); 529 item = new QTableWidgetItem(QString("%1 Kg").arg(val, 4, 'f', 3, '0'));
511 item->setTextAlignment(Qt::AlignRight|Qt::AlignVCenter); 530 item->setTextAlignment(Qt::AlignRight|Qt::AlignVCenter);
512 ui->fermentablesTable->setItem(recipe->fermentables_row, 7, item); 531 ui->fermentablesTable->setItem(recipe->fermentables_row, 7, item);
526 ui->fermentablesTable->setItem(i, 8, item); 545 ui->fermentablesTable->setItem(i, 8, item);
527 if (i == recipe->fermentables_row) 546 if (i == recipe->fermentables_row)
528 this->pctEdit->setValue(perc); 547 this->pctEdit->setValue(perc);
529 } 548 }
530 } 549 }
531 this->ignoreChanges = false; 550 // this->ignoreChanges = false;
532 is_changed(); 551 is_changed();
533 } 552 }
534 553
535 554
536 void EditRecipe::ferment_pct_changed(double val) 555 void EditRecipe::ferment_pct_changed(double val)
555 double oldperc = recipe->fermentables.at(recipe->fermentables_row).f_percentage; 574 double oldperc = recipe->fermentables.at(recipe->fermentables_row).f_percentage;
556 double diffp = val - oldperc; 575 double diffp = val - oldperc;
557 double diffw = (diffp / 100) * total; 576 double diffw = (diffp / 100) * total;
558 qDebug() << "row100" << row100 << "total" << total << "diff kg" << diffw << "diff %" << diffp; 577 qDebug() << "row100" << row100 << "total" << total << "diff kg" << diffw << "diff %" << diffp;
559 578
560 this->ignoreChanges = true; 579 // this->ignoreChanges = true;
561 recipe->fermentables[recipe->fermentables_row].f_percentage += diffp; 580 recipe->fermentables[recipe->fermentables_row].f_percentage += diffp;
562 recipe->fermentables[recipe->fermentables_row].f_amount += diffw; 581 recipe->fermentables[recipe->fermentables_row].f_amount += diffw;
563 recipe->fermentables[row100].f_percentage -= diffp; 582 recipe->fermentables[row100].f_percentage -= diffp;
564 recipe->fermentables[row100].f_amount -= diffw; 583 recipe->fermentables[row100].f_amount -= diffw;
565 584
578 597
579 item = new QTableWidgetItem(QString("%1%").arg(recipe->fermentables[row100].f_percentage, 2, 'f', 1, '0')); 598 item = new QTableWidgetItem(QString("%1%").arg(recipe->fermentables[row100].f_percentage, 2, 'f', 1, '0'));
580 item->setTextAlignment(Qt::AlignRight|Qt::AlignVCenter); 599 item->setTextAlignment(Qt::AlignRight|Qt::AlignVCenter);
581 ui->fermentablesTable->setItem(row100, 8, item); 600 ui->fermentablesTable->setItem(row100, 8, item);
582 601
583 this->ignoreChanges = false; 602 // this->ignoreChanges = false;
584 is_changed(); 603 is_changed();
585 } 604 }
586 605
587 606
588 void EditRecipe::ferment_to100_changed(bool val) 607 void EditRecipe::ferment_to100_changed(bool val)
589 { 608 {
590 qDebug() << "ferment_to100_changed()" << recipe->fermentables_row << val << recipe->fermentables_use100; 609 qDebug() << "ferment_to100_changed()" << recipe->fermentables_row << val << recipe->fermentables_use100;
610
611 if (recipe->fermentables.at(recipe->fermentables_row).f_added >= 4) {
612 const QSignalBlocker blocker1(to100Edit);
613 recipe->fermentables[recipe->fermentables_row].f_adjust_to_total_100 = false;
614 to100Edit->setChecked(false);
615 return;
616 }
591 617
592 /* 618 /*
593 * Three scenario's. 619 * Three scenario's.
594 * 1. There is no fermentable selected yet, just mark it. 620 * 1. There is no fermentable selected yet, just mark it.
595 * 2. There is current one is selected, deselect it. 621 * 2. There is current one is selected, deselect it.
616 } else { 642 } else {
617 qDebug() << "bug"; 643 qDebug() << "bug";
618 return; 644 return;
619 } 645 }
620 646
621 this->ignoreChanges = true; 647 // this->ignoreChanges = true;
622 for (int i = 0; i < recipe->fermentables.size(); i++) { 648 for (int i = 0; i < recipe->fermentables.size(); i++) {
623 to100Fermentables(i); 649 to100Fermentables(i);
624 } 650 }
625 this->ignoreChanges = false; 651 // this->ignoreChanges = false;
626 is_changed(); 652 is_changed();
627 } 653 }
654
628 655
629 void EditRecipe::ferment_select_changed(int val) 656 void EditRecipe::ferment_select_changed(int val)
630 { 657 {
631 QSqlQuery query; 658 QSqlQuery query;
632 bool instock = finstockEdit->isChecked(); 659 bool instock = finstockEdit->isChecked();
655 qDebug() << "found" << query.value(2).toString() << query.value(0).toString(); 682 qDebug() << "found" << query.value(2).toString() << query.value(0).toString();
656 683
657 /* 684 /*
658 * Replace the fermentable record contents 685 * Replace the fermentable record contents
659 */ 686 */
660 this->ignoreChanges = true;
661 recipe->fermentables[recipe->fermentables_row].f_name = query.value(0).toString(); 687 recipe->fermentables[recipe->fermentables_row].f_name = query.value(0).toString();
662 recipe->fermentables[recipe->fermentables_row].f_origin = query.value(1).toString(); 688 recipe->fermentables[recipe->fermentables_row].f_origin = query.value(1).toString();
663 recipe->fermentables[recipe->fermentables_row].f_supplier = query.value(2).toString(); 689 recipe->fermentables[recipe->fermentables_row].f_supplier = query.value(2).toString();
664 recipe->fermentables[recipe->fermentables_row].f_cost = query.value(3).toDouble(); 690 recipe->fermentables[recipe->fermentables_row].f_cost = query.value(3).toDouble();
665 recipe->fermentables[recipe->fermentables_row].f_type = query.value(4).toInt(); 691 recipe->fermentables[recipe->fermentables_row].f_type = query.value(4).toInt();
702 728
703 item = new QTableWidgetItem(QString("%1%").arg(recipe->fermentables.at(recipe->fermentables_row).f_yield, 2, 'f', 1, '0')); 729 item = new QTableWidgetItem(QString("%1%").arg(recipe->fermentables.at(recipe->fermentables_row).f_yield, 2, 'f', 1, '0'));
704 item->setTextAlignment(Qt::AlignRight|Qt::AlignVCenter); 730 item->setTextAlignment(Qt::AlignRight|Qt::AlignVCenter);
705 ui->fermentablesTable->setItem(recipe->fermentables_row, 6, item); 731 ui->fermentablesTable->setItem(recipe->fermentables_row, 6, item);
706 732
707 this->ignoreChanges = false;
708 calcFermentables(); 733 calcFermentables();
709 is_changed(); 734 is_changed();
710 } 735 }
711 736
712 737
736 761
737 void EditRecipe::ferment_added_changed(int val) 762 void EditRecipe::ferment_added_changed(int val)
738 { 763 {
739 qDebug() << "ferment_added_changed()" << recipe->fermentables_row << val; 764 qDebug() << "ferment_added_changed()" << recipe->fermentables_row << val;
740 765
741 this->ignoreChanges = true; 766 // this->ignoreChanges = true;
742 recipe->fermentables[recipe->fermentables_row].f_added = val; 767 recipe->fermentables[recipe->fermentables_row].f_added = val;
743 QTableWidgetItem *item = new QTableWidgetItem(f_added[val]); 768 QTableWidgetItem *item = new QTableWidgetItem(f_added[val]);
744 item->setTextAlignment(Qt::AlignCenter|Qt::AlignVCenter); 769 item->setTextAlignment(Qt::AlignCenter|Qt::AlignVCenter);
745 ui->fermentablesTable->setItem(recipe->fermentables_row, 5, item); 770 ui->fermentablesTable->setItem(recipe->fermentables_row, 5, item);
771
772 famountEdit->setReadOnly(recipe->fermentables_use100 && recipe->fermentables.at(recipe->fermentables_row).f_added < 4);
773 pctEdit->setReadOnly(! (recipe->fermentables_use100 && recipe->fermentables.at(recipe->fermentables_row).f_added < 4));
746 774
747 double total = 0; 775 double total = 0;
748 for (int i = 0; i < recipe->fermentables.size(); i++) 776 for (int i = 0; i < recipe->fermentables.size(); i++)
749 if (recipe->fermentables.at(i).f_added < 4) // Only before bottle/kegging 777 if (recipe->fermentables.at(i).f_added < 4) // Only before bottle/kegging
750 total += recipe->fermentables.at(i).f_amount; 778 total += recipe->fermentables.at(i).f_amount;
751 for (int i = 0; i < recipe->fermentables.size(); i++) 779 for (int i = 0; i < recipe->fermentables.size(); i++)
752 if (recipe->fermentables.at(i).f_added < 4) 780 if (recipe->fermentables.at(i).f_added < 4)
753 recipe->fermentables[i].f_percentage = recipe->fermentables.at(i).f_amount / total * 100; 781 recipe->fermentables[i].f_percentage = recipe->fermentables.at(i).f_amount / total * 100;
754 782
755 this->ignoreChanges = false; 783 // this->ignoreChanges = false;
756 is_changed(); 784 is_changed();
757 emit refreshAll(); 785 emit refreshAll();
758 } 786 }
759 787
760 788
840 famountEdit->setObjectName(QString::fromUtf8("famountEdit")); 868 famountEdit->setObjectName(QString::fromUtf8("famountEdit"));
841 famountEdit->setGeometry(QRect(160, 100, 121, 24)); 869 famountEdit->setGeometry(QRect(160, 100, 121, 24));
842 famountEdit->setAlignment(Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter); 870 famountEdit->setAlignment(Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter);
843 famountEdit->setAccelerated(true); 871 famountEdit->setAccelerated(true);
844 famountEdit->setDecimals(3); 872 famountEdit->setDecimals(3);
845 famountEdit->setReadOnly(recipe->fermentables_use100); 873 famountEdit->setReadOnly(recipe->fermentables_use100 && recipe->fermentables.at(recipe->fermentables_row).f_added < 4);
846 famountEdit->setMaximum(100000.0); 874 famountEdit->setMaximum(100000.0);
847 famountEdit->setSingleStep(0.0010); 875 famountEdit->setSingleStep(0.0010);
848 famountEdit->setValue(recipe->fermentables.at(recipe->fermentables_row).f_amount); 876 famountEdit->setValue(recipe->fermentables.at(recipe->fermentables_row).f_amount);
849 877
850 pctEdit = new QDoubleSpinBox(dialog); 878 pctEdit = new QDoubleSpinBox(dialog);
851 pctEdit->setObjectName(QString::fromUtf8("pctEdit")); 879 pctEdit->setObjectName(QString::fromUtf8("pctEdit"));
852 pctEdit->setGeometry(QRect(160, 130, 121, 24)); 880 pctEdit->setGeometry(QRect(160, 130, 121, 24));
853 pctEdit->setAlignment(Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter); 881 pctEdit->setAlignment(Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter);
854 pctEdit->setAccelerated(true); 882 pctEdit->setAccelerated(true);
855 pctEdit->setDecimals(1); 883 pctEdit->setDecimals(1);
856 if (recipe->fermentables_use100) { 884 if (recipe->fermentables_use100 && recipe->fermentables.at(recipe->fermentables_row).f_added < 4) {
857 if (recipe->fermentables.at(recipe->fermentables_row).f_adjust_to_total_100) 885 if (recipe->fermentables.at(recipe->fermentables_row).f_adjust_to_total_100)
858 pctEdit->setReadOnly(true); 886 pctEdit->setReadOnly(true);
859 else 887 else
860 pctEdit->setReadOnly(false); 888 pctEdit->setReadOnly(false);
861 } else { 889 } else {

mercurial