We can fetch a list of suppliers and show it in the wrong window. Still a lot to learn about Qt5

Sat, 12 Feb 2022 21:24:43 +0100

author
Michiel Broek <mbroek@mbse.eu>
date
Sat, 12 Feb 2022 21:24:43 +0100
changeset 6
f8474f2c5db9
parent 5
22baafbf770d
child 7
51fbea52551e

We can fetch a list of suppliers and show it in the wrong window. Still a lot to learn about Qt5

CMakeLists.txt file | annotate | diff | comparison | revisions
src/AboutDialog.cpp file | annotate | diff | comparison | revisions
src/AboutDialog.h file | annotate | diff | comparison | revisions
src/InventorySuppliers.cpp file | annotate | diff | comparison | revisions
src/InventorySuppliers.h file | annotate | diff | comparison | revisions
src/MainWindow.cpp file | annotate | diff | comparison | revisions
src/MainWindow.h file | annotate | diff | comparison | revisions
src/bmsapp.cpp file | annotate | diff | comparison | revisions
src/bmsapp.h file | annotate | diff | comparison | revisions
src/database/database.cpp file | annotate | diff | comparison | revisions
src/database/database.h file | annotate | diff | comparison | revisions
src/main.cpp file | annotate | diff | comparison | revisions
ui/InventorySuppliers.ui file | annotate | diff | comparison | revisions
--- a/CMakeLists.txt	Fri Feb 11 16:00:06 2022 +0100
+++ b/CMakeLists.txt	Sat Feb 12 21:24:43 2022 +0100
@@ -98,6 +98,7 @@
     ${SRCDIR}/main.cpp
     ${SRCDIR}/bmsapp.cpp
     ${SRCDIR}/AboutDialog.cpp
+    ${SRCDIR}/InventorySuppliers.cpp
     ${SRCDIR}/MainWindow.cpp
     ${SRCDIR}/database/database.cpp
 )
@@ -105,12 +106,14 @@
 set( HDRS
     ${SRCDIR}/bmsapp.h
     ${SRCDIR}/AboutDialog.h
+    ${SRCDIR}/InventorySuppliers.h
     ${SRCDIR}/MainWindow.h
     ${SRCDIR}/database/database.h
 )
 
 set( UIS
     ${UIDIR}/AboutDialog.ui
+    ${UIDIR}/InventorySuppliers.ui
     ${UIDIR}/MainWindow.ui
 )
 
--- a/src/AboutDialog.cpp	Fri Feb 11 16:00:06 2022 +0100
+++ b/src/AboutDialog.cpp	Sat Feb 12 21:24:43 2022 +0100
@@ -16,13 +16,16 @@
  */
 #include "AboutDialog.h"
 #include "../ui/ui_AboutDialog.h"
+#include <QDebug>
 
 AboutDialog::AboutDialog(QWidget *parent) : QDialog(parent), ui(new Ui::AboutDialog)
 {
+    qDebug() << Q_FUNC_INFO;
     ui->setupUi(this);
 }
 
 AboutDialog::~AboutDialog()
 {
+    qDebug() << Q_FUNC_INFO;
     delete ui;
 }
--- a/src/AboutDialog.h	Fri Feb 11 16:00:06 2022 +0100
+++ b/src/AboutDialog.h	Sat Feb 12 21:24:43 2022 +0100
@@ -3,6 +3,7 @@
 
 #include <QDialog>
 
+
 namespace Ui {
 class AboutDialog;
 }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/InventorySuppliers.cpp	Sat Feb 12 21:24:43 2022 +0100
