From e2fa613a49e8d0386bcc299b5131de3b3485fdee Mon Sep 17 00:00:00 2001 From: eelke Date: Thu, 15 Aug 2019 19:32:33 +0200 Subject: [PATCH] Most of menu is back in its original state before the plugin experiment. --- pglab/DatabaseWindow.cpp | 307 +++++++++++++++++++++++++++------------ pglab/DatabaseWindow.h | 31 +++- 2 files changed, 237 insertions(+), 101 deletions(-) diff --git a/pglab/DatabaseWindow.cpp b/pglab/DatabaseWindow.cpp index 4f06d4e..7fade0a 100644 --- a/pglab/DatabaseWindow.cpp +++ b/pglab/DatabaseWindow.cpp @@ -71,7 +71,14 @@ void DatabaseWindow::newCodeGenPage(QString query, std::shared_ptrInit(m_database->catalog(), query, dbres); // addPage(cgtab, "Codegen"); -// + // +} + +QueryTool *DatabaseWindow::GetActiveQueryTool() +{ + auto widget = m_tabWidget->currentWidget(); + auto qt = dynamic_cast(widget); + return qt; } void DatabaseWindow::setConfig(const ConnectionConfig &config) @@ -109,6 +116,37 @@ void DatabaseWindow::createActions() action->setObjectName("actionClose"); action->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_W)); } + { + QIcon icon; + icon.addFile(QString::fromUtf8(":/icons/page_white_copy.png"), QSize(), QIcon::Normal, QIcon::On); + auto action = actionCopy = new QAction(icon, tr("Copy"), this); + action->setObjectName("actionCopy"); + action->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_C)); + } + { + QIcon icon; + icon.addFile(QString::fromUtf8(":/icons/token_shortland_character.png"), QSize(), QIcon::Normal, QIcon::On); + auto action = actionCopyAsCString = new QAction(icon, tr("Copy as C string"), this); + action->setObjectName("actionCopyAsCString"); + action->setShortcut(QKeySequence(Qt::CTRL + Qt::ALT + Qt::Key_C)); + } + { + QIcon icon; + icon.addFile(QString::fromUtf8(":/icons/token_shortland_character.png"), QSize(), QIcon::Normal, QIcon::On); + auto action = actionCopyAsRawCppString = new QAction(icon, tr("Copy as raw C++-string"), this); + action->setObjectName("actionCopyAsRawCppString"); + action->setShortcut(QKeySequence(Qt::CTRL + Qt::ALT + Qt::Key_C)); + } + { + QIcon icon; + icon.addFile(QString::fromUtf8(":/icons/table_save.png"), QSize(), QIcon::Normal, QIcon::On); + auto action = actionExportData = new QAction(icon, tr("Export data"), this); + action->setObjectName("actionExportData"); + } + { + auto action = actionGenerateCode = new QAction(tr("Generate code"), this); + action->setObjectName("actionGenerateCode"); + } { QIcon icon; icon.addFile(QString::fromUtf8(":/icons/page_white_add.png"), QSize(), QIcon::Normal, QIcon::On); @@ -141,72 +179,170 @@ void DatabaseWindow::createActions() action->setObjectName("actionOpenSql"); action->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_O)); } - -// { -// auto ca = makeContextAction(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(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(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); -// } - + { + QIcon icon; + icon.addFile(QString::fromUtf8(":/icons/script_save.png"), QSize(), QIcon::Normal, QIcon::On); + auto action = actionSaveSql = new QAction(icon, tr("Save query"), this); + action->setObjectName("actionSaveSql"); + action->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_S)); + } + { + auto action = actionSaveSqlAs = new QAction(tr("Save query as"), this); + action->setObjectName("actionSaveSqlAs"); + } + { + auto action = actionSaveCopyOfSqlAs = new QAction(tr("Save copy of query as"), this); + action->setObjectName("actionSaveCopyOfSqlAs"); + } + { + auto action = actionShowConnectionManager = new QAction(tr("Show connection manager"), this); + action->setObjectName("actionShowConnectionManager"); + } } + 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, - actionInspectUserSchemas, - actionInspectPgCatalog, - actionInspectInformationSchema, - seperator, - actionClose + actionNewSql, + actionOpenSql, + seperator(), + actionSaveSql, + actionSaveSqlAs, + actionSaveCopyOfSqlAs, + seperator(), + actionInspectUserSchemas, + actionInspectPgCatalog, + actionInspectInformationSchema, + seperator(), + actionExportData, + seperator(), + actionClose }); + menuEdit = mb->addMenu(tr("edit")); + menuEdit->addActions({ + actionCopy, + actionCopyAsCString, + actionCopyAsRawCppString, + actionGenerateCode + }); + menuWindow = mb->addMenu(tr("Window")); + menuWindow->addActions({ + actionShowConnectionManager + }); + + menuHelp = mb->addMenu(tr("Help")); menuHelp->addActions({ - seperator, - actionAbout + seperator(), + actionAbout }); setMenuBar(mb); } +QAction *DatabaseWindow::seperator() +{ + auto ac = new QAction(this); + ac->setSeparator(true); + return ac; +} + + + +void DatabaseWindow::catalogLoaded() +{ + try { + m_database = loadWatcher.future().result(); + + for (auto f : { "user", "pg_catalog", "information_schema" }) { + // TODO open inspector windows + } + + + newCreateTablePage(); + } catch (const OpenDatabaseException &ex) { + QMessageBox::critical(this, "Error reading database", ex.text()); + close(); + } +} + + +void DatabaseWindow::on_actionAbout_triggered() +{ + QMessageBox::about(this, "pgLab 0.1", tr( + "Copyrights 2016-2019, Eelke Klein, All Rights Reserved.\n" + "\n" + "The program is provided AS IS with NO WARRANTY OF ANY KIND, " + "INCLUDING THE WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS " + "FOR A PARTICULAR PURPOSE.\n" + "\n" + "This program is dynamically linked with Qt 5.12 Copyright (C) 2018 " + "The Qt Company Ltd. https://www.qt.io/licensing/. \n" + "\n" + "Icons by fatcow http://www.fatcow.com/free-icons provided under Creative Commons " + "attribution 3.0 license." + )); + +} + +void DatabaseWindow::on_actionCopy_triggered() +{ + QWidget *w = QApplication::focusWidget(); + QTableView *tv = dynamic_cast(w); + if (tv) { + copySelectionToClipboard(tv); + } + else { + const QMetaObject *meta = w->metaObject(); + int i = meta->indexOfSlot("copy"); + if (i != -1) { + QMetaMethod method = meta->method(i); + method.invoke(w, Qt::AutoConnection); + } + } +} + +void DatabaseWindow::on_actionCopyAsCString_triggered() +{ + auto query_tool = GetActiveQueryTool(); + if (query_tool) { + query_tool->copyQueryAsCString(); + } +} + +void DatabaseWindow::on_actionCopyAsRawCppString_triggered() +{ + auto query_tool = GetActiveQueryTool(); + if (query_tool) { + query_tool->copyQueryAsRawCppString(); + } +} + +void DatabaseWindow::on_actionExportData_triggered() +{ + auto query_tool = GetActiveQueryTool(); + if (query_tool) { + query_tool->exportData(); + } +} + +void DatabaseWindow::on_actionGenerateCode_triggered() +{ + auto query_tool = GetActiveQueryTool(); + if (query_tool) { + query_tool->generateCode(); + } +} void DatabaseWindow::on_actionClose_triggered() { m_tabWidget->tabCloseRequested(m_tabWidget->currentIndex()); } -void DatabaseWindow::on_actionNewSql_triggered() -{ - auto *ct = new QueryTool(m_database, this); - addPage(ct, "query"); - ct->newdoc(); -} - void DatabaseWindow::on_actionInspectInformationSchema_triggered() { auto ct = new CatalogInspector(m_database, this); @@ -228,6 +364,13 @@ void DatabaseWindow::on_actionInspectUserSchemas_triggered() ct->setNamespaceFilter(NamespaceFilter::User); } +void DatabaseWindow::on_actionNewSql_triggered() +{ + auto *ct = new QueryTool(m_database, this); + addPage(ct, "query"); + ct->newdoc(); +} + void DatabaseWindow::on_actionOpenSql_triggered() { QString home_dir = QStandardPaths::locate(QStandardPaths::HomeLocation, "", QStandardPaths::LocateDirectory); @@ -235,69 +378,41 @@ void DatabaseWindow::on_actionOpenSql_triggered() 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? + if (ct->load(file_name)) { + addPage(ct, ""); + } + else { + delete ct; } } } -void DatabaseWindow::catalogLoaded() +void DatabaseWindow::on_actionSaveSql_triggered() { - try { - m_database = loadWatcher.future().result(); - - for (auto f : { "user", "pg_catalog", "information_schema" }) { - // TODO open inspector windows - } - - - newCreateTablePage(); - } catch (const OpenDatabaseException &ex) { - QMessageBox::critical(this, "Error reading database", ex.text()); - close(); + auto query_tool = GetActiveQueryTool(); + if (query_tool) { + query_tool->save(); } } -void DatabaseWindow::on_actionAbout_triggered() +void DatabaseWindow::on_actionSaveSqlAs_triggered() { - QMessageBox::about(this, "pgLab 0.1", tr( - "Copyrights 2016-2019, Eelke Klein, All Rights Reserved.\n" - "\n" - "The program is provided AS IS with NO WARRANTY OF ANY KIND, " - "INCLUDING THE WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS " - "FOR A PARTICULAR PURPOSE.\n" - "\n" - "This program is dynamically linked with Qt 5.12 Copyright (C) 2018 " - "The Qt Company Ltd. https://www.qt.io/licensing/. \n" - "\n" - "Icons by fatcow http://www.fatcow.com/free-icons provided under Creative Commons " - "attribution 3.0 license." - )); - + auto query_tool = GetActiveQueryTool(); + if (query_tool) { + query_tool->saveAs(); + } } -void DatabaseWindow::on_actionShow_connection_manager_triggered() +void DatabaseWindow::on_actionSaveCopyOfSqlAs_triggered() +{ + auto query_tool = GetActiveQueryTool(); + if (query_tool) { + query_tool->saveCopyAs(); + } +} + +void DatabaseWindow::on_actionShowConnectionManager_triggered() { m_masterController->showConnectionManager(); } -void DatabaseWindow::on_actionCopy_triggered() -{ - // What should be copied? - - QWidget *w = QApplication::focusWidget(); - QTableView *tv = dynamic_cast(w); - if (tv) { - copySelectionToClipboard(tv); - } - else { - const QMetaObject *meta = w->metaObject(); - int i = meta->indexOfSlot("copy"); - if (i != -1) { - QMetaMethod method = meta->method(i); - method.invoke(w, Qt::AutoConnection); - } - } -} - diff --git a/pglab/DatabaseWindow.h b/pglab/DatabaseWindow.h index 71ee989..61a673e 100644 --- a/pglab/DatabaseWindow.h +++ b/pglab/DatabaseWindow.h @@ -16,6 +16,7 @@ namespace Pgsql { class MasterController; class QCloseEvent; class OpenDatabase; +class QueryTool; class PgClass; class QAction; @@ -44,6 +45,8 @@ public: void addPage(QWidget* page, QString caption); void newCodeGenPage(QString query, std::shared_ptr dbres); + + QueryTool *GetActiveQueryTool(); private: QTabWidget *m_tabWidget = nullptr; QToolBar *m_mainToolBar = nullptr; @@ -59,15 +62,25 @@ private: // Standard actions QAction *actionAbout = nullptr; QAction *actionClose = nullptr; + QAction *actionCopy = nullptr; + QAction *actionCopyAsCString = nullptr; + QAction *actionCopyAsRawCppString = nullptr; + QAction *actionExportData = nullptr; + QAction *actionGenerateCode = nullptr; QAction *actionInspectInformationSchema = nullptr; ///< Create or switch to pgcatalog tab QAction *actionInspectPgCatalog = nullptr; ///< Create or switch to pgcatalog tab QAction *actionInspectUserSchemas = nullptr; ///< Create or switch to pgcatalog tab QAction *actionNewSql = nullptr; QAction *actionOpenSql = nullptr; + QAction *actionSaveSql = nullptr; + QAction *actionSaveSqlAs = nullptr; + QAction *actionSaveCopyOfSqlAs = nullptr; + QAction *actionShowConnectionManager = nullptr; - + QMenu *menuEdit = nullptr; QMenu *menuFile = nullptr; QMenu *menuHelp = nullptr; + QMenu *menuWindow = nullptr; class LoadCatalog: public ControllableTask { @@ -92,20 +105,28 @@ private: void createActions(); void initMenus(); + QAction* seperator(); private slots: void catalogLoaded(); // void tabWidget_tabCloseRequested(int index); // void tabWidget_currentChanged(int index); + void on_actionAbout_triggered(); void on_actionClose_triggered(); - void on_actionNewSql_triggered(); + void on_actionCopy_triggered(); + void on_actionCopyAsCString_triggered(); + void on_actionCopyAsRawCppString_triggered(); + void on_actionExportData_triggered(); + void on_actionGenerateCode_triggered(); void on_actionInspectInformationSchema_triggered(); void on_actionInspectPgCatalog_triggered(); void on_actionInspectUserSchemas_triggered(); + void on_actionNewSql_triggered(); void on_actionOpenSql_triggered(); - void on_actionAbout_triggered(); - void on_actionShow_connection_manager_triggered(); - void on_actionCopy_triggered(); + void on_actionSaveSql_triggered(); + void on_actionSaveSqlAs_triggered(); + void on_actionSaveCopyOfSqlAs_triggered(); + void on_actionShowConnectionManager_triggered(); }; #endif // MAINWINDOW_H