src/EditRecipe.cpp

Sat, 11 Feb 2023 15:48:02 +0100

author
Michiel Broek <mbroek@mbse.eu>
date
Sat, 11 Feb 2023 15:48:02 +0100
changeset 493
520306773450
parent 408
1c913473e4dd
permissions
-rw-r--r--

Monitor iSpindels: use global variables instead of repeated expensive MySQL calls. Use the yeast temperature ranges for the colors on the thermometer scale. Don't show SVG and ABV if the OG is not yet known. Turn statusfield red if offline. Extra mon_ispindles fields yeast_lo and yeast_hi. Need at least bmsd version 0.3.42. If a websocket message is received that cannot be parsed, show the whole received message.

/**
 * EditRecipe.cpp is part of bmsapp.
 *
 * 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/>.
 */
#include "MainWindow.h"
#include "EditRecipe.h"
#include "PrinterDialog.h"
#include "../ui/ui_EditRecipe.h"
#include "Utils.h"
#include "global.h"
#include "config.h"
#include "database/db_recipe.h"
#include "database/db_product.h"


EditRecipe::EditRecipe(int id, QWidget *parent) : QDialog(parent), ui(new Ui::EditRecipe)
{
    QSqlQuery query, wquery, yquery;

    qInfo() << "EditRecipe record:" << id;
    recipe = new Recipe;
    ui->setupUi(this);
    recipe->fermentables_row = recipe->hops_row = recipe->miscs_row = recipe->yeasts_row = recipe->mashs_row = -1;
    recipe->fermentables_use100 = false;
    this->recno = id;

    WindowTitle();

    ui->typeEdit->addItem(tr("Extract"));
    ui->typeEdit->addItem(tr("Partial Mash"));
    ui->typeEdit->addItem(tr("All Grain"));

    ui->color_methodEdit->addItem("Morey");
    ui->color_methodEdit->addItem("Mosher");
    ui->color_methodEdit->addItem("Daniels");
    ui->color_methodEdit->addItem("Halberstadt");
    ui->color_methodEdit->addItem("Naudts");

    for (int i = 0; i < 3; i++)
	ui->ibu_methodEdit->addItem(g_ibu_method[i]);

    for (int i = 0; i < my_acids.size(); i++) {
	ui->mw_acidPick->addItem(my_acids.at(i).name_en);
	ui->sp_acidtypeEdit->addItem(my_acids.at(i).name_en);
    }

    query.prepare("SELECT name FROM inventory_waters ORDER BY record");
    query.exec();
    query.first();
    ui->w1_nameEdit->addItem("");
    ui->w2_nameEdit->addItem("");
    for (int i = 0; i < query.size(); i++) {
	ui->w1_nameEdit->addItem(query.value(0).toString());
	ui->w2_nameEdit->addItem(query.value(0).toString());
	query.next();
    }

    query.prepare("SELECT name FROM profile_water ORDER BY name");
    query.exec();
    ui->wt_sourceEdit->addItem("");
    while (query.next()) {
        ui->wt_sourceEdit->addItem(query.value(0).toString());
    }

    query.prepare("SELECT name FROM profile_mash ORDER BY name");
    query.exec();
    ui->mash_pickEdit->addItem("");
    while (query.next()) {
	ui->mash_pickEdit->addItem(query.value(0).toString());
    }

    ui->beerstyleEdit->addItem("");	// First add a dummy
    query.prepare("SELECT style_guide,style_letter,name FROM profile_styles ORDER BY style_guide,style_letter,name");
    query.exec();
    query.first();
    for (int i = 0; i < query.size(); i++) {
	ui->beerstyleEdit->addItem(query.value(0).toString()+" "+query.value(1).toString()+" "+query.value(2).toString());
	query.next();
    }

    ui->spargeGroup->setId(ui->w1_spButton, 0);
    ui->spargeGroup->setId(ui->w2_spButton, 1);
    ui->spargeGroup->setId(ui->wg_spButton, 2);

    if (id >= 0) {
	if (! DB_recipe::load(recipe, this, id))
            return;
    } else {
	/* Set some defaults */
	recipe->record = -1;
	recipe->locked = false;
	recipe->st_name = "";
	recipe->st_letter = "";
	recipe->st_guide = "";
	recipe->st_category = "";
	recipe->st_category_number = 0;
	recipe->st_type = 0;
	recipe->st_og_min = 1.025; recipe->st_og_max = 1.100;
	recipe->st_fg_min = 1.000; recipe->st_fg_max = 1.020;
	recipe->st_ibu_min = 5; recipe->st_ibu_max = 200;
	recipe->st_color_min = 3; recipe->st_color_max = 100;
	recipe->st_carb_min = 1.0; recipe->st_carb_max = 4.5;
	recipe->st_abv_min = 1; recipe->st_abv_max = 15;
	recipe->name = "";
	recipe->notes = "";
	recipe->efficiency = 75;
	recipe->batch_size = 20;
	recipe->boil_time = 60;
	recipe->boil_size = recipe->batch_size + ((0.1 * recipe->batch_size) * recipe->boil_time / 60.0);
	recipe->type = 2;
	recipe->est_og = recipe->est_fg = recipe->est_color = recipe->est_ibu = recipe->est_abv = 0;
	recipe->sparge_temp = 80;
        recipe->sparge_ph = 5.4;
        recipe->sparge_volume = 8;
        recipe->sparge_source = 0;
        recipe->sparge_acid_type = 0;
        recipe->sparge_acid_perc = 80;
        recipe->sparge_acid_amount = 0;
        recipe->mash_ph = 5.4;
        recipe->mash_name = "";
        recipe->calc_acid = true;
	recipe->w1_name = "";
        recipe->w1_amount = 0;
        recipe->w1_calcium = 0;
        recipe->w1_sulfate = 0;
        recipe->w1_chloride = 0;
        recipe->w1_sodium = 0;
        recipe->w1_magnesium = 0;
        recipe->w1_total_alkalinity = 0;
        recipe->w1_ph = 7;
        recipe->w1_cost = 0;
        recipe->w2_name = "";
        recipe->w2_amount = 0;
        recipe->w2_calcium = 0;
        recipe->w2_sulfate = 0;
        recipe->w2_chloride = 0;
        recipe->w2_sodium = 0;
        recipe->w2_magnesium = 0;
        recipe->w2_total_alkalinity = 0;
        recipe->w2_ph = 7;
        recipe->w2_cost = 0;
        recipe->wg_amount = 0;
        recipe->wg_calcium = 0;
        recipe->wg_sulfate = 0;
        recipe->wg_chloride = 0;
        recipe->wg_sodium = 0;
        recipe->wg_magnesium = 0;
        recipe->wg_total_alkalinity = 0;
        recipe->wg_ph = 7;
        recipe->wb_calcium = 0;
        recipe->wb_sulfate = 0;
        recipe->wb_chloride = 0;
        recipe->wb_sodium = 0;
        recipe->wb_magnesium = 0;
        recipe->wb_total_alkalinity = 0;
        recipe->wb_ph = 7;
	recipe->wa_acid_name = 0;
	recipe->wa_acid_perc = 80;
	recipe->wa_base_name = 0;
    }

    // Tab generic.
    ui->lockedEdit->setChecked(recipe->locked);
    ui->st_nameEdit->setText(recipe->st_name);
    ui->st_groupEdit->setText(recipe->st_letter);
    ui->st_guideEdit->setText(recipe->st_guide);
    ui->st_catEdit->setText(recipe->st_category);
    ui->st_catnrEdit->setText(QString("%1").arg(recipe->st_category_number));
    ui->st_typeEdit->setText(QCoreApplication::translate("BeerType", g_style_types[recipe->st_type]));
    ui->nameEdit->setText(recipe->name);
    ui->notesEdit->setPlainText(recipe->notes);
    ui->typeEdit->setCurrentIndex(recipe->type);
    ui->batch_sizeEdit->setValue(recipe->batch_size);
    ui->boil_sizeEdit->setValue(recipe->boil_size);
    ui->boil_timeEdit->setValue(recipe->boil_time);
    ui->efficiencyEdit->setValue(recipe->efficiency);
    ui->est_ogEdit->setValue(recipe->est_og);
    ui->est_ogShow->setRange(recipe->st_og_min, recipe->st_og_max);
    ui->est_ogShow->setPrecision(3);
    ui->est_ogShow->setMarkerTextIsValue(true);
    ui->est_ogShow->setValue(recipe->est_og);
    ui->est_fgEdit->setValue(recipe->est_fg);
    ui->est_fgShow->setRange(recipe->st_fg_min, recipe->st_fg_max);
    ui->est_fgShow->setPrecision(3);
    ui->est_fgShow->setMarkerTextIsValue(true);
    ui->est_fgShow->setValue(recipe->est_fg);
    ui->est_abvEdit->setValue(recipe->est_abv);
    ui->est_abvShow->setRange(recipe->st_abv_min, recipe->st_abv_max);
    ui->est_abvShow->setPrecision(1);
    ui->est_abvShow->setMarkerTextIsValue(true);
    ui->est_abvShow->setValue(recipe->est_abv);
    ui->est_colorEdit->setValue(recipe->est_color);
    ui->est_colorEdit->setStyleSheet(Utils::ebc_to_style(recipe->est_color));
    ui->est_colorShow->setPrecision(0);
    ui->est_colorShow->setMarkerTextIsValue(true);
    ui->est_colorShow->setRange(recipe->st_color_min, recipe->st_color_max);
    ui->est_colorShow->setValue(recipe->est_color);
    ui->color_methodEdit->setCurrentIndex(recipe->color_method);
    ui->est_ibuEdit->setValue(recipe->est_ibu);
    ui->est_ibuShow->setPrecision(0);
    ui->est_ibuShow->setMarkerTextIsValue(true);
    ui->est_ibuShow->setRange(recipe->st_ibu_min, recipe->st_ibu_max);
    ui->est_ibuShow->setValue(recipe->est_ibu);
    ui->ibu_methodEdit->setCurrentIndex(recipe->ibu_method);
    ui->est_carbEdit->setValue(recipe->est_carb);
    ui->est_carbShow->setPrecision(1);
    ui->est_carbShow->setMarkerTextIsValue(true);
    ui->est_carbShow->setRange(recipe->st_carb_min, recipe->st_carb_max);
    ui->est_carbShow->setValue(recipe->est_carb);
    calcStyle();
    ui->est_buguShow->setPrecision(2);
    ui->est_buguShow->setMarkerTextIsValue(true);
    ui->est_bufguShow->setPrecision(2);
    ui->est_bufguShow->setMarkerTextIsValue(true);

    // Tab fermentables.
    ui->est_og2Edit->setValue(recipe->est_og);
    ui->est_color2Edit->setValue(recipe->est_color);
    ui->est_color2Edit->setStyleSheet(Utils::ebc_to_style(recipe->est_color));

    // Tab hops.
    ui->est_ibu2Edit->setValue(recipe->est_ibu);

    // Tab yeasts.
    ui->est_og3Edit->setValue(recipe->est_og);
    ui->est_fg3Edit->setValue(recipe->est_fg);
    ui->est_abv2Edit->setValue(recipe->est_abv);

    // Tab mashs.
    ui->mash_nameEdit->setText(recipe->mash_name);

    // Tab waters.
    qDebug() << "water 1" << recipe->w1_name << "default" << my_default_water;
    if (recipe->w1_ph > 4.0) {
	/*
	 * Water data seems present, use that and set the name between []
	 */
	ui->w1_nameEdit->setPlaceholderText(QString("["+recipe->w1_name+"]"));
    } else {
	bool found = false;
	if (recipe->w1_name != "") {
	    /*
	     * We have a name, but do we know it?
	     */
	    query.prepare("SELECT * FROM inventory_waters WHERE name=:water");
            query.bindValue(":water", recipe->w1_name);
	    query.exec();
	    found = query.first();
	}
	if (!found) {
	    /*
	     * Try to load default water
	     */
	    query.prepare("SELECT * FROM inventory_waters WHERE record=:record");
	    query.bindValue(":record", my_default_water);
	    query.exec();
	    found = query.first();
	}
	if (found) {
	    recipe->w1_calcium = query.value(3).toDouble();
	    recipe->w1_magnesium = query.value(8).toDouble();
	    recipe->w1_total_alkalinity = query.value(11).toDouble();
	    recipe->w1_sodium = query.value(7).toDouble();
	    recipe->w1_chloride = query.value(6).toDouble();
	    recipe->w1_sulfate = query.value(5).toDouble();
	    recipe->w1_ph = query.value(9).toDouble();
            ui->w1_nameEdit->setCurrentIndex(query.value(0).toInt());
	} else {
	    recipe->w1_calcium = 0;
	    recipe->w1_magnesium = 0;
	    recipe->w1_total_alkalinity = 0;
	    recipe->w1_sodium = 0;
	    recipe->w1_chloride = 0;
	    recipe->w1_sulfate = 0;
	    recipe->w1_ph = 0;
	}
    }
    ui->w1_volEdit->setValue(recipe->w1_amount);
    ui->w1_caEdit->setValue(recipe->w1_calcium);
    ui->w1_mgEdit->setValue(recipe->w1_magnesium);
    ui->w1_hco3Edit->setValue(Utils::Bicarbonate(recipe->w1_total_alkalinity, recipe->w1_ph));
    ui->w1_caco3Edit->setValue(recipe->w1_total_alkalinity);
    ui->w1_naEdit->setValue(recipe->w1_sodium);
    ui->w1_clEdit->setValue(recipe->w1_chloride);
    ui->w1_so4Edit->setValue(recipe->w1_sulfate);
    ui->w1_phEdit->setValue(recipe->w1_ph);

    qDebug() << "water 2" << recipe->w2_name;
    if (recipe->w2_ph  > 4.0) {
	ui->w2_nameEdit->setPlaceholderText(QString("["+recipe->w2_name+"]"));
    } else if (recipe->w2_name != "") {
	query.prepare("SELECT * FROM inventory_waters WHERE name=:water");
	query.bindValue(":water", recipe->w2_name);
	query.exec();
	if (query.first()) {
	    recipe->w2_calcium = query.value(3).toDouble();
            recipe->w2_magnesium = query.value(8).toDouble();
            recipe->w2_total_alkalinity = query.value(11).toDouble();
            recipe->w2_sodium = query.value(7).toDouble();
            recipe->w2_chloride = query.value(6).toDouble();
            recipe->w2_sulfate = query.value(5).toDouble();
            recipe->w2_ph = query.value(9).toDouble();
	    ui->w2_nameEdit->setCurrentIndex(query.value(0).toInt());
	} else {
	    recipe->w2_calcium = 0;
            recipe->w2_magnesium = 0;
            recipe->w2_total_alkalinity = 0;
            recipe->w2_sodium = 0;
            recipe->w2_chloride = 0;
            recipe->w2_sulfate = 0;
            recipe->w2_ph = 0;
	}
    }
    ui->w2_volEdit->setValue(recipe->w2_amount);
    ui->w2_caEdit->setValue(recipe->w2_calcium);
    ui->w2_mgEdit->setValue(recipe->w2_magnesium);
    ui->w2_hco3Edit->setValue(Utils::Bicarbonate(recipe->w2_total_alkalinity, recipe->w2_ph));
    ui->w2_caco3Edit->setValue(recipe->w2_total_alkalinity);
    ui->w2_naEdit->setValue(recipe->w2_sodium);
    ui->w2_clEdit->setValue(recipe->w2_chloride);
    ui->w2_so4Edit->setValue(recipe->w2_sulfate);
    ui->w2_phEdit->setValue(recipe->w2_ph);
    ui->mw_autoEdit->setChecked(recipe->calc_acid);
    ui->mw_phEdit->setReadOnly(! recipe->calc_acid);
    ui->mw_phEdit->setButtonSymbols(recipe->calc_acid ? QAbstractSpinBox::UpDownArrows : QAbstractSpinBox::NoButtons);
    ui->mw_acidvolEdit->setReadOnly(recipe->calc_acid);
    ui->mw_acidvolEdit->setButtonSymbols(recipe->calc_acid ? QAbstractSpinBox::NoButtons : QAbstractSpinBox::UpDownArrows);

    ui->sp_phEdit->setReadOnly(! recipe->calc_acid);
    ui->sp_phEdit->setButtonSymbols(recipe->calc_acid ? QAbstractSpinBox::UpDownArrows : QAbstractSpinBox::NoButtons);
    ui->sp_acidvolEdit->setReadOnly(recipe->calc_acid);
    ui->sp_acidvolEdit->setButtonSymbols(recipe->calc_acid ? QAbstractSpinBox::NoButtons : QAbstractSpinBox::UpDownArrows);
    ui->sp_volEdit->setValue(recipe->sparge_volume);
    ui->sp_phEdit->setValue(recipe->sparge_ph);
    ui->sp_acidtypeEdit->setCurrentIndex(recipe->sparge_acid_type);
    ui->sp_acidpercEdit->setValue(recipe->sparge_acid_perc);

    // All signals from tab "Generic"
    connect(ui->lockedEdit, &QCheckBox::stateChanged, this, &EditRecipe::is_changed);
    connect(ui->nameEdit, &QLineEdit::textChanged, this, &EditRecipe::name_changed);
    connect(ui->notesEdit, SIGNAL(textChanged()), this, SLOT(notes_changed()));
    connect(ui->typeEdit, QOverload<int>::of(&QComboBox::currentIndexChanged), this, &EditRecipe::brew_type_changed);
    connect(ui->batch_sizeEdit, QOverload<double>::of(&QDoubleSpinBox::valueChanged), this, &EditRecipe::batch_size_changed);
    connect(ui->boil_timeEdit, QOverload<int>::of(&QSpinBox::valueChanged), this, &EditRecipe::boil_time_changed);
    connect(ui->efficiencyEdit, QOverload<double>::of(&QDoubleSpinBox::valueChanged), this, &EditRecipe::efficiency_changed);
    connect(ui->beerstyleEdit, QOverload<int>::of(&QComboBox::currentIndexChanged), this, &EditRecipe::style_changed);
    connect(ui->est_ogEdit, QOverload<double>::of(&QDoubleSpinBox::valueChanged), this, &EditRecipe::est_og_changed);
    connect(ui->color_methodEdit, QOverload<int>::of(&QComboBox::currentIndexChanged), this, &EditRecipe::colormethod_changed);
    connect(ui->ibu_methodEdit, QOverload<int>::of(&QComboBox::currentIndexChanged), this, &EditRecipe::ibumethod_changed);
    connect(ui->lockedEdit, &QCheckBox::stateChanged, this, &EditRecipe::locked_changed);

    // All signals from tab "Fermentables"
    ui->fermentablesTable->setEditTriggers(QAbstractItemView::NoEditTriggers);
    connect(ui->est_og2Edit, QOverload<double>::of(&QDoubleSpinBox::valueChanged), this, &EditRecipe::est_og_changed);
    connect(ui->perc_mashShow, &QProgressBar::valueChanged, this, &EditRecipe::ferment_perc_mash_valueChanged);
    connect(ui->perc_sugarsShow, &QProgressBar::valueChanged, this, &EditRecipe::ferment_perc_sugars_valueChanged);
    connect(ui->perc_caraShow, &QProgressBar::valueChanged, this, &EditRecipe::ferment_perc_cara_valueChanged);
    connect(ui->lintnerShow, &QProgressBar::valueChanged, this, &EditRecipe::ferment_lintner_valueChanged);
    connect(ui->addFermentable, SIGNAL(clicked()), this, SLOT(addFermentRow_clicked()));

    // All signals from tab "Hops"
    ui->hopsTable->setEditTriggers(QAbstractItemView::NoEditTriggers);
    connect(ui->hop_tasteShow, &QProgressBar::valueChanged, this, &EditRecipe::hop_Flavour_valueChanged);
    connect(ui->hop_aromaShow, &QProgressBar::valueChanged, this, &EditRecipe::hop_Aroma_valueChanged);
    connect(ui->addHop, SIGNAL(clicked()), this, SLOT(addHopRow_clicked()));

    // All signals from tab "Miscs"
    ui->miscsTable->setEditTriggers(QAbstractItemView::NoEditTriggers);
    connect(ui->addMisc, SIGNAL(clicked()), this, SLOT(addMiscRow_clicked()));

    // All signals from tab "Yeasts"
    ui->yeastsTable->setEditTriggers(QAbstractItemView::NoEditTriggers);
    connect(ui->addYeast, SIGNAL(clicked()), this, SLOT(addYeastRow_clicked()));

    // All signals from tab "Mash"
    ui->mashsTable->setEditTriggers(QAbstractItemView::NoEditTriggers);
    connect(ui->addMash, SIGNAL(clicked()), this, SLOT(addMashRow_clicked()));
    connect(ui->mash_pickEdit, QOverload<int>::of(&QComboBox::currentIndexChanged), this, &EditRecipe::mash_select_changed);
    connect(ui->mash_nameEdit, &QLineEdit::textChanged, this, &EditRecipe::mash_name_changed);

    // All signals from tab "Water"
    connect(ui->bs_cacl2Edit, QOverload<double>::of(&QDoubleSpinBox::valueChanged), this, &EditRecipe::wb_cacl2_changed);
    connect(ui->bs_caso4Edit, QOverload<double>::of(&QDoubleSpinBox::valueChanged), this, &EditRecipe::wb_caso4_changed);
    connect(ui->bs_mgso4Edit, QOverload<double>::of(&QDoubleSpinBox::valueChanged), this, &EditRecipe::wb_mgso4_changed);
    connect(ui->bs_naclEdit, QOverload<double>::of(&QDoubleSpinBox::valueChanged), this, &EditRecipe::wb_nacl_changed);
    connect(ui->bs_mgcl2Edit, QOverload<double>::of(&QDoubleSpinBox::valueChanged), this, &EditRecipe::wb_mgcl2_changed);
    connect(ui->bs_nahco3Edit, QOverload<double>::of(&QDoubleSpinBox::valueChanged), this, &EditRecipe::wb_nahco3_changed);
    connect(ui->bs_caco3Edit, QOverload<double>::of(&QDoubleSpinBox::valueChanged), this, &EditRecipe::wb_caco3_changed);
    connect(ui->ss_cacl2Edit, QOverload<double>::of(&QDoubleSpinBox::valueChanged), this, &EditRecipe::sp_cacl2_changed);
    connect(ui->ss_caso4Edit, QOverload<double>::of(&QDoubleSpinBox::valueChanged), this, &EditRecipe::sp_caso4_changed);
    connect(ui->ss_mgso4Edit, QOverload<double>::of(&QDoubleSpinBox::valueChanged), this, &EditRecipe::sp_mgso4_changed);
    connect(ui->ss_naclEdit, QOverload<double>::of(&QDoubleSpinBox::valueChanged), this, &EditRecipe::sp_nacl_changed);
    connect(ui->ss_mgcl2Edit, QOverload<double>::of(&QDoubleSpinBox::valueChanged), this, &EditRecipe::sp_mgcl2_changed);
    connect(ui->mw_autoEdit, &QCheckBox::stateChanged, this, &EditRecipe::mw_calc_acid_clicked);
    connect(ui->mw_phEdit, QOverload<double>::of(&QDoubleSpinBox::valueChanged), this, &EditRecipe::mw_ph_changed);
    connect(ui->mw_acidvolEdit, QOverload<double>::of(&QDoubleSpinBox::valueChanged), this, &EditRecipe::mw_acid_changed);
    connect(ui->mw_acidPick, QOverload<int>::of(&QComboBox::currentIndexChanged), this, &EditRecipe::mw_type_changed);
    connect(ui->wt_sourceEdit, QOverload<int>::of(&QComboBox::currentIndexChanged), this, &EditRecipe::wt_target_changed);
    connect(ui->w1_nameEdit, QOverload<int>::of(&QComboBox::currentIndexChanged), this, &EditRecipe::w1_name_changed);
    connect(ui->w2_nameEdit, QOverload<int>::of(&QComboBox::currentIndexChanged), this, &EditRecipe::w2_name_changed);
    connect(ui->w2_volEdit, QOverload<double>::of(&QDoubleSpinBox::valueChanged), this, &EditRecipe::w2_volume_changed);
    connect(ui->spargeGroup, SIGNAL(buttonClicked(int)), this, SLOT(sp_group_changed(int)));
    connect(ui->sp_acidtypeEdit, QOverload<int>::of(&QComboBox::currentIndexChanged), this, &EditRecipe::sp_type_changed);
    connect(ui->sp_phEdit, QOverload<double>::of(&QDoubleSpinBox::valueChanged), this, &EditRecipe::sp_ph_changed);
    connect(ui->sp_volEdit, QOverload<double>::of(&QDoubleSpinBox::valueChanged), this, &EditRecipe::sp_vol_changed);
    connect(ui->sp_acidvolEdit, QOverload<double>::of(&QDoubleSpinBox::valueChanged), this, &EditRecipe::sp_acid_changed);
    setLocked(recipe->locked);

    ui->saveButton->setEnabled(false);
    ui->deleteButton->setEnabled((id >= 0 && ! recipe->locked) ? true:false);

    emit refreshAll();
}


