DPS/include/EventModule.h

126 lines
3.1 KiB
C
Raw Normal View History

2025-06-23 10:41:33 +08:00
#pragma once
#include <qwidget.h>
#include <qmenu.h>
#include "json.hpp"
#include "WebEngineView.h"
class WebEventDispatch;
class ShareEventModule;
using json = nlohmann::json;
/**
2025-06-23 18:01:09 +08:00
* @brief
2025-06-23 10:41:33 +08:00
*/
class WEBQT_EXPORT EventModule :public QObject
{
Q_OBJECT
public:
/**
2025-06-23 18:01:09 +08:00
* @brief
* @param name WEB端定义的功能模块对应
2025-06-23 10:41:33 +08:00
*/
EventModule(std::string name);
virtual ~EventModule();
/**
2025-06-23 18:01:09 +08:00
* @brief
* @return
2025-06-23 10:41:33 +08:00
*/
const std::string getName();
/**
2025-06-23 18:01:09 +08:00
* @brief WEB端
* @param eventName
* @param msg
2025-06-23 10:41:33 +08:00
*/
void send(const std::string& eventName, const json& msg);
/**
2025-06-23 18:01:09 +08:00
* @brief WEB端
* @param eventName
2025-06-23 10:41:33 +08:00
*/
void send(const std::string& eventName);
/**
2025-06-23 18:01:09 +08:00
* @brief WEB端
* @param eventName
* @param msg
2025-06-23 10:41:33 +08:00
*/
template<class T>
void send(const std::string& eventName, T msg) {
json j;
j["event"] = eventName;
j["data"] = msg;
sentToWeb(j);
}
/**
2025-06-23 18:01:09 +08:00
* @brief 广WEB端
* @param eventName
* @param msg
2025-06-23 10:41:33 +08:00
*/
void broadcast(const std::string& eventName, const json& msg);
/**
2025-06-23 18:01:09 +08:00
* @brief 广WEB端
* @param eventName
2025-06-23 10:41:33 +08:00
*/
void broadcast(const std::string& eventName);
/**
2025-06-23 18:01:09 +08:00
* @brief 广WEB端
* @param eventName
* @param msg
2025-06-23 10:41:33 +08:00
*/
template<class T>
void broadcast(const std::string& eventName, T msg) {
json j;
j["event"] = eventName;
j["data"] = msg;
broadcastToWeb(j);
}
/**
2025-06-23 18:01:09 +08:00
* @brief
* @param shareEventModule
2025-06-23 10:41:33 +08:00
*/
void addShareEventModule(ShareEventModule* shareEventModule);
/**
2025-06-23 18:01:09 +08:00
* @brief
* @param request
* @param menu
2025-06-23 10:41:33 +08:00
*/
virtual void contextMenu(const json& request, QMenu* menu);
/**
2025-06-23 18:01:09 +08:00
* @brief
* @param eventName
* @param parameter
* @return truefalse
2025-06-23 10:41:33 +08:00
*/
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;
};
/**
2025-06-23 18:01:09 +08:00
* @brief
2025-06-23 10:41:33 +08:00
*/
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);
};