diff --git a/src/CreateIndexDialog.cpp b/src/CreateIndexDialog.cpp index c95b61e1d..ebb78b285 100644 --- a/src/CreateIndexDialog.cpp +++ b/src/CreateIndexDialog.cpp @@ -1,7 +1,6 @@ #include "CreateIndexDialog.h" #include "ui_CreateIndexDialog.h" #include "sqlitedb.h" -#include "sqlitetypes.h" #include #include @@ -9,7 +8,8 @@ CreateIndexDialog::CreateIndexDialog(DBBrowserDB& db, QWidget* parent) : QDialog(parent), pdb(db), - ui(new Ui::CreateIndexDialog) + ui(new Ui::CreateIndexDialog), + index(sqlb::Index(QString(""))) { // Create UI ui->setupUi(this); @@ -24,6 +24,8 @@ CreateIndexDialog::CreateIndexDialog(DBBrowserDB& db, QWidget* parent) QHeaderView *tableHeaderView = ui->tableIndexColumns->horizontalHeader(); tableHeaderView->setSectionResizeMode(0, QHeaderView::Stretch); + + updateSqlText(); } CreateIndexDialog::~CreateIndexDialog() @@ -33,6 +35,10 @@ CreateIndexDialog::~CreateIndexDialog() void CreateIndexDialog::tableChanged(const QString& new_table) { + // Set the table name and clear all index columns + index.setTable(new_table); + index.clearColumns(); + // And fill the table again QStringList fields = pdb.getObjectByName(new_table).table.fieldNames(); ui->tableIndexColumns->setRowCount(fields.size()); @@ -55,45 +61,60 @@ void CreateIndexDialog::tableChanged(const QString& new_table) order->addItem("ASC"); order->addItem("DESC"); ui->tableIndexColumns->setCellWidget(i, 2, order); + connect(order, static_cast(&QComboBox::currentTextChanged), + [=](QString new_order) + { + int colnum = index.findColumn(fields.at(i)); + if(colnum != -1) + { + index.column(colnum)->setOrder(new_order); + updateSqlText(); + } + }); } + + updateSqlText(); } void CreateIndexDialog::checkInput() { + // Check if index name is set bool valid = true; if(ui->editIndexName->text().isEmpty()) valid = false; - int num_columns = 0; + // Check if index has any columns + index.clearColumns(); for(int i=0; i < ui->tableIndexColumns->rowCount(); ++i) { if(ui->tableIndexColumns->item(i, 1) && ui->tableIndexColumns->item(i, 1)->data(Qt::CheckStateRole) == Qt::Checked) - num_columns++; + { + index.addColumn(sqlb::IndexedColumnPtr(new sqlb::IndexedColumn( + ui->tableIndexColumns->item(i, 0)->text(), + qobject_cast(ui->tableIndexColumns->cellWidget(i, 2))->currentText()))); + } } - if(num_columns == 0) + if(index.columns().size() == 0) valid = false; + // Only activate OK button if index data is valid ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(valid); + + // Set the index name and the unique flag + index.setName(ui->editIndexName->text()); + index.setUnique(ui->checkIndexUnique->isChecked()); + updateSqlText(); } void CreateIndexDialog::accept() { - sqlb::Index index(ui->editIndexName->text()); - index.setUnique(ui->checkIndexUnique->isChecked()); - index.setTable(ui->comboTableName->currentText()); - - for(int i=0; i < ui->tableIndexColumns->rowCount(); ++i) - { - if(ui->tableIndexColumns->item(i, 1)->data(Qt::CheckStateRole) == Qt::Checked) - { - index.addColumn(sqlb::IndexedColumnPtr(new sqlb::IndexedColumn( - ui->tableIndexColumns->item(i, 0)->text(), - qobject_cast(ui->tableIndexColumns->cellWidget(i, 2))->currentText()))); - } - } - if(pdb.executeSQL(index.sql())) QDialog::accept(); else QMessageBox::warning(this, QApplication::applicationName(), tr("Creating the index failed:\n%1").arg(pdb.lastError())); } + +void CreateIndexDialog::updateSqlText() +{ + ui->sqlTextEdit->setText(index.sql()); +} diff --git a/src/CreateIndexDialog.h b/src/CreateIndexDialog.h index 46df8909d..edc46c7a8 100644 --- a/src/CreateIndexDialog.h +++ b/src/CreateIndexDialog.h @@ -1,6 +1,8 @@ #ifndef CREATEINDEXDIALOG_H #define CREATEINDEXDIALOG_H +#include "sqlitetypes.h" + #include class DBBrowserDB; @@ -25,6 +27,9 @@ private slots: private: DBBrowserDB& pdb; Ui::CreateIndexDialog* ui; + sqlb::Index index; + + void updateSqlText(); }; #endif diff --git a/src/CreateIndexDialog.ui b/src/CreateIndexDialog.ui index 96d6c2d7e..8dabbcf32 100644 --- a/src/CreateIndexDialog.ui +++ b/src/CreateIndexDialog.ui @@ -7,7 +7,7 @@ 0 0 610 - 403 + 504 @@ -39,62 +39,6 @@ - - - - &Columns - - - tableIndexColumns - - - - - - - - 0 - 1 - - - - - 0 - 250 - - - - QAbstractItemView::NoEditTriggers - - - false - - - true - - - QAbstractItemView::NoSelection - - - false - - - - Column - - - - - Use in Index - - - - - Order - - - - @@ -125,6 +69,78 @@ + + + + &Columns + + + tableIndexColumns + + + + + + + Qt::Vertical + + + + + 0 + 1 + + + + + 0 + 250 + + + + QAbstractItemView::NoEditTriggers + + + false + + + true + + + QAbstractItemView::NoSelection + + + false + + + + Column + + + + + Use in Index + + + + + Order + + + + + + + 0 + 100 + + + + true + + + + @@ -139,6 +155,14 @@ + + + SqlTextEdit + QWidget +
sqltextedit.h
+ 1 +
+
editIndexName comboTableName @@ -230,6 +254,22 @@ + + checkIndexUnique + toggled(bool) + CreateIndexDialog + checkInput() + + + 332 + 90 + + + 304 + 251 + + + tableChanged(QString) diff --git a/src/sqlitetypes.h b/src/sqlitetypes.h index 1db869c34..879a62609 100644 --- a/src/sqlitetypes.h +++ b/src/sqlitetypes.h @@ -306,10 +306,11 @@ class Index void setColumns(const IndexedColumnVector& columns); const IndexedColumnVector& columns() const { return m_columns; } + void clearColumns() { m_columns.clear(); } void addColumn(const IndexedColumnPtr& c) { m_columns.append(c); } bool removeColumn(const QString& name); void setColumn(int index, IndexedColumnPtr c) { m_columns[index] = c; } - const IndexedColumnPtr& column(int index) const { return m_columns[index]; } + IndexedColumnPtr& column(int index) { return m_columns[index]; } int findColumn(const QString& name) const; QStringList columnSqlList() const;