@@ -0,0 +1,71 @@
+/**
+ * InventorySuppliers.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 "InventorySuppliers.h"
+#include "../ui/ui_InventorySuppliers.h"
+#include "config.h"
+
+#include <QDebug>
+//#include <QSqlTableModel>
+//#include <QTableView>
+//#include <QtWidgets>
+#include <QtSql>
+#include <QTableWidget>
+
+
+
+InventorySuppliers::InventorySuppliers(QWidget *parent) : QDialog(parent), ui(new Ui::InventorySuppliers)
+{
+    qDebug() << Q_FUNC_INFO;
+
+    QTableWidget* table = new QTableWidget();
+    QSqlQuery query("SELECT * FROM inventory_suppliers ORDER BY name");
+
+    table->setColumnCount(query.record().count() - 1);	/* Skip the last uuid column */
+    table->setRowCount(query.size());
+
+    table->setHorizontalHeaderLabels({"record", "name", "address", "city", "zip", "country", "website", "email", "phone", "remark"});
+
+    qDebug() << query.record().count() << query.size();
+    // So far, so good.
+    query.first();
+    for (int ridx = 0 ; ridx < query.size() ; ridx++ ) {
+	for (int cidx = 0 ; cidx < query.record().count() - 1; cidx++) {
+	    QTableWidgetItem* item = new QTableWidgetItem(query.value(cidx).toString());
+	    table->setItem(ridx, cidx, item );
+	    //qDebug() << ridx << cidx << query.value(cidx).toString();
+	}
+	query.next();
+    }
+    table->show(); /* TODO: Uses a separate window */
+
+    ui->setupUi(this);
+    setWindowTitle( QString("BMSapp - %1 - Inventory Suppliers").arg(VERSIONSTRING) );
+}
+
+InventorySuppliers::~InventorySuppliers()
+{
+    qDebug() << Q_FUNC_INFO;
+    delete ui;
+}
+
+
+void InventorySuppliers::on_changeButton_clicked()
+{
+    qDebug() << Q_FUNC_INFO;
+    emit firstWindow();
+}
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/InventorySuppliers.h	Sat Feb 12 21:24:43 2022 +0100
@@ -0,0 +1,28 @@
+#ifndef _INVENTORYSPUPPLIERS_H
+#define _INVENTORYSPUPPLIERS_H
+
+#include <QDialog>
+
+namespace Ui {
+class InventorySuppliers;
+}
+
+class InventorySuppliers : public QDialog
+{
+    Q_OBJECT
+
+public:
+    explicit InventorySuppliers(QWidget *parent = nullptr);
+    ~InventorySuppliers();
+
+signals:
+    void firstWindow();
+
+private slots:
+    void on_changeButton_clicked();
+
+private:
+    Ui::InventorySuppliers *ui;
+};
+
+#endif
--- a/src/MainWindow.cpp	Fri Feb 11 16:00:06 2022 +0100
+++ b/src/MainWindow.cpp	Sat Feb 12 21:24:43 2022 +0100
@@ -16,7 +16,9 @@
  */
 #include "MainWindow.h"
 #include "AboutDialog.h"
+#include "InventorySuppliers.h"
 #include "../ui/ui_MainWindow.h"
+#include "config.h"
 
 #include <QApplication>
 #include <QCloseEvent>
@@ -27,24 +29,48 @@
 
 MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent),  ui(new Ui::MainWindow)
 {
+    qDebug() << Q_FUNC_INFO;
     ui->setupUi(this);
+
+    setWindowTitle( QString("BMSapp - %1").arg(VERSIONSTRING) );
 }
 
 
 MainWindow::~MainWindow()
 {
+    qDebug() << Q_FUNC_INFO;
     delete ui;
 }
 
 
 void MainWindow::on_actionExit_triggered()
 {
+    qDebug() << Q_FUNC_INFO;
     this->close();
 }
 
 
