cmake-plugin: 添加2个基于源代码构建的二级菜单支持系列插件模板

This commit is contained in:
zinface 2023-03-29 20:02:14 +08:00
parent 34b7f57739
commit 6249c13434
9 changed files with 326 additions and 0 deletions

View File

@ -11,6 +11,8 @@ if(USE_NOTEPAD_PLUGIN)
add_subdirectory(template-plugins/base-plugin)
add_subdirectory(template-plugins/base-widget-plugin)
add_subdirectory(template-plugins/base-widget-ui-plugin)
add_subdirectory(template-plugins/base-secondary-menu-plugin)
add_subdirectory(template-plugins/base-secondary-menu-plugin-v1)
endif(USE_NOTEPAD_PLUGIN_BASE)
# opencc 项目(基于 git)

View File

@ -0,0 +1,82 @@
# base-secondary-menu-plugin-v1/CMakeLists.txt
#
# 使
#
# base-secondary-menu-plugin-v1
# base-secondary-menu-plugin-v1 -> your plugin name
set(LOCAL_PLUGIN_NAME "base-secondary-menu-plugin-v1")
# base-secondary-menu-plugin-v1
# base-secondary-menu-plugin-v1
# 1. base-secondary-menu-plugin-v1
# 2. base-secondary-menu-plugin-v1
if(TRUE)
# base-secondary-menu-plugin-v1
spark_aux_source_paths(LocalSources
.
)
spark_add_library(${LOCAL_PLUGIN_NAME} SHARED ${LocalSources})
target_include_directories(${LOCAL_PLUGIN_NAME} PRIVATE
${PROJECT_SOURCE_DIR}/src
${PROJECT_SOURCE_DIR}/src/cceditor
${PROJECT_SOURCE_DIR}/src/qscint/src
${PROJECT_SOURCE_DIR}/src/qscint/src/Qsci
${PROJECT_SOURCE_DIR}/src/qscint/scintilla/src
${PROJECT_SOURCE_DIR}/src/qscint/scintilla/include
${PROJECT_SOURCE_DIR}/src/qscint/scintilla/lexlib
${PROJECT_SOURCE_DIR}/src/qscint/scintilla/boostregex
)
# target_link_libraries(${LOCAL_PLUGIN_NAME} QSci)
target_link_QSci(${LOCAL_PLUGIN_NAME})
if(USE_QT6)
# target_link_qt6_Core5Compat(${LOCAL_PLUGIN_NAME}) # : Qt6 使 Core5Compat Qt5
# target_link_qt6_PrintSupport(${LOCAL_PLUGIN_NAME})
# target_link_qt6_XmlPatterns(${LOCAL_PLUGIN_NAME}) # Bug
else()
# target_link_qt5_PrintSupport(${LOCAL_PLUGIN_NAME})
# target_link_qt5_XmlPatterns(${LOCAL_PLUGIN_NAME})
endif(USE_QT6)
# Notepad-- plugin
set_target_properties(${LOCAL_PLUGIN_NAME}
PROPERTIES
RUNTIME_OUTPUT_DIRECTORY $<TARGET_FILE_DIR:${PROJECT_NAME}>/plugin
LIBRARY_OUTPUT_DIRECTORY $<TARGET_FILE_DIR:${PROJECT_NAME}>/plugin
ARCHIVE_OUTPUT_DIRECTORY $<TARGET_FILE_DIR:${PROJECT_NAME}>/plugin)
# bin/plugin 每个位置或许都不一样(特别是 Linux)
# install(TARGETS ${LOCAL_PLUGIN_NAME} DESTINATION bin/plugin)
endif(TRUE)
# ----------------- base-secondary-menu-plugin-v1 ----------------- #
if(WIN32 AND NOTEPAD_BUILD_BY_SHARED)
# Windows QScintilla
target_compile_definitions(${LOCAL_PLUGIN_NAME}
PRIVATE
NOTEPAD_PLUGIN_MANAGER
QSCINTILLA_DLL # Windows 使 QSci Q_DECL_IMPORT
# QSCINTILLA_EXPORT Q_DECL_IMPORT
)
else()
# Windows QScintilla
target_compile_definitions(${LOCAL_PLUGIN_NAME}
PRIVATE
NOTEPAD_PLUGIN_MANAGER
# QSCINTILLA_DLL # Windows 使 QSci Q_DECL_IMPORT
# QSCINTILLA_EXPORT Q_DECL_IMPORT
)
endif(WIN32 AND NOTEPAD_BUILD_BY_SHARED)
if(UNIX)
# Unix/Linux
target_compile_definitions(${LOCAL_PLUGIN_NAME}
PRIVATE
NOTEPAD_PLUGIN_MANAGER
)
endif(UNIX)

