Added profile fermentation tables and editor.

Wed, 16 Mar 2022 21:26:31 +0100

author
Michiel Broek <mbroek@mbse.eu>
date
Wed, 16 Mar 2022 21:26:31 +0100
changeset 57
75d11cc05ce4
parent 56
eb6c564192f4
child 58
27eaf3a22c1a

Added profile fermentation tables and editor.

CMakeLists.txt file | annotate | diff | comparison | revisions
src/EditProfileFerment.cpp file | annotate | diff | comparison | revisions
src/EditProfileFerment.h file | annotate | diff | comparison | revisions
src/MainWindow.cpp file | annotate | diff | comparison | revisions
src/MainWindow.h file | annotate | diff | comparison | revisions
src/ProfileFerments.cpp file | annotate | diff | comparison | revisions
src/ProfileFerments.h file | annotate | diff | comparison | revisions
src/Utils.cpp file | annotate | diff | comparison | revisions
src/Utils.h file | annotate | diff | comparison | revisions
ui/EditProfileFerment.ui file | annotate | diff | comparison | revisions
ui/MainWindow.ui file | annotate | diff | comparison | revisions
ui/ProfileFerments.ui file | annotate | diff | comparison | revisions
--- a/CMakeLists.txt	Tue Mar 15 17:00:54 2022 +0100
+++ b/CMakeLists.txt	Wed Mar 16 21:26:31 2022 +0100
@@ -121,6 +121,8 @@
     ${SRCDIR}/EditProfileMash.cpp
     ${SRCDIR}/ProfileStyles.cpp
     ${SRCDIR}/EditProfileStyle.cpp
+    ${SRCDIR}/ProfileFerments.cpp
+    ${SRCDIR}/EditProfileFerment.cpp
     ${SRCDIR}/Setup.cpp
     ${SRCDIR}/Utils.cpp
     ${SRCDIR}/PrinterDialog.cpp
@@ -152,6 +154,8 @@
     ${SRCDIR}/EditProfileMash.h
     ${SRCDIR}/ProfileStyles.h
     ${SRCDIR}/EditProfileStyle.h
+    ${SRCDIR}/ProfileFerments.h
+    ${SRCDIR}/EditProfileFerment.h
     ${SRCDIR}/Setup.h
     ${SRCDIR}/Utils.h
     ${SRCDIR}/PrinterDialog.h
@@ -182,6 +186,8 @@
     ${UIDIR}/EditProfileMash.ui
     ${UIDIR}/ProfileStyles.ui
     ${UIDIR}/EditProfileStyle.ui
