src/MainWindow.cpp

Fri, 22 Apr 2022 13:46:59 +0200

author
Michiel Broek <mbroek@mbse.eu>
date
Fri, 22 Apr 2022 13:46:59 +0200
changeset 152
58e4ce7dd217
parent 145
fd4f0de86fd9
child 173
8514932b61aa
permissions
-rw-r--r--

Fixed vanishing mash profiles from some recipes. All ingnoreChanges flags removed and replaced by blocking signals. Update prompts and yeast amounts depending on the yeast form. Save water profile names fixed.

/**
 * MainWindow.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 "MainWindow.h"
#include "AboutDialog.h"
#include "RecipesTree.h"
#include "InventorySuppliers.h"
#include "InventoryFermentables.h"
#include "InventoryHops.h"
#include "InventoryYeasts.h"
#include "InventoryMiscs.h"
#include "InventoryWaters.h"
#include "InventoryEquipments.h"
#include "ProfileWaters.h"
#include "ProfileMashs.h"
#include "ProfileStyles.h"
#include "ProfileFerments.h"
#include "Setup.h"
#include "PrinterDialog.h"
#include "../ui/ui_MainWindow.h"
#include "database/database.h"
#include "config.h"
#include "global.h"

#include <QApplication>
#include <QCloseEvent>
#include <QDebug>
#include <QStandardItem>
#include <QWidget>
#include <QtWidgets/QTableWidget>
#include <QtWidgets/QGroupBox>
#include <QtWidgets/QPushButton>
#include <QUrl>


MainWindow::MainWindow(bool useDevelopOption, bool startConfigOption, QWidget *parent) : QMainWindow(parent),  ui(new Ui::MainWindow)
{
    qDebug() << Q_FUNC_INFO << useDevelopOption << startConfigOption;
    ui->setupUi(this);
    setWindowTitle( QString("BMSapp - %1").arg(VERSIONSTRING) );

    readsettings();
    db = new DataBase();
    db->openDataBase(useDevelopOption);

    loadSetup();
    openWS(useDevelopOption);

    Acid a;
    a.name_en = "Lactic";
    a.name_nl = "Melkzuur";
    a.pK1 = 3.86;
    a.pK2 = 20.0;
    a.pK3 = 20.0;
    a.MolWt = 90.08;
    a.AcidSG = 1238.0;
    a.AcidPrc = 80.0;
    my_acids.append(a);
    a.name_en = "Hydrochloric";
    a.name_nl = "Zoutzuur";
    a.pK1 = -7.0;
    a.pK2 = 20.0;
    a.pK3 = 20.0;
    a.MolWt = 36.46;
    a.AcidSG = 1497.0;
    a.AcidPrc = 28.0;
    my_acids.append(a);
    a.name_en = "Phosphoric";
    a.name_nl = "Fosforzuur";
    a.pK1 = 2.12;
    a.pK2 = 7.20;
    a.pK3 = 12.44;
    a.MolWt = 98.00;
    a.AcidSG = 1982.0;
    a.AcidPrc = 75.0;
    my_acids.append(a);
    a.name_en = "Sulfuric";
    a.name_nl = "Zwavelzuur";
    a.pK1 = -1.0;
    a.pK2 = 1.92;
    a.pK3 = 20.0;
    a.MolWt = 98.07;
    a.AcidSG = 1884.0;
    a.AcidPrc = 93.0;
    my_acids.append(a);
}


MainWindow::~MainWindow()
{
    qDebug() << Q_FUNC_INFO;

    writesettings();
    webSocket->close(QWebSocketProtocol::CloseCodeNormal, "");
    db->closeDataBase();
    delete ui;
}



void MainWindow::loadSetup()
{
    /*
     * Load dedaults from the setup.
     */
    QSqlQuery query("SELECT * FROM profile_setup WHERE record='1'");
    query.first();
    my_brewery_name = query.value(1).toString();
    my_logoByteArray = query.value(2).toByteArray();
    my_factor_mashhop = query.value(3).toInt();
    my_factor_fwh = query.value(4).toInt();
    my_factor_pellet = query.value(5).toInt();
    my_factor_plug = query.value(6).toInt();
    my_factor_wethop = query.value(7).toInt();
    my_factor_cryohop = query.value(8).toInt();
    my_ibu_method = query.value(9).toInt();
    my_color_method = query.value(10).toInt();
    my_brix_correction = query.value(11).toDouble();
    my_grain_absorbtion = query.value(12).toDouble();
    my_default_water = query.value(13).toInt();
    my_yeastlab = query.value(14).toString();

    qDebug() << "loadSetup" << my_brewery_name;
}


bool MainWindow::openWS(bool develop)
{
    QString server;
    if (develop)
	server = wsDev.host;
    else
	server = wsProd.host;
    QUrl url(QString("ws://%1/ws").arg(server));
    qDebug() << "Open websocket:" << url;

    webSocket = new QWebSocket;
    QObject::connect(webSocket, &QWebSocket::connected, this, &MainWindow::wsConnected);
    QObject::connect(webSocket, &QWebSocket::disconnected, this, &MainWindow::wsClosed);

    webSocket->open(QUrl(url));
    return true;
}


void MainWindow::wsConnected()
{
    qDebug() << Q_FUNC_INFO;

    connect(webSocket, &QWebSocket::textMessageReceived, this, &MainWindow::wsTextMessageReceived);
}


void MainWindow::wsClosed()
{
    qDebug() << Q_FUNC_INFO << webSocket->closeReason();

    // Should triger a periodic timer to try to reconnect.
}


void MainWindow::wsTextMessageReceived(QString message)
{
//    qDebug() << "WS received:" << message;
}


void MainWindow::readsettings()
{
    QSettings settings(QSettings::IniFormat, QSettings::UserScope, "mbse", "bmsapp");

    settings.beginGroup("dbprod");
    dbProd.host = settings.value("host").toString();
    if (dbProd.host.isEmpty()) {
        dbProd.host = "localhost";
        dbProd.port = "3306";
        dbProd.name = "bms_prod";
        dbProd.user = "nobody";
        dbProd.pass = "secret";
        dbProd.charset = "utf8";
        settings.setValue("host", dbProd.host);
        settings.setValue("port", dbProd.port);
        settings.setValue("name", dbProd.name);
        settings.setValue("user", dbProd.user);
        settings.setValue("pass", dbProd.pass);
        settings.setValue("charset", dbProd.charset);
    } else {
        dbProd.port = settings.value("port").toString();
        dbProd.name = settings.value("name").toString();
        dbProd.user = settings.value("user").toString();
        dbProd.pass = settings.value("pass").toString();
        dbProd.charset = settings.value("charset").toString();
    }
    settings.endGroup();
    qDebug() << "MySQL prod" << dbProd.host << dbProd.port << dbProd.name << dbProd.pass;

    settings.beginGroup("dbdev");
    dbDev.host = settings.value("host").toString();
    if (dbDev.host.isEmpty()) {
        dbDev.host = "localhost";
        dbDev.port = "3306";
        dbDev.name = "bms_dev";
        dbDev.user = "nobody";
        dbDev.pass = "secret";
        dbDev.charset = "utf8";
        settings.setValue("host", dbDev.host);
        settings.setValue("port", dbDev.port);
        settings.setValue("name", dbDev.name);
        settings.setValue("user", dbDev.user);
        settings.setValue("pass", dbDev.pass);
        settings.setValue("charset", dbDev.charset);
    } else {
        dbDev.port = settings.value("port").toString();
        dbDev.name = settings.value("name").toString();
        dbDev.user = settings.value("user").toString();
        dbDev.pass = settings.value("pass").toString();
        dbDev.charset = settings.value("charset").toString();
    }
    settings.endGroup();
    qDebug() << "MySQL dev" << dbDev.host << dbDev.port << dbDev.name << dbDev.pass;

    settings.beginGroup("wsprod");
    wsProd.host = settings.value("host").toString();
    if (wsProd.host.isEmpty()) {
        wsProd.host = "localhost";
        settings.setValue("host", wsProd.host);
    }
    settings.endGroup();
    qDebug() << "WS prod" << wsProd.host;

    settings.beginGroup("wsdev");
    wsDev.host = settings.value("host").toString();
    if (wsDev.host.isEmpty()) {
        wsDev.host = "localhost";
        settings.setValue("host", wsDev.host);
    }
    settings.endGroup();
    qDebug() << "WS dev" << wsDev.host;
}


void MainWindow::writesettings()
{
    QSettings settings(QSettings::IniFormat, QSettings::UserScope, "mbse", "bmsapp");

    settings.beginGroup("dbprod");
    settings.setValue("host", dbProd.host);
    settings.setValue("port", dbProd.port);
    settings.setValue("name", dbProd.name);
    settings.setValue("user", dbProd.user);
    settings.setValue("pass", dbProd.pass);
    settings.setValue("charset", dbProd.charset);
    settings.endGroup();

    settings.beginGroup("dbdev");
    settings.setValue("host", dbDev.host);
    settings.setValue("port", dbDev.port);
    settings.setValue("name", dbDev.name);
    settings.setValue("user", dbDev.user);
    settings.setValue("pass", dbDev.pass);
    settings.setValue("charset", dbDev.charset);
    settings.endGroup();

    settings.beginGroup("wsprod");
    settings.setValue("host", wsProd.host);
    settings.endGroup();

    settings.beginGroup("wsdev");
    settings.setValue("host", wsDev.host);
    settings.endGroup();

    qDebug() << "writesettings() done.";
}


void MainWindow::on_actionExit_triggered()
{
    qDebug() << Q_FUNC_INFO;
    this->close();
}


void MainWindow::fromRecipesTree()
{
    qDebug() << Q_FUNC_INFO;
    ui->mainStack->setCurrentIndex(-1);
    ui->mainStack->removeWidget(RecipesTreeWindow);
    delete RecipesTreeWindow;
    setWindowTitle( QString("BMSapp - %1").arg(VERSIONSTRING) );
    ui->menuBar->setVisible(true);
}


void MainWindow::on_actionRecipes_triggered()
{
    qDebug() << Q_FUNC_INFO;

    RecipesTreeWindow = new RecipesTree(this);
    int index = ui->mainStack->count();
    ui->mainStack->addWidget(RecipesTreeWindow);
    ui->mainStack->setCurrentIndex(index);
    setWindowTitle( QString("BMSapp - %1 - Recipes").arg(VERSIONSTRING));
    ui->menuBar->setVisible(false);
}


void MainWindow::fromInventorySuppliers()
{
    qDebug() << Q_FUNC_INFO;
    ui->mainStack->setCurrentIndex(-1);
    ui->mainStack->removeWidget(InventorySuppliersWindow);
    delete InventorySuppliersWindow;
    setWindowTitle( QString("BMSapp - %1").arg(VERSIONSTRING) );
    ui->menuBar->setVisible(true);
}


void MainWindow::on_actionSuppliers_triggered()
{
    qDebug() << Q_FUNC_INFO;

    InventorySuppliersWindow = new InventorySuppliers(this);
    int index = ui->mainStack->count();
    ui->mainStack->addWidget(InventorySuppliersWindow);
    ui->mainStack->setCurrentIndex(index);
    setWindowTitle( QString("BMSapp - %1 - Inventory Suppliers").arg(VERSIONSTRING));
    ui->menuBar->setVisible(false);
}


void MainWindow::fromInventoryFermentables()
{
    qDebug() << Q_FUNC_INFO;
    ui->mainStack->setCurrentIndex(-1);
    ui->mainStack->removeWidget(InventoryFermentablesWindow);
    delete InventoryFermentablesWindow;
    setWindowTitle( QString("BMSapp - %1").arg(VERSIONSTRING) );
    ui->menuBar->setVisible(true);
    statusBar()->clearMessage();
}


void MainWindow::on_actionFermentables_triggered()
{
    qDebug() << Q_FUNC_INFO;

    InventoryFermentablesWindow = new InventoryFermentables(this);
    int index = ui->mainStack->count();
    ui->mainStack->addWidget(InventoryFermentablesWindow);
    ui->mainStack->setCurrentIndex(index);
    setWindowTitle(QString("BMSapp - %1 - Inventory Fermentables").arg(VERSIONSTRING));
    ui->menuBar->setVisible(false);
}


void MainWindow::fromInventoryHops()
{
    qDebug() << Q_FUNC_INFO;
    ui->mainStack->setCurrentIndex(-1);
    ui->mainStack->removeWidget(InventoryHopsWindow);
    delete InventoryHopsWindow;
    setWindowTitle( QString("BMSapp - %1").arg(VERSIONSTRING) );
    ui->menuBar->setVisible(true);
    statusBar()->clearMessage();
}


void MainWindow::on_actionHops_triggered()
{
    qDebug() << Q_FUNC_INFO;
    InventoryHopsWindow = new InventoryHops(this);
    int index = ui->mainStack->count();
    ui->mainStack->addWidget(InventoryHopsWindow);
    ui->mainStack->setCurrentIndex(index);
    setWindowTitle(QString("BMSapp - %1 - Inventory Hops").arg(VERSIONSTRING));
    ui->menuBar->setVisible(false);
}


