src/InventoryHops.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
child 380
8f5c03ed4321
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.

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


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

    gridLayout = new QGridLayout(this);
    gridLayout->setObjectName(QString::fromUtf8("gridLayout"));
    tableHops = new QTableWidget(this);
    tableHops->setObjectName(QString::fromUtf8("tableHops"));
    tableHops->setEnabled(true);
    QSizePolicy sizePolicy(QSizePolicy::Fixed, QSizePolicy::Expanding);
    sizePolicy.setHorizontalStretch(0);
    sizePolicy.setVerticalStretch(0);
    tableHops->setSizePolicy(sizePolicy);
    tableHops->setMinimumSize(QSize(1124, 0));
    gridLayout->addWidget(tableHops, 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(fromInventoryHops()));
    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 InventoryHops::refreshTable()
{
    QString w;

    qDebug() << "InventoryHops reload";

    QSqlQuery query("SELECT * FROM inventory_hops ORDER BY origin,name");
    const QStringList labels({tr("Origin"), tr("Name"), tr("Type"), tr("Form"), tr("Alpha"), tr("Beta"), tr("Cohumulone"), tr("HSI"), tr("Harvest"), tr("Stock"), tr("Edit")});

    /* origin supplier name type graintype color yield inventory Edit */
    this->tableHops->setColumnCount(11);
    this->tableHops->setColumnWidth(0, 130);	/* Origin	*/
    this->tableHops->setColumnWidth(1, 250);	/* Name		*/
    this->tableHops->setColumnWidth(2,  80);	/* Type		*/
    this->tableHops->setColumnWidth(3,  80);	/* Form		*/
    this->tableHops->setColumnWidth(4,  80);	/* Alpha	*/
    this->tableHops->setColumnWidth(5,  80);	/* Beta		*/
    this->tableHops->setColumnWidth(6,  80);	/* cohumulone	*/
    this->tableHops->setColumnWidth(7,  80);	/* HSI		*/
    this->tableHops->setColumnWidth(8,  80);	/* Harvest date	*/
    this->tableHops->setColumnWidth(9,  80);	/* Stock	*/
    this->tableHops->setColumnWidth(10, 80);	/* Edit button	*/
    this->tableHops->setRowCount(query.size());
    this->tableHops->setHorizontalHeaderLabels(labels);
    this->tableHops->verticalHeader()->hide();

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

    query.first();
    for (int ridx = 0 ; ridx < query.size() ; ridx++ ) {
	this->tableHops->setItem(ridx, 0, new QTableWidgetItem(query.value(12).toString()));	/* Origin */
	this->tableHops->setItem(ridx, 1, new QTableWidgetItem(query.value(1).toString()));	/* Name	*/
	this->tableHops->setItem(ridx, 2, new QTableWidgetItem(g_hop_types[query.value(9).toInt()]));	/* Type */
	this->tableHops->setItem(ridx, 3, new QTableWidgetItem(QCoreApplication::translate("HopForm", g_hop_forms[query.value(10).toInt()])));	/* Form */
	w = QString("%1 %").arg(query.value(2).toDouble(), 2, 'f', 1, '0' );	/* Alpha% */
	QTableWidgetItem *item = new QTableWidgetItem(w);
        item->setTextAlignment(Qt::AlignRight|Qt::AlignVCenter);
        this->tableHops->setItem(ridx, 4, item);
	w = QString("%1 %").arg(query.value(3).toDouble(), 2, 'f', 1, '0' );	/* Beta% */
        item = new QTableWidgetItem(w);
        item->setTextAlignment(Qt::AlignRight|Qt::AlignVCenter);
        this->tableHops->setItem(ridx, 5, item);

	w = QString("%1 %").arg(query.value(6).toDouble(), 2, 'f', 1, '0' );    /* Cohumulone */
        item = new QTableWidgetItem(w);
        item->setTextAlignment(Qt::AlignRight|Qt::AlignVCenter);
        this->tableHops->setItem(ridx, 6, item);

	w = QString("%1").arg(query.value(8).toDouble(), 2, 'f', 1, '0' );    /* HSI */
        item = new QTableWidgetItem(w);
        item->setTextAlignment(Qt::AlignRight|Qt::AlignVCenter);
        this->tableHops->setItem(ridx, 7, item);

	if (query.value("inventory").toDouble() > 0)
	    this->tableHops->setItem(ridx, 8, new QTableWidgetItem(query.value("production_date").toString()));
	else
	    this->tableHops->setItem(ridx, 8, new QTableWidgetItem(QString("")));

	w = QString("");
	if (query.value("inventory").toDouble() > 0) {
	    if (query.value("form").toInt() < HOP_FORMS_CO2EXTRACT) {
	    	if (query.value("inventory").toDouble() < 1.000) {
	      	    w = QString("%1 gr").arg(query.value("inventory").toDouble() * 1000.0, 2, 'f', 1, '0' );
	    	} else {
              	    w = QString("%1 kg").arg(query.value("inventory").toDouble(), 4, 'f', 3, '0' );
	    	}
	    } else {
            	if (query.value("inventory").toDouble() < 1.000) {
              	    w = QString("%1 mL").arg(query.value("inventory").toDouble() * 1000.0, 2, 'f', 1, '0' );
            	} else {
              	    w = QString("%1 L").arg(query.value("inventory").toDouble(), 4, 'f', 3, '0' );
            	}
	    }
        }
	item = new QTableWidgetItem(w);
	item->setTextAlignment(Qt::AlignRight|Qt::AlignVCenter);
	this->tableHops->setItem(ridx, 9, item);

	/* Add the Edit button */
	QWidget* 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()));
	QHBoxLayout* pLayout = new QHBoxLayout(pWidget);
	pLayout->addWidget(btn_edit);
	pLayout->setContentsMargins(5, 0, 5, 0);
	pWidget->setLayout(pLayout);
	this->tableHops->setCellWidget(ridx, 10, pWidget);
	query.next();
    }
    emit setStatus(QString(tr("Total items: %1")).arg(query.size()));
}


