src/MainWindow.cpp

Tue, 12 Apr 2022 21:03:19 +0200

author
Michiel Broek <mbroek@mbse.eu>
date
Tue, 12 Apr 2022 21:03:19 +0200
changeset 131
0115b97e8c39
parent 103
6da4e93b6ceb
child 133
08635b028dcf
permissions
-rw-r--r--

Added global variables, C++ lovers will hate that. Added global acid data. Fixed several load and save errors in the json arrays in the recipe record. Added first part of the miscs table. The first part of the water tab has values.

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

    openWS(useDevelopOption);

    Acid a;
    a.name = "Lactic";
    a.pK1 = 3.86;
    a.pK2 = 20;
    a.pK3 = 20;
    a.MolWt = 90.08;
    a.AcidSG = 1238;
    a.AcidPrc = 80;
    my_acids.append(a);
    a.name = "Hydrochloric";
    a.pK1 = -7;
    a.pK2 = 20;
    a.pK3 = 20;
    a.MolWt = 36.46;
    a.AcidSG = 1497;
    a.AcidPrc = 28;
    my_acids.append(a);
    a.name = "Phosphoric";
    a.pK1 = 2.12;
    a.pK2 = 7.20;
    a.pK3 = 12.44;
    a.MolWt = 98.00;
    a.AcidSG = 1982;
    a.AcidPrc = 75;
    my_acids.append(a);
    a.name = "Sulfuric";
    a.pK1 = -1;
    a.pK2 = 1.92;
    a.pK3 = 20;
    a.MolWt = 98.07;
    a.AcidSG = 1884;
    a.AcidPrc = 93;
    my_acids.append(a);

    qDebug() << "acids" << my_acids.size();
}


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

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



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


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