EditRecipe::~EditRecipe()
{
    qDebug() << "EditRecipe done start";
    delete ui;
    emit entry_changed();
    qDebug() << "EditRecipe done final";
}


void EditRecipe::refreshAll()
{
    refreshFermentables();
    calcFermentables();		/* Must be before Hops */
    refreshHops();
    calcIBUs();
    refreshMiscs();
    refreshYeasts();
    calcYeast();
    calcMash();
    refreshMashs();
    refreshWaters();
    calcWater();
}


/*
 * Window header, mark any change with '**'
 */
void EditRecipe::WindowTitle()
{
    QString txt;

    if (this->recno < 0) {
	txt = QString(tr("BMSapp - Add new recipe"));
    } else {
	txt = QString(tr("BMSapp - Edit recipe %1").arg(this->recno));
    }

    if (this->textIsChanged) {
	txt.append((QString(" **")));
    }
    setWindowTitle(txt);
}


void EditRecipe::on_saveButton_clicked()
{
    QSqlQuery query;

    /* If there are errors in the form, show a message and do "return;" */
    if (ui->nameEdit->text().length() < 2) {
	QMessageBox::warning(this, tr("Edit Recipe"), tr("Name empty or too short."));
	return;
    }
    if (ui->st_nameEdit->text().length() < 2) {
        QMessageBox::warning(this, tr("Edit Recipe"), tr("No beerstyle selected."));
        return;
    }

    if (this->textIsChanged) {
	recipe->record = this->recno;
        if (DB_recipe::save(recipe, this)) {
            /*
             * If this was a new record, update the global recno.
             */
            this->recno = recipe->record;
        }
    }

    ui->saveButton->setEnabled(false);
    this->textIsChanged = false;
    WindowTitle();
}


