src/InventorySuppliers.cpp

Mon, 14 Feb 2022 20:58:07 +0100

author
Michiel Broek <mbroek@mbse.eu>
date
Mon, 14 Feb 2022 20:58:07 +0100
changeset 10
8aa2bd9ba9e8
parent 9
85656dc48131
child 11
c9cdc15d3caf
permissions
-rw-r--r--

Added the EditSupplier popup window. It is now ready to validate the form data.

/**
 * InventorySuppliers.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 "InventorySuppliers.h"
#include "EditSupplier.h"
#include "../ui/ui_InventorySuppliers.h"
#include "config.h"
#include "bmsapp.h"

#include <QDebug>
#include <QtSql>
#include <QtWidgets>
#include <QTableWidget>





InventorySuppliers::InventorySuppliers(QWidget *parent) : QDialog(parent), ui(new Ui::InventorySuppliers)
{
    qDebug() << Q_FUNC_INFO;

    ui->setupUi(this);
    InventorySuppliers::loadTable();

    setWindowTitle( QString("BMSapp - %1 - Inventory Suppliers").arg(VERSIONSTRING) );
}


void InventorySuppliers::loadTable(void)
{
    ui->tableSuppliers = new QTableWidget(ui->tableSuppliers);
    QSqlQuery query("SELECT * FROM inventory_suppliers ORDER BY name");
    const QStringList labels({tr("Record"), tr("Name"), tr("Address"), tr("City"), tr("Country"), tr("Phone"), tr("Edit")});

    ui->tableSuppliers->setColumnCount(7);
    ui->tableSuppliers->setColumnWidth(0, 50);	/* Record	*/
    ui->tableSuppliers->setColumnWidth(1, 250);	/* Name		*/
    ui->tableSuppliers->setColumnWidth(2, 250);	/* Address	*/
    ui->tableSuppliers->setColumnWidth(3, 200);	/* City		*/
    ui->tableSuppliers->setColumnWidth(4, 120);	/* Country	*/
    ui->tableSuppliers->setColumnWidth(5, 120);	/* Phone	*/
    ui->tableSuppliers->setColumnWidth(6, 90);	/* Edit button	*/
    ui->tableSuppliers->setRowCount(query.size());
    ui->tableSuppliers->setHorizontalHeaderLabels(labels);
    ui->tableSuppliers->verticalHeader()->hide();
    ui->tableSuppliers->setFixedSize(1280, 640);	/* Even if this is too large, it works */

    qDebug() << query.record().count() << query.size();
    // So far, so good.
    query.first();
    for (int ridx = 0 ; ridx < query.size() ; ridx++ ) {
	for (int cidx = 0 ; cidx < 4; cidx++) {
	    QTableWidgetItem* item = new QTableWidgetItem(query.value(cidx).toString());
	    ui->tableSuppliers->setItem(ridx, cidx, item );
	}
	QTableWidgetItem* item = new QTableWidgetItem(query.value(5).toString());
	ui->tableSuppliers->setItem(ridx, 4, item );
	item = new QTableWidgetItem(query.value(8).toString());
        ui->tableSuppliers->setItem(ridx, 5, 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->setAlignment(Qt::AlignCenter);
	pLayout->setContentsMargins(0, 0, 0, 0);
	pWidget->setLayout(pLayout);
	ui->tableSuppliers->setCellWidget(ridx, 6, pWidget);
	query.next();
    }

    setWindowTitle( QString("BMSapp - %1 - Inventory Suppliers").arg(VERSIONSTRING) );
}


InventorySuppliers::~InventorySuppliers()
{
    qDebug() << Q_FUNC_INFO;
    delete ui;
}


bool InventorySuppliers::edit(int recno)
{
    qDebug() << Q_FUNC_INFO << recno;

    EditSupplier dialog(recno, this);
    dialog.setModal(true);
    int rc = dialog.exec();	/* rc 0 == cancel, rc 1 == ok */

    qDebug() << Q_FUNC_INFO << recno << rc;
    return false;
}


void InventorySuppliers::on_editButton_clicked()
{
    QPushButton *pb = qobject_cast<QPushButton *>(QObject::sender());
    int recno = pb->objectName().toInt();
    qDebug() << Q_FUNC_INFO << recno;
    edit(recno);
}


void InventorySuppliers::on_insertButton_clicked()
{
    qDebug() << Q_FUNC_INFO;
    edit(-1);
}


void InventorySuppliers::on_quitButton_clicked()
{
    qDebug() << Q_FUNC_INFO;
    emit firstWindow();
}

mercurial