src/EditProductTab10.cpp

Mon, 20 Jun 2022 19:55:23 +0200

author
Michiel Broek <mbroek@mbse.eu>
date
Mon, 20 Jun 2022 19:55:23 +0200
changeset 301
fe6346211b5b
parent 298
180c77a81e15
child 333
499c95108bbd
permissions
-rw-r--r--

Finally the translation of string arrays is working.

/**
 * EditProduct.cpp is part of bmsapp.
 *
 * Tab 10, fermentation stages.
 *
 * 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/>.
 */


void EditProduct::brix_changed(double val)
{
    ret_fg = Utils::brix_to_fg(Utils::sg_to_plato(product->brew_fermenter_sg), val);
    //qDebug() << "brix_changed" << val << product->brew_fermenter_sg << ret_fg;
}


double EditProduct::get_fg(double gravity)
{
    QDialog* dialog = new QDialog(this);
    dialog->resize(360, 110);
    QDialogButtonBox *buttonBox = new QDialogButtonBox(dialog);
    buttonBox->setObjectName(QString::fromUtf8("buttonBox"));
    buttonBox->setGeometry(QRect(30, 60, 300, 32));
    buttonBox->setLayoutDirection(Qt::LeftToRight);
    buttonBox->setOrientation(Qt::Horizontal);
    buttonBox->setStandardButtons(QDialogButtonBox::Cancel|QDialogButtonBox::Ok);
    buttonBox->setCenterButtons(true);

    QLabel *brixLabel = new QLabel(dialog);
    brixLabel->setObjectName(QString::fromUtf8("brixLabel"));
    brixLabel->setText(tr("Refractometer Brix:"));
    brixLabel->setGeometry(QRect(10, 20, 161, 24));
    brixLabel->setAlignment(Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter);

    QDoubleSpinBox *brixEdit = new QDoubleSpinBox(dialog);
    brixEdit->setObjectName(QString::fromUtf8("brixEdit"));
    brixEdit->setGeometry(QRect(180, 20, 101, 24));
    brixEdit->setAlignment(Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter);
    brixEdit->setAccelerated(true);
    brixEdit->setDecimals(1);
    brixEdit->setMaximum(32.0);
    brixEdit->setSingleStep(0.1);
    /*
     * Search the Brix value that is needed to get this gravity.
     * Set the found value as preset in the spinbox.
     */
    double brix = 0.0;
    for (brix = 0.0; brix < 32.0; brix += 0.1) {
	if (Utils::brix_to_fg(Utils::sg_to_plato(product->brew_fermenter_sg), brix) >= gravity)
	    break;
    }
    brixEdit->setValue(brix);

    connect(brixEdit, QOverload<double>::of(&QDoubleSpinBox::valueChanged), this, &EditProduct::brix_changed);
    connect(buttonBox, SIGNAL(rejected()), dialog, SLOT(reject()));
    connect(buttonBox, SIGNAL(accepted()), dialog, SLOT(accept()));

    dialog->setModal(true);
    dialog->exec();
    if (dialog->result() == QDialog::Rejected) {
	ret_fg = gravity;
    }

    disconnect(brixEdit, nullptr, nullptr, nullptr);
    disconnect(buttonBox, nullptr, nullptr, nullptr);
    return ret_fg;
}


void EditProduct::primary_start_changed(double val)
{
    product->primary_start_temp = val;
    is_changed();
    setStage();
}


void EditProduct::primary_peak_changed(double val)
{
    product->primary_max_temp = val;
    is_changed();
    setStage();
}


void EditProduct::primary_end_changed(double val)
{
    product->primary_end_temp = val;
    is_changed();
    setStage();
}


void EditProduct::primary_sg_changed(double val)
{
    if (product->primary_end_sg == 0 && val == 0.001) {
	product->primary_end_sg = 0.990;
	const QSignalBlocker blocker1(ui->prim_endsgEdit);
	ui->prim_endsgEdit->setValue(0.990);
    } else {
    	product->primary_end_sg = val;
    }
    ui->prim_attShow->setValue(Utils::calc_svg(product->brew_fermenter_sg, product->primary_end_sg));
    is_changed();
    setStage();
}


void EditProduct::primary_sg_button()
{
    double rc = get_fg(product->primary_end_sg);
    ui->prim_endsgEdit->setValue(rc);
}


void EditProduct::primary_date_changed(QDate val)
{
    product->primary_end_date = ui->prim_enddateEdit->nullDate();
    is_changed();
    setStage();
}


void EditProduct::primary_date_button()
{
    ui->prim_enddateEdit->setDate(QDate::currentDate());
}


void EditProduct::primary_date_ack()
{
    int rc = QMessageBox::warning(this, tr("Confirm primary"), tr("Confirm that the primary fermentation data is correct"),
                            QMessageBox::Yes | QMessageBox::No, QMessageBox::No);

    if (rc == QMessageBox::No)
        return;

    product->stage = PROD_STAGE_SECONDARY;
    setStage();
    is_changed();
}


void EditProduct::secondary_temp_changed(double val)
{
    product->secondary_temp = val;
    is_changed();
    setStage();
}


void EditProduct::secondary_sg_changed(double val)
{
    if (product->secondary_end_sg == 0 && val == 0.001) {
        product->secondary_end_sg = 0.990;
        const QSignalBlocker blocker1(ui->sec_sgEdit);
        ui->sec_sgEdit->setValue(0.990);
    } else {
        product->secondary_end_sg = val;
    }
    ui->sec_attShow->setValue(Utils::calc_svg(product->brew_fermenter_sg, product->secondary_end_sg));
    is_changed();
    setStage();
}


void EditProduct::secondary_sg_button()
{
    double rc;

    /*
     * Get a sensible start value.
     */
    if (product->secondary_end_sg >= 0.990)
	rc = get_fg(product->secondary_end_sg);
    else
	rc = get_fg(product->primary_end_sg);
    qDebug() << "secondary_sg_button" << rc << product->secondary_end_sg;
    ui->sec_sgEdit->setValue(rc);
}


void EditProduct::secondary_date_changed(QDate val)
{
    product->secondary_end_date = ui->sec_enddateEdit->nullDate();
    qDebug() << "secondary_date_changed" << val << product->secondary_end_date;
    is_changed();
    setStage();
}


void EditProduct::secondary_date_button()
{
    ui->sec_enddateEdit->setDate(QDate::currentDate());
}


void EditProduct::secondary_date_ack()
{
    int rc = QMessageBox::warning(this, tr("Confirm secondary"), tr("Confirm that the secondary fermentation data is correct"),
                            QMessageBox::Yes | QMessageBox::No, QMessageBox::No);

    if (rc == QMessageBox::No)
        return;

    product->stage = PROD_STAGE_TERTIARY;
    setStage();
    is_changed();
}


void EditProduct::tertiary_temp_changed(double val)
{
    product->tertiary_temp = val;
    is_changed();
    setStage();
}


void EditProduct::tertiary_sg_changed(double val)
{
                qDebug() << "tertiary_sg_changed" << val;
    if (product->fg == 0 && val == 0.001) {
        product->fg = 0.990;
        const QSignalBlocker blocker1(ui->tert_sgEdit);
        ui->tert_sgEdit->setValue(0.990);
    } else {
        product->fg = val;
    }
    ui->tert_attShow->setValue(Utils::calc_svg(product->brew_fermenter_sg, product->fg));
    product->package_abv = Utils::abvol(product->brew_fermenter_sg, product->fg);
    ui->tert_abvShow->setValue(product->package_abv);
    ui->pack_abvShow->setValue(product->package_abv);
    is_changed();
    setStage();
}