+void MainWindow::fromInventorySuppliers()
+{
+    qDebug() << Q_FUNC_INFO;
+    delete InventorySuppliersWindow;
+    this->show();
+}
+
+
+void MainWindow::on_actionSuppliers_triggered()
+{
+    qDebug() << Q_FUNC_INFO;
+    InventorySuppliersWindow = new InventorySuppliers(this);
+    QObject::connect(InventorySuppliersWindow, SIGNAL(firstWindow()), this, SLOT(fromInventorySuppliers()));
+    this->hide();    // Close the main window
+    InventorySuppliersWindow->show();  // Show a second window
+}
+
+
 void MainWindow::on_actionAbout_triggered()
 {
+    qDebug() << Q_FUNC_INFO;
     AboutDialog dialog(this);
     dialog.setModal(true);
     dialog.exec();
--- a/src/MainWindow.h	Fri Feb 11 16:00:06 2022 +0100
+++ b/src/MainWindow.h	Sat Feb 12 21:24:43 2022 +0100
@@ -1,6 +1,8 @@
 #ifndef _MAINWINDOW_H
 #define _MAINWINDOW_H
 
+#include "InventorySuppliers.h"
+
 #include <QMainWindow>
 #include <QStandardItemModel>
 #include <QMenu>
@@ -20,12 +22,17 @@
 
 private slots:
     void on_actionExit_triggered();
+    void on_actionSuppliers_triggered();
     void on_actionAbout_triggered();
 
 public slots:
+    void fromInventorySuppliers();
 
 private:
     Ui::MainWindow *ui;
+
+    // Keep pointers to new windows.
+    InventorySuppliers *InventorySuppliersWindow;
 };
 
 #endif
--- a/src/bmsapp.cpp	Fri Feb 11 16:00:06 2022 +0100
+++ b/src/bmsapp.cpp	Sat Feb 12 21:24:43 2022 +0100
@@ -46,17 +46,18 @@
 
 
 
-bool Bmsapp::initialize()
+bool Bmsapp::initialize(bool useDevelop)
 {
     ensureDirectoriesExist();
     readsettings();
 
+    // QLocale german(QLocale::German,QLocale::Germany);
+    // QLocale::setDefault(german);
+
     qDebug() << "Loading Database...";
 
     db = new DataBase();
-    db->connectToDataBase();
-
-    return true;
+    return db->openDataBase(useDevelop);
 }
 
 
@@ -66,6 +67,7 @@
     qDebug() << "BMSapp is cleaning up.";
 
     writesettings();
+    db->closeDataBase();
 }
 
 
@@ -153,11 +155,12 @@
 
 
 
