diff --git a/src/CreateIndexDialog.cpp b/src/CreateIndexDialog.cpp new file mode 100644 index 00000000..dc534909 --- /dev/null +++ b/src/CreateIndexDialog.cpp @@ -0,0 +1,97 @@ +#include "CreateIndexDialog.h" +#include "ui_CreateIndexDialog.h" +#include "sqlitedb.h" +#include +#include + +CreateIndexDialog::CreateIndexDialog(DBBrowserDB* db, QWidget* parent) + : QDialog(parent), + pdb(db), + ui(new Ui::CreateIndexDialog) +{ + // Create UI + ui->setupUi(this); + + // Fill table combobox + QList tables = pdb->objMap.values("table"); + for(int i=0;icomboTableName->addItem(tables.at(i).getname()); +} + +CreateIndexDialog::~CreateIndexDialog() +{ + delete ui; +} + +void CreateIndexDialog::tableChanged(QString new_table) +{ + // And fill the table again + QStringList fields = pdb->getTableFields(new_table); + ui->tableIndexColumns->setRowCount(fields.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 + QTableWidgetItem* enabled = new QTableWidgetItem(""); + enabled->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsUserCheckable); + enabled->setCheckState(Qt::Unchecked); + ui->tableIndexColumns->setItem(i, 1, enabled); + + // 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("ASC"); + order->addItem("DESC"); + ui->tableIndexColumns->setCellWidget(i, 2, order); + } +} + +void CreateIndexDialog::checkInput() +{ + ui->editIndexName->setText(ui->editIndexName->text().trimmed()); + + bool valid = true; + if(ui->editIndexName->text().isEmpty() || ui->editIndexName->text().contains(" ")) + valid = false; + + int num_columns = 0; + for(int i=0;itableIndexColumns->rowCount();i++) + { + if(ui->tableIndexColumns->item(i, 1) && ui->tableIndexColumns->item(i, 1)->data(Qt::CheckStateRole) == Qt::Checked) + num_columns++; + } + if(num_columns == 0) + valid = false; + + ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(valid); +} + +void CreateIndexDialog::accept() +{ + QString sql = QString("CREATE %1 INDEX `%2` ON `%3` (") + .arg(ui->checkIndexUnique->isChecked() ? "UNIQUE" : "") + .arg(ui->editIndexName->text()) + .arg(ui->comboTableName->currentText()); + + for(int i=0;itableIndexColumns->rowCount();i++) + { + if(ui->tableIndexColumns->item(i, 1)->data(Qt::CheckStateRole) == Qt::Checked) + { + sql.append(QString("`%1` %2,") + .arg(ui->tableIndexColumns->item(i, 0)->text()) + .arg(qobject_cast(ui->tableIndexColumns->cellWidget(i, 2))->currentText())); + } + } + sql.remove(sql.count() - 1, 1); // Remove last comma + sql.append(");"); + + if(pdb->executeSQL(sql)) + { + QDialog::accept(); + } else { + QMessageBox::warning(this, QApplication::applicationName(), tr("Creating the index failed:\n").arg(pdb->lastErrorMessage)); + } +} diff --git a/src/CreateIndexDialog.h b/src/CreateIndexDialog.h new file mode 100644 index 00000000..120ee884 --- /dev/null +++ b/src/CreateIndexDialog.h @@ -0,0 +1,29 @@ +#ifndef __CREATEINDEXDIALOG_H__ +#define __CREATEINDEXDIALOG_H__ + +#include +class DBBrowserDB; + +namespace Ui { +class CreateIndexDialog; +} + +class CreateIndexDialog : public QDialog +{ + Q_OBJECT + +public: + CreateIndexDialog(DBBrowserDB* db, QWidget* parent = 0); + ~CreateIndexDialog(); + +private slots: + void accept(); + void tableChanged(QString new_table); + void checkInput(); + +private: + DBBrowserDB* pdb; + Ui::CreateIndexDialog* ui; +}; + +#endif diff --git a/src/CreateIndexDialog.ui b/src/CreateIndexDialog.ui new file mode 100644 index 00000000..a6aa4a94 --- /dev/null +++ b/src/CreateIndexDialog.ui @@ -0,0 +1,226 @@ + + + CreateIndexDialog + + + + 0 + 0 + 610 + 342 + + + + Create New Index + + + + :/icons/index_create:/icons/index_create + + + + + + + + &Name + + + editIndexName + + + + + + + + + + &Columns + + + tableIndexColumns + + + + + + + + 0 + 250 + + + + QAbstractItemView::NoEditTriggers + + + false + + + true + + + QAbstractItemView::NoSelection + + + false + + + + Column + + + + + Use in Index + + + + + Order + + + + + + + + &Table + + + comboTableName + + + + + + + + + + &Unique + + + checkIndexUnique + + + + + + + + + + + + + + + + Qt::Horizontal + + + QDialogButtonBox::Cancel|QDialogButtonBox::Ok + + + + + + + editIndexName + comboTableName + checkIndexUnique + tableIndexColumns + buttonBox + + + + + + + buttonBox + accepted() + CreateIndexDialog + accept() + + + 252 + 337 + + + 157 + 274 + + + + + buttonBox + rejected() + CreateIndexDialog + reject() + + + 320 + 337 + + + 286 + 274 + + + + + comboTableName + currentIndexChanged(QString) + CreateIndexDialog + tableChanged(QString) + + + 91 + 43 + + + 236 + 31 + + + + + editIndexName + textChanged(QString) + CreateIndexDialog + checkInput() + + + 429 + 14 + + + 443 + 39 + + + + + tableIndexColumns + cellChanged(int,int) + CreateIndexDialog + checkInput() + + + 443 + 170 + + + 566 + 40 + + + + + + tableChanged(QString) + checkInput() + + diff --git a/src/CreateIndexForm.cpp b/src/CreateIndexForm.cpp deleted file mode 100644 index 0c55bcba..00000000 --- a/src/CreateIndexForm.cpp +++ /dev/null @@ -1,104 +0,0 @@ -#include "CreateIndexForm.h" -#include -/* - * Constructs a createIndexForm as a child of 'parent', with the - * name 'name' and widget flags set to 'f'. - * - * The dialog will by default be modeless, unless you set 'modal' to - * true to construct a modal dialog. - */ -createIndexForm::createIndexForm(QWidget* parent, Qt::WindowFlags fl) - : QDialog(parent, fl) -{ - setupUi(this); - -} - -/* - * Destroys the object and frees any allocated resources - */ -createIndexForm::~createIndexForm() -{ - // no need to delete child widgets, Qt does it all for us -} - -/* - * Sets the strings of the subwidgets using the current - * language. - */ -void createIndexForm::languageChange() -{ - retranslateUi(this); -} - - -void createIndexForm::tableSelected( const QString & entry ) -{ - objectMap::Iterator it; - for ( it = mtablemap.begin(); it != mtablemap.end(); ++it ) { - QString tname = it.value().getname() ; - - //populate the fields with first table name - if ((tname.compare(entry)==0)){ - comboFields->clear(); - fieldMap::Iterator fit; - fieldMap fmap = it.value().fldmap; - for ( fit = fmap.begin(); fit != fmap.end(); ++fit ) { - comboFields->addItem( fit.value().getname() ); - } - } - } -} - -void createIndexForm::confirmCreate() -{ - bool ok = true; - QString idxname = indexLineEdit->text(); - if (idxname.isEmpty()) { - ok = false; - QMessageBox::information( this, QApplication::applicationName(), "Please select a name for the index" ); - return; - } - if (idxname.contains(" ")>0) { - ok = false; - QMessageBox::warning( this, QApplication::applicationName(), "Spaces are not allowed in the index name" ); - return; - } - if (ok){ - createStatement = "CREATE "; - if (comboUnique->currentIndex()==1){ - createStatement.append("UNIQUE "); - } - createStatement.append("INDEX `"); - createStatement.append(indexLineEdit->text()); - createStatement.append("` ON "); - createStatement.append(comboTables->currentText()); - createStatement.append("("); - createStatement.append(comboFields->currentText()); - createStatement.append(" "); - if (comboOrder->currentIndex()==0){ - createStatement.append("ASC"); - } else { - createStatement.append("DESC"); - } - createStatement.append(");"); - accept(); - } -} - -void createIndexForm::populateTable(const QList& rmap) -{ - QList::ConstIterator it; - for ( it = rmap.begin(); it != rmap.end(); ++it ) { - comboTables->addItem( (*it).getname() ); - - //populate the fields with first table name - /*if (it==mtablemap.begin()){ - fieldMap::Iterator fit; - fieldMap fmap = (*it).value().fldmap; - for ( fit = fmap.begin(); fit != fmap.end(); ++fit ) { - comboFields->addItem( fit.value().getname() ); - } - }*/ - } -} diff --git a/src/CreateIndexForm.h b/src/CreateIndexForm.h deleted file mode 100644 index 9d637575..00000000 --- a/src/CreateIndexForm.h +++ /dev/null @@ -1,257 +0,0 @@ -#ifndef CREATEINDEXFORM_H -#define CREATEINDEXFORM_H - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include "sqlitedb.h" - -class Ui_createIndexForm -{ -public: - QVBoxLayout *vboxLayout; - QHBoxLayout *hboxLayout; - QLabel *textLabel2; - QLineEdit *indexLineEdit; - QGroupBox *groupBox2; - QGridLayout *gridLayout; - QVBoxLayout *vboxLayout1; - QComboBox *comboTables; - QComboBox *comboFields; - QComboBox *comboOrder; - QComboBox *comboUnique; - QVBoxLayout *vboxLayout2; - QLabel *textLabel3; - QLabel *textLabel4; - QLabel *textLabel5; - QLabel *textLabel6; - QHBoxLayout *hboxLayout1; - QSpacerItem *spacer8; - QPushButton *buttonCreate; - QPushButton *buttonCancel; - - void setupUi(QDialog *createIndexForm) - { - if (createIndexForm->objectName().isEmpty()) - createIndexForm->setObjectName(QString::fromUtf8("createIndexForm")); - createIndexForm->resize(300, 258); - vboxLayout = new QVBoxLayout(createIndexForm); - vboxLayout->setSpacing(6); - vboxLayout->setContentsMargins(11, 11, 11, 11); - vboxLayout->setObjectName(QString::fromUtf8("vboxLayout")); - hboxLayout = new QHBoxLayout(); - hboxLayout->setSpacing(6); - hboxLayout->setObjectName(QString::fromUtf8("hboxLayout")); - textLabel2 = new QLabel(createIndexForm); - textLabel2->setObjectName(QString::fromUtf8("textLabel2")); - textLabel2->setWordWrap(false); - - hboxLayout->addWidget(textLabel2); - - indexLineEdit = new QLineEdit(createIndexForm); - indexLineEdit->setObjectName(QString::fromUtf8("indexLineEdit")); - - hboxLayout->addWidget(indexLineEdit); - - - vboxLayout->addLayout(hboxLayout); - - groupBox2 = new QGroupBox(createIndexForm); - groupBox2->setObjectName(QString::fromUtf8("groupBox2")); - - gridLayout = new QGridLayout(); - groupBox2->setLayout(gridLayout); - groupBox2->layout()->setSpacing(6); - groupBox2->layout()->setContentsMargins(11, 11, 11, 11); - - gridLayout->setAlignment(Qt::AlignTop); - gridLayout->setObjectName(QString::fromUtf8("gridLayout")); - vboxLayout1 = new QVBoxLayout(); - vboxLayout1->setSpacing(6); - vboxLayout1->setObjectName(QString::fromUtf8("vboxLayout1")); - comboTables = new QComboBox(groupBox2); - comboTables->setObjectName(QString::fromUtf8("comboTables")); - - vboxLayout1->addWidget(comboTables); - - comboFields = new QComboBox(groupBox2); - comboFields->setObjectName(QString::fromUtf8("comboFields")); - - vboxLayout1->addWidget(comboFields); - - comboOrder = new QComboBox(groupBox2); - comboOrder->setObjectName(QString::fromUtf8("comboOrder")); - - vboxLayout1->addWidget(comboOrder); - - comboUnique = new QComboBox(groupBox2); - comboUnique->setObjectName(QString::fromUtf8("comboUnique")); - - vboxLayout1->addWidget(comboUnique); - - - gridLayout->addLayout(vboxLayout1, 0, 1, 1, 1); - - vboxLayout2 = new QVBoxLayout(); - vboxLayout2->setSpacing(6); - vboxLayout2->setObjectName(QString::fromUtf8("vboxLayout2")); - textLabel3 = new QLabel(groupBox2); - textLabel3->setObjectName(QString::fromUtf8("textLabel3")); - textLabel3->setAlignment(Qt::AlignVCenter|Qt::AlignRight); - textLabel3->setWordWrap(false); - - vboxLayout2->addWidget(textLabel3); - - textLabel4 = new QLabel(groupBox2); - textLabel4->setObjectName(QString::fromUtf8("textLabel4")); - textLabel4->setAlignment(Qt::AlignVCenter|Qt::AlignRight); - textLabel4->setWordWrap(false); - - vboxLayout2->addWidget(textLabel4); - - textLabel5 = new QLabel(groupBox2); - textLabel5->setObjectName(QString::fromUtf8("textLabel5")); - textLabel5->setAlignment(Qt::AlignVCenter|Qt::AlignRight); - textLabel5->setWordWrap(false); - - vboxLayout2->addWidget(textLabel5); - - textLabel6 = new QLabel(groupBox2); - textLabel6->setObjectName(QString::fromUtf8("textLabel6")); - textLabel6->setAlignment(Qt::AlignVCenter|Qt::AlignRight); - textLabel6->setWordWrap(false); - - vboxLayout2->addWidget(textLabel6); - - - gridLayout->addLayout(vboxLayout2, 0, 0, 1, 1); - - - vboxLayout->addWidget(groupBox2); - - hboxLayout1 = new QHBoxLayout(); - hboxLayout1->setSpacing(6); - hboxLayout1->setObjectName(QString::fromUtf8("hboxLayout1")); - spacer8 = new QSpacerItem(51, 20, QSizePolicy::Expanding, QSizePolicy::Minimum); - - hboxLayout1->addItem(spacer8); - - buttonCreate = new QPushButton(createIndexForm); - buttonCreate->setObjectName(QString::fromUtf8("buttonCreate")); - - hboxLayout1->addWidget(buttonCreate); - - buttonCancel = new QPushButton(createIndexForm); - buttonCancel->setObjectName(QString::fromUtf8("buttonCancel")); - buttonCancel->setDefault(true); - - hboxLayout1->addWidget(buttonCancel); - - - vboxLayout->addLayout(hboxLayout1); - - - retranslateUi(createIndexForm); - QObject::connect(buttonCreate, SIGNAL(clicked()), createIndexForm, SLOT(confirmCreate())); - QObject::connect(buttonCancel, SIGNAL(clicked()), createIndexForm, SLOT(reject())); - QObject::connect(comboTables, SIGNAL(activated(QString)), createIndexForm, SLOT(tableSelected(QString))); - - QMetaObject::connectSlotsByName(createIndexForm); - } // setupUi - - void retranslateUi(QDialog *createIndexForm) - { - createIndexForm->setWindowTitle(QApplication::translate("createIndexForm", "Create Index", 0, QApplication::UnicodeUTF8)); - textLabel2->setText(QApplication::translate("createIndexForm", "Index name:", 0, QApplication::UnicodeUTF8)); - indexLineEdit->setText(QString()); -#ifndef QT_NO_TOOLTIP - indexLineEdit->setProperty("toolTip", QVariant(QApplication::translate("createIndexForm", "Enter the name for the new index", 0, QApplication::UnicodeUTF8))); -#endif // QT_NO_TOOLTIP -#ifndef QT_NO_WHATSTHIS - indexLineEdit->setProperty("whatsThis", QVariant(QApplication::translate("createIndexForm", "This area contains the name of the index to be created", 0, QApplication::UnicodeUTF8))); -#endif // QT_NO_WHATSTHIS - groupBox2->setTitle(QApplication::translate("createIndexForm", "Define properties:", 0, QApplication::UnicodeUTF8)); -#ifndef QT_NO_TOOLTIP - comboTables->setProperty("toolTip", QVariant(QApplication::translate("createIndexForm", "Choose the table to index", 0, QApplication::UnicodeUTF8))); -#endif // QT_NO_TOOLTIP -#ifndef QT_NO_WHATSTHIS - comboTables->setProperty("whatsThis", QVariant(QApplication::translate("createIndexForm", "This control is used to select the table to be indexed. Changing the selected table will automatically update the fields available in the control below", 0, QApplication::UnicodeUTF8))); -#endif // QT_NO_WHATSTHIS -#ifndef QT_NO_TOOLTIP - comboFields->setProperty("toolTip", QVariant(QApplication::translate("createIndexForm", "Choose the field to be indexed", 0, QApplication::UnicodeUTF8))); -#endif // QT_NO_TOOLTIP -#ifndef QT_NO_WHATSTHIS - comboFields->setProperty("whatsThis", QVariant(QApplication::translate("createIndexForm", "This control specifies the field to be used as an index", 0, QApplication::UnicodeUTF8))); -#endif // QT_NO_WHATSTHIS - comboOrder->clear(); - comboOrder->insertItems(0, QStringList() - << QApplication::translate("createIndexForm", "Ascending", 0, QApplication::UnicodeUTF8) - << QApplication::translate("createIndexForm", "Descending", 0, QApplication::UnicodeUTF8) - ); -#ifndef QT_NO_TOOLTIP - comboOrder->setProperty("toolTip", QVariant(QApplication::translate("createIndexForm", "Choose the index order", 0, QApplication::UnicodeUTF8))); -#endif // QT_NO_TOOLTIP -#ifndef QT_NO_WHATSTHIS - comboOrder->setProperty("whatsThis", QVariant(QApplication::translate("createIndexForm", "This option controls the ordering of the index. Ascending is the recommended ordering", 0, QApplication::UnicodeUTF8))); -#endif // QT_NO_WHATSTHIS - comboUnique->clear(); - comboUnique->insertItems(0, QStringList() - << QApplication::translate("createIndexForm", "Allowed", 0, QApplication::UnicodeUTF8) - << QApplication::translate("createIndexForm", "Not allowed", 0, QApplication::UnicodeUTF8) - ); -#ifndef QT_NO_TOOLTIP - comboUnique->setProperty("toolTip", QVariant(QApplication::translate("createIndexForm", "Allow duplicate values in the index field", 0, QApplication::UnicodeUTF8))); -#endif // QT_NO_TOOLTIP -#ifndef QT_NO_WHATSTHIS - comboUnique->setProperty("whatsThis", QVariant(QApplication::translate("createIndexForm", "This control determines if the indexed field allows duplicate values to be inserted into the database. Attempting to insert a duplicate value in an indexed fiield that does not allow this option will generate an error", 0, QApplication::UnicodeUTF8))); -#endif // QT_NO_WHATSTHIS - textLabel3->setText(QApplication::translate("createIndexForm", "Table to index:", 0, QApplication::UnicodeUTF8)); - textLabel4->setText(QApplication::translate("createIndexForm", "Field to index:", 0, QApplication::UnicodeUTF8)); - textLabel5->setText(QApplication::translate("createIndexForm", "Indexing order:", 0, QApplication::UnicodeUTF8)); - textLabel6->setText(QApplication::translate("createIndexForm", "Duplicate values:", 0, QApplication::UnicodeUTF8)); - buttonCreate->setText(QApplication::translate("createIndexForm", "Create", 0, QApplication::UnicodeUTF8)); -#ifndef QT_NO_TOOLTIP - buttonCreate->setProperty("toolTip", QVariant(QApplication::translate("createIndexForm", "Create Index", 0, QApplication::UnicodeUTF8))); -#endif // QT_NO_TOOLTIP - buttonCancel->setText(QApplication::translate("createIndexForm", "Cancel", 0, QApplication::UnicodeUTF8)); -#ifndef QT_NO_TOOLTIP - buttonCancel->setProperty("toolTip", QVariant(QApplication::translate("createIndexForm", "Cancel and close dialog box", 0, QApplication::UnicodeUTF8))); -#endif // QT_NO_TOOLTIP - } // retranslateUi - -}; - -namespace Ui { - class createIndexForm: public Ui_createIndexForm {}; -} // namespace Ui - -class createIndexForm : public QDialog, public Ui::createIndexForm -{ - Q_OBJECT - -public: - createIndexForm(QWidget* parent = 0, Qt::WindowFlags fl = Qt::Window); - ~createIndexForm(); - - objectMap mtablemap; - QString createStatement; - -public slots: - virtual void tableSelected( const QString & entry ); - virtual void confirmCreate(); - virtual void populateTable(const QList &rmap ); - -protected slots: - virtual void languageChange(); - -}; - -#endif // CREATEINDEXFORM_H diff --git a/src/MainWindow.cpp b/src/MainWindow.cpp index 7532d236..149391fd 100644 --- a/src/MainWindow.cpp +++ b/src/MainWindow.cpp @@ -11,7 +11,7 @@ #include #include #include -#include "CreateIndexForm.h" +#include "CreateIndexDialog.h" #include "DialogAbout.h" #include "EditTableForm.h" #include "EditFieldForm.h" @@ -566,18 +566,11 @@ void MainWindow::createIndex() QMessageBox::information( this, QApplication::applicationName(), "There is no database opened. Please open or create a new database file." ); return; } - createIndexForm dialog(this); - dialog.populateTable(db.objMap.values("table")); + CreateIndexDialog dialog(&db, this); if(dialog.exec()) { - if (!db.executeSQL(dialog.createStatement)){ - QString error = "Error: could not create the index. Message from database engine: "; - error.append(db.lastErrorMessage); - QMessageBox::warning( this, QApplication::applicationName(), error ); - } else { - populateStructure(); - resetBrowser(); - } + populateStructure(); + resetBrowser(); } } diff --git a/src/src.pro b/src/src.pro index 83d3835c..2e545dc8 100644 --- a/src/src.pro +++ b/src/src.pro @@ -14,7 +14,6 @@ HEADERS += \ sqlitedb.h \ sqlbrowser_util.h \ SQLLogDock.h \ - CreateIndexForm.h \ DialogAbout.h \ EditFieldForm.h \ EditForm.h \ @@ -24,14 +23,14 @@ HEADERS += \ ImportCSVForm.h \ MainWindow.h \ PreferencesForm.h \ - SQLiteSyntaxHighlighter.h + SQLiteSyntaxHighlighter.h \ + CreateIndexDialog.h SOURCES += \ sqlitedb.cpp \ sqlbrowser_util.c \ SQLLogDock.cpp \ main.cpp \ - CreateIndexForm.cpp \ DialogAbout.cpp \ EditFieldForm.cpp \ EditForm.cpp \ @@ -41,7 +40,8 @@ SOURCES += \ ImportCSVForm.cpp \ MainWindow.cpp \ PreferencesForm.cpp \ - SQLiteSyntaxHighlighter.cpp + SQLiteSyntaxHighlighter.cpp \ + CreateIndexDialog.cpp QMAKE_CXXFLAGS += -DAPP_VERSION=\\\"`cd $$PWD;git log -n1 --format=%h_git`\\\" @@ -67,4 +67,5 @@ FORMS += \ EditFieldForm.ui \ EditTableForm.ui \ MainWindow.ui \ - PreferencesForm.ui + PreferencesForm.ui \ + CreateIndexDialog.ui