src/EditRecipe.cpp

Thu, 31 Mar 2022 23:10:57 +0200

author
Michiel Broek <mbroek@mbse.eu>
date
Thu, 31 Mar 2022 23:10:57 +0200
changeset 97
8283bbf95806
parent 96
c36fef8bb088
child 98
1425bf3e18ed
permissions
-rw-r--r--

Stripped down the RangedSlider. It is more compact and still does everything. Base colors (red and green) are fixed inside, also automatisc setting of outer limits. The tooltip shows the current ranges. Still some finetuning to be done.

/**
 * 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 "EditRecipe.h"
#include "../ui/ui_EditRecipe.h"
#include "MainWindow.h"
#include "Utils.h"


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

    qDebug() << "EditRecipe record:" << id;
    ui->setupUi(this);
    this->recno = id;

    WindowTitle();

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

    if (id >= 0) {
	query.prepare("SELECT * FROM recipes WHERE record = :recno");
	query.bindValue(":recno", id);
	query.exec();
	query.next();

	ui->lockedEdit->setChecked(query.value(2).toInt() ? true:false);
	//  3 st_name
	//  4 st_letter
	//  5 st_guide
	//  6 st_category
	//  7 st_category_number
	//  8 st_type
	//  9 st_og_min
	// 10 st_og_max
	// 11 st_fg_min
	// 12 st_fg_max
	// 13 st_ibu_min
	// 14 st_ibu_max
	// 15 st_color_min
	// 16 st_color_max
	// 17 st_carb_min
	// 18 st_carb_max
	// 19 st_abv_min
	// 20 st_abv_max
	ui->nameEdit->setText(query.value(21).toString());
	ui->notesEdit->setPlainText(query.value(22).toString());
	ui->typeEdit->setCurrentIndex(query.value(23).toInt());
	ui->batch_sizeEdit->setValue(query.value(24).toDouble());
	ui->boil_sizeEdit->setValue(query.value(25).toDouble());
	ui->boil_timeEdit->setValue(query.value(26).toInt());
	ui->efficiencyEdit->setValue(query.value(27).toDouble());

	ui->est_ogEdit->setValue(query.value(28).toDouble());
	ui->est_ogShow->setRange(query.value(9).toDouble(), query.value(10).toDouble());
	ui->est_ogShow->setPrecision(3);
	ui->est_ogShow->setMarkerTextIsValue(true);
	ui->est_ogShow->setValue(query.value(28).toDouble());

	ui->est_fgEdit->setValue(query.value(29).toDouble());
        ui->est_fgShow->setRange(query.value(11).toDouble(), query.value(12).toDouble());
        ui->est_fgShow->setPrecision(3);
        ui->est_fgShow->setMarkerTextIsValue(true);
        ui->est_fgShow->setValue(query.value(29).toDouble());

	ui->est_abvEdit->setValue(query.value(30).toDouble());
        ui->est_abvShow->setRange(query.value(19).toDouble(), query.value(20).toDouble());
        ui->est_abvShow->setPrecision(1);
        ui->est_abvShow->setMarkerTextIsValue(true);
        ui->est_abvShow->setValue(query.value(30).toDouble());

	ui->est_colorEdit->setValue(query.value(31).toDouble());
	QColor color = Utils::ebc_to_color(query.value(31).toInt());
	ui->est_colorGlass->setColor(color);
        ui->est_colorShow->setPrecision(0);
        ui->est_colorShow->setMarkerTextIsValue(true);
	ui->est_colorShow->setRange(query.value(15).toDouble(), query.value(16).toDouble());
        ui->est_colorShow->setValue(query.value(31).toDouble());

	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");
	ui->color_methodEdit->setCurrentIndex(query.value(32).toInt());

	// 33 est_ibu
	// 34 ibu_method
	// 35 est_carb
	// 36 sparge_temp
	// 37 sparge_ph
	// 38 sparge_volume
	// 39 sparge_source
	// 40 sparge_acid_type
	// 41 sparge_acid_perc
	// 42 sparge_acid_amount
	// 43 mash_ph
	// 44 mash_name
	// 45 calc_acid
	// 46 w1_name
	// 47 w1_amount
	// 48 w1_calcium
	// 49 w1_sulfate
	// 50 w1_chloride
	// 51 w1_sodium
	// 52 w1_magnesium
	// 53 w1_total_alkalinity
	// 54 w1_ph
	// 55 w1_cost
	// 56 w2_name
	// 57 w2_amount
	// 58 w2_calcium
	// 59 w2_sulfate
	// 60 w2_chloride
	// 61 w2_sodium
	// 62 w2_magnesium
	// 63 w2_total_alkalinity
	// 64 w2_ph
	// 65 w2_cost
	// 66 wg_amount
	// 67 wg_calcium
	// 68 wg_sulfate
	// 69 wg_chloride
	// 70 wg_sodium
	// 71 wg_magnesium
	// 72 wg_total_alkalinity
	// 73 wg_ph
	// 74 wb_calcium
	// 75 wb_sulfate
	// 76 wb_chloride
	// 77 wb_sodium
	// 78 wb_magnesium
	// 79 wb_total_alkalinity
	// 80 wb_ph
	// 81 wa_acid_name
	// 82 wa_acid_perc
	// 83 wa_base_name
	// 84 json_fermentables
	// 85 json_hops
	// 86 json_miscs
	// 87 json_yeasts
	// 88 json_mashs
    } else {
	/* Set some defaults */
    }

    connect(ui->lockedEdit, &QCheckBox::stateChanged, this, &EditRecipe::is_changed);

    connect(ui->nameEdit, &QLineEdit::textChanged, this, &EditRecipe::is_changed);
    connect(ui->notesEdit, SIGNAL(textChanged()), this, SLOT(is_changed()));
    connect(ui->typeEdit, &QComboBox::currentTextChanged, this, &EditRecipe::is_changed);
    connect(ui->batch_sizeEdit, &QDoubleSpinBox::textChanged, this, &EditRecipe::is_changed);
    connect(ui->boil_sizeEdit, &QDoubleSpinBox::textChanged, this, &EditRecipe::is_changed);
    connect(ui->boil_timeEdit, &QSpinBox::textChanged, this, &EditRecipe::is_changed);
    connect(ui->efficiencyEdit, &QDoubleSpinBox::textChanged, this, &EditRecipe::is_changed);

    connect(ui->color_methodEdit, &QComboBox::currentTextChanged, this, &EditRecipe::is_changed);

    ui->saveButton->setEnabled(false);
    ui->deleteButton->setEnabled((id >= 0) ? true:false);
}


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


