Forgot one signal in Setup. EditSupplier now has signals to track edit changes. The Window header has ** mark if anything is changed. Added errors message boxes.

Thu, 17 Feb 2022 21:00:24 +0100

author
Michiel Broek <mbroek@mbse.eu>
date
Thu, 17 Feb 2022 21:00:24 +0100
changeset 17
f0bcdbd3d36f
parent 16
a5d8e783a7b0
child 18
d0ca50776b0b

Forgot one signal in Setup. EditSupplier now has signals to track edit changes. The Window header has ** mark if anything is changed. Added errors message boxes.

src/EditSupplier.cpp file | annotate | diff | comparison | revisions
src/EditSupplier.h file | annotate | diff | comparison | revisions
src/Setup.cpp file | annotate | diff | comparison | revisions
--- a/src/EditSupplier.cpp	Thu Feb 17 19:57:32 2022 +0100
+++ b/src/EditSupplier.cpp	Thu Feb 17 21:00:24 2022 +0100
@@ -27,10 +27,8 @@
     ui->setupUi(this);
     this->recno = id;
 
-    if (id < 0) {
-	setWindowTitle(QString("BMSapp - Add new supplier"));
-    } else {
-	setWindowTitle(QString("BMSapp - Edit supplier %1").arg(id));
+    WindowTitle();
+    if (id >= 0) {
 	query.prepare("SELECT * FROM inventory_suppliers WHERE record = :recno");
 	query.bindValue(":recno", id);
 	query.exec();
@@ -46,7 +44,15 @@
 	ui->phoneEdit->setText(query.value(8).toString());
 	ui->notesEdit->setPlainText(query.value(9).toString());
     }
-    connect(ui->notesEdit, SIGNAL(textChanged()), this, SLOT(onTextChanged()));
+    connect(ui->nameEdit, &QLineEdit::textChanged, this, &EditSupplier::is_changed);
+    connect(ui->addressEdit, &QLineEdit::textChanged, this, &EditSupplier::is_changed);
+    connect(ui->cityEdit, &QLineEdit::textChanged, this, &EditSupplier::is_changed);
+    connect(ui->zipEdit, &QLineEdit::textChanged, this, &EditSupplier::is_changed);
+    connect(ui->countryEdit, &QLineEdit::textChanged, this, &EditSupplier::is_changed);
+    connect(ui->webEdit, &QLineEdit::textChanged, this, &EditSupplier::is_changed);
+    connect(ui->emailEdit, &QLineEdit::textChanged, this, &EditSupplier::is_changed);
+    connect(ui->phoneEdit, &QLineEdit::textChanged, this, &EditSupplier::is_changed);
+    connect(ui->notesEdit, SIGNAL(textChanged()), this, SLOT(is_changed()));
 }
 
 
@@ -58,27 +64,37 @@
 }
 
 
+/*
+ * Window header, mark any change with '**'
+ */
+void EditSupplier::WindowTitle()
+{
+    QString txt;
+
+    if (this->recno < 0) {
+	txt = QString(tr("BMSapp - Add new supplier"));
+    } else {
+	txt = QString(tr("BMSapp - Edit supplier %1").arg(this->recno));
+    }
+
+    if (this->textIsChanged) {
+	txt.append((QString(" **")));
+    }
+    setWindowTitle(txt);
+}
+
+
 void EditSupplier::onOKButtonClicked()
 {
     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();
+	QMessageBox::warning(this, tr("Edit Supplier"), tr("Name empty or too short."));
 	return;
     }
 
-    if (modified && valid) {
-	qDebug() << "EditSupplier do SQL";
+    if (this->textIsChanged) {
     	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");
@@ -102,7 +118,14 @@
 	}
 	query.exec();
 	if (query.lastError().isValid()) {
-	    qDebug() << query.lastError();
+	    qDebug() << "EditSupplier" << 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() << "EditSupplier Saved";
 	}
 
     	this->close();
@@ -115,9 +138,10 @@
 }
 
 
-void EditSupplier::onTextChanged()
+void EditSupplier::is_changed()
 {
     this->textIsChanged = true;
+    WindowTitle();
 }
 
 
--- a/src/EditSupplier.h	Thu Feb 17 19:57:32 2022 +0100
+++ b/src/EditSupplier.h	Thu Feb 17 21:00:24 2022 +0100
@@ -22,12 +22,14 @@
 private slots:
     void onOKButtonClicked();
     void onCancelButtonClicked();
-    void onTextChanged();
+    void is_changed();
 
 private:
     Ui::EditSupplier *ui;
     int recno;
     bool textIsChanged = false;
+
+    void WindowTitle();
 };
 
 #endif
--- a/src/Setup.cpp	Thu Feb 17 19:57:32 2022 +0100
+++ b/src/Setup.cpp	Thu Feb 17 21:00:24 2022 +0100
@@ -34,6 +34,7 @@
     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());

mercurial