Add Edit Index feature

Improve the Create Index dialog so that it allows creating as well as
editing indices.

Chenge the code for the main window to allow editing existing indices.
This commit is contained in:
Martin Kleusberg
2017-01-20 12:15:49 +01:00
parent 81dedbf98b
commit c9ceb5da59
10 changed files with 120 additions and 50 deletions
+70 -21
View File
@@ -5,11 +5,13 @@
#include <QMessageBox>
#include <QPushButton>
CreateIndexDialog::CreateIndexDialog(DBBrowserDB& db, QWidget* parent)
CreateIndexDialog::CreateIndexDialog(DBBrowserDB& db, const QString& indexName, bool createIndex, QWidget* parent)
: QDialog(parent),
pdb(db),
ui(new Ui::CreateIndexDialog),
index(sqlb::Index(QString("")))
curIndex(indexName),
index(indexName),
newIndex(createIndex),
ui(new Ui::CreateIndexDialog)
{
// Create UI
ui->setupUi(this);
@@ -19,12 +21,36 @@ CreateIndexDialog::CreateIndexDialog(DBBrowserDB& db, QWidget* parent)
QList<DBBrowserObject> tables = pdb.objMap.values("table");
for(auto it=tables.constBegin();it!=tables.constEnd();++it)
dbobjs.insert((*it).getname(), (*it));
ui->comboTableName->blockSignals(true);
for(auto it=dbobjs.constBegin();it!=dbobjs.constEnd();++it)
ui->comboTableName->addItem(QIcon(QString(":icons/table")), (*it).getname());
ui->comboTableName->blockSignals(false);
QHeaderView *tableHeaderView = ui->tableIndexColumns->horizontalHeader();
tableHeaderView->setSectionResizeMode(0, QHeaderView::Stretch);
// Editing an existing index?
if(!newIndex)
{
// Load the current layour and fill in the dialog fields
index = pdb.getObjectByName(curIndex).index;
ui->editIndexName->blockSignals(true);
ui->editIndexName->setText(index.name());
ui->editIndexName->blockSignals(false);
ui->checkIndexUnique->blockSignals(true);
ui->checkIndexUnique->setChecked(index.unique());
ui->checkIndexUnique->blockSignals(false);
ui->comboTableName->blockSignals(true);
ui->comboTableName->setCurrentText(index.table());
ui->comboTableName->blockSignals(false);
tableChanged(index.table(), true);
} else {
tableChanged(ui->comboTableName->currentText(), false);
}
// Refresh SQL preview
updateSqlText();
}
@@ -33,11 +59,14 @@ CreateIndexDialog::~CreateIndexDialog()
delete ui;
}
void CreateIndexDialog::tableChanged(const QString& new_table)
void CreateIndexDialog::tableChanged(const QString& new_table, bool initialLoad)
{
// Set the table name and clear all index columns
index.setTable(new_table);
index.clearColumns();
if(!initialLoad)
{
index.setTable(new_table);
index.clearColumns();
}
// And fill the table again
QStringList fields = pdb.getObjectByName(new_table).table.fieldNames();
@@ -50,16 +79,36 @@ void CreateIndexDialog::tableChanged(const QString& new_table)
ui->tableIndexColumns->setItem(i, 0, name);
// Put a checkbox to enable usage in the index of this field in the second column
QTableWidgetItem* enabled = new QTableWidgetItem("");
enabled->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsUserCheckable);
enabled->setCheckState(Qt::Unchecked);
ui->tableIndexColumns->setItem(i, 1, enabled);
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<void(QCheckBox::*)(bool)>(&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<QComboBox*>(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);
connect(order, static_cast<void(QComboBox::*)(const QString&)>(&QComboBox::currentTextChanged),
[=](QString new_order)
@@ -84,17 +133,6 @@ void CreateIndexDialog::checkInput()
valid = false;
// 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)
{
index.addColumn(sqlb::IndexedColumnPtr(new sqlb::IndexedColumn(
ui->tableIndexColumns->item(i, 0)->text(),
false,
qobject_cast<QComboBox*>(ui->tableIndexColumns->cellWidget(i, 2))->currentText())));
}
}
if(index.columns().size() == 0)
valid = false;
@@ -109,6 +147,17 @@ void CreateIndexDialog::checkInput()
void CreateIndexDialog::accept()
{
// When editing an index, delete the old one first
if(!newIndex)
{
if(!pdb.executeSQL(QString("DROP INDEX %1;").arg(sqlb::escapeIdentifier(curIndex))))
{
QMessageBox::warning(this, qApp->applicationName(), tr("Deleting the old index failed:\n%1").arg(pdb.lastError()));
return;
}
}
// Create the new index
if(pdb.executeSQL(index.sql()))
QDialog::accept();
else
+5 -3
View File
@@ -16,18 +16,20 @@ class CreateIndexDialog : public QDialog
Q_OBJECT
public:
explicit CreateIndexDialog(DBBrowserDB& db, QWidget* parent = 0);
explicit CreateIndexDialog(DBBrowserDB& db, const QString& indexName, bool createIndex, QWidget* parent = 0);
~CreateIndexDialog();
private slots:
void accept();
void tableChanged(const QString& new_table);
void tableChanged(const QString& new_table, bool initialLoad = false);
void checkInput();
private:
DBBrowserDB& pdb;
Ui::CreateIndexDialog* ui;
QString curIndex;
sqlb::Index index;
bool newIndex;
Ui::CreateIndexDialog* ui;
void updateSqlText();
};
+34 -18
View File
@@ -137,7 +137,7 @@ void MainWindow::init()
// Create popup menus
popupTableMenu = new QMenu(this);
popupTableMenu->addAction(ui->actionEditBrowseTable);
popupTableMenu->addAction(ui->editModifyTableAction);
popupTableMenu->addAction(ui->editModifyObjectAction);
popupTableMenu->addAction(ui->editDeleteObjectAction);
popupTableMenu->addSeparator();
popupTableMenu->addAction(ui->actionEditCopyCreateStatement);
@@ -724,8 +724,9 @@ void MainWindow::createIndex()
return;
}
CreateIndexDialog dialog(db, this);
dialog.exec();
CreateIndexDialog dialog(db, "", true, this);
if(dialog.exec())
populateTable();
}
void MainWindow::compact()
@@ -757,20 +758,25 @@ void MainWindow::deleteObject()
}
}
void MainWindow::editTable()
void MainWindow::editObject()
{
if (!db.isOpen()){
QMessageBox::information( this, QApplication::applicationName(), tr("There is no database opened."));
if(!ui->dbTreeWidget->selectionModel()->hasSelection())
return;
}
if(!ui->dbTreeWidget->selectionModel()->hasSelection()){
return;
}
QString tableToEdit = ui->dbTreeWidget->model()->data(ui->dbTreeWidget->currentIndex().sibling(ui->dbTreeWidget->currentIndex().row(), 0)).toString();
EditTableDialog dialog(db, tableToEdit, false, this);
if(dialog.exec())
populateTable();
// Get name and type of the object to edit
QString name = ui->dbTreeWidget->model()->data(ui->dbTreeWidget->currentIndex().sibling(ui->dbTreeWidget->currentIndex().row(), 0), Qt::EditRole).toString();
QString type = ui->dbTreeWidget->model()->data(ui->dbTreeWidget->currentIndex().sibling(ui->dbTreeWidget->currentIndex().row(), 1), Qt::EditRole).toString();
if(type == "table")
{
EditTableDialog dialog(db, name, false, this);
if(dialog.exec())
populateTable();
} else if(type == "index") {
CreateIndexDialog dialog(db, name, false, this);
if(dialog.exec())
populateTable();
}
}
void MainWindow::helpWhatsThis()
@@ -1224,7 +1230,7 @@ void MainWindow::changeTreeSelection()
{
// Just assume first that something's selected that can not be edited at all
ui->editDeleteObjectAction->setEnabled(false);
ui->editModifyTableAction->setEnabled(false);
ui->editModifyObjectAction->setEnabled(false);
ui->actionEditBrowseTable->setEnabled(false);
if(!ui->dbTreeWidget->currentIndex().isValid())
@@ -1236,30 +1242,40 @@ void MainWindow::changeTreeSelection()
if (type.isEmpty())
{
ui->editDeleteObjectAction->setIcon(QIcon(":icons/table_delete"));
ui->editModifyObjectAction->setIcon(QIcon(":icons/table_modify"));
} else {
ui->editDeleteObjectAction->setIcon(QIcon(QString(":icons/%1_delete").arg(type)));
ui->editModifyObjectAction->setIcon(QIcon(QString(":icons/%1_modify").arg(type)));
}
if (type == "view") {
ui->editDeleteObjectAction->setText(tr("Delete View"));
ui->editDeleteObjectAction->setToolTip(tr("Delete View"));
ui->editModifyObjectAction->setText(tr("Modify View"));
ui->editModifyObjectAction->setToolTip(tr("Modify View"));
} else if(type == "trigger") {
ui->editDeleteObjectAction->setText(tr("Delete Trigger"));
ui->editDeleteObjectAction->setToolTip(tr("Delete Trigger"));
ui->editModifyObjectAction->setText(tr("Modify Trigger"));
ui->editModifyObjectAction->setToolTip(tr("Modify Trigger"));
} else if(type == "index") {
ui->editDeleteObjectAction->setText(tr("Delete Index"));
ui->editDeleteObjectAction->setToolTip(tr("Delete Index"));
ui->editModifyObjectAction->setText(tr("Modify Index"));
ui->editModifyObjectAction->setToolTip(tr("Modify Index"));
} else {
ui->editDeleteObjectAction->setText(tr("Delete Table"));
ui->editDeleteObjectAction->setToolTip(tr("Delete Table"));
ui->editModifyObjectAction->setText(tr("Modify Table"));
ui->editModifyObjectAction->setToolTip(tr("Modify Table"));
}
// Activate actions
if(type == "table")
if(type == "table" || type == "index")
{
ui->editDeleteObjectAction->setEnabled(!db.readOnly());
ui->editModifyTableAction->setEnabled(!db.readOnly());
} else if(type == "view" || type == "trigger" || type == "index") {
ui->editModifyObjectAction->setEnabled(!db.readOnly());
} else if(type == "view" || type == "trigger") {
ui->editDeleteObjectAction->setEnabled(!db.readOnly());
}
if(type == "table" || type == "view")
+1 -1
View File
@@ -194,7 +194,7 @@ private slots:
void createIndex();
void compact();
void deleteObject();
void editTable();
void editObject();
void helpWhatsThis();
void helpAbout();
void updateRecordText(const QPersistentModelIndex& idx, const QByteArray& text, bool isBlob);
+6 -6
View File
@@ -39,7 +39,7 @@
</property>
<addaction name="editCreateTableAction"/>
<addaction name="editCreateIndexAction"/>
<addaction name="editModifyTableAction"/>
<addaction name="editModifyObjectAction"/>
<addaction name="editDeleteObjectAction"/>
</widget>
</item>
@@ -898,7 +898,7 @@
<string>&amp;Edit</string>
</property>
<addaction name="editCreateTableAction"/>
<addaction name="editModifyTableAction"/>
<addaction name="editModifyObjectAction"/>
<addaction name="editDeleteObjectAction"/>
<addaction name="separator"/>
<addaction name="editCreateIndexAction"/>
@@ -1338,7 +1338,7 @@
<enum>QAction::NoRole</enum>
</property>
</action>
<action name="editModifyTableAction">
<action name="editModifyObjectAction">
<property name="enabled">
<bool>false</bool>
</property>
@@ -2173,10 +2173,10 @@
</hints>
</connection>
<connection>
<sender>editModifyTableAction</sender>
<sender>editModifyObjectAction</sender>
<signal>triggered()</signal>
<receiver>MainWindow</receiver>
<slot>editTable()</slot>
<slot>editObject()</slot>
<hints>
<hint type="sourcelabel">
<x>-1</x>
@@ -2824,7 +2824,7 @@
<slot>createIndex()</slot>
<slot>createTable()</slot>
<slot>deleteObject()</slot>
<slot>editTable()</slot>
<slot>editObject()</slot>
<slot>editTablePopup()</slot>
<slot>addField()</slot>
<slot>editField()</slot>
+3
View File
@@ -48,5 +48,8 @@
<file alias="copy">page_copy.png</file>
<file>resultset_previous.png</file>
<file>resultset_first.png</file>
<file alias="view_modify">picture_edit.png</file>
<file alias="trigger_modify">script_edit.png</file>
<file alias="index_modify">tag_blue_edit.png</file>
</qresource>
</RCC>
Binary file not shown.

After

Width:  |  Height:  |  Size: 826 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 880 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 748 B

+1 -1
View File
@@ -280,7 +280,7 @@ public:
void setExpression(bool expr) { m_isExpression = expr; }
bool expression() const { return m_isExpression; }
void setOrder(const QString& order) { m_order = order; }
void setOrder(const QString& order) { m_order = order.toUpper(); }
QString order() const { return m_order; }
QString toString(const QString& indent = "\t", const QString& sep = "\t") const;