From 07feab6ab66636fef75bb77aafd01614e854c4a7 Mon Sep 17 00:00:00 2001 From: Raoul Snyman Date: Thu, 27 Oct 2011 14:08:05 +0200 Subject: [PATCH] Various updates. --- IoSQL.pro | 6 ++- Makefile | 11 ++-- lexerscheme.cpp | 15 ++++++ lexerscheme.h | 21 ++++++++ mainwindow.cpp | 66 ++++++++++++++--------- mainwindow.h | 5 +- registerdialog.cpp | 17 ++++++ registerdialog.h | 2 + registerdialog.ui | 9 +++- sqlform.cpp | 32 +++++++++-- tableform.cpp | 97 +++++++++++++++++++++------------- tableform.h | 1 + tableform.ui | 129 +++++++++++++++++++++++---------------------- 13 files changed, 273 insertions(+), 138 deletions(-) create mode 100644 lexerscheme.cpp create mode 100644 lexerscheme.h diff --git a/IoSQL.pro b/IoSQL.pro index a1da6f2..7d6c40f 100644 --- a/IoSQL.pro +++ b/IoSQL.pro @@ -9,11 +9,13 @@ SOURCES += main.cpp \ mainwindow.cpp \ registerdialog.cpp \ tableform.cpp \ - sqlform.cpp + sqlform.cpp \ + lexerscheme.cpp HEADERS += mainwindow.h \ registerdialog.h \ tableform.h \ - sqlform.h + sqlform.h \ + lexerscheme.h FORMS += mainwindow.ui \ registerdialog.ui \ tableform.ui \ diff --git a/Makefile b/Makefile index dc68a2c..baf5508 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ ############################################################################# # Makefile for building: IoSQL -# Generated by qmake (2.01a) (Qt 4.7.2) on: Fri Jun 24 13:41:46 2011 +# Generated by qmake (2.01a) (Qt 4.7.2) on: Tue Sep 6 09:52:18 2011 # Project: IoSQL.pro # Template: app # Command: /usr/bin/qmake -spec /usr/share/qt4/mkspecs/linux-g++ QMLJSDEBUGGER_PATH=/usr/share/qtcreator/qml/qmljsdebugger -o Makefile IoSQL.pro @@ -47,7 +47,8 @@ SOURCES = main.cpp \ mainwindow.cpp \ registerdialog.cpp \ tableform.cpp \ - sqlform.cpp moc_mainwindow.cpp \ + sqlform.cpp \ + lexerscheme.cpp moc_mainwindow.cpp \ moc_registerdialog.cpp \ moc_tableform.cpp \ moc_sqlform.cpp \ @@ -57,6 +58,7 @@ OBJECTS = main.o \ registerdialog.o \ tableform.o \ sqlform.o \ + lexerscheme.o \ moc_mainwindow.o \ moc_registerdialog.o \ moc_tableform.o \ @@ -171,7 +173,7 @@ qmake: FORCE dist: @$(CHK_DIR_EXISTS) .tmp/IoSQL1.0.0 || $(MKDIR) .tmp/IoSQL1.0.0 - $(COPY_FILE) --parents $(SOURCES) $(DIST) .tmp/IoSQL1.0.0/ && $(COPY_FILE) --parents mainwindow.h registerdialog.h tableform.h sqlform.h .tmp/IoSQL1.0.0/ && $(COPY_FILE) --parents IoSQL.qrc .tmp/IoSQL1.0.0/ && $(COPY_FILE) --parents main.cpp mainwindow.cpp registerdialog.cpp tableform.cpp sqlform.cpp .tmp/IoSQL1.0.0/ && $(COPY_FILE) --parents mainwindow.ui registerdialog.ui tableform.ui sqlform.ui .tmp/IoSQL1.0.0/ && (cd `dirname .tmp/IoSQL1.0.0` && $(TAR) IoSQL1.0.0.tar IoSQL1.0.0 && $(COMPRESS) IoSQL1.0.0.tar) && $(MOVE) `dirname .tmp/IoSQL1.0.0`/IoSQL1.0.0.tar.gz . && $(DEL_FILE) -r .tmp/IoSQL1.0.0 + $(COPY_FILE) --parents $(SOURCES) $(DIST) .tmp/IoSQL1.0.0/ && $(COPY_FILE) --parents mainwindow.h registerdialog.h tableform.h sqlform.h lexerscheme.h .tmp/IoSQL1.0.0/ && $(COPY_FILE) --parents IoSQL.qrc .tmp/IoSQL1.0.0/ && $(COPY_FILE) --parents main.cpp mainwindow.cpp registerdialog.cpp tableform.cpp sqlform.cpp lexerscheme.cpp .tmp/IoSQL1.0.0/ && $(COPY_FILE) --parents mainwindow.ui registerdialog.ui tableform.ui sqlform.ui .tmp/IoSQL1.0.0/ && (cd `dirname .tmp/IoSQL1.0.0` && $(TAR) IoSQL1.0.0.tar IoSQL1.0.0 && $(COMPRESS) IoSQL1.0.0.tar) && $(MOVE) `dirname .tmp/IoSQL1.0.0`/IoSQL1.0.0.tar.gz . && $(DEL_FILE) -r .tmp/IoSQL1.0.0 clean:compiler_clean @@ -285,6 +287,9 @@ sqlform.o: sqlform.cpp sqlform.h \ ui_sqlform.h $(CXX) -c $(CXXFLAGS) $(INCPATH) -o sqlform.o sqlform.cpp +lexerscheme.o: lexerscheme.cpp lexerscheme.h + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o lexerscheme.o lexerscheme.cpp + moc_mainwindow.o: moc_mainwindow.cpp $(CXX) -c $(CXXFLAGS) $(INCPATH) -o moc_mainwindow.o moc_mainwindow.cpp diff --git a/lexerscheme.cpp b/lexerscheme.cpp new file mode 100644 index 0000000..f881f2b --- /dev/null +++ b/lexerscheme.cpp @@ -0,0 +1,15 @@ +#include "lexerscheme.h" + +LexerScheme::LexerScheme() +{ +} + +QColor LexerScheme::getColor(int style) +{ + return colorMap[style]; +} + +void LexerScheme::setColor(int style, QColor color) +{ + colorMap[style] = color; +} diff --git a/lexerscheme.h b/lexerscheme.h new file mode 100644 index 0000000..465156b --- /dev/null +++ b/lexerscheme.h @@ -0,0 +1,21 @@ +#ifndef LEXERSCHEME_H +#define LEXERSCHEME_H + +#include +#include + +class LexerScheme +{ + // Default colors + QColor defaultPaper; + QColor defaultColor; + // The rest of the colours + QMap colorMap; + +public: + LexerScheme(); + QColor getColor(int style); + void setColor(int style, QColor color); +}; + +#endif // LEXERSCHEME_H diff --git a/mainwindow.cpp b/mainwindow.cpp index 2f7d6f7..dfffc66 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -20,16 +20,8 @@ MainWindow::MainWindow(QWidget *parent) : QCoreApplication::setOrganizationName("Saturn Laboratories"); QCoreApplication::setOrganizationDomain("saturnlaboratories.co.za"); QCoreApplication::setApplicationName("IoSQL"); + ui->setupUi(this); - QList children = ui->MdiArea->findChildren(); - Q_FOREACH (QTabBar* tab, children) - { - if (!tab->tabsClosable()) - { - tab->setTabsClosable(true); - connect(tab, SIGNAL(tabCloseRequested(int)), this, SLOT(closeTab(int))); - } - } m_WindowMapper = new QSignalMapper(this); m_RegisterDialog = new RegisterDialog(this); m_QueryForm = new SqlForm(this); @@ -37,19 +29,24 @@ MainWindow::MainWindow(QWidget *parent) : connect(m_WindowMapper, SIGNAL(mapped(QWidget *)), this, SLOT(setActiveSubWindow(QWidget *))); QSettings settings; - QStringList connectionList = settings.value("connectionList").toStringList(); - for (int i = 0; i < connectionList.count(); i++) + //qDebug(qPrintable(settings.value("connectionList", QVariant("")).toString())); + QString connections = settings.value("connectionList", QVariant("")).toString(); + if (!connections.isEmpty()) { - QMap connectionMap = settings.value("connection " + connectionList.at(i)).toMap(); - loadConnection( - connectionMap.value("connectionName").toString(), - connectionMap.value("serverType").toString(), - connectionMap.value("serverName").toString(), - connectionMap.value("serverPort").toInt(), - connectionMap.value("databaseName").toString(), - connectionMap.value("username").toString(), - connectionMap.value("password").toString() - ); + QStringList connectionList = connections.split(","); + for (int i = 0; i < connectionList.count(); i++) + { + QMap connectionMap = settings.value("connection " + connectionList.at(i)).toMap(); + loadConnection( + connectionMap.value("connectionName").toString(), + connectionMap.value("serverType").toString(), + connectionMap.value("serverName").toString(), + connectionMap.value("serverPort").toInt(), + connectionMap.value("databaseName").toString(), + connectionMap.value("username").toString(), + connectionMap.value("password").toString() + ); + } } QMdiArea::ViewMode viewMode = QMdiArea::ViewMode(settings.value("window mode", QVariant(QMdiArea::SubWindowView)).toInt()); QTabWidget::TabPosition tabPosition = QTabWidget::TabPosition(settings.value("tab position", QVariant(QTabWidget::North)).toInt()); @@ -95,6 +92,7 @@ MainWindow::MainWindow(QWidget *parent) : ui->TabLocationEastAction->setChecked(true); ui->TabLocationWestAction->setChecked(false); } + this->updateTabCloseButtons(); } MainWindow::~MainWindow() @@ -147,10 +145,10 @@ bool MainWindow::setupConnection(QString connectionName, QString serverType, QSt QStringList connectionList; if (settings.contains("connectionList")) { - connectionList = settings.value("connectionList").toStringList(); + connectionList = settings.value("connectionList").toString().split(","); } connectionList.append(connectionName); - settings.setValue("connectionList", connectionList); + settings.setValue("connectionList", connectionList.join(",")); } } else @@ -161,6 +159,23 @@ bool MainWindow::setupConnection(QString connectionName, QString serverType, QSt return ok; } +void MainWindow::updateTabCloseButtons() +{ + if (ui->MdiArea->viewMode() != QMdiArea::TabbedView) + { + return; + } + QList children = ui->MdiArea->findChildren(); + Q_FOREACH (QTabBar* tab, children) + { + if (!tab->tabsClosable()) + { + tab->setTabsClosable(true); + connect(tab, SIGNAL(tabCloseRequested(int)), this, SLOT(closeTab(int))); + } + } +} + void MainWindow::on_WindowMenu_aboutToShow() { ui->WindowMenu->clear(); @@ -274,8 +289,6 @@ void MainWindow::on_DatabaseTreeWidget_doubleClicked(QModelIndex index) else if (item->parent() != 0 && item->childCount() == 0) { QApplication::setOverrideCursor(QCursor(Qt::WaitCursor)); - TableForm *tableForm = new TableForm(ui->MdiArea); - tableForm->setTable(item->parent()->parent()->text(0), item->text(0)); QString tableName = item->parent()->parent()->text(0) + "." + item->text(0); QList windows = ui->MdiArea->subWindowList(); bool foundWindow = false; @@ -292,6 +305,8 @@ void MainWindow::on_DatabaseTreeWidget_doubleClicked(QModelIndex index) } if (!foundWindow) { + TableForm *tableForm = new TableForm(ui->MdiArea); + tableForm->setTable(item->parent()->parent()->text(0), item->text(0)); QMdiSubWindow *subWindow = ui->MdiArea->addSubWindow(tableForm); subWindow->setWindowTitle(tableName); subWindow->setWindowIcon(QIcon(":/Table/images/table/icon.png")); @@ -370,6 +385,7 @@ void MainWindow::on_ModeTabbedAction_triggered() ui->ModeSubWindowsAction->setChecked(false); ui->ModeTabbedAction->setChecked(true); ui->TabLocationMenu->setEnabled(true); + this->updateTabCloseButtons(); } void MainWindow::on_TabLocationNorthAction_triggered() diff --git a/mainwindow.h b/mainwindow.h index e5520d7..5ad69e5 100644 --- a/mainwindow.h +++ b/mainwindow.h @@ -16,13 +16,14 @@ class MainWindow : public QMainWindow { public: MainWindow(QWidget *parent = 0); ~MainWindow(); -void loadConnection(QString connectionName, QString serverType, QString serverName, int serverPort, QString databaseName, QString username, QString password); -bool setupConnection(QString connectionName, QString serverType, QString serverName, int serverPort, QString databaseName, QString username, QString password); + void loadConnection(QString connectionName, QString serverType, QString serverName, int serverPort, QString databaseName, QString username, QString password); + bool setupConnection(QString connectionName, QString serverType, QString serverName, int serverPort, QString databaseName, QString username, QString password); protected: void changeEvent(QEvent *e); private: + void updateTabCloseButtons(); Ui::MainWindow *ui; QSignalMapper *m_WindowMapper; RegisterDialog *m_RegisterDialog; diff --git a/registerdialog.cpp b/registerdialog.cpp index a982dba..5a56885 100644 --- a/registerdialog.cpp +++ b/registerdialog.cpp @@ -123,3 +123,20 @@ void RegisterDialog::setPassword(const QString password) { ui->PasswordLineEdit->setText(password); } + +void RegisterDialog::on_TypeComboBox_currentIndexChanged(int index) +{ + QString portNumber = ui->PortLineEdit->text(); + if (index == 0 && (portNumber.isEmpty() || portNumber == "3306" || portNumber == "0")) + { + ui->PortLineEdit->setText("5432"); + } + else if (index == 1 && (portNumber.isEmpty() || portNumber == "5432" || portNumber == "0")) + { + ui->PortLineEdit->setText("3306"); + } + else if (index == 2 && (portNumber.isEmpty() || portNumber == "5432" || portNumber == "3306")) + { + ui->PortLineEdit->setText("0"); + } +} diff --git a/registerdialog.h b/registerdialog.h index 58fd9fb..01f1b67 100644 --- a/registerdialog.h +++ b/registerdialog.h @@ -32,6 +32,8 @@ protected: private slots: + void on_TypeComboBox_currentIndexChanged(int index); + private: Ui::RegisterDialog *ui; }; diff --git a/registerdialog.ui b/registerdialog.ui index 7b5f06f..a0ee393 100644 --- a/registerdialog.ui +++ b/registerdialog.ui @@ -52,7 +52,11 @@ - + + + + + @@ -63,6 +67,9 @@ + + 5432 + true diff --git a/sqlform.cpp b/sqlform.cpp index ed12c22..d758163 100644 --- a/sqlform.cpp +++ b/sqlform.cpp @@ -39,12 +39,16 @@ SqlForm::~SqlForm() void SqlForm::reloadConnections() { QSettings settings; - QStringList connectionList = settings.value("connectionList").toStringList(); - m_ConnectionComboBox->clear(); - for (int i = 0; i < connectionList.count(); i++) + QString connections = settings.value("connectionList", QVariant("")).toString(); + if (!connections.isEmpty()) { - QMap connectionMap = settings.value("connection " + connectionList.at(i)).toMap(); - m_ConnectionComboBox->addItem(connectionMap.value("connectionName").toString()); + QStringList connectionList = connections.split(","); + m_ConnectionComboBox->clear(); + for (int i = 0; i < connectionList.count(); i++) + { + QMap connectionMap = settings.value("connection " + connectionList.at(i)).toMap(); + m_ConnectionComboBox->addItem(connectionMap.value("connectionName").toString()); + } } } @@ -62,6 +66,8 @@ void SqlForm::changeEvent(QEvent *e) void SqlForm::on_RunQueryAction_triggered() { + QCursor oldCursor = cursor(); + setCursor(Qt::BusyCursor); m_Database = QSqlDatabase::database(m_ConnectionComboBox->currentText()); m_Database.open(); QCoreApplication::processEvents(); @@ -79,6 +85,22 @@ void SqlForm::on_RunQueryAction_triggered() query = ui->QueryTextEdit->text(); } m_QueryModel->setQuery(query, m_Database); + /*QSqlError error = m_QueryModel->lastError(); + QString errorMessage; + if (error == QSqlError::ConnectionError) + { + errorMessage = "Connection Error: "; + if (!error.driverText().isEmpty()) + { + errorMessage = errorMessage + error.driverText(); + } + if (!error.databaseText().isEmpty()) + { + errorMessage = errorMessage + error.databaseText(); + } + + }*/ ui->ResultsTableView->setModel(m_QueryModel); ui->ResultsTableView->resizeColumnsToContents(); + setCursor(oldCursor); } diff --git a/tableform.cpp b/tableform.cpp index b50cae6..90175b7 100644 --- a/tableform.cpp +++ b/tableform.cpp @@ -1,11 +1,7 @@ #include "tableform.h" #include "ui_tableform.h" -#include -#include -#include -#include -#include +#include #include #include #include @@ -49,69 +45,85 @@ void TableForm::changeEvent(QEvent *e) } } -void TableForm::setTable(const QString connectionName, const QString tableName) +void TableForm::setColumns() { - QMainWindow * mainWindow = qobject_cast(window()); - if (mainWindow) - mainWindow->statusBar()->showMessage("Opening table \"" + tableName + "\"..."); - QCoreApplication::processEvents(); - m_ConnectionName = connectionName; - m_TableName = tableName; - m_Database = QSqlDatabase::database(m_ConnectionName); - m_DataModel = new QSqlTableModel(this, m_Database); - m_DataModel->setTable(m_TableName); - m_DataModel->select(); - ui->DataTableView->setModel(m_DataModel); - ui->DataTableView->resizeColumnsToContents(); - QSqlRecord record = m_DataModel->record(); + QSqlQuery query; + if (m_Database.driverName() == "QPSQL") + { + query = QSqlQuery("SELECT * FROM \"" + m_TableName + "\" LIMIT 1", m_Database); + // SELECT column_name, column_default, is_nullable, data_type, character_maximum_length FROM information_schema.columns WHERE table_name = "" ORDER BY ordinal_position + } + else if (m_Database.driverName() == "MYSQL") + { + query = QSqlQuery("SELECT * FROM `" + m_TableName + "` LIMIT 1", m_Database); + // SELECT COLUMN_NAME AS column_name, COLUMN_DEFAULT AS column_default, IS_NULLABLE AS is_nullable, DATA_TYPE AS data_type, CHARACTER_MAXIMUM_LENGTH AS character_maximum_length FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = "" ORDER BY ordinal_position + } + else if (m_Database.driverName() == "SQLITE") + { + query = QSqlQuery("SELECT * FROM '" + m_TableName + "'' LIMIT 1", m_Database); + // SELECT + } + QSqlRecord record = query.record(); ui->StructureTableWidget->setRowCount(record.count()); for (int i = 0; i < record.count(); i++) { QSqlField field = record.field(i); QTableWidgetItem *nameItem = new QTableWidgetItem(field.name()); - QString typeName; - switch (field.type()) - { - case QVariant::String: typeName = "varchar"; break; - case QVariant::Int: typeName = "integer"; break; - case QVariant::Date: typeName = "date"; break; - case QVariant::DateTime: typeName = "datetime"; break; - case QVariant::Bool: typeName = "boolean"; break; - case QVariant::ByteArray: typeName = "byte array"; break; - case QVariant::LongLong: typeName = "bigint"; break; - default: typeName = "unknown"; break; - } - QTableWidgetItem *typeItem = new QTableWidgetItem(typeName); + QString typeName = QVariant::typeToName(field.type()); + QTableWidgetItem *typeItem = new QTableWidgetItem(typeName + " - " + QString::number(field.type())); QTableWidgetItem *sizeItem; if (field.length() >= 0) { - sizeItem = new QTableWidgetItem(field.length()); + sizeItem = new QTableWidgetItem(QString::number(field.length())); } else if (field.precision() >= 0) { - sizeItem = new QTableWidgetItem(field.precision()); + sizeItem = new QTableWidgetItem(QString::number(field.precision())); } else { sizeItem = new QTableWidgetItem(""); } QTableWidgetItem *nullItem; - if (field.requiredStatus()) + if (field.requiredStatus() == 1) { nullItem = new QTableWidgetItem("not null"); } - else + else if (field.requiredStatus() == 0) { nullItem = new QTableWidgetItem("nullable"); } + else + { + nullItem = new QTableWidgetItem("unknown"); + } ui->StructureTableWidget->setItem(i, 0, nameItem); ui->StructureTableWidget->setItem(i, 1, typeItem); ui->StructureTableWidget->setItem(i, 2, sizeItem); ui->StructureTableWidget->setItem(i, 3, nullItem); } ui->StructureTableWidget->resizeColumnsToContents(); +} + +void TableForm::setTable(const QString connectionName, const QString tableName) +{ + QMainWindow * mainWindow = qobject_cast(window()); if (mainWindow) + { + mainWindow->statusBar()->showMessage("Opening table \"" + tableName + "\"..."); + } + QCoreApplication::processEvents(); + m_ConnectionName = connectionName; + m_TableName = tableName; + m_Database = QSqlDatabase::database(m_ConnectionName); + this->setColumns(); + m_DataModel = new QSqlTableModel(this, m_Database); + m_DataModel->setTable(m_TableName); + ui->DataTableView->setModel(m_DataModel); + if (mainWindow) + { mainWindow->statusBar()->clearMessage(); + } } void TableForm::on_DataCommitAction_triggered() @@ -126,15 +138,26 @@ void TableForm::on_DataCommitAction_triggered() void TableForm::on_DataRefreshAction_triggered() { + QMainWindow * mainWindow = qobject_cast(window()); + if (mainWindow) + { + mainWindow->statusBar()->showMessage("Fetching results from \"" + m_TableName + "\"..."); + } m_DataModel->select(); ui->DataTableView->resizeColumnsToContents(); + if (mainWindow) + { + mainWindow->statusBar()->clearMessage(); + } } void TableForm::on_QueryRunAction_triggered() { QMainWindow * mainWindow = qobject_cast(window()); if (mainWindow) + { mainWindow->statusBar()->showMessage("Running query..."); + } QApplication::setOverrideCursor(QCursor(Qt::WaitCursor)); QCoreApplication::processEvents(); QString query = ""; @@ -155,5 +178,7 @@ void TableForm::on_QueryRunAction_triggered() ui->ResultsTableView->resizeColumnsToContents(); QApplication::restoreOverrideCursor(); if (mainWindow) + { mainWindow->statusBar()->clearMessage(); + } } diff --git a/tableform.h b/tableform.h index c3370e7..4d804f2 100644 --- a/tableform.h +++ b/tableform.h @@ -16,6 +16,7 @@ public: TableForm(QWidget *parent = 0); ~TableForm(); void setTable(const QString connectionName, const QString tableName); + void setColumns(); protected: void changeEvent(QEvent *e); diff --git a/tableform.ui b/tableform.ui index 3df074d..21fdc53 100644 --- a/tableform.ui +++ b/tableform.ui @@ -29,70 +29,6 @@ 0 - - - Data - - - - 0 - - - 0 - - - - - - 0 - 0 - - - - - 0 - 0 - - - - - 0 - 0 - - - - - 22 - 22 - - - - - - - - - - - - - - - true - - - false - - - 20 - - - 18 - - - - - Structure @@ -159,6 +95,70 @@ + + + Data + + + + 0 + + + 0 + + + + + + 0 + 0 + + + + + 0 + 0 + + + + + 0 + 0 + + + + + 22 + 22 + + + + + + + + + + + + + + + true + + + false + + + 20 + + + 18 + + + + + Query @@ -314,6 +314,7 @@ QsciScintilla QFrame
Qsci/qsciscintilla.h
+ 1