src/ChartFermenter.cpp

Thu, 18 Aug 2022 20:34:15 +0200

author
Michiel Broek <mbroek@mbse.eu>
date
Thu, 18 Aug 2022 20:34:15 +0200
changeset 401
583148eb6e01
parent 371
d03a426e0b6b
child 434
ebf4996ab396
permissions
-rw-r--r--

Init est_carb field for new products.

/**
 * 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);

    QSplineSeries *pv_air = new QSplineSeries();
    QSplineSeries *pv_beer = new QSplineSeries();
    QSplineSeries *pv_chiller = new QSplineSeries();
    QSplineSeries *pwr_cool = new QSplineSeries();
    QSplineSeries *pwr_heat = new QSplineSeries();

    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_cool->append(timestamp, query.value("cooler_power").toDouble());
        pwr_heat->append(timestamp, query.value("heater_power").toDouble());
    }

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

    pwr_cool->setName("Cool %");
    pwr_cool->setOpacity(0.25);
    pwr_cool->setColor(QColorConstants::Blue);
    pwr_heat->setName("Heat %");
    pwr_heat->setOpacity(0.25);
    pwr_heat->setColor(QColorConstants::Red);

    chart = new QChart();
    chart->setTitle(QString("%1 \"%2\"").arg(code).arg(name));
    chart->addSeries(pwr_cool);
    chart->addSeries(pwr_heat);
    chart->addSeries(pv_chiller);
    chart->addSeries(pv_air);
    chart->addSeries(pv_beer);

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