This repository has been archived on 2024-07-22. You can view files and clone it, but cannot push or open issues or pull requests.
Doric/doric-Qt/example/doric/DoricContext.cpp

132 lines
3.7 KiB
C++
Raw Permalink Normal View History

2021-02-04 16:59:58 +08:00
#include "DoricContext.h"
#include "DoricContextManager.h"
#include "DoricNativeDriver.h"
2021-02-05 18:12:25 +08:00
2021-02-09 17:42:11 +08:00
#include "shader/DoricRootNode.h"
2021-02-04 16:59:58 +08:00
#include "utils/DoricConstant.h"
2021-02-05 18:12:25 +08:00
#include "utils/DoricContextHolder.h"
2021-02-04 16:59:58 +08:00
DoricContext::DoricContext(QString contextId, QString source, QString extra) {
this->mRootNode = new DoricRootNode();
2021-02-20 17:02:26 +08:00
this->mRootNode->setContext(this);
2021-02-04 16:59:58 +08:00
this->mContextId = contextId;
this->source = source;
this->extra = extra;
}
2021-04-09 16:39:43 +08:00
DoricContext::~DoricContext() {
QVariantList args;
callEntity(DoricConstant::DORIC_ENTITY_DESTROY, args);
DoricContextManager::getInstance()->destroyContext(this);
2021-06-08 10:08:38 +08:00
QList<QObject *> plugins = mPluginMap.values();
foreach (QObject *plugin, plugins) { delete plugin; }
2021-04-09 16:39:43 +08:00
}
2021-02-04 16:59:58 +08:00
DoricContext *DoricContext::create(QString script, QString source,
QString extra) {
DoricContext *context =
DoricContextManager::getInstance()->createContext(script, source, extra);
context->script = script;
context->init(extra);
QVariantList args;
context->callEntity(DoricConstant::DORIC_ENTITY_CREATE, args);
return context;
}
void DoricContext::init(QString initData) {
this->extra = initData;
if (!initData.isEmpty()) {
QVariantList args;
args.push_back(initData);
callEntity(DoricConstant::DORIC_ENTITY_INIT, args);
}
}
void DoricContext::build(int width, int height) {
QMap<QString, QVariant> map;
map.insert("width", QVariant(width));
map.insert("height", QVariant(height));
QVariant jsValue(map);
this->initParams = jsValue;
QVariantList args;
args.push_back(this->initParams);
callEntity(DoricConstant::DORIC_ENTITY_BUILD, args);
}
2021-05-12 11:08:40 +08:00
std::shared_ptr<DoricAsyncResult> DoricContext::callEntity(QString methodName,
QVariantList args) {
2021-02-04 16:59:58 +08:00
return getDriver()->invokeContextEntityMethod(this->mContextId, methodName,
args);
}
DoricInterfaceDriver *DoricContext::getDriver() {
if (driver == NULL) {
driver = DoricNativeDriver::getInstance();
return driver;
}
return driver;
}
2021-02-09 10:38:27 +08:00
DoricRootNode *DoricContext::getRootNode() { return mRootNode; }
2021-02-04 16:59:58 +08:00
QObject *DoricContext::obtainPlugin(QString name) {
if (mPluginMap.keys().contains(name)) {
return mPluginMap.value(name);
} else {
2021-02-20 16:20:18 +08:00
QObject *plugin = getDriver()->getRegistry()->plugins.createObject(name);
2021-02-05 18:12:25 +08:00
dynamic_cast<DoricContextHolder *>(plugin)->setContext(this);
2021-02-04 16:59:58 +08:00
mPluginMap.insert(name, plugin);
return plugin;
}
}
2021-02-23 14:28:08 +08:00
2021-06-09 16:46:33 +08:00
void DoricContext::setQmlEngine(QQmlApplicationEngine *engine) {
mQmlEngine = engine;
}
2021-02-23 14:28:08 +08:00
2021-06-09 16:46:33 +08:00
QQmlApplicationEngine *DoricContext::getQmlEngine() { return mQmlEngine; }
DoricViewNode *DoricContext::targetViewNode(QString id) {
if (id == mRootNode->getId()) {
return mRootNode;
}
return nullptr;
}
2021-04-09 16:39:43 +08:00
QString DoricContext::getContextId() { return mContextId; }
2021-04-22 20:46:00 +08:00
QList<DoricViewNode *> DoricContext::allHeadNodes(QString type) {
return mHeadNodes[type].values();
}
void DoricContext::addHeadNode(QString type, DoricViewNode *viewNode) {
if (mHeadNodes.contains(type)) {
QMap<QString, DoricViewNode *> map = mHeadNodes[type];
map.insert(viewNode->getId(), viewNode);
2021-04-22 21:03:03 +08:00
mHeadNodes.insert(type, map);
2021-04-22 20:46:00 +08:00
} else {
QMap<QString, DoricViewNode *> map;
map.insert(viewNode->getId(), viewNode);
mHeadNodes.insert(type, map);
}
}
void DoricContext::removeHeadNode(QString type, DoricViewNode *viewNode) {
if (mHeadNodes.contains(type)) {
QMap<QString, DoricViewNode *> map = mHeadNodes[type];
map.remove(viewNode->getId());
2021-04-22 21:03:03 +08:00
mHeadNodes.insert(type, map);
2021-04-22 20:46:00 +08:00
}
}
void DoricContext::clearHeadNodes(QString type) {
if (mHeadNodes.contains(type)) {
QMap<QString, DoricViewNode *> map = mHeadNodes[type];
map.clear();
2021-04-22 21:03:03 +08:00
mHeadNodes.insert(type, map);
2021-04-22 20:46:00 +08:00
}
}