src/EditProfileStyle.cpp

Fri, 20 May 2022 20:43:33 +0200

author
Michiel Broek <mbroek@mbse.eu>
date
Fri, 20 May 2022 20:43:33 +0200
changeset 225
448e4187cada
parent 90
2396457a8167
permissions
-rw-r--r--

Implemented the tasting tab. On the generic tab, show the ingredients check or the read only prompt on the same place depending on the product stage. Fixed the yeasts ingredients in stock check. Reordered the tab order of all edit fields. It looks like this module is ready and needs testing.

/**
 * EditProfileStyle.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 "EditProfileStyle.h"
#include "../ui/ui_EditProfileStyle.h"
#include "MainWindow.h"


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

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

    WindowTitle();

    ui->typeEdit->addItem(tr("Lager"));
    ui->typeEdit->addItem(tr("Ale"));
    ui->typeEdit->addItem(tr("Mead"));
    ui->typeEdit->addItem(tr("Wheat"));
    ui->typeEdit->addItem(tr("Mixed"));
    ui->typeEdit->addItem(tr("Cider"));

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

	ui->styleEdit->setText(query.value(1).toString());
	ui->catEdit->setText(query.value(2).toString());
	ui->catnrEdit->setValue(query.value(3).toInt());
	ui->groupEdit->setText(query.value(4).toString());
	ui->guideEdit->setText(query.value(5).toString());
	ui->typeEdit->setCurrentIndex(query.value(6).toInt());
	ui->ogminEdit->setValue(query.value(7).toDouble());
	ui->ogmaxEdit->setValue(query.value(8).toDouble());
	ui->fgminEdit->setValue(query.value(9).toDouble());
	ui->fgmaxEdit->setValue(query.value(10).toDouble());
	ui->ibuminEdit->setValue(query.value(11).toDouble());
	ui->ibumaxEdit->setValue(query.value(12).toDouble());
	ui->ebcminEdit->setValue(query.value(13).toDouble());
        ui->ebcmaxEdit->setValue(query.value(14).toDouble());
	ui->co2minEdit->setValue(query.value(15).toDouble());
        ui->co2maxEdit->setValue(query.value(16).toDouble());
	ui->abvminEdit->setValue(query.value(17).toDouble());
        ui->abvmaxEdit->setValue(query.value(18).toDouble());
	ui->notesEdit->setPlainText(query.value(19).toString());
	ui->profileEdit->setPlainText(query.value(20).toString());
	ui->ingredientsEdit->setPlainText(query.value(21).toString());
	ui->examplesEdit->setPlainText(query.value(22).toString());
    } else {
	/* Set some defaults */
	ui->catnrEdit->setValue(0);
	ui->guideEdit->setText("BKG 2019");
	ui->typeEdit->setCurrentIndex(0);
	ui->ogminEdit->setValue(1.030);
	ui->ogmaxEdit->setValue(1.060);
	ui->fgminEdit->setValue(1.005);
	ui->fgmaxEdit->setValue(1.010);
	ui->ibuminEdit->setValue(20);
	ui->ibumaxEdit->setValue(30);
	ui->ebcminEdit->setValue(52);
	ui->ebcmaxEdit->setValue(79);
	ui->co2minEdit->setValue(2.0);
	ui->co2maxEdit->setValue(2.5);
	ui->abvminEdit->setValue(4.0);
	ui->abvmaxEdit->setValue(5.0);
    }
    connect(ui->styleEdit, &QLineEdit::textChanged, this, &EditProfileStyle::is_changed);
    connect(ui->catEdit, &QLineEdit::textChanged, this, &EditProfileStyle::is_changed);
    connect(ui->catnrEdit, &QSpinBox::textChanged, this, &EditProfileStyle::is_changed);
    connect(ui->groupEdit, &QLineEdit::textChanged, this, &EditProfileStyle::is_changed);
    connect(ui->guideEdit, &QLineEdit::textChanged, this, &EditProfileStyle::is_changed);
    connect(ui->typeEdit, &QComboBox::currentTextChanged, this, &EditProfileStyle::is_changed);
    connect(ui->ogminEdit, &QDoubleSpinBox::textChanged, this, &EditProfileStyle::ogmin_changed);
    connect(ui->ogmaxEdit, &QDoubleSpinBox::textChanged, this, &EditProfileStyle::ogmax_changed);
    connect(ui->fgminEdit, &QDoubleSpinBox::textChanged, this, &EditProfileStyle::fgmin_changed);
    connect(ui->fgmaxEdit, &QDoubleSpinBox::textChanged, this, &EditProfileStyle::fgmax_changed);
    connect(ui->ibuminEdit, &QSpinBox::textChanged, this, &EditProfileStyle::ibumin_changed);
    connect(ui->ibumaxEdit, &QSpinBox::textChanged, this, &EditProfileStyle::ibumax_changed);
    connect(ui->ebcminEdit, &QSpinBox::textChanged, this, &EditProfileStyle::ebcmin_changed);
    connect(ui->ebcmaxEdit, &QSpinBox::textChanged, this, &EditProfileStyle::ebcmax_changed);
    connect(ui->co2minEdit, &QDoubleSpinBox::textChanged, this, &EditProfileStyle::co2min_changed);
    connect(ui->co2maxEdit, &QDoubleSpinBox::textChanged, this, &EditProfileStyle::co2max_changed);
    connect(ui->abvminEdit, &QDoubleSpinBox::textChanged, this, &EditProfileStyle::abvmin_changed);
    connect(ui->abvmaxEdit, &QDoubleSpinBox::textChanged, this, &EditProfileStyle::abvmax_changed);
    connect(ui->notesEdit, SIGNAL(textChanged()), this, SLOT(is_changed()));
    connect(ui->profileEdit, SIGNAL(textChanged()), this, SLOT(is_changed()));
    connect(ui->ingredientsEdit, SIGNAL(textChanged()), this, SLOT(is_changed()));
    connect(ui->examplesEdit, SIGNAL(textChanged()), this, SLOT(is_changed()));

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


