src/CalibrateiSpindel.cpp

Sat, 14 Oct 2023 11:23:24 +0200

author
Michiel Broek <mbroek@mbse.eu>
date
Sat, 14 Oct 2023 11:23:24 +0200
changeset 505
7ae4d022cf8f
parent 504
ea7cf64c9a6c
child 506
ea07f6c97a69
permissions
-rw-r--r--

Load and show current calibration data.

/**
 * CalibrateiSpindel.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 "CalibrateiSpindel.h"
#include "../ui/ui_CalibrateiSpindel.h"
#include "global.h"
#include "Utils.h"
#include "MainWindow.h"



CalibrateiSpindel::CalibrateiSpindel(int id, QWidget *parent) : QDialog(parent), ui(new Ui::CalibrateiSpindel)
{
    QSqlQuery query;

#ifdef DEBUG_MONITOR
    qDebug() << "CalibrateiSpindel record:" << id;
#endif

    ui->setupUi(this);
    this->recno = id;
    setWindowFlags(Qt::Window | Qt::WindowTitleHint | Qt::CustomizeWindowHint);
    WindowTitle();

    query.prepare("SELECT node,alias,calibrate FROM mon_ispindels WHERE record = :recno");
    query.bindValue(":recno", this->recno);
    query.exec();
    if (query.next()) {

	_node = query.value("node").toString();
	_alias = query.value("alias").toString();
	ui->nameEdit->setText(_node+"/"+_alias);

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

	if (!json.trimmed().isEmpty()) {
	    const auto& formattedJson = QString("%1").arg(json);
	    QJsonDocument jsonResponse = QJsonDocument::fromJson(formattedJson.toUtf8(),  &parseError);
	    if (parseError.error != QJsonParseError::NoError) {
                qWarning() << "Parse error: " << parseError.errorString() << "at" << parseError.offset ;
            } else {

	    	QJsonObject jsonObj = jsonResponse.object();
//	    	qDebug() << "polyData: " << jsonObj["polyData"].toArray();
		QJsonArray polyData = jsonObj.value("polyData").toArray();
//		qDebug() << polyData;
		for (int i = 0; i < polyData.size(); i++) {
		    Old[i] = New[i] = polyData.at(i).toDouble();
		    qDebug() << i << New[i];
		}
		_data_old = QString("(%1 * x^3) + (%2 * x^2) + (%3 * x) + %4").arg(Old[0], 0, 'f', 9, '0').arg(Old[1], 0, 'f', 9, '0').arg(Old[2], 0, 'f', 9, '0').arg(Old[3], 0, 'f', 9, '0');
		ui->oldEdit->setText(_data_old);

	    	qDebug() << "calData:  " << jsonObj["calData"].toArray();
		QJsonArray calData = jsonObj.value("calData").toArray();
		qDebug() << calData;
		totaldata = 0;
		for (int i = 0; i < calData.size(); i++) {
		    QJsonObject calObj = calData.at(i).toObject();
		    oCal[i].plato = nCal[i].plato = calObj["plato"].toDouble();
		    oCal[i].angle = nCal[i].angle = calObj["angle"].toDouble();
		    oCal[i].sg = nCal[i].sg = Utils::plato_to_sg(oCal[i].plato);
		    totaldata++;
		}

	    }
	}

    }

    connect(ui->dataTable, SIGNAL(cellChanged(int, int)), this, SLOT(cell_Changed(int, int)));
//    connect(parent, SIGNAL(updateiSpindel(QString)), this, SLOT(refreshiSpindel(QString)));
    emit refreshTable();
}


void CalibrateiSpindel::refreshTable()
{
    QString w;
    QWidget* pWidget;
    QHBoxLayout* pLayout;
    double  d;

    qDebug() << "refreshTable" << totaldata;

    /*
     * During filling the table turn off the cellChanged signal because every cell that is filled
     * triggers the cellChanged signal. The QTableWidget has no better signal to use.
     */
    this->ignoreChanges = true;

    const QStringList labels({tr("SG"), tr("°Plato"), tr("Angle"), tr("Del")});
    ui->dataTable->setColumnCount(4);
    ui->dataTable->setColumnWidth(0, 100);	/* SG		*/
    ui->dataTable->setColumnWidth(1, 100);	/* °Plato	*/
    ui->dataTable->setColumnWidth(2, 100);	/* Tilt angle	*/
    ui->dataTable->setColumnWidth(3,  55);	/* Del button	*/
    ui->dataTable->setHorizontalHeaderLabels(labels);
    ui->dataTable->verticalHeader()->hide();
    ui->dataTable->setRowCount(totaldata);

    for (int i = 0; i < totaldata; i++) {
	qDebug() << i << nCal[i].sg << nCal[i].plato << nCal[i].angle;

	w = QString("%1").arg(nCal[i].sg, 1, 'f', 4, '0');
	QTableWidgetItem *item = new QTableWidgetItem(w);
	item->setTextAlignment(Qt::AlignCenter|Qt::AlignVCenter);
	ui->dataTable->setItem(i, 0, item);

	w = QString("%1").arg(nCal[i].plato, 1, 'f', 3, '0');
        item = new QTableWidgetItem(w);
        item->setTextAlignment(Qt::AlignCenter|Qt::AlignVCenter);
        ui->dataTable->setItem(i, 1, item);

	w = QString("%1").arg(nCal[i].angle, 1, 'f', 5, '0');
        item = new QTableWidgetItem(w);
        item->setTextAlignment(Qt::AlignCenter|Qt::AlignVCenter);
        ui->dataTable->setItem(i, 2, item);

	/* Add the Delete row button */
        pWidget = new QWidget();
        QPushButton* btn_del = new QPushButton();
        btn_del->setObjectName(QString("%1").arg(i));  /* Send row with the button */
        btn_del->setText(tr("Del"));
        connect(btn_del, SIGNAL(clicked()), this, SLOT(on_deleteRow_clicked()));
        pLayout = new QHBoxLayout(pWidget);
        pLayout->addWidget(btn_del);
        pLayout->setContentsMargins(5, 0, 5, 0);
        pWidget->setLayout(pLayout);
        ui->dataTable->setCellWidget(i, 3, pWidget);
    }

    this->ignoreChanges = false;
}


CalibrateiSpindel::~CalibrateiSpindel()
{
    delete ui;
}


void CalibrateiSpindel::on_quitButton_clicked()
{
    this->close();
    this->setResult(1);
}


void CalibrateiSpindel::on_saveButton_clicked()
{
}


void CalibrateiSpindel::on_deleteRow_clicked()
{
    QPushButton *pb = qobject_cast<QPushButton *>(QObject::sender());
    int row = pb->objectName().toInt();
    qDebug() << "Delete row" << row;
}


void CalibrateiSpindel::on_addButton_clicked()
{
    qDebug() << "Add row" << totaldata;
}


void CalibrateiSpindel::cell_Changed(int nRow, int nCol)
{
    QString w;

    if (this->ignoreChanges)
        return;

    qDebug() << "Cell at row " + QString::number(nRow) + " column " + QString::number(nCol) + " was changed.";

}


/*
 * Window header, mark any change with '**'
 */
void CalibrateiSpindel::WindowTitle()
{
    QString txt;

    txt = QString(tr("BMSapp - Calibrate iSpindel %1").arg(this->recno));

    if (this->textIsChanged) {
        txt.append((QString(" **")));
    }
    setWindowTitle(txt);
}

mercurial