From 3e4917428de88a1e4e2ae6efb18a164387417a31 Mon Sep 17 00:00:00 2001 From: eelke Date: Sun, 6 Jan 2019 10:11:48 +0100 Subject: [PATCH] TablesPage is now CatalogInspector and is now a module. --- .../{TablesPage.cpp => CatalogInspector.cpp} | 89 ++++++++++++++----- pglab/{TablesPage.h => CatalogInspector.h} | 19 +++- pglab/CrudTab.cpp | 2 +- pglab/DatabaseWindow.cpp | 18 +--- pglab/QueryToolModule.cpp | 2 +- pglab/pglab.pro | 8 +- pglab/plugin_support/PluginModule.h | 8 +- 7 files changed, 101 insertions(+), 45 deletions(-) rename pglab/{TablesPage.cpp => CatalogInspector.cpp} (71%) rename pglab/{TablesPage.h => CatalogInspector.h} (81%) diff --git a/pglab/TablesPage.cpp b/pglab/CatalogInspector.cpp similarity index 71% rename from pglab/TablesPage.cpp rename to pglab/CatalogInspector.cpp index 1445c62..c8063e4 100644 --- a/pglab/TablesPage.cpp +++ b/pglab/CatalogInspector.cpp @@ -1,4 +1,4 @@ -#include "TablesPage.h" +#include "CatalogInspector.h" #include "ui_TablesPage.h" #include "catalog/PgAttribute.h" @@ -10,6 +10,7 @@ #include "ConstraintModel.h" #include "IconColumnDelegate.h" #include "IndexModel.h" +#include "OpenDatabase.h" #include "PgLabItemDelegate.h" #include "PropertiesPage.h" #include "ResultTableModelUtil.h" @@ -22,7 +23,7 @@ #include #include "plugin_support/IPluginContentWidgetContext.h" -TablesPage::TablesPage(IPluginContentWidgetContext *context_, QWidget *parent) +CatalogInspector::CatalogInspector(IPluginContentWidgetContext *context_, QWidget *parent) : PluginContentWidget(context_, parent) , ui(new Ui::TablesPage) { @@ -61,24 +62,24 @@ TablesPage::TablesPage(IPluginContentWidgetContext *context_, QWidget *parent) // --------------- // Table selection connect(ui->tableListTable->selectionModel(), &QItemSelectionModel::currentRowChanged, this, - &TablesPage::tableListTable_currentRowChanged); + &CatalogInspector::tableListTable_currentRowChanged); connect(m_tablesModel, &QAbstractItemModel::layoutChanged, - this, &TablesPage::tableListTable_layoutChanged); + this, &CatalogInspector::tableListTable_layoutChanged); //layoutChanged(const QList &parents = ..., QAbstractItemModel::LayoutChangeHint hint = ...) connect(ui->constraintsTable->selectionModel(), &QItemSelectionModel::selectionChanged, this, - &TablesPage::constraintsTable_selectionChanged); + &CatalogInspector::constraintsTable_selectionChanged); connect(ui->constraintsTable->model(), &QAbstractItemModel::modelReset, this, - &TablesPage::constraintsTable_modelReset); + &CatalogInspector::constraintsTable_modelReset); // React to changes in de selected indexes, does not trigger when model is reset connect(ui->indexesTable->selectionModel(), &QItemSelectionModel::selectionChanged, this, - &TablesPage::indexesTable_selectionChanged); + &CatalogInspector::indexesTable_selectionChanged); // Capture model reset independently connect(ui->indexesTable->model(), &QAbstractItemModel::modelReset, this, - &TablesPage::indexesTable_modelReset); + &CatalogInspector::indexesTable_modelReset); // Non designer based code // - Columns page @@ -102,12 +103,15 @@ TablesPage::TablesPage(IPluginContentWidgetContext *context_, QWidget *parent) // Force focus on columns tab by default ui->twDetails->setCurrentIndex(0); + auto db = context_->getObject(); + + setCatalog(db->catalog()); retranslateUi(false); } -void TablesPage::retranslateUi(bool all) +void CatalogInspector::retranslateUi(bool all) { if (all) ui->retranslateUi(this); @@ -123,12 +127,12 @@ void TablesPage::retranslateUi(bool all) } -TablesPage::~TablesPage() +CatalogInspector::~CatalogInspector() { delete ui; } -void TablesPage::setCatalog(std::shared_ptr cat) +void CatalogInspector::setCatalog(std::shared_ptr cat) { m_catalog = cat; m_tablesModel->setCatalog(cat); @@ -142,13 +146,28 @@ void TablesPage::setCatalog(std::shared_ptr cat) highlighter->setTypes(*cat->types()); } -void TablesPage::setNamespaceFilter(TablesTableModel::NamespaceFilter filter) +void CatalogInspector::setNamespaceFilter(TablesTableModel::NamespaceFilter filter) { m_tablesModel->setNamespaceFilter(filter); + QString hint = "Catalog instpector"; + QString caption = "Inspector"; + switch (filter) { + case TablesTableModel::PgCatalog: + hint += " - pg_catalog"; + caption = "pg_catalog"; + break; + case TablesTableModel::InformationSchema: + hint += " - information_schema"; + caption = "information_schema"; + break; + default: + break; + } + context()->setCaption(this, caption, hint); } -void TablesPage::tableListTable_currentRowChanged(const QModelIndex ¤t, const QModelIndex &previous) +void CatalogInspector::tableListTable_currentRowChanged(const QModelIndex ¤t, const QModelIndex &previous) { if (current.row() != previous.row()) { if (current.isValid()) { @@ -161,7 +180,7 @@ void TablesPage::tableListTable_currentRowChanged(const QModelIndex ¤t, co } -void TablesPage::tableListTable_layoutChanged(const QList &, QAbstractItemModel::LayoutChangeHint ) +void CatalogInspector::tableListTable_layoutChanged(const QList &, QAbstractItemModel::LayoutChangeHint ) { auto&& index = ui->tableListTable->selectionModel()->currentIndex(); if (index.isValid()) { @@ -173,7 +192,7 @@ void TablesPage::tableListTable_layoutChanged(const QList } -void TablesPage::selectedTableChanged(const std::optional &table) +void CatalogInspector::selectedTableChanged(const std::optional &table) { m_columnsPage->setData(m_catalog, table); @@ -189,7 +208,7 @@ void TablesPage::selectedTableChanged(const std::optional &table) updateSqlTab(table); } -void TablesPage::constraintsTable_selectionChanged(const QItemSelection &/*selected*/, const QItemSelection &/*deselected*/) +void CatalogInspector::constraintsTable_selectionChanged(const QItemSelection &/*selected*/, const QItemSelection &/*deselected*/) { const auto indexes = ui->constraintsTable->selectionModel()->selectedIndexes(); std::unordered_set rijen; @@ -206,12 +225,12 @@ void TablesPage::constraintsTable_selectionChanged(const QItemSelection &/*selec ui->constraintSqlEdit->setPlainText(drops % "\n" % creates); } -void TablesPage::constraintsTable_modelReset() +void CatalogInspector::constraintsTable_modelReset() { ui->constraintSqlEdit->clear(); } -void TablesPage::indexesTable_selectionChanged(const QItemSelection &/*selected*/, const QItemSelection &/*deselected*/) +void CatalogInspector::indexesTable_selectionChanged(const QItemSelection &/*selected*/, const QItemSelection &/*deselected*/) { const auto indexes = ui->indexesTable->selectionModel()->selectedIndexes(); std::unordered_set rijen; @@ -229,12 +248,12 @@ void TablesPage::indexesTable_selectionChanged(const QItemSelection &/*selected* } -void TablesPage::indexesTable_modelReset() +void CatalogInspector::indexesTable_modelReset() { ui->indexSqlEdit->clear(); } -void TablesPage::on_tableListTable_doubleClicked(const QModelIndex &index) +void CatalogInspector::on_tableListTable_doubleClicked(const QModelIndex &index) { PgClass table = m_tablesModel->getTable(index.row()); if (table.oid() != InvalidOid) { @@ -244,7 +263,7 @@ void TablesPage::on_tableListTable_doubleClicked(const QModelIndex &index) } } -void TablesPage::updateSqlTab(const std::optional &table) +void CatalogInspector::updateSqlTab(const std::optional &table) { if (!table.has_value()) { m_sqlCodePreview->clear(); @@ -286,3 +305,31 @@ void TablesPage::updateSqlTab(const std::optional &table) // m_sqlCodePreview->setPlainText(drop_sql % "\n\n" % create_sql); } + + +void CatalogInspectorModule::init() +{ + registerModuleAction("open", + [this] (IPluginContentWidgetContext* context, + const ModuleActionParameters ¶ms) + { + moduleAction_open(context, params); + }); +} + +void CatalogInspectorModule::moduleAction_open( + IPluginContentWidgetContext* context, + const ModuleActionParameters ¶ms + ) +{ + auto ct = new CatalogInspector(context, nullptr); + context->addContentWidget(this, ct); + auto nsf = params.at("namespace-filter").toString(); + TablesTableModel::NamespaceFilter filter = TablesTableModel::User; + if (nsf == "pg_catalog") filter = TablesTableModel::PgCatalog; + else if (nsf == "information_schema") filter = TablesTableModel::InformationSchema; + ct->setNamespaceFilter(filter); +} + +REGISTER_PLUGIN_MODULE_CAT(CatalogInspectorModule, "Catalog inspector tool", + "pglab.catalog-inspector", "database") diff --git a/pglab/TablesPage.h b/pglab/CatalogInspector.h similarity index 81% rename from pglab/TablesPage.h rename to pglab/CatalogInspector.h index e741605..8301398 100644 --- a/pglab/TablesPage.h +++ b/pglab/CatalogInspector.h @@ -25,13 +25,13 @@ class TriggerPage; class PgClass; class SqlCodePreview; -class TablesPage : public PluginContentWidget +class CatalogInspector : public PluginContentWidget { Q_OBJECT public: - explicit TablesPage(IPluginContentWidgetContext *context, QWidget *parent = nullptr); - ~TablesPage(); + explicit CatalogInspector(IPluginContentWidgetContext *context, QWidget *parent = nullptr); + ~CatalogInspector(); void setCatalog(std::shared_ptr cat); void setNamespaceFilter(TablesTableModel::NamespaceFilter filter); @@ -62,4 +62,17 @@ private slots: void on_tableListTable_doubleClicked(const QModelIndex &index); }; +class CatalogInspectorModule: public PluginModule { + Q_OBJECT +public: + using PluginModule::PluginModule; + + void init(); +private slots: + +private: + void moduleAction_open(IPluginContentWidgetContext* context, const ModuleActionParameters ¶ms); +}; + + #endif // TABLESPAGE_H diff --git a/pglab/CrudTab.cpp b/pglab/CrudTab.cpp index b0b0cf0..7b73a1e 100644 --- a/pglab/CrudTab.cpp +++ b/pglab/CrudTab.cpp @@ -138,4 +138,4 @@ void CrudPageModule::moduleAction_open( } -REGISTER_PLUGIN_MODULE(CrudPageModule, "CRUD tool", "pglab.crudpage") +REGISTER_PLUGIN_MODULE_CAT(CrudPageModule, "CRUD tool", "pglab.crudpage", "database") diff --git a/pglab/DatabaseWindow.cpp b/pglab/DatabaseWindow.cpp index 9381f95..4a06812 100644 --- a/pglab/DatabaseWindow.cpp +++ b/pglab/DatabaseWindow.cpp @@ -9,7 +9,6 @@ #include // Pages that should become modules -#include "TablesPage.h" #include "EditTableWidget.h" #include "CodeGenerator.h" #include "FunctionsPage.h" @@ -71,19 +70,10 @@ void DatabaseWindow::catalogLoaded() auto ctx = context(); ctx->registerObject(m_database); - auto tt = new TablesPage(ctx, this); - tt->setCatalog(m_database->catalog()); - m_tabWidget->addTab(tt, "Tables"); - - auto pg_cat_tables = new TablesPage(ctx, this); - pg_cat_tables->setNamespaceFilter(TablesTableModel::PgCatalog); - pg_cat_tables->setCatalog(m_database->catalog()); - m_tabWidget->addTab(pg_cat_tables, "pg_catalog"); - - auto info_schema_tables = new TablesPage(ctx, this); - info_schema_tables->setNamespaceFilter(TablesTableModel::InformationSchema); - info_schema_tables->setCatalog(m_database->catalog()); - m_tabWidget->addTab(info_schema_tables, "information_schema"); + for (auto f : { "user", "pg_catalog", "information_schema" }) + ctx->moduleAction("pglab.catalog-inspector", "open", { + { "namespace-filter", f } + }); auto functions_page = new FunctionsPage(this); functions_page->setCatalog(m_database->catalog()); diff --git a/pglab/QueryToolModule.cpp b/pglab/QueryToolModule.cpp index 55d7406..cc36d80 100644 --- a/pglab/QueryToolModule.cpp +++ b/pglab/QueryToolModule.cpp @@ -82,4 +82,4 @@ void QueryToolModule::menuAction_open(IPluginContentWidgetContext* context) } } -REGISTER_PLUGIN_MODULE(QueryToolModule, "Query tool", "pglab.querytool") +REGISTER_PLUGIN_MODULE_CAT(QueryToolModule, "Query tool", "pglab.querytool", "database") diff --git a/pglab/pglab.pro b/pglab/pglab.pro index 02a678d..fe0a375 100644 --- a/pglab/pglab.pro +++ b/pglab/pglab.pro @@ -48,7 +48,6 @@ SOURCES += main.cpp\ ResultTableModelUtil.cpp \ BaseTableModel.cpp \ QueryParamListController.cpp \ - TablesPage.cpp \ TablesTableModel.cpp \ ColumnTableModel.cpp \ NamespaceFilterWidget.cpp \ @@ -91,7 +90,8 @@ PropertyProxyModel.cpp \ plugin_support/LMainWindow.cpp \ plugin_support/LWidgetAction.cpp \ QueryTool.cpp \ - QueryToolModule.cpp + QueryToolModule.cpp \ + CatalogInspector.cpp HEADERS += \ QueryResultModel.h \ @@ -115,7 +115,6 @@ HEADERS += \ ResultTableModelUtil.h \ BaseTableModel.h \ QueryParamListController.h \ - TablesPage.h \ TablesTableModel.h \ ColumnTableModel.h \ NamespaceFilterWidget.h \ @@ -162,7 +161,8 @@ CustomDataRole.h \ plugin_support/LMainWindow.h \ plugin_support/LWidgetAction.h \ QueryTool.h \ - QueryToolModule.h + QueryToolModule.h \ + CatalogInspector.h FORMS += \ ConnectionManagerWindow.ui \ diff --git a/pglab/plugin_support/PluginModule.h b/pglab/plugin_support/PluginModule.h index 951347a..9927b34 100644 --- a/pglab/plugin_support/PluginModule.h +++ b/pglab/plugin_support/PluginModule.h @@ -60,9 +60,10 @@ private: template -std::shared_ptr createPluginModule(QString name, QString ident) +std::shared_ptr createPluginModule(QString name, QString ident, QString category={}) { auto module = std::make_shared(std::move(name), std::move(ident)); + module->setDisplayCategory(category); PluginRegister::getInstance()->registerModule(module); return std::move(module); @@ -73,5 +74,10 @@ std::shared_ptr createPluginModule(QString name, QString ident) std::weak_ptr register_variable = createPluginModule\ (name, ident);} +#define REGISTER_PLUGIN_MODULE_CAT(module, name, ident, category) \ + namespace {\ + std::weak_ptr register_variable = createPluginModule\ + (name, ident, category);} + #endif // PLUGIN_SUPPORTPLUGINMODULE_H