2018-12-28 12:55:11 +01:00
|
|
|
|
#include "DatabaseWindow.h"
|
2017-01-21 18:16:57 +01:00
|
|
|
|
#include "util.h"
|
2019-01-29 19:41:27 +01:00
|
|
|
|
#include "OpenDatabase.h"
|
2017-02-01 18:01:02 +01:00
|
|
|
|
#include "MasterController.h"
|
2018-12-30 15:46:15 +01:00
|
|
|
|
#include "TaskExecutor.h"
|
2019-08-15 16:18:47 +02:00
|
|
|
|
#include <QAction>
|
2019-01-01 14:34:14 +01:00
|
|
|
|
#include <QApplication>
|
2019-08-15 16:18:47 +02:00
|
|
|
|
#include <QFileDialog>
|
|
|
|
|
|
#include <QMenuBar>
|
2019-01-01 14:34:14 +01:00
|
|
|
|
#include <QMessageBox>
|
|
|
|
|
|
#include <QMetaMethod>
|
2019-08-15 16:18:47 +02:00
|
|
|
|
#include <QStandardPaths>
|
2019-01-06 08:17:37 +01:00
|
|
|
|
#include <QTableView>
|
|
|
|
|
|
|
|
|
|
|
|
#include "EditTableWidget.h"
|
2019-08-15 16:38:01 +02:00
|
|
|
|
#include "CatalogInspector.h"
|
2019-01-06 08:17:37 +01:00
|
|
|
|
#include "CodeGenerator.h"
|
2019-08-15 16:18:47 +02:00
|
|
|
|
#include "QueryTool.h"
|
2017-01-08 15:16:16 +01:00
|
|
|
|
|
2016-12-26 16:06:55 +01:00
|
|
|
|
namespace pg = Pgsql;
|
|
|
|
|
|
|
2018-12-30 15:46:15 +01:00
|
|
|
|
|
2019-01-05 09:49:12 +01:00
|
|
|
|
DatabaseWindow::DatabaseWindow(MasterController *master, QWidget *parent)
|
2019-08-15 16:18:47 +02:00
|
|
|
|
: QMainWindow(parent)
|
2019-01-05 09:49:12 +01:00
|
|
|
|
, m_masterController(master)
|
|
|
|
|
|
{
|
2018-12-30 15:46:15 +01:00
|
|
|
|
connect(&loadWatcher, &QFutureWatcher<LoadCatalog::Result>::finished,
|
|
|
|
|
|
this, &DatabaseWindow::catalogLoaded);
|
2019-01-01 11:15:16 +01:00
|
|
|
|
|
2019-08-15 16:18:47 +02:00
|
|
|
|
m_tabWidget = new QTabWidget(this);
|
|
|
|
|
|
setCentralWidget(m_tabWidget);
|
|
|
|
|
|
|
|
|
|
|
|
createActions();
|
|
|
|
|
|
initMenus();
|
|
|
|
|
|
|
2019-01-01 14:34:14 +01:00
|
|
|
|
QMetaObject::connectSlotsByName(this);
|
2016-12-26 16:06:55 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2019-01-05 11:12:47 +01:00
|
|
|
|
DatabaseWindow::~DatabaseWindow() = default;
|
2017-01-21 18:16:57 +01:00
|
|
|
|
|
2019-08-15 16:18:47 +02:00
|
|
|
|
void DatabaseWindow::addPage(QWidget* page, QString caption)
|
|
|
|
|
|
{
|
|
|
|
|
|
m_tabWidget->addTab(page, caption);
|
|
|
|
|
|
m_tabWidget->setCurrentWidget(page);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void DatabaseWindow::setTabCaptionForWidget(QWidget *widget, const QString &caption, const QString &hint)
|
|
|
|
|
|
{
|
|
|
|
|
|
auto index = m_tabWidget->indexOf(widget);
|
|
|
|
|
|
m_tabWidget->setTabText(index, caption);
|
|
|
|
|
|
m_tabWidget->setTabToolTip(index, hint);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void DatabaseWindow::setTabIcon(QWidget *widget, const QString &iconname)
|
|
|
|
|
|
{
|
|
|
|
|
|
auto index = m_tabWidget->indexOf(widget);
|
|
|
|
|
|
auto n = ":/icons/16x16/" + iconname;
|
|
|
|
|
|
m_tabWidget->setTabIcon(index, QIcon(n));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-12-28 12:55:11 +01:00
|
|
|
|
void DatabaseWindow::newCreateTablePage()
|
2018-12-15 20:27:40 +01:00
|
|
|
|
{
|
|
|
|
|
|
auto w = new EditTableWidget(m_database, this);
|
2019-01-01 14:34:14 +01:00
|
|
|
|
m_tabWidget->addTab(w, "Create table");
|
2018-12-15 20:27:40 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-12-28 12:55:11 +01:00
|
|
|
|
void DatabaseWindow::newCodeGenPage(QString query, std::shared_ptr<const Pgsql::Result> dbres)
|
2018-09-18 11:53:19 +02:00
|
|
|
|
{
|
2019-08-14 09:06:48 +02:00
|
|
|
|
// TODO should this call be this direct or should it go through module system
|
|
|
|
|
|
// yes it should otherwise context cannot properly setup toolbar and menu!!!
|
|
|
|
|
|
// auto cgtab = new CodeGenerator(context(), pluginModule(), this);
|
|
|
|
|
|
// cgtab->Init(m_database->catalog(), query, dbres);
|
|
|
|
|
|
// addPage(cgtab, "Codegen");
|
|
|
|
|
|
//
|
2018-09-18 11:53:19 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-12-28 12:55:11 +01:00
|
|
|
|
void DatabaseWindow::setConfig(const ConnectionConfig &config)
|
2017-01-15 21:01:40 +01:00
|
|
|
|
{
|
|
|
|
|
|
m_config = config;
|
2017-12-19 18:17:41 +01:00
|
|
|
|
try {
|
|
|
|
|
|
QString title = "pglab - ";
|
|
|
|
|
|
title += m_config.name().c_str();
|
|
|
|
|
|
setWindowTitle(title);
|
|
|
|
|
|
|
2018-12-30 15:46:15 +01:00
|
|
|
|
auto f = TaskExecutor::run(new LoadCatalog(m_config));
|
|
|
|
|
|
loadWatcher.setFuture(f);
|
|
|
|
|
|
|
2017-12-25 15:33:10 +01:00
|
|
|
|
} catch (std::runtime_error &ex) {
|
|
|
|
|
|
QMessageBox::critical(this, "Error reading database",
|
|
|
|
|
|
QString::fromUtf8(ex.what()));
|
|
|
|
|
|
|
|
|
|
|
|
close();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2019-08-15 16:18:47 +02:00
|
|
|
|
|
|
|
|
|
|
void DatabaseWindow::createActions()
|
|
|
|
|
|
{
|
|
|
|
|
|
{
|
|
|
|
|
|
QIcon icon;
|
|
|
|
|
|
icon.addFile(QString::fromUtf8(":/icons/about.png"), QSize(), QIcon::Normal, QIcon::On);
|
2019-08-15 16:38:01 +02:00
|
|
|
|
auto action = actionAbout = new QAction(icon, tr("About"), this);
|
|
|
|
|
|
action->setObjectName("actionAbout");
|
2019-08-15 16:18:47 +02:00
|
|
|
|
}
|
|
|
|
|
|
{
|
|
|
|
|
|
QIcon icon;
|
|
|
|
|
|
icon.addFile(QString::fromUtf8(":/icons/page_white_delete.png"), QSize(), QIcon::Normal, QIcon::On);
|
|
|
|
|
|
auto action = actionClose = new QAction(icon, tr("Close"), this);
|
2019-08-15 16:38:01 +02:00
|
|
|
|
action->setObjectName("actionClose");
|
2019-08-15 16:18:47 +02:00
|
|
|
|
action->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_W));
|
|
|
|
|
|
}
|
2019-08-15 16:38:01 +02:00
|
|
|
|
{
|
|
|
|
|
|
QIcon icon;
|
|
|
|
|
|
icon.addFile(QString::fromUtf8(":/icons/page_white_add.png"), QSize(), QIcon::Normal, QIcon::On);
|
|
|
|
|
|
auto action = actionInspectInformationSchema = new QAction(icon, tr("Inspect information_schema"), this);
|
|
|
|
|
|
action->setObjectName("actionInspectInformationSchema");
|
|
|
|
|
|
}
|
|
|
|
|
|
{
|
|
|
|
|
|
QIcon icon;
|
|
|
|
|
|
icon.addFile(QString::fromUtf8(":/icons/page_white_add.png"), QSize(), QIcon::Normal, QIcon::On);
|
|
|
|
|
|
auto action = actionInspectPgCatalog = new QAction(icon, tr("Inspect pg_catalog"), this);
|
|
|
|
|
|
action->setObjectName("actionInspectPgCatalog");
|
|
|
|
|
|
}
|
|
|
|
|
|
{
|
|
|
|
|
|
QIcon icon;
|
|
|
|
|
|
icon.addFile(QString::fromUtf8(":/icons/page_white_add.png"), QSize(), QIcon::Normal, QIcon::On);
|
|
|
|
|
|
auto action = actionInspectUserSchemas = new QAction(icon, tr("Inspect user schemas"), this);
|
|
|
|
|
|
action->setObjectName("actionInspectUserSchemas");
|
|
|
|
|
|
}
|
2019-08-15 16:18:47 +02:00
|
|
|
|
{
|
|
|
|
|
|
QIcon icon;
|
|
|
|
|
|
icon.addFile(QString::fromUtf8(":/icons/new_query_tab.png"), QSize(), QIcon::Normal, QIcon::On);
|
|
|
|
|
|
auto action = actionNewSql = new QAction(icon, tr("New Query"), this);
|
2019-08-15 16:38:01 +02:00
|
|
|
|
action->setObjectName("actionNewSql");
|
2019-08-15 16:18:47 +02:00
|
|
|
|
action->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_N));
|
|
|
|
|
|
}
|
|
|
|
|
|
{
|
|
|
|
|
|
QIcon icon;
|
|
|
|
|
|
icon.addFile(QString::fromUtf8(":/icons/folder.png"), QSize(), QIcon::Normal, QIcon::On);
|
|
|
|
|
|
auto action = actionOpenSql = new QAction(icon, tr("Open Query"), this);
|
2019-08-15 16:38:01 +02:00
|
|
|
|
action->setObjectName("actionOpenSql");
|
2019-08-15 16:18:47 +02:00
|
|
|
|
action->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_O));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// {
|
|
|
|
|
|
// auto ca = makeContextAction<QueryTool>(tr("Save SQL"), &QueryTool::save);
|
|
|
|
|
|
// ca->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_S));
|
|
|
|
|
|
// ca->setMenuLocation(MenuPath("File/Save"));
|
|
|
|
|
|
// ca->setToolbarLocation(ToolbarLocation("main", "save"));
|
|
|
|
|
|
// // how we tell the system we want this to become a menu button with this as it's default action
|
|
|
|
|
|
// registerContextAction(ca);
|
|
|
|
|
|
// }
|
|
|
|
|
|
// {
|
|
|
|
|
|
// auto ca = makeContextAction<QueryTool>(tr("Save SQL as"), &QueryTool::saveAs);
|
|
|
|
|
|
// ca->setMenuLocation(MenuPath("File/Save"));
|
|
|
|
|
|
// ca->setToolbarLocation(ToolbarLocation("main", "save"));
|
|
|
|
|
|
// // how we tell the system we want this to become a secondary action for the previous button?
|
|
|
|
|
|
// registerContextAction(ca);
|
|
|
|
|
|
// }
|
|
|
|
|
|
// {
|
|
|
|
|
|
// auto ca = makeContextAction<QueryTool>(tr("Save copy of SQL as"), &QueryTool::saveCopyAs);
|
|
|
|
|
|
// ca->setMenuLocation(MenuPath("File/Save"));
|
|
|
|
|
|
// ca->setToolbarLocation(ToolbarLocation("main", "save"));
|
|
|
|
|
|
// // how we tell the system we want this to become a secondary action for the previous button?
|
|
|
|
|
|
// registerContextAction(ca);
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void DatabaseWindow::initMenus()
|
|
|
|
|
|
{
|
|
|
|
|
|
auto seperator = new QAction(this);
|
|
|
|
|
|
seperator->setSeparator(true);
|
|
|
|
|
|
|
|
|
|
|
|
auto mb = new QMenuBar(this);
|
|
|
|
|
|
menuFile = mb->addMenu(tr("File"));
|
|
|
|
|
|
menuFile->addActions({
|
|
|
|
|
|
actionNewSql,
|
|
|
|
|
|
actionOpenSql,
|
|
|
|
|
|
seperator,
|
2019-08-15 16:38:01 +02:00
|
|
|
|
actionInspectUserSchemas,
|
|
|
|
|
|
actionInspectPgCatalog,
|
|
|
|
|
|
actionInspectInformationSchema,
|
2019-08-15 16:18:47 +02:00
|
|
|
|
seperator,
|
|
|
|
|
|
actionClose
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
menuHelp = mb->addMenu(tr("Help"));
|
|
|
|
|
|
menuHelp->addActions({
|
|
|
|
|
|
seperator,
|
|
|
|
|
|
actionAbout
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
setMenuBar(mb);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void DatabaseWindow::on_actionClose_triggered()
|
|
|
|
|
|
{
|
|
|
|
|
|
m_tabWidget->tabCloseRequested(m_tabWidget->currentIndex());
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void DatabaseWindow::on_actionNewSql_triggered()
|
|
|
|
|
|
{
|
|
|
|
|
|
auto *ct = new QueryTool(m_database, this);
|
2019-08-15 16:38:01 +02:00
|
|
|
|
addPage(ct, "query");
|
2019-08-15 16:18:47 +02:00
|
|
|
|
ct->newdoc();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2019-08-15 16:38:01 +02:00
|
|
|
|
void DatabaseWindow::on_actionInspectInformationSchema_triggered()
|
|
|
|
|
|
{
|
|
|
|
|
|
auto ct = new CatalogInspector(m_database, this);
|
|
|
|
|
|
addPage(ct, "information_schema");
|
|
|
|
|
|
ct->setNamespaceFilter(NamespaceFilter::InformationSchema);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void DatabaseWindow::on_actionInspectPgCatalog_triggered()
|
|
|
|
|
|
{
|
|
|
|
|
|
auto ct = new CatalogInspector(m_database, this);
|
|
|
|
|
|
addPage(ct, "pg_catalog");
|
|
|
|
|
|
ct->setNamespaceFilter(NamespaceFilter::PgCatalog);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void DatabaseWindow::on_actionInspectUserSchemas_triggered()
|
|
|
|
|
|
{
|
|
|
|
|
|
auto ct = new CatalogInspector(m_database, this);
|
|
|
|
|
|
addPage(ct, "Schema");
|
|
|
|
|
|
ct->setNamespaceFilter(NamespaceFilter::User);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2019-08-15 16:18:47 +02:00
|
|
|
|
void DatabaseWindow::on_actionOpenSql_triggered()
|
|
|
|
|
|
{
|
|
|
|
|
|
QString home_dir = QStandardPaths::locate(QStandardPaths::HomeLocation, "", QStandardPaths::LocateDirectory);
|
|
|
|
|
|
QString file_name = QFileDialog::getOpenFileName(this,
|
|
|
|
|
|
tr("Open sql query"), home_dir, tr("SQL files (*.sql *.txt)"));
|
|
|
|
|
|
if ( ! file_name.isEmpty()) {
|
|
|
|
|
|
auto *ct = new QueryTool(m_database, this);
|
|
|
|
|
|
addPage(ct, "");
|
|
|
|
|
|
if (!ct->load(file_name)) {
|
|
|
|
|
|
// TODO load has failed remove widget or never add it?
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-12-28 12:55:11 +01:00
|
|
|
|
void DatabaseWindow::catalogLoaded()
|
2017-12-25 15:33:10 +01:00
|
|
|
|
{
|
|
|
|
|
|
try {
|
2018-12-30 15:46:15 +01:00
|
|
|
|
m_database = loadWatcher.future().result();
|
2017-12-25 15:33:10 +01:00
|
|
|
|
|
2019-08-15 16:18:47 +02:00
|
|
|
|
for (auto f : { "user", "pg_catalog", "information_schema" }) {
|
|
|
|
|
|
// TODO open inspector windows
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-12-29 10:56:24 +01:00
|
|
|
|
|
2018-12-15 20:27:40 +01:00
|
|
|
|
newCreateTablePage();
|
2019-01-29 19:41:27 +01:00
|
|
|
|
} catch (const OpenDatabaseException &ex) {
|
|
|
|
|
|
QMessageBox::critical(this, "Error reading database", ex.text());
|
2017-12-19 19:06:36 +01:00
|
|
|
|
close();
|
2017-02-04 11:55:49 +01:00
|
|
|
|
}
|
2017-01-15 21:01:40 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-12-28 12:55:11 +01:00
|
|
|
|
void DatabaseWindow::on_actionAbout_triggered()
|
2017-01-09 07:39:09 +01:00
|
|
|
|
{
|
2017-02-01 18:01:02 +01:00
|
|
|
|
QMessageBox::about(this, "pgLab 0.1", tr(
|
2019-01-05 11:12:47 +01:00
|
|
|
|
"Copyrights 2016-2019, Eelke Klein, All Rights Reserved.\n"
|
2017-02-01 18:01:02 +01:00
|
|
|
|
"\n"
|
2017-12-28 09:20:42 +01:00
|
|
|
|
"The program is provided AS IS with NO WARRANTY OF ANY KIND, "
|
|
|
|
|
|
"INCLUDING THE WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS "
|
2017-02-01 18:01:02 +01:00
|
|
|
|
"FOR A PARTICULAR PURPOSE.\n"
|
|
|
|
|
|
"\n"
|
2018-12-29 18:59:54 +01:00
|
|
|
|
"This program is dynamically linked with Qt 5.12 Copyright (C) 2018 "
|
2017-02-04 11:55:49 +01:00
|
|
|
|
"The Qt Company Ltd. https://www.qt.io/licensing/. \n"
|
2017-02-01 18:01:02 +01:00
|
|
|
|
"\n"
|
|
|
|
|
|
"Icons by fatcow http://www.fatcow.com/free-icons provided under Creative Commons "
|
2017-12-19 19:55:12 +01:00
|
|
|
|
"attribution 3.0 license."
|
2017-02-01 18:01:02 +01:00
|
|
|
|
));
|
|
|
|
|
|
|
2017-01-09 07:39:09 +01:00
|
|
|
|
}
|
2017-01-13 19:09:58 +01:00
|
|
|
|
|
2018-12-28 12:55:11 +01:00
|
|
|
|
void DatabaseWindow::on_actionShow_connection_manager_triggered()
|
2017-02-01 18:01:02 +01:00
|
|
|
|
{
|
|
|
|
|
|
m_masterController->showConnectionManager();
|
|
|
|
|
|
}
|
2017-02-04 11:55:49 +01:00
|
|
|
|
|
2018-12-28 12:55:11 +01:00
|
|
|
|
void DatabaseWindow::on_actionCopy_triggered()
|
2017-02-04 11:55:49 +01:00
|
|
|
|
{
|
|
|
|
|
|
// What should be copied?
|
|
|
|
|
|
|
|
|
|
|
|
QWidget *w = QApplication::focusWidget();
|
|
|
|
|
|
QTableView *tv = dynamic_cast<QTableView*>(w);
|
|
|
|
|
|
if (tv) {
|
|
|
|
|
|
copySelectionToClipboard(tv);
|
|
|
|
|
|
}
|
2017-02-05 08:23:06 +01:00
|
|
|
|
else {
|
|
|
|
|
|
const QMetaObject *meta = w->metaObject();
|
|
|
|
|
|
int i = meta->indexOfSlot("copy");
|
|
|
|
|
|
if (i != -1) {
|
|
|
|
|
|
QMetaMethod method = meta->method(i);
|
|
|
|
|
|
method.invoke(w, Qt::AutoConnection);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2017-02-04 11:55:49 +01:00
|
|
|
|
}
|
2017-02-05 08:23:06 +01:00
|
|
|
|
|