From b04d52b99c6ed9a93e5a0be250f00fb1b117dc14 Mon Sep 17 00:00:00 2001 From: Martin Kleusberg Date: Thu, 24 Dec 2015 13:48:35 +0100 Subject: [PATCH] EditTableDialog: Don't allow two columns with the same name Don't allow the user to type in a field name that is already used in the edited table. Show a warning and reset the name if he tries do so anyway. Also fix the warning for the foreign key check. Here a warning was shown and the renaming cancelled but the field name wasn't reset to the old value. --- src/EditTableDialog.cpp | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/EditTableDialog.cpp b/src/EditTableDialog.cpp index 8d6e7ee7..3eb3e0df 100644 --- a/src/EditTableDialog.cpp +++ b/src/EditTableDialog.cpp @@ -216,6 +216,18 @@ void EditTableDialog::itemChanged(QTreeWidgetItem *item, int column) switch(column) { case kName: + // When a field of that name already exists, show a warning to the user and don't apply the new name + if(fieldNameExists(item->text(column))) + { + QMessageBox::warning(this, qApp->applicationName(), tr("There already is a field with that name. Please rename it first or choose a different " + "name for this field.")); + // Reset the name to the old value but avoid calling this method again for that automatic change + ui->treeWidget->blockSignals(true); + item->setText(column, oldFieldName); + ui->treeWidget->blockSignals(false); + return; + } + // When editing an exiting table, check if any foreign keys would cause trouble in case this name is edited if(!m_bNewTable) { @@ -229,6 +241,10 @@ void EditTableDialog::itemChanged(QTreeWidgetItem *item, int column) { QMessageBox::warning(this, qApp->applicationName(), tr("This column is referenced in a foreign key in table %1, column %2 and thus " "its name cannot be changed.").arg(fkobj.getname()).arg(fkfield->name())); + // Reset the name to the old value but avoid calling this method again for that automatic change + ui->treeWidget->blockSignals(true); + item->setText(column, oldFieldName); + ui->treeWidget->blockSignals(false); return; } }