-int Bmsapp::run() {
+int Bmsapp::run(bool useDevelop, bool startConfig) {
 
     int rc = 0;
 
-    if (! initialize()) {
+    qDebug() << Q_FUNC_INFO;
+    if (! initialize(useDevelop)) {
 	cleanup();
 	return 1;
     }
--- a/src/bmsapp.h	Fri Feb 11 16:00:06 2022 +0100
+++ b/src/bmsapp.h	Sat Feb 12 21:24:43 2022 +0100
@@ -51,7 +51,7 @@
      * @brief Blocking call that executes the application.
      * @return Exit code from the application.
      */
-    static int run();
+    static int run(bool useDevelop, bool startConfig);
 
 private:
     static MainWindow* m_mainWindow;
@@ -66,7 +66,7 @@
      * @brief Run before showing MainWindow, does all system setup.
      * @return false if anything goes awry, true if it's ok to start MainWindow.
      */
-    static bool initialize();
+    static bool initialize(bool useDevelop);
 
     /**
      * @brief Run after QApplication exits to clean up shit, close database, etc.
--- a/src/database/database.cpp	Fri Feb 11 16:00:06 2022 +0100
+++ b/src/database/database.cpp	Sat Feb 12 21:24:43 2022 +0100
@@ -21,22 +21,23 @@
 {
      
 }
-     
+
+
 DataBase::~DataBase()
 {
-     
-}
-     
-void DataBase::connectToDataBase()
-{
-    this->openDataBase();
+
 }
 
-bool DataBase::openDataBase()
+
+bool DataBase::openDataBase(bool develop)
 {
     QSettings settings(QSettings::IniFormat, QSettings::UserScope, "mbse", "bmsapp");
 
-    settings.beginGroup("dbdev");
+    if (develop)
+    	settings.beginGroup("dbdev");
+    else
+	settings.beginGroup("dbprod");
+
     qDebug() << settings.value("host").toString() << settings.value("port").toString() << settings.value("name").toString() << settings.value("user").toString() << settings.value("pass").toString();
     db = QSqlDatabase::addDatabase("QMYSQL");
     db.setHostName(settings.value("host").toString());
--- a/src/database/database.h	Fri Feb 11 16:00:06 2022 +0100
+++ b/src/database/database.h	Sat Feb 12 21:24:43 2022 +0100
@@ -13,14 +13,11 @@
 public:
     explicit DataBase(QObject *parent = 0);
     ~DataBase();
-    void connectToDataBase();
-     
+    bool openDataBase(bool develop);
+    void closeDataBase();
+
 private:
     QSqlDatabase    db;
-     
-private:
-    bool openDataBase();        
-    void closeDataBase();       
 };
 
 
--- a/src/main.cpp	Fri Feb 11 16:00:06 2022 +0100
+++ b/src/main.cpp	Sat Feb 12 21:24:43 2022 +0100
@@ -46,13 +46,10 @@
     /* Setup commandline parser */
     QCommandLineParser parser;
     parser.setApplicationDescription("Brewery Management System Application.");
-    parser.addOptions({
-        // A boolean option with multiple names (-f, --force)
-        {{"c", "config"},
-            QCoreApplication::translate("main", "Start the configuration editor.")},
-	{{"d", "develop"},
-	    QCoreApplication::translate("main", "Use the development database.")},
-    });
+    QCommandLineOption const startConfigOption({"c", "config"}, "Start the configuration editor.");
+    parser.addOption(startConfigOption);
+    QCommandLineOption const useDevelopOption({"d", "develop"}, "Use the development database.");
+    parser.addOption(useDevelopOption);
     parser.addHelpOption();
     parser.addVersionOption();
     parser.process(app);
@@ -60,7 +57,7 @@
     qDebug().noquote() << "Starting" << app.applicationName() << app.applicationVersion() << QDateTime::currentDateTime().toString();
 
     try {
-	auto mainAppReturnValue = Bmsapp::run();
+	auto mainAppReturnValue = Bmsapp::run(parser.isSet(useDevelopOption), parser.isSet(startConfigOption));
 	qDebug().noquote() << "Finished" << app.applicationName() << app.applicationVersion() << QDateTime::currentDateTime().toString();
 	return mainAppReturnValue;
     }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ui/InventorySuppliers.ui	Sat Feb 12 21:24:43 2022 +0100
@@ -0,0 +1,67 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<ui version="4.0">
+ <class>InventorySuppliers</class>
+ <widget class="QDialog" name="InventorySuppliers">
+  <property name="geometry">
+   <rect>
+    <x>0</x>
+    <y>0</y>
+    <width>1280</width>
+    <height>640</height>
+   </rect>
+  </property>
+  <property name="windowTitle">
+   <string>Dialog</string>
+  </property>
+  <layout class="QVBoxLayout" name="verticalLayout">
+   <item>
+    <widget class="QTableWidget" name="mytable"/>
+   </item>
+   <item>
+    <widget class="QDialogButtonBox" name="changeButton">
+     <property name="orientation">
+      <enum>Qt::Horizontal</enum>
+     </property>
+     <property name="standardButtons">
+      <set>QDialogButtonBox::Ok</set>
+     </property>
+    </widget>
+   </item>
+  </layout>
+ </widget>
+ <resources/>
+ <connections>
+  <connection>
+   <sender>changeButton</sender>
+   <signal>accepted()</signal>
+   <receiver>InventorySuppliers</receiver>
+   <slot>accept()</slot>
+   <hints>
+    <hint type="sourcelabel">
+     <x>248</x>
+     <y>254</y>
+    </hint>
+    <hint type="destinationlabel">
+     <x>157</x>
+     <y>274</y>
+    </hint>
+   </hints>
+  </connection>
+  <connection>
+   <sender>changeButton</sender>
+   <signal>rejected()</signal>
+   <receiver>InventorySuppliers</receiver>
+   <slot>reject()</slot>
+   <hints>
+    <hint type="sourcelabel">
+     <x>316</x>
+     <y>260</y>
+    </hint>
+    <hint type="destinationlabel">
+     <x>286</x>
+     <y>274</y>
+    </hint>
+   </hints>
+  </connection>
+ </connections>
+</ui>

mercurial