mirror of
https://github.com/sqlitebrowser/sqlitebrowser.git
synced 2026-01-19 18:40:13 -06:00
Add button for adding expression columns in Edit Index dialog
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.
This commit is contained in:
@@ -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);
|
||||
}
|
||||
@@ -115,13 +123,17 @@ 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
|
||||
@@ -129,12 +141,12 @@ void EditIndexDialog::updateColumnLists()
|
||||
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);
|
||||
@@ -142,6 +154,7 @@ void EditIndexDialog::updateColumnLists()
|
||||
}
|
||||
});
|
||||
}
|
||||
ui->tableIndexColumns->blockSignals(false);
|
||||
|
||||
checkInput();
|
||||
}
|
||||
@@ -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));
|
||||
}
|
||||
|
||||
@@ -30,6 +30,7 @@ private slots:
|
||||
void removeFromIndex(const QModelIndex& idx = QModelIndex());
|
||||
void moveColumnUp();
|
||||
void moveColumnDown();
|
||||
void addExpressionColumn();
|
||||
|
||||
private:
|
||||
DBBrowserDB& pdb;
|
||||
|
||||
@@ -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">
|
||||
@@ -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>
|
||||
@@ -427,7 +438,7 @@
|
||||
<hints>
|
||||
<hint type="sourcelabel">
|
||||
<x>406</x>
|
||||
<y>266</y>
|
||||
<y>247</y>
|
||||
</hint>
|
||||
<hint type="destinationlabel">
|
||||
<x>385</x>
|
||||
@@ -443,7 +454,7 @@
|
||||
<hints>
|
||||
<hint type="sourcelabel">
|
||||
<x>406</x>
|
||||
<y>304</y>
|
||||
<y>285</y>
|
||||
</hint>
|
||||
<hint type="destinationlabel">
|
||||
<x>350</x>
|
||||
@@ -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>
|
||||
@@ -541,5 +568,6 @@
|
||||
<slot>removeFromIndex(QModelIndex)</slot>
|
||||
<slot>moveColumnUp()</slot>
|
||||
<slot>moveColumnDown()</slot>
|
||||
<slot>addExpressionColumn()</slot>
|
||||
</slots>
|
||||
</ui>
|
||||
|
||||
Reference in New Issue
Block a user