src/EditSupplier.cpp

changeset 17
f0bcdbd3d36f
parent 11
c9cdc15d3caf
child 21
15e5879df8dc
equal deleted inserted replaced
16:a5d8e783a7b0 17:f0bcdbd3d36f
25 25
26 qDebug() << "EditSupplier record:" << id; 26 qDebug() << "EditSupplier record:" << id;
27 ui->setupUi(this); 27 ui->setupUi(this);
28 this->recno = id; 28 this->recno = id;
29 29
30 if (id < 0) { 30 WindowTitle();
31 setWindowTitle(QString("BMSapp - Add new supplier")); 31 if (id >= 0) {
32 } else {
33 setWindowTitle(QString("BMSapp - Edit supplier %1").arg(id));
34 query.prepare("SELECT * FROM inventory_suppliers WHERE record = :recno"); 32 query.prepare("SELECT * FROM inventory_suppliers WHERE record = :recno");
35 query.bindValue(":recno", id); 33 query.bindValue(":recno", id);
36 query.exec(); 34 query.exec();
37 query.next(); 35 query.next();
38 36
44 ui->webEdit->setText(query.value(6).toString()); 42 ui->webEdit->setText(query.value(6).toString());
45 ui->emailEdit->setText(query.value(7).toString()); 43 ui->emailEdit->setText(query.value(7).toString());
46 ui->phoneEdit->setText(query.value(8).toString()); 44 ui->phoneEdit->setText(query.value(8).toString());
47 ui->notesEdit->setPlainText(query.value(9).toString()); 45 ui->notesEdit->setPlainText(query.value(9).toString());
48 } 46 }
49 connect(ui->notesEdit, SIGNAL(textChanged()), this, SLOT(onTextChanged())); 47 connect(ui->nameEdit, &QLineEdit::textChanged, this, &EditSupplier::is_changed);
48 connect(ui->addressEdit, &QLineEdit::textChanged, this, &EditSupplier::is_changed);
49 connect(ui->cityEdit, &QLineEdit::textChanged, this, &EditSupplier::is_changed);
50 connect(ui->zipEdit, &QLineEdit::textChanged, this, &EditSupplier::is_changed);
51 connect(ui->countryEdit, &QLineEdit::textChanged, this, &EditSupplier::is_changed);
52 connect(ui->webEdit, &QLineEdit::textChanged, this, &EditSupplier::is_changed);
53 connect(ui->emailEdit, &QLineEdit::textChanged, this, &EditSupplier::is_changed);
54 connect(ui->phoneEdit, &QLineEdit::textChanged, this, &EditSupplier::is_changed);
55 connect(ui->notesEdit, SIGNAL(textChanged()), this, SLOT(is_changed()));
50 } 56 }
51 57
52 58
53 EditSupplier::~EditSupplier() 59 EditSupplier::~EditSupplier()
54 { 60 {
56 delete ui; 62 delete ui;
57 emit entry_changed(); 63 emit entry_changed();
58 } 64 }
59 65
60 66
67 /*
68 * Window header, mark any change with '**'
69 */
70 void EditSupplier::WindowTitle()
71 {
72 QString txt;
73
74 if (this->recno < 0) {
75 txt = QString(tr("BMSapp - Add new supplier"));
76 } else {
77 txt = QString(tr("BMSapp - Edit supplier %1").arg(this->recno));
78 }
79
80 if (this->textIsChanged) {
81 txt.append((QString(" **")));
82 }
83 setWindowTitle(txt);
84 }
85
86
61 void EditSupplier::onOKButtonClicked() 87 void EditSupplier::onOKButtonClicked()
62 { 88 {
63 QSqlQuery query; 89 QSqlQuery query;
64 bool modified, valid;
65 90
66 /* The notes field uses a signal textChanged() */
67 modified = (ui->nameEdit->isModified() || ui->addressEdit->isModified() || ui->cityEdit->isModified() ||
68 ui->zipEdit->isModified() || ui->countryEdit->isModified() || ui->webEdit->isModified() ||
69 ui->emailEdit->isModified() || ui->phoneEdit->isModified() || this->textIsChanged);
70
71 valid = true;
72 /* If there are errors in the form, show a message and do "return;" */ 91 /* If there are errors in the form, show a message and do "return;" */
73 if (ui->nameEdit->text().length() < 2) { 92 if (ui->nameEdit->text().length() < 2) {
74 // QMessageBox msgBox; 93 QMessageBox::warning(this, tr("Edit Supplier"), tr("Name empty or too short."));
75 // msgBox.setText("Name empty or too short.");
76 // msgBox.exec();
77 return; 94 return;
78 } 95 }
79 96
80 if (modified && valid) { 97 if (this->textIsChanged) {
81 qDebug() << "EditSupplier do SQL";
82 if (this->recno == -1) { 98 if (this->recno == -1) {
83 query.prepare("INSERT INTO inventory_suppliers SET name = :name, address = :address, city = :city, zip = :zip, " 99 query.prepare("INSERT INTO inventory_suppliers SET name = :name, address = :address, city = :city, zip = :zip, "
84 "country = :country, website = :web, email = :email, phone = :phone, notes = :notes, uuid = :uuid"); 100 "country = :country, website = :web, email = :email, phone = :phone, notes = :notes, uuid = :uuid");
85 } else { 101 } else {
86 query.prepare("UPDATE inventory_suppliers SET name = :name, address = :address, city = :city, zip = :zip, " 102 query.prepare("UPDATE inventory_suppliers SET name = :name, address = :address, city = :city, zip = :zip, "
100 } else { 116 } else {
101 query.bindValue(":recno", this->recno); 117 query.bindValue(":recno", this->recno);
102 } 118 }
103 query.exec(); 119 query.exec();
104 if (query.lastError().isValid()) { 120 if (query.lastError().isValid()) {
105 qDebug() << query.lastError(); 121 qDebug() << "EditSupplier" << query.lastError();
122 QMessageBox::warning(this, tr("Database error"),
123 tr("MySQL error: %1\n%2\n%3")
124 .arg(query.lastError().nativeErrorCode())
125 .arg(query.lastError().driverText())
126 .arg(query.lastError().databaseText()));
127 } else {
128 qDebug() << "EditSupplier Saved";
106 } 129 }
107 130
108 this->close(); 131 this->close();
109 this->setResult(1); 132 this->setResult(1);
110 } else { 133 } else {
113 this->setResult(0); 136 this->setResult(0);
114 } 137 }
115 } 138 }
116 139
117 140
118 void EditSupplier::onTextChanged() 141 void EditSupplier::is_changed()
119 { 142 {
120 this->textIsChanged = true; 143 this->textIsChanged = true;
144 WindowTitle();
121 } 145 }
122 146
123 147
124 void EditSupplier::onCancelButtonClicked() 148 void EditSupplier::onCancelButtonClicked()
125 { 149 {

mercurial