Skip to content

Commit

Permalink
EditTableDialog: Don't suggest field name that already exists
Browse files Browse the repository at this point in the history
  • Loading branch information
MKleusberg committed Dec 24, 2015
1 parent d7c0358 commit 5af384f
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
26 changes: 25 additions & 1 deletion src/EditTableDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,20 @@ void EditTableDialog::addField()
{
QTreeWidgetItem *tbitem = new QTreeWidgetItem(ui->treeWidget);
tbitem->setFlags(tbitem->flags() | Qt::ItemIsEditable);
tbitem->setText(kName, "Field" + QString::number(ui->treeWidget->topLevelItemCount()));

// Find an unused name for the field by starting with 'Fieldx' where x is the number of fields + 1.
// If this name happens to exist already, increase x by one until we find an unused name.
{
unsigned int field_number = ui->treeWidget->topLevelItemCount();
QString field_name;
do
{
field_name = "Field" + QString::number(field_number);
field_number++;
} while(fieldNameExists(field_name));
tbitem->setText(kName, field_name);
}

QComboBox* typeBox = new QComboBox(ui->treeWidget);
typeBox->setProperty("column", tbitem->text(kName));
typeBox->setEditable(true);
Expand Down Expand Up @@ -600,3 +613,14 @@ void EditTableDialog::setWithoutRowid(bool without_rowid)

// TODO: Update table if we're editing an existing table
}

bool EditTableDialog::fieldNameExists(const QString& name)
{
foreach(const sqlb::FieldPtr& ptr, m_table.fields())
{
if(ptr->name() == name)
return true;
}

return false;
}
3 changes: 3 additions & 0 deletions src/EditTableDialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ class EditTableDialog : public QDialog

void moveCurrentField(bool down);

// Returns true if there already is a field of that name
bool fieldNameExists(const QString& name);

private slots:
virtual void populateFields();
virtual void addField();
Expand Down

0 comments on commit 5af384f

Please sign in to comment.