The Supplier editor saves changes and inserts new suppliers. It sends a signal to InventorySuppliers when done (always for now). Refresh the table still doesn't work. Added a missing iconn in the Inventory menus dropdown.

Tue, 15 Feb 2022 21:21:12 +0100

author
Michiel Broek <mbroek@mbse.eu>
date
Tue, 15 Feb 2022 21:21:12 +0100
changeset 11
c9cdc15d3caf
parent 10
8aa2bd9ba9e8
child 12
66e10898a2a9

The Supplier editor saves changes and inserts new suppliers. It sends a signal to InventorySuppliers when done (always for now). Refresh the table still doesn't work. Added a missing iconn in the Inventory menus dropdown.

src/EditSupplier.cpp file | annotate | diff | comparison | revisions
src/EditSupplier.h file | annotate | diff | comparison | revisions
src/InventorySuppliers.cpp file | annotate | diff | comparison | revisions
src/InventorySuppliers.h file | annotate | diff | comparison | revisions
src/bmsapp.h file | annotate | diff | comparison | revisions
ui/EditSupplier.ui file | annotate | diff | comparison | revisions
ui/MainWindow.ui file | annotate | diff | comparison | revisions
--- a/src/EditSupplier.cpp	Mon Feb 14 20:58:07 2022 +0100
+++ b/src/EditSupplier.cpp	Tue Feb 15 21:21:12 2022 +0100
@@ -17,21 +17,20 @@
 #include "EditSupplier.h"
 #include "../ui/ui_EditSupplier.h"
 #include "bmsapp.h"
-#include <QDebug>
-#include <QtSql>
 
 
 EditSupplier::EditSupplier(int id, QWidget *parent) : QDialog(parent), ui(new Ui::EditSupplier)
 {
     QSqlQuery query;
 
-    qDebug() << Q_FUNC_INFO << id;
+    qDebug() << "EditSupplier record:" << id;
     ui->setupUi(this);
+    this->recno = id;
 
     if (id < 0) {
-	setWindowTitle( QString("BMSapp - Add new supplier"));
+	setWindowTitle(QString("BMSapp - Add new supplier"));
     } else {
-	setWindowTitle( QString("BMSapp - Edit supplier %1").arg(id));
+	setWindowTitle(QString("BMSapp - Edit supplier %1").arg(id));
 	query.prepare("SELECT * FROM inventory_suppliers WHERE record = :recno");
 	query.bindValue(":recno", id);
 	query.exec();
@@ -45,34 +44,85 @@
 	ui->webEdit->setText(query.value(6).toString());
 	ui->emailEdit->setText(query.value(7).toString());
 	ui->phoneEdit->setText(query.value(8).toString());
-	ui->notesEdit->setText(query.value(9).toString());
+	ui->notesEdit->setPlainText(query.value(9).toString());
     }
+    connect(ui->notesEdit, SIGNAL(textChanged()), this, SLOT(onTextChanged()));
 }
 
 
 EditSupplier::~EditSupplier()
 {
-    qDebug() << Q_FUNC_INFO;
+    qDebug() << "EditSupplier closed";
     delete ui;
+    emit entry_changed();
 }
 
 
 void EditSupplier::onOKButtonClicked()
 {
-    qDebug() << Q_FUNC_INFO << "Ok, check for valid data";
+    QSqlQuery query;
+    bool modified, valid;
+
+    /* The notes field uses a signal textChanged() */
+    modified = (ui->nameEdit->isModified() || ui->addressEdit->isModified() || ui->cityEdit->isModified() ||
+		ui->zipEdit->isModified() || ui->countryEdit->isModified() || ui->webEdit->isModified() ||
+		ui->emailEdit->isModified() || ui->phoneEdit->isModified() || this->textIsChanged);
 
+    valid = true;
     /* If there are errors in the form, show a message and do "return;" */
+    if (ui->nameEdit->text().length() < 2) {
+//	QMessageBox msgBox;
+//	msgBox.setText("Name empty or too short.");
+//	msgBox.exec();
+	return;
+    }
 
-    /* Save or insert the data */
+    if (modified && valid) {
+	qDebug() << "EditSupplier do SQL";
+    	if (this->recno == -1) {
+    	    query.prepare("INSERT INTO inventory_suppliers SET name = :name, address = :address, city = :city, zip = :zip, "
+		"country = :country, website = :web, email = :email, phone = :phone, notes = :notes, uuid = :uuid");
+    	} else {
+	    query.prepare("UPDATE inventory_suppliers SET name = :name, address = :address, city = :city, zip = :zip, "
+		"country = :country, website = :web, email = :email, phone = :phone, notes = :notes WHERE record = :recno");
+    	}
+	query.bindValue(":name", ui->nameEdit->text());
+	query.bindValue(":address", ui->addressEdit->text());
+	query.bindValue(":city", ui->cityEdit->text());
+	query.bindValue(":zip", ui->zipEdit->text());
+	query.bindValue(":country", ui->countryEdit->text());
+	query.bindValue(":web", ui->webEdit->text());
+	query.bindValue(":email", ui->emailEdit->text());
+	query.bindValue(":phone", ui->phoneEdit->text());
+	query.bindValue(":notes", ui->notesEdit->toPlainText());
+	if (this->recno == -1) {
+	    query.bindValue(":uuid", QUuid::createUuid().toString().mid(1, 36));
+	} else {
+	    query.bindValue(":recno", this->recno);
+	}
+	query.exec();
+	if (query.lastError().isValid()) {
+	    qDebug() << query.lastError();
+	}
 
-    this->close();
-    this->setResult(1);
+    	this->close();
+    	this->setResult(1);
+    } else {
+	/* Not saving */
+	this->close();
+	this->setResult(0);
+    }
+}
+
+
+void EditSupplier::onTextChanged()
+{
+    this->textIsChanged = true;
 }
 
 
 void EditSupplier::onCancelButtonClicked()
 {
-    qDebug() << Q_FUNC_INFO;
     this->close();
     this->setResult(0);
 }