void MainWindow::fromInventoryYeasts()
{
    qDebug() << Q_FUNC_INFO;
    ui->mainStack->setCurrentIndex(-1);
    ui->mainStack->removeWidget(InventoryYeastsWindow);
    delete InventoryYeastsWindow;
    setWindowTitle( QString("BMSapp - %1").arg(VERSIONSTRING) );
    ui->menuBar->setVisible(true);
    statusBar()->clearMessage();
}


void MainWindow::on_actionYeasts_triggered()
{
    qDebug() << Q_FUNC_INFO;
    InventoryYeastsWindow = new InventoryYeasts(this);
    int index = ui->mainStack->count();
    ui->mainStack->addWidget(InventoryYeastsWindow);
    ui->mainStack->setCurrentIndex(index);
    setWindowTitle(QString("BMSapp - %1 - Inventory Yeasts").arg(VERSIONSTRING));
    ui->menuBar->setVisible(false);
}


void MainWindow::fromInventoryMiscs()
{
    qDebug() << Q_FUNC_INFO;
    ui->mainStack->setCurrentIndex(-1);
    ui->mainStack->removeWidget(InventoryMiscsWindow);
    delete InventoryMiscsWindow;
    setWindowTitle( QString("BMSapp - %1").arg(VERSIONSTRING) );
    ui->menuBar->setVisible(true);
    statusBar()->clearMessage();
}


void MainWindow::on_actionMiscs_triggered()
{
    qDebug() << Q_FUNC_INFO;
    InventoryMiscsWindow = new InventoryMiscs(this);
    int index = ui->mainStack->count();
    ui->mainStack->addWidget(InventoryMiscsWindow);
    ui->mainStack->setCurrentIndex(index);
    setWindowTitle(QString("BMSapp - %1 - Inventory Miscs").arg(VERSIONSTRING));
    ui->menuBar->setVisible(false);
}


void MainWindow::fromInventoryWaters()
{
    qDebug() << Q_FUNC_INFO;
    ui->mainStack->setCurrentIndex(-1);
    ui->mainStack->removeWidget(InventoryWatersWindow);
    delete InventoryWatersWindow;
    setWindowTitle( QString("BMSapp - %1").arg(VERSIONSTRING) );
    ui->menuBar->setVisible(true);
    statusBar()->clearMessage();
}


void MainWindow::on_actionWaters_triggered()
{
    qDebug() << Q_FUNC_INFO;
    InventoryWatersWindow = new InventoryWaters(this);
    int index = ui->mainStack->count();
    ui->mainStack->addWidget(InventoryWatersWindow);
    ui->mainStack->setCurrentIndex(index);
    setWindowTitle(QString("BMSapp - %1 - Inventory Waters").arg(VERSIONSTRING));
    ui->menuBar->setVisible(false);
}


void MainWindow::fromInventoryEquipments()
{
    qDebug() << Q_FUNC_INFO;
    ui->mainStack->setCurrentIndex(-1);
    ui->mainStack->removeWidget(InventoryEquipmentsWindow);
    delete InventoryEquipmentsWindow;
    setWindowTitle( QString("BMSapp - %1").arg(VERSIONSTRING) );
    ui->menuBar->setVisible(true);
    statusBar()->clearMessage();
}


void MainWindow::on_actionEquipments_triggered()
{
    qDebug() << Q_FUNC_INFO;
    InventoryEquipmentsWindow = new InventoryEquipments(this);
    int index = ui->mainStack->count();
    ui->mainStack->addWidget(InventoryEquipmentsWindow);
    ui->mainStack->setCurrentIndex(index);
    setWindowTitle(QString("BMSapp - %1 - Inventory Equipments").arg(VERSIONSTRING));
    ui->menuBar->setVisible(false);
}


void MainWindow::on_actionSupplies_list_triggered()
{
    qDebug() << Q_FUNC_INFO;
    PrinterDialog(PR_SUPPLIES, -1, this);
}


void MainWindow::on_actionYeast_bank_triggered()
{
    qDebug() << Q_FUNC_INFO;
    PrinterDialog(PR_YEASTBANK, -1, this);
}


void MainWindow::fromProfileWaters()
{
    qDebug() << Q_FUNC_INFO;
    ui->mainStack->setCurrentIndex(-1);
    ui->mainStack->removeWidget(ProfileWatersWindow);
    delete ProfileWatersWindow;
    setWindowTitle( QString("BMSapp - %1").arg(VERSIONSTRING) );
    ui->menuBar->setVisible(true);
    statusBar()->clearMessage();
}


