src/EditSupplier.cpp

changeset 17
f0bcdbd3d36f
parent 11
c9cdc15d3caf
child 21
15e5879df8dc
--- 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();
 }
 
 

mercurial