# HG changeset patch # User Michiel Broek # Date 1647462391 -3600 # Node ID 75d11cc05ce4e86337a2e2a0f1afb208c196a587 # Parent eb6c564192f41d2205e43e1c0fc0bc5983f02244 Added profile fermentation tables and editor. diff -r eb6c564192f4 -r 75d11cc05ce4 CMakeLists.txt --- 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 ) diff -r eb6c564192f4 -r 75d11cc05ce4 src/EditProfileFerment.cpp --- /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 . + */ +#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(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(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(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); +} diff -r eb6c564192f4 -r 75d11cc05ce4 src/EditProfileFerment.h --- /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 +#include + +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 diff -r eb6c564192f4 -r 75d11cc05ce4 src/MainWindow.cpp --- 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; diff -r eb6c564192f4 -r 75d11cc05ce4 src/MainWindow.h --- 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 @@ -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; }; diff -r eb6c564192f4 -r 75d11cc05ce4 src/ProfileFerments.cpp --- /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 . + */ +#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(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(); +} + diff -r eb6c564192f4 -r 75d11cc05ce4 src/ProfileFerments.h --- /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 + +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 diff -r eb6c564192f4 -r 75d11cc05ce4 src/Utils.cpp --- 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"); +} + + diff -r eb6c564192f4 -r 75d11cc05ce4 src/Utils.h --- 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 + /** * @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 diff -r eb6c564192f4 -r 75d11cc05ce4 ui/EditProfileFerment.ui --- /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 @@ + + + EditProfileFerment + + + + 0 + 0 + 1024 + 560 + + + + Dialog + + + + + + + + 10 + 10 + 91 + 20 + + + + Name: + + + Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter + + + + + + 110 + 10 + 571 + 23 + + + + 128 + + + Name of the fermentation profile + + + + + + 110 + 510 + 80 + 23 + + + + + 0 + 0 + + + + Quit + + + + :icons/silk/door_out.png:icons/silk/door_out.png + + + + + false + + + + 820 + 510 + 80 + 23 + + + + Save + + + + :icons/silk/disk.png:icons/silk/disk.png + + + + + false + + + + 463 + 510 + 80 + 23 + + + + Delete + + + + :icons/silk/delete.png:icons/silk/delete.png + + + + + + 110 + 130 + 791 + 351 + + + + + + + 10 + 130 + 91 + 20 + + + + Steps: + + + Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter + + + + + + 10 + 100 + 91 + 20 + + + + Total time: + + + Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter + + + + + + 110 + 100 + 171 + 23 + + + + true + + + + + + 820 + 100 + 80 + 23 + + + + Add step + + + + :/icons/silk/add.png:/icons/silk/add.png + + + + + + 10 + 40 + 91 + 20 + + + + Start low: + + + Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter + + + + + + 470 + 40 + 91 + 20 + + + + Start high: + + + Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter + + + + + + 110 + 40 + 111 + 24 + + + + Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter + + + true + + + °C + + + 1 + + + 1.000000000000000 + + + 45.000000000000000 + + + 0.100000000000000 + + + + + + 570 + 40 + 111 + 24 + + + + Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter + + + true + + + °C + + + 1 + + + 1.000000000000000 + + + 45.000000000000000 + + + 0.100000000000000 + + + + + + 10 + 70 + 91 + 20 + + + + Sensor: + + + Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter + + + + + + 110 + 70 + 111 + 23 + + + + + + + + + nameEdit + quitButton + deleteButton + saveButton + + + + + + diff -r eb6c564192f4 -r 75d11cc05ce4 ui/MainWindow.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 @@ - + @@ -324,7 +324,7 @@ Beer styles - + true diff -r eb6c564192f4 -r 75d11cc05ce4 ui/ProfileFerments.ui --- /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 @@ + + + ProfileFerments + + + + 0 + 0 + 1280 + 640 + + + + Dialog + + + + + + true + + + + + + + true + + + false + + + + 6 + + + 0 + + + 0 + + + 0 + + + 0 + + + + + + 0 + 0 + + + + + 80 + 24 + + + + Quit + + + + :icons/silk/door_out.png:icons/silk/door_out.png + + + + + + + + 0 + 0 + + + + + 80 + 24 + + + + New + + + + :icons/silk/table_row_insert.png:icons/silk/table_row_insert.png + + + + + + + + + + + + +