Skip to content

Commit

Permalink
Add button for adding expression columns in Edit Index dialog
Browse files Browse the repository at this point in the history
Add a button for adding new expression columns to the Edit Index dialog.

Allow the user to edit the 'name' of existing expression columns in the
Edit Index dialog.
  • Loading branch information
MKleusberg committed May 14, 2017
1 parent 6321d14 commit 8cc3154
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 8 deletions.
49 changes: 44 additions & 5 deletions src/EditIndexDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,14 @@ EditIndexDialog::EditIndexDialog(DBBrowserDB& db, const QString& indexName, bool
tableChanged(ui->comboTableName->currentText(), false);
}

// Add event handler for index column name changes. These are only allowed for expression columns, though.
connect(ui->tableIndexColumns, static_cast<void(QTableWidget::*)(QTableWidgetItem*)>(&QTableWidget::itemChanged),
[=](QTableWidgetItem* item)
{
index.columns().at(item->row())->setName(item->text());
updateSqlText();
});

// Create a savepoint to revert back to
pdb.setSavepoint(m_sRestorePointName);
}
Expand Down Expand Up @@ -115,33 +123,38 @@ void EditIndexDialog::updateColumnLists()

// Fill the index column list. This is done separately from the table column to include expression columns (these are not found in the original
// table) and to preserve the order of the index columns
sqlb::FieldInfoList indexFields = index.fieldInformation();
auto indexFields = index.columns();
ui->tableIndexColumns->blockSignals(true);
ui->tableIndexColumns->setRowCount(indexFields.size());
for(int i=0;i<indexFields.size();++i)
{
// Put the name of the field in the first column
QTableWidgetItem* name = new QTableWidgetItem(indexFields.at(i).name);
name->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
QTableWidgetItem* name = new QTableWidgetItem(indexFields.at(i)->name());
Qt::ItemFlags flags = Qt::ItemIsSelectable | Qt::ItemIsEnabled;
if(indexFields.at(i)->expression())
flags |= Qt::ItemIsEditable;
name->setFlags(flags);
ui->tableIndexColumns->setItem(i, 0, name);

// And put a combobox to select the order in which to index the field in the last column
QComboBox* order = new QComboBox(this);
order->addItem("");
order->addItem("ASC");
order->addItem("DESC");
order->setCurrentText(indexFields.at(i).type.toUpper());
order->setCurrentText(indexFields.at(i)->order().toUpper());
ui->tableIndexColumns->setCellWidget(i, 1, order);
connect(order, static_cast<void(QComboBox::*)(const QString&)>(&QComboBox::currentTextChanged),
[=](QString new_order)
{
int colnum = index.findColumn(indexFields.at(i).name);
int colnum = index.findColumn(indexFields.at(i)->name());
if(colnum != -1)
{
index.column(colnum)->setOrder(new_order);
updateSqlText();
}
});
}
ui->tableIndexColumns->blockSignals(false);

checkInput();
}
Expand Down Expand Up @@ -275,3 +288,29 @@ void EditIndexDialog::moveCurrentColumn(bool down)
// Select old row at new position
ui->tableIndexColumns->selectRow(newRow);
}

void EditIndexDialog::addExpressionColumn()
{
// Check if there already is an empty expression column
int row = index.findColumn("");
if(row == -1)
{
// There is no empty expression column yet, so add one.

// Add new expression column to the index
index.addColumn(sqlb::IndexedColumnPtr(new sqlb::IndexedColumn(
"", // Column name
true, // Is expression
""))); // Order

// Update UI
updateColumnLists();

// Get row number of new column
row = ui->tableIndexColumns->rowCount() - 1;
}

// Now we should have the row number of the empty expression column, no matter if it was newly added or it already existed.
// Select the row for editing
ui->tableIndexColumns->editItem(ui->tableIndexColumns->item(row, 0));
}
1 change: 1 addition & 0 deletions src/EditIndexDialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ private slots:
void removeFromIndex(const QModelIndex& idx = QModelIndex());
void moveColumnUp();
void moveColumnDown();
void addExpressionColumn();

private:
DBBrowserDB& pdb;
Expand Down
34 changes: 31 additions & 3 deletions src/EditIndexDialog.ui
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,17 @@
</property>
</widget>
</item>
<item>
<widget class="QToolButton" name="buttonAddExpressionColumn">
<property name="toolTip">
<string>Add a new expression column to the index. Expression columns contain SQL expression rather than column names.</string>
</property>
<property name="icon">
<iconset resource="icons/icons.qrc">
<normaloff>:/icons/cog_go.png</normaloff>:/icons/cog_go.png</iconset>
</property>
</widget>
</item>
<item>
<spacer name="verticalSpacer_2">
<property name="orientation">
Expand Down Expand Up @@ -196,7 +207,7 @@
</size>
</property>
<property name="editTriggers">
<set>QAbstractItemView::NoEditTriggers</set>
<set>QAbstractItemView::AnyKeyPressed|QAbstractItemView::EditKeyPressed|QAbstractItemView::SelectedClicked</set>
</property>
<property name="showDropIndicator" stdset="0">
<bool>false</bool>
Expand Down Expand Up @@ -427,7 +438,7 @@
<hints>
<hint type="sourcelabel">
<x>406</x>
<y>266</y>
<y>247</y>
</hint>
<hint type="destinationlabel">
<x>385</x>
Expand All @@ -443,7 +454,7 @@
<hints>
<hint type="sourcelabel">
<x>406</x>
<y>304</y>
<y>285</y>
</hint>
<hint type="destinationlabel">
<x>350</x>
Expand Down Expand Up @@ -531,6 +542,22 @@
</hint>
</hints>
</connection>
<connection>
<sender>buttonAddExpressionColumn</sender>
<signal>clicked()</signal>
<receiver>EditIndexDialog</receiver>
<slot>addExpressionColumn()</slot>
<hints>
<hint type="sourcelabel">
<x>398</x>
<y>311</y>
</hint>
<hint type="destinationlabel">
<x>379</x>
<y>500</y>
</hint>
</hints>
</connection>
</connections>
<slots>
<slot>tableChanged(QString)</slot>
Expand All @@ -541,5 +568,6 @@
<slot>removeFromIndex(QModelIndex)</slot>
<slot>moveColumnUp()</slot>
<slot>moveColumnDown()</slot>
<slot>addExpressionColumn()</slot>
</slots>
</ui>

0 comments on commit 8cc3154

Please sign in to comment.