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.
This commit is contained in:
eelke 2019-01-05 19:58:23 +01:00
parent d0c4dabe8b
commit f4f2474a81
21 changed files with 418 additions and 204 deletions

View file

@ -1,7 +1,43 @@
#include "PluginContentWidgetContextBase.h"
#include "PluginRegister.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;
@ -25,3 +61,43 @@ void PluginContentWidgetContextBase::moduleAction(
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);
}