Needs work for correctly placing the items in menu and on toolbar. Old system still needs to be removed left in place to keep app useable.
117 lines
3 KiB
C++
117 lines
3 KiB
C++
#include "PluginContentWidgetContextBase.h"
|
|
#include "PluginContentWidget.h"
|
|
#include "PluginModule.h"
|
|
#include "PluginRegister.h"
|
|
#include <QAction>
|
|
#include <QDebug>
|
|
#include <QMenuBar>
|
|
#include <QToolBar>
|
|
#include <vector>
|
|
|
|
|
|
|
|
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);
|
|
}
|
|
|