--- a/src/Setup.cpp Wed Feb 16 22:11:29 2022 +0100 +++ b/src/Setup.cpp Thu Feb 17 19:57:32 2022 +0100 @@ -23,11 +23,84 @@ Setup::Setup(QWidget *parent) : QDialog(parent), ui(new Ui::Setup) { + QSqlQuery query; + qDebug() << "Setup start"; - ui->setupUi(this); + setWindowTitle( QString("BMSapp - %1 - Setup").arg(VERSIONSTRING) ); - setWindowTitle( QString("BMSapp - %1 - Setup").arg(VERSIONSTRING) ); + query.prepare("SELECT * FROM profile_setup WHERE record='1'"); + query.exec(); + query.next(); + + ui->breweryEdit->setText(query.value(1).toString()); // max 128 + + ui->fwhEdit->setValue(query.value(4).toInt()); + ui->mashhopEdit->setValue(query.value(3).toInt()); + ui->pelletEdit->setValue(query.value(5).toInt()); + ui->hopplugEdit->setValue(query.value(6).toInt()); + ui->wethopEdit->setValue(query.value(7).toInt()); + ui->cryohopEdit->setValue(query.value(8).toInt()); + connect(ui->fwhEdit, &QSpinBox::textChanged, this, &Setup::is_changed); + connect(ui->mashhopEdit, &QSpinBox::textChanged, this, &Setup::is_changed); + connect(ui->pelletEdit, &QSpinBox::textChanged, this, &Setup::is_changed); + connect(ui->hopplugEdit, &QSpinBox::textChanged, this, &Setup::is_changed); + connect(ui->wethopEdit, &QSpinBox::textChanged, this, &Setup::is_changed); + connect(ui->cryohopEdit, &QSpinBox::textChanged, this, &Setup::is_changed); + + ui->grainEdit->setValue(query.value(12).toDouble()); + ui->brixEdit->setValue(query.value(11).toDouble()); + connect(ui->grainEdit, &QDoubleSpinBox::textChanged, this, &Setup::is_changed); + connect(ui->brixEdit, &QDoubleSpinBox::textChanged, this, &Setup::is_changed); + + ui->colorEdit->addItem(tr("Morey")); + ui->colorEdit->addItem(tr("Mosher")); + ui->colorEdit->addItem(tr("Daniels")); + ui->colorEdit->addItem(tr("Halberstadt")); + ui->colorEdit->addItem(tr("Naudts")); + ui->colorEdit->setEditable(true); + ui->colorEdit->setCurrentIndex(query.value(10).toInt()); + connect(ui->colorEdit, &QComboBox::currentTextChanged, this, &Setup::is_changed); + + ui->ibuEdit->addItem(tr("Tinseth")); + ui->ibuEdit->addItem(tr("Rager")); + ui->ibuEdit->addItem(tr("Daniels")); + ui->ibuEdit->setEditable(true); + ui->ibuEdit->setCurrentIndex(query.value(9).toInt()); + connect(ui->ibuEdit, &QComboBox::currentTextChanged, this, &Setup::is_changed); + + QSqlQuery query2("SELECT record,name FROM inventory_waters"); + query2.first(); + int pos = -1; + ui->waterEdit->setEditable(true); + ui->waterEdit->setPlaceholderText(tr("Choose default water")); + for (int i = 0 ; i < query2.size() ; i++ ) { + ui->waterEdit->addItem(query2.value(1).toString()); + if (query2.value(0).toInt() == query.value(13).toInt()) { + pos = i; + } + query2.next(); + } + if (pos >= 0) + ui->waterEdit->setCurrentIndex(pos); + connect(ui->waterEdit, &QComboBox::currentTextChanged, this, &Setup::is_changed); + + QSqlQuery query3("SELECT DISTINCT laboratory FROM inventory_yeasts ORDER BY laboratory"); + query3.first(); + pos = -1; + ui->yeastEdit->setEditable(true); + ui->yeastEdit->setPlaceholderText(tr("Choose laboratory")); + for (int i = 0 ; i < query3.size() ; i++ ) { + ui->yeastEdit->addItem(query3.value(0).toString()); + if (QString::compare(query.value(14).toString(), query3.value(0).toString(), Qt::CaseSensitive) == 0) + pos = i; + query3.next(); + } + if (pos >= 0) + ui->yeastEdit->setCurrentIndex(pos); + connect(ui->yeastEdit, &QComboBox::currentTextChanged, this, &Setup::is_changed); + + // query.value(2).toString() logo varchar(1024) } @@ -38,8 +111,79 @@ } +/* + * Also called from the Quit button if there are changes to save. + */ +void Setup::on_saveButton_clicked() +{ + QSqlQuery query; + + /* + * Search record number of the current water. + */ + query.prepare("SELECT record FROM inventory_waters WHERE name=:name"); + query.bindValue(":name", ui->waterEdit->currentText()); + query.exec(); + query.first(); + int record = query.value(0).toInt(); + + /* + * Update all other data + */ + query.prepare("UPDATE profile_setup SET brewery_name=:brewery, factor_mashhop=:mashhop, factor_fwh=:fwh, factor_pellet=:pellet, " + "factor_plug=:plug, factor_wethop=:wet, factor_cryohop=:cryo, color_method=:color, ibu_method=:ibu, " + "brix_correction=:brix, grain_absorbtion=:grain, default_water=:water, my_yeastlab=:yeast WHERE record='1'"); + query.bindValue(":brewery", ui->breweryEdit->text()); + query.bindValue(":mashhop", ui->mashhopEdit->value()); + query.bindValue(":fwh", ui->fwhEdit->value()); + query.bindValue(":pellet", ui->pelletEdit->value()); + query.bindValue(":plug", ui->hopplugEdit->value()); + query.bindValue(":wet", ui->wethopEdit->value()); + query.bindValue(":cryo", ui->cryohopEdit->value()); + query.bindValue(":color", ui->colorEdit->currentIndex()); + query.bindValue(":ibu", ui->ibuEdit->currentIndex()); + query.bindValue(":brix", ui->brixEdit->value()); + query.bindValue(":grain", ui->grainEdit->value()); + query.bindValue(":water", record); + query.bindValue(":yeast", ui->yeastEdit->currentText()); + query.exec(); + if (query.lastError().isValid()) { + qDebug() << "Setup Save error:" << 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() << "Setup Saved"; + } + + this->fieldIsChanged = false; + setWindowTitle( QString("BMSapp - %1 - Setup").arg(VERSIONSTRING) ); +} + + void Setup::on_quitButton_clicked() { + if (this->fieldIsChanged) { + int rc = QMessageBox::warning(this, tr("Setup changed"), tr("The setup has been modified\n 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 setup page */ + } + } emit firstWindow(); } + +void Setup::is_changed() +{ + this->fieldIsChanged = true; + setWindowTitle( QString("BMSapp - %1 - Setup **").arg(VERSIONSTRING) ); +}