cmake-plugin: 改进插件框架兼接口开发模式,更多注册动作,增加相应的框架内辅助接口

This commit is contained in:
zinface 2023-12-21 13:51:38 +08:00
parent 4fedd93d1a
commit f9f82c66ca
7 changed files with 133 additions and 45 deletions

View File

@ -1,4 +1,5 @@
#include "helloworld.h" #include "helloworld.h"
#include "pluginframeworkhelper.h"
#include "qdebug.h" #include "qdebug.h"
#include <QAction> #include <QAction>
@ -43,18 +44,8 @@ QString HelloWorld::PluginComment()
IPluginFramework::MenuType HelloWorld::PluginMenuType() IPluginFramework::MenuType HelloWorld::PluginMenuType()
{ {
return None; // return None;
// return SecondaryMenu; return SecondaryMenu;
}
void HelloWorld::registerNotepad(QWidget *notepad)
{
s_notepad = notepad;
}
void HelloWorld::registerCurrentEditCallback(std::function<QsciScintilla *(QWidget *)> get_cur_edit_callback)
{
s_get_cur_edit_callback = get_cur_edit_callback;
} }
void HelloWorld::PluginTrigger() void HelloWorld::PluginTrigger()
@ -67,10 +58,49 @@ void HelloWorld::PluginTrigger()
QMessageBox::information(nullptr, "提示信息", QString("您当前编辑器有 %1 个字符").arg(length)); QMessageBox::information(nullptr, "提示信息", QString("您当前编辑器有 %1 个字符").arg(length));
} }
void HelloWorld::registerNotepad(QWidget *notepad)
{
s_notepad = notepad;
}
void HelloWorld::registerStrFileName(QString str_file_name)
{
s_str_file_name = str_file_name;
}
void HelloWorld::registerCurrentEditCallback(std::function<QsciScintilla *(QWidget *)> get_cur_edit_callback)
{
s_get_cur_edit_callback = get_cur_edit_callback;
}
void HelloWorld::registerPluginCallBack(std::function<bool (QWidget *, int, void *)> plugin_callback)
{
s_plugin_callback = plugin_callback;
}
void HelloWorld::registerPluginActions(QMenu *rootMenu) void HelloWorld::registerPluginActions(QMenu *rootMenu)
{ {
QMessageBox::information(nullptr, "提示信息", "您的 MenuType 类型为 1 需要注册 QAction 集合"); QMessageBox::information(nullptr, "提示信息", "您的 MenuType 类型为 1 需要注册 QAction 集合");
rootMenu->addAction("第一个 QAction", [](){ rootMenu->addAction("第一个 QAction", [](){
QMessageBox::information(nullptr, "提示信息", "您已 trigger 触发插件的第一个 QAction"); QMessageBox::information(nullptr, "提示信息", "您已 trigger 触发插件的第一个 QAction");
}); });
rootMenu->addAction("创建一个编辑器", [this](){
QVariant editName = PluginFrameworkHelper::DoNewEdit(s_notepad, s_plugin_callback);
QMessageBox::information(nullptr, "提示信息", QString("您已 创建了一个编辑器: %1").arg(editName.toString()));
});
rootMenu->addAction("改变当前编辑器语法为 Json", [this](){
bool ret = PluginFrameworkHelper::DoChangeSyntax(s_notepad, s_plugin_callback, IPluginFramework::Json);
if (ret) {
QMessageBox::information(nullptr, "提示信息", QString("您已 改变了当前编辑器语法为 Json"));
} else {
QMessageBox::information(nullptr, "提示信息", QString("您未能成功改变当前编辑器语法为 Json"));
}
});
rootMenu->addAction("显示当前插件文件路径", [this](){
QMessageBox::information(nullptr, "提示信息", QString("您当前插件的路径为:\n%1").arg(s_str_file_name));
});
} }

View File