--- a/src/EditSupplier.h	Mon Feb 14 20:58:07 2022 +0100
+++ b/src/EditSupplier.h	Tue Feb 15 21:21:12 2022 +0100
@@ -12,6 +12,9 @@
 {
     Q_OBJECT
 
+signals:
+    void entry_changed();
+
 public:
     explicit EditSupplier(int id, QWidget *parent = 0);
     ~EditSupplier();
@@ -19,9 +22,12 @@
 private slots:
     void onOKButtonClicked();
     void onCancelButtonClicked();
+    void onTextChanged();
 
 private:
     Ui::EditSupplier *ui;
+    int recno;
+    bool textIsChanged = false;
 };
 
 #endif
--- a/src/InventorySuppliers.cpp	Mon Feb 14 20:58:07 2022 +0100
+++ b/src/InventorySuppliers.cpp	Tue Feb 15 21:21:12 2022 +0100
@@ -34,18 +34,25 @@
     qDebug() << Q_FUNC_INFO;
 
     ui->setupUi(this);
-    InventorySuppliers::loadTable();
+    emit refreshTable();
 
     setWindowTitle( QString("BMSapp - %1 - Inventory Suppliers").arg(VERSIONSTRING) );
 }
 
 
-void InventorySuppliers::loadTable(void)
+void InventorySuppliers::refreshTable()
 {
+    qDebug() << "slot"  << Q_FUNC_INFO;
+
     ui->tableSuppliers = new QTableWidget(ui->tableSuppliers);
+    emit ui->tableSuppliers->clearContents();
     QSqlQuery query("SELECT * FROM inventory_suppliers ORDER BY name");
     const QStringList labels({tr("Record"), tr("Name"), tr("Address"), tr("City"), tr("Country"), tr("Phone"), tr("Edit")});
 
+//    ui->tableSuppliers->clear();
+//    ui->tableSuppliers->setRowCount(0);
+//    ui->tableSuppliers->setColumnCount(0);
+
     ui->tableSuppliers->setColumnCount(7);
     ui->tableSuppliers->setColumnWidth(0, 50);	/* Record	*/
     ui->tableSuppliers->setColumnWidth(1, 250);	/* Name		*/
@@ -97,16 +104,18 @@
 }
 
 
-bool InventorySuppliers::edit(int recno)
+void InventorySuppliers::edit(int recno)
 {
     qDebug() << Q_FUNC_INFO << recno;
 
     EditSupplier dialog(recno, this);
-    dialog.setModal(true);
-    int rc = dialog.exec();	/* rc 0 == cancel, rc 1 == ok */
+    /* Signal from editor if a refresh is needed */
+    connect(&dialog, SIGNAL(entry_changed()), this, SLOT(refreshTable()));
 
-    qDebug() << Q_FUNC_INFO << recno << rc;
-    return false;
+    dialog.setModal(true);
+    dialog.exec();
+
+    qDebug() << Q_FUNC_INFO << "Back from edit record:" << recno;
 }
 
 
@@ -128,7 +137,6 @@
 
 void InventorySuppliers::on_quitButton_clicked()
 {
-    qDebug() << Q_FUNC_INFO;
     emit firstWindow();
 }
 
--- a/src/InventorySuppliers.h	Mon Feb 14 20:58:07 2022 +0100
+++ b/src/InventorySuppliers.h	Tue Feb 15 21:21:12 2022 +0100
@@ -22,11 +22,11 @@
     void on_quitButton_clicked();
     void on_insertButton_clicked();
     void on_editButton_clicked();
+    void refreshTable(void);
 
 private:
     Ui::InventorySuppliers *ui;
-    bool edit(int recno);
-    void loadTable(void);
+    void edit(int recno);
 };
 
 #endif
--- a/src/bmsapp.h	Mon Feb 14 20:58:07 2022 +0100
+++ b/src/bmsapp.h	Tue Feb 15 21:21:12 2022 +0100
@@ -15,6 +15,9 @@
 #include <QString>
 #include <QTextStream>
 #include <QTranslator>
+#include <QtSql>
+#include <QUuid>
+#include <QPlainTextEdit>
 
 #include "database/database.h"
 
--- a/ui/EditSupplier.ui	Mon Feb 14 20:58:07 2022 +0100
+++ b/ui/EditSupplier.ui	Tue Feb 15 21:21:12 2022 +0100
@@ -162,7 +162,7 @@
          </sizepolicy>
         </property>
         <property name="maxLength">
-         <number>20</number>
+         <number>16</number>
         </property>
         <property name="placeholderText">
          <string>+31 123 45678</string>
@@ -177,7 +177,7 @@
        </widget>
       </item>
       <item row="8" column="1">
-       <widget class="QTextEdit" name="notesEdit">
+       <widget class="QPlainTextEdit" name="notesEdit">
         <property name="horizontalScrollBarPolicy">
          <enum>Qt::ScrollBarAlwaysOff</enum>
         </property>
--- a/ui/MainWindow.ui	Mon Feb 14 20:58:07 2022 +0100
+++ b/ui/MainWindow.ui	Tue Feb 15 21:21:12 2022 +0100
@@ -211,6 +211,10 @@
    </property>
   </action>
   <action name="actionSuppliers">
+   <property name="icon">
+    <iconset resource="../resources/icons.qrc">
+     <normaloff>:/icons/silk/icons/silk/group.png</normaloff>:/icons/silk/icons/silk/group.png</iconset>
+   </property>
    <property name="text">
     <string>Suppliers</string>
    </property>

mercurial