/*
 * 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 (this->textIsChanged) {
    	if (this->recno == -1) {
    	    query.prepare("INSERT INTO recipes SET name=:name, "
		"uuid = :uuid");
    	} else {
	    query.prepare("UPDATE recipes SET name=:name "
                " WHERE record = :recno");
    	}
	//query.bindValue(":name", ui->nameEdit->text());
	//query.bindValue(":notes", ui->notesEdit->toPlainText());
	if (this->recno == -1) {
	    query.bindValue(":uuid", QUuid::createUuid().toString().mid(1, 36));
	} else {
	    query.bindValue(":recno", this->recno);
	}
	query.exec();
	if (query.lastError().isValid()) {
	    qDebug() << "EditRecipe" << query.lastError();
	    QMessageBox::warning(this, tr("Database error"),
                        tr("MySQL error: %1\n%2\n%3")
                        .arg(query.lastError().nativeErrorCode())
                        .arg(query.lastError().driverText())
                        .arg(query.lastError().databaseText()));
	} else {
	    qDebug() << "EditRecipe Saved";
	}
    }

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


void EditRecipe::on_deleteButton_clicked()
{
    QSqlQuery query;

    query.prepare("DELETE FROM recipes WHERE record = :recno");
    query.bindValue(":recno", this->recno);
    query.exec();
    if (query.lastError().isValid()) {
	qDebug() << "EditRecipe" << query.lastError();
	QMessageBox::warning(this, tr("Database error"),
                        tr("MySQL error: %1\n%2\n%3")
                        .arg(query.lastError().nativeErrorCode())
                        .arg(query.lastError().driverText())
                        .arg(query.lastError().databaseText()));
    } else {
	qDebug() << "EditRecipe Deleted" << this->recno;
    }

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


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


void EditRecipe::time_changed()
{
    is_changed();
}


void EditRecipe::on_quitButton_clicked()
{
    if (this->textIsChanged) {
	int rc = QMessageBox::warning(this, tr("Recipe changed"), tr("The ingredient 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