From 2b277a44f41db76dbd62b988f634d5a5d0f017df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E5=8A=B2=E9=B9=8F?= Date: Fri, 19 Mar 2021 18:02:17 +0800 Subject: [PATCH] add click response callback --- doric-Qt/doric/engine/DoricNativeJSE.cpp | 8 ++++ doric-Qt/doric/shader/DoricViewNode.cpp | 37 ++++++++++++++++++- doric-Qt/doric/shader/DoricViewNode.h | 8 ++++ doric-Qt/doric/utils/DoricMouseAreaBridge.cpp | 2 +- 4 files changed, 52 insertions(+), 3 deletions(-) diff --git a/doric-Qt/doric/engine/DoricNativeJSE.cpp b/doric-Qt/doric/engine/DoricNativeJSE.cpp index 7e6721cc..63bc2c6d 100644 --- a/doric-Qt/doric/engine/DoricNativeJSE.cpp +++ b/doric-Qt/doric/engine/DoricNativeJSE.cpp @@ -56,6 +56,14 @@ QJSValue DoricNativeJSE::invokeObject(QString objectName, QString functionName, } } args.push_back(arg); + } else if (variant.type() == QVariant::StringList) { + QStringList array = variant.toStringList(); + QJSValue arg = mJSEngine.newArray(array.size()); + + for (int i = 0; i != array.size(); i++) { + arg.setProperty(i, array.at(i)); + } + args.push_back(arg); } } diff --git a/doric-Qt/doric/shader/DoricViewNode.cpp b/doric-Qt/doric/shader/DoricViewNode.cpp index 89a2a057..0731180e 100644 --- a/doric-Qt/doric/shader/DoricViewNode.cpp +++ b/doric-Qt/doric/shader/DoricViewNode.cpp @@ -1,5 +1,6 @@ #include +#include "../utils/DoricConstant.h" #include "../utils/DoricUtils.h" #include "DoricSuperNode.h" #include "DoricViewNode.h" @@ -115,9 +116,41 @@ void DoricViewNode::blend(QQuickItem *view, QString name, QJSValue prop) { view->setProperty("y", prop.toInt()); } else if (name == "corners") { view->setProperty("radius", prop.toInt()); - } else if (name == "layoutConfig") { - + } else if (name == "onClick") { + if (prop.isString()) + clickFunction = prop.toString(); } else { qCritical() << name << ": " << prop.toString(); } } + +QList DoricViewNode::getIdList() { + QList ids; + + DoricViewNode *viewNode = this; + do { + ids.append(viewNode->mId); + viewNode = viewNode->mSuperNode; + } while (viewNode != nullptr); + + return ids; +} + +void DoricViewNode::callJSResponse(QString funcId, QVariantList args) { + QVariantList nArgs; + QList idList = getIdList(); + nArgs.append(idList); + nArgs.append(funcId); + foreach (const QVariant &arg, args) + nArgs.append(arg); + + return getContext()->callEntity(DoricConstant::DORIC_ENTITY_RESPONSE, nArgs); +} + +void DoricViewNode::onClick() { + if (clickFunction.isEmpty()) { + return; + } + QVariantList args; + callJSResponse(clickFunction, args); +} diff --git a/doric-Qt/doric/shader/DoricViewNode.h b/doric-Qt/doric/shader/DoricViewNode.h index be272a9d..d5d720db 100644 --- a/doric-Qt/doric/shader/DoricViewNode.h +++ b/doric-Qt/doric/shader/DoricViewNode.h @@ -23,6 +23,10 @@ private: QJSValue mLayoutConfig; + QList getIdList(); + + QString clickFunction; + public: QString mType; @@ -58,5 +62,9 @@ public: virtual void blend(QQuickItem *view, QString name, QJSValue prop); virtual void blendLayoutConfig(QJSValue jsObject); + + void onClick(); + + void callJSResponse(QString funcId, QVariantList args); }; #endif // DORICVIEWNODE_H diff --git a/doric-Qt/doric/utils/DoricMouseAreaBridge.cpp b/doric-Qt/doric/utils/DoricMouseAreaBridge.cpp index 7db1b513..dbf27c52 100644 --- a/doric-Qt/doric/utils/DoricMouseAreaBridge.cpp +++ b/doric-Qt/doric/utils/DoricMouseAreaBridge.cpp @@ -6,5 +6,5 @@ DoricMouseAreaBridge::DoricMouseAreaBridge(QObject *parent) : QObject(parent) {} void DoricMouseAreaBridge::onClick(qint64 pointer) { QObject *object = (QObject *)(pointer); DoricViewNode *viewNode = dynamic_cast(object); - qCritical() << viewNode; + viewNode->onClick(); }