h & cpp split

This commit is contained in:
王劲鹏 2019-12-14 10:04:17 +08:00
parent 4610e71f42
commit ed2a1326fc
17 changed files with 210 additions and 159 deletions

39
doric/context.cpp Normal file
View File

@ -0,0 +1,39 @@
#include <QJsonObject>
#include <QJsonDocument>
#include "constant.h"
#include "context.h"
#include "driver/native_driver.h"
Context::Context(int contextId, QString *source) {
this->driver = NativeDriver::getInstance();
this->contextId = contextId;
this->source = source;
}
void Context::show() {
QString *method = new QString(Constant::DORIC_ENTITY_SHOW);
QVector<QString*> *arguments = new QVector<QString*>();
driver->invokeContextEntityMethod(contextId, method, nullptr);
delete arguments;
delete method;
}
void Context::init(double width, double height) {
QJsonObject *jsonObject = new QJsonObject();
jsonObject->insert("width", width);
jsonObject->insert("height", height);
QString *method = new QString(Constant::DORIC_ENTITY_INIT);
QVariant *variant = new QVariant();
variant->setValue(*jsonObject);
driver->invokeContextEntityMethod(contextId, method, variant, nullptr);
delete variant;
delete method;
delete jsonObject;
}

View File

@ -2,12 +2,8 @@
#define CONTEXT_H #define CONTEXT_H
#include <QString> #include <QString>
#include <QJsonObject>
#include <QJsonDocument>
#include "constant.h"
#include "driver/driver.h" #include "driver/driver.h"
#include "driver/native_driver.h"
class Context class Context
{ {
@ -17,38 +13,13 @@ private:
QString *source; QString *source;
public: public:
Driver *driver = NativeDriver::getInstance(); Driver *driver;
Context(int contextId, QString *source) { Context(int contextId, QString *source);
this->contextId = contextId;
this->source = source;
}
void show() { void show();
QString *method = new QString(Constant::DORIC_ENTITY_SHOW);
QVector<QString*> *arguments = new QVector<QString*>();
driver->invokeContextEntityMethod(contextId, method, nullptr); void init(double width, double height);
delete arguments;
delete method;
}
void init(double width, double height) {
QJsonObject *jsonObject = new QJsonObject();
jsonObject->insert("width", width);
jsonObject->insert("height", height);
QString *method = new QString(Constant::DORIC_ENTITY_INIT);
QVariant *variant = new QVariant();
variant->setValue(*jsonObject);
driver->invokeContextEntityMethod(contextId, method, variant, nullptr);
delete variant;
delete method;
delete jsonObject;
}
}; };
#endif // CONTEXT_H #endif // CONTEXT_H

View File

@ -15,9 +15,15 @@ DEFINES += QT_DEPRECATED_WARNINGS
SOURCES += \ SOURCES += \
constant.cpp \ constant.cpp \
context.cpp \
driver/native_driver.cpp \ driver/native_driver.cpp \
engine/js_engine.cpp \
main.cpp \ main.cpp \
native/native_bridge.cpp native/native_bridge.cpp \
native/native_empty.cpp \
native/native_log.cpp \
native/native_timer.cpp \
registry.cpp
RESOURCES += qml.qrc RESOURCES += qml.qrc

View File

@ -3,6 +3,10 @@
#include "native_driver.h" #include "native_driver.h"
#include "utility/utility.h" #include "utility/utility.h"
NativeDriver::NativeDriver() {
qDebug() << "NativeDriver constructor";
}
NativeDriver::~NativeDriver() { NativeDriver::~NativeDriver() {
qDebug() << "NativeDriver destructor"; qDebug() << "NativeDriver destructor";
} }

View File

@ -11,9 +11,7 @@ class NativeDriver : public Driver {
private: private:
static NativeDriver *local_instance; static NativeDriver *local_instance;
NativeDriver() { NativeDriver();
qDebug() << "NativeDriver constructor";
}
~NativeDriver() override; ~NativeDriver() override;

View File

@ -0,0 +1,80 @@
#include <QFile>
#include <QResource>
#include "constant.h"
#include "js_engine.h"
JSEngine::JSEngine() {
initJSEngine();
injectGlobal();
initDoricRuntime();
}
void JSEngine::prepareContext(int contextId, QString *script) {
QString contextIdString = QString::number(contextId);
QString source = QString(Constant::TEMPLATE_CONTEXT_CREATE)
.replace("%s1", *script)
.replace("%s2", contextIdString)
.replace("%s3", contextIdString)
.replace("%s4", contextIdString);
QJSValue result = engine->evaluate(source, "context://" + contextIdString);
qDebug() << "context://" + contextIdString + " result: " + result.toString();
}
void JSEngine::destroyContext(int contextId) {
QString contextIdString = QString::number(contextId);
QString source = QString(Constant::TEMPLATE_CONTEXT_DESTROY)
.replace("%s", contextIdString);
QJSValue result = engine->evaluate(source, "_context://" + contextIdString);
qDebug() << "context://" + contextIdString + " result: " + result.toString();
}
void JSEngine::initJSEngine() {
engine->installExtensions(QJSEngine::AllExtensions);
}
void JSEngine::injectGlobal() {
QJSValue log = engine->newQObject(nativeLog);
engine->globalObject().setProperty(Constant::INJECT_LOG, log.property("function"));
QJSValue timer = engine->newQObject(nativeTimer);
engine->globalObject().setProperty(Constant::INJECT_TIMER_SET, timer.property("setTimer"));
engine->globalObject().setProperty(Constant::INJECT_TIMER_CLEAR, timer.property("clearTimer"));
QJSValue empty = engine->newQObject(nativeEmpty);
engine->globalObject().setProperty(Constant::INJECT_EMPTY, empty.property("function"));
QJSValue bridge = engine->newQObject(nativeBridge);
engine->globalObject().setProperty(Constant::INJECT_BRIDGE, bridge.property("function"));
}
void JSEngine::initDoricRuntime() {
{
QResource resource(":/doric/doric-sandbox.js");
QFile *file = new QFile(resource.fileName());
file->open(QFile::ReadOnly | QFile::Text);
QTextStream in(file);
QString script = in.readAll();
file->close();
delete file;
QJSValue result = engine->evaluate(script, "doric-sandbox.js");
qDebug() << "doric-sandbox.js result: " + result.toString();
}
{
QResource resource(":/doric/doric-lib.js");
QFile *file = new QFile(resource.fileName());
file->open(QFile::ReadOnly | QFile::Text);
QTextStream in(file);
QString script = in.readAll();
file->close();
delete file;
QString lib = QString(Constant::TEMPLATE_MODULE)
.replace("%s1", "doric")
.replace("%s2", script);
QJSValue result = engine->evaluate(lib, "doric-lib.js");
qDebug() << "doric-lib.js result: " + result.toString();
}
}

View File

@ -1,47 +1,23 @@
#ifndef JS_ENGINE_H #ifndef JS_ENGINE_H
#define JS_ENGINE_H #define JS_ENGINE_H
#include <QFile>
#include <QJSEngine> #include <QJSEngine>
#include <QObject>
#include <QResource>
#include "constant.h"
#include "native/native_bridge.h" #include "native/native_bridge.h"
#include "native/native_empty.h" #include "native/native_empty.h"
#include "native/native_log.h" #include "native/native_log.h"
#include "native/native_timer.h" #include "native/native_timer.h"
class JSEngine : public QObject { class JSEngine {
Q_OBJECT
public: public:
QJSEngine *engine = new QJSEngine(); QJSEngine *engine = new QJSEngine();
JSEngine(QObject *parent = nullptr) : QObject(parent) { JSEngine();
initJSEngine();
injectGlobal();
initDoricRuntime();
}
void prepareContext(int contextId, QString *script) { void prepareContext(int contextId, QString *script);
QString contextIdString = QString::number(contextId);
QString source = QString(Constant::TEMPLATE_CONTEXT_CREATE)
.replace("%s1", *script)
.replace("%s2", contextIdString)
.replace("%s3", contextIdString)
.replace("%s4", contextIdString);
QJSValue result = engine->evaluate(source, "context://" + contextIdString);
qDebug() << "context://" + contextIdString + " result: " + result.toString();
}
void destroyContext(int contextId) { void destroyContext(int contextId);
QString contextIdString = QString::number(contextId);
QString source = QString(Constant::TEMPLATE_CONTEXT_DESTROY)
.replace("%s", contextIdString);
QJSValue result = engine->evaluate(source, "_context://" + contextIdString);
qDebug() << "context://" + contextIdString + " result: " + result.toString();
}
private: private:
NativeLog *nativeLog = new NativeLog(); NativeLog *nativeLog = new NativeLog();
@ -49,55 +25,11 @@ private:
NativeEmpty *nativeEmpty = new NativeEmpty(); NativeEmpty *nativeEmpty = new NativeEmpty();
NativeBridge *nativeBridge = new NativeBridge(); NativeBridge *nativeBridge = new NativeBridge();
void initJSEngine() { void initJSEngine();
engine->installExtensions(QJSEngine::AllExtensions);
}
void injectGlobal() { void injectGlobal();
QJSValue log = engine->newQObject(nativeLog);
engine->globalObject().setProperty(Constant::INJECT_LOG, log.property("function"));
QJSValue timer = engine->newQObject(nativeTimer); void initDoricRuntime();
engine->globalObject().setProperty(Constant::INJECT_TIMER_SET, timer.property("setTimer"));
engine->globalObject().setProperty(Constant::INJECT_TIMER_CLEAR, timer.property("clearTimer"));
QJSValue empty = engine->newQObject(nativeEmpty);
engine->globalObject().setProperty(Constant::INJECT_EMPTY, empty.property("function"));
QJSValue bridge = engine->newQObject(nativeBridge);
engine->globalObject().setProperty(Constant::INJECT_BRIDGE, bridge.property("function"));
}
void initDoricRuntime() {
{
QResource resource(":/doric/doric-sandbox.js");
QFile *file = new QFile(resource.fileName());
file->open(QFile::ReadOnly | QFile::Text);
QTextStream in(file);
QString script = in.readAll();
file->close();
delete file;
QJSValue result = engine->evaluate(script, "doric-sandbox.js");
qDebug() << "doric-sandbox.js result: " + result.toString();
}
{
QResource resource(":/doric/doric-lib.js");
QFile *file = new QFile(resource.fileName());
file->open(QFile::ReadOnly | QFile::Text);
QTextStream in(file);
QString script = in.readAll();
file->close();
delete file;
QString lib = QString(Constant::TEMPLATE_MODULE)
.replace("%s1", "doric")
.replace("%s2", script);
QJSValue result = engine->evaluate(lib, "doric-lib.js");
qDebug() << "doric-lib.js result: " + result.toString();
}
}
}; };
#endif // JS_ENGINE_H #endif // JS_ENGINE_H

View File

@ -1,6 +1,7 @@
#include <QFile> #include <QFile>
#include <QGuiApplication> #include <QGuiApplication>
#include <QQmlApplicationEngine> #include <QQmlApplicationEngine>
#include <QResource>
#include <QTextStream> #include <QTextStream>
#include "context_manager.h" #include "context_manager.h"

View File

@ -1,7 +1,6 @@
#ifndef NATIVE_BRIDGE_H #ifndef NATIVE_BRIDGE_H
#define NATIVE_BRIDGE_H #define NATIVE_BRIDGE_H
#include <QDebug>
#include <QJSValue> #include <QJSValue>
#include <QObject> #include <QObject>

View File

@ -0,0 +1,7 @@
#include <QDebug>
#include "native_empty.h"
void NativeEmpty::function() {
qDebug() << "nativeEmpty";
}

View File

@ -2,7 +2,6 @@
#define NATIVE_EMPTY_H #define NATIVE_EMPTY_H
#include <QObject> #include <QObject>
#include <QDebug>
class NativeEmpty : public QObject { class NativeEmpty : public QObject {
Q_OBJECT Q_OBJECT
@ -10,9 +9,7 @@ class NativeEmpty : public QObject {
public: public:
NativeEmpty(QObject *parent = nullptr) : QObject(parent) {} NativeEmpty(QObject *parent = nullptr) : QObject(parent) {}
Q_INVOKABLE void function() { Q_INVOKABLE void function();
qDebug() << "nativeEmpty";
}
}; };
#endif // NATIVE_EMPTY_H #endif // NATIVE_EMPTY_H

View File

@ -0,0 +1,13 @@
#include <QDebug>
#include "native_log.h"
void NativeLog::function(QString level, QString content) {
if (level == 'w') {
qWarning() << content;
} else if (level == 'd') {
qDebug() << content;
} else if (level == 'e') {
qCritical() << content;
}
}

View File

@ -2,7 +2,6 @@
#define NATIVELOG_H #define NATIVELOG_H
#include <QObject> #include <QObject>
#include <QDebug>
class NativeLog : public QObject { class NativeLog : public QObject {
Q_OBJECT Q_OBJECT
@ -10,15 +9,7 @@ class NativeLog : public QObject {
public: public:
NativeLog(QObject *parent = nullptr) : QObject(parent) {} NativeLog(QObject *parent = nullptr) : QObject(parent) {}
Q_INVOKABLE void function(QString level, QString content) { Q_INVOKABLE void function(QString level, QString content);
if (level == 'w') {
qWarning() << content;
} else if (level == 'd') {
qDebug() << content;
} else if (level == 'e') {
qCritical() << content;
}
}
}; };
#endif // NATIVELOG_H #endif // NATIVELOG_H

View File

@ -0,0 +1,30 @@
#include <QTimer>
#include "native_timer.h"
void NativeTimer::setTimer(long timerId, int time, bool repeat) {
QTimer *timer = new QTimer(this);
timer->setSingleShot(!repeat);
connect(timer, &QTimer::timeout, this, [=] () {
if (deletedTimerIds->contains(timerId)) {
deletedTimerIds->remove(timerId);
delete timer;
} else {
engine->evaluate(
Constant::GLOBAL_DORIC + "." +
Constant::DORIC_TIMER_CALLBACK + "(" +
QString::number(timerId) + ")"
);
if (!repeat) {
deletedTimerIds->remove(timerId);
delete timer;
}
}
});
timer->start(time);
}
void NativeTimer::clearTimer(long timerId) {
deletedTimerIds->insert(timerId);
}

View File

@ -4,7 +4,6 @@
#include <QJSEngine> #include <QJSEngine>
#include <QObject> #include <QObject>
#include <QSet> #include <QSet>
#include <QTimer>
#include "constant.h" #include "constant.h"
@ -20,32 +19,9 @@ public:
this->engine = engine; this->engine = engine;
} }
Q_INVOKABLE void setTimer(long timerId, int time, bool repeat) { Q_INVOKABLE void setTimer(long timerId, int time, bool repeat);
QTimer *timer = new QTimer(this);
timer->setSingleShot(!repeat);
connect(timer, &QTimer::timeout, this, [=] () {
if (deletedTimerIds->contains(timerId)) {
deletedTimerIds->remove(timerId);
delete timer;
} else {
engine->evaluate(
Constant::GLOBAL_DORIC + "." +
Constant::DORIC_TIMER_CALLBACK + "(" +
QString::number(timerId) + ")"
);
if (!repeat) { Q_INVOKABLE void clearTimer(long timerId);
deletedTimerIds->remove(timerId);
delete timer;
}
}
});
timer->start(time);
}
Q_INVOKABLE void clearTimer(long timerId) {
deletedTimerIds->insert(timerId);
}
}; };
#endif // NATIVE_TIMER_H #endif // NATIVE_TIMER_H

11
doric/registry.cpp Normal file
View File

@ -0,0 +1,11 @@
#include <QDebug>
#include "registry.h"
Registry::Registry() {
registerNativePlugin(typeid(ShaderPlugin).name());
}
void Registry::registerNativePlugin(QString name) {
qDebug() << name;
}

View File

@ -12,13 +12,9 @@ private:
QMap<QString, QString> pluginInfoMap; QMap<QString, QString> pluginInfoMap;
public: public:
Registry() { Registry();
registerNativePlugin(typeid(ShaderPlugin).name());
}
void registerNativePlugin(QString name) { void registerNativePlugin(QString name);
qDebug() << name;
}
}; };
#endif // REGISTRY_H #endif // REGISTRY_H