src/Setup.cpp

Thu, 17 Mar 2022 13:54:39 +0100

author
Michiel Broek <mbroek@mbse.eu>
date
Thu, 17 Mar 2022 13:54:39 +0100
changeset 60
0d65238ebedc
parent 41
dc4b659a320b
child 78
3a6cba2dd05d
permissions
-rw-r--r--

Updated translations and some messages.

/**
 * Setup.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 "Setup.h"
#include "../ui/ui_Setup.h"
#include "config.h"
#include "bmsapp.h"



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) );

    query.prepare("SELECT * FROM profile_setup WHERE record='1'");
    query.exec();
    query.next();

    ui->breweryEdit->setText(query.value(1).toString()); // max 128
    connect(ui->breweryEdit, &QLineEdit::textChanged, this, &Setup::is_changed);

    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("Morey");
    ui->colorEdit->addItem("Mosher");
    ui->colorEdit->addItem("Daniels");
    ui->colorEdit->addItem("Halberstadt");
    ui->colorEdit->addItem("Naudts");
    ui->colorEdit->setEditable(true);
    ui->colorEdit->setCurrentIndex(query.value(10).toInt());
    connect(ui->colorEdit, &QComboBox::currentTextChanged, this, &Setup::is_changed);

    ui->ibuEdit->addItem("Tinseth");
    ui->ibuEdit->addItem("Rager");
    ui->ibuEdit->addItem("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);

    /* logo */
    logoByteArray = query.value(2).toByteArray();
    QPixmap outPixmap = QPixmap();
    outPixmap.loadFromData(logoByteArray);
    ui->logoLabel->setPixmap(outPixmap);
    ui->logoLabel->adjustSize();
}


Setup::~Setup()
{
    qDebug() << "Setup done";
    delete ui;
}


bool Setup::loadFile(const QString &fileName)
{
    QImageReader reader(fileName);
    reader.setAutoTransform(true);
    const QImage newImage = reader.read();
    if (newImage.isNull()) {
        QMessageBox::information(this, QGuiApplication::applicationDisplayName(), tr("Cannot load %1: %2")
                                 .arg(QDir::toNativeSeparators(fileName), reader.errorString()));
        return false;
    }
    setImage(newImage);
    setWindowFilePath(fileName);
    is_changed();
    return true;
}


void Setup::setImage(const QImage &newImage)
{
    image = newImage;

    qDebug() << "setImage" << image.width() << image.height() << "size" << image.sizeInBytes();

    QBuffer buffer(&logoByteArray);
    buffer.open(QIODevice::WriteOnly);
    image.save(&buffer, "PNG"); // writes image into logoByteArray in PNG format

    ui->logoLabel->setPixmap(QPixmap::fromImage(image));
    scaleFactor = 1.0;

//    ui->logoLabel->resize(scaleFactor * ui->logoLabel->pixmap(Qt::ReturnByValue).size());
    ui->logoLabel->adjustSize();
}


void Setup::on_openButton_clicked()
{
    static bool firstDialog = true;

    qDebug() << "Setup open";

    QFileDialog dialog(this, tr("Open File"));

    if (firstDialog) {
        firstDialog = false;
        const QStringList picturesLocations = QStandardPaths::standardLocations(QStandardPaths::PicturesLocation);
        dialog.setDirectory(picturesLocations.isEmpty() ? QDir::currentPath() : picturesLocations.last());
    }

    /* Only a few image formats are valid */
    QStringList mimeTypeFilters ({ "image/bmp", "image/gif", "image/jpeg", "image/png", "image/svg+xml" });
    dialog.setMimeTypeFilters(mimeTypeFilters);
    dialog.setNameFilter("Images (*.bmp *.BMP *.gif *.GIF *.jpg *.JPG *.png *.PNG *.svg *.SVG)");
    dialog.setAcceptMode(QFileDialog::AcceptOpen);

    while (dialog.exec() == QDialog::Accepted && !loadFile(dialog.selectedFiles().constFirst())) {}
}


/*
 * 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, brewery_logo=:logo, 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(":logo", logoByteArray);
    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 global setup 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 setup page */
	}
    }
    emit firstWindow();
}


void Setup::is_changed()
{
    this->fieldIsChanged = true;
    setWindowTitle( QString("BMSapp - %1 - Setup **").arg(VERSIONSTRING) );
}

mercurial