@ -23,27 +23,18 @@ public:
QString PluginComment(); QString PluginComment();
MenuType PluginMenuType(); MenuType PluginMenuType();
// IPluginFramework interface
public:
void registerNotepad(QWidget *notepad);
// IPluginFramework interface
public:
void registerCurrentEditCallback(std::function<QsciScintilla *(QWidget *)> get_cur_edit_callback);
// IPluginFramework interface
public:
void PluginTrigger(); void PluginTrigger();
void registerNotepad(QWidget *notepad);
// IPluginFramework interface void registerStrFileName(QString str_file_name) override;
public: void registerCurrentEditCallback(std::function<QsciScintilla *(QWidget *)> get_cur_edit_callback);
// void registerTrigger(QAction *action); void registerPluginCallBack(std::function<bool (QWidget *, int, void *)> plugin_callback) override;
void registerPluginActions(QMenu *rootMenu); void registerPluginActions(QMenu *rootMenu);
private: private:
QWidget *s_notepad; QWidget *s_notepad;
QString s_str_file_name;
std::function<QsciScintilla *(QWidget *)> s_get_cur_edit_callback; std::function<QsciScintilla *(QWidget *)> s_get_cur_edit_callback;
std::function<bool(QWidget*, int, void*)> s_plugin_callback;
}; };
#endif // HELLOWORLD_H #endif // HELLOWORLD_H

View File

