cmake-plugin: 添加接口化实现的插件示例(base-plugin-interface)

This commit is contained in:
zinface 2023-04-27 20:20:35 +08:00
parent db3e804780
commit 372378f5e4
14 changed files with 478 additions and 0 deletions

View File

@ -17,6 +17,8 @@ if(USE_NOTEPAD_PLUGIN)
add_subdirectory(template-plugins/base-plugin-widget)
add_subdirectory(template-plugins/base-plugin-window)
add_subdirectory(template-plugins/base-plugin-window-ui)
add_subdirectory(template-plugins/base-plugin-interface)
# add_subdirectory(template-plugins/base-secondary-menu-plugin)
# add_subdirectory(template-plugins/base-secondary-menu-plugin-v1)
# add_subdirectory(template-plugins/base-secondary-menu-ui-plugin-v1)

View File

@ -0,0 +1,115 @@
# base-plugin-interface/CMakeLists.txt
#
# 使
#
# base-plugin-interface
# base-plugin-interface -> your plugin name
set(LOCAL_PLUGIN_NAME "base-plugin-interface")
set(LOCAL_PLUGIN_VERSION "0.1")
configure_file(${CMAKE_SOURCE_DIR}/cmake/modules/config.h.in plugin-config.h @ONLY)
# base-plugin-interface
# base-plugin-interface
# 1. base-plugin-interface
# 2. base-plugin-interface
if(TRUE)
# base-plugin-interface
spark_file_glob(LocalSources
./*.h ./*.cpp ./*.ui
)
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-plugin-interface ----------------- #
target_compile_definitions(${LOCAL_PLUGIN_NAME}
#
PRIVATE
NOTEPAD_PLUGIN_DECLARE_PLUGIN_CONFIG)
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)
# ----------------- base-plugin-interface ----------------- #
macro(target_link_plugin_interface _target)
set(interfaces
interfaces/ndd-base
interfaces/ndd-menu
interfaces/ndd-dock)
spark_add_library_file_glob(plugin_interface STATIC
interfaces/ndd-base/*.h interfaces/ndd-base/*.cpp
interfaces/ndd-menu/*.h interfaces/ndd-menu/*.cpp
interfaces/ndd-dock/*.h interfaces/ndd-dock/*.cpp
)
target_include_directories(plugin_interface
PUBLIC
${interfaces})
target_link_qt5(plugin_interface)
target_link_libraries(${_target}
plugin_interface)
endmacro(target_link_plugin_interface _target)
target_link_plugin_interface(${LOCAL_PLUGIN_NAME})

View File

@ -0,0 +1,37 @@
# base-plugin-interface 设计
- 主体结构预览
```
.
├── CMakeLists.txt
├── interfaces
│ ├── ndd-base
│ │ ├── ndd_plugin_base_interface.cpp
│ │ └── ndd_plugin_base_interface.h
│ ├── ndd-dock
│ │ ├── ndd_plugin_dock_interface.cpp
│ │ └── ndd_plugin_dock_interface.h
│ └── ndd-menu
│ ├── ndd_plugin_menu_interface.cpp
│ └── ndd_plugin_menu_interface.h
├── ndd_plugin_implement.cpp
├── ndd_plugin_implement.h
├── ndd_plugin_interface.cpp
├── ndd_plugin_interface.h
├── plugin.cpp
└── README.md
4 directories, 13 files
```
- 接口化与继承实现说明
```c
// 前提:一切基于接口化操作
// 如果插件实现需要拥有某种特定功能,即可继承一个类接口
// 如果插件继承了一个包含虚函数的接口,继承者应该去实现它
```

View File

@ -0,0 +1,58 @@
#include "ndd_plugin_base_interface.h"
BaseNddPluginInterface::BaseNddPluginInterface(QObject *parent) : QObject(parent)
{
}
BaseNddPluginInterface::BaseNddPluginInterface(QString pluginFile, QString pluginName, QString pluginVersion, QObject *parent) : QObject(parent)
, m_pluginFile(pluginFile)
, m_pluginName(pluginName)
, m_pluginVersion(pluginVersion)
{
m_pluginDir = QFileInfo(m_pluginFile).dir().path();
m_pluginResourceDir = QFileInfo(m_pluginFile).dir().filePath(m_pluginName);
}
QString BaseNddPluginInterface::pluginFile() const
{
return m_pluginFile;
}
void BaseNddPluginInterface::setPluginFile(const QString &newPluginFile)
{
m_pluginFile = newPluginFile;
}
QString BaseNddPluginInterface::pluginName() const
{
return m_pluginName;
}
void BaseNddPluginInterface::setPluginName(const QString &newPluginName)
{
m_pluginName = newPluginName;
}
QString BaseNddPluginInterface::pluginVersion() const
{
return m_pluginVersion;
}
void BaseNddPluginInterface::setPluginVersion(const QString &newPluginVersion)
{
m_pluginVersion = newPluginVersion;
}
QString BaseNddPluginInterface::pluginDir() const
{
return QFileInfo(m_pluginFile).dir().path();
// return m_pluginDir;
}
QString BaseNddPluginInterface::pluginResourceDir() const
{
return QFileInfo(m_pluginFile).dir().filePath(m_pluginName);
// return m_pluginResourceDir;
}

View File

@ -0,0 +1,46 @@
#ifndef __NDD_PLUGIN_BASE_INTERFACE__H__
#define __NDD_PLUGIN_BASE_INTERFACE__H__
#include <QDir>
#include <QFileInfo>
class BaseNddPluginInterface : public QObject {
Q_OBJECT
Q_PROPERTY(QString pluginFile READ pluginFile WRITE setPluginFile);
Q_PROPERTY(QString pluginName READ pluginName WRITE setPluginName);
Q_PROPERTY(QString pluginVersion READ pluginVersion WRITE setPluginVersion);
// Q_PROPERTY(QString pluginDir READ pluginDir);
// Q_PROPERTY(QString pluginResourceDir READ pluginResourceDir);
public:
explicit BaseNddPluginInterface(QObject *parent = nullptr);
explicit BaseNddPluginInterface(QString pluginFile, QString pluginName, QString pluginVersion, QObject *parent = nullptr);
QString pluginFile() const;
void setPluginFile(const QString &newPluginFile);
QString pluginName() const;
void setPluginName(const QString &newPluginName);
QString pluginVersion() const;
void setPluginVersion(const QString &newPluginVersion);
QString pluginDir() const;
QString pluginResourceDir() const;
private:
QString m_pluginFile;
QString m_pluginName;
QString m_pluginVersion;
QString m_pluginDir;
QString m_pluginResourceDir;
};
#define USE_INTERFACE_IMPLEMENT(interface) \
interface->setPluginFile(s_strFileName); \
interface->setPluginName(PLUGIN_NAME); \
interface->setPluginVersion(PLUGIN_VERSION); \
#endif //!__NDD_PLUGIN_BASE_INTERFACE__H__

View File

@ -0,0 +1,22 @@
#include "ndd_plugin_dock_interface.h"
DockNddPluginInterface::DockNddPluginInterface(QObject *parent) : QObject(parent)
{
}
int DockNddPluginInterface::showInDock(Qt::DockWidgetArea area) {
// m_widget 是
auto mainwindow = dynamic_cast<QMainWindow*>(m_widget);
if (!mainwindow) {
return -1;
}
if (!m_dockerContainer) {
m_dockerContainer = new QDockWidget(mainwindow);
auto m_dockTitleBarWidget = m_dockerContainer->titleBarWidget();
}
return 0;
}

View File

@ -0,0 +1,25 @@
#ifndef __NDD_PLUGIN_DOCK_INTERFACE__H__
#define __NDD_PLUGIN_DOCK_INTERFACE__H__
#include <QMainWindow>
#include <QDockWidget>
class DockNddPluginInterface : public QObject{
public:
explicit DockNddPluginInterface(QObject *parent = nullptr);
/**
* @brief DockWidget
* @note
* @param area: DockWidget
* @retval
*/
int showInDock(Qt::DockWidgetArea area);
QWidget *m_widget;
QDockWidget *m_dockerContainer;
};
#endif //!__NDD_PLUGIN_DOCK_INTERFACE__H__

