diff --git a/pglab/CodeGenerator.cpp b/pglab/CodeGenerator.cpp index 2fa9752..207dff4 100644 --- a/pglab/CodeGenerator.cpp +++ b/pglab/CodeGenerator.cpp @@ -5,9 +5,9 @@ #include "UserConfiguration.h" #include -CodeGenerator::CodeGenerator(IPluginContentWidgetContext *context, PluginModule *module, QWidget *parent) : - PluginContentWidget(context, module, parent), - ui(new Ui::CodeGenerator) +CodeGenerator::CodeGenerator(QWidget *parent) + : QWidget(parent) + , ui(new Ui::CodeGenerator) { ui->setupUi(this); diff --git a/pglab/CodeGenerator.h b/pglab/CodeGenerator.h index ecd01dd..9c3e716 100644 --- a/pglab/CodeGenerator.h +++ b/pglab/CodeGenerator.h @@ -2,7 +2,6 @@ #define CODEGENERATOR_H #include -#include "plugin_support/PluginContentWidget.h" #include "Pgsql_declare.h" namespace Ui { @@ -11,12 +10,10 @@ class CodeGenerator; class PgDatabaseCatalog; -class CodeGenerator : public PluginContentWidget -{ +class CodeGenerator : public QWidget { Q_OBJECT - public: - CodeGenerator(IPluginContentWidgetContext *context, PluginModule *module, QWidget *parent = nullptr); + CodeGenerator(QWidget *parent = nullptr); ~CodeGenerator(); void Init(std::shared_ptr catalog, QString query, std::shared_ptr dbres); diff --git a/pglab/CrudTab.cpp b/pglab/CrudTab.cpp index ceb2f94..8953bf2 100644 --- a/pglab/CrudTab.cpp +++ b/pglab/CrudTab.cpp @@ -12,16 +12,16 @@ #include #include #include -#include "plugin_support/PluginRegister.h" -#include "plugin_support/IPluginContentWidgetContext.h" -CrudTab::CrudTab(IPluginContentWidgetContext *context, PluginModule *module, QWidget *parent) - : PluginContentWidget(context, module, parent) +CrudTab::CrudTab(std::shared_ptr open_database, QWidget *parent) + : QWidget(parent) , ui(new Ui::CrudTab) { ui->setupUi(this); + m_db = open_database; + SetTableViewDefault(ui->tableView); auto delegate = new PgLabItemDelegate(ui->tableView); @@ -48,7 +48,6 @@ CrudTab::~CrudTab() void CrudTab::setConfig(Oid oid) //std::shared_ptr db, const PgClass &table) { - m_db = context()->getObject(); // getDatabase();; m_table = *m_db->catalog()->classes()->getByKey(oid); m_crudModel->setConfig(m_db, *m_table); } @@ -117,33 +116,33 @@ void CrudTab::initActions() } } -QList CrudTab::actions() -{ - return { m_refreshAction }; -} +//QList CrudTab::actions() +//{ +// return { m_refreshAction }; +//} -void CrudPageModule::init() -{ - registerModuleAction("open", - [this] (IPluginContentWidgetContext* context, - const ModuleActionParameters ¶ms) - { - moduleAction_open(context, params); - }); -} +//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, this); - context->addContentWidget(ct); // maybe CrudTab should do this - ct->setConfig(params.at("oid").toUInt()); +//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, this); +// context->addContentWidget(ct); // maybe CrudTab should do this +// ct->setConfig(params.at("oid").toUInt()); -} +//} -REGISTER_PLUGIN_MODULE_CAT(CrudPageModule, "CRUD tool", "pglab.crudpage", "database") +//REGISTER_PLUGIN_MODULE_CAT(CrudPageModule, "CRUD tool", "pglab.crudpage", "database") diff --git a/pglab/CrudTab.h b/pglab/CrudTab.h index b75dbb5..961eaaf 100644 --- a/pglab/CrudTab.h +++ b/pglab/CrudTab.h @@ -3,8 +3,6 @@ #include "catalog/PgClass.h" #include -#include "plugin_support/PluginContentWidget.h" -#include "plugin_support/PluginModule.h" #include #include @@ -15,13 +13,10 @@ namespace Ui { class OpenDatabase; class CrudModel; -class CrudTab : public PluginContentWidget -{ +class CrudTab : public QWidget { Q_OBJECT - public: - explicit CrudTab(IPluginContentWidgetContext *context, PluginModule *module, - QWidget *parent = nullptr); + explicit CrudTab(std::shared_ptr open_database, QWidget *parent = nullptr); ~CrudTab() override; void setConfig(Oid oid); @@ -38,25 +33,25 @@ private: void initActions(); - virtual QList actions() override; +// virtual QList actions() override; private slots: void on_actionRemove_rows_triggered(); void headerCustomContextMenu(const QPoint &pos); }; -class CrudPageModule: public PluginModule { - Q_OBJECT -public: - using PluginModule::PluginModule; +//class CrudPageModule: public PluginModule { +// Q_OBJECT +//public: +// using PluginModule::PluginModule; - void init(); -private slots: +// void init(); +//private slots: -private: +//private: - void moduleAction_open(IPluginContentWidgetContext* context, const ModuleActionParameters ¶ms); -}; +// void moduleAction_open(IPluginContentWidgetContext* context, const ModuleActionParameters ¶ms); +//}; #endif // CRUDTAB_H diff --git a/pglab/QueryTool.cpp b/pglab/QueryTool.cpp index e93a89f..41eca8b 100644 --- a/pglab/QueryTool.cpp +++ b/pglab/QueryTool.cpp @@ -19,7 +19,6 @@ #include "util.h" #include "GlobalIoService.h" #include "UserConfiguration.h" -#include "plugin_support/IPluginContentWidgetContext.h" QueryTool::QueryTool(std::shared_ptr open_database, QWidget *parent) diff --git a/pglab/main.cpp b/pglab/main.cpp index a78a29c..38482a6 100644 --- a/pglab/main.cpp +++ b/pglab/main.cpp @@ -5,7 +5,6 @@ #endif #include #include "GlobalIoService.h" -#include "plugin_support/PluginRegister.h" int main(int argc, char *argv[]) { @@ -30,8 +29,6 @@ int main(int argc, char *argv[]) QCoreApplication::setOrganizationDomain("eelkeklein.nl"); QCoreApplication::setApplicationName("pglab"); - PluginRegister::getInstance()->initModules(); - std::thread asio_service_thread; int result = -1; { diff --git a/pglab/pglab.pro b/pglab/pglab.pro index 8a485cc..ec5369a 100644 --- a/pglab/pglab.pro +++ b/pglab/pglab.pro @@ -75,16 +75,8 @@ PropertyProxyModel.cpp \ SequenceModel.cpp \ DatabaseWindow.cpp \ PgLabTableView.cpp \ - plugin_support/PluginModule.cpp \ - plugin_support/MenuPath.cpp \ - plugin_support/MenuLocation.cpp \ - plugin_support/ToolbarLocation.cpp \ - plugin_support/PluginRegister.cpp \ - plugin_support/PluginContentWidget.cpp \ - plugin_support/PluginContentWidgetContextBase.cpp \ QueryTool.cpp \ CatalogInspector.cpp \ - plugin_support/StaticAction.cpp \ widgets/CatalogIndexPage.cpp \ widgets/CatalogPageBase.cpp \ widgets/CatalogConstraintPage.cpp \ @@ -148,18 +140,8 @@ CustomDataRole.h \ SequenceModel.h \ DatabaseWindow.h \ PgLabTableView.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/ModuleActionParameters.h \ - plugin_support/IPluginContentWidgetContext.h \ - plugin_support/PluginContentWidgetContextBase.h \ QueryTool.h \ CatalogInspector.h \ - plugin_support/StaticAction.h \ widgets/CatalogIndexPage.h \ widgets/CatalogPageBase.h \ widgets/CatalogConstraintPage.h \ diff --git a/pglab/plugin_support/IPluginContentWidgetContext.h b/pglab/plugin_support/IPluginContentWidgetContext.h deleted file mode 100644 index 5c531c0..0000000 --- a/pglab/plugin_support/IPluginContentWidgetContext.h +++ /dev/null @@ -1,98 +0,0 @@ -#ifndef IPLUGINCONTENTWIDGETCONTEXT_H -#define IPLUGINCONTENTWIDGETCONTEXT_H - -#include -#include -#include -#include -#include "plugin_support/ModuleActionParameters.h" - -class OpenDatabase; -class PluginModule; -class PluginContentWidget; - - -/** This class serves to isolate the plugin from the actual construct in which it is - * used. - * - * It provides interface for operating on the context without needing to many details. - * Actual default implementation is in PluginContentWidgetContextBase. - * - * objectRegistry implementation is from Igor Tandetnik answer to the following question - * https://stackoverflow.com/questions/35413745/using-shared-ptr-with-a-generic-registry-or-shared-object-storage-or-not - */ -class IPluginContentWidgetContext { -public: - - virtual ~IPluginContentWidgetContext() = default; - /** Tells the context what to use as a caption for this content widget. - * - * Depending on the context the caption might not be visible or used as the caption - * of a window or tab. - */ - virtual void setCaption(PluginContentWidget *content, const QString &caption, const QString &hint = {}) = 0; - /** Tells the context what icon to use. - * - * In general the icon is used in a similar place as the caption. - * \param iconname Assumed to be the name of an iconresource. The system will look for different - * sizes under :/icons//iconname - */ - virtual void setIcon(PluginContentWidget *content, const QString &iconname) = 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; - virtual void removeContentWidget(PluginContentWidget *widget) = 0; - - /** Return a widget you can use as a parent - */ - virtual QWidget* container() = 0; - - template - bool addObjects(Args&&...args); - template - bool registerObject(std::shared_ptr object); - template - std::shared_ptr getObject() const; - -private: - std::map > m_objectRegistry; - -}; - -template -bool IPluginContentWidgetContext::addObjects(Args&&...args) -{ - std::type_index key(typeid(T)); - if (!m_objectRegistry.count(key)) { - auto p = std::make_shared(std::forward(args)...); - m_objectRegistry[key] = p; - return true; - } - return false; -} - -template -bool IPluginContentWidgetContext::registerObject(std::shared_ptr object) -{ - return m_objectRegistry.emplace(std::type_index(typeid(T)), object).second; -} - -template -std::shared_ptr IPluginContentWidgetContext::getObject() const -{ - auto it = m_objectRegistry.find(typeid(T)); - if (it == m_objectRegistry.end()) { - return {}; - } - return std::static_pointer_cast(it->second); -} - - -#endif // IPLUGINCONTENTWIDGETCONTEXT_H diff --git a/pglab/plugin_support/MenuLocation.cpp b/pglab/plugin_support/MenuLocation.cpp deleted file mode 100644 index 3b80754..0000000 --- a/pglab/plugin_support/MenuLocation.cpp +++ /dev/null @@ -1,8 +0,0 @@ -#include "MenuLocation.h" - -MenuLocation::MenuLocation() = default; - -MenuLocation::MenuLocation(MenuPath path, int position) - : m_path(std::move(path)) - , m_position(position) -{} diff --git a/pglab/plugin_support/MenuLocation.h b/pglab/plugin_support/MenuLocation.h deleted file mode 100644 index 6ac0752..0000000 --- a/pglab/plugin_support/MenuLocation.h +++ /dev/null @@ -1,20 +0,0 @@ -#ifndef MENULOCATION_H -#define MENULOCATION_H - -#include "plugin_support/MenuPath.h" -#include - -/// -class MenuLocation { -public: - - MenuLocation(); - MenuLocation(MenuPath path, int position = -1); - - bool isEmpty() const; -private: - MenuPath m_path; - int m_position = -1; -}; - -#endif // MENULOCATION_H diff --git a/pglab/plugin_support/MenuPath.cpp b/pglab/plugin_support/MenuPath.cpp deleted file mode 100644 index bd8f210..0000000 --- a/pglab/plugin_support/MenuPath.cpp +++ /dev/null @@ -1,17 +0,0 @@ -#include "MenuPath.h" - -MenuPath::MenuPath() = default; - -//MenuPath::MenuPath(QString menu_path) -// : m_menuPath(std::move(menu_path)) -//{ -//} - -MenuPath::MenuPath(QString menu_path) -{ - auto parts = menu_path.splitRef('/'); - for (auto&& elem : parts) { - m_elems.emplace_back(elem); - } -} - diff --git a/pglab/plugin_support/MenuPath.h b/pglab/plugin_support/MenuPath.h deleted file mode 100644 index da32305..0000000 --- a/pglab/plugin_support/MenuPath.h +++ /dev/null @@ -1,47 +0,0 @@ -#ifndef MENUPATH_H -#define MENUPATH_H - -#include -#include -#include - - -/// For a menu path we use a string very similar to a file path. Width however the addition of semicolons -/// for specifying groups. So the following path -/// -/// /File:Save -/// -/// Specifies the File menu and the group Save within that menu while -/// -/// /File/Export -/// -/// Would specify the export sub menu of the file menu -/// -class MenuPath { -public: - MenuPath(); - MenuPath(QString menu_path); - - -private: - class Elem { - public: - QStringRef menu; - QStringRef group; - - explicit Elem(QStringRef s) - { - auto p = s.split(':'); - menu = p[0]; - if (p.size() > 1) { - group = p[1]; - } - } - }; - - std::vector m_elems; -}; - - - -#endif // MENUPATH_H diff --git a/pglab/plugin_support/ModuleActionParameters.h b/pglab/plugin_support/ModuleActionParameters.h deleted file mode 100644 index e3fb45c..0000000 --- a/pglab/plugin_support/ModuleActionParameters.h +++ /dev/null @@ -1,10 +0,0 @@ -#ifndef MODULEACTIONPARAMETERS_H -#define MODULEACTIONPARAMETERS_H - -#include -#include -#include - -using ModuleActionParameters = std::map; - -#endif // MODULEACTIONPARAMETERS_H diff --git a/pglab/plugin_support/PluginContentWidget.cpp b/pglab/plugin_support/PluginContentWidget.cpp deleted file mode 100644 index 83b9782..0000000 --- a/pglab/plugin_support/PluginContentWidget.cpp +++ /dev/null @@ -1,22 +0,0 @@ -#include "PluginContentWidget.h" - -PluginContentWidget::PluginContentWidget(IPluginContentWidgetContext *context, - PluginModule *module, QWidget* parent) - : QWidget(parent) - , m_context(context) - , m_pluginModule(module) -{} - -bool PluginContentWidget::canClose() -{ - return true; -} - -QList PluginContentWidget::actions() -{ return QList(); } - -PluginModule *PluginContentWidget::pluginModule() -{ return m_pluginModule; } - -IPluginContentWidgetContext *PluginContentWidget::context() -{ return m_context; } diff --git a/pglab/plugin_support/PluginContentWidget.h b/pglab/plugin_support/PluginContentWidget.h deleted file mode 100644 index ee79c3e..0000000 --- a/pglab/plugin_support/PluginContentWidget.h +++ /dev/null @@ -1,38 +0,0 @@ -#ifndef PGLPAGE_H -#define PGLPAGE_H - -#include -#include - -class IPluginContentWidgetContext; -class PluginModule; - -/// Provides a pluggable system for toolbar buttons and menu actions -/// -/// We will need several kind of actions -/// - create actions, these will create a new document or load from file , always available in menu -/// - save actions available when on tab -/// - edit actions -/// - custom menu? -/// -/// Can we use same groupings for toolbars and menu's -/// How about additional toolbars? -/// -class PluginContentWidget: public QWidget{ -public: - PluginContentWidget(IPluginContentWidgetContext *context, PluginModule *module, QWidget* parent = nullptr); - /// Returns the toolbar buttons for this page - virtual bool canClose(); - - virtual QList actions(); - - PluginModule *pluginModule(); - IPluginContentWidgetContext *context(); -protected: - -private: - IPluginContentWidgetContext *m_context = nullptr; - PluginModule *m_pluginModule = nullptr; -}; - -#endif // PGLPAGE_H diff --git a/pglab/plugin_support/PluginContentWidgetContextBase.cpp b/pglab/plugin_support/PluginContentWidgetContextBase.cpp deleted file mode 100644 index f97e577..0000000 --- a/pglab/plugin_support/PluginContentWidgetContextBase.cpp +++ /dev/null @@ -1,117 +0,0 @@ -#include "PluginContentWidgetContextBase.h" -#include "PluginContentWidget.h" -#include "PluginModule.h" -#include "PluginRegister.h" -#include -#include -#include -#include -#include - - - -LWidgetData::LWidgetData(PluginModule *module, PluginContentWidget *widget) - : m_module(module) - , m_widget(widget) -{} - -void LWidgetData::init() -{ -} - -void LWidgetData::addToMenu(QMenuBar *menubar) -{ - auto&& menu = menubar->actions().first()->menu(); - - auto ti = std::type_index(typeid(*m_widget)); - auto&& actions = m_module->actionsForContext(ti); - m_menuActions.reserve(actions.size()); - for (auto&& actiondef : actions) { - auto ac = actiondef->createAction(m_widget); - menu->addAction(ac); - m_menuActions.push_back(ac); - } -} - -void LWidgetData::removeFromMenu(QMenuBar *menubar) -{ - for (auto&& action : m_menuActions) { - delete action; - } - m_menuActions.clear(); -} - - -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); -} - -void PluginContentWidgetContextBase::addContentWidget(PluginContentWidget *widget) -{ - auto res = m_widgetLst.emplace(widget, LWidgetData{widget->pluginModule(), widget}); - if (!res.second) - throw std::runtime_error("Unexpected conflicting key on insertiong PluginContentWidgetContextBase::addContentWidget"); - - res.first->second.init(); -} - -void PluginContentWidgetContextBase::removeContentWidget(PluginContentWidget *widget) -{ - auto res = m_widgetLst.find(widget); - if (res == m_widgetLst.end()) - return; - - m_widgetLst.erase(res); -} - - -void PluginContentWidgetContextBase::addWidgetActionsToToolbar(PluginContentWidget *widget, QToolBar *toolbar) -{ - auto && actions = widget->actions(); - toolbar->addActions(actions); - -} - -void PluginContentWidgetContextBase::removeWidgetActionsFromToolbar(PluginContentWidget *widget, QToolBar *toolbar) -{ - auto && actions = widget->actions(); - for (auto && ac : actions) - toolbar->removeAction(ac); - -} - -void PluginContentWidgetContextBase::addContextActionsToMenu(PluginContentWidget *widget, QMenuBar *menubar) -{ - auto res = m_widgetLst.find(widget); - if (res == m_widgetLst.end()) - return; - res->second.addToMenu(menubar); -} - -void PluginContentWidgetContextBase::removeContextActionsFromMenu(PluginContentWidget *widget, QMenuBar *menubar) -{ - auto res = m_widgetLst.find(widget); - if (res == m_widgetLst.end()) - return; - res->second.removeFromMenu(menubar); -} - diff --git a/pglab/plugin_support/PluginContentWidgetContextBase.h b/pglab/plugin_support/PluginContentWidgetContextBase.h deleted file mode 100644 index aeaa40c..0000000 --- a/pglab/plugin_support/PluginContentWidgetContextBase.h +++ /dev/null @@ -1,79 +0,0 @@ -#ifndef PLUGINCONTENTWIDGETCONTEXTBASE_H -#define PLUGINCONTENTWIDGETCONTEXTBASE_H - -#include "plugin_support/IPluginContentWidgetContext.h" -#include -#include - -class LContextAction; -class QToolBar; -class QAction; -class QMenuBar; - -/// Maintains the list of actions added to a toolbar for a specific widget -/// it facilitates the removal of all those actions. -class WidgetToolbarActionList { -public: - QToolBar *m_toolBar; - std::vector m_actions; - void removeAll() - { -// for (auto && a : m_actions) -// m_toolBar->removeAction(a); - } -}; - -class WidgetToolbarManager { -public: - void addAction(QAction *action, QString section); - -private: - -}; - - -class LWidgetData { -public: - LWidgetData(PluginModule *module, PluginContentWidget *widget); - PluginModule* module() { return m_module; } - PluginContentWidget *widget() { return m_widget; } - void init(); - - void addToMenu(QMenuBar* menubar); - void removeFromMenu(QMenuBar* menubar); -private: - PluginModule *m_module; - PluginContentWidget *m_widget; - WidgetToolbarManager m_toolbarManager; - - std::vector m_menuActions; ///< List of actions we put in the menu -}; - -/// 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; - - void addContentWidget(PluginContentWidget *widget) override; - void removeContentWidget(PluginContentWidget *widget) override; - - void addWidgetActionsToToolbar(PluginContentWidget *widget, QToolBar *toolbar); - void removeWidgetActionsFromToolbar(PluginContentWidget *widget, QToolBar *toolbar); - - void addContextActionsToMenu(PluginContentWidget *widget, QMenuBar *menubar); - void removeContextActionsFromMenu(PluginContentWidget *widget, QMenuBar *menubar); -private: - - using WidgetLst = std::map; - - WidgetLst m_widgetLst; /// Keeps track of which widget belongs to which module -}; - -#endif // PLUGINCONTENTWIDGETCONTEXTBASE_H diff --git a/pglab/plugin_support/PluginModule.cpp b/pglab/plugin_support/PluginModule.cpp deleted file mode 100644 index 1074b25..0000000 --- a/pglab/plugin_support/PluginModule.cpp +++ /dev/null @@ -1,68 +0,0 @@ -#include "plugin_support/PluginModule.h" -#include "PluginContentWidget.h" -#include -#include -#include - -PluginModule::PluginModule(QString name, QString ident) - : m_name(std::move(name)) - , m_ident(std::move(ident)) -{ -} - -void PluginModule::setDisplayCategory(QString category) -{ - m_displayCategory = std::move(category); -} - -void PluginModule::registerStaticAction(StaticAction action) -{ - m_menuActions.emplace_back(std::move(action)); -} - -const PluginModule::StaticActionList& PluginModule::staticActions() const -{ - return m_menuActions; -} - -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; -} - -void PluginModule::registerContextAction(std::shared_ptr action) -{ - auto find_result_iter = m_contextMap.find(action->contextTypeIndex()); - if (find_result_iter != m_contextMap.end()) - find_result_iter->second.push_back(action); - else - m_contextMap.emplace(action->contextTypeIndex(), ContextActionContainer({ action })); -} - -const PluginModule::ContextActionContainer &PluginModule::actionsForContext(std::type_index ti) -{ - static const ContextActionContainer empty_result; - - auto find_result_iter = m_contextMap.find(ti); - if (find_result_iter != m_contextMap.end()) - return find_result_iter->second; - - return empty_result; -} - -const PluginModule::ContextActionContainer& PluginModule::actionsForContext(PluginContentWidget *widget) -{ - return actionsForContext(std::type_index(typeid(*widget))); -} - diff --git a/pglab/plugin_support/PluginModule.h b/pglab/plugin_support/PluginModule.h deleted file mode 100644 index f212fa5..0000000 --- a/pglab/plugin_support/PluginModule.h +++ /dev/null @@ -1,97 +0,0 @@ -#ifndef PLUGIN_SUPPORTPLUGINMODULE_H -#define PLUGIN_SUPPORTPLUGINMODULE_H - -#include "ModuleActionParameters.h" -#include "StaticAction.h" -#include "PluginRegister.h" -#include -#include -#include -#include -#include -#include -#include - -class QAction; -class IPluginContentWidgetContext; -class PluginContentWidget; - -/** Defines available actions for the application framework. - * - * There are static and context actions. - * There can be multiple contexts which are seperated by there typeid/type_index. - */ -class PluginModule: public QObject { - Q_OBJECT -public: - using StaticActionList = std::vector; - using ModuleAction = std::function; - using ModuleActionMap = std::map; - - using ContextActionContainer = std::vector>; - - PluginModule(QString name, QString ident); - - virtual void init() {} - - const QString& name() const { return m_name; } - const QString& identifier() const { return m_ident; } - const QString& displayCategory() const { return m_displayCategory; } - - void setDisplayCategory(QString category); - - /// registers an action that should always be accessible from the menu - void registerStaticAction(StaticAction action); - const StaticActionList& staticActions() const; - - /// "API" action that other modules can trigger by name without being linked to - /// this module. Allows for loose coupling. - 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; - - void registerContextAction(std::shared_ptr action); - - std::set contextTypeIndexes(); - const ContextActionContainer& actionsForContext(std::type_index ti); - const ContextActionContainer& actionsForContext(PluginContentWidget *widget); - -private: - using ContextMap = std::unordered_map; - - /// Name shown to end users - QString m_name; - /// Unique identifier - QString m_ident; - QString m_displayCategory; - - StaticActionList m_menuActions; - ModuleActionMap m_moduleActions; - ContextMap m_contextMap; -}; - - -template -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); -} - -#define REGISTER_PLUGIN_MODULE(module, name, ident) \ - namespace {\ - 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 diff --git a/pglab/plugin_support/PluginRegister.cpp b/pglab/plugin_support/PluginRegister.cpp deleted file mode 100644 index 8b1dd99..0000000 --- a/pglab/plugin_support/PluginRegister.cpp +++ /dev/null @@ -1,45 +0,0 @@ -#include "PluginRegister.h" -#include "plugin_support/PluginModule.h" -#include - - -PluginRegister* PluginRegister::s_pluginRegister; - -PluginRegister* PluginRegister::getInstance() -{ - static std::mutex m; - // check if set without locking first (in most cases it will be set and it will never be unset) so locking the mutex everytime - // is a waist of time. - if (!s_pluginRegister) { - // not set then lock - std::lock_guard guard(m); - // recheck in case someone else just set it - if (!s_pluginRegister) { - s_pluginRegister = new PluginRegister; - } - } - return s_pluginRegister; -} - -PluginRegister::PluginRegister() = default; - -void PluginRegister::initModules() -{ - for (auto && mod : m_moduleMap) { - mod.second->init(); - } -} - -void PluginRegister::registerModule(PluginModuleSPtr module) -{ - qDebug() << "registerModule " << 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(); -} diff --git a/pglab/plugin_support/PluginRegister.h b/pglab/plugin_support/PluginRegister.h deleted file mode 100644 index cc8449a..0000000 --- a/pglab/plugin_support/PluginRegister.h +++ /dev/null @@ -1,34 +0,0 @@ -#ifndef PLUGINREGISTER_H -#define PLUGINREGISTER_H - -#include -#include -#include -#include - -class QAction; -class PluginModule; - -class PluginRegister { -public: - using PluginModuleSPtr = std::shared_ptr; - using ModuleMap = std::map; - - static PluginRegister* getInstance(); - - PluginRegister(); - void registerModule(PluginModuleSPtr module); - - void initModules(); - const ModuleMap& modules() const{ return m_moduleMap; } - - const PluginModule* findModule(const QString &module_ident) const; -private: - - ModuleMap m_moduleMap; - - static PluginRegister* s_pluginRegister; -}; - - -#endif // PLUGINREGISTER_H diff --git a/pglab/plugin_support/StaticAction.cpp b/pglab/plugin_support/StaticAction.cpp deleted file mode 100644 index a537fb0..0000000 --- a/pglab/plugin_support/StaticAction.cpp +++ /dev/null @@ -1,75 +0,0 @@ -#include "StaticAction.h" - -BaseAction::BaseAction(const QString &text) - : m_text(text) -{} - -const QIcon& BaseAction::icon() const -{ - return m_icon; -} - -const MenuLocation& BaseAction::menuLocation() const -{ - return m_menuLocation; -} - -void BaseAction::setIcon(QIcon icon) -{ - m_icon = std::move(icon); -} - -void BaseAction::setMenuLocation(MenuLocation menu_location) -{ - m_menuLocation = std::move(menu_location); -} - -void BaseAction::setToolbarLocation(ToolbarLocation toolbar_location) -{ - m_toolbarLocation = toolbar_location; -} - -void BaseAction::setShortcut(QKeySequence shortcut) -{ - m_shortcut = std::move(shortcut); -} - -void BaseAction::setText(QString text) -{ - m_text = std::move(text); -} - -void BaseAction::setToolTip(QString tooltip) -{ - m_toolTip = std::move(tooltip); -} - -const QKeySequence& BaseAction::shortcut() const -{ - return m_shortcut; -} - -const QString& BaseAction::text() const -{ - return m_text; -} - -const QString& BaseAction::toolTip() const -{ - return m_toolTip; -} - -StaticAction::StaticAction(QString text, Func func) - : BaseAction(std::move(text)) - , m_func(std::move(func)) -{} - -void StaticAction::perform(IPluginContentWidgetContext *context) const -{ - if (m_func) - m_func(context); -} - - -ContextBaseAction::~ContextBaseAction() -{} diff --git a/pglab/plugin_support/StaticAction.h b/pglab/plugin_support/StaticAction.h deleted file mode 100644 index f342dbe..0000000 --- a/pglab/plugin_support/StaticAction.h +++ /dev/null @@ -1,122 +0,0 @@ -#ifndef MENUACTION_H -#define MENUACTION_H - -#include "MenuLocation.h" -#include "ToolbarLocation.h" -#include "plugin_support/PluginContentWidget.h" - -#include -#include -#include -#include -#include -#include - -class IPluginContentWidgetContext; - -class BaseAction { -public: - explicit BaseAction(const QString &text); - - const QIcon& icon() const; - const MenuLocation& menuLocation() const; - void setIcon(QIcon icon); - void setMenuLocation(MenuLocation menu_location); - void setToolbarLocation(ToolbarLocation toolbar_location); - void setShortcut(QKeySequence shortcut); - void setText(QString text); - void setToolTip(QString tooltip); - const QKeySequence& shortcut() const; - const QString& text() const; - const QString& toolTip() const; - -private: - QString m_text; - QString m_toolTip; - QIcon m_icon; - QKeySequence m_shortcut; - MenuLocation m_menuLocation; - ToolbarLocation m_toolbarLocation; - -}; -/** An action for in a menu or toolbar that does not pertain to a specific - * widget. It often will create a widget for instance a New or Open action. - * It does need a context. - * - */ -class StaticAction: public BaseAction { -public: - using Func = std::function; - - StaticAction(QString text, Func func); - - - void perform(IPluginContentWidgetContext *context) const; -private: - - Func m_func; -}; - - -class ContextBaseAction: public BaseAction { -public: - using BaseAction::BaseAction; - virtual ~ContextBaseAction(); - - virtual std::type_index contextTypeIndex() const = 0; - - virtual QAction* createAction(PluginContentWidget *widget) - { - auto action = new QAction(widget); - action->setText(text()); - setupConnectionForAction(action, widget); - return action; - } - - virtual void setupConnectionForAction(QAction *action, PluginContentWidget *context) = 0; -}; - -class QAction; - -/** Defines an action that can be performed within a certain context. - * For instance the save action for a query can only be called when a query is loaded. - * - * Note Func should be something that QAction::triggered can connect to. If not you - * could get quite a vague error. - */ -template -class ContextAction: public ContextBaseAction { -public: - //using Func = void (Context::*)(bool); - - ContextAction(QString text, Func func) - : ContextBaseAction(text) - , m_func(func) - {} - - std::type_index contextTypeIndex() const override - { - return std::type_index(typeid(Context)); - } - - // Mostly a helper for the code that creates the QAction - // without the helper that would need template code to. - virtual void setupConnectionForAction(QAction *action, PluginContentWidget *context) override - { - QObject::connect(action, &QAction::triggered, dynamic_cast(context), m_func); - } - -private: - Func m_func; -}; - -template -auto makeContextAction(QString text, Func func) -{ - return std::make_shared>(text, func); -} - - - - -#endif // MENUACTION_H diff --git a/pglab/plugin_support/ToolbarLocation.cpp b/pglab/plugin_support/ToolbarLocation.cpp deleted file mode 100644 index 61a57b3..0000000 --- a/pglab/plugin_support/ToolbarLocation.cpp +++ /dev/null @@ -1,14 +0,0 @@ -#include "ToolbarLocation.h" - -ToolbarLocation::ToolbarLocation() = default; - -ToolbarLocation::ToolbarLocation(QString toolbar, QString group, int position) - : m_toolbar(std::move(toolbar)) - , m_group(std::move(group)) - , m_position(position) -{} - -bool ToolbarLocation::isEmpty() const -{ - return m_toolbar.isEmpty(); -} diff --git a/pglab/plugin_support/ToolbarLocation.h b/pglab/plugin_support/ToolbarLocation.h deleted file mode 100644 index ce0cb3d..0000000 --- a/pglab/plugin_support/ToolbarLocation.h +++ /dev/null @@ -1,19 +0,0 @@ -#ifndef TOOLBARLOCATION_H -#define TOOLBARLOCATION_H - -#include - -class ToolbarLocation { -public: - ToolbarLocation(); - ToolbarLocation(QString toolbar, QString group, int position = -1); - - bool isEmpty() const; -private: - QString m_toolbar; - QString m_group; - int m_position = -1; -}; - - -#endif // TOOLBARLOCATION_H