Extended the plugin system to allow for dynamic runtime bindings between modules.
As a test implementation, this allows the TablesPage to open a CrudTab for a table/view without the need for TablesPage, CrudTab and DatabaseWindow to know anything about each other.
This commit is contained in:
parent
f996703937
commit
b0cd47ef46
17 changed files with 209 additions and 55 deletions
|
|
@ -4,11 +4,16 @@
|
|||
#include "ResultTableModelUtil.h"
|
||||
#include "PgLabItemDelegate.h"
|
||||
#include "IntegerRange.h"
|
||||
#include "OpenDatabase.h"
|
||||
#include "catalog/PgClassContainer.h"
|
||||
#include "catalog/PgDatabaseCatalog.h"
|
||||
#include <QDebug>
|
||||
#include <QMenu>
|
||||
#include <QMessageBox>
|
||||
#include <iterator>
|
||||
#include <set>
|
||||
#include "plugin_support/PluginRegister.h"
|
||||
#include "plugin_support/IPluginContentWidgetContext.h"
|
||||
|
||||
|
||||
CrudTab::CrudTab(IPluginContentWidgetContext *context, QWidget *parent)
|
||||
|
|
@ -41,11 +46,11 @@ CrudTab::~CrudTab()
|
|||
delete ui;
|
||||
}
|
||||
|
||||
void CrudTab::setConfig(std::shared_ptr<OpenDatabase> db, const PgClass &table)
|
||||
void CrudTab::setConfig(Oid oid) //std::shared_ptr<OpenDatabase> db, const PgClass &table)
|
||||
{
|
||||
m_db = db;
|
||||
m_table = table;
|
||||
m_crudModel->setConfig(db, table);
|
||||
m_db = context()->getDatabase();;
|
||||
m_table = *m_db->catalog()->classes()->getByKey(oid);
|
||||
m_crudModel->setConfig(m_db, *m_table);
|
||||
}
|
||||
|
||||
void CrudTab::refresh()
|
||||
|
|
@ -113,3 +118,34 @@ void CrudTab::headerCustomContextMenu(const QPoint &pos)
|
|||
auto horizontal_header = ui->tableView->horizontalHeader();
|
||||
menu->popup(horizontal_header->mapToGlobal(pos));
|
||||
}
|
||||
|
||||
|
||||
void CrudPageModule::init()
|
||||
{
|
||||
registerModuleAction("open",
|
||||
[this] (IPluginContentWidgetContext* context,
|
||||
const ModuleActionParameters ¶ms)
|
||||
{
|
||||
moduleAction_open(context, params);
|
||||
});
|
||||
}
|
||||
|
||||
void CrudPageModule::moduleAction_open(
|
||||
IPluginContentWidgetContext* context,
|
||||
const ModuleActionParameters ¶ms
|
||||
)
|
||||
{
|
||||
// create new widget for specified table
|
||||
// hand widget to context for display
|
||||
CrudTab *ct = new CrudTab(context, nullptr);
|
||||
context->addContentWidget(ct); // maybe CrudTab should do this
|
||||
ct->setConfig(params.at("oid").toUInt());
|
||||
|
||||
}
|
||||
|
||||
namespace {
|
||||
|
||||
std::weak_ptr<PluginModule> register_variable = createPluginModule<CrudPageModule>
|
||||
("CRUD tool", "pglab.crudpage");
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@
|
|||
#include "catalog/PgClass.h"
|
||||
#include <QWidget>
|
||||
#include "plugin_support/PluginContentWidget.h"
|
||||
#include "plugin_support/PluginModule.h"
|
||||
#include <memory>
|
||||
#include <optional>
|
||||
|
||||
|
|
@ -22,7 +23,7 @@ public:
|
|||
explicit CrudTab(IPluginContentWidgetContext *context, QWidget *parent = nullptr);
|
||||
~CrudTab() override;
|
||||
|
||||
void setConfig(std::shared_ptr<OpenDatabase> db, const PgClass &table);
|
||||
void setConfig(Oid oid);
|
||||
|
||||
void refresh();
|
||||
|
||||
|
|
@ -41,4 +42,17 @@ private slots:
|
|||
void headerCustomContextMenu(const QPoint &pos);
|
||||
};
|
||||
|
||||
class CrudPageModule: public PluginModule {
|
||||
Q_OBJECT
|
||||
public:
|
||||
using PluginModule::PluginModule;
|
||||
|
||||
void init();
|
||||
private slots:
|
||||
|
||||
private:
|
||||
void moduleAction_open(IPluginContentWidgetContext* context, const ModuleActionParameters ¶ms);
|
||||
};
|
||||
|
||||
|
||||
#endif // CRUDTAB_H
|
||||
|
|
|
|||
|
|
@ -18,18 +18,17 @@
|
|||
#include "plugin_support/PluginContentWidget.h"
|
||||
#include "CodeGenerator.h"
|
||||
#include "MasterController.h"
|
||||
#include "CrudTab.h"
|
||||
#include "WorkManager.h"
|
||||
#include "ScopeGuard.h"
|
||||
#include "EditTableWidget.h"
|
||||
#include "IPluginContentWidgetContext.h"
|
||||
#include "plugin_support/PluginContentWidgetContextBase.h"
|
||||
#include "TaskExecutor.h"
|
||||
|
||||
namespace pg = Pgsql;
|
||||
|
||||
namespace DatabaseWindow_details {
|
||||
|
||||
class DatabaseWindowContentContext: public IPluginContentWidgetContext {
|
||||
class DatabaseWindowContentContext: public PluginContentWidgetContextBase {
|
||||
public:
|
||||
explicit DatabaseWindowContentContext(DatabaseWindow *window)
|
||||
: m_window(window)
|
||||
|
|
@ -54,6 +53,11 @@ namespace DatabaseWindow_details {
|
|||
{
|
||||
m_window->statusBar()->showMessage(msg);
|
||||
}
|
||||
|
||||
void addContentWidget(PluginContentWidget *widget) override
|
||||
{
|
||||
m_window->addPage(widget, "");
|
||||
}
|
||||
private:
|
||||
DatabaseWindow *m_window;
|
||||
};
|
||||
|
|
@ -96,13 +100,6 @@ void DatabaseWindow::newCreateTablePage()
|
|||
ui->tabWidget->addTab(w, "Create table");
|
||||
}
|
||||
|
||||
void DatabaseWindow::newCrudPage(const PgClass &table)
|
||||
{
|
||||
CrudTab *ct = new CrudTab(m_context, this);
|
||||
ct->setConfig(m_database, table);
|
||||
addPage(ct, table.objectName());
|
||||
}
|
||||
|
||||
void DatabaseWindow::newCodeGenPage(QString query, std::shared_ptr<const Pgsql::Result> dbres)
|
||||
{
|
||||
auto cgtab = new CodeGenerator(m_context, this);
|
||||
|
|
@ -110,7 +107,6 @@ void DatabaseWindow::newCodeGenPage(QString query, std::shared_ptr<const Pgsql::
|
|||
addPage(cgtab, "Codegen");
|
||||
}
|
||||
|
||||
|
||||
QueryTab *DatabaseWindow::GetActiveQueryTab()
|
||||
{
|
||||
QWidget *widget = ui->tabWidget->currentWidget();
|
||||
|
|
@ -143,16 +139,16 @@ void DatabaseWindow::catalogLoaded()
|
|||
//SCOPE_EXIT { loadFuture = {}; };
|
||||
m_database = loadWatcher.future().result();
|
||||
|
||||
auto tt = new TablesPage(this);
|
||||
auto tt = new TablesPage(m_context, this);
|
||||
tt->setCatalog(m_database->catalog());
|
||||
ui->tabWidget->addTab(tt, "Tables");
|
||||
|
||||
auto pg_cat_tables = new TablesPage(this);
|
||||
auto pg_cat_tables = new TablesPage(m_context, this);
|
||||
pg_cat_tables->setNamespaceFilter(TablesTableModel::PgCatalog);
|
||||
pg_cat_tables->setCatalog(m_database->catalog());
|
||||
ui->tabWidget->addTab(pg_cat_tables, "pg_catalog");
|
||||
|
||||
auto info_schema_tables = new TablesPage(this);
|
||||
auto info_schema_tables = new TablesPage(m_context, this);
|
||||
info_schema_tables->setNamespaceFilter(TablesTableModel::InformationSchema);
|
||||
info_schema_tables->setCatalog(m_database->catalog());
|
||||
ui->tabWidget->addTab(info_schema_tables, "information_schema");
|
||||
|
|
|
|||
|
|
@ -55,11 +55,13 @@ public:
|
|||
|
||||
std::shared_ptr<OpenDatabase> getDatabase() { return m_database; }
|
||||
|
||||
void newCrudPage(const PgClass &table);
|
||||
void newCodeGenPage(QString query, std::shared_ptr<const Pgsql::Result> dbres);
|
||||
|
||||
void setTabCaptionForWidget(QWidget *widget, const QString &caption, const QString &hint);
|
||||
void setTabIcon(QWidget *widget, const QString &iconname);
|
||||
|
||||
/// Called when a newly created page is added to the QTabWidget
|
||||
void addPage(PluginContentWidget* page, QString caption);
|
||||
private:
|
||||
|
||||
Ui::MainWindow *ui;
|
||||
|
|
@ -124,8 +126,6 @@ private:
|
|||
void newCreateTablePage();
|
||||
|
||||
|
||||
/// Called when a newly created page is added to the QTabWidget
|
||||
void addPage(PluginContentWidget* page, QString caption);
|
||||
/// Called when a page is completely removed from the QTabWidget
|
||||
void removePage(PluginContentWidget *page);
|
||||
|
||||
|
|
|
|||
|
|
@ -21,7 +21,7 @@
|
|||
#include "util.h"
|
||||
#include "GlobalIoService.h"
|
||||
#include "UserConfiguration.h"
|
||||
#include "IPluginContentWidgetContext.h"
|
||||
#include "plugin_support/IPluginContentWidgetContext.h"
|
||||
#include "plugin_support/PluginRegister.h"
|
||||
|
||||
QueryTab::QueryTab(IPluginContentWidgetContext *context_, QWidget *parent)
|
||||
|
|
@ -687,16 +687,7 @@ void QueryToolModule::new_triggered()
|
|||
|
||||
namespace {
|
||||
|
||||
|
||||
std::shared_ptr<PluginModule> createModule()
|
||||
{
|
||||
auto module = std::make_shared<QueryToolModule>("Query tool", "pglab.querytool");
|
||||
module->init();
|
||||
|
||||
PluginRegister::getInstance()->registerModule(module);
|
||||
return std::move(module);
|
||||
}
|
||||
|
||||
std::weak_ptr<PluginModule> register_variable = createModule();
|
||||
std::weak_ptr<PluginModule> register_variable = createPluginModule<QueryToolModule>
|
||||
("Query tool", "pglab.querytool");
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -9,8 +9,8 @@
|
|||
|
||||
#include <QWidget>
|
||||
#include "plugin_support/PluginContentWidget.h"
|
||||
#include <memory>
|
||||
#include "plugin_support/PluginModule.h"
|
||||
#include <memory>
|
||||
|
||||
namespace Ui {
|
||||
class QueryTab;
|
||||
|
|
|
|||
|
|
@ -21,11 +21,11 @@
|
|||
#include "SqlCodePreview.h"
|
||||
#include <QStringBuilder>
|
||||
#include <unordered_set>
|
||||
#include "plugin_support/IPluginContentWidgetContext.h"
|
||||
|
||||
TablesPage::TablesPage(DatabaseWindow *parent)
|
||||
: QWidget(parent)
|
||||
TablesPage::TablesPage(IPluginContentWidgetContext *context_, QWidget *parent)
|
||||
: PluginContentWidget(context_, parent)
|
||||
, ui(new Ui::TablesPage)
|
||||
, m_window(parent)
|
||||
{
|
||||
ui->setupUi(this);
|
||||
|
||||
|
|
@ -239,9 +239,10 @@ void TablesPage::on_tableListTable_doubleClicked(const QModelIndex &index)
|
|||
{
|
||||
PgClass table = m_tablesModel->getTable(index.row());
|
||||
if (table.oid() != InvalidOid) {
|
||||
m_window->newCrudPage(table);
|
||||
context()->moduleAction("pglab.crudpage", "open", {
|
||||
{ "oid", table.oid() }
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void TablesPage::updateSqlTab(const std::optional<PgClass> &table)
|
||||
|
|
|
|||
|
|
@ -6,6 +6,8 @@
|
|||
#include <optional>
|
||||
#include <QItemSelection>
|
||||
#include "TablesTableModel.h"
|
||||
#include "plugin_support/PluginContentWidget.h"
|
||||
#include "plugin_support/PluginModule.h"
|
||||
|
||||
namespace Ui {
|
||||
class TablesPage;
|
||||
|
|
@ -18,25 +20,23 @@ class ConstraintModel;
|
|||
class PgDatabaseCatalog;
|
||||
class NamespaceFilterWidget;
|
||||
class IndexModel;
|
||||
class DatabaseWindow;
|
||||
class PropertiesPage;
|
||||
class TriggerPage;
|
||||
class PgClass;
|
||||
class SqlCodePreview;
|
||||
|
||||
class TablesPage : public QWidget
|
||||
class TablesPage : public PluginContentWidget
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit TablesPage(DatabaseWindow *parent = nullptr);
|
||||
explicit TablesPage(IPluginContentWidgetContext *context, QWidget *parent = nullptr);
|
||||
~TablesPage();
|
||||
|
||||
void setCatalog(std::shared_ptr<PgDatabaseCatalog> cat);
|
||||
void setNamespaceFilter(TablesTableModel::NamespaceFilter filter);
|
||||
private:
|
||||
Ui::TablesPage *ui;
|
||||
DatabaseWindow *m_window;
|
||||
// QWidget *m_columnsTab;
|
||||
ColumnPage *m_columnsPage;
|
||||
// QWidget *m_propertiesTab;
|
||||
|
|
|
|||
|
|
@ -87,7 +87,8 @@ PropertyProxyModel.cpp \
|
|||
plugin_support/MenuLocation.cpp \
|
||||
plugin_support/ToolbarLocation.cpp \
|
||||
plugin_support/PluginRegister.cpp \
|
||||
plugin_support/PluginContentWidget.cpp
|
||||
plugin_support/PluginContentWidget.cpp \
|
||||
plugin_support/PluginContentWidgetContextBase.cpp
|
||||
|
||||
HEADERS += \
|
||||
QueryResultModel.h \
|
||||
|
|
@ -147,13 +148,15 @@ CustomDataRole.h \
|
|||
SequencesPage.h \
|
||||
DatabaseWindow.h \
|
||||
PgLabTableView.h \
|
||||
IPluginContentWidgetContext.h \
|
||||
plugin_support/PluginModule.h \
|
||||
plugin_support/MenuPath.h \
|
||||
plugin_support/MenuLocation.h \
|
||||
plugin_support/ToolbarLocation.h \
|
||||
plugin_support/PluginRegister.h \
|
||||
plugin_support/PluginContentWidget.h
|
||||
plugin_support/PluginContentWidget.h \
|
||||
plugin_support/ModuleActionParameters.h \
|
||||
plugin_support/IPluginContentWidgetContext.h \
|
||||
plugin_support/PluginContentWidgetContextBase.h
|
||||
|
||||
FORMS += mainwindow.ui \
|
||||
ConnectionManagerWindow.ui \
|
||||
|
|
|
|||
|
|
@ -3,7 +3,7 @@
|
|||
|
||||
#include <QString>
|
||||
#include <memory>
|
||||
#include "tsqueue.h"
|
||||
#include "plugin_support/ModuleActionParameters.h"
|
||||
|
||||
class OpenDatabase;
|
||||
class PluginContentWidget;
|
||||
|
|
@ -11,7 +11,8 @@ class PluginContentWidget;
|
|||
/** This class serves to isolate the plugin from the actual construct in which it is
|
||||
* used.
|
||||
*
|
||||
* It provides functions for operating on the context without needing to many details.
|
||||
* It provides interface for operating on the context without needing to many details.
|
||||
* Actual default implementation is in PluginContentWidgetContextBase.
|
||||
*/
|
||||
class IPluginContentWidgetContext {
|
||||
public:
|
||||
|
|
@ -36,9 +37,15 @@ public:
|
|||
*/
|
||||
virtual std::shared_ptr<OpenDatabase> getDatabase() = 0;
|
||||
|
||||
virtual void QueueTask(TSQueue::t_Callable c) = 0;
|
||||
|
||||
virtual void showStatusMessage(const QString &msg) = 0;
|
||||
|
||||
virtual void moduleAction(
|
||||
const QString &module_identifier,
|
||||
QString module_action,
|
||||
const ModuleActionParameters &action_params
|
||||
) = 0;
|
||||
|
||||
virtual void addContentWidget(PluginContentWidget *widget) = 0;
|
||||
};
|
||||
|
||||
#endif // IPLUGINCONTENTWIDGETCONTEXT_H
|
||||
10
pglab/plugin_support/ModuleActionParameters.h
Normal file
10
pglab/plugin_support/ModuleActionParameters.h
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
#ifndef MODULEACTIONPARAMETERS_H
|
||||
#define MODULEACTIONPARAMETERS_H
|
||||
|
||||
#include <map>
|
||||
#include <QString>
|
||||
#include <QVariant>
|
||||
|
||||
using ModuleActionParameters = std::map<QString, QVariant>;
|
||||
|
||||
#endif // MODULEACTIONPARAMETERS_H
|
||||
27
pglab/plugin_support/PluginContentWidgetContextBase.cpp
Normal file
27
pglab/plugin_support/PluginContentWidgetContextBase.cpp
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
#include "PluginContentWidgetContextBase.h"
|
||||
#include "PluginRegister.h"
|
||||
#include "PluginModule.h"
|
||||
#include <QDebug>
|
||||
|
||||
PluginContentWidgetContextBase::PluginContentWidgetContextBase() = default;
|
||||
|
||||
void PluginContentWidgetContextBase::moduleAction(
|
||||
const QString &module_identifier,
|
||||
QString module_action,
|
||||
const ModuleActionParameters &action_params
|
||||
)
|
||||
{
|
||||
auto reg = PluginRegister::getInstance();
|
||||
auto mod = reg->findModule(module_identifier);
|
||||
if (mod) {
|
||||
auto action = mod->findModuleAction(module_action);
|
||||
if (action) {
|
||||
qDebug() << QString("module %1 action %2 called ").arg(module_identifier, module_action);
|
||||
(*action)(this, action_params);
|
||||
}
|
||||
else
|
||||
qWarning() << QString("module %1 has no action %2").arg(module_identifier, module_action);
|
||||
}
|
||||
else
|
||||
qWarning() << QString("module not found %1").arg(module_identifier);
|
||||
}
|
||||
19
pglab/plugin_support/PluginContentWidgetContextBase.h
Normal file
19
pglab/plugin_support/PluginContentWidgetContextBase.h
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
#ifndef PLUGINCONTENTWIDGETCONTEXTBASE_H
|
||||
#define PLUGINCONTENTWIDGETCONTEXTBASE_H
|
||||
|
||||
#include "plugin_support/IPluginContentWidgetContext.h"
|
||||
|
||||
/// Provides base implementation of IPluginContentWidgetContext
|
||||
class PluginContentWidgetContextBase : public IPluginContentWidgetContext
|
||||
{
|
||||
public:
|
||||
PluginContentWidgetContextBase();
|
||||
|
||||
void moduleAction(
|
||||
const QString &module_identifier,
|
||||
QString module_action,
|
||||
const ModuleActionParameters &action_params
|
||||
) override;
|
||||
};
|
||||
|
||||
#endif // PLUGINCONTENTWIDGETCONTEXTBASE_H
|
||||
|
|
@ -16,4 +16,18 @@ void PluginModule::registerAction(QAction *action, MenuLocation menu_location, T
|
|||
|
||||
}
|
||||
|
||||
void PluginModule::registerModuleAction(QString module_action, ModuleAction action)
|
||||
{
|
||||
m_moduleActions.emplace(
|
||||
std::move(module_action),
|
||||
std::move(action)
|
||||
);
|
||||
}
|
||||
|
||||
const PluginModule::ModuleAction* PluginModule::findModuleAction(const QString &module_action) const
|
||||
{
|
||||
auto res = m_moduleActions.find(module_action);
|
||||
if (res == m_moduleActions.end())
|
||||
return nullptr;
|
||||
return &res->second;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,16 +1,23 @@
|
|||
#ifndef PLUGIN_SUPPORTPLUGINMODULE_H
|
||||
#define PLUGIN_SUPPORTPLUGINMODULE_H
|
||||
|
||||
#include "plugin_support/MenuLocation.h"
|
||||
#include "plugin_support/ToolbarLocation.h"
|
||||
#include "MenuLocation.h"
|
||||
#include "ToolbarLocation.h"
|
||||
#include "ModuleActionParameters.h"
|
||||
#include "PluginRegister.h"
|
||||
#include <QObject>
|
||||
#include <functional>
|
||||
#include <map>
|
||||
|
||||
class QAction;
|
||||
|
||||
class IPluginContentWidgetContext;
|
||||
|
||||
class PluginModule: public QObject {
|
||||
Q_OBJECT
|
||||
public:
|
||||
using ModuleAction = std::function<void(IPluginContentWidgetContext*, const ModuleActionParameters &)>;
|
||||
using ModuleActionMap = std::map<QString, ModuleAction>;
|
||||
|
||||
PluginModule(QString name, QString ident);
|
||||
|
||||
const QString& name() const { return m_name; }
|
||||
|
|
@ -19,13 +26,32 @@ public:
|
|||
|
||||
void setDisplayCategory(QString category);
|
||||
void registerAction(QAction *action, MenuLocation menu_location, ToolbarLocation toolbar_location);
|
||||
|
||||
void registerModuleAction(QString module_action, ModuleAction action);
|
||||
|
||||
/// Searches for and returns a pointer to the requested module action.
|
||||
/// When the action is not found nullptr is returned.
|
||||
const ModuleAction* findModuleAction(const QString &module_action) const;
|
||||
private:
|
||||
/// Name shown to end users
|
||||
QString m_name;
|
||||
/// Unique identifier
|
||||
QString m_ident;
|
||||
QString m_displayCategory;
|
||||
|
||||
ModuleActionMap m_moduleActions;
|
||||
};
|
||||
|
||||
|
||||
template <typename T>
|
||||
std::shared_ptr<PluginModule> createPluginModule(QString name, QString ident)
|
||||
{
|
||||
auto module = std::make_shared<T>(std::move(name), std::move(ident));
|
||||
module->init();
|
||||
|
||||
PluginRegister::getInstance()->registerModule(module);
|
||||
return std::move(module);
|
||||
}
|
||||
|
||||
|
||||
#endif // PLUGIN_SUPPORTPLUGINMODULE_H
|
||||
|
|
|
|||
|
|
@ -25,5 +25,13 @@ PluginRegister::PluginRegister() = default;
|
|||
|
||||
void PluginRegister::registerModule(PluginModuleSPtr module)
|
||||
{
|
||||
qDebug() << "Register called for " << module->identifier();
|
||||
m_moduleMap.emplace(module->identifier(), module);
|
||||
}
|
||||
|
||||
const PluginModule* PluginRegister::findModule(const QString &module_ident) const
|
||||
{
|
||||
auto res = m_moduleMap.find(module_ident);
|
||||
if (res == m_moduleMap.end())
|
||||
return nullptr;
|
||||
return res->second.get();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -19,6 +19,8 @@ public:
|
|||
PluginRegister();
|
||||
void registerModule(PluginModuleSPtr module);
|
||||
const ModuleMap& modules() const { return m_moduleMap; }
|
||||
|
||||
const PluginModule* findModule(const QString &module_ident) const;
|
||||
private:
|
||||
|
||||
ModuleMap m_moduleMap;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue