src/InventoryYeasts.cpp

Fri, 29 Jul 2022 13:12:26 +0200

author
Michiel Broek <mbroek@mbse.eu>
date
Fri, 29 Jul 2022 13:12:26 +0200
changeset 373
b02aca4e926c
parent 301
fe6346211b5b
permissions
-rw-r--r--

First load of changes for hops. In EditHop load the dropdown buttons from the global table. Use named query fields. Added database utilisation and bu_factor fields for hop extracts. Added edit fields for these new fields. Added post boil SG, utilisation and bu_factor parameters to the toIBU function. Added hops form parameter to the hopFlavourContribution and hopAromaContribution display bars. In the hops inventory list dispay volumes instead of weight for hop extracts. Modified the TinsethIBU function to use utilisation and bu_factor parameters. Add calculations for co2 and iso hop extracts, this is work in progress. The toIBU function makes use of the preSG and postSG values to use the correct SG to caall the TinsethIBU function. This results in a bit lower IBU values mostly affecting the late additions. Added use hop at bottling for iso hop extracts like Tetra hops using the formula from BarthHaas.

/**
 * InventoryYeasts.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 "InventoryYeasts.h"
#include "EditYeast.h"
#include "MainWindow.h"
#include "config.h"
#include "global.h"


InventoryYeasts::InventoryYeasts(QWidget *parent) : QDialog(parent)
{
    qDebug() << "InventoryYeasts start";

    gridLayout = new QGridLayout(this);
    gridLayout->setObjectName(QString::fromUtf8("gridLayout"));
    tableYeasts = new QTableWidget(this);
    tableYeasts->setObjectName(QString::fromUtf8("tableYeasts"));
    tableYeasts->setEnabled(true);
    QSizePolicy sizePolicy(QSizePolicy::Fixed, QSizePolicy::Expanding);
    sizePolicy.setHorizontalStretch(0);
    sizePolicy.setVerticalStretch(0);
    tableYeasts->setSizePolicy(sizePolicy);
    tableYeasts->setMinimumSize(QSize(1094, 0));
    gridLayout->addWidget(tableYeasts, 0, 0, 1, 1);

    groupBox = new QGroupBox(this);
    groupBox->setObjectName(QString::fromUtf8("groupBox"));
    groupBox->setEnabled(true);
    groupBox->setFlat(false);
    horizontalLayout = new QHBoxLayout(groupBox);
    horizontalLayout->setSpacing(6);
    horizontalLayout->setObjectName(QString::fromUtf8("horizontalLayout"));
    horizontalLayout->setContentsMargins(0, 0, 0, 0);

    quitButton = new QPushButton(groupBox);
    quitButton->setObjectName(QString::fromUtf8("quitButton"));
    quitButton->setMinimumSize(QSize(80, 24));
    quitButton->setText(tr("Quit"));
    QIcon icon;
    icon.addFile(QString::fromUtf8(":icons/silk/door_out.png"), QSize(), QIcon::Normal, QIcon::Off);
    quitButton->setIcon(icon);
    horizontalLayout->addWidget(quitButton, 0, Qt::AlignLeft);

    exportButton = new QPushButton(groupBox);
    exportButton->setObjectName(QString::fromUtf8("exportButton"));
    exportButton->setMinimumSize(QSize(80, 24));
    exportButton->setText(tr("Export"));
    QIcon icon1;
    icon1.addFile(QString::fromUtf8(":/icons/silk/database_save.png"), QSize(), QIcon::Normal, QIcon::Off);
    exportButton->setIcon(icon1);
    horizontalLayout->addWidget(exportButton, 0, Qt::AlignCenter);

    insertButton = new QPushButton(groupBox);
    insertButton->setObjectName(QString::fromUtf8("insertButton"));
    insertButton->setMinimumSize(QSize(80, 24));
    insertButton->setText(tr("New"));
    QIcon icon3;
    icon3.addFile(QString::fromUtf8(":icons/silk/table_row_insert.png"), QSize(), QIcon::Normal, QIcon::Off);
    insertButton->setIcon(icon3);
    horizontalLayout->addWidget(insertButton, 0, Qt::AlignRight);
    gridLayout->addWidget(groupBox, 1, 0, 1, 1);

    connect(quitButton, SIGNAL(clicked()), parent, SLOT(fromInventoryYeasts()));
    connect(insertButton, SIGNAL(clicked()), this, SLOT(on_insertButton_clicked()));
    connect(exportButton, SIGNAL(clicked()), this, SLOT(on_exportButton_clicked()));
    connect(this, SIGNAL(setStatus(QString)), parent, SLOT(statusMsg(QString)));
    emit refreshTable();
}


void InventoryYeasts::refreshTable()
{
    QString w;
    QWidget* pWidget;
    QLabel *label;
    QHBoxLayout* pLayout;

    qDebug() << "InventoryYeasts reload";

    QSqlQuery query("SELECT * FROM inventory_yeasts ORDER BY laboratory,product_id,name");
    const QStringList labels({tr("Laboratory"), tr("Product id"), tr("Name"), tr("Type"), tr("Form"), tr("SVG %"), tr("Tol %"), tr("POF+"), tr("STA1"), tr("Stock"), tr("Edit")});

    /* origin supplier name type graintype color yield inventory Edit */
    this->tableYeasts->setColumnCount(11);
    this->tableYeasts->setColumnWidth(0, 120);	/* Laboratory	*/
    this->tableYeasts->setColumnWidth(1, 120);	/* Product ID	*/
    this->tableYeasts->setColumnWidth(2, 250);	/* Name		*/
    this->tableYeasts->setColumnWidth(3,  70);	/* Type		*/
    this->tableYeasts->setColumnWidth(4,  70);	/* Form		*/
    this->tableYeasts->setColumnWidth(5,  80);	/* SVG		*/
    this->tableYeasts->setColumnWidth(6,  80);	/* Tolerance	*/
    this->tableYeasts->setColumnWidth(7,  60);	/* POF+		*/
    this->tableYeasts->setColumnWidth(8,  60);	/* STA1		*/
    this->tableYeasts->setColumnWidth(9,  80);	/* Stock	*/
    this->tableYeasts->setColumnWidth(10, 80);	/* Edit button	*/
    this->tableYeasts->setRowCount(query.size());
    this->tableYeasts->setHorizontalHeaderLabels(labels);
    this->tableYeasts->verticalHeader()->hide();

    QTableWidgetItem *rightitem = new QTableWidgetItem();
    rightitem->setTextAlignment(Qt::AlignRight);

    query.first();
    for (int ridx = 0 ; ridx < query.size() ; ridx++ ) {
	this->tableYeasts->setItem(ridx, 0, new QTableWidgetItem(query.value(4).toString()));	/* Laboratory */
	this->tableYeasts->setItem(ridx, 1, new QTableWidgetItem(query.value(5).toString()));	/* Product id */
	this->tableYeasts->setItem(ridx, 2, new QTableWidgetItem(query.value(1).toString()));	/* Name	*/
	this->tableYeasts->setItem(ridx, 3, new QTableWidgetItem(QCoreApplication::translate("YeastType", g_yeast_types[query.value(2).toInt()])));/* Type */
	this->tableYeasts->setItem(ridx, 4, new QTableWidgetItem(QCoreApplication::translate("YeastForm", g_yeast_forms[query.value(3).toInt()])));/* Form */
	w = QString("%1 %").arg(query.value(9).toDouble(), 2, 'f', 1, '0' );	/* Attenuation% */
	QTableWidgetItem *item = new QTableWidgetItem(w);
        item->setTextAlignment(Qt::AlignRight|Qt::AlignVCenter);
        this->tableYeasts->setItem(ridx, 5, item);
	w = QString("");
	if (query.value(10).toDouble() > 0)
	    w = QString("%1 %").arg(query.value(10).toDouble(), 2, 'f', 1, '0' );	/* Alcohol% */
        item = new QTableWidgetItem(w);
        item->setTextAlignment(Qt::AlignRight|Qt::AlignVCenter);
        this->tableYeasts->setItem(ridx, 6, item);

	if (query.value(24).toInt()) {
	    pWidget = new QWidget();
	    label = new QLabel;
	    label->setPixmap(QPixmap(":icons/silk/tick.png"));
	    pLayout = new QHBoxLayout(pWidget);
            pLayout->addWidget(label);
	    pLayout->setAlignment(Qt::AlignCenter);
            pLayout->setContentsMargins(0, 0, 0, 0);
            pWidget->setLayout(pLayout);
	    this->tableYeasts->setCellWidget(ridx, 7, pWidget);
	} else {
	    this->tableYeasts->removeCellWidget(ridx, 7);
	}
        if (query.value(19).toInt()) {
            pWidget = new QWidget();
            label = new QLabel;
            label->setPixmap(QPixmap(":icons/silk/tick.png"));
            pLayout = new QHBoxLayout(pWidget);
            pLayout->addWidget(label);
            pLayout->setAlignment(Qt::AlignCenter);
            pLayout->setContentsMargins(0, 0, 0, 0);
            pWidget->setLayout(pLayout);
            this->tableYeasts->setCellWidget(ridx, 8, pWidget);
        } else {
	    this->tableYeasts->removeCellWidget(ridx, 8);
	}

	w = QString("");
	if (query.value(14).toDouble() > 0) {
	  if (query.value(3).toInt() == 0 && query.value(14).toDouble() > 1) {		/* Liquid yeast, multiple packs */
	    w = QString("%1 packs").arg(query.value(14).toDouble(), 1, 'f', 0, '0');
	  } else if (query.value(3).toInt() == 0) {					/* Liquid yeast */
	    w = QString("%1 pack").arg(query.value(14).toDouble(), 1, 'f', 0, '0');
	  } else if (query.value(3).toInt() == 1 || query.value(3).toInt() == 6) {	/* Dry or dried */
	    w = QString("%1 gram").arg(query.value(14).toDouble() * 1000, 2, 'f', 1, '0');
	  } else {									/* Any other form */
	    w = QString("%1 ml").arg(query.value(14).toDouble() * 1000, 2, 'f', 1, '0');
	  }
	}
	item = new QTableWidgetItem(w);
	item->setTextAlignment(Qt::AlignRight|Qt::AlignVCenter);
	this->tableYeasts->setItem(ridx, 9, item);

	/* Add the Edit button */
	pWidget = new QWidget();
	QPushButton* btn_edit = new QPushButton();
	btn_edit->setObjectName(QString("%1").arg(query.value(0).toString()));	/* Send record with the button */
	btn_edit->setText(tr("Edit"));
	connect(btn_edit, SIGNAL(clicked()), this, SLOT(on_editButton_clicked()));
	pLayout = new QHBoxLayout(pWidget);
	pLayout->addWidget(btn_edit);
	pLayout->setContentsMargins(5, 0, 5, 0);
	pWidget->setLayout(pLayout);
	this->tableYeasts->setCellWidget(ridx, 10, pWidget);
	query.next();
    }
    emit setStatus(QString(tr("Total items: %1")).arg(query.size()));
}