View File

@ -0,0 +1,56 @@
#include "ndd_plugin_implement.h"
#include <qsciscintilla.h>
#include <QMenu>
#include <QDebug>
NddPluginImplement::NddPluginImplement(QWidget *parent, QsciScintilla *pEdit) : QWidget(parent)
, currentEdit(pEdit)
{
}
NddPluginImplement::~NddPluginImplement()
{
}
void NddPluginImplement::setMenuActions(QMenu *menu)
{
QAction *action1 = menu->addAction("第一项打印");
connect(action1, &QAction::triggered, this, [](){
// 普通的菜单项
qDebug() << "action1";
});
QAction *action2 = menu->addAction("第二项打印");
connect(action2, &QAction::triggered, this, [](){
// 普通的菜单项
qDebug() << "action2";
});
QAction *action3 = menu->addAction("打印默认编辑器内容(源代码项注意)");
connect(action3, &QAction::triggered, this, [=](){
// FAQ: 当第一次使用 currentEdit 时,使用时可能会引发错误。
// 此时 currentEdit 可能是空的。
if (currentEdit == nullptr) {
qDebug() << "Warring: currentEdit == nullptr!";
return;
}
qDebug() << currentEdit->text();
});
QAction *action4 = menu->addAction("设置当前编辑器为默认");
connect(action4, &QAction::triggered, this, [=](){
// 获取当前编辑器,并作为当前默认编辑器
currentEdit = getCurrentEditFunc();
qDebug() << "设置完成";
});
QAction *action5 = menu->addAction("打印当前编辑器内容");
connect(action5, &QAction::triggered, this, [=](){
// 获取当前编辑器,并直接打印编辑器内容
QsciScintilla *edit = getCurrentEditFunc();
qDebug() << edit->text();
});
}

View File

@ -0,0 +1,25 @@
#ifndef NDD_PLUGIN_IMPLEMENT_H
#define NDD_PLUGIN_IMPLEMENT_H
#include <QMainWindow>
class QsciScintilla;
class NddPluginImplement : public QWidget
{
Q_OBJECT
public:
explicit NddPluginImplement(QWidget *parent = nullptr, QsciScintilla *pEdit = nullptr);
~NddPluginImplement();
void setMenuActions(QMenu *menu);
void setCurrentEditFunc(std::function<QsciScintilla* ()> func) {
getCurrentEditFunc = func;
}
private:
QsciScintilla *currentEdit;
std::function<QsciScintilla* ()> getCurrentEditFunc;
};
#endif // NDD_PLUGIN_IMPLEMENT_H

View File

@ -0,0 +1,11 @@
#include <qobject.h>
#include <qstring.h>
#include <include/pluginGl.h>
#include <functional>
#include <qsciscintilla.h>
#include "ndd_plugin_implement.h"
NOTEPAD_PLUGIN_METADATA_IDENTIFY_V1("base-secondary-menu-plugin-v1", "0.1", "author", "二级菜单高级插件", "");
NOTEPAD_PLUGIN_METADATA_IMPLEMENT_V1(NddPluginImplement, false);

View File