void MainWindow::on_actionWater_profiles_triggered()
{
    qDebug() << Q_FUNC_INFO;
    ProfileWatersWindow = new ProfileWaters(this);
    int index = ui->mainStack->count();
    ui->mainStack->addWidget(ProfileWatersWindow);
    ui->mainStack->setCurrentIndex(index);
    setWindowTitle(QString("BMSapp - %1 - Water Profiles").arg(VERSIONSTRING));
    ui->menuBar->setVisible(false);
}


void MainWindow::fromProfileMashs()
{
    qDebug() << Q_FUNC_INFO;
    ui->mainStack->setCurrentIndex(-1);
    ui->mainStack->removeWidget(ProfileMashsWindow);
    delete ProfileMashsWindow;
    setWindowTitle( QString("BMSapp - %1").arg(VERSIONSTRING) );
    ui->menuBar->setVisible(true);
    statusBar()->clearMessage();
}


void MainWindow::on_actionMash_profiles_triggered()
{
    qDebug() << Q_FUNC_INFO;
    ProfileMashsWindow = new ProfileMashs(this);
    int index = ui->mainStack->count();
    ui->mainStack->addWidget(ProfileMashsWindow);
    ui->mainStack->setCurrentIndex(index);
    setWindowTitle(QString("BMSapp - %1 - Mash Profiles").arg(VERSIONSTRING));
    ui->menuBar->setVisible(false);
}


void MainWindow::fromProfileStyles()
{
    qDebug() << Q_FUNC_INFO;
    ui->mainStack->setCurrentIndex(-1);
    ui->mainStack->removeWidget(ProfileStylesWindow);
    delete ProfileStylesWindow;
    setWindowTitle( QString("BMSapp - %1").arg(VERSIONSTRING) );
    ui->menuBar->setVisible(true);
    statusBar()->clearMessage();
}


void MainWindow::on_actionStyles_profiles_triggered()
{
    qDebug() << Q_FUNC_INFO;
    ProfileStylesWindow = new ProfileStyles(this);
    int index = ui->mainStack->count();
    ui->mainStack->addWidget(ProfileStylesWindow);
    ui->mainStack->setCurrentIndex(index);
    setWindowTitle(QString("BMSapp - %1 - Styles Profiles").arg(VERSIONSTRING));
    ui->menuBar->setVisible(false);
}


void MainWindow::fromProfileFerments()
{
    qDebug() << Q_FUNC_INFO;
    ui->mainStack->setCurrentIndex(-1);
    ui->mainStack->removeWidget(ProfileFermentsWindow);
    delete ProfileFermentsWindow;
    setWindowTitle( QString("BMSapp - %1").arg(VERSIONSTRING) );
    ui->menuBar->setVisible(true);
    statusBar()->clearMessage();
}


void MainWindow::on_actionFerments_profiles_triggered()
{
    qDebug() << Q_FUNC_INFO;
    ProfileFermentsWindow = new ProfileFerments(this);
    int index = ui->mainStack->count();
    ui->mainStack->addWidget(ProfileFermentsWindow);
    ui->mainStack->setCurrentIndex(index);
    setWindowTitle(QString("BMSapp - %1 - Fermentation Profiles").arg(VERSIONSTRING));
    ui->menuBar->setVisible(false);
}


void MainWindow::fromSetup()
{
    qDebug() << Q_FUNC_INFO;
    ui->mainStack->setCurrentIndex(-1);
    ui->mainStack->removeWidget(SetupWindow);
    delete SetupWindow;
    setWindowTitle( QString("BMSapp - %1").arg(VERSIONSTRING) );
    ui->menuBar->setVisible(true);
    loadSetup();
}


void MainWindow::on_actionSetup_triggered()
{
    qDebug() << Q_FUNC_INFO;
    SetupWindow = new Setup(this);
    int index = ui->mainStack->count();
    ui->mainStack->addWidget(SetupWindow);
    ui->mainStack->setCurrentIndex(index);
    ui->menuBar->setVisible(false);
}


void MainWindow::on_actionAbout_triggered()
{
    AboutDialog dialog(this);
    dialog.setModal(true);
    dialog.exec();
}


void MainWindow::windowTitle(QString msg)
{
    setWindowTitle(QString("BMSapp - %1 - %2").arg(VERSIONSTRING).arg(msg));
}


void MainWindow::statusMsg(QString msg)
{
    statusBar()->showMessage(msg);
}

mercurial