InventoryHops::~InventoryHops() {}


void InventoryHops::edit(int recno)
{
    EditHop 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 InventoryHops::on_editButton_clicked()
{
    QPushButton *pb = qobject_cast<QPushButton *>(QObject::sender());
    int recno = pb->objectName().toInt();
    edit(recno);
}


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


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

    QSqlQuery query("SELECT * FROM inventory_hops ORDER BY origin,name");
    const QStringList types({ "Bittering", "Aroma", "Both" });
    const QStringList forms({ "Pellet", "Plug", "Leaf", "Leaf", "Pellet" });
    /*                                                "Leaf Wet", "Cryo"       */
    /*  We use more hop forms then beerxml knows about, so we send known names */
    /*  instead of what we internally use.                                     */

    QString fileName = QFileDialog::getSaveFileName(this, tr("Save File"), QDir::homePath() + "/hops.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("HOPS");

    query.first();
    for (int i = 0 ; i < query.size() ; i++ ) {
        xmlWriter->writeStartElement("HOP");
        xmlWriter->writeTextElement("VERSION", "1");
        xmlWriter->writeTextElement("NAME", query.value(1).toString());
	if (query.value(11).toString().length())
	    xmlWriter->writeTextElement("NOTES", query.value(11).toString());
	xmlWriter->writeTextElement("ALWAYS_ON_STOCK", query.value(14).toInt() ? "TRUE":"FALSE");
	xmlWriter->writeTextElement("ALPHA", QString::number(query.value(2).toDouble(), 'f', 4));
	xmlWriter->writeTextElement("BETA", QString::number(query.value(3).toDouble(), 'f', 4));
	xmlWriter->writeTextElement("HUMULENE", QString::number(query.value(4).toDouble(), 'f', 4));
	xmlWriter->writeTextElement("CAROPHYLLENE", QString::number(query.value(5).toDouble(), 'f', 4));
	xmlWriter->writeTextElement("COHUMULONE", QString::number(query.value(6).toDouble(), 'f', 4));
	xmlWriter->writeTextElement("MYRCENE", QString::number(query.value(7).toDouble(), 'f', 4));
	xmlWriter->writeTextElement("TOTAL_OIL", QString::number(query.value(19).toDouble(), 'f', 4));
	xmlWriter->writeTextElement("HSI", QString::number(query.value(8).toDouble(), 'f', 4));
	xmlWriter->writeTextElement("TYPE", types[query.value(9).toInt()]);
	xmlWriter->writeTextElement("FORM", forms[query.value(10).toInt()]);
	xmlWriter->writeTextElement("ORIGIN", query.value(12).toString());
	if (query.value(13).toString().length())
	    xmlWriter->writeTextElement("SUBSTITUTES", query.value(13).toString());
	if (query.value(16).toDouble() > 0)
	    xmlWriter->writeTextElement("COST", QString::number(query.value(16).toDouble(), 'f', 4));
	xmlWriter->writeEndElement();
        query.next();
    }

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

    file.close();
}

mercurial