#include "EditRecipeTab1.cpp"
#include "EditRecipeTab2.cpp"
#include "EditRecipeTab3.cpp"
#include "EditRecipeTab4.cpp"
#include "EditRecipeTab5.cpp"
#include "EditRecipeTab6.cpp"
#include "EditRecipeTab7.cpp"
#include "EditRecipeExport.cpp"


void EditRecipe::on_deleteButton_clicked()
{
    int rc = QMessageBox::warning(this, tr("Delete recipe"), tr("Delete %1").arg(recipe->name),
                    QMessageBox::Yes | QMessageBox::No, QMessageBox::No);
    if (rc == QMessageBox::No)
        return;

    DB_recipe::dele(this, this->recno);

    this->close();
    this->setResult(1);
}


void EditRecipe::is_changed()
{
    ui->saveButton->setEnabled(true);
    ui->deleteButton->setEnabled(((this->recno >= 0) ? true:false) && ! recipe->locked);
    this->textIsChanged = true;
    WindowTitle();
}


void EditRecipe::on_quitButton_clicked()
{
    if (this->textIsChanged) {
	int rc = QMessageBox::warning(this, tr("Recipe changed"), tr("The recipe has been modified. Save changes?"),
                                QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel, QMessageBox::Save);
        switch (rc) {
            case QMessageBox::Save:
                        on_saveButton_clicked();
                        break;  /* Saved and then Quit */
            case QMessageBox::Discard:
                        break;  /* Quit without Save */
            case QMessageBox::Cancel:
                        return; /* Return to the editor page */
        }
    }

    this->close();
    this->setResult(1);
}

mercurial