From 683853e72e3df4d06eb8e7ccb63f725ce556f1ee Mon Sep 17 00:00:00 2001 From: eelke Date: Thu, 8 Jul 2021 16:28:32 +0200 Subject: [PATCH] Do not rely on connectSlotsByName anymore as it breaks easily. In the process I also made the initialisation of the actions more compact. --- pglab/DatabaseWindow.cpp | 261 ++++++++++++--------------------------- pglab/DatabaseWindow.h | 55 +++++---- 2 files changed, 108 insertions(+), 208 deletions(-) diff --git a/pglab/DatabaseWindow.cpp b/pglab/DatabaseWindow.cpp index 7588768..a8dbf57 100644 --- a/pglab/DatabaseWindow.cpp +++ b/pglab/DatabaseWindow.cpp @@ -26,10 +26,6 @@ namespace pg = Pgsql; - - - - DatabaseWindow::DatabaseWindow(MasterController *master, QWidget *parent) : QMainWindow(parent) , m_masterController(master) @@ -41,10 +37,11 @@ DatabaseWindow::DatabaseWindow(MasterController *master, QWidget *parent) m_tabWidget->setObjectName("m_tabWidget"); setCentralWidget(m_tabWidget); - createActions(); - initMenus(); + connect(m_tabWidget, &QTabWidget::tabCloseRequested, this, &DatabaseWindow::m_tabWidget_tabCloseRequested); + connect(m_tabWidget, &QTabWidget::currentChanged, this, &DatabaseWindow::m_tabWidget_currentChanged); - QMetaObject::connectSlotsByName(this); + createActions(); + initMenus(); setAcceptDrops(true); } @@ -121,158 +118,58 @@ void DatabaseWindow::setConfig(const ConnectionConfig &config) } } +QAction* DatabaseWindow::createAction(QString iconname, QString caption, void (DatabaseWindow::*func)()) +{ + QIcon icon; + icon.addFile(iconname, QSize(), QIcon::Normal, QIcon::On); + QAction *action = new QAction(icon, caption, this); + connect(action, &QAction::triggered, this, func); + return action; +} + +QAction* DatabaseWindow::createAction(QString caption, void (DatabaseWindow::*func)()) +{ + QAction *action = new QAction(caption, this); + connect(action, &QAction::triggered, this, func); + return action; +} void DatabaseWindow::createActions() { - { - QIcon icon; - icon.addFile(QString::fromUtf8(":/icons/about.png"), QSize(), QIcon::Normal, QIcon::On); - auto action = actionAbout = new QAction(icon, tr("About"), this); - action->setObjectName("actionAbout"); - } - { - QIcon icon; - icon.addFile(QString::fromUtf8(":/icons/script_delete.png"), QSize(), QIcon::Normal, QIcon::On); - auto action = actionCancelQuery = new QAction(icon, tr("Cancel query"), this); - action->setObjectName("actionCancelQuery"); - } - { - 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); - 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::SHIFT | 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"); + actionAbout = createAction(":/icons/about.png", tr("About"), &DatabaseWindow::actionAbout_triggered); + actionCancelQuery = createAction(":/icons/script_delete.png", tr("Cancel query"), &DatabaseWindow::actionCancelQuery_triggered); + actionClose = createAction(":/icons/page_white_delete.png", tr("Close"), &DatabaseWindow::actionClose_triggered); + actionCopy = createAction(":/icons/page_white_copy.png", tr("Copy"), &DatabaseWindow::actionCopy_triggered); + actionCopyAsCString = createAction(":/icons/token_shortland_character.png", tr("Copy as C string"), &DatabaseWindow::actionCopyAsCString_triggered); + actionCopyAsRawCppString = createAction(":/icons/token_shortland_character.png", tr("Copy as raw C++-string"), &DatabaseWindow::actionCopyAsRawCppString_triggered); + actionExecuteQuery = createAction(":/icons/script_go.png", tr("Execute query"), &DatabaseWindow::actionExecuteQuery_triggered); + actionExplain = createAction(":/icons/lightbulb_off.png", tr("Explain"), &DatabaseWindow::actionExplain_triggered); + actionExplainAnalyze = createAction(":/icons/lightbulb.png", tr("Explain analyze"), &DatabaseWindow::actionExplainAnalyze_triggered); + actionExportData = createAction(":/icons/table_save.png", tr("Export data"), &DatabaseWindow::actionExportData_triggered); + actionGenerateCode = createAction(tr("Generate code"), &DatabaseWindow::actionGenerateCode_triggered); + actionInspectInformationSchema = createAction(":/icons/page_white_add.png", tr("Inspect information_schema"), &DatabaseWindow::actionInspectInformationSchema_triggered); + actionInspectPgCatalog = createAction(":/icons/page_white_add.png", tr("Inspect pg_catalog"), &DatabaseWindow::actionInspectPgCatalog_triggered); + actionInspectUserSchemas = createAction(":/icons/page_white_add.png", tr("Inspect user schemas"), &DatabaseWindow::actionInspectUserSchemas_triggered); + actionServerInspector = createAction(":/icons/page_white_add.png", tr("Inspect server"), &DatabaseWindow::actionServerInspector_triggered); + actionNewSql = createAction(":/icons/new_query_tab.png", tr("New Query"), &DatabaseWindow::actionNewSql_triggered); + actionOpenSql = createAction(":/icons/folder.png", tr("Open Query"), &DatabaseWindow::actionOpenSql_triggered); + actionSaveSql = createAction(":/icons/script_save.png", tr("Save Query"), &DatabaseWindow::actionSaveSql_triggered); + actionPasteLangString = createAction(tr("Paste lang string"), &DatabaseWindow::actionPasteLangString_triggered); + actionRefreshCatalog = createAction(tr("Refresh"), &DatabaseWindow::actionRefreshCatalog_triggered); + actionRefreshCrud = createAction(tr("Refresh"), &DatabaseWindow::actionRefreshCrud_triggered); + actionSaveSqlAs = createAction(tr("Save query as"), &DatabaseWindow::actionSaveSqlAs_triggered); + actionSaveCopyOfSqlAs = createAction(tr("Save copy of query as"), &DatabaseWindow::actionSaveCopyOfSqlAs_triggered); + actionShowConnectionManager = createAction(tr("Show connection manager"), &DatabaseWindow::actionShowConnectionManager_triggered); - } - { - QIcon icon; - icon.addFile(QString::fromUtf8(":/icons/script_go.png"), QSize(), QIcon::Normal, QIcon::On); - auto action = actionExecuteQuery = new QAction(icon, tr("Execute query"), this); - action->setObjectName("actionExecuteQuery"); - action->setShortcut(QKeySequence(Qt::Key_F5)); - action->setShortcutContext(Qt::WidgetWithChildrenShortcut); - } - { - QIcon icon; - icon.addFile(QString::fromUtf8(":/icons/lightbulb_off.png"), QSize(), QIcon::Normal, QIcon::On); - auto action = actionExplain = new QAction(icon, tr("Explain"), this); - action->setObjectName("actionExplain"); - action->setShortcut(QKeySequence(Qt::Key_F7)); - } - { - QIcon icon; - icon.addFile(QString::fromUtf8(":/icons/lightbulb.png"), QSize(), QIcon::Normal, QIcon::On); - auto action = actionExplainAnalyze = new QAction(icon, tr("Explain analyze"), this); - action->setObjectName("actionExplainAnalyze"); - action->setShortcut(QKeySequence(Qt::SHIFT | Qt::Key_F7)); - } - { - 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); - 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 = actionServerInspector = new QAction(icon, tr("Inspect server"), this); - action->setObjectName("actionServerInspector"); - } - { - 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"); - } - { - 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); - action->setObjectName("actionNewSql"); - 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); - action->setObjectName("actionOpenSql"); - action->setShortcut(QKeySequence(Qt::CTRL | Qt::Key_O)); - } - { - QIcon icon; - //icon.addFile(QString::fromUtf8(":/icons/folder.png"), QSize(), QIcon::Normal, QIcon::On); - auto action = actionPasteLangString = new QAction(icon, tr("Paste lang string"), this); - action->setObjectName("actionPasteLangString"); - action->setShortcut(QKeySequence(Qt::CTRL | Qt::SHIFT | Qt::Key_V)); - } - { - QIcon icon; - auto action = actionRefreshCatalog = new QAction(icon, tr("Refresh"), this); - action->setShortcut(QKeySequence(Qt::Key_F5)); - action->setShortcutContext(Qt::WidgetWithChildrenShortcut); - action->setObjectName("actionRefreshCatalog"); - } - { - auto action = actionRefreshCrud = new QAction(QIcon(), tr("Refresh"), this); - action->setShortcut(QKeySequence(Qt::Key_F5)); - action->setShortcutContext(Qt::WidgetWithChildrenShortcut); - action->setObjectName("actionRefreshCrud"); - } - { - 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"); - } + actionClose->setShortcut(QKeySequence(Qt::CTRL | Qt::Key_W)); + actionCopy->setShortcut(QKeySequence(Qt::CTRL | Qt::Key_C)); + actionCopyAsCString->setShortcut(QKeySequence(Qt::CTRL | Qt::SHIFT | Qt::Key_C)); + actionExecuteQuery->setShortcut(QKeySequence(Qt::Key_F5)); + actionExplain->setShortcut(QKeySequence(Qt::Key_F7)); + actionExplainAnalyze->setShortcut(QKeySequence(Qt::SHIFT | Qt::Key_F7)); + actionNewSql->setShortcut(QKeySequence(Qt::CTRL | Qt::Key_N)); + actionOpenSql->setShortcut(QKeySequence(Qt::CTRL | Qt::Key_O)); + actionSaveSql->setShortcut(QKeySequence(Qt::CTRL | Qt::Key_S)); } @@ -422,7 +319,7 @@ void DatabaseWindow::catalogLoaded() // // TODO open inspector windows // } // newCreateTablePage(); - on_actionNewSql_triggered(); + actionNewSql_triggered(); } catch (const OpenDatabaseException &ex) { QMessageBox::critical(this, "Error reading database", ex.text()); close(); @@ -435,7 +332,7 @@ void DatabaseWindow::tableSelected(Oid tableoid) } -void DatabaseWindow::on_actionAbout_triggered() +void DatabaseWindow::actionAbout_triggered() { QMessageBox::about(this, "pgLab 0.1", tr( "Copyrights 2016-2019, Eelke Klein, All Rights Reserved.\n" @@ -453,7 +350,7 @@ void DatabaseWindow::on_actionAbout_triggered() } -void DatabaseWindow::on_actionCancelQuery_triggered() +void DatabaseWindow::actionCancelQuery_triggered() { auto query_tool = GetActiveQueryTool(); if (query_tool) { @@ -461,12 +358,12 @@ void DatabaseWindow::on_actionCancelQuery_triggered() } } -void DatabaseWindow::on_actionClose_triggered() +void DatabaseWindow::actionClose_triggered() { m_tabWidget->tabCloseRequested(m_tabWidget->currentIndex()); } -void DatabaseWindow::on_actionCopy_triggered() +void DatabaseWindow::actionCopy_triggered() { QWidget *w = QApplication::focusWidget(); if (w == nullptr) @@ -489,76 +386,76 @@ void DatabaseWindow::InvokeCopyIfPresent(QWidget *w) } } -void DatabaseWindow::on_actionCopyAsCString_triggered() +void DatabaseWindow::actionCopyAsCString_triggered() { auto query_tool = GetActiveQueryTool(); if (query_tool) query_tool->copyQueryAsCString(); } -void DatabaseWindow::on_actionCopyAsRawCppString_triggered() +void DatabaseWindow::actionCopyAsRawCppString_triggered() { auto query_tool = GetActiveQueryTool(); if (query_tool) query_tool->copyQueryAsRawCppString(); } -void DatabaseWindow::on_actionExecuteQuery_triggered() +void DatabaseWindow::actionExecuteQuery_triggered() { auto query_tool = GetActiveQueryTool(); if (query_tool) query_tool->execute(); } -void DatabaseWindow::on_actionExplain_triggered() +void DatabaseWindow::actionExplain_triggered() { auto query_tool = GetActiveQueryTool(); if (query_tool) query_tool->explain(false); } -void DatabaseWindow::on_actionExplainAnalyze_triggered() +void DatabaseWindow::actionExplainAnalyze_triggered() { auto query_tool = GetActiveQueryTool(); if (query_tool) query_tool->explain(true); } -void DatabaseWindow::on_actionExportData_triggered() +void DatabaseWindow::actionExportData_triggered() { auto query_tool = GetActiveQueryTool(); if (query_tool) query_tool->exportData(); } -void DatabaseWindow::on_actionGenerateCode_triggered() +void DatabaseWindow::actionGenerateCode_triggered() { auto query_tool = GetActiveQueryTool(); if (query_tool) query_tool->generateCode(); } -void DatabaseWindow::on_actionInspectInformationSchema_triggered() +void DatabaseWindow::actionInspectInformationSchema_triggered() { newCatalogInspectorPage("information_schema", NamespaceFilter::InformationSchema); } -void DatabaseWindow::on_actionInspectPgCatalog_triggered() +void DatabaseWindow::actionInspectPgCatalog_triggered() { newCatalogInspectorPage("pg_catalog", NamespaceFilter::PgCatalog); } -void DatabaseWindow::on_actionInspectUserSchemas_triggered() +void DatabaseWindow::actionInspectUserSchemas_triggered() { newCatalogInspectorPage("Schema", NamespaceFilter::User); } -void DatabaseWindow::on_actionServerInspector_triggered() +void DatabaseWindow::actionServerInspector_triggered() { newServerInspectorPage(); } -void DatabaseWindow::on_actionNewSql_triggered() +void DatabaseWindow::actionNewSql_triggered() { auto *ct = new QueryTool(this, this); ct->addAction(actionExecuteQuery); @@ -566,7 +463,7 @@ void DatabaseWindow::on_actionNewSql_triggered() ct->newdoc(); } -void DatabaseWindow::on_actionOpenSql_triggered() +void DatabaseWindow::actionOpenSql_triggered() { QString home_dir = QStandardPaths::locate(QStandardPaths::HomeLocation, "", QStandardPaths::LocateDirectory); QString file_name = QFileDialog::getOpenFileName(this, @@ -574,57 +471,57 @@ void DatabaseWindow::on_actionOpenSql_triggered() openSqlFile(file_name); } -void DatabaseWindow::on_actionPasteLangString_triggered() +void DatabaseWindow::actionPasteLangString_triggered() { auto query_tool = GetActiveQueryTool(); if (query_tool) query_tool->pasteLangString(); } -void DatabaseWindow::on_actionRefreshCatalog_triggered() +void DatabaseWindow::actionRefreshCatalog_triggered() { m_database->refresh(); } -void DatabaseWindow::on_actionRefreshCrud_triggered() +void DatabaseWindow::actionRefreshCrud_triggered() { auto crud = GetActiveCrud(); if (crud) crud->refresh(); } -void DatabaseWindow::on_actionSaveSql_triggered() +void DatabaseWindow::actionSaveSql_triggered() { auto query_tool = GetActiveQueryTool(); if (query_tool) query_tool->save(); } -void DatabaseWindow::on_actionSaveSqlAs_triggered() +void DatabaseWindow::actionSaveSqlAs_triggered() { auto query_tool = GetActiveQueryTool(); if (query_tool) query_tool->saveAs(); } -void DatabaseWindow::on_actionSaveCopyOfSqlAs_triggered() +void DatabaseWindow::actionSaveCopyOfSqlAs_triggered() { auto query_tool = GetActiveQueryTool(); if (query_tool) query_tool->saveCopyAs(); } -void DatabaseWindow::on_actionShowConnectionManager_triggered() +void DatabaseWindow::actionShowConnectionManager_triggered() { m_masterController->connectionController()->showConnectionManager(); } -void DatabaseWindow::on_m_tabWidget_tabCloseRequested(int index) +void DatabaseWindow::m_tabWidget_tabCloseRequested(int index) { closeTab(index); } -void DatabaseWindow::on_m_tabWidget_currentChanged(int) +void DatabaseWindow::m_tabWidget_currentChanged(int) { auto widget = m_tabWidget->currentWidget(); auto qt = dynamic_cast(widget); diff --git a/pglab/DatabaseWindow.h b/pglab/DatabaseWindow.h index 6094fad..95e908a 100644 --- a/pglab/DatabaseWindow.h +++ b/pglab/DatabaseWindow.h @@ -135,38 +135,41 @@ private: /// If it has it invokes this method using reflection. static void InvokeCopyIfPresent(QWidget *w); + QAction* createAction(QString icon, QString caption, void (DatabaseWindow::*func)()); + QAction* createAction(QString caption, void (DatabaseWindow::*func)()); + private slots: void catalogLoaded(); void tableSelected(Oid tableoid); // void tabWidget_tabCloseRequested(int index); // void tabWidget_currentChanged(int index); - void on_actionAbout_triggered(); - void on_actionCancelQuery_triggered(); - void on_actionClose_triggered(); - void on_actionCopy_triggered(); - void on_actionCopyAsCString_triggered(); - void on_actionCopyAsRawCppString_triggered(); - void on_actionExecuteQuery_triggered(); - void on_actionExplain_triggered(); - void on_actionExplainAnalyze_triggered(); - void on_actionExportData_triggered(); - void on_actionGenerateCode_triggered(); - void on_actionInspectInformationSchema_triggered(); - void on_actionInspectPgCatalog_triggered(); - void on_actionInspectUserSchemas_triggered(); - void on_actionServerInspector_triggered(); - void on_actionNewSql_triggered(); - void on_actionOpenSql_triggered(); - void on_actionPasteLangString_triggered(); - void on_actionRefreshCatalog_triggered(); - void on_actionRefreshCrud_triggered(); - void on_actionSaveSql_triggered(); - void on_actionSaveSqlAs_triggered(); - void on_actionSaveCopyOfSqlAs_triggered(); - void on_actionShowConnectionManager_triggered(); - void on_m_tabWidget_tabCloseRequested(int index); - void on_m_tabWidget_currentChanged(int index); + void actionAbout_triggered(); + void actionCancelQuery_triggered(); + void actionClose_triggered(); + void actionCopy_triggered(); + void actionCopyAsCString_triggered(); + void actionCopyAsRawCppString_triggered(); + void actionExecuteQuery_triggered(); + void actionExplain_triggered(); + void actionExplainAnalyze_triggered(); + void actionExportData_triggered(); + void actionGenerateCode_triggered(); + void actionInspectInformationSchema_triggered(); + void actionInspectPgCatalog_triggered(); + void actionInspectUserSchemas_triggered(); + void actionServerInspector_triggered(); + void actionNewSql_triggered(); + void actionOpenSql_triggered(); + void actionPasteLangString_triggered(); + void actionRefreshCatalog_triggered(); + void actionRefreshCrud_triggered(); + void actionSaveSql_triggered(); + void actionSaveSqlAs_triggered(); + void actionSaveCopyOfSqlAs_triggered(); + void actionShowConnectionManager_triggered(); + void m_tabWidget_tabCloseRequested(int index); + void m_tabWidget_currentChanged(int index); // IDatabaseWindow interface public: