src/ProfileMashs.cpp

Mon, 07 Mar 2022 17:33:22 +0100

author
Michiel Broek <mbroek@mbse.eu>
date
Mon, 07 Mar 2022 17:33:22 +0100
changeset 49
29cf6e350063
child 75
5f953abbd73c
permissions
-rw-r--r--

Added Mash profiles table and the first part of the Mash profile editor. Edit and write must be written.

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


ProfileMashs::ProfileMashs(QWidget *parent) : QDialog(parent), ui(new Ui::ProfileMashs)
{
    qDebug() << "ProfileMashs start";

    ui->setupUi(this);
    emit refreshTable();

    setWindowTitle( QString("BMSapp - %1 - Profile Mashs").arg(VERSIONSTRING) );
}


void ProfileMashs::refreshTable()
{
    QString w;
    QWidget* pWidget;
    QLabel *label;
    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")});

    ui->tableMashs->setColumnCount(4);
    ui->tableMashs->setColumnWidth(0, 250);	/* Name		*/
    ui->tableMashs->setColumnWidth(1, 675);	/* Notes	*/
    ui->tableMashs->setColumnWidth(2,  75);	/* Steps	*/
    ui->tableMashs->setColumnWidth(3,  80);	/* Edit button	*/
    ui->tableMashs->setRowCount(query.size());
    ui->tableMashs->setHorizontalHeaderLabels(labels);
    ui->tableMashs->verticalHeader()->hide();
    ui->tableMashs->setFixedSize(1080 + 24, 640);	/* Even if this is too large, it works */

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

    query.first();
    for (int ridx = 0 ; ridx < query.size() ; ridx++ ) {
	ui->tableMashs->setItem(ridx, 0, new QTableWidgetItem(query.value(1).toString()));	/* Name */
	ui->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 ;

//	    qDebug() << " ** " << doc << doc.isArray() << doc.array().size() ;

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

    setWindowTitle( QString("BMSapp - %1 - Profile Mashs").arg(VERSIONSTRING) );
}


ProfileMashs::~ProfileMashs()
{
    qDebug() << "ProfileMashs done";
    delete ui;
}


void ProfileMashs::edit(int recno)
{
    qDebug() << "ProfileMashs edit:" << 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();
    qDebug() << Q_FUNC_INFO << recno;
    edit(recno);
}


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


void ProfileMashs::on_quitButton_clicked()
{
    emit firstWindow();
}

mercurial