src/InventoryEquipments.cpp

Wed, 06 Apr 2022 20:26:47 +0200

author
Michiel Broek <mbroek@mbse.eu>
date
Wed, 06 Apr 2022 20:26:47 +0200
changeset 110
224be4d9f8eb
parent 90
2396457a8167
child 275
f472f9773782
permissions
-rw-r--r--

Fermentables are loaded in the main recipe record in a QList. The refresh table function does the sorting, on added moment (mash first), amount and finally color of the malt. Removed the old hidden table columns. Do all manipulation on the recipe record with QList arrays. The ferment_amount_changed() slot is complete. The whole recipe editor is going in the right direction.

/**
 * InventoryEquipments.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 "InventoryEquipments.h"
#include "EditEquipment.h"
#include "MainWindow.h"
#include "config.h"


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

    gridLayout = new QGridLayout(this);
    gridLayout->setObjectName(QString::fromUtf8("gridLayout"));
    tableEquipments = new QTableWidget(this);
    tableEquipments->setObjectName(QString::fromUtf8("tableEquipments"));
    tableEquipments->setEnabled(true);
    QSizePolicy sizePolicy(QSizePolicy::Fixed, QSizePolicy::Expanding);
    sizePolicy.setHorizontalStretch(0);
    sizePolicy.setVerticalStretch(0);
    tableEquipments->setSizePolicy(sizePolicy);
    tableEquipments->setMinimumSize(QSize(1144, 0));
    gridLayout->addWidget(tableEquipments, 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::AlignRight);

    importButton = new QPushButton(groupBox);
    importButton->setObjectName(QString::fromUtf8("importButton"));
    importButton->setMinimumSize(QSize(80, 24));
    importButton->setText(tr("Import"));
    QIcon icon2;
    icon2.addFile(QString::fromUtf8(":/icons/silk/database_add.png"), QSize(), QIcon::Normal, QIcon::Off);
    importButton->setIcon(icon2);
    horizontalLayout->addWidget(importButton, 0, Qt::AlignRight);

    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(fromInventoryEquipments()));
    connect(insertButton, SIGNAL(clicked()), this, SLOT(on_insertButton_clicked()));
    connect(exportButton, SIGNAL(clicked()), this, SLOT(on_exportButton_clicked()));
//    connect(importButton, SIGNAL(clicked()), this, SLOT(on_importButton_clicked()));
    connect(this, SIGNAL(setStatus(QString)), parent, SLOT(statusMsg(QString)));
    emit refreshTable();
}


void InventoryEquipments::refreshTable()
{
    QString w;

    qDebug() << "InventoryEquipments reload";

    QSqlQuery query("SELECT * FROM inventory_equipments ORDER BY name");
    const QStringList labels({tr("Name"), tr("Boil volume"), tr("Batch size"), tr("Notes"), tr("Edit")});

    this->tableEquipments->setColumnCount(5);
    this->tableEquipments->setColumnWidth(0, 180);	/* Name		*/
    this->tableEquipments->setColumnWidth(1,  90);	/* Boil volume	*/
    this->tableEquipments->setColumnWidth(2,  90);	/* Batch size	*/
    this->tableEquipments->setColumnWidth(3, 680);	/* Notes	*/
    this->tableEquipments->setColumnWidth(4,  80);	/* Edit button	*/
    this->tableEquipments->setRowCount(query.size());
    this->tableEquipments->setHorizontalHeaderLabels(labels);
    this->tableEquipments->verticalHeader()->hide();

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

    query.first();
    for (int ridx = 0 ; ridx < query.size() ; ridx++ ) {
	this->tableEquipments->setItem(ridx, 0, new QTableWidgetItem(query.value(1).toString()));	/* Name */
	w = QString("%1 L").arg(query.value(2).toDouble(), 2, 'f', 1, '0' );			/* Boil volume */
	QTableWidgetItem *item = new QTableWidgetItem(w);
        item->setTextAlignment(Qt::AlignRight|Qt::AlignVCenter);
        this->tableEquipments->setItem(ridx, 1, item);
	w = QString("%1 L").arg(query.value(3).toDouble(), 2, 'f', 1, '0' );			/* Batch size */
        item = new QTableWidgetItem(w);
        item->setTextAlignment(Qt::AlignRight|Qt::AlignVCenter);
        this->tableEquipments->setItem(ridx, 2, item);
	this->tableEquipments->setItem(ridx, 3, new QTableWidgetItem(query.value(16).toString()));	/* Notes */

	/* 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->tableEquipments->setCellWidget(ridx, 4, pWidget);
	query.next();
    }
    emit setStatus(QString(tr("Total items: %1")).arg(query.size()));
}


InventoryEquipments::~InventoryEquipments() {}


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


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


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

    QSqlQuery query("SELECT * FROM inventory_equipments ORDER BY name");

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

    query.first();
    for (int i = 0 ; i < query.size() ; i++ ) {
        xmlWriter->writeStartElement("EQUIPMENT");
        xmlWriter->writeTextElement("VERSION", "1");
        xmlWriter->writeTextElement("NAME", query.value(1).toString());
        xmlWriter->writeTextElement("NOTES", query.value(16).toString());
        xmlWriter->writeTextElement("BOIL_SIZE", QString::number(query.value(2).toDouble(), 'f', 4));
        xmlWriter->writeTextElement("BATCH_SIZE", QString::number(query.value(3).toDouble(), 'f', 4));
        xmlWriter->writeTextElement("TUN_VOLUME", QString::number(query.value(4).toDouble(), 'f', 5));
        xmlWriter->writeTextElement("TUN_WEIGHT", QString::number(query.value(5).toDouble(), 'f', 5));
        xmlWriter->writeTextElement("TUN_SPECIFIC_HEAT", QString::number(query.value(6).toDouble(), 'f', 5));
        xmlWriter->writeTextElement("TUN_HEIGHT", QString::number(query.value(8).toDouble(), 'f', 5));
        xmlWriter->writeTextElement("TRUB_CHILLER_LOSS", QString::number(query.value(10).toDouble(), 'f', 5));
        xmlWriter->writeTextElement("EVAP_RATE", QString::number(query.value(11).toDouble(), 'f', 5));
	xmlWriter->writeTextElement("BOIL_TIME", QString::number(query.value(12).toDouble(), 'f', 5));
	xmlWriter->writeTextElement("CALC_BOIL_VOLUME", query.value(13).toInt() ? "TRUE":"FALSE");
	xmlWriter->writeTextElement("TOP_UP_KETTLE", QString::number(query.value(14).toDouble(), 'f', 5));
	xmlWriter->writeTextElement("HOP_UTILIZATION", QString::number(query.value(15).toDouble(), 'f', 5));
	xmlWriter->writeTextElement("LAUTER_VOLUME", QString::number(query.value(17).toDouble(), 'f', 5));
	xmlWriter->writeTextElement("LAUTER_HEIGHT", QString::number(query.value(18).toDouble(), 'f', 5));
	xmlWriter->writeTextElement("LAUTER_DEADSPACE", QString::number(query.value(19).toDouble(), 'f', 5));
	xmlWriter->writeTextElement("KETTLE_VOLUME", QString::number(query.value(20).toDouble(), 'f', 5));
	xmlWriter->writeTextElement("KETTLE_HEIGHT", QString::number(query.value(21).toDouble(), 'f', 5));
	xmlWriter->writeTextElement("MASH_VOLUME", QString::number(query.value(22).toDouble(), 'f', 5));
	xmlWriter->writeTextElement("EFFICIENCY", QString::number(query.value(24).toDouble(), 'f', 5));
        xmlWriter->writeEndElement();
        query.next();
    }

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

    file.close();
}

mercurial