src/EditRecipeTab4.cpp

Tue, 12 Apr 2022 21:03:19 +0200

author
Michiel Broek <mbroek@mbse.eu>
date
Tue, 12 Apr 2022 21:03:19 +0200
changeset 131
0115b97e8c39
parent 128
0f4eee875ea6
child 132
9ede9c75cb54
permissions
-rw-r--r--

Added global variables, C++ lovers will hate that. Added global acid data. Fixed several load and save errors in the json arrays in the recipe record. Added first part of the miscs table. The first part of the water tab has values.

/**
 * EditRecipe.cpp is part of bmsapp.
 *
 * tab 4, miscs.
 *
 * bmsapp is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * bmsapp is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */


bool EditRecipe::misc_sort_test(const Miscs &D1, const Miscs &D2)
{
    if (D1.m_use_use > D2.m_use_use)
	return false;
    if (D1.m_use_use < D2.m_use_use)
	return true;
    if (D1.m_type > D2.m_type)
	return false;
    if (D1.m_type < D2.m_type)
	return true;
    return (D1.m_amount > D2.m_amount);
}


void EditRecipe::refreshMiscs()
{
    QString w;
    QWidget* pWidget;
    QHBoxLayout* pLayout;
    QTableWidgetItem *item;

    qDebug() << "refreshMiscs" << recipe->miscs.size();
    std::sort(recipe->miscs.begin(), recipe->miscs.end(), misc_sort_test);

    /*
     * During filling the table turn off the cellChanged signal because every cell that is filled
     * triggers the cellChanged signal. The QTableWidget has no better signal to use.
     */
    this->ignoreChanges = true;

    const QStringList labels({tr("Ingredient"), tr("Type"), tr("Use at"), tr("Time"), tr("Amount"), tr("Delete"), tr("Edit") });

    ui->miscsTable->setColumnCount(7);
    ui->miscsTable->setColumnWidth(0, 300);	/* Ingredient	*/
    ui->miscsTable->setColumnWidth(1, 100);	/* Type		*/
    ui->miscsTable->setColumnWidth(2, 100);	/* Added	*/
    ui->miscsTable->setColumnWidth(3,  75);	/* Time		*/
    ui->miscsTable->setColumnWidth(4,  90);	/* Amount	*/
    ui->miscsTable->setColumnWidth(5,  80);	/* Delete	*/
    ui->miscsTable->setColumnWidth(6,  80);	/* Edit		*/
    ui->miscsTable->setHorizontalHeaderLabels(labels);
    ui->miscsTable->verticalHeader()->hide();
    ui->miscsTable->setRowCount(recipe->miscs.size());

    ui->mw_acidPick->setCurrentIndex(-1);

    for (int i = 0; i < recipe->miscs.size(); i++) {

	ui->miscsTable->setItem(i, 0, new QTableWidgetItem(recipe->miscs.at(i).m_name));

	item = new QTableWidgetItem(m_types[recipe->miscs.at(i).m_type]);
        item->setTextAlignment(Qt::AlignCenter|Qt::AlignVCenter);
        ui->miscsTable->setItem(i, 1, item);

	item = new QTableWidgetItem(m_uses[recipe->miscs.at(i).m_use_use]);
        item->setTextAlignment(Qt::AlignCenter|Qt::AlignVCenter);
        ui->miscsTable->setItem(i, 2, item);

	if (recipe->miscs.at(i).m_use_use == 2) {	// Boil
	    item = new QTableWidgetItem(QString("%1 min.").arg(recipe->miscs.at(i).m_time, 1, 'f', 0, '0'));
	} else if (recipe->miscs.at(i).m_use_use == 3 || recipe->miscs.at(i).m_use_use == 4) {	// Primary or secondary
	    item = new QTableWidgetItem(QString("%1 days.").arg(recipe->miscs.at(i).m_time / 1440, 1, 'f', 0, '0'));
	} else {
	    item = new QTableWidgetItem(QString(""));
	}
	item->setTextAlignment(Qt::AlignRight|Qt::AlignVCenter);
        ui->miscsTable->setItem(i, 3, item);

	if (recipe->miscs.at(i).m_amount_is_weight)
	    item = new QTableWidgetItem(QString("%1 gr").arg(recipe->miscs.at(i).m_amount * 1000.0, 2, 'f', 1, '0'));
	else
	    item = new QTableWidgetItem(QString("%1 ml").arg(recipe->miscs.at(i).m_amount * 1000.0, 2, 'f', 1, '0'));
	item->setTextAlignment(Qt::AlignRight|Qt::AlignVCenter);
        ui->miscsTable->setItem(i, 4, item);

	/*
	 * Add the Delete and Edit row buttons.
	 * Not for water agents, these are set on the water tab.
	 */
	if (recipe->miscs.at(i).m_type == 4) {
	    ui->miscsTable->removeCellWidget(i, 5);
	    ui->miscsTable->removeCellWidget(i, 6);
	} else {
            pWidget = new QWidget();
            QPushButton* btn_dele = new QPushButton();
            btn_dele->setObjectName(QString("%1").arg(i));  /* Send row with the button */
            btn_dele->setText(tr("Delete"));
            connect(btn_dele, SIGNAL(clicked()), this, SLOT(on_deleteFermentRow_clicked()));
            pLayout = new QHBoxLayout(pWidget);
            pLayout->addWidget(btn_dele);
            pLayout->setContentsMargins(5, 0, 5, 0);
            pWidget->setLayout(pLayout);
            ui->miscsTable->setCellWidget(i, 5, pWidget);

            pWidget = new QWidget();
            QPushButton* btn_edit = new QPushButton();
            btn_edit->setObjectName(QString("%1").arg(i));  /* Send row with the button */
            btn_edit->setText(tr("Edit"));
            connect(btn_edit, SIGNAL(clicked()), this, SLOT(on_editFermentRow_clicked()));
            pLayout = new QHBoxLayout(pWidget);
            pLayout->addWidget(btn_edit);
            pLayout->setContentsMargins(5, 0, 5, 0);
            pWidget->setLayout(pLayout);
            ui->miscsTable->setCellWidget(i, 6, pWidget);
	}

	/*
	 * Update the water agents.
	 */
	if (recipe->miscs.at(i).m_type == 4) {
	    if (recipe->miscs.at(i).m_name == "CaCl2") {
		ui->bs_cacl2Edit->setValue(recipe->miscs.at(i).m_amount * 1000.0);
	    } else if (recipe->miscs.at(i).m_name == "CaSO4") {
		ui->bs_caso4Edit->setValue(recipe->miscs.at(i).m_amount * 1000.0);
	    } else if (recipe->miscs.at(i).m_name == "MgSO4") {
		ui->bs_mgso4Edit->setValue(recipe->miscs.at(i).m_amount * 1000.0);
	    } else if (recipe->miscs.at(i).m_name == "NaCl") {
		ui->bs_naclEdit->setValue(recipe->miscs.at(i).m_amount * 1000.0);
	    } else if (recipe->miscs.at(i).m_name == "MgCl2") {
		ui->bs_mgcl2Edit->setValue(recipe->miscs.at(i).m_amount * 1000.0);
	    } else if (recipe->miscs.at(i).m_name == "NaHCO3") {
		ui->bs_nahco3Edit->setValue(recipe->miscs.at(i).m_amount * 1000.0);
	    } else if (recipe->miscs.at(i).m_name == "CaCO3") {
		ui->bs_caco3Edit->setValue(recipe->miscs.at(i).m_amount * 1000.0);
	    } else if (recipe->miscs.at(i).m_name == "Melkzuur" || recipe->miscs.at(i).m_name == "Lactic") {
		recipe->wa_acid_name = 0;
		recipe->wa_acid_perc = my_acids.at(0).AcidPrc;
		ui->mw_acidPick->setCurrentIndex(0);
		ui->mw_acidpercEdit->setValue(my_acids.at(0).AcidPrc);
		ui->mw_acidvolEdit->setValue(recipe->miscs.at(i).m_amount * 1000.0);
	    } else if (recipe->miscs.at(i).m_name == "Zoutzuur" || recipe->miscs.at(i).m_name == "Hydrochloric") {
                recipe->wa_acid_name = 1;
                recipe->wa_acid_perc = my_acids.at(1).AcidPrc;
		ui->mw_acidPick->setCurrentIndex(1);
                ui->mw_acidpercEdit->setValue(my_acids.at(1).AcidPrc);
                ui->mw_acidvolEdit->setValue(recipe->miscs.at(i).m_amount * 1000.0);
	    } else if (recipe->miscs.at(i).m_name == "Fosforzuur" || recipe->miscs.at(i).m_name == "Phosphoric") {
                recipe->wa_acid_name = 2;
                recipe->wa_acid_perc = my_acids.at(2).AcidPrc;
		ui->mw_acidPick->setCurrentIndex(2);
                ui->mw_acidpercEdit->setValue(my_acids.at(2).AcidPrc);
                ui->mw_acidvolEdit->setValue(recipe->miscs.at(i).m_amount * 1000.0);
	    } else if (recipe->miscs.at(i).m_name == "Zwavelzuur" || recipe->miscs.at(i).m_name == "Sulfuric") {
                recipe->wa_acid_name = 3;
                recipe->wa_acid_perc = my_acids.at(3).AcidPrc;
		ui->mw_acidPick->setCurrentIndex(3);
                ui->mw_acidpercEdit->setValue(my_acids.at(3).AcidPrc);
                ui->mw_acidvolEdit->setValue(recipe->miscs.at(i).m_amount * 1000.0);
	    }
	}
    }
}

mercurial