diff -r b68c0c61d261 -r 54b5abd46958 src/ProdOnCode.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/ProdOnCode.cpp Sat May 21 20:26:25 2022 +0200 @@ -0,0 +1,143 @@ +/** + * ProdOnCode.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 . + */ +#include "ProdOnCode.h" +#include "MainWindow.h" +#include "EditProduct.h" +#include "config.h" +#include "global.h" + + +ProdOnCode::ProdOnCode(QWidget *parent) : QDialog(parent) +{ + qDebug() << "ProdOnCode start"; + + gridLayout = new QGridLayout(this); + gridLayout->setObjectName(QString::fromUtf8("gridLayout")); + tableOnCode = new QTableWidget(this); + tableOnCode->setObjectName(QString::fromUtf8("tableOnCode")); + tableOnCode->setEnabled(true); + QSizePolicy sizePolicy(QSizePolicy::Fixed, QSizePolicy::Expanding); + sizePolicy.setHorizontalStretch(0); + sizePolicy.setVerticalStretch(0); + sizePolicy.setHeightForWidth(tableOnCode->sizePolicy().hasHeightForWidth()); + tableOnCode->setSizePolicy(sizePolicy); + tableOnCode->setMinimumSize(QSize(1164, 0)); + gridLayout->addWidget(tableOnCode, 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::AlignCenter); + gridLayout->addWidget(groupBox, 1, 0, 1, 1); + + connect(quitButton, SIGNAL(clicked()), parent, SLOT(fromProdOnCode())); + connect(this, SIGNAL(setStatus(QString)), parent, SLOT(statusMsg(QString))); + emit refreshTable(); +} + + +void ProdOnCode::refreshTable() +{ + QString w; + QWidget* pWidget; + QHBoxLayout* pLayout; + + qDebug() << "ProdOnCode reload"; + QSqlQuery query("SELECT record,name,st_name,og,fg,brew_date_start,code FROM products WHERE stage = '11' ORDER BY code,brew_date_start"); + const QStringList labels({tr("Code"), tr("Name"), tr("Style"), tr("OG"), tr("FG"), tr("Date"), tr("Edit")}); + + this->tableOnCode->setColumnCount(7); + this->tableOnCode->setColumnWidth(0, 100); /* Code */ + this->tableOnCode->setColumnWidth(1, 500); /* Product name */ + this->tableOnCode->setColumnWidth(2, 200); /* Style */ + this->tableOnCode->setColumnWidth(3, 75); /* OG */ + this->tableOnCode->setColumnWidth(4, 75); /* FG */ + this->tableOnCode->setColumnWidth(5, 100); /* Date */ + this->tableOnCode->setColumnWidth(6, 90); /* Edit button */ + this->tableOnCode->setRowCount(query.size()); + this->tableOnCode->setHorizontalHeaderLabels(labels); + this->tableOnCode->verticalHeader()->hide(); + + query.first(); + for (int ridx = 0 ; ridx < query.size() ; ridx++ ) { + + this->tableOnCode->setItem(ridx, 0, new QTableWidgetItem(query.value("code").toString())); + this->tableOnCode->setItem(ridx, 1, new QTableWidgetItem(query.value("name").toString())); + this->tableOnCode->setItem(ridx, 2, new QTableWidgetItem(query.value("st_name").toString())); + QTableWidgetItem *item = new QTableWidgetItem(QString("%1").arg(query.value("og").toFloat(), 4, 'f', 3, '0' )); + item->setTextAlignment(Qt::AlignCenter|Qt::AlignVCenter); + this->tableOnCode->setItem(ridx, 3, item); + + item = new QTableWidgetItem(QString("%1").arg(query.value("fg").toFloat(), 4, 'f', 3, '0' )); + item->setTextAlignment(Qt::AlignCenter|Qt::AlignVCenter); + this->tableOnCode->setItem(ridx, 4, item); + + this->tableOnCode->setItem(ridx, 5, new QTableWidgetItem(query.value("brew_date_start").toDate().toString("dd MMM yyyy"))); + + /* 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->tableOnCode->setCellWidget(ridx, 6, pWidget); + query.next(); + } + + emit setStatus(QString(tr("Total items: %1")).arg(query.size())); +} + + +ProdOnCode::~ProdOnCode() {} + + +void ProdOnCode::edit(int recno) +{ + EditProduct 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 ProdOnCode::on_editButton_clicked() +{ + QPushButton *pb = qobject_cast(QObject::sender()); + int recno = pb->objectName().toInt(); + edit(recno); +} + +