498 lines
19 KiB
C++
498 lines
19 KiB
C++
#include <QtGui/QMdiSubWindow>
|
|
#include <QtGui/QMessageBox>
|
|
#include <QtSql/QSqlDatabase>
|
|
#include <QtSql/QSqlError>
|
|
#include <QDesktopServices>
|
|
#include <QTabBar>
|
|
#include <QSettings>
|
|
#include <QMap>
|
|
#include <QtDebug>
|
|
#include "mainwindow.h"
|
|
#include "ui_mainwindow.h"
|
|
#include "tableform.h"
|
|
#include "registerdialog.h"
|
|
#include "sqlform.h"
|
|
|
|
MainWindow::MainWindow(QWidget *parent) :
|
|
QMainWindow(parent),
|
|
ui(new Ui::MainWindow)
|
|
{
|
|
QCoreApplication::setOrganizationName("Saturn Laboratories");
|
|
QCoreApplication::setOrganizationDomain("saturnlaboratories.co.za");
|
|
QCoreApplication::setApplicationName("IoSQL");
|
|
|
|
ui->setupUi(this);
|
|
m_WindowMapper = new QSignalMapper(this);
|
|
m_RegisterDialog = new RegisterDialog(this);
|
|
m_QueryForm = new SqlForm(this);
|
|
m_QueryForm->hide();
|
|
connect(m_WindowMapper, SIGNAL(mapped(QWidget *)),
|
|
this, SLOT(setActiveSubWindow(QWidget *)));
|
|
QSettings settings;
|
|
//qDebug(qPrintable(settings.value("connectionList", QVariant("")).toString()));
|
|
QString connections = settings.value("connectionList", QVariant("")).toString();
|
|
if (!connections.isEmpty())
|
|
{
|
|
QStringList connectionList = connections.split(",");
|
|
for (int i = 0; i < connectionList.count(); i++)
|
|
{
|
|
QMap<QString, QVariant> 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());
|
|
ui->MdiArea->setViewMode(viewMode);
|
|
ui->MdiArea->setTabPosition(tabPosition);
|
|
if (viewMode == QMdiArea::SubWindowView)
|
|
{
|
|
ui->ModeSubWindowsAction->setChecked(true);
|
|
ui->ModeTabbedAction->setChecked(false);
|
|
ui->TabLocationMenu->setEnabled(false);
|
|
}
|
|
else
|
|
{
|
|
ui->ModeSubWindowsAction->setChecked(false);
|
|
ui->ModeTabbedAction->setChecked(true);
|
|
ui->TabLocationMenu->setEnabled(true);
|
|
}
|
|
if (tabPosition == QTabWidget::North)
|
|
{
|
|
ui->TabLocationNorthAction->setChecked(true);
|
|
ui->TabLocationSouthAction->setChecked(false);
|
|
ui->TabLocationEastAction->setChecked(false);
|
|
ui->TabLocationWestAction->setChecked(false);
|
|
}
|
|
else if (tabPosition == QTabWidget::South)
|
|
{
|
|
ui->TabLocationNorthAction->setChecked(false);
|
|
ui->TabLocationSouthAction->setChecked(true);
|
|
ui->TabLocationEastAction->setChecked(false);
|
|
ui->TabLocationWestAction->setChecked(false);
|
|
}
|
|
else if (tabPosition == QTabWidget::East)
|
|
{
|
|
ui->TabLocationNorthAction->setChecked(false);
|
|
ui->TabLocationSouthAction->setChecked(false);
|
|
ui->TabLocationEastAction->setChecked(true);
|
|
ui->TabLocationWestAction->setChecked(false);
|
|
}
|
|
else if (tabPosition == QTabWidget::West)
|
|
{
|
|
ui->TabLocationNorthAction->setChecked(false);
|
|
ui->TabLocationSouthAction->setChecked(false);
|
|
ui->TabLocationEastAction->setChecked(true);
|
|
ui->TabLocationWestAction->setChecked(false);
|
|
}
|
|
this->updateTabCloseButtons();
|
|
}
|
|
|
|
MainWindow::~MainWindow()
|
|
{
|
|
delete m_QueryForm;
|
|
delete m_WindowMapper;
|
|
delete m_RegisterDialog;
|
|
delete ui;
|
|
}
|
|
|
|
void MainWindow::loadConnection(QString connectionName, QString serverType, QString serverName, int serverPort, QString databaseName, QString username, QString password)
|
|
{
|
|
QSqlDatabase db = QSqlDatabase::addDatabase(serverType, connectionName);
|
|
db.setHostName(serverName);
|
|
db.setPort(serverPort);
|
|
db.setDatabaseName(databaseName);
|
|
db.setUserName(username);
|
|
db.setPassword(password);
|
|
QTreeWidgetItem *item = new QTreeWidgetItem(QStringList(connectionName));
|
|
item->setIcon(0, QIcon(":/Server/images/server-database-register.png"));
|
|
ui->DatabaseTreeWidget->addTopLevelItem(item);
|
|
}
|
|
|
|
bool MainWindow::setupConnection(QString connectionName, QString serverType, QString serverName, int serverPort, QString databaseName, QString username, QString password)
|
|
{
|
|
QSqlDatabase db = QSqlDatabase::addDatabase(serverType, connectionName);
|
|
db.setHostName(serverName);
|
|
db.setPort(serverPort);
|
|
db.setDatabaseName(databaseName);
|
|
db.setUserName(username);
|
|
db.setPassword(password);
|
|
bool ok = db.open();
|
|
if (ok)
|
|
{
|
|
QTreeWidgetItem *item = new QTreeWidgetItem(QStringList(connectionName));
|
|
item->setIcon(0, QIcon(":/Server/images/server-database-register.png"));
|
|
ui->DatabaseTreeWidget->addTopLevelItem(item);
|
|
QSettings settings;
|
|
if (!settings.contains("connection " + connectionName))
|
|
{
|
|
QMap<QString, QVariant> connectionMap;
|
|
connectionMap.insert("connectionName", connectionName);
|
|
connectionMap.insert("serverType", serverType);
|
|
connectionMap.insert("serverName", serverName);
|
|
connectionMap.insert("serverPort", serverPort);
|
|
connectionMap.insert("databaseName", databaseName);
|
|
connectionMap.insert("username", username);
|
|
connectionMap.insert("password", password);
|
|
settings.setValue("connection " + connectionName, connectionMap);
|
|
QStringList connectionList;
|
|
if (settings.contains("connectionList"))
|
|
{
|
|
connectionList = settings.value("connectionList").toString().split(",");
|
|
}
|
|
connectionList.append(connectionName);
|
|
settings.setValue("connectionList", connectionList.join(","));
|
|
}
|
|
}
|
|
else
|
|
{
|
|
QSqlError error = QSqlDatabase::database(connectionName).lastError();
|
|
QMessageBox::critical(this, "Error opening database", "Error occurred: " + error.text());
|
|
}
|
|
return ok;
|
|
}
|
|
|
|
void MainWindow::updateTabCloseButtons()
|
|
{
|
|
if (ui->MdiArea->viewMode() != QMdiArea::TabbedView)
|
|
{
|
|
return;
|
|
}
|
|
QList<QTabBar*> children = ui->MdiArea->findChildren<QTabBar *>();
|
|
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();
|
|
ui->WindowMenu->addAction(ui->WindowCascadeAction);
|
|
ui->WindowMenu->addAction(ui->WindowTileAction);
|
|
ui->WindowMenu->addSeparator();
|
|
QList<QMdiSubWindow *> windows = ui->MdiArea->subWindowList();
|
|
if (!windows.isEmpty())
|
|
{
|
|
ui->WindowMenu->addSeparator();
|
|
for (int i = 0; i < windows.size(); ++i)
|
|
{
|
|
QMdiSubWindow *child = windows.at(i);
|
|
QString text;
|
|
if (i < 9)
|
|
{
|
|
text = tr("&%1 %2").arg(i + 1).arg(child->windowTitle());
|
|
}
|
|
else
|
|
{
|
|
text = tr("%1").arg(child->windowTitle());
|
|
}
|
|
QAction *action = ui->WindowMenu->addAction(text);
|
|
action->setCheckable(true);
|
|
action->setChecked(child == ui->MdiArea->activeSubWindow());
|
|
connect(action, SIGNAL(triggered()), m_WindowMapper, SLOT(map()));
|
|
m_WindowMapper->setMapping(action, child);
|
|
}
|
|
}
|
|
}
|
|
|
|
void MainWindow::setActiveSubWindow(QWidget *window)
|
|
{
|
|
if (!window)
|
|
return;
|
|
ui->MdiArea->setActiveSubWindow(qobject_cast<QMdiSubWindow *>(window));
|
|
}
|
|
|
|
void MainWindow::changeEvent(QEvent *e)
|
|
{
|
|
QMainWindow::changeEvent(e);
|
|
switch (e->type()) {
|
|
case QEvent::LanguageChange:
|
|
ui->retranslateUi(this);
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
|
|
void MainWindow::on_DatabaseRegisterAction_triggered()
|
|
{
|
|
if (m_RegisterDialog->exec())
|
|
{
|
|
setupConnection(
|
|
m_RegisterDialog->connectionName(),
|
|
m_RegisterDialog->serverType(),
|
|
m_RegisterDialog->serverName(),
|
|
m_RegisterDialog->serverPort(),
|
|
m_RegisterDialog->database(),
|
|
m_RegisterDialog->username(),
|
|
m_RegisterDialog->password()
|
|
);
|
|
m_QueryForm->reloadConnections();
|
|
}
|
|
}
|
|
|
|
void MainWindow::on_DatabaseTreeWidget_doubleClicked(QModelIndex index)
|
|
{
|
|
QTreeWidgetItem *item = ui->DatabaseTreeWidget->currentItem();
|
|
if (item->parent() == 0 && item->childCount() == 0)
|
|
{
|
|
QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
|
|
QSqlDatabase db = QSqlDatabase::database(item->text(0));
|
|
bool ok = db.open();
|
|
if (ok)
|
|
{
|
|
/* Get the list of tables */
|
|
QTreeWidgetItem *tablesItem = new QTreeWidgetItem(item, QStringList("Tables"));
|
|
tablesItem->setIcon(0, QIcon(":/Table/images/table/list.png"));
|
|
item->addChild(tablesItem);
|
|
QStringList tables = db.tables();
|
|
tables.sort();
|
|
for (int i = 0; i < tables.count(); i++)
|
|
{
|
|
QTreeWidgetItem *table = new QTreeWidgetItem(tablesItem, QStringList(tables.at(i)));
|
|
table->setIcon(0, QIcon(":/Table/images/table/table.png"));
|
|
tablesItem->addChild(table);
|
|
}
|
|
/* Get the list of views */
|
|
QTreeWidgetItem *viewsItem = new QTreeWidgetItem(item, QStringList("Views"));
|
|
viewsItem->setIcon(0, QIcon(":/View/images/view/list.png"));
|
|
item->addChild(viewsItem);
|
|
QStringList views = db.tables(QSql::Views);
|
|
views.sort();
|
|
for (int i = 0; i < views.count(); i++)
|
|
{
|
|
QTreeWidgetItem *view = new QTreeWidgetItem(viewsItem, QStringList(views.at(i)));
|
|
view->setIcon(0, QIcon(":/View/images/view/view.png"));
|
|
viewsItem->addChild(view);
|
|
}
|
|
QApplication::restoreOverrideCursor();
|
|
}
|
|
else
|
|
{
|
|
QApplication::restoreOverrideCursor();
|
|
QSqlError error = db.lastError();
|
|
QMessageBox::critical(this, "Error opening database", "Error occurred: " + error.text());
|
|
}
|
|
}
|
|
else if (item->parent() != 0 && item->childCount() == 0)
|
|
{
|
|
QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
|
|
QString tableName = item->parent()->parent()->text(0) + "." + item->text(0);
|
|
QList<QMdiSubWindow *> windows = ui->MdiArea->subWindowList();
|
|
bool foundWindow = false;
|
|
if (!windows.isEmpty())
|
|
{
|
|
for (int i = 0; i < windows.size(); ++i)
|
|
{
|
|
if (windows.at(i)->windowTitle() == tableName)
|
|
{
|
|
ui->MdiArea->setActiveSubWindow(windows.at(i));
|
|
foundWindow = true;
|
|
}
|
|
}
|
|
}
|
|
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"));
|
|
subWindow->showMaximized();
|
|
}
|
|
QApplication::restoreOverrideCursor();
|
|
}
|
|
}
|
|
|
|
|
|
void MainWindow::on_DatabaseDeregisterAction_triggered()
|
|
{
|
|
QTreeWidgetItem *item = ui->DatabaseTreeWidget->currentItem();
|
|
if (item != 0 && item->parent() == 0 && item->childCount() == 0)
|
|
{
|
|
if (QMessageBox::question(this, "De-register Database", "Are you sure you want to de-register \"" + item->text(0) + "\"?", QMessageBox::Yes | QMessageBox::No) == QMessageBox::Yes)
|
|
{
|
|
QString connectionName = item->text(0);
|
|
QSettings settings;
|
|
if (settings.contains("connection " + connectionName))
|
|
{
|
|
settings.remove("connection " + connectionName);
|
|
QStringList connectionList;
|
|
if (settings.contains("connectionList"))
|
|
{
|
|
connectionList = settings.value("connectionList").toStringList();
|
|
}
|
|
connectionList.removeAt(connectionList.indexOf(connectionName));
|
|
settings.setValue("connectionList", connectionList);
|
|
}
|
|
delete item;
|
|
}
|
|
m_QueryForm->reloadConnections();
|
|
}
|
|
}
|
|
|
|
void MainWindow::on_HelpAboutQtAction_triggered()
|
|
{
|
|
QMessageBox::aboutQt(this);
|
|
}
|
|
|
|
void MainWindow::on_HelpAboutAction_triggered()
|
|
{
|
|
QMessageBox::about(this, "About IoSQL", "<h3>About IoSQL</h3>"
|
|
"<p>IoSQL is a light-weight database-independent data explorer.</p>"
|
|
"<p>Copyright © 2010 Saturn Laboratories.</p>"
|
|
"<p>This program is free software: you can redistribute it and/or modify it "
|
|
"under the terms of the GNU General Public License as published by the Free "
|
|
"Software Foundation, either version 3 of the License, or (at your option) "
|
|
"any later version.</p>"
|
|
"<p>This program is distributed in the hope that it will be useful, "
|
|
"but WITHOUT ANY WARRANTY; without even the implied warranty of "
|
|
"MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the "
|
|
"GNU General Public License for more details.</p>"
|
|
"<p>You should have received a copy of the GNU General Public License "
|
|
"along with this program. If not, see "
|
|
"<a href=\"http://www.gnu.org/licenses/\">http://www.gnu.org/licenses/</a></p>");
|
|
}
|
|
|
|
void MainWindow::on_ModeSubWindowsAction_triggered()
|
|
{
|
|
ui->MdiArea->setViewMode(QMdiArea::SubWindowView);
|
|
QSettings settings;
|
|
settings.setValue("window mode", QVariant(QMdiArea::SubWindowView));
|
|
ui->ModeSubWindowsAction->setChecked(true);
|
|
ui->ModeTabbedAction->setChecked(false);
|
|
ui->TabLocationMenu->setEnabled(false);
|
|
}
|
|
|
|
void MainWindow::on_ModeTabbedAction_triggered()
|
|
{
|
|
ui->MdiArea->setViewMode(QMdiArea::TabbedView);
|
|
ui->MdiArea->setDocumentMode(true);
|
|
QSettings settings;
|
|
settings.setValue("window mode", QVariant(QMdiArea::TabbedView));
|
|
ui->ModeSubWindowsAction->setChecked(false);
|
|
ui->ModeTabbedAction->setChecked(true);
|
|
ui->TabLocationMenu->setEnabled(true);
|
|
this->updateTabCloseButtons();
|
|
}
|
|
|
|
void MainWindow::on_TabLocationNorthAction_triggered()
|
|
{
|
|
ui->MdiArea->setTabPosition(QTabWidget::North);
|
|
QSettings settings;
|
|
settings.setValue("tab position", QVariant(QTabWidget::North));
|
|
ui->TabLocationNorthAction->setChecked(true);
|
|
ui->TabLocationSouthAction->setChecked(false);
|
|
ui->TabLocationEastAction->setChecked(false);
|
|
ui->TabLocationWestAction->setChecked(false);
|
|
}
|
|
|
|
void MainWindow::on_TabLocationSouthAction_triggered()
|
|
{
|
|
ui->MdiArea->setTabPosition(QTabWidget::South);
|
|
QSettings settings;
|
|
settings.setValue("tab position", QVariant(QTabWidget::South));
|
|
ui->TabLocationNorthAction->setChecked(false);
|
|
ui->TabLocationSouthAction->setChecked(true);
|
|
ui->TabLocationEastAction->setChecked(false);
|
|
ui->TabLocationWestAction->setChecked(false);
|
|
}
|
|
|
|
void MainWindow::on_TabLocationEastAction_triggered()
|
|
{
|
|
ui->MdiArea->setTabPosition(QTabWidget::East);
|
|
QSettings settings;
|
|
settings.setValue("tab position", QVariant(QTabWidget::East));
|
|
ui->TabLocationNorthAction->setChecked(false);
|
|
ui->TabLocationSouthAction->setChecked(false);
|
|
ui->TabLocationEastAction->setChecked(true);
|
|
ui->TabLocationWestAction->setChecked(false);
|
|
}
|
|
|
|
void MainWindow::on_TabLocationWestAction_triggered()
|
|
{
|
|
ui->MdiArea->setTabPosition(QTabWidget::West);
|
|
QSettings settings;
|
|
settings.setValue("tab position", QVariant(QTabWidget::West));
|
|
ui->TabLocationNorthAction->setChecked(false);
|
|
ui->TabLocationSouthAction->setChecked(false);
|
|
ui->TabLocationEastAction->setChecked(false);
|
|
ui->TabLocationWestAction->setChecked(true);
|
|
}
|
|
|
|
void MainWindow::on_DatabaseEditAction_triggered()
|
|
{
|
|
QTreeWidgetItem *item = ui->DatabaseTreeWidget->currentItem();
|
|
if (item != 0 && item->parent() == 0 && item->childCount() == 0)
|
|
{
|
|
QString oldConnectionName = item->text(0);
|
|
QSettings settings;
|
|
QMap<QString, QVariant> connectionMap = settings.value("connection " + oldConnectionName).toMap();
|
|
m_RegisterDialog->setConnectionName(oldConnectionName);
|
|
m_RegisterDialog->setServerType(connectionMap.value("serverType", QVariant("")).toString());
|
|
m_RegisterDialog->setServerName(connectionMap.value("serverName", QVariant("")).toString());
|
|
m_RegisterDialog->setServerPort(connectionMap.value("serverPort", QVariant(0)).toInt());
|
|
m_RegisterDialog->setDatabase(connectionMap.value("databaseName", QVariant("")).toString());
|
|
m_RegisterDialog->setUsername(connectionMap.value("username", QVariant("")).toString());
|
|
m_RegisterDialog->setPassword(connectionMap.value("password", QVariant("")).toString());
|
|
if (m_RegisterDialog->exec())
|
|
{
|
|
QString connectionName = m_RegisterDialog->connectionName();
|
|
QString serverType = m_RegisterDialog->serverType();
|
|
QString serverName = m_RegisterDialog->serverName();
|
|
int serverPort = m_RegisterDialog->serverPort();
|
|
QString database = m_RegisterDialog->database();
|
|
QString username = m_RegisterDialog->username();
|
|
QString password = m_RegisterDialog->password();
|
|
QSqlDatabase db = QSqlDatabase::database(connectionName);
|
|
if (connectionName != oldConnectionName || serverType != db.driverName())
|
|
{
|
|
QSqlDatabase::removeDatabase(oldConnectionName);
|
|
db = QSqlDatabase::addDatabase(serverType, connectionName);
|
|
}
|
|
db.setHostName(serverName);
|
|
db.setPort(serverPort);
|
|
db.setDatabaseName(database);
|
|
db.setUserName(username);
|
|
db.setPassword(password);
|
|
connectionMap.insert("connectionName", connectionName);
|
|
connectionMap.insert("serverType", serverType);
|
|
connectionMap.insert("serverName", serverName);
|
|
connectionMap.insert("serverPort", serverPort);
|
|
connectionMap.insert("databaseName", database);
|
|
connectionMap.insert("username", username);
|
|
connectionMap.insert("password", password);
|
|
settings.remove("connection " + oldConnectionName);
|
|
settings.setValue("connection " + connectionName, connectionMap);
|
|
item->setText(0, connectionName);
|
|
}
|
|
m_QueryForm->reloadConnections();
|
|
}
|
|
}
|
|
|
|
void MainWindow::on_DatabaseRunQueryAction_triggered()
|
|
{
|
|
m_QueryForm->show();
|
|
}
|
|
|
|
void MainWindow::closeTab(int i)
|
|
{
|
|
QMdiSubWindow *sub = ui->MdiArea->subWindowList()[i];
|
|
QWidget *win = sub->widget();
|
|
win->close();
|
|
ui->MdiArea->setActiveSubWindow(sub);
|
|
ui->MdiArea->closeActiveSubWindow();
|
|
}
|