From 8cc3154c72442160f57e6e5eb400b05bdb8cd3a9 Mon Sep 17 00:00:00 2001 From: Martin Kleusberg Date: Sun, 14 May 2017 21:37:11 +0200 Subject: [PATCH] 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. --- src/EditIndexDialog.cpp | 49 ++++++++++++++++++++++++++++++++++++----- src/EditIndexDialog.h | 1 + src/EditIndexDialog.ui | 34 +++++++++++++++++++++++++--- 3 files changed, 76 insertions(+), 8 deletions(-) diff --git a/src/EditIndexDialog.cpp b/src/EditIndexDialog.cpp index 608654e5..1397c6b9 100644 --- a/src/EditIndexDialog.cpp +++ b/src/EditIndexDialog.cpp @@ -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(&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;isetFlags(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(&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)); +} diff --git a/src/EditIndexDialog.h b/src/EditIndexDialog.h index 2b2ee4ca..ba967b90 100644 --- a/src/EditIndexDialog.h +++ b/src/EditIndexDialog.h @@ -30,6 +30,7 @@ private slots: void removeFromIndex(const QModelIndex& idx = QModelIndex()); void moveColumnUp(); void moveColumnDown(); + void addExpressionColumn(); private: DBBrowserDB& pdb; diff --git a/src/EditIndexDialog.ui b/src/EditIndexDialog.ui index bb3a2cab..4477fbcf 100644 --- a/src/EditIndexDialog.ui +++ b/src/EditIndexDialog.ui @@ -166,6 +166,17 @@ + + + + Add a new expression column to the index. Expression columns contain SQL expression rather than column names. + + + + :/icons/cog_go.png:/icons/cog_go.png + + + @@ -196,7 +207,7 @@ - QAbstractItemView::NoEditTriggers + QAbstractItemView::AnyKeyPressed|QAbstractItemView::EditKeyPressed|QAbstractItemView::SelectedClicked false @@ -427,7 +438,7 @@ 406 - 266 + 247 385 @@ -443,7 +454,7 @@ 406 - 304 + 285 350 @@ -531,6 +542,22 @@ + + buttonAddExpressionColumn + clicked() + EditIndexDialog + addExpressionColumn() + + + 398 + 311 + + + 379 + 500 + + + tableChanged(QString) @@ -541,5 +568,6 @@ removeFromIndex(QModelIndex) moveColumnUp() moveColumnDown() + addExpressionColumn()