src/ChartFermenter.cpp

Fri, 18 Nov 2022 16:57:02 +0100

author
Michiel Broek <mbroek@mbse.eu>
date
Fri, 18 Nov 2022 16:57:02 +0100
changeset 443
3c195eb4e7a1
parent 434
ebf4996ab396
child 490
b67fb2e5bb41
permissions
-rw-r--r--

Details CO2 monitor shows the style limits for the specific beer. Adjust the scale of the pressure widget to the beer limits. Moved more functions to the global Utils. Fix expected pressure in the package screen for other priming sugars. Disabled some debug log messages.

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


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

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

    QDialog* dialog = new QDialog(parent);
    dialog->setWindowTitle(tr("BMSapp - Fermenter log ") + "\"" + name + "\"");
    dialog->setObjectName(QString::fromUtf8("ChartFermenter"));
    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);

    QLineSeries *pv_air = new QLineSeries();
    QLineSeries *pv_beer = new QLineSeries();
    QLineSeries *pv_chiller = new QLineSeries();
    QLineSeries *pwr_cool1 = new QLineSeries();
    QLineSeries *pwr_cool0 = new QLineSeries();
    QLineSeries *pwr_heat1 = new QLineSeries();	// Top side of area
    QLineSeries *pwr_heat0 = new QLineSeries();	// Bottom side of area

    query.prepare("SELECT * FROM log_fermenter WHERE code=:code ORDER BY datetime");
    query.bindValue(":code", code);
    query.exec();
    while (query.next()) {
        timestamp = query.value("datetime").toDateTime().toSecsSinceEpoch() * 1000;
        pv_air->append(timestamp, query.value("temp_air").toDouble());
        pv_beer->append(timestamp, query.value("temp_beer").toDouble());
        if (query.value("temp_chiller").toDouble() > 0)
            pv_chiller->append(timestamp, query.value("temp_chiller").toDouble());
	pwr_cool0->append(timestamp, 0);
        pwr_cool1->append(timestamp, query.value("cooler_power").toInt());
	pwr_heat0->append(timestamp, 0);
        pwr_heat1->append(timestamp, query.value("heater_power").toInt());
    }

    pv_air->setName(tr("Air"));
    pv_air->setColor(QColorConstants::Svg::lightgreen);
    pv_beer->setName(tr("Beer"));
    QPen pen(QColorConstants::Svg::navy);
    pen.setWidth(2);
    pv_beer->setPen(pen);
    pv_chiller->setName(tr("Chiller"));
    pv_chiller->setColor(QColorConstants::Svg::lightsalmon);
    pv_chiller->setOpacity(0.75);

    QAreaSeries *pwr_cool = new QAreaSeries(pwr_cool0, pwr_cool1);
    pwr_cool->setName("Cool %");
    pwr_cool->setOpacity(0.50);
    pwr_cool->setColor(QColorConstants::Blue);
    QAreaSeries *pwr_heat = new QAreaSeries(pwr_heat0, pwr_heat1);
    pwr_heat->setName("Heat %");
    pwr_heat->setOpacity(0.50);
    pwr_heat->setColor(QColorConstants::Red);

    chart = new QChart();
    chart->setTitle(QString("%1 \"%2\"").arg(code).arg(name));
    chart->addSeries(pwr_cool);		// Order is important, first drawn is lowest layer.
    chart->addSeries(pwr_heat);
    chart->addSeries(pv_chiller);
    chart->addSeries(pv_air);
    chart->addSeries(pv_beer);		// Top layer

    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);
    pv_air->attachAxis(axisX);
    pv_beer->attachAxis(axisX);
    pv_chiller->attachAxis(axisX);

    QValueAxis *axisY = new QValueAxis;
    axisY->setTickCount(11);
    axisY->setMinorTickCount(1);
    axisY->setLabelFormat("%i");
    axisY->setTitleText(tr("Temp °C"));
    axisY->setLabelsFont(QFont("Helvetica", 8, QFont::Normal));
    chart->addAxis(axisY, Qt::AlignLeft);
    pv_air->attachAxis(axisY);
    pv_beer->attachAxis(axisY);
    pv_chiller->attachAxis(axisY);

    QValueAxis *axisYR = new QValueAxis;
    axisYR->setRange(0, 100);
    axisYR->setTickCount(11);
    axisYR->setLabelFormat("%i");
    axisYR->setTitleText(tr("Power %"));
    axisYR->setLabelsFont(QFont("Helvetica", 8, QFont::Normal));
    chart->addAxis(axisYR, Qt::AlignRight);
    pwr_cool->attachAxis(axisYR);
    pwr_heat->attachAxis(axisYR);

    connect(pv_air, &QLineSeries::hovered, this, &ChartFermenter::tooltip);
    connect(pv_beer, &QLineSeries::hovered, this, &ChartFermenter::tooltip);
    connect(pv_chiller, &QLineSeries::hovered, this, &ChartFermenter::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();
}


ChartFermenter::~ChartFermenter() {}


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

    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 ChartFermenter::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());
	//qDebug() << "tooltip" << QString("%1 %2°C").arg( timeis.toString("dd-MM-yyyy hh:mm") ).arg(point.y(), 2, 'f', 1);

	t_tooltip->setSeries(series);
	t_tooltip->setText(QString("%1\n%2 %3°C").arg(timeis.toString("dd-MM-yyyy hh:mm")).arg(series->name()).arg(point.y(), 2, 'f', 1));
	t_tooltip->setAnchor(point);
	t_tooltip->setZValue(11);
	t_tooltip->updateGeometry();
	t_tooltip->show();
    } else {
	t_tooltip->hide();
    }
}

mercurial