DPS/include/EventModule.h

126 lines
2.8 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#pragma once
#include <qwidget.h>
#include <qmenu.h>
#include "json.hpp"
#include "WebEngineView.h"
class WebEventDispatch;
class ShareEventModule;
using json = nlohmann::json;
/**
* @brief 消息处理模块
*/
class WEBQT_EXPORT EventModule :public QObject
{
Q_OBJECT
public:
/**
* @brief 消息处理模块
* @param name 消息处理模块对应的功能模块名称需与WEB端定义的功能模块对应。
*/
EventModule(std::string name);
virtual ~EventModule();
/**
* @brief 返回功能模块名称
* @return 功能模块名称
*/
const std::string getName();
/**
* @brief 发送方法消息到WEB端
* @param eventName 消息名称
* @param msg 消息参数
*/
void send(const std::string& eventName, const json& msg);
/**
* @brief 发送方法消息到WEB端
* @param eventName 消息名称
*/
void send(const std::string& eventName);
/**
* @brief 发送方法消息到WEB端
* @param eventName 消息名称
* @param msg 模板化的消息参数
*/
template<class T>
void send(const std::string& eventName, T msg) {
json j;
j["event"] = eventName;
j["data"] = msg;
sentToWeb(j);
}
/**
* @brief 广播方法消息到WEB端
* @param eventName 消息名称
* @param msg 消息参数
*/
void broadcast(const std::string& eventName, const json& msg);
/**
* @brief 广播方法消息到WEB端
* @param eventName 消息名称
*/
void broadcast(const std::string& eventName);
/**
* @brief 广播方法消息到WEB端
* @param eventName 消息名称
* @param msg 模板化的消息参数
*/
template<class T>
void broadcast(const std::string& eventName, T msg) {
json j;
j["event"] = eventName;
j["data"] = msg;
broadcastToWeb(j);
}
/**
* @brief 为本模块增加共享事件处理模块
* @param shareEventModule 共享事件处理模块
*/
void addShareEventModule(ShareEventModule* shareEventModule);
/**
* @brief 右键菜单消息处理回调函数
* @param request 参数信息
* @param menu 可供操作的菜单对象
*/
virtual void contextMenu(const json& request, QMenu* menu);
/**
* @brief 通用消息处理回调函数
* @param eventName 事件名称
* @param parameter 参数信息,包含输入参数和返回参数。
* @return 如果成功处理则返回true如果没有处理该消息则返回false
*/
virtual bool onMessage(const std::string& eventName, json& parameter);
private:
void sentToWeb(json& msg);
void broadcastToWeb(json& msg);
std::list<ShareEventModule*> _shareEventModule;
std::string _name;
WebEventDispatch* _parent;
friend class WebEventDispatch;
};
/**
* @brief 共享事件处理函数
*/
class WEBQT_EXPORT ShareEventModule :public EventModule {
Q_OBJECT
public:
ShareEventModule();
virtual ~ShareEventModule();
private:
void detach();
int _ref;
friend class EventModule;
};
/**
* @brief
*/
class WEBQT_EXPORT SignalEventModule :public EventModule {
Q_OBJECT
public:
SignalEventModule(std::string name);
virtual ~SignalEventModule();
signals:
bool onmessage(const std::string&, json&);
protected:
bool onMessage(const std::string& eventName, json& parameter);
};