InventoryYeasts::~InventoryYeasts() {}


void InventoryYeasts::edit(int recno)
{
    EditYeast dialog(recno, this);
    /* Signal from editor if a refresh is needed */
    connect(&dialog, SIGNAL(entry_changed()), this, SLOT(refreshTable()));
    dialog.setModal(true);
    dialog.exec();
}


void InventoryYeasts::on_editButton_clicked()
{
    QPushButton *pb = qobject_cast<QPushButton *>(QObject::sender());
    int recno = pb->objectName().toInt();
    edit(recno);
}


void InventoryYeasts::on_insertButton_clicked()
{
    edit(-1);
}


void InventoryYeasts::on_exportButton_clicked()
{
    qDebug() << Q_FUNC_INFO;

    QSqlQuery query("SELECT * FROM inventory_yeasts ORDER BY laboratory,product_id,name");
    const QStringList types({ "Lager", "Ale", "Wheat", "Wine", "Champagne", "Other", "Other", "Other" });
    /*                        "Lager", "Ale", "Wheat", "Wine", "Champagne", "Brett", "Kveik", "Hybrid"  */
    /*  We use more yeat forms then beerxml knows about, so we send known names */
    /*  instead of what we internally use.                                      */
    const QStringList forms({ "Liquid", "Dry", "Slant", "Culture", "Culture", "Culture", "Dry" });
    /*                        "Liquid", "Dry", "Slant", "Culture", "Frozen",  "Bottle",  "Dried"  */
    const QStringList flocs({ "Low", "Medium", "High", "Very high" });

    QString fileName = QFileDialog::getSaveFileName(this, tr("Save File"), QDir::homePath() + "/yeasts.xml", tr("Files (*.xml)"));
    if (fileName == 0) {
        QMessageBox::warning(this, tr("Save File"), tr("No XML file selected."));
        return;
    }

    QFile file(fileName);
    file.open(QIODevice::WriteOnly);

    QXmlStreamWriter *xmlWriter = new QXmlStreamWriter(&file);
    xmlWriter->writeStartDocument();
    xmlWriter->setAutoFormatting(true);
    xmlWriter->setAutoFormattingIndent(1);
    xmlWriter->writeStartElement("YEASTS");

    query.first();
    for (int i = 0 ; i < query.size() ; i++ ) {
        xmlWriter->writeStartElement("YEAST");
        xmlWriter->writeTextElement("VERSION", "1");
        xmlWriter->writeTextElement("NAME", query.value(1).toString());
	xmlWriter->writeTextElement("TYPE", types[query.value(2).toInt()]);
	xmlWriter->writeTextElement("FORM", forms[query.value(3).toInt()]);
	xmlWriter->writeTextElement("AMOUNT_IS_WEIGHT", (query.value(3).toInt() == 1 || query.value(3).toInt() == 6) ? "TRUE":"FALSE");
	xmlWriter->writeTextElement("LABORATORY", query.value(4).toString());
	if (query.value(5).toString().length())
	    xmlWriter->writeTextElement("PRODUCT_ID", query.value(5).toString());
	xmlWriter->writeTextElement("MIN_TEMPERATURE", QString::number(query.value(6).toDouble(), 'f', 4));
	xmlWriter->writeTextElement("MAX_TEMPERATURE", QString::number(query.value(7).toDouble(), 'f', 4));
	xmlWriter->writeTextElement("ATTENUATION", QString::number(query.value(9).toDouble(), 'f', 4));
	xmlWriter->writeTextElement("ADD_TO_SECONDARY", "FALSE");
	xmlWriter->writeTextElement("FLOCCULATION", flocs[query.value(8).toInt()]);
	xmlWriter->writeTextElement("MAX_REUSE", QString::number(query.value(13).toInt()));
	if (query.value(15).toDouble() > 0)
	    xmlWriter->writeTextElement("COST", QString::number(query.value(15).toDouble(), 'f', 5));
	if (query.value(11).toString().length())
	    xmlWriter->writeTextElement("NOTES", query.value(11).toString());
	xmlWriter->writeEndElement();
        query.next();
    }

    xmlWriter->writeEndElement();
    xmlWriter->writeEndDocument();
    QMessageBox::information(this, tr("Save File"), tr("XML export ready"));

    file.close();
}

mercurial