src/ChartCarbonate.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 497
cbd7644d99ca
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.

/**
 * ChartCarbonate.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 "ChartCarbonate.h"
#include "callout.h"
#include "MainWindow.h"


ChartCarbonate::ChartCarbonate(QString code, QString name, QWidget *parent) : QDialog(parent)
{
    QSqlQuery query;
    double timestamp;

    qDebug() << "ChartCarbonate:" << code << name;

    QDialog* dialog = new QDialog(parent);
    dialog->setWindowTitle(tr("BMSapp - Carbonation ") + "\"" + name + "\"");
    dialog->setObjectName(QString::fromUtf8("ChartCarbonate"));
    dialog->setWindowFlags(Qt::Window | Qt::WindowTitleHint | Qt::CustomizeWindowHint);
    dialog->resize(1024, 600);

    QPushButton *saveButton = new QPushButton(tr("Save"));
    saveButton->setAutoDefault(false);
    QIcon icon1;
    icon1.addFile(QString::fromUtf8(":icons/silk/disk.png"), QSize(), QIcon::Normal, QIcon::Off);
    saveButton->setIcon(icon1);

    QDialogButtonBox *buttonBox = new QDialogButtonBox(dialog);
    buttonBox->setObjectName(QString::fromUtf8("buttonBox"));
    buttonBox->setOrientation(Qt::Vertical);
    buttonBox->setStandardButtons(QDialogButtonBox::Ok);
    buttonBox->addButton(saveButton,QDialogButtonBox::ActionRole);

    temperature = new QSplineSeries();
    pressure = new QSplineSeries();

    query.prepare("SELECT * FROM log_co2pressure WHERE code=:code ORDER BY datetime");
    query.bindValue(":code", code);
    query.exec();
    while (query.next()) {
        timestamp = query.value("datetime").toDateTime().toSecsSinceEpoch() * 1000;
        temperature->append(timestamp, query.value("temperature").toDouble());
        pressure->append(timestamp, query.value("pressure").toDouble());
    }

    temperature->setName(tr("Temperature °C"));
    temperature->setColor(QColorConstants::Svg::red);
    pressure->setName(tr("Pressure bar"));
    QPen pen(QColorConstants::Svg::navy);
    pen.setWidth(2);
    pressure->setPen(pen);

    chart = new QChart();
    chart->setTitle(QString("%1 \"%2\"").arg(code).arg(name));
    chart->addSeries(temperature);
    chart->addSeries(pressure);

    QDateTimeAxis *axisX = new QDateTimeAxis;
    axisX->setTickCount(10);
    axisX->setFormat("dd MMM");
    axisX->setTitleText(tr("Date"));
    axisX->setLabelsFont(QFont("Helvetica", 8, QFont::Normal));
    chart->addAxis(axisX, Qt::AlignBottom);
    temperature->attachAxis(axisX);
    pressure->attachAxis(axisX);

    QValueAxis *axisYT = new QValueAxis;
    axisYT->setRange(14.0, 25.0);
    axisYT->setTickCount(12);
    axisYT->setLabelFormat("%.1f");
    axisYT->setTitleText(tr("Temperature °C"));
    axisYT->setLabelsFont(QFont("Helvetica", 8, QFont::Normal));
    chart->addAxis(axisYT, Qt::AlignRight);
    temperature->attachAxis(axisYT);

    QValueAxis *axisYP = new QValueAxis;
    axisYP->setRange(0, 5.5);
    axisYP->setTickCount(12);
    axisYP->setLabelFormat("%.1f");
    axisYP->setTitleText(tr("Pressure bar"));
    axisYP->setLabelsFont(QFont("Helvetica", 8, QFont::Normal));
    chart->addAxis(axisYP, Qt::AlignLeft);
    pressure->attachAxis(axisYP);

    chart->setAcceptHoverEvents(true);

    connect(temperature, &QSplineSeries::hovered, this, &ChartCarbonate::tooltip);
    connect(pressure, &QSplineSeries::hovered, this, &ChartCarbonate::tooltip);

    chartView = new QChartView(chart);
    chartView->setRenderHint(QPainter::Antialiasing);
    dialog->setLayout(new QHBoxLayout);
    dialog->layout()->addWidget(chartView);
    dialog->layout()->addWidget(buttonBox);

    QObject::connect(buttonBox, SIGNAL(accepted()), dialog, SLOT(accept()));
    QObject::connect(buttonBox, SIGNAL(rejected()), dialog, SLOT(reject()));
    QObject::connect(saveButton, SIGNAL(clicked()), this, SLOT(savePNG()));

    dialog->setModal(true);
    dialog->exec();
}


ChartCarbonate::~ChartCarbonate() {}


void ChartCarbonate::savePNG()
{
    QSettings settings(QSettings::IniFormat, QSettings::UserScope, "mbse", "bmsapp");
    QString dirName;

    /*
     * First check if the directory stored in the settings file exists.
     * It might be on a removable media that was last used ...
     * If so, fallback to the user's home directory.
     */
    dirName = settings.value("paths/download").toString();
    if (! QDir(dirName).exists()) {
	dirName = QDir::homePath();
    }

    QString path = QFileDialog::getSaveFileName(this, tr("Save Image"), dirName + "/carbonation.png", tr("Image (*.png)"));
    if (path.isEmpty()) {
	QMessageBox::warning(this, tr("Save File"), tr("No image file selected."));
	return;
    }

    /*
     * Update to current selected path
     */
    settings.setValue("paths/download", QFileInfo(path).absolutePath());

    QImage img((chartView->size()), QImage::Format_ARGB32);
    QPainter painter;
    painter.begin(&img);
    chartView->render(&painter);
    painter.setRenderHint(QPainter::Antialiasing);
    painter.end();
    img.save(path);
}


void ChartCarbonate::tooltip(QPointF point, bool state)
{
    QAbstractSeries *series = qobject_cast<QAbstractSeries *>(sender());

    if (t_tooltip == 0)
        t_tooltip = new Callout(chart, series);

    if (state) {
	QDateTime timeis = QDateTime::fromMSecsSinceEpoch(point.x());
	QString suffix = (series == temperature) ? "°C":" bar";
	t_tooltip->setSeries(series);
	t_tooltip->setText(QString("%1\n%2%3").arg(timeis.toString("dd-MM-yyyy hh:mm")).arg(point.y(), 2, 'f', 1).arg(suffix));
	t_tooltip->setAnchor(point);
	t_tooltip->setZValue(11);
	t_tooltip->updateGeometry();
	t_tooltip->show();
    } else {
	t_tooltip->hide();
    }
}

mercurial