src/MoniSpindels.cpp

Sat, 11 Feb 2023 15:48:02 +0100

author
Michiel Broek <mbroek@mbse.eu>
date
Sat, 11 Feb 2023 15:48:02 +0100
changeset 493
520306773450
parent 492
c3a781b4d35b
child 503
61c114afb0ee
permissions
-rw-r--r--

Monitor iSpindels: use global variables instead of repeated expensive MySQL calls. Use the yeast temperature ranges for the colors on the thermometer scale. Don't show SVG and ABV if the OG is not yet known. Turn statusfield red if offline. Extra mon_ispindles fields yeast_lo and yeast_hi. Need at least bmsd version 0.3.42. If a websocket message is received that cannot be parsed, show the whole received message.

/**
 * MoniSpindels.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 "MoniSpindels.h"
#include "DetailiSpindel.h"
#include "MainWindow.h"
#include "Utils.h"
#include "config.h"



/*
 * Build the table and buttons on the mainscreen.
 * Don't use a ui file, do it dynamicly.
 */
MoniSpindels::MoniSpindels(QWidget *parent) : QDialog(parent)
{
#ifdef DEBUG_MONITOR
    qDebug() << "MoniSpindels start";
#endif

    gridLayout = new QGridLayout(this);
    gridLayout->setObjectName(QString::fromUtf8("gridLayout"));
    tableiSpindels = new QTableWidget(this);
    tableiSpindels->setObjectName(QString::fromUtf8("tableiSpindels"));
    tableiSpindels->setEnabled(true);
    QSizePolicy sizePolicy(QSizePolicy::Fixed, QSizePolicy::Expanding);
    sizePolicy.setHorizontalStretch(0);
    sizePolicy.setVerticalStretch(0);
    sizePolicy.setHeightForWidth(tableiSpindels->sizePolicy().hasHeightForWidth());
    tableiSpindels->setSizePolicy(sizePolicy);
    tableiSpindels->setMinimumSize(QSize(1054, 0));
    gridLayout->addWidget(tableiSpindels, 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(fromMoniSpindels()));
    connect(this, SIGNAL(setStatus(QString)), parent, SLOT(statusMsg(QString)));
    connect(parent, SIGNAL(updateiSpindels(QString)), this, SLOT(refreshiSpindels(QString)));
    emit refreshTable();
}


void MoniSpindels::refreshTable()
{
    QTableWidgetItem *item;

    QSqlQuery query("SELECT record,alias,node,online,mode,temperature,gravity,beercode,beername FROM mon_ispindels ORDER BY alias");
    const QStringList labels({tr("Unit"), tr("Node"), tr("Status"), tr("Beer"), tr("Temperature"), tr("SG"), tr("Details")});

    this->tableiSpindels->setColumnCount(7);
    this->tableiSpindels->setColumnWidth(0, 150);	/* Alias	*/
    this->tableiSpindels->setColumnWidth(1, 120);	/* Node		*/
    this->tableiSpindels->setColumnWidth(2, 100);	/* Status	*/
    this->tableiSpindels->setColumnWidth(3, 390);	/* Beer		*/
    this->tableiSpindels->setColumnWidth(4,  90);	/* Temperature	*/
    this->tableiSpindels->setColumnWidth(5,  90);	/* Gravity	*/
    this->tableiSpindels->setColumnWidth(6,  90);	/* Edit button	*/
    this->tableiSpindels->setRowCount(query.size());
    this->tableiSpindels->setHorizontalHeaderLabels(labels);
    this->tableiSpindels->verticalHeader()->hide();
    /* Set the widget size to 1054 x 575 in the ui. */

    query.first();
    for (int i = 0 ; i < query.size() ; i++ ) {
	this->tableiSpindels->setItem(i, 0, new QTableWidgetItem(query.value("alias").toString()));
	this->tableiSpindels->setItem(i, 1, new QTableWidgetItem(query.value("node").toString()));

	if (query.value("online").toInt()) {
            item = new QTableWidgetItem(QString("Ok"));
        } else {
            item = new QTableWidgetItem(QString("Offline"));
            item->setForeground(QBrush(QColor(Qt::red)));
        }
        item->setTextAlignment(Qt::AlignCenter|Qt::AlignVCenter);
        this->tableiSpindels->setItem(i, 2, item);

	if (query.value("mode").toString() == "ON") {
	    item = new QTableWidgetItem(query.value("beercode").toString()+" - "+query.value("beername").toString());
	    this->tableiSpindels->setItem(i, 3, item);
	} else {
	    this->tableiSpindels->setItem(i, 3, new QTableWidgetItem(QString("")));
	}

	if (query.value("online").toInt()) {
	    item = new QTableWidgetItem(QString("%1°C").arg(query.value("temperature").toDouble(), 4, 'f', 3, '0'));
	    item->setTextAlignment(Qt::AlignCenter|Qt::AlignVCenter);
	    this->tableiSpindels->setItem(i, 4, item);
	    double sg = Utils::plato_to_sg(query.value("gravity").toDouble());
	    item = new QTableWidgetItem(QString("%1").arg(sg, 5, 'f', 4, '0'));
	    item->setTextAlignment(Qt::AlignCenter|Qt::AlignVCenter);
	    this->tableiSpindels->setItem(i, 5, item);
	} else {
	    this->tableiSpindels->setItem(i, 4, new QTableWidgetItem(QString("")));
            this->tableiSpindels->setItem(i, 5, new QTableWidgetItem(QString("")));
	}

	/* Add the Edit button */
	QWidget* pWidget = new QWidget();
	QPushButton* btn_edit = new QPushButton();
	btn_edit->setObjectName(QString("%1").arg(query.value("record").toString()));	/* Send record with the button */
	btn_edit->setText(tr("Details"));
	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->tableiSpindels->setCellWidget(i, 6, pWidget);
	query.next();
    }
    emit setStatus(QString(tr("Total items: %1")).arg(query.size()));
}


MoniSpindels::~MoniSpindels() {}


void MoniSpindels::refreshiSpindels(QString data)
{
    emit refreshTable();
    emit updateiSpindel(data);
}


void MoniSpindels::edit(int recno)
{
    DetailiSpindel dialog(recno, this);
    dialog.setModal(true);
    dialog.exec();
}


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

mercurial