EditProfileStyle::~EditProfileStyle()
{
    qDebug() << "EditProfileStyle done";
    delete ui;
    emit entry_changed();
}


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

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

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


void EditProfileStyle::on_saveButton_clicked()
{
    QSqlQuery query;

    /* If there are errors in the form, show a message and do "return;" */
    if (ui->styleEdit->text().length() < 2) {
	QMessageBox::warning(this, tr("Edit Style"), tr("Beer style is empty or too short."));
	return;
    }

    if (this->textIsChanged) {
    	if (this->recno == -1) {
    	    query.prepare("INSERT INTO profile_styles SET name=:name, category=:category, "
		"category_number=:catnr, style_letter=:group, style_guide=:guide, type=:type, "
		"og_min=:og_min, og_max=:og_max, fg_min=:fg_min, fg_max=:fg_max, ibu_min=:ibu_min, "
		"ibu_max=:ibu_max, color_min=:ebc_min, color_max=:ebc_max, carb_min=:co2_min, "
		"carb_max=:co2_max, abv_min=:abv_min, abv_max=:abv_max, notes=:notes, "
		"profile=:profile, ingredients=:ingredients, examples=:examples, uuid=:uuid");
    	} else {
	    query.prepare("UPDATE profile_styles SET name=:name, category=:category, "
		"category_number=:catnr, style_letter=:group, style_guide=:guide, type=:type, "
                "og_min=:og_min, og_max=:og_max, fg_min=:fg_min, fg_max=:fg_max, ibu_min=:ibu_min, "
                "ibu_max=:ibu_max, color_min=:ebc_min, color_max=:ebc_max, carb_min=:co2_min, "
                "carb_max=:co2_max, abv_min=:abv_min, abv_max=:abv_max, notes=:notes, "
                "profile=:profile, ingredients=:ingredients, examples=:examples WHERE record=:recno");
    	}
	query.bindValue(":name", ui->styleEdit->text());
	query.bindValue(":category", ui->catEdit->text());
	query.bindValue(":catnr", QString("%1").arg(ui->catnrEdit->value()));
	query.bindValue(":group", ui->groupEdit->text());
	query.bindValue(":guide", ui->guideEdit->text());
	query.bindValue(":type", ui->typeEdit->currentIndex());
	query.bindValue(":og_min", QString("%1").arg(ui->ogminEdit->value(), 4, 'f', 3, '0'));
	query.bindValue(":og_max", QString("%1").arg(ui->ogmaxEdit->value(), 4, 'f', 3, '0'));
	query.bindValue(":fg_min", QString("%1").arg(ui->fgminEdit->value(), 4, 'f', 3, '0'));
	query.bindValue(":fg_max", QString("%1").arg(ui->fgmaxEdit->value(), 4, 'f', 3, '0'));
	query.bindValue(":ibu_min", QString("%1").arg(ui->ibuminEdit->value(), 1, 'f', 0, '0'));
	query.bindValue(":ibu_max", QString("%1").arg(ui->ibumaxEdit->value(), 1, 'f', 0, '0'));
	query.bindValue(":ebc_min", QString("%1").arg(ui->ebcminEdit->value(), 1, 'f', 0, '0'));
	query.bindValue(":ebc_max", QString("%1").arg(ui->ebcmaxEdit->value(), 1, 'f', 0, '0'));
	query.bindValue(":co2_min", QString("%1").arg(ui->co2minEdit->value(), 2, 'f', 1, '0'));
	query.bindValue(":co2_max", QString("%1").arg(ui->co2maxEdit->value(), 2, 'f', 1, '0'));
	query.bindValue(":abv_min", QString("%1").arg(ui->abvminEdit->value(), 2, 'f', 1, '0'));
	query.bindValue(":abv_max", QString("%1").arg(ui->abvmaxEdit->value(), 2, 'f', 1, '0'));
	query.bindValue(":notes", ui->notesEdit->toPlainText());
	query.bindValue(":profile", ui->profileEdit->toPlainText());
	query.bindValue(":ingredients", ui->ingredientsEdit->toPlainText());
	query.bindValue(":examples", ui->examplesEdit->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() << "EditProfileStyle" << 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() << "EditProfileStyle Saved";
	}
    }

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


void EditProfileStyle::on_deleteButton_clicked()
{
    QSqlQuery query;

    query.prepare("DELETE FROM profile_styles WHERE record = :recno");
    query.bindValue(":recno", this->recno);
    query.exec();
    if (query.lastError().isValid()) {
	qDebug() << "EditProfileStyle" << 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() << "EditProfileStyle Deleted" << this->recno;
    }

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


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


void EditProfileStyle::ogmin_changed()
{
    if (ui->ogminEdit->value() > (ui->ogmaxEdit->value() - 0.001))
	ui->ogmaxEdit->setValue(ui->ogminEdit->value() + 0.001);
    is_changed();
}


void EditProfileStyle::ogmax_changed()
{
    if (ui->ogmaxEdit->value() < (ui->ogminEdit->value() + 0.001))
        ui->ogminEdit->setValue(ui->ogmaxEdit->value() - 0.001);
    is_changed();
}


void EditProfileStyle::fgmin_changed()
{
    if (ui->fgminEdit->value() > (ui->fgmaxEdit->value() - 0.001))
        ui->fgmaxEdit->setValue(ui->fgminEdit->value() + 0.001);
    is_changed();
}


void EditProfileStyle::fgmax_changed()
{
    if (ui->fgmaxEdit->value() < (ui->fgminEdit->value() + 0.001))
        ui->fgminEdit->setValue(ui->fgmaxEdit->value() - 0.001);
    is_changed();
}


void EditProfileStyle::ibumin_changed()
{
    if (ui->ibuminEdit->value() > (ui->ibumaxEdit->value() - 1))
        ui->ibumaxEdit->setValue(ui->ibuminEdit->value() + 1);
    is_changed();
}


void EditProfileStyle::ibumax_changed()
{
    if (ui->ibumaxEdit->value() < (ui->ibuminEdit->value() + 1))
        ui->ibuminEdit->setValue(ui->ibumaxEdit->value() - 1);
    is_changed();
}


void EditProfileStyle::ebcmin_changed()
{
    if (ui->ebcminEdit->value() > (ui->ebcmaxEdit->value() - 1))
        ui->ebcmaxEdit->setValue(ui->ebcminEdit->value() + 1);
    is_changed();
}


void EditProfileStyle::ebcmax_changed()
{
    if (ui->ebcmaxEdit->value() < (ui->ebcminEdit->value() + 1))
        ui->ebcminEdit->setValue(ui->ebcmaxEdit->value() - 1);
    is_changed();
}


void EditProfileStyle::co2min_changed()
{
    if (ui->co2minEdit->value() > (ui->co2maxEdit->value() - 0.1))
        ui->co2maxEdit->setValue(ui->co2minEdit->value() + 0.1);
    is_changed();
}


void EditProfileStyle::co2max_changed()
{
    if (ui->co2maxEdit->value() < (ui->co2minEdit->value() + 0.1))
        ui->co2minEdit->setValue(ui->co2maxEdit->value() - 0.1);
    is_changed();
}


void EditProfileStyle::abvmin_changed()
{
    if (ui->abvminEdit->value() > (ui->abvmaxEdit->value() - 0.1))
        ui->abvmaxEdit->setValue(ui->abvminEdit->value() + 0.1);
    is_changed();
}


void EditProfileStyle::abvmax_changed()
{
    if (ui->abvmaxEdit->value() < (ui->abvminEdit->value() + 0.1))
        ui->abvminEdit->setValue(ui->abvmaxEdit->value() - 0.1);
    is_changed();
}


void EditProfileStyle::on_quitButton_clicked()
{
    if (this->textIsChanged) {
	int rc = QMessageBox::warning(this, tr("Style changed"), tr("This beerstyle 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