diff --git a/src/EditIndexDialog.cpp b/src/EditIndexDialog.cpp index 69ae484d..2bfb8851 100644 --- a/src/EditIndexDialog.cpp +++ b/src/EditIndexDialog.cpp @@ -49,9 +49,6 @@ EditIndexDialog::EditIndexDialog(DBBrowserDB& db, const QString& indexName, bool } else { tableChanged(ui->comboTableName->currentText(), false); } - - // Refresh SQL preview - updateSqlText(); } EditIndexDialog::~EditIndexDialog() @@ -76,52 +73,61 @@ void EditIndexDialog::tableChanged(const QString& new_table, bool initialLoad) return; } - // And fill the table again - QStringList fields = pdb.getObjectByName(new_table).dynamicCast()->fieldNames(); - ui->tableIndexColumns->setRowCount(fields.size()); - for(int i=0; i < fields.size(); ++i) + updateColumnLists(); +} + +void EditIndexDialog::updateColumnLists() +{ + // Fill the table column list + sqlb::FieldInfoList tableFields = pdb.getObjectByName(index.table()).dynamicCast()->fieldInformation(); + ui->tableTableColumns->setRowCount(tableFields.size()); + int tableRows = 0; + for(int i=0;isetFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled); + ui->tableTableColumns->setItem(tableRows, 0, name); + + // Put the data type in the second column + QTableWidgetItem* type = new QTableWidgetItem(tableFields.at(i).type); + type->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled); + ui->tableTableColumns->setItem(tableRows, 1, type); + + tableRows++; + } + } + + // Set row count to actual count. This is needed for the intial loading, when some rows might have been omitted because they were used in the index + ui->tableTableColumns->setRowCount(tableRows); + + // 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(); + ui->tableIndexColumns->setRowCount(indexFields.size()); + for(int i=0;isetFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled); ui->tableIndexColumns->setItem(i, 0, name); - // Put a checkbox to enable usage in the index of this field in the second column - QCheckBox* enabled = new QCheckBox(this); - if(initialLoad && index.findColumn(fields.at(i)) != -1) - enabled->setCheckState(Qt::Checked); - else - enabled->setCheckState(Qt::Unchecked); - ui->tableIndexColumns->setCellWidget(i, 1, enabled); - connect(enabled, static_cast(&QCheckBox::toggled), - [=](bool use_in_index) - { - if(use_in_index) - { - index.addColumn(sqlb::IndexedColumnPtr(new sqlb::IndexedColumn( - ui->tableIndexColumns->item(i, 0)->text(), - false, - qobject_cast(ui->tableIndexColumns->cellWidget(i, 2))->currentText()))); - } else { - index.removeColumn(ui->tableIndexColumns->item(i, 0)->text()); - } - - checkInput(); - updateSqlText(); - }); - // 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"); - if(initialLoad && index.findColumn(fields.at(i)) != -1) - order->setCurrentText(index.column(index.findColumn(fields.at(i)))->order()); - ui->tableIndexColumns->setCellWidget(i, 2, order); + order->setCurrentText(indexFields.at(i).type.toUpper()); + ui->tableIndexColumns->setCellWidget(i, 1, order); connect(order, static_cast(&QComboBox::currentTextChanged), [=](QString new_order) { - int colnum = index.findColumn(fields.at(i)); + int colnum = index.findColumn(indexFields.at(i).name); if(colnum != -1) { index.column(colnum)->setOrder(new_order); @@ -130,7 +136,50 @@ void EditIndexDialog::tableChanged(const QString& new_table, bool initialLoad) }); } - updateSqlText(); + checkInput(); +} + +void EditIndexDialog::addToIndex(const QModelIndex& idx) +{ + // Get current row number + int row; + if(idx.isValid()) + row = idx.row(); + else + row = ui->tableTableColumns->currentRow(); + + // No row selected? Abort. + if(row == -1) + return; + + // Add field to index + index.addColumn(sqlb::IndexedColumnPtr(new sqlb::IndexedColumn( + ui->tableTableColumns->item(row, 0)->text(), // Column name + false, // Is expression + ""))); // Order + + // Update UI + updateColumnLists(); +} + +void EditIndexDialog::removeFromIndex(const QModelIndex& idx) +{ + // Get current row number + int row; + if(idx.isValid()) + row = idx.row(); + else + row = ui->tableIndexColumns->currentRow(); + + // No row selected? Abort. + if(row == -1) + return; + + // Remove column from index + index.removeColumn(ui->tableIndexColumns->item(row, 0)->text()); + + // Update UI + updateColumnLists(); } void EditIndexDialog::checkInput() diff --git a/src/EditIndexDialog.h b/src/EditIndexDialog.h index caf2e3b3..574a6d66 100644 --- a/src/EditIndexDialog.h +++ b/src/EditIndexDialog.h @@ -4,6 +4,7 @@ #include "sqlitetypes.h" #include +#include class DBBrowserDB; @@ -23,6 +24,8 @@ private slots: void accept(); void tableChanged(const QString& new_table, bool initialLoad = false); void checkInput(); + void addToIndex(const QModelIndex& idx = QModelIndex()); + void removeFromIndex(const QModelIndex& idx = QModelIndex()); private: DBBrowserDB& pdb; @@ -31,6 +34,7 @@ private: bool newIndex; Ui::EditIndexDialog* ui; + void updateColumnLists(); void updateSqlText(); }; diff --git a/src/EditIndexDialog.ui b/src/EditIndexDialog.ui index 7ad3d399..61b8b67e 100644 --- a/src/EditIndexDialog.ui +++ b/src/EditIndexDialog.ui @@ -6,8 +6,8 @@ 0 0 - 610 - 504 + 686 + 506 @@ -17,15 +17,9 @@ :/icons/index_create:/icons/index_create - + - - QFormLayout::AllNonFixedFieldsGrow - - - Qt::AlignLeading|Qt::AlignLeft|Qt::AlignTop - @@ -72,7 +66,7 @@ - &Columns + Colu&mns tableIndexColumns @@ -81,61 +75,155 @@ + + + 0 + 0 + + Qt::Vertical - - - - 0 - 1 - - - - - 0 - 250 - - - - QAbstractItemView::NoEditTriggers - - - false - - - true - - - QAbstractItemView::NoSelection - - - false - - - - Column - - - - - Use in Index - - - - - Order - - + + + + + + QAbstractItemView::NoEditTriggers + + + false + + + true + + + QAbstractItemView::SingleSelection + + + QAbstractItemView::SelectRows + + + true + + + false + + + + Table column + + + + + Type + + + + + + + + + + Qt::Vertical + + + + 20 + 40 + + + + + + + + + :/icons/run:/icons/run + + + + + + + + :/icons/resultset_previous.png:/icons/resultset_previous.png + + + + + + + Qt::Vertical + + + + 20 + 40 + + + + + + + + + + + 0 + 1 + + + + + 0 + 250 + + + + QAbstractItemView::NoEditTriggers + + + false + + + true + + + QAbstractItemView::SingleSelection + + + QAbstractItemView::SelectRows + + + true + + + false + + + + Index column + + + + + Order + + + + + - + 0 100 - + true @@ -167,8 +255,10 @@ editIndexName comboTableName checkIndexUnique + tableTableColumns tableIndexColumns - buttonBox + buttonToIndex + buttonFromIndex @@ -181,8 +271,8 @@ accept() - 252 - 337 + 258 + 499 157 @@ -197,8 +287,8 @@ reject() - 320 - 337 + 326 + 499 286 @@ -213,8 +303,8 @@ tableChanged(QString) - 91 - 43 + 145 + 74 236 @@ -261,8 +351,8 @@ checkInput() - 332 - 90 + 90 + 100 304 @@ -270,9 +360,77 @@ + + buttonToIndex + clicked() + EditIndexDialog + addToIndex() + + + 372 + 217 + + + 385 + 103 + + + + + buttonFromIndex + clicked() + EditIndexDialog + removeFromIndex() + + + 382 + 257 + + + 350 + 95 + + + + + tableIndexColumns + doubleClicked(QModelIndex) + EditIndexDialog + removeFromIndex(QModelIndex) + + + 520 + 236 + + + 684 + 175 + + + + + tableTableColumns + doubleClicked(QModelIndex) + EditIndexDialog + addToIndex(QModelIndex) + + + 104 + 228 + + + 19 + 205 + + + tableChanged(QString) checkInput() + addToIndex() + addToIndex(QModelIndex) + removeFromIndex() + removeFromIndex(QModelIndex)