src/ProfileMashs.cpp

Thu, 18 Aug 2022 20:34:15 +0200

author
Michiel Broek <mbroek@mbse.eu>
date
Thu, 18 Aug 2022 20:34:15 +0200
changeset 401
583148eb6e01
parent 90
2396457a8167
permissions
-rw-r--r--

Init est_carb field for new products.

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


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

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

    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(fromProfileMashs()));
    connect(insertButton, SIGNAL(clicked()), this, SLOT(on_insertButton_clicked()));
    connect(this, SIGNAL(setStatus(QString)), parent, SLOT(statusMsg(QString)));
    emit refreshTable();
}


void ProfileMashs::refreshTable()
{
    QString w;
    QWidget* pWidget;
    QHBoxLayout* pLayout;

    qDebug() << "ProfileMashs reload";

    QSqlQuery query("SELECT * FROM profile_mash ORDER BY name");
    const QStringList labels({tr("Name"), tr("Notes"), tr("Steps"), tr("Edit")});

    this->tableMashs->setColumnCount(4);
    this->tableMashs->setColumnWidth(0, 250);	/* Name		*/
    this->tableMashs->setColumnWidth(1, 675);	/* Notes	*/
    this->tableMashs->setColumnWidth(2,  75);	/* Steps	*/
    this->tableMashs->setColumnWidth(3,  80);	/* Edit button	*/
    this->tableMashs->setRowCount(query.size());
    this->tableMashs->setHorizontalHeaderLabels(labels);
    this->tableMashs->verticalHeader()->hide();

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

    query.first();
    for (int ridx = 0 ; ridx < query.size() ; ridx++ ) {
	this->tableMashs->setItem(ridx, 0, new QTableWidgetItem(query.value(1).toString()));	/* Name */
	this->tableMashs->setItem(ridx, 1, new QTableWidgetItem(query.value(2).toString()));	/* Notes */

	QJsonParseError parseError;
	const auto& json = query.value(3).toString();

	if (!json.trimmed().isEmpty()) {
	    const auto& formattedJson = QString("%1").arg(json);
            const auto& doc = QJsonDocument::fromJson(formattedJson.toUtf8(),  &parseError);

	    if (parseError.error != QJsonParseError::NoError)
    		qDebug() << "Parse error: " << parseError.errorString() << "at" << parseError.offset ;

	    w = QString("%1").arg(doc.array().size());
	    QTableWidgetItem *item = new QTableWidgetItem(w);
	    item->setTextAlignment(Qt::AlignCenter|Qt::AlignVCenter);
	    this->tableMashs->setItem(ridx, 2, 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->tableMashs->setCellWidget(ridx, 3, pWidget);
	query.next();
    }
    emit setStatus(QString(tr("Total items: %1")).arg(query.size()));
}


ProfileMashs::~ProfileMashs() {}


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


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

mercurial