src/InventoryMiscs.cpp

Sat, 08 Jun 2024 15:54:30 +0200

author
Michiel Broek <mbroek@mbse.eu>
date
Sat, 08 Jun 2024 15:54:30 +0200
changeset 527
84091b9cb800
parent 301
fe6346211b5b
permissions
-rw-r--r--

Version 0.4.6a1. Added HLT equipment volume and deadspace settings. In EditProduct the target water selection is now sticky. Changed the water treatment tab. Added a row wich displays the salt adjustments. This can be selected between actual and target values. The treated water show can select between mash or sparge water. The total line will become the final water in the boil kettle. Database update function is expanded with the new settings. Added a popup message warning that the database is upgraded and user action is required for the equipment profiles.

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


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

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

    qDebug() << "InventoryMiscs reload";

    QSqlQuery query("SELECT * FROM inventory_miscs ORDER BY name");
    const QStringList labels({tr("Name"), tr("Type"), tr("Use"), tr("Time"), tr("Stock"), tr("Edit")});

    /* origin supplier name type graintype color yield inventory Edit */
    this->tableMiscs->setColumnCount(6);
    this->tableMiscs->setColumnWidth(0, 275);	/* Name		*/
    this->tableMiscs->setColumnWidth(1, 120);	/* Type		*/
    this->tableMiscs->setColumnWidth(2, 120);	/* Use 		*/
    this->tableMiscs->setColumnWidth(3, 120);	/* Time		*/
    this->tableMiscs->setColumnWidth(4,  80);	/* Stock	*/
    this->tableMiscs->setColumnWidth(5,  80);	/* Edit button	*/
    this->tableMiscs->setRowCount(query.size());
    this->tableMiscs->setHorizontalHeaderLabels(labels);
    this->tableMiscs->verticalHeader()->hide();

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

    query.first();
    for (int ridx = 0 ; ridx < query.size() ; ridx++ ) {
	this->tableMiscs->setItem(ridx, 0, new QTableWidgetItem(query.value(1).toString()));	/* Name */
	this->tableMiscs->setItem(ridx, 1, new QTableWidgetItem(QCoreApplication::translate("MiscType", g_misc_types[query.value(2).toInt()])));/* Type */
	this->tableMiscs->setItem(ridx, 2, new QTableWidgetItem(QCoreApplication::translate("MiscUse", g_misc_uses[query.value(3).toInt()])));	/* Use */

	w = QString("");	/* Use time */
	if (query.value(4).toInt() > 0) {
	  if (query.value(4).toInt() == 1)
	    w = QString(tr("1 minute"));
	  else if (query.value(4).toInt() < 180)
	    w = QString(tr("%1 minutes")).arg(query.value(4).toInt());
	  else if (query.value(4).toInt() < 1440)
	    w = QString(tr("%1 hours")).arg(query.value(4).toInt() / 60);
	  else if (query.value(4).toInt() == 1440)
	    w = QString(tr("1 day"));
	  else
	    w = QString(tr("%1 days")).arg(query.value(4).toInt() / 1440);
	}
	QTableWidgetItem *item = new QTableWidgetItem(w);
        item->setTextAlignment(Qt::AlignRight|Qt::AlignVCenter);
        this->tableMiscs->setItem(ridx, 3, item);

	w = QString("");
	if (query.value(9).toDouble() > 0) {
	  if (query.value(5).toInt()) { /* Amount is weight */
	    w = QString("%1 gr").arg(query.value(9).toDouble() * 1000.0, 2, 'f', 1, '0' );
	  } else {
            w = QString("%1 ml").arg(query.value(9).toDouble() * 1000.0, 2, 'f', 1, '0' );
	  }
	}
	item = new QTableWidgetItem(w);
	item->setTextAlignment(Qt::AlignRight|Qt::AlignVCenter);
	this->tableMiscs->setItem(ridx, 4, 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->tableMiscs->setCellWidget(ridx, 5, pWidget);
	query.next();
    }
    emit setStatus(QString(tr("Total items: %1")).arg(query.size()));
}


InventoryMiscs::~InventoryMiscs() {}


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


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


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

    QSqlQuery query("SELECT * FROM inventory_miscs ORDER BY name");
    const QStringList types({ "Spice", "Herb", "Flavor", "Fining", "Water agent", "Other",          "Other" });
    /*                        'Spice', 'Herb', 'Flavor', 'Fining', 'Water agent', 'Yeast nutrient', 'Other'  */
    /*  We use more misc types then beerxml knows about, so we send known names */
    /*  instead of what we internally use.                                      */
    const QStringList uses({ "Secondary", "Mash", "Boil", "Primary", "Secondary", "Bottling" });
    /*                         'Starter', 'Mash', 'Boil', 'Primary', 'Secondary', 'Bottling'  */
    /* Stupid beerxml leaves us no other choice then to map a starter ingredient to secondary */

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

    query.first();
    for (int i = 0 ; i < query.size() ; i++ ) {
        xmlWriter->writeStartElement("MISC");
        xmlWriter->writeTextElement("VERSION", "1");
        xmlWriter->writeTextElement("NAME", query.value(1).toString());
	xmlWriter->writeTextElement("TYPE", types[query.value(2).toInt()]);
	xmlWriter->writeTextElement("AMOUNT_IS_WEIGHT", query.value(5).toInt() ? "TRUE":"FALSE");
	xmlWriter->writeTextElement("USE", uses[query.value(3).toInt()]);
	if (query.value(4).toDouble() > 0)
	    xmlWriter->writeTextElement("TIME", QString::number(query.value(4).toDouble(), 'f', 3));
	if (query.value(10).toDouble() > 0)
	    xmlWriter->writeTextElement("COST", QString::number(query.value(10).toDouble(), 'f', 5));
	xmlWriter->writeTextElement("ALWAYS_ON_STOCK", query.value(8).toInt() ? "TRUE":"FALSE");
	if (query.value(7).toString().length())
	    xmlWriter->writeTextElement("NOTES", query.value(7).toString());
	if (query.value(6).toString().length())
	    xmlWriter->writeTextElement("USE_FOR", query.value(6).toString()); 

	xmlWriter->writeEndElement();
        query.next();
    }

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

    file.close();
}

mercurial