Mon, 18 Apr 2022 10:55:33 +0200
Upgrade yeasts fields if needed during recipe startup. Added yeast table.
/** * EditRecipe.cpp is part of bmsapp. * * tab 5, yeasts. * * 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/>. */ bool EditRecipe::yeast_sort_test(const Yeasts &D1, const Yeasts &D2) { if (D1.y_use > D2.y_use) return false; if (D1.y_use < D2.y_use) return true; return (D1.y_amount > D2.y_amount); } void EditRecipe::refreshYeasts() { QString w; QWidget* pWidget; QHBoxLayout* pLayout; QTableWidgetItem *item; qDebug() << "refreshYeasts" << recipe->yeasts.size(); std::sort(recipe->yeasts.begin(), recipe->yeasts.end(), yeast_sort_test); /* * 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("Yeast"), tr("Laboratory"), tr("Code"), tr("Type"), tr("Use for"), tr("Min. °C"), tr("Max. °C"), tr("Tol. %"), tr("Attn. %"), tr("Amount"), tr("Delete"), tr("Edit") }); ui->yeastsTable->setColumnCount(12); ui->yeastsTable->setColumnWidth(0, 200); /* Yeast */ ui->yeastsTable->setColumnWidth(1, 125); /* Laboratory */ ui->yeastsTable->setColumnWidth(2, 80); /* Code */ ui->yeastsTable->setColumnWidth(3, 80); /* Type */ ui->yeastsTable->setColumnWidth(4, 100); /* Usage */ ui->yeastsTable->setColumnWidth(5, 60); /* Min. */ ui->yeastsTable->setColumnWidth(6, 60); /* Max. */ ui->yeastsTable->setColumnWidth(7, 60); /* Tolerance */ ui->yeastsTable->setColumnWidth(8, 60); /* Attenuation */ ui->yeastsTable->setColumnWidth(9, 90); /* Amount */ ui->yeastsTable->setColumnWidth(10, 80); /* Delete */ ui->yeastsTable->setColumnWidth(11, 80); /* Edit */ ui->yeastsTable->setHorizontalHeaderLabels(labels); ui->yeastsTable->verticalHeader()->hide(); ui->yeastsTable->setRowCount(recipe->yeasts.size()); for (int i = 0; i < recipe->yeasts.size(); i++) { ui->yeastsTable->setItem(i, 0, new QTableWidgetItem(recipe->yeasts.at(i).y_name)); ui->yeastsTable->setItem(i, 1, new QTableWidgetItem(recipe->yeasts.at(i).y_laboratory)); ui->yeastsTable->setItem(i, 2, new QTableWidgetItem(recipe->yeasts.at(i).y_product_id)); item = new QTableWidgetItem(y_types[recipe->yeasts.at(i).y_type]); item->setTextAlignment(Qt::AlignCenter|Qt::AlignVCenter); ui->yeastsTable->setItem(i, 3, item); item = new QTableWidgetItem(y_use[recipe->yeasts.at(i).y_use]); item->setTextAlignment(Qt::AlignCenter|Qt::AlignVCenter); ui->yeastsTable->setItem(i, 4, item); item = new QTableWidgetItem(QString("%1").arg(recipe->yeasts.at(i).y_min_temperature, 2, 'f', 1, '0')); item->setTextAlignment(Qt::AlignRight|Qt::AlignVCenter); ui->yeastsTable->setItem(i, 5, item); item = new QTableWidgetItem(QString("%1").arg(recipe->yeasts.at(i).y_max_temperature, 2, 'f', 1, '0')); item->setTextAlignment(Qt::AlignRight|Qt::AlignVCenter); ui->yeastsTable->setItem(i, 6, item); item = new QTableWidgetItem(QString("%1").arg(recipe->yeasts.at(i).y_tolerance, 2, 'f', 1, '0')); item->setTextAlignment(Qt::AlignRight|Qt::AlignVCenter); ui->yeastsTable->setItem(i, 7, item); item = new QTableWidgetItem(QString("%1").arg(recipe->yeasts.at(i).y_attenuation, 2, 'f', 1, '0')); item->setTextAlignment(Qt::AlignRight|Qt::AlignVCenter); ui->yeastsTable->setItem(i, 8, item); if (recipe->yeasts.at(i).y_type == 0) item = new QTableWidgetItem(QString("%1 pack").arg(recipe->yeasts.at(i).y_amount * 1000.0, 3, 'f', 2, '0')); else if (recipe->yeasts.at(i).y_type == 1) item = new QTableWidgetItem(QString("%1 gr").arg(recipe->yeasts.at(i).y_amount * 1000.0, 3, 'f', 2, '0')); else item = new QTableWidgetItem(QString("%1 ml").arg(recipe->yeasts.at(i).y_amount * 1000.0, 3, 'f', 2, '0')); item->setTextAlignment(Qt::AlignRight|Qt::AlignVCenter); ui->yeastsTable->setItem(i, 9, item); pWidget = new QWidget(); QPushButton* btn_dele = new QPushButton(); btn_dele->setObjectName(QString("%1").arg(i)); /* Send row with the button */ btn_dele->setText(tr("Delete")); connect(btn_dele, SIGNAL(clicked()), this, SLOT(deleteYeastRow_clicked())); pLayout = new QHBoxLayout(pWidget); pLayout->addWidget(btn_dele); pLayout->setContentsMargins(5, 0, 5, 0); pWidget->setLayout(pLayout); ui->yeastsTable->setCellWidget(i, 10, pWidget); pWidget = new QWidget(); QPushButton* btn_edit = new QPushButton(); btn_edit->setObjectName(QString("%1").arg(i)); /* Send row with the button */ btn_edit->setText(tr("Edit")); connect(btn_edit, SIGNAL(clicked()), this, SLOT(editYeastRow_clicked())); pLayout = new QHBoxLayout(pWidget); pLayout->addWidget(btn_edit); pLayout->setContentsMargins(5, 0, 5, 0); pWidget->setLayout(pLayout); ui->yeastsTable->setCellWidget(i, 11, pWidget); } this->ignoreChanges = false; } void EditRecipe::addYeastRow_clicked() { } void EditRecipe::deleteYeastRow_clicked() { QPushButton *pb = qobject_cast<QPushButton *>(QObject::sender()); int row = pb->objectName().toInt(); qDebug() << "Delete yeast row" << row << recipe->yeasts.size(); if (recipe->yeasts.size() < 1) return; int rc = QMessageBox::warning(this, tr("Delete yeast"), tr("Delete %1").arg(recipe->yeasts.at(row).y_name), QMessageBox::Yes | QMessageBox::No, QMessageBox::No); if (rc == QMessageBox::No) return; this->ignoreChanges = true; recipe->yeasts.removeAt(row); this->ignoreChanges = false; is_changed(); emit refreshAll(); } void EditRecipe::editYeastRow_clicked() { QSqlQuery query; QPushButton *pb = qobject_cast<QPushButton *>(QObject::sender()); recipe->yeasts_row = pb->objectName().toInt(); qDebug() << "Edit yeast row" << recipe->yeasts_row; Yeasts backup = recipe->yeasts.at(recipe->yeasts_row); QDialog* dialog = new QDialog(this); dialog->resize(738, 230); QDialogButtonBox *buttonBox = new QDialogButtonBox(dialog); buttonBox->setObjectName(QString::fromUtf8("buttonBox")); buttonBox->setGeometry(QRect(30, 180, 671, 32)); buttonBox->setLayoutDirection(Qt::LeftToRight); buttonBox->setOrientation(Qt::Horizontal); buttonBox->setStandardButtons(QDialogButtonBox::Cancel|QDialogButtonBox::Ok); buttonBox->setCenterButtons(true); connect(buttonBox, SIGNAL(rejected()), dialog, SLOT(reject())); connect(buttonBox, SIGNAL(accepted()), dialog, SLOT(accept())); disconnect(buttonBox, nullptr, nullptr, nullptr); emit refreshAll(); }