@ -42,6 +42,7 @@ NDD_EXPORT bool NDD_PROC_IDENTIFY(NDD_PROC_DATA* pProcData) {
pProcData->m_version = plugin.PluginVersion(); pProcData->m_version = plugin.PluginVersion();
pProcData->m_auther = plugin.PluginAuthor(); pProcData->m_auther = plugin.PluginAuthor();
pProcData->m_menuType = plugin.PluginMenuType(); pProcData->m_menuType = plugin.PluginMenuType();
return true; return true;
} }
@ -49,10 +50,11 @@ NDD_EXPORT bool NDD_PROC_IDENTIFY(NDD_PROC_DATA* pProcData) {
NDD_EXPORT int NDD_PROC_MAIN(QWidget* pNotepad, const QString& strFileName, std::function<QsciScintilla* (QWidget*)>getCurEdit, std::function<bool(QWidget*, int, void*)> pluginCallBack, NDD_PROC_DATA* procData){ NDD_EXPORT int NDD_PROC_MAIN(QWidget* pNotepad, const QString& strFileName, std::function<QsciScintilla* (QWidget*)>getCurEdit, std::function<bool(QWidget*, int, void*)> pluginCallBack, NDD_PROC_DATA* procData){
plugin.registerNotepad(pNotepad); plugin.registerNotepad(pNotepad);
plugin.registerStrFileName(strFileName);
plugin.registerCurrentEditCallback(getCurEdit); plugin.registerCurrentEditCallback(getCurEdit);
plugin.registerPluginCallBack(pluginCallBack);
if (plugin.PluginMenuType() == IPluginFramework::None) { if (plugin.PluginMenuType() == IPluginFramework::None) {
// plugin.registerTrigger(procData->m_pAction); // 已使用 lambda 完成 PluginTrigger
procData->m_pAction->connect(procData->m_pAction, &QAction::triggered, [](){ procData->m_pAction->connect(procData->m_pAction, &QAction::triggered, [](){
plugin.PluginTrigger(); plugin.PluginTrigger();
}); });

View File

@ -13,7 +13,10 @@
class IPluginFramework class IPluginFramework
{ {
public: public:
// /**
* @brief
* @note None SecondaryMenu
*/
enum MenuType { enum MenuType {
None = 0, None = 0,
SecondaryMenu = 1, SecondaryMenu = 1,
@ -24,8 +27,17 @@ public:
* @note * @note
*/ */
enum Do { enum Do {
NewEdit, NewEdit = 1,
ChangeSyntax, ChangeSyntax = 2,
};
/**
* @brief
*/
enum SyntaxType {
Js = 0,
Json,
Html,
}; };
/********************************************* 插件基本信息/ /********************************************* 插件基本信息/
@ -59,8 +71,19 @@ public:
/********************************************* 插件的主程序 Notepad 注册*/ /********************************************* 插件的主程序 Notepad 注册*/
/**
* @brief registerNotepad
* @param notepad
*/
virtual void registerNotepad(QWidget *notepad) = 0; virtual void registerNotepad(QWidget *notepad) = 0;
/********************************************* 插件路径注册*/
/**
* @brief registerStrFileName
* @param str_file_name
*/
virtual void registerStrFileName(QString str_file_name) = 0;
/********************************************* 插件的一级(MenuType=None)或二级(MenuType=SecondaryMenu)菜单触发与注册接口/ /********************************************* 插件的一级(MenuType=None)或二级(MenuType=SecondaryMenu)菜单触发与注册接口/
/** /**
@ -69,15 +92,6 @@ public:
*/ */
virtual void PluginTrigger() = 0; virtual void PluginTrigger() = 0;
/**
* @brief ( MenuType None )
* @param action
* @note C 使 lambda PluginTrigger
*/
// virtual void registerTrigger(QAction *action) = 0;
/** /**
* @brief () * @brief ()
* @param menu * @param menu
@ -85,7 +99,7 @@ public:
*/ */
virtual void registerPluginActions(QMenu *rootMenu) = 0; virtual void registerPluginActions(QMenu *rootMenu) = 0;
/********************************************* 插件当前编辑器函数回调注册*/ /********************************************* 当前编辑器函数回调注册*/
/** /**
* @brief * @brief
@ -94,11 +108,24 @@ public:
*/ */
virtual void registerCurrentEditCallback(std::function<QsciScintilla*(QWidget*)> get_cur_edit_callback) = 0; virtual void registerCurrentEditCallback(std::function<QsciScintilla*(QWidget*)> get_cur_edit_callback) = 0;
//protected: /********************************************* 编辑器功能函数回调注册*/
/** /**
* @brief s_getCurEdit * @brief Notepad
* @param plugin_callBack
* @(Do::NewEdit, Do::)
*/ */
// std::function<QsciScintilla*(QWidget*)> s_getCurEdit; virtual void registerPluginCallBack(std::function<bool(QWidget*, int, void*)> plugin_callback) = 0;
protected:
/** s_notepad 为 CCNotepad当前主程序 */
/** s_strFileName 为当前路径 */
/** s_get_cur_edit_callback 为回调函数,用于获取当前编辑器 */
/** s_plugin_callBack 为回调函数,用于使当前主程序做某些事 */
// QWidget *s_notepad;
// QString s_str_file_name;
// std::function<QsciScintilla*(QWidget*)> s_get_cur_edit_callback
// std::function<bool(QWidget*, int, void*)> s_plugin_callback
}; };
Q_DECLARE_INTERFACE(IPluginFramework, IPluginFramework_IID) Q_DECLARE_INTERFACE(IPluginFramework, IPluginFramework_IID)

View File

@ -4,7 +4,11 @@
# framework # framework
# #
macro(add_framework_plugin _target) macro(add_framework_plugin _target)
option(${_target}_ENABLE "是否确认构建 ${_target} 插件" OFF)
if(${${_target}_ENABLE})
set(${_target}_ARGN ${ARGN}) set(${_target}_ARGN ${ARGN})
# set(${_target}_DIR_OR_SOURCES) # set(${_target}_DIR_OR_SOURCES)
foreach(arg IN LISTS ${_target}_ARGN) foreach(arg IN LISTS ${_target}_ARGN)
@ -60,4 +64,7 @@ macro(add_framework_plugin _target)
# QSCINTILLA_EXPORT Q_DECL_IMPORT # QSCINTILLA_EXPORT Q_DECL_IMPORT
) )
endif(WIN32 AND NOTEPAD_BUILD_BY_SHARED) endif(WIN32 AND NOTEPAD_BUILD_BY_SHARED)
endif(${${_target}_ENABLE})
endmacro(add_framework_plugin _target) endmacro(add_framework_plugin _target)

View File

@ -0,0 +1,14 @@
#include "pluginframeworkhelper.h"
QVariant PluginFrameworkHelper::DoNewEdit(QWidget *notepad, std::function<bool (QWidget *, int, void *)> plugin_callback)
{
QVariant var;
plugin_callback(notepad, IPluginFramework::NewEdit, (void*)&var);
return var;
}
bool PluginFrameworkHelper::DoChangeSyntax(QWidget *notepad, std::function<bool (QWidget *, int, void *)> plugin_callback, IPluginFramework::SyntaxType syntax)
{
int v = syntax;
return plugin_callback(notepad, IPluginFramework::ChangeSyntax, (void*)&v);
}

View File

@ -0,0 +1,17 @@
#ifndef PLUGINFRAMEWORKHELPER_H
#define PLUGINFRAMEWORKHELPER_H
#include "IPluginFramework.h"
#include <QObject>
#include <QWidget>
#include <qsciscintilla.h>
class PluginFrameworkHelper
{
public:
static QVariant DoNewEdit(QWidget *notepad, std::function<bool(QWidget*, int, void*)> plugin_callback);
static bool DoChangeSyntax(QWidget *notepad, std::function<bool(QWidget*, int, void*)> plugin_callback, IPluginFramework::SyntaxType syntax);
};
#endif // PLUGINFRAMEWORKHELPER_H