+    ${UIDIR}/ProfileFerments.ui
+    ${UIDIR}/EditProfileFerment.ui
     ${UIDIR}/Setup.ui
     ${UIDIR}/MainWindow.ui
 )
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/EditProfileFerment.cpp	Wed Mar 16 21:26:31 2022 +0100
@@ -0,0 +1,449 @@
+/**
+ * EditProfileFerment.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 "EditProfileFerment.h"
+#include "../ui/ui_EditProfileFerment.h"
+#include "bmsapp.h"
+
+
+EditProfileFerment::EditProfileFerment(int id, QWidget *parent) : QDialog(parent), ui(new Ui::EditProfileFerment)
+{
+    QSqlQuery query;
+
+    qDebug() << "EditProfileFerment record:" << id;
+    ui->setupUi(this);
+    this->recno = id;
+
+    ui->sensorEdit->addItem(tr("Beer"));
+    ui->sensorEdit->addItem(tr("Fridge"));
+
+    if (id >= 0) {
+        query.prepare("SELECT * FROM profile_fermentation WHERE record = :recno");
+        query.bindValue(":recno", id);
+        query.exec();
+        query.next();
+
+        ui->nameEdit->setText(query.value(2).toString());
+	ui->temploEdit->setValue(query.value(3).toDouble());
+	ui->temphiEdit->setValue(query.value(4).toDouble());
+	ui->sensorEdit->setCurrentIndex(query.value(5).toInt());
+
+	QJsonParseError parseError;
+        const auto& json = query.value(8).toString();
+
+        if (!json.trimmed().isEmpty()) {
+            const auto& formattedJson = QString("%1").arg(json);
+            this->steps = QJsonDocument::fromJson(formattedJson.toUtf8(),  &parseError);
+
+            if (parseError.error != QJsonParseError::NoError)
+                qDebug() << "Parse error: " << parseError.errorString() << "at" << parseError.offset ;
+	}
+
+    } else {
+        /* Set some defaults */
+	const auto& formattedJson = QString("[]");
+	this->steps = QJsonDocument::fromJson(formattedJson.toUtf8());
+    }
+
+    connect(ui->nameEdit, &QLineEdit::textChanged, this, &EditProfileFerment::is_changed);
+    connect(ui->temploEdit, &QDoubleSpinBox::textChanged, this, &EditProfileFerment::templo_changed);
+    connect(ui->temphiEdit, &QDoubleSpinBox::textChanged, this, &EditProfileFerment::temphi_changed);
+    connect(ui->sensorEdit, &QComboBox::currentTextChanged, this, &EditProfileFerment::is_changed);
+    connect(ui->stepsTable, SIGNAL(cellChanged(int, int)), this, SLOT(cell_Changed(int, int)));
+
+    ui->saveButton->setEnabled(false);
+    ui->deleteButton->setEnabled((id >= 0) ? true:false);
+
+    emit refreshTable();
+
+    WindowTitle();
+}
+
+
+void EditProfileFerment::refreshTable()
+{
+    QString w;
+    QWidget* pWidget;
+    QHBoxLayout* pLayout;
+    double  d;
+
+//    qDebug() << "refreshTable" << this->steps << this->steps.isArray() << this->steps.array().size() ;
+    /*
+     * During filling the table turn off the cellChanged signal because every cell that is filled
+     * triggers the cellChanged signal. The QTableWidget has no better signal to use.
+     */
+    this->ignoreChanges = true;
+
+    const QStringList labels({tr("Step name"), tr("Start °C"), tr("End °C"), tr("Sensor"), tr("Ramp time"), tr("Rest time"), tr("Button")});
+    ui->stepsTable->setColumnCount(7);
+    ui->stepsTable->setColumnWidth(0, 248);	/* Step name	*/
+    ui->stepsTable->setColumnWidth(1,  75);	/* Start temp	*/
+    ui->stepsTable->setColumnWidth(2,  75);	/* End temp	*/
+    ui->stepsTable->setColumnWidth(3, 120);	/* Sensor	*/
+    ui->stepsTable->setColumnWidth(4,  85);	/* Ramp time	*/
+    ui->stepsTable->setColumnWidth(5,  85);	/* Rest time	*/
+    ui->stepsTable->setColumnWidth(6,  80);	/* Button	*/
+    ui->stepsTable->setHorizontalHeaderLabels(labels);
+    ui->stepsTable->verticalHeader()->hide();
+    ui->stepsTable->setRowCount(this->steps.array().size());
+
+    totalsteps = 0;
+    duration = 0;
+
+    if (this->steps.isArray()) {
+	totalsteps = this->steps.array().size();
+	for (int i = 0; i < this->steps.array().size(); i++) {
+	    QJsonObject obj = this->steps.array().at(i).toObject();
+
+	    ui->stepsTable->setItem(i, 0, new QTableWidgetItem(obj["name"].toString()));
+
+	    /* Numbers can be double quoted or not, the old application could do this wrong. */
+	    if (obj["target_lo"].isString())
+		d = QString(obj["target_lo"].toString()).toDouble();
+	    else
+		d = obj["target_lo"].toDouble();
+	    w = QString("%1").arg(d, 2, 'f', 1, '0');
+	    QTableWidgetItem *item = new QTableWidgetItem(w);
+            item->setTextAlignment(Qt::AlignCenter|Qt::AlignVCenter);
+	    ui->stepsTable->setItem(i, 1, item);
+
+	    if (obj["target_hi"].isString())
+                d = QString(obj["target_hi"].toString()).toDouble();
+            else
+                d = obj["target_hi"].toDouble();
+            w = QString("%1").arg(d, 2, 'f', 1, '0');
+            item = new QTableWidgetItem(w);
+            item->setTextAlignment(Qt::AlignCenter|Qt::AlignVCenter);
+            ui->stepsTable->setItem(i, 2, item);
+
+	    /* Adding fridgemode 0, 1 as combobox. */
+            QComboBox* myComboBox = new QComboBox();
+            myComboBox->addItem(tr("Beer"));
+            myComboBox->addItem(tr("Fridge"));
+            ui->stepsTable->setCellWidget(i, 3, myComboBox);
+
+            if (obj["fridgemode"].isString()) {
+                d = QString(obj["fridgemode"].toString()).toDouble();
+	    } else {
+                d = obj["fridgemode"].toDouble();
+	    }
+
+            myComboBox->setCurrentIndex((int)d);
+            connect<void(QComboBox::*)(int)>(myComboBox, &QComboBox::currentIndexChanged, this, &EditProfileFerment::combo_Changed);
+
+	    if (obj["steptime"].isString())
+                d = QString(obj["steptime"].toString()).toDouble();
+            else
+                d = obj["steptime"].toDouble();
+            w = QString("%1").arg(d, 1, 'f', 0, '0');
+            item = new QTableWidgetItem(w);
+            item->setTextAlignment(Qt::AlignCenter|Qt::AlignVCenter);
+            ui->stepsTable->setItem(i, 4, item);
+	    duration += (int)d;
+
+	    if (obj["resttime"].isString())
+                d = QString(obj["resttime"].toString()).toDouble();
+            else
+                d = obj["resttime"].toDouble();
+            w = QString("%1").arg(d, 1, 'f', 0, '0');
+            item = new QTableWidgetItem(w);
+            item->setTextAlignment(Qt::AlignCenter|Qt::AlignVCenter);
+            ui->stepsTable->setItem(i, 5, item);
+	    duration += (int)d;
+
+	    /* Add the Delete row button */
+            pWidget = new QWidget();
+            QPushButton* btn_edit = new QPushButton();
+            btn_edit->setObjectName(QString("%1").arg(i));  /* Send row with the button */
+            btn_edit->setText(tr("Delete"));
+            connect(btn_edit, SIGNAL(clicked()), this, SLOT(on_deleteRow_clicked()));
+            pLayout = new QHBoxLayout(pWidget);
+            pLayout->addWidget(btn_edit);
+            pLayout->setContentsMargins(5, 0, 5, 0);
+            pWidget->setLayout(pLayout);
+            ui->stepsTable->setCellWidget(i, 6, pWidget);
+	}
+    }
+
+    /* Show the calculated total fermentation time. */
+    ui->totalEdit->setText(Utils::hours_to_string(duration));
+    this->ignoreChanges = false;
+}
+
+
+EditProfileFerment::~EditProfileFerment()
+{
+    qDebug() << "EditProfileFerment done";
+    delete ui;
+    emit entry_changed();
+}
+
+
+/*
+ * Window header, mark any change with '**'
+ */
+void EditProfileFerment::WindowTitle()
+{
+    QString txt;
+
+    if (this->recno < 0) {
+	txt = QString(tr("BMSapp - Add new fermentation profile"));
+    } else {
+	txt = QString(tr("BMSapp - Edit fermentation profile %1").arg(this->recno));
+    }
+
+    if (this->textIsChanged) {
+	txt.append((QString(" **")));
+    }
+    setWindowTitle(txt);
+}
+
+
+void EditProfileFerment::on_saveButton_clicked()
+{
+    QSqlQuery query;
+
+    /* If there are errors in the form, show a message and do "return;" */
+    if (ui->nameEdit->text().length() < 2) {
+	QMessageBox::warning(this, tr("Edit Ferment"), tr("Name empty or too short."));
+	return;
+    }
+
+    if (this->textIsChanged) {
+    	if (this->recno == -1) {
+    	    query.prepare("INSERT INTO profile_fermentation SET name=:name, inittemp_lo=:templo, "
+		"inittemp_hi=:temphi, fridgemode=:fridgemode, totalsteps=:totalsteps, duration=:duration, "
+		"steps=:steps, uuid = :uuid");
+    	} else {
+	    query.prepare("UPDATE profile_fermentation SET name=:name, inittemp_lo=:templo, "
+		"inittemp_hi=:temphi, fridgemode=:fridgemode, totalsteps=:totalsteps, duration=:duration, "
+                "steps=:steps WHERE record = :recno");
+    	}
+	query.bindValue(":name", ui->nameEdit->text());
+	query.bindValue(":templo", QString("%1").arg(ui->temploEdit->value(), 2, 'f', 1, '0'));
+	query.bindValue(":temphi", QString("%1").arg(ui->temphiEdit->value(), 2, 'f', 1, '0'));
+	query.bindValue(":fridgemode", ui->sensorEdit->currentIndex());
+	query.bindValue(":totalsteps", QString("%1").arg(totalsteps));
+	query.bindValue(":duration", QString("%1").arg(duration));
+	query.bindValue(":steps", this->steps.toJson(QJsonDocument::Compact));
+	if (this->recno == -1) {
+	    query.bindValue(":uuid", QUuid::createUuid().toString().mid(1, 36));
+	} else {
+	    query.bindValue(":recno", this->recno);
+	}
+	query.exec();
+	if (query.lastError().isValid()) {
+	    qDebug() << "EditProfileFerment" << query.lastError();
+	    QMessageBox::warning(this, tr("Database error"),
+                        tr("MySQL error: %1\n%2\n%3")
+                        .arg(query.lastError().nativeErrorCode())
+                        .arg(query.lastError().driverText())
+                        .arg(query.lastError().databaseText()));
+	} else {
+	    qDebug() << "EditProfileFerment Saved";
+	}
+    }
+
+    ui->saveButton->setEnabled(false);
+    this->textIsChanged = false;
+    WindowTitle();
+}
+
+
+void EditProfileFerment::on_deleteButton_clicked()
+{
+    QSqlQuery query;
+
+    query.prepare("DELETE FROM profile_fermentation WHERE record = :recno");
+    query.bindValue(":recno", this->recno);
+    query.exec();
+    if (query.lastError().isValid()) {
+	qDebug() << "EditProfileFerment" << query.lastError();
+	QMessageBox::warning(this, tr("Database error"),
+                        tr("MySQL error: %1\n%2\n%3")
+                        .arg(query.lastError().nativeErrorCode())
+                        .arg(query.lastError().driverText())
+                        .arg(query.lastError().databaseText()));
+    } else {
+	qDebug() << "EditProfileFerment Deleted" << this->recno;
+    }
+
+    this->close();
+    this->setResult(1);
+}
+
+
+void EditProfileFerment::is_changed()
+{
+    ui->saveButton->setEnabled(true);
+    ui->deleteButton->setEnabled((this->recno >= 0) ? true:false);
+    this->textIsChanged = true;
+    WindowTitle();
+}
+
+
+/*
+ * Rebuild the json string from the table contents.
+ */
+void EditProfileFerment::make_Json()
+{
+    QTableWidgetItem *item;
+    QJsonArray array;
+
+    for (int i = 0; i < ui->stepsTable->rowCount(); i++) {
+
+	QJsonObject obj;
+	item = ui->stepsTable->item(i, 0);
+	obj.insert("name", item->text());
+	item = ui->stepsTable->item(i, 1);
+        obj.insert("target_lo", item->text().toDouble());
+        item = ui->stepsTable->item(i, 2);
+        obj.insert("target_hi", item->text().toDouble());
+	QWidget *widget = ui->stepsTable->cellWidget(i, 3);
+        obj.insert("fridgemode", static_cast<QComboBox*>(widget)->currentIndex() );
+	item = ui->stepsTable->item(i, 4);
+        obj.insert("steptime", item->text().toInt());
+	item = ui->stepsTable->item(i, 5);
+        obj.insert("resttime", item->text().toInt());
+//	qDebug() << "make_Json" << i << obj;
+	array.append(obj);	/* Append this object */
+    }
+
+//    qDebug() << array;
+    /* Copy to the global array and refresh */
+    this->steps.setArray(array);
+    is_changed();
+    emit refreshTable();
+}
+
+
+void EditProfileFerment::cell_Changed(int nRow, int nCol)
+{
+    QString w;
+
+    if (this->ignoreChanges)
+	return;
+
+    qDebug() << "Cell at row " + QString::number(nRow) + " column " + QString::number(nCol) + " was changed.";
+
+    /*
+     * Check the temperature ranges. Make sure that the high temperature is at least
+     * 0.1 degree higher then the low temperature.
+     */
+    if (nCol == 1) {		// Low temperature was changed
+	if (ui->stepsTable->item(nRow, 1)->text().toDouble() > (ui->stepsTable->item(nRow, 2)->text().toDouble() - 0.1)) {
+	    w = QString("%1").arg(ui->stepsTable->item(nRow, 1)->text().toDouble() + 0.1, 2, 'f', 1, '0');
+	    ui->stepsTable->setItem(nRow, 2, new QTableWidgetItem(w));
+	}
+    } else if (nCol == 2) {	// High temperature was changed
+	if (ui->stepsTable->item(nRow, 2)->text().toDouble() < (ui->stepsTable->item(nRow, 1)->text().toDouble() + 0.1)) {
+	    w = QString("%1").arg(ui->stepsTable->item(nRow, 2)->text().toDouble() - 0.1, 2, 'f', 1, '0');
+	    ui->stepsTable->setItem(nRow, 1, new QTableWidgetItem(w));
+	}
+    }
+    make_Json();
+}
+
+
+void EditProfileFerment::combo_Changed()
+{
+    make_Json();
+}
+
+
+void EditProfileFerment::templo_changed()
+{
+    if (ui->temploEdit->value() > (ui->temphiEdit->value() - 0.1))
+        ui->temphiEdit->setValue(ui->temploEdit->value() + 0.1);
+    is_changed();
+}
+
+
+void EditProfileFerment::temphi_changed()
+{
+    if (ui->temphiEdit->value() < (ui->temploEdit->value() + 0.1))
+        ui->temploEdit->setValue(ui->temphiEdit->value() - 0.1);
+    is_changed();
+}
+
+
+/*
+ * Add new row and initialize all fields.
+ */
+void EditProfileFerment::on_addButton_clicked()
+{
+    QWidget* pWidget;
+    QHBoxLayout* pLayout;
+
+    qDebug() << "Add row" << totalsteps;
+    this->ignoreChanges = true;
+    ui->stepsTable->insertRow(totalsteps);
+    ui->stepsTable->setItem(totalsteps, 0, new QTableWidgetItem(QString("new row %1").arg(totalsteps)));
+    ui->stepsTable->setItem(totalsteps, 1, new QTableWidgetItem(QString("19.8")));
+    ui->stepsTable->setItem(totalsteps, 2, new QTableWidgetItem(QString("20.0")));
+    QComboBox* myComboBox = new QComboBox();
+    myComboBox->addItem(tr("Beer"));
+    myComboBox->addItem(tr("Fridge"));
+    myComboBox->setCurrentIndex(0);
+    ui->stepsTable->setCellWidget(totalsteps, 3, myComboBox);
+    ui->stepsTable->setItem(totalsteps, 4, new QTableWidgetItem(QString("0")));
+    ui->stepsTable->setItem(totalsteps, 5, new QTableWidgetItem(QString("0")));
+
+    pWidget = new QWidget();
+    QPushButton* btn_edit = new QPushButton();
+    btn_edit->setObjectName(QString("%1").arg(totalsteps));  /* Send row with the button */
+    btn_edit->setText(tr("Delete"));
+    connect(btn_edit, SIGNAL(clicked()), this, SLOT(on_deleteRow_clicked()));
+    pLayout = new QHBoxLayout(pWidget);
+    pLayout->addWidget(btn_edit);
+    pLayout->setContentsMargins(5, 0, 5, 0);
+    pWidget->setLayout(pLayout);
+    ui->stepsTable->setCellWidget(totalsteps, 6, pWidget);
+
+    this->ignoreChanges = false;
+    make_Json();
+}
+
+
+void EditProfileFerment::on_deleteRow_clicked()
+{
+    QPushButton *pb = qobject_cast<QPushButton *>(QObject::sender());
+    int row = pb->objectName().toInt();
+    qDebug() << "Delete row" << row;
+    ui->stepsTable->removeRow(row);
+    make_Json();
+}
+
+
+void EditProfileFerment::on_quitButton_clicked()
+{
+    if (this->textIsChanged) {
+	int rc = QMessageBox::warning(this, tr("Fermentation changed"), tr("This fermentation profile has been modified. Save changes?"),
+                                QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel, QMessageBox::Save);
+        switch (rc) {
+            case QMessageBox::Save:
+                        on_saveButton_clicked();
+                        break;  /* Saved and then Quit */
+            case QMessageBox::Discard:
+                        break;  /* Quit without Save */
+            case QMessageBox::Cancel:
+                        return; /* Return to the editor page */
+        }
+    }
+
+    this->close();
+    this->setResult(1);
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/EditProfileFerment.h	Wed Mar 16 21:26:31 2022 +0100
@@ -0,0 +1,46 @@
+#ifndef _EDITPROFILEFERMENT_H
+#define _EDITPROFILEFERMENT_H
+
+#include <QDialog>
+#include <QJsonDocument>
+
+namespace Ui {
+class EditProfileFerment;
+}
+
+class EditProfileFerment : public QDialog
+{
+    Q_OBJECT
+
+signals:
+    void entry_changed();
+
+public:
+    explicit EditProfileFerment(int id, QWidget *parent = 0);
+    ~EditProfileFerment();
+
+private slots:
+    void on_saveButton_clicked();
+    void on_quitButton_clicked();
+    void on_deleteButton_clicked();
+    void is_changed();
+    void make_Json();
+    void refreshTable(void);
+    void cell_Changed(int nRow, int nCol);
+    void combo_Changed();
+    void templo_changed();
+    void temphi_changed();
+    void on_addButton_clicked();
+    void on_deleteRow_clicked();
+
+private:
+    Ui::EditProfileFerment *ui;
+    int recno, duration, totalsteps;
+    bool textIsChanged = false;
+    bool ignoreChanges = false;
+    QJsonDocument steps;
+
+    void WindowTitle();
+};
+
+#endif
--- a/src/MainWindow.cpp	Tue Mar 15 17:00:54 2022 +0100
+++ b/src/MainWindow.cpp	Wed Mar 16 21:26:31 2022 +0100
@@ -26,6 +26,7 @@
 #include "ProfileWaters.h"
 #include "ProfileMashs.h"
 #include "ProfileStyles.h"
+#include "ProfileFerments.h"
 #include "Setup.h"
 #include "PrinterDialog.h"
 #include "../ui/ui_MainWindow.h"
@@ -255,6 +256,24 @@
 }
 
 
