pgLab/pglab/plugin_support/PluginContentWidgetContextBase.cpp
eelke f4f2474a81 Moved definition of widget instance actions to the module so other parts of the system can no about them.
The plugin system will create the Action objects and bind them to the specified slots of the
specific widget instances.
2019-01-05 19:58:23 +01:00

103 lines
2.7 KiB
C++

#include "PluginContentWidgetContextBase.h"
#include "PluginContentWidget.h"
#include "PluginModule.h"
#include "PluginRegister.h"
#include "LWidgetAction.h"
#include <QAction>
#include <QDebug>
#include <QToolBar>
#include <vector>
LWidgetData::LWidgetData(PluginModule *module)
: m_module(module)
{}
void LWidgetData::init(PluginContentWidget *widget)
{
auto&& widget_actions = m_module->widgetActions();
m_widgetActions.reserve(widget_actions.size());
for (auto&& wa : widget_actions) {
m_widgetActions.push_back(createAction(wa, widget));
}
}
QList<QAction *> LWidgetData::actions()
{
return m_widgetActions;
}
QAction *LWidgetData::createAction(const LWidgetAction &wa, PluginContentWidget *widget)
{
auto ac = new QAction(wa.icon(), wa.text(), widget);
ac->setShortcut(wa.shortcut());
ac->setToolTip(wa.toolTip());
QObject::connect(ac, SIGNAL(triggered()), widget, wa.slotName());
return ac;
}
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(PluginModule *module, PluginContentWidget *widget)
{
auto res = m_widgetLst.emplace(widget, module);
if (!res.second)
throw std::runtime_error("Unexpected conflicting key on insertiong PluginContentWidgetContextBase::addContentWidget");
res.first->second.init(widget);
}
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 res = m_widgetLst.find(widget);
if (res == m_widgetLst.end())
return;
auto && actions = res->second.actions();
toolbar->addActions(actions);
}
void PluginContentWidgetContextBase::removeWidgetActionsFromToolbar(PluginContentWidget *widget, QToolBar *toolbar)
{
auto res = m_widgetLst.find(widget);
if (res == m_widgetLst.end())
return;
auto && actions = res->second.actions();
for (auto && ac : actions)
toolbar->removeAction(ac);
}