@ -0,0 +1,82 @@
# base-secondary-menu-plugin/CMakeLists.txt
#
# 使
#
# base-secondary-menu-plugin
# base-secondary-menu-plugin -> your plugin name
set(LOCAL_PLUGIN_NAME "base-secondary-menu-plugin")
# base-secondary-menu-plugin
# base-secondary-menu-plugin
# 1. base-secondary-menu-plugin
# 2. base-secondary-menu-plugin
if(TRUE)
# base-secondary-menu-plugin
spark_aux_source_paths(LocalSources
.
)
spark_add_library(${LOCAL_PLUGIN_NAME} SHARED ${LocalSources})
target_include_directories(${LOCAL_PLUGIN_NAME} PRIVATE
${PROJECT_SOURCE_DIR}/src
${PROJECT_SOURCE_DIR}/src/cceditor
${PROJECT_SOURCE_DIR}/src/qscint/src
${PROJECT_SOURCE_DIR}/src/qscint/src/Qsci
${PROJECT_SOURCE_DIR}/src/qscint/scintilla/src
${PROJECT_SOURCE_DIR}/src/qscint/scintilla/include
${PROJECT_SOURCE_DIR}/src/qscint/scintilla/lexlib
${PROJECT_SOURCE_DIR}/src/qscint/scintilla/boostregex
)
# target_link_libraries(${LOCAL_PLUGIN_NAME} QSci)
target_link_QSci(${LOCAL_PLUGIN_NAME})
if(USE_QT6)
# target_link_qt6_Core5Compat(${LOCAL_PLUGIN_NAME}) # : Qt6 使 Core5Compat Qt5
# target_link_qt6_PrintSupport(${LOCAL_PLUGIN_NAME})
# target_link_qt6_XmlPatterns(${LOCAL_PLUGIN_NAME}) # Bug
else()
# target_link_qt5_PrintSupport(${LOCAL_PLUGIN_NAME})
# target_link_qt5_XmlPatterns(${LOCAL_PLUGIN_NAME})
endif(USE_QT6)
# Notepad-- plugin
set_target_properties(${LOCAL_PLUGIN_NAME}
PROPERTIES
RUNTIME_OUTPUT_DIRECTORY $<TARGET_FILE_DIR:${PROJECT_NAME}>/plugin
LIBRARY_OUTPUT_DIRECTORY $<TARGET_FILE_DIR:${PROJECT_NAME}>/plugin
ARCHIVE_OUTPUT_DIRECTORY $<TARGET_FILE_DIR:${PROJECT_NAME}>/plugin)
# bin/plugin 每个位置或许都不一样(特别是 Linux)
# install(TARGETS ${LOCAL_PLUGIN_NAME} DESTINATION bin/plugin)
endif(TRUE)
# ----------------- base-secondary-menu-plugin ----------------- #
if(WIN32 AND NOTEPAD_BUILD_BY_SHARED)
# Windows QScintilla
target_compile_definitions(${LOCAL_PLUGIN_NAME}
PRIVATE
NOTEPAD_PLUGIN_MANAGER
QSCINTILLA_DLL # Windows 使 QSci Q_DECL_IMPORT
# QSCINTILLA_EXPORT Q_DECL_IMPORT
)
else()
# Windows QScintilla
target_compile_definitions(${LOCAL_PLUGIN_NAME}
PRIVATE
NOTEPAD_PLUGIN_MANAGER
# QSCINTILLA_DLL # Windows 使 QSci Q_DECL_IMPORT
# QSCINTILLA_EXPORT Q_DECL_IMPORT
)
endif(WIN32 AND NOTEPAD_BUILD_BY_SHARED)
if(UNIX)
# Unix/Linux
target_compile_definitions(${LOCAL_PLUGIN_NAME}
PRIVATE
NOTEPAD_PLUGIN_MANAGER
)
endif(UNIX)

View File

@ -0,0 +1,6 @@
#include "ndd_plugin_implement.h"
NddPluginImplement::NddPluginImplement()
{
}

View File

@ -0,0 +1,13 @@
#ifndef NDD_PLUGIN_IMPLEMENT_H
#define NDD_PLUGIN_IMPLEMENT_H
#include <QMainWindow>
class NddPluginImplement
{
public:
NddPluginImplement();
};
#endif // NDD_PLUGIN_IMPLEMENT_H

View File

@ -0,0 +1,49 @@
#include <qobject.h>
#include <qstring.h>
#include <include/pluginGl.h>
#include <functional>
#include <qsciscintilla.h>
#include "ndd_plugin_implement.h"
#include <QDebug>
bool NDD_PROC_IDENTIFY(NDD_PROC_DATA* pProcData) {
// NOTEPAD_PLUGIN_METADATA
// (name, version, author, comment, filepath)
// 使用插件宏模板来完成简单的插件处理
NOTEPAD_PLUGIN_METADATA("base-secondary-menu-plugin", "0.1", "author", "基本二级菜单插件", "");
pProcData->m_menuType = 1;
return true;
}
static QWidget *s_pNotepad;
static NDD_PROC_DATA s_pProcData;
static std::function<QsciScintilla* ()> s_getCurEdit;
int NDD_PROC_MAIN(QWidget* pNotepad, const QString& strFileName, std::function<QsciScintilla* ()>getCurEdit, NDD_PROC_DATA* pProcData) {
// NOTEPAD_PLUGIN_IMPLEMENT
// (NddPluginImplement);
// 使用插件宏模板来完成基本的插件内容处理
// NddPluginImplement *imp = new NddPluginImplement();
if(pNotepad != nullptr) {
s_pNotepad = pNotepad;
} else return -1;
if(pProcData != nullptr) {
s_pProcData = *pProcData;
} else return -1;
if(getCurEdit != nullptr) {
s_getCurEdit = getCurEdit;
} else return -1;
QAction *action = s_pProcData.m_rootMenu->addAction("打印信息");
action->connect(action, &QAction::triggered, [](){
qDebug() << "打印信息";
});
// 插件已成功实现
return 0;
}