+void MainWindow::fromProfileFerments()
+{
+    qDebug() << Q_FUNC_INFO;
+    delete ProfileFermentsWindow;
+    this->show();
+}
+
+
+void MainWindow::on_actionFerments_profiles_triggered()
+{
+    qDebug() << Q_FUNC_INFO;
+    ProfileFermentsWindow = new ProfileFerments(this);
+    QObject::connect(ProfileFermentsWindow, SIGNAL(firstWindow()), this, SLOT(fromProfileFerments()));
+    this->hide();    // Close the main window
+    ProfileFermentsWindow->show();  // Show a second window
+}
+
+
 void MainWindow::fromSetup()
 {
     qDebug() << Q_FUNC_INFO;
--- a/src/MainWindow.h	Tue Mar 15 17:00:54 2022 +0100
+++ b/src/MainWindow.h	Wed Mar 16 21:26:31 2022 +0100
@@ -11,6 +11,7 @@
 #include "ProfileWaters.h"
 #include "ProfileMashs.h"
 #include "ProfileStyles.h"
+#include "ProfileFerments.h"
 #include "Setup.h"
 
 #include <QMainWindow>
@@ -44,6 +45,7 @@
     void on_actionWater_profiles_triggered();
     void on_actionMash_profiles_triggered();
     void on_actionStyles_profiles_triggered();
+    void on_actionFerments_profiles_triggered();
     void on_actionSetup_triggered();
     void on_actionAbout_triggered();
 
@@ -58,6 +60,7 @@
     void fromProfileWaters();
     void fromProfileMashs();
     void fromProfileStyles();
+    void fromProfileFerments();
     void fromSetup();
 
 private:
@@ -74,6 +77,7 @@
     ProfileWaters *ProfileWatersWindow;
     ProfileMashs *ProfileMashsWindow;
     ProfileStyles *ProfileStylesWindow;
+    ProfileFerments *ProfileFermentsWindow;
     Setup *SetupWindow;
 };
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/ProfileFerments.cpp	Wed Mar 16 21:26:31 2022 +0100
@@ -0,0 +1,147 @@
+/**
+ * ProfileFerments.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 "ProfileFerments.h"
+#include "EditProfileFerment.h"
+#include "../ui/ui_ProfileFerments.h"
+#include "config.h"
+#include "bmsapp.h"
+
+
+ProfileFerments::ProfileFerments(QWidget *parent) : QDialog(parent), ui(new Ui::ProfileFerments)
+{
+    qDebug() << "ProfileFerments start";
+
+    ui->setupUi(this);
+    emit refreshTable();
+
+    setWindowTitle( QString("BMSapp - %1 - Profile Fermentation").arg(VERSIONSTRING) );
+}
+
+
+void ProfileFerments::refreshTable()
+{
+    QString w;
+    QWidget* pWidget;
+    QLabel *label;
+    QHBoxLayout* pLayout;
+
+    qDebug() << "ProfileFerments reload";
+
+    QSqlQuery query("SELECT * FROM profile_fermentation ORDER BY name");
+    const QStringList labels({tr("Name"), tr("Start low"), tr("Start high"), tr("Sensor"), tr("Steps"), tr("Duration"), tr("Edit")});
+
+    ui->tableFerments->setColumnCount(7);
+    ui->tableFerments->setColumnWidth(0, 450);	/* Name		*/
+    ui->tableFerments->setColumnWidth(1,  90);	/* Min temp	*/
+    ui->tableFerments->setColumnWidth(2,  90);	/* Max temp	*/
+    ui->tableFerments->setColumnWidth(3,  75);	/* Sensor	*/
+    ui->tableFerments->setColumnWidth(4,  75);	/* Steps	*/
+    ui->tableFerments->setColumnWidth(5, 120);	/* Duration	*/
+    ui->tableFerments->setColumnWidth(6,  80);	/* Edit button	*/
+    ui->tableFerments->setRowCount(query.size());
+    ui->tableFerments->setHorizontalHeaderLabels(labels);
+    ui->tableFerments->verticalHeader()->hide();
+    ui->tableFerments->setFixedSize(980 + 24, 640);	/* Even if this is too large, it works */
+
+    QTableWidgetItem *rightitem = new QTableWidgetItem();
+    rightitem->setTextAlignment(Qt::AlignRight);
+
+    query.first();
+    for (int ridx = 0 ; ridx < query.size() ; ridx++ ) {
+	ui->tableFerments->setItem(ridx, 0, new QTableWidgetItem(query.value(2).toString()));	/* Name */
+
+	w = QString("%1 °C").arg(query.value(3).toDouble(), 2, 'f', 1, '0' );      /* Min start temp */
+        QTableWidgetItem *item = new QTableWidgetItem(w);
+        item->setTextAlignment(Qt::AlignRight|Qt::AlignVCenter);
+        ui->tableFerments->setItem(ridx, 1, item);
+
+	w = QString("%1 °C").arg(query.value(4).toDouble(), 2, 'f', 1, '0' );      /* Max start temp */
+        item = new QTableWidgetItem(w);
+        item->setTextAlignment(Qt::AlignRight|Qt::AlignVCenter);
+        ui->tableFerments->setItem(ridx, 2, item);
+
+	w = (query.value(5).toInt()) ? tr("Fridge") : tr("Beer");	/* Initial sensor */
+	item = new QTableWidgetItem(w);
+        item->setTextAlignment(Qt::AlignCenter|Qt::AlignVCenter);
+        ui->tableFerments->setItem(ridx, 3, item);
+
+	w = QString("%1").arg(query.value(6).toInt());	/* Number of steps */
+	item = new QTableWidgetItem(w);
+        item->setTextAlignment(Qt::AlignCenter|Qt::AlignVCenter);
+        ui->tableFerments->setItem(ridx, 4, item);
+
+	w = Utils::hours_to_string(query.value(7).toInt());	/* Duration */
+	item = new QTableWidgetItem(w);
+        item->setTextAlignment(Qt::AlignCenter|Qt::AlignVCenter);
+        ui->tableFerments->setItem(ridx, 5, item);
+
+	/* Add the Edit button */
+	pWidget = new QWidget();
+	QPushButton* btn_edit = new QPushButton();
+	btn_edit->setObjectName(QString("%1").arg(query.value(0).toString()));	/* Send record with the button */
+	btn_edit->setText(tr("Edit"));
+	connect(btn_edit, SIGNAL(clicked()), this, SLOT(on_editButton_clicked()));
+	pLayout = new QHBoxLayout(pWidget);
+	pLayout->addWidget(btn_edit);
+	pLayout->setContentsMargins(5, 0, 5, 0);
+	pWidget->setLayout(pLayout);
+	ui->tableFerments->setCellWidget(ridx, 6, pWidget);
+	query.next();
+    }
+}
+
+
+ProfileFerments::~ProfileFerments()
+{
+    qDebug() << "ProfileFerments done";
+    delete ui;
+}
+
+
+void ProfileFerments::edit(int recno)
+{
+    qDebug() << "ProfileFerments edit:" << recno;
+
+    EditProfileFerment dialog(recno, this);
+    /* Signal from editor if a refresh is needed */
+    connect(&dialog, SIGNAL(entry_changed()), this, SLOT(refreshTable()));
+    dialog.setModal(true);
+    dialog.exec();
+}
+
+
+void ProfileFerments::on_editButton_clicked()
+{
+    QPushButton *pb = qobject_cast<QPushButton *>(QObject::sender());
+    int recno = pb->objectName().toInt();
+    qDebug() << Q_FUNC_INFO << recno;
+    edit(recno);
+}
+
+
+void ProfileFerments::on_insertButton_clicked()
+{
+    qDebug() << Q_FUNC_INFO;
+    edit(-1);
+}
+
+
+void ProfileFerments::on_quitButton_clicked()
+{
+    emit firstWindow();
+}
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/ProfileFerments.h	Wed Mar 16 21:26:31 2022 +0100
@@ -0,0 +1,32 @@
+#ifndef _PROFILEFERMENT_H
+#define _PROFILEFERMENT_H
+
+#include <QDialog>
+
+namespace Ui {
+class ProfileFerments;
+}
+
+class ProfileFerments : public QDialog
+{
+    Q_OBJECT
+
+public:
+    explicit ProfileFerments(QWidget *parent = nullptr);
+    ~ProfileFerments();
+
+signals:
+    void firstWindow();
+
+private slots:
+    void on_quitButton_clicked();
+    void on_insertButton_clicked();
+    void on_editButton_clicked();
+    void refreshTable(void);
+
+private:
+    Ui::ProfileFerments *ui;
+    void edit(int recno);
+};
+
+#endif
--- a/src/Utils.cpp	Tue Mar 15 17:00:54 2022 +0100
+++ b/src/Utils.cpp	Wed Mar 16 21:26:31 2022 +0100
@@ -55,3 +55,33 @@
 
 
 
+QString Utils::hours_to_string(int hours)
+{
+    int dd, hh, ww;
+
+    if (hours == 1)
+	return QObject::tr("1 hour");
+    if (hours < 24)
+	return QString("%1 ").arg(hours) + QString(QObject::tr("hours"));
+
+    dd = hours / 24;
+    hh = hours % 24;
+    if (dd == 1) {
+	if (hh == 0)
+	    return QString(QObject::tr("1 day"));
+	else if (hh == 1)
+	    return QString(QObject::tr("1 day, ")) + QString("%1 ").arg(hh) + QString(QObject::tr("hour"));
+	else
+	    return QString(QObject::tr("1 day, ")) + QString("%1 ").arg(hh) + QString(QObject::tr("hours"));
+    } else {
+	if (hh == 0)
+	    return QString("%1 ").arg(dd) + QString(QObject::tr("days"));
+	else if (hh == 1)
+	    return QString("%1 ").arg(dd) + QString(QObject::tr("days, ")) + QString("%1 ").arg(hh) + QString(QObject::tr("hour"));
+	else
+	    return QString("%1 ").arg(dd) + QString(QObject::tr("days, ")) + QString("%1 ").arg(hh) + QString(QObject::tr("hours"));
+    }
+    return QString("hours_to_string error");
+}
+
+
--- a/src/Utils.h	Tue Mar 15 17:00:54 2022 +0100
+++ b/src/Utils.h	Wed Mar 16 21:26:31 2022 +0100
@@ -1,6 +1,8 @@
 #ifndef _UTILS_H
 #define	_UTILS_H
 