void EditProduct::tertiary_sg_button()
{
    double rc;

    /*
     * Get a sensible start value.
     */
    if (product->fg >= 0.990)
        rc = get_fg(product->fg);
    else
        rc = get_fg(product->secondary_end_sg);
    qDebug() << "tertiary_sg_button" << rc << product->fg;
    ui->tert_sgEdit->setValue(rc);
}


void EditProduct::ferm_log1_button()
{
    QSqlQuery query;
    double timestamp;

    QDialog* dialog = new QDialog(this);
    dialog->resize(1024, 600);
    dialog->setWindowTitle(tr("Fermenter log"));
    dialog->setWindowFlags(Qt::Window | Qt::WindowTitleHint | Qt::CustomizeWindowHint);

    QDialogButtonBox *buttonBox = new QDialogButtonBox(dialog);
    buttonBox->setObjectName(QString::fromUtf8("buttonBox"));
    buttonBox->setGeometry(QRect(40, 565, 944, 36));
    buttonBox->setLayoutDirection(Qt::LeftToRight);
    buttonBox->setOrientation(Qt::Horizontal);
    buttonBox->setStandardButtons(QDialogButtonBox::Ok);
    buttonBox->setCenterButtons(true);

    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", product->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);

    QChart *chart = new QChart();
    chart->setTitle(QString("%1 \"%2\"").arg(product->code).arg(product->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);

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

    connect(buttonBox, SIGNAL(accepted()), dialog, SLOT(accept()));
    dialog->setModal(true);
    dialog->exec();
}


void EditProduct::ferm_log2_button()
{
    QSqlQuery query;
    double timestamp;

    QDialog* dialog = new QDialog(this);
    dialog->resize(1024, 600);
    dialog->setWindowTitle(tr("iSpindel log"));
    dialog->setWindowFlags(Qt::Window | Qt::WindowTitleHint | Qt::CustomizeWindowHint);

    QDialogButtonBox *buttonBox = new QDialogButtonBox(dialog);
    buttonBox->setObjectName(QString::fromUtf8("buttonBox"));
    buttonBox->setGeometry(QRect(40, 565, 944, 36));
    buttonBox->setLayoutDirection(Qt::LeftToRight);
    buttonBox->setOrientation(Qt::Horizontal);
    buttonBox->setStandardButtons(QDialogButtonBox::Ok);
    buttonBox->setCenterButtons(true);

    QSplineSeries *temperature = new QSplineSeries();
    QSplineSeries *density = new QSplineSeries();
    QSplineSeries *battery = new QSplineSeries();

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

    temperature->setName(tr("Temp °C"));
    temperature->setColor(QColorConstants::Svg::red);
    density->setName(tr("SG"));
    QPen pen(QColorConstants::Svg::navy);
    pen.setWidth(3);
    density->setPen(pen);
    battery->setName(tr("Battery"));
    battery->setColor(QColorConstants::Svg::lightgreen);

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

    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);
    battery->attachAxis(axisX);
    temperature->attachAxis(axisX);
    density->attachAxis(axisX);

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

    QValueAxis *axisYD = new QValueAxis;
    axisYD->setTickCount(10);
    axisYD->setLabelFormat("%.3f");
    axisYD->setTitleText("SG");
    axisYD->setLabelsFont(QFont("Helvetica", 8, QFont::Normal));
    chart->addAxis(axisYD, Qt::AlignLeft);
    density->attachAxis(axisYD);

    QValueAxis *axisYB = new QValueAxis;
    axisYB->setTickCount(10);
    axisYB->setLabelFormat("%.2f");
    axisYB->setTitleText(tr("Battery volt"));
    axisYB->setLabelsFont(QFont("Helvetica", 8, QFont::Normal));
    chart->addAxis(axisYB, Qt::AlignRight);
    battery->attachAxis(axisYB);

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

    connect(buttonBox, SIGNAL(accepted()), dialog, SLOT(accept()));
    dialog->setModal(true);
    dialog->exec();
}

mercurial