From 43496c95498692069cc013caf7b0d211d8c5aefe Mon Sep 17 00:00:00 2001 From: Martin Kleusberg Date: Tue, 1 Jan 2013 16:23:12 +0100 Subject: [PATCH] Merge editTableForm and createTableForm Add the functionality needed to create new tables to the editTableForm dialog. Delete the createTableForm as it is no longer needed, also delete its semi-finished replacement createTableDialog. Fix not working add column function and as usual some other bugs here and there. --- src/createtabledialog.cpp | 14 --- src/createtabledialog.h | 22 ---- src/createtabledialog.ui | 183 -------------------------------- src/createtableform.cpp | 138 ------------------------ src/createtableform.h | 213 -------------------------------------- src/edittableform.cpp | 66 ++++++++---- src/mainwindow.cpp | 19 ++-- src/src.pro | 5 - 8 files changed, 50 insertions(+), 610 deletions(-) delete mode 100644 src/createtabledialog.cpp delete mode 100644 src/createtabledialog.h delete mode 100644 src/createtabledialog.ui delete mode 100644 src/createtableform.cpp delete mode 100644 src/createtableform.h diff --git a/src/createtabledialog.cpp b/src/createtabledialog.cpp deleted file mode 100644 index 60c6d066..00000000 --- a/src/createtabledialog.cpp +++ /dev/null @@ -1,14 +0,0 @@ -#include "createtabledialog.h" -#include "ui_createtabledialog.h" - -CreateTableDialog::CreateTableDialog(QWidget *parent) : - QDialog(parent), - ui(new Ui::CreateTableDialog) -{ - ui->setupUi(this); -} - -CreateTableDialog::~CreateTableDialog() -{ - delete ui; -} diff --git a/src/createtabledialog.h b/src/createtabledialog.h deleted file mode 100644 index cb382ba7..00000000 --- a/src/createtabledialog.h +++ /dev/null @@ -1,22 +0,0 @@ -#ifndef CREATETABLEDIALOG_H -#define CREATETABLEDIALOG_H - -#include - -namespace Ui { -class CreateTableDialog; -} - -class CreateTableDialog : public QDialog -{ - Q_OBJECT - -public: - explicit CreateTableDialog(QWidget *parent = 0); - ~CreateTableDialog(); - -private: - Ui::CreateTableDialog *ui; -}; - -#endif // CREATETABLEDIALOG_H diff --git a/src/createtabledialog.ui b/src/createtabledialog.ui deleted file mode 100644 index ae5aea94..00000000 --- a/src/createtabledialog.ui +++ /dev/null @@ -1,183 +0,0 @@ - - - CreateTableDialog - - - - 0 - 0 - 466 - 364 - - - - Dialog - - - - - - - - - Table name: - - - - - - - - - - - - - - - - GroupBox - - - - - - - - - Field name: - - - - - - - - - - - 35 - 16777215 - - - - - - - - :/icons/add:/icons/add - - - - - - - - - - - - - - 0 - 0 - - - - Data type: - - - - - - - - 0 - 0 - - - - - INTEGER - - - - - TEXT - - - - - REAL - - - - - BLOB - - - - - - - - Not NULL - - - - - - - - - - - - - Qt::Horizontal - - - QDialogButtonBox::Cancel|QDialogButtonBox::Ok - - - - - - - - - - - buttonBox - accepted() - CreateTableDialog - accept() - - - 248 - 254 - - - 157 - 274 - - - - - buttonBox - rejected() - CreateTableDialog - reject() - - - 316 - 260 - - - 286 - 274 - - - - - diff --git a/src/createtableform.cpp b/src/createtableform.cpp deleted file mode 100644 index e96a3f33..00000000 --- a/src/createtableform.cpp +++ /dev/null @@ -1,138 +0,0 @@ -#include "createtableform.h" -#include -#include "editfieldform.h" -/* - * Constructs a createTableForm 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. - */ -createTableForm::createTableForm(DBBrowserDB *db, QWidget* parent, Qt::WindowFlags fl) - : QDialog(parent, fl), - pdb(db) -{ - setupUi(this); - - init(); -} - -/* - * Destroys the object and frees any allocated resources - */ -createTableForm::~createTableForm() -{ - // no need to delete child widgets, Qt does it all for us -} - -/* - * Sets the strings of the subwidgets using the current - * language. - */ -void createTableForm::languageChange() -{ - retranslateUi(this); -} - -void createTableForm::init() -{ -} - -//** Create The Table -void createTableForm::confirmCreate() -{ - bool ok = true; - QString tabname = tablenameLineEdit->text(); - if (tabname.isEmpty()) { - ok = false; - statusBar->showMessage("Please enter a name for the table", 4000); - tablenameLineEdit->setFocus(); - return; - } - if (tabname.contains(" ")>0) { - ok = false; - statusBar->showMessage("Spaces are not allowed in the table name", 4000); - tablenameLineEdit->setFocus(); - return; - } - - if (treeWidget->invisibleRootItem()->childCount() == 0) { - ok = false; - statusBar->showMessage("No fields defined", 4000); - return; - } - - /*check field names for empty or illegal names - TODO: move to add - - for (int r=0; rnumRows();r++){ - QString rowname = fieldsTable->text(r, 0); - if (rowname.isEmpty()) { - ok = false; - QMessageBox::warning( this, applicationName, "Empty field names are not allowed" ); - break; - } - if (rowname.contains(" ")>0) { - ok = false; - QMessageBox::warning( this, applicationName, "Spaces are not allowed in the field names" ); - break; - } - }*/ - - if (!ok){ - return; - } - - if (ok){ - //QString esc("\""); - // #TODO The colnames need to be escaped eg create table 'group' - createStatement = "CREATE TABLE "; - //createStatement.append(esc).append(tabname).append(esc); - createStatement.append(tabname); - createStatement.append(" ("); - for(int i = 0; i < treeWidget->invisibleRootItem()->childCount(); i++){ - QTreeWidgetItem *item = treeWidget->invisibleRootItem()->child(i); - //createStatement.append(esc).append(item->text(0)).append(esc); - createStatement.append( item->text(0) ); - createStatement.append(" "); - createStatement.append(item->text(1)); - if(i < treeWidget->invisibleRootItem()->childCount() -1){ - createStatement.append(", "); - } - } - createStatement.append(");"); - accept(); - } -} - - -void createTableForm::addField() -{ - //TODO maybe embedd locally - editFieldForm * addForm = new editFieldForm( this ); - addForm->setModal(true); - addForm->setInitialValues(pdb, true, QString(""), QString(""),QString("")); - if (addForm->exec()) - { - QTreeWidgetItem *newItem = new QTreeWidgetItem(); - newItem->setText(0, addForm->field_name); - newItem->setText(1, addForm->field_type); - newItem->setIcon(0, QIcon(":/icons/field")); - treeWidget->addTopLevelItem(newItem); - } -} - - -void createTableForm::deleteField() -{ - if( !treeWidget->currentItem() ){ - return; - } - treeWidget->invisibleRootItem()->removeChild(treeWidget->currentItem()); -} - - -void createTableForm::fieldSelectionChanged() -{ - buttonDeleteField->setEnabled( treeWidget->selectionModel()->hasSelection() ); -} diff --git a/src/createtableform.h b/src/createtableform.h deleted file mode 100644 index 4c1ba4f3..00000000 --- a/src/createtableform.h +++ /dev/null @@ -1,213 +0,0 @@ -#ifndef CREATETABLEFORM_H -#define CREATETABLEFORM_H - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -class DBBrowserDB; - -class Ui_createTableForm -{ -public: - QVBoxLayout *mainVBoxLayout; - - QHBoxLayout *topTableLayout; - - QGroupBox *groupBoxFields; - QVBoxLayout *groupBoxLayout; - QLabel *textLabel1; - QLineEdit *tablenameLineEdit; - - QTreeWidget *treeWidget; - - QHBoxLayout *fieldActionsButtonLayout; - QPushButton *buttonAddField; - QPushButton *buttonDeleteField; - - QHBoxLayout *bottomButtonLayout; - QPushButton *buttonCreate; - QPushButton *buttonCancel; - - QStatusBar *statusBar; - - void setupUi(QDialog *createTableForm) - { - if (createTableForm->objectName().isEmpty()){ - createTableForm->setObjectName(QString::fromUtf8("createTableForm")); - } - createTableForm->setWindowIcon(QIcon(":/icons/db_new")); - - createTableForm->resize(309, 320); - - //** Main Layout - mainVBoxLayout = new QVBoxLayout(createTableForm); - mainVBoxLayout->setSpacing(10); - mainVBoxLayout->setContentsMargins(0, 0, 0, 0); - mainVBoxLayout->setObjectName(QString::fromUtf8("vboxLayout")); - - //**** Top Table Box **** - topTableLayout = new QHBoxLayout(); - topTableLayout->setSpacing(10); - topTableLayout->setContentsMargins(10,10,10,10); - //topTableLayout->setObjectName(QString::fromUtf8("hboxLayout")); - textLabel1 = new QLabel(createTableForm); - //textLabel1->setObjectName(QString::fromUtf8("textLabel1")); - textLabel1->setWordWrap(false); - - topTableLayout->addWidget(textLabel1); - - tablenameLineEdit = new QLineEdit(createTableForm); - //tablenameLineEdit->setObjectName(QString::fromUtf8("tablenameLineEdit")); - - topTableLayout->addWidget(tablenameLineEdit); - - - mainVBoxLayout->addLayout(topTableLayout); - - //**** Fields GroupBox - groupBoxFields = new QGroupBox(createTableForm); - groupBoxFields->setObjectName(QString::fromUtf8("groupBox1")); - groupBoxLayout = new QVBoxLayout(); - groupBoxFields->setLayout(groupBoxLayout); - - treeWidget = new QTreeWidget(); - groupBoxLayout->addWidget(treeWidget); - treeWidget->headerItem()->setText(0, QApplication::translate("createTableForm", "Field name", 0, QApplication::UnicodeUTF8)); - treeWidget->headerItem()->setText(1, QApplication::translate("createTableForm", "Field type", 0, QApplication::UnicodeUTF8)); - treeWidget->setRootIsDecorated(false); - treeWidget->setAlternatingRowColors(true); - - //** Field Action Buttons Box - fieldActionsButtonLayout = new QHBoxLayout(); - fieldActionsButtonLayout->setSpacing(6); - fieldActionsButtonLayout->setObjectName(QString::fromUtf8("hboxLayout1")); - fieldActionsButtonLayout->addStretch(10); // force right - - //*** Delete Field Button - buttonDeleteField = new QPushButton(groupBoxFields); - buttonDeleteField->setObjectName(QString::fromUtf8("buttonDeleteField")); - buttonDeleteField->setEnabled(false); - buttonDeleteField->setIcon(QIcon(":/icons/field_delete")); - fieldActionsButtonLayout->addWidget(buttonDeleteField); - - //*** Add Field Button - buttonAddField = new QPushButton(groupBoxFields); - buttonAddField->setObjectName(QString::fromUtf8("buttonAddField")); - buttonAddField->setIcon(QIcon(":/icons/field_add")); - fieldActionsButtonLayout->addWidget(buttonAddField); - - groupBoxLayout->addLayout(fieldActionsButtonLayout); - - mainVBoxLayout->addWidget(groupBoxFields); - - //*************************************** - //*** Bottom Dialog action Buttons - bottomButtonLayout = new QHBoxLayout(); - bottomButtonLayout->setSpacing(6); - //bottomButtonLayout->setObjectName(QString::fromUtf8("hboxLayout2")); - bottomButtonLayout->addStretch(10); // force right - - //** Cancel Button - buttonCancel = new QPushButton(createTableForm); - //buttonCancel->setObjectName(QString::fromUtf8("buttonCancel")); - buttonCancel->setIcon(QIcon(":/icons/cancel")); - bottomButtonLayout->addWidget(buttonCancel); - - //** Create Button - buttonCreate = new QPushButton(createTableForm); - //buttonCreate->setObjectName(QString::fromUtf8("buttonCreate")); - buttonCreate->setIcon(QIcon(":/icons/save")); - bottomButtonLayout->addWidget(buttonCreate); - mainVBoxLayout->addLayout(bottomButtonLayout); - - statusBar = new QStatusBar(); - mainVBoxLayout->addWidget(statusBar); - - //** Setup - retranslateUi(createTableForm); - QObject::connect(buttonCancel, SIGNAL(clicked()), createTableForm, SLOT(reject())); - QObject::connect(buttonCreate, SIGNAL(clicked()), createTableForm, SLOT(confirmCreate())); - QObject::connect(buttonAddField, SIGNAL(clicked()), createTableForm, SLOT(addField())); - QObject::connect(buttonDeleteField, SIGNAL(clicked()), createTableForm, SLOT(deleteField())); - QObject::connect(treeWidget, SIGNAL(itemSelectionChanged()), createTableForm, SLOT(fieldSelectionChanged())); - - QMetaObject::connectSlotsByName(createTableForm); - } // setupUi - - void retranslateUi(QDialog *createTableForm) - { - createTableForm->setWindowTitle(QApplication::translate("createTableForm", "Create Table", 0, QApplication::UnicodeUTF8)); - textLabel1->setText(QApplication::translate("createTableForm", "Table name:", 0, QApplication::UnicodeUTF8)); - tablenameLineEdit->setText(QString()); -#ifndef QT_NO_TOOLTIP - tablenameLineEdit->setProperty("toolTip", QVariant(QApplication::translate("createTableForm", "Enter the name for the new table", 0, QApplication::UnicodeUTF8))); -#endif // QT_NO_TOOLTIP -#ifndef QT_NO_WHATSTHIS - tablenameLineEdit->setProperty("whatsThis", QVariant(QApplication::translate("createTableForm", "Use this control to enter the name of the table to be created.", 0, QApplication::UnicodeUTF8))); -#endif // QT_NO_WHATSTHIS - groupBoxFields->setTitle(QApplication::translate("createTableForm", "Define fields:", 0, QApplication::UnicodeUTF8)); - - buttonAddField->setText(QApplication::translate("createTableForm", "Add", 0, QApplication::UnicodeUTF8)); -#ifndef QT_NO_TOOLTIP - buttonAddField->setProperty("toolTip", QVariant(QApplication::translate("createTableForm", "Add a new field definition", 0, QApplication::UnicodeUTF8))); -#endif // QT_NO_TOOLTIP -#ifndef QT_NO_WHATSTHIS - buttonAddField->setProperty("whatsThis", QVariant(QApplication::translate("createTableForm", "This button is used to add a new field definition to your table", 0, QApplication::UnicodeUTF8))); -#endif // QT_NO_WHATSTHIS - buttonDeleteField->setText(QApplication::translate("createTableForm", "Delete", 0, QApplication::UnicodeUTF8)); -#ifndef QT_NO_TOOLTIP - buttonDeleteField->setProperty("toolTip", QVariant(QApplication::translate("createTableForm", "Delete current field definition", 0, QApplication::UnicodeUTF8))); -#endif // QT_NO_TOOLTIP -#ifndef QT_NO_WHATSTHIS - buttonDeleteField->setProperty("whatsThis", QVariant(QApplication::translate("createTableForm", "This button is used to delete the currently selected field definition from your table", 0, QApplication::UnicodeUTF8))); -#endif // QT_NO_WHATSTHIS - buttonCreate->setText(QApplication::translate("createTableForm", "Create", 0, QApplication::UnicodeUTF8)); -#ifndef QT_NO_TOOLTIP - buttonCreate->setProperty("toolTip", QVariant(QApplication::translate("createTableForm", "Create the table", 0, QApplication::UnicodeUTF8))); -#endif // QT_NO_TOOLTIP - buttonCancel->setText(QApplication::translate("createTableForm", "Cancel", 0, QApplication::UnicodeUTF8)); -#ifndef QT_NO_TOOLTIP - buttonCancel->setProperty("toolTip", QVariant(QApplication::translate("createTableForm", "Cancel and close dialog box", 0, QApplication::UnicodeUTF8))); -#endif // QT_NO_TOOLTIP - } // retranslateUi - -}; - -namespace Ui { - class createTableForm: public Ui_createTableForm {}; -} // namespace Ui - -class createTableForm : public QDialog, public Ui::createTableForm -{ - Q_OBJECT - -public: - createTableForm( DBBrowserDB *db = 0, QWidget* parent = 0, Qt::WindowFlags fl = Qt::Window); - ~createTableForm(); - - QString createStatement; - -public slots: - virtual void confirmCreate(); - virtual void addField(); - virtual void deleteField(); - virtual void fieldSelectionChanged(); - -protected slots: - virtual void languageChange(); - -private: - void init(); - DBBrowserDB *pdb; - -}; - -#endif // CREATETABLEFORM_H diff --git a/src/edittableform.cpp b/src/edittableform.cpp index 19894b5f..9517e892 100644 --- a/src/edittableform.cpp +++ b/src/edittableform.cpp @@ -39,8 +39,10 @@ void editTableForm::setActiveTable(DBBrowserDB * thedb, QString tableName) { pdb = thedb; curTable = tableName; - populateFields(); + if(curTable != "") + populateFields(); ui->editTableName->setText(curTable); + checkInput(); } void editTableForm::populateFields() @@ -65,22 +67,49 @@ void editTableForm::populateFields() void editTableForm::accept() { - // Rename table if necessary - if(ui->editTableName->text() != curTable) + // Are we editing an already existing table or designing a new one? In the first case there is a table name set, + // in the latter the current table name is empty + if(curTable == "") { - QApplication::setOverrideCursor( Qt::WaitCursor ); // this might take time + // Creation of new table + + // Build SQL statement from what the use entered + QString sql = QString("CREATE TABLE `%1` (").arg(ui->editTableName->text()); + for(int i=0;itreeWidget->topLevelItemCount();i++) + sql.append(QString("%1 %2,").arg(ui->treeWidget->topLevelItem(i)->text(0)).arg(ui->treeWidget->topLevelItem(i)->text(1))); + sql.remove(sql.count() - 1, 1); // Remove last comma + sql.append(");"); + + // Execute it modified = true; - QString newName = ui->editTableName->text(); - QString sql = QString("ALTER TABLE `%1` RENAME TO `%2`").arg(curTable, newName); if (!pdb->executeSQL(sql)){ - QApplication::restoreOverrideCursor(); - QString error("Error renaming table. Message from database engine:\n"); + QString error("Error creating table. Message from database engine:\n"); error.append(pdb->lastErrorMessage).append("\n\n").append(sql); QMessageBox::warning( this, QApplication::applicationName(), error ); - } else { - QApplication::restoreOverrideCursor(); + return; + } + } else { + // Editing of old table + + // Rename table if necessary + if(ui->editTableName->text() != curTable) + { + QApplication::setOverrideCursor( Qt::WaitCursor ); // this might take time + modified = true; + QString newName = ui->editTableName->text(); + QString sql = QString("ALTER TABLE `%1` RENAME TO `%2`").arg(curTable, newName); + if (!pdb->executeSQL(sql)){ + QApplication::restoreOverrideCursor(); + QString error("Error renaming table. Message from database engine:\n"); + error.append(pdb->lastErrorMessage).append("\n\n").append(sql); + QMessageBox::warning( this, QApplication::applicationName(), error ); + return; + } else { + QApplication::restoreOverrideCursor(); + } } } + QDialog::accept(); } @@ -101,12 +130,10 @@ void editTableForm::editField() QTreeWidgetItem *item = ui->treeWidget->currentItem(); editFieldForm * fieldForm = new editFieldForm( this ); fieldForm->setModal(true); - fieldForm->setInitialValues(pdb, false, curTable, item->text(0), item->text(1)); + fieldForm->setInitialValues(pdb, curTable == "", curTable, item->text(0), item->text(1)); if (fieldForm->exec()) { modified = true; - //do the sql rename here - //qDebug(fieldForm->name + fieldForm->type); item->setText(0,fieldForm->field_name); item->setText(1,fieldForm->field_type); } @@ -212,16 +239,11 @@ void editTableForm::addField() addForm->setInitialValues(pdb, true, curTable, QString(""),QString("")); if (addForm->exec()) { - modified = true; - - QTreeWidgetItem *tbitem = new QTreeWidgetItem(); + QTreeWidgetItem *tbitem = new QTreeWidgetItem(ui->treeWidget); tbitem->setText( 0, addForm->field_name); - tbitem->setText( 1, addForm->field_name); - //do the sql creation here + tbitem->setText( 1, addForm->field_type); modified = true; - //do the sql rename here - //qDebug(fieldForm->name + fieldForm->type); - QString sql = "CREATE TEMPORARY TABLE TEMP_TABLE("; + ui->treeWidget->addTopLevelItem(tbitem); } // Q3ListViewItemIterator it( fieldListView ); @@ -317,7 +339,7 @@ void editTableForm::addField() // while ( it.current() ) { // item = it.current(); // sql.append(item->text(0)); - /// sql.append(" "); + // sql.append(" "); // sql.append(item->text(1)); // if (item->nextSibling() != 0) // { diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index 75422140..4d30c358 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -11,7 +11,6 @@ #include #include #include -#include "createtableform.h" #include "createindexform.h" #include "deletetableform.h" #include "deleteindexform.h" @@ -575,7 +574,6 @@ void MainWindow::lookfor( const QString & wfield, const QString & woperator, con QApplication::restoreOverrideCursor(); } - void MainWindow::showrecord( int dec ) { updateTableView(dec); @@ -587,21 +585,16 @@ void MainWindow::createTable() QMessageBox::information( this, QApplication::applicationName(), "There is no database opened. Please open or create a new database file." ); return; } - createTableForm * tableForm = new createTableForm(&db, this); + + editTableForm * tableForm = new editTableForm(this); + tableForm->setActiveTable(&db, ""); tableForm->setModal(true); - if ( tableForm->exec() ) { - if (!db.executeSQL(tableForm->createStatement)){ - QString error = "Error: could not create the table. Message from database engine: "; - error.append(db.lastErrorMessage); - QMessageBox::warning( this, QApplication::applicationName(), error ); - } else { - populateStructure(); - resetBrowser(); - } + if(tableForm->exec()) { + populateStructure(); + resetBrowser(); } } - void MainWindow::createIndex() { if (!db.isOpen()){ diff --git a/src/src.pro b/src/src.pro index 854852d1..e10c95bf 100644 --- a/src/src.pro +++ b/src/src.pro @@ -17,7 +17,6 @@ HEADERS += \ sqlite_source/sqlite3.h \ choosetableform.h \ createindexform.h \ - createtableform.h \ deleteindexform.h \ deletetableform.h \ editfieldform.h \ @@ -28,7 +27,6 @@ HEADERS += \ importcsvform.h \ preferencesform.h \ mainwindow.h \ - createtabledialog.h \ SQLLogDock.h \ sqlitesyntaxhighlighter.h \ dialogabout.h @@ -40,7 +38,6 @@ SOURCES += \ sqlite_source/sqlite3.c \ choosetableform.cpp \ createindexform.cpp \ - createtableform.cpp \ deleteindexform.cpp \ deletetableform.cpp \ editfieldform.cpp \ @@ -51,7 +48,6 @@ SOURCES += \ importcsvform.cpp \ preferencesform.cpp \ mainwindow.cpp \ - createtabledialog.cpp \ SQLLogDock.cpp \ sqlitesyntaxhighlighter.cpp \ dialogabout.cpp @@ -76,7 +72,6 @@ mac { RESOURCES += icons/icons.qrc FORMS += \ - createtabledialog.ui \ dialogabout.ui \ preferencesform.ui \ mainwindow.ui \