+#include <QString>
+
 
 /**
  * @namespace Utils
@@ -13,6 +15,9 @@
     double kolbach_to_lintner(double kolbach);
     double ebc_to_srm(double ebc);
     double srm_to_ebc(double srm);
+
+    QString hours_to_string(int hours);
+
 }
 
 #endif
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ui/EditProfileFerment.ui	Wed Mar 16 21:26:31 2022 +0100
@@ -0,0 +1,320 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<ui version="4.0">
+ <class>EditProfileFerment</class>
+ <widget class="QDialog" name="EditProfileFerment">
+  <property name="geometry">
+   <rect>
+    <x>0</x>
+    <y>0</y>
+    <width>1024</width>
+    <height>560</height>
+   </rect>
+  </property>
+  <property name="windowTitle">
+   <string>Dialog</string>
+  </property>
+  <layout class="QGridLayout" name="gridLayout">
+   <item row="0" column="0">
+    <widget class="QWidget" name="topWidget" native="true">
+     <widget class="QLabel" name="nameLabel">
+      <property name="geometry">
+       <rect>
+        <x>10</x>
+        <y>10</y>
+        <width>91</width>
+        <height>20</height>
+       </rect>
+      </property>
+      <property name="text">
+       <string>Name:</string>
+      </property>
+      <property name="alignment">
+       <set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
+      </property>
+     </widget>
+     <widget class="QLineEdit" name="nameEdit">
+      <property name="geometry">
+       <rect>
+        <x>110</x>
+        <y>10</y>
+        <width>571</width>
+        <height>23</height>
+       </rect>
+      </property>
+      <property name="maxLength">
+       <number>128</number>
+      </property>
+      <property name="placeholderText">
+       <string>Name of the fermentation profile</string>
+      </property>
+     </widget>
+     <widget class="QPushButton" name="quitButton">
+      <property name="geometry">
+       <rect>
+        <x>110</x>
+        <y>510</y>
+        <width>80</width>
+        <height>23</height>
+       </rect>
+      </property>
+      <property name="sizePolicy">
+       <sizepolicy hsizetype="Fixed" vsizetype="Fixed">
+        <horstretch>0</horstretch>
+        <verstretch>0</verstretch>
+       </sizepolicy>
+      </property>
+      <property name="text">
+       <string>Quit</string>
+      </property>
+      <property name="icon">
+       <iconset>
+        <normaloff>:icons/silk/door_out.png</normaloff>:icons/silk/door_out.png</iconset>
+      </property>
+     </widget>
+     <widget class="QPushButton" name="saveButton">
+      <property name="enabled">
+       <bool>false</bool>
+      </property>
+      <property name="geometry">
+       <rect>
+        <x>820</x>
+        <y>510</y>
+        <width>80</width>
+        <height>23</height>
+       </rect>
+      </property>
+      <property name="text">
+       <string>Save</string>
+      </property>
+      <property name="icon">
+       <iconset>
+        <normaloff>:icons/silk/disk.png</normaloff>:icons/silk/disk.png</iconset>
+      </property>
+     </widget>
+     <widget class="QPushButton" name="deleteButton">
+      <property name="enabled">
+       <bool>false</bool>
+      </property>
+      <property name="geometry">
+       <rect>
+        <x>463</x>
+        <y>510</y>
+        <width>80</width>
+        <height>23</height>
+       </rect>
+      </property>
+      <property name="text">
+       <string>Delete</string>
+      </property>
+      <property name="icon">
+       <iconset>
+        <normaloff>:icons/silk/delete.png</normaloff>:icons/silk/delete.png</iconset>
+      </property>
+     </widget>
+     <widget class="QTableWidget" name="stepsTable">
+      <property name="geometry">
+       <rect>
+        <x>110</x>
+        <y>130</y>
+        <width>791</width>
+        <height>351</height>
+       </rect>
+      </property>
+     </widget>
+     <widget class="QLabel" name="stepsLabel">
+      <property name="geometry">
+       <rect>
+        <x>10</x>
+        <y>130</y>
+        <width>91</width>
+        <height>20</height>
+       </rect>
+      </property>
+      <property name="text">
+       <string>Steps:</string>
+      </property>
+      <property name="alignment">
+       <set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
+      </property>
+     </widget>
+     <widget class="QLabel" name="totalLabel">
+      <property name="geometry">
+       <rect>
+        <x>10</x>
+        <y>100</y>
+        <width>91</width>
+        <height>20</height>
+       </rect>
+      </property>
+      <property name="text">
+       <string>Total time:</string>
+      </property>
+      <property name="alignment">
+       <set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
+      </property>
+     </widget>
+     <widget class="QLineEdit" name="totalEdit">
+      <property name="geometry">
+       <rect>
+        <x>110</x>
+        <y>100</y>
+        <width>171</width>
+        <height>23</height>
+       </rect>
+      </property>
+      <property name="readOnly">
+       <bool>true</bool>
+      </property>
+     </widget>
+     <widget class="QPushButton" name="addButton">
+      <property name="geometry">
+       <rect>
+        <x>820</x>
+        <y>100</y>
+        <width>80</width>
+        <height>23</height>
+       </rect>
+      </property>
+      <property name="text">
+       <string>Add step</string>
+      </property>
+      <property name="icon">
+       <iconset resource="../../../../../../home/mbroek/MyProjects/bmsapp/resources/icons.qrc">
+        <normaloff>:/icons/silk/add.png</normaloff>:/icons/silk/add.png</iconset>
+      </property>
+     </widget>
+     <widget class="QLabel" name="temploLabel">
+      <property name="geometry">
+       <rect>
+        <x>10</x>
+        <y>40</y>
+        <width>91</width>
+        <height>20</height>
+       </rect>
+      </property>
+      <property name="text">
+       <string>Start low:</string>
+      </property>
+      <property name="alignment">
+       <set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
+      </property>
+     </widget>
+     <widget class="QLabel" name="temphiLabel">
+      <property name="geometry">
+       <rect>
+        <x>470</x>
+        <y>40</y>
+        <width>91</width>
+        <height>20</height>
+       </rect>
+      </property>
+      <property name="text">
+       <string>Start high:</string>
+      </property>
+      <property name="alignment">
+       <set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
+      </property>
+     </widget>
+     <widget class="QDoubleSpinBox" name="temploEdit">
+      <property name="geometry">
+       <rect>
+        <x>110</x>
+        <y>40</y>
+        <width>111</width>
+        <height>24</height>
+       </rect>
+      </property>
+      <property name="alignment">
+       <set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
+      </property>
+      <property name="accelerated">
+       <bool>true</bool>
+      </property>
+      <property name="suffix">
+       <string> °C</string>
+      </property>
+      <property name="decimals">
+       <number>1</number>
+      </property>
+      <property name="minimum">
+       <double>1.000000000000000</double>
+      </property>
+      <property name="maximum">
+       <double>45.000000000000000</double>
+      </property>
+      <property name="singleStep">
+       <double>0.100000000000000</double>
+      </property>
+     </widget>
+     <widget class="QDoubleSpinBox" name="temphiEdit">
+      <property name="geometry">
+       <rect>
+        <x>570</x>
+        <y>40</y>
+        <width>111</width>
+        <height>24</height>
+       </rect>
+      </property>
+      <property name="alignment">
+       <set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
+      </property>
+      <property name="accelerated">
+       <bool>true</bool>
+      </property>
+      <property name="suffix">
+       <string> °C</string>
+      </property>
+      <property name="decimals">
+       <number>1</number>
+      </property>
+      <property name="minimum">
+       <double>1.000000000000000</double>
+      </property>
+      <property name="maximum">
+       <double>45.000000000000000</double>
+      </property>
+      <property name="singleStep">
+       <double>0.100000000000000</double>
+      </property>
+     </widget>
+     <widget class="QLabel" name="sensorLabel">
+      <property name="geometry">
+       <rect>
+        <x>10</x>
+        <y>70</y>
+        <width>91</width>
+        <height>20</height>
+       </rect>
+      </property>
+      <property name="text">
+       <string>Sensor:</string>
+      </property>
+      <property name="alignment">
+       <set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
+      </property>
+     </widget>
+     <widget class="QComboBox" name="sensorEdit">
+      <property name="geometry">
+       <rect>
+        <x>110</x>
+        <y>70</y>
+        <width>111</width>
+        <height>23</height>
+       </rect>
+      </property>
+     </widget>
+    </widget>
+   </item>
+  </layout>
+ </widget>
+ <tabstops>
+  <tabstop>nameEdit</tabstop>
+  <tabstop>quitButton</tabstop>
+  <tabstop>deleteButton</tabstop>
+  <tabstop>saveButton</tabstop>
+ </tabstops>
+ <resources>
+  <include location="../../../../../../home/mbroek/MyProjects/bmsapp/resources/icons.qrc"/>
+ </resources>
+ <connections/>
+</ui>
--- a/ui/MainWindow.ui	Tue Mar 15 17:00:54 2022 +0100
+++ b/ui/MainWindow.ui	Wed Mar 16 21:26:31 2022 +0100
@@ -80,7 +80,7 @@
     <addaction name="actionWater_profiles"/>
     <addaction name="actionMash_profiles"/>
     <addaction name="actionStyles_profiles"/>
-    <addaction name="actionFermentation_profiles"/>
+    <addaction name="actionFerments_profiles"/>
     <addaction name="separator"/>
     <addaction name="actionSetup"/>
    </widget>
@@ -324,7 +324,7 @@
     <string>Beer styles</string>
    </property>
   </action>
-  <action name="actionFermentation_profiles">
+  <action name="actionFerments_profiles">
    <property name="enabled">
     <bool>true</bool>
    </property>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ui/ProfileFerments.ui	Wed Mar 16 21:26:31 2022 +0100
@@ -0,0 +1,103 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<ui version="4.0">
+ <class>ProfileFerments</class>
+ <widget class="QDialog" name="ProfileFerments">
+  <property name="geometry">
+   <rect>
+    <x>0</x>
+    <y>0</y>
+    <width>1280</width>
+    <height>640</height>
+   </rect>
+  </property>
+  <property name="windowTitle">
+   <string>Dialog</string>
+  </property>
+  <layout class="QGridLayout" name="gridLayout">
+   <item row="0" column="0">
+    <widget class="QTableWidget" name="tableFerments">
+     <property name="enabled">
+      <bool>true</bool>
+     </property>
+    </widget>
+   </item>
+   <item row="1" column="0">
+    <widget class="QGroupBox" name="groupBox">
+     <property name="enabled">
+      <bool>true</bool>
+     </property>
+     <property name="flat">
+      <bool>false</bool>
+     </property>
+     <layout class="QHBoxLayout" name="horizontalLayout">
+      <property name="spacing">
+       <number>6</number>
+      </property>
+      <property name="leftMargin">
+       <number>0</number>
+      </property>
+      <property name="topMargin">
+       <number>0</number>
+      </property>
+      <property name="rightMargin">
+       <number>0</number>
+      </property>
+      <property name="bottomMargin">
+       <number>0</number>
+      </property>
+      <item alignment="Qt::AlignLeft">
+       <widget class="QPushButton" name="quitButton">
+        <property name="sizePolicy">
+         <sizepolicy hsizetype="Fixed" vsizetype="Fixed">
+          <horstretch>0</horstretch>
+          <verstretch>0</verstretch>
+         </sizepolicy>
+        </property>
+        <property name="minimumSize">
+         <size>
+          <width>80</width>
+          <height>24</height>
+         </size>
+        </property>
+        <property name="text">
+         <string>Quit</string>
+        </property>
+        <property name="icon">
+         <iconset>
+          <normaloff>:icons/silk/door_out.png</normaloff>:icons/silk/door_out.png</iconset>
+        </property>
+       </widget>
+      </item>
+      <item>
+       <widget class="QPushButton" name="insertButton">
+        <property name="sizePolicy">
+         <sizepolicy hsizetype="Fixed" vsizetype="Fixed">
+          <horstretch>0</horstretch>
+          <verstretch>0</verstretch>
+         </sizepolicy>
+        </property>
+        <property name="minimumSize">
+         <size>
+          <width>80</width>
+          <height>24</height>
+         </size>
+        </property>
+        <property name="text">
+         <string>New</string>
+        </property>
+        <property name="icon">
+         <iconset>
+          <normaloff>:icons/silk/table_row_insert.png</normaloff>:icons/silk/table_row_insert.png</iconset>
+        </property>
+       </widget>
+      </item>
+     </layout>
+    </widget>
+   </item>
+  </layout>
+ </widget>
+ <resources>
+  <include location="../../../../../../home/mbroek/MyProjects/bmsapp/resources/icons.qrc"/>
+ </resources>
+ <connections/>
+</ui>

mercurial