View File

@ -0,0 +1,16 @@
#include "ndd_plugin_menu_interface.h"
MenuNddPluginInterface::MenuNddPluginInterface(QObject *parent) : QObject(parent) {
}
QMenu *MenuNddPluginInterface::menu() const
{
return m_menu;
}
void MenuNddPluginInterface::setMenu(QMenu *newMenu)
{
m_menu = newMenu;
setMenuAction();
}

View File

@ -0,0 +1,26 @@
#ifndef __NDD_PLUGIN_MENU_INTERFACE__H__
#define __NDD_PLUGIN_MENU_INTERFACE__H__
#include <QMenu>
class MenuNddPluginInterface : public QObject {
Q_OBJECT
Q_PROPERTY(QMenu* menu READ menu WRITE setMenu);
public:
explicit MenuNddPluginInterface(QObject *parent = nullptr);
QMenu *menu() const;
void setMenu(QMenu *newMenu);
private:
QMenu *m_menu = nullptr;
protected:
virtual void setMenuAction() = 0;
};
#define USE_MENU_IMPLEMENT(menu) \
menu->setMenu(s_pProcData.m_rootMenu);
#endif //!__NDD_PLUGIN_MENU_INTERFACE__H__

View File

@ -0,0 +1,22 @@
#include "ndd_plugin_implement.h"
#include "qdebug.h"
NddPluginImplement::NddPluginImplement(QObject *parent) : BaseNddPluginInterface(parent), MenuNddPluginInterface(parent)
{
}
void NddPluginImplement::setMenuAction()
{
menu()->addAction("First Menu", [](){
qDebug() << ">>> First Menu";
});
menu()->addAction("plugin infomations", [=](){
qInfo() << "[pluginDir]:" << pluginDir();
qInfo() << "[pluginFile]:" << pluginFile();
qInfo() << "[pluginVersion]:" << pluginVersion();
qInfo() << "[pluginResourceDir]:" << pluginResourceDir();
});
}

View File

@ -0,0 +1,20 @@
#ifndef NDD_PLUGIN_IMPLEMENT_H
#define NDD_PLUGIN_IMPLEMENT_H
#include "ndd_plugin_base_interface.h"
#include "ndd_plugin_menu_interface.h"
#include "ndd_plugin_dock_interface.h"
#include <QMainWindow>
class NddPluginImplement : public BaseNddPluginInterface, public MenuNddPluginInterface
{
public:
explicit NddPluginImplement(QObject *parent = nullptr);
// MenuNddPluginInterface interface
protected:
void setMenuAction() override;
};
#endif // NDD_PLUGIN_IMPLEMENT_H

View File

@ -0,0 +1,8 @@
#include "ndd_plugin_interface.h"
// Temporary interface design storage point ...
NddPluginInterface::NddPluginInterface(QObject *parent) : BaseNddPluginInterface(parent)
{
}

View File

@ -0,0 +1,29 @@
#ifndef __NDD_PLUGIN_INTERFACE__H__
#define __NDD_PLUGIN_INTERFACE__H__
// #include <QWidget>
// #include <QDockWidget>
// #include <QMainWindow>
// #include <QFileInfo>
// #include <QDir>
// #include <qsciscintilla.h>
// #include <qstackedwidget.h>
// #include <qstatusbar.h>
// #include <qtoolbar.h>
// #include <QLabel>
// #include <QDebug>
// #include <QMenu>
#include "ndd_plugin_base_interface.h"
class NddPluginInterface : BaseNddPluginInterface {
public:
explicit NddPluginInterface(QObject *parent = nullptr);
private:
};
#endif //!__NDD_PLUGIN_INTERFACE__H__

View File

@ -0,0 +1,52 @@
#include <qobject.h>
#include <qstring.h>
#include <include/pluginGl.h>
#include <functional>
#include <qsciscintilla.h>
#include "ndd_plugin_implement.h"
#include "ndd_plugin_interface.h"
#include <QDebug>
/**************************************************************
// 当编译标志 NOTEPAD_PLUGIN_DECLARE_PLUGIN_CONFIG 被定义
// 将会自动 #include <plugin-config.h>,并提供几个插件相关的宏
// #define NOTEPAD_VERSION "v1.23.2"
// #define PLUGIN_NAME "base-plugin"
// #define PLUGIN_VERSION "0.1"
**************************************************************/
// 使用可扩展宏 IDENTIFY_
NOTEPAD_PLUGIN_METADATA_IDENTIFY_1 {
USE_IDENTIFY_VARIABLES
NOTEPAD_PLUGIN_METADATA("基本插件(interface)", PLUGIN_VERSION, "author", "基本 接口化与多继承实现 的插件", "");
return true;
}
// 使用可扩展宏 IMPLEMENT_ ,在 _1 中将进行二级菜单扩展的全局声明
NOTEPAD_PLUGIN_METADATA_IMPLEMENT_1 {
USE_IMPLEMENT_VARIABLES
qInfo() << PLUGIN_NAME << ":" << PLUGIN_VERSION;
qInfo() << PLUGIN_NAME << ":" << QString("基于 %1 编译").arg(NOTEPAD_VERSION);
// auto base = new BaseNddPluginInterface();
// USE_INTERFACE_IMPLEMENT(base)
// qInfo() << "[pluginDir]:" << base->pluginDir();
// qInfo() << "[pluginFile]:" << base->pluginFile();
// qInfo() << "[pluginVersion]:" << base->pluginVersion();
// qInfo() << "[pluginResourceDir]:" << base->pluginResourceDir();
auto imp = new NddPluginImplement();
USE_INTERFACE_IMPLEMENT(imp);
USE_MENU_IMPLEMENT(imp);
return 0;
}