cmake-plugin: 引入 NDD_DECLARE_PLUGIN 来重新定义插件入口的初始化,并提供更简单的 hello-simple 插件示例

This commit is contained in:
zinface 2024-01-08 20:01:51 +08:00
parent cd91e69268
commit 4c63813eeb
5 changed files with 210 additions and 4 deletions

View File

@ -29,6 +29,9 @@ if(USE_NOTEPAD_PLUGIN)
framework-plugins/helloworld
)
# framework-hello
add_framework_plugin(framework-hello-simple framework-plugins/hello-simple)
# plantuml
add_framework_plugin(framework-plantuml-preview
framework-plugins/plantuml-preview

View File

@ -0,0 +1,89 @@
#include "hello.h"
#include "pluginframeworkhelper.h"
#include <qmessagebox.h>
Hello::Hello(QObject *parent)
: QObject{parent}
{
}
Hello &Hello::instance()
{
static Hello hello;
return hello;
}
QString Hello::PluginName()
{
return "Hello";
}
QString Hello::PluginVersion()
{
return "0.1";
}
QString Hello::PluginAuthor()
{
return "zinface";
}
QString Hello::PluginComment()
{
return "hello 插件";
}
IPluginFramework::MenuType Hello::PluginMenuType()
{
return IPluginFramework::MenuType::SecondaryMenu;
}
void Hello::registerNotepad(QWidget *notepad)
{
s_notepad = notepad;
}
void Hello::registerStrFileName(QString str_file_name)
{
s_str_file_name = str_file_name;
}
void Hello::PluginTrigger()
{
QMessageBox::information(nullptr, "Tip", "This is Hello Plugin");
}
void Hello::registerPluginActions(QMenu *rootMenu)
{
rootMenu->addAction("First Plugin Action", this, [](){
QMessageBox::information(nullptr, "Tip", "This is Hello Plugin(First Action)");
});
rootMenu->addAction("Second Plugin Action", this, [](){
QMessageBox::information(nullptr, "Tip", "This is Hello Plugin(Second Action)");
});
rootMenu->addAction("Three Plugin Action", this, [this](){
// QsciScintilla *edit = s_get_cur_edit_callback(s_notepad);
// QMessageBox::information(nullptr, "Tip", edit->text());
QVariant editName = PluginFrameworkHelper::DoNewEdit(s_notepad, s_plugin_callback);
QMessageBox::information(nullptr, "New Edit", editName.toString());
});
}
void Hello::registerCurrentEditCallback(std::function<QsciScintilla *(QWidget *)> get_cur_edit_callback)
{
s_get_cur_edit_callback = get_cur_edit_callback;
}
void Hello::registerPluginCallBack(std::function<bool (QWidget *, int, void *)> plugin_callback)
{
s_plugin_callback = plugin_callback;
}
NDD_DECLARE_PLUGIN(Hello::instance())

View File

@ -0,0 +1,40 @@
#ifndef HELLO_H
#define HELLO_H
#include <IPluginFramework.h>
#include <QObject>
class Hello : public QObject, IPluginFramework
{
Q_OBJECT
public:
explicit Hello(QObject *parent = nullptr);
static Hello &instance();
signals:
// IPluginFramework interface
public:
QString PluginName() override;
QString PluginVersion() override;
QString PluginAuthor() override;
QString PluginComment() override;
MenuType PluginMenuType() override;
void registerNotepad(QWidget *notepad) override;
void registerStrFileName(QString str_file_name) override;
void PluginTrigger() override;
void registerPluginActions(QMenu *rootMenu) override;
void registerCurrentEditCallback(std::function<QsciScintilla *(QWidget *)> get_cur_edit_callback) override;
void registerPluginCallBack(std::function<bool (QWidget *, int, void *)> plugin_callback) override;
private:
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;
};
#endif // HELLO_H

View File

@ -116,18 +116,90 @@ public:
*/
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 为回调函数,用于使当前主程序做某些事 */
//private:
// 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
// std::function<QsciScintilla*(QWidget*)> s_get_cur_edit_callback;
// std::function<bool(QWidget*, int, void*)> s_plugin_callback;
};
Q_DECLARE_INTERFACE(IPluginFramework, IPluginFramework_IID)
#endif //!__IPLUGINFRAMEWORK__H__
#ifdef NOTEPAD_PLUGIN_MANAGER
#include "qdebug.h"
#include <pluginGl.h>
#include <functional>
#include <qsciscintilla.h>
#if defined(Q_OS_WIN)
#if defined(NDD_EXPORTDLL)
#define NDD_EXPORT __declspec(dllexport)
#else
#define NDD_EXPORT __declspec(dllimport)
#endif
#else
#define NDD_EXPORT __attribute__((visibility("default")))
#endif
#ifdef __cplusplus
extern "C" {
#endif
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);
#ifdef __cplusplus
}
#endif
#define NDD_DECLARE_PLUGIN(plugin) \
NDD_EXPORT bool NDD_PROC_IDENTIFY(NDD_PROC_DATA* pProcData) { \
qDebug() << (pProcData == NULL); \
if(pProcData == NULL) \
{ \
return false; \
} \
pProcData->m_strPlugName = plugin.PluginName(); \
pProcData->m_strComment = plugin.PluginComment(); \
pProcData->m_version = plugin.PluginVersion(); \
pProcData->m_auther = plugin.PluginAuthor(); \
pProcData->m_menuType = plugin.PluginMenuType(); \
\
return true; \
} \
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.registerStrFileName(strFileName); \
plugin.registerCurrentEditCallback(getCurEdit); \
plugin.registerPluginCallBack(pluginCallBack); \
\
if (plugin.PluginMenuType() == IPluginFramework::None) { \
procData->m_pAction->connect(procData->m_pAction, &QAction::triggered, [](){ \
plugin.PluginTrigger(); \
}); \
} else { \
plugin.registerPluginActions(procData->m_rootMenu); \
} \
\
qDebug() << strFileName; \
} \
#else
#error Missing definition NOTEPAD_PLUGIN_MANAGER
#endif //NOTEPAD_PLUGIN_MANAGER

View File

@ -53,6 +53,7 @@ if(${${_target}_ENABLE})
# Windows QScintilla
target_compile_definitions(${_target}
PRIVATE
NOTEPAD_PLUGIN_MANAGER
QSCINTILLA_DLL # Windows 使 QSci Q_DECL_IMPORT
# QSCINTILLA_EXPORT Q_DECL_IMPORT
)
@ -60,6 +61,7 @@ if(${${_target}_ENABLE})
# Windows QScintilla
target_compile_definitions(${_target}
PRIVATE
NOTEPAD_PLUGIN_MANAGER
# QSCINTILLA_DLL # Windows 使 QSci Q_DECL_IMPORT
# QSCINTILLA_EXPORT Q_DECL_IMPORT
)