From 7ced952a57875ed33fdb2791f9306eeeaf95b89f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E5=8A=B2=E9=B9=8F?= Date: Fri, 19 Feb 2021 15:43:09 +0800 Subject: [PATCH] add quick item to quick view dynamically --- doric-Qt/doric/demo/DoricDemoBridge.cpp | 27 ++++++++++++++----- doric-Qt/doric/doric.pro.user | 2 +- doric-Qt/doric/plugin/DoricShaderPlugin.cpp | 12 ++++++--- doric-Qt/doric/qml.qrc | 10 ++++--- doric-Qt/doric/{ => resources}/main.qml | 0 doric-Qt/doric/resources/panel.qml | 8 ++++++ .../{ => resources}/qtquickcontrols2.conf | 0 doric-Qt/doric/{ => resources}/stack.qml | 0 doric-Qt/doric/resources/view.qml | 6 +++++ doric-Qt/doric/shader/DoricStackNode.cpp | 2 +- doric-Qt/doric/shader/DoricStackNode.h | 9 +++---- doric-Qt/doric/shader/DoricSuperNode.h | 3 +++ doric-Qt/doric/shader/DoricViewNode.cpp | 19 ++++++++++++- doric-Qt/doric/shader/DoricViewNode.h | 14 ++++++++++ doric-Qt/doric/utils/DoricUtils.h | 5 ++++ 15 files changed, 97 insertions(+), 20 deletions(-) rename doric-Qt/doric/{ => resources}/main.qml (100%) create mode 100644 doric-Qt/doric/resources/panel.qml rename doric-Qt/doric/{ => resources}/qtquickcontrols2.conf (100%) rename doric-Qt/doric/{ => resources}/stack.qml (100%) create mode 100644 doric-Qt/doric/resources/view.qml diff --git a/doric-Qt/doric/demo/DoricDemoBridge.cpp b/doric-Qt/doric/demo/DoricDemoBridge.cpp index 5786e3f2..a7ee3cfd 100644 --- a/doric-Qt/doric/demo/DoricDemoBridge.cpp +++ b/doric-Qt/doric/demo/DoricDemoBridge.cpp @@ -14,17 +14,32 @@ void DoricDemoBridge::navigate(QVariant route) { QString script = DoricUtils::readAssetFile("/doric/bundles", name); QQuickView *view = new QQuickView(); - view->setWidth(450); - view->setHeight(800); - QColor blue("blue"); - view->setColor(blue); - view->show(); + { + const QUrl url(QStringLiteral("qrc:/doric/qml/view.qml")); + view->setSource(url); + view->setWidth(450); + view->setHeight(800); + } DoricPanel *panel = new DoricPanel(); - panel->setParent(view); + panel->setParentItem(view->rootObject()); panel->setWidth(450); panel->setHeight(800); panel->config(script, name, NULL); + + QQmlEngine *engine = view->engine(); + QQmlComponent component(engine); + const QUrl empty(QStringLiteral("qrc:/doric/qml/panel.qml")); + component.loadUrl(empty); + QQuickItem *childItem = qobject_cast(component.create()); + + if (childItem == nullptr) { + qCritical() << component.errorString(); + return; + } + childItem->setParentItem(view->rootObject()); + + view->show(); break; } } diff --git a/doric-Qt/doric/doric.pro.user b/doric-Qt/doric/doric.pro.user index 88721de2..52bbe3b0 100644 --- a/doric-Qt/doric/doric.pro.user +++ b/doric-Qt/doric/doric.pro.user @@ -1,6 +1,6 @@ - + EnvironmentId diff --git a/doric-Qt/doric/plugin/DoricShaderPlugin.cpp b/doric-Qt/doric/plugin/DoricShaderPlugin.cpp index cdea7f66..e30934ab 100644 --- a/doric-Qt/doric/plugin/DoricShaderPlugin.cpp +++ b/doric-Qt/doric/plugin/DoricShaderPlugin.cpp @@ -1,13 +1,19 @@ -#include "DoricShaderPlugin.h" - #include +#include "../shader/DoricRootNode.h" +#include "DoricShaderPlugin.h" + void DoricShaderPlugin::render(QJSValue jsValue, QString callbackId) { qDebug() << getContext(); getContext()->getDriver()->asyncCall( [this, jsValue] { QString viewId = jsValue.property("id").toString(); - getContext()->getDriver(); + DoricRootNode *rootNode = getContext()->getRootNode(); + + if (rootNode->getId().isEmpty() && jsValue.property("type").toString() == "Root") { + rootNode->setId(viewId); + rootNode->blend(jsValue.property("props")); + } }, DoricThreadMode::UI); } diff --git a/doric-Qt/doric/qml.qrc b/doric-Qt/doric/qml.qrc index 181c1653..164c2d3a 100644 --- a/doric-Qt/doric/qml.qrc +++ b/doric-Qt/doric/qml.qrc @@ -1,8 +1,7 @@ - main.qml - qtquickcontrols2.conf - stack.qml + ./resources/main.qml + ./resources/qtquickcontrols2.conf ../../doric-js/bundle/doric-sandbox.js @@ -14,4 +13,9 @@ ../../doric-demo/bundle/src/Snake.js ../../doric-demo/bundle/src/Snake.es5.js + + ./resources/view.qml + ./resources/panel.qml + ./resources/stack.qml + diff --git a/doric-Qt/doric/main.qml b/doric-Qt/doric/resources/main.qml similarity index 100% rename from doric-Qt/doric/main.qml rename to doric-Qt/doric/resources/main.qml diff --git a/doric-Qt/doric/resources/panel.qml b/doric-Qt/doric/resources/panel.qml new file mode 100644 index 00000000..15c79292 --- /dev/null +++ b/doric-Qt/doric/resources/panel.qml @@ -0,0 +1,8 @@ +import QtQuick 2.12 +import QtQuick.Controls 2.5 + +Rectangle { + width: 100 + height: 100 + color: 'red' +} diff --git a/doric-Qt/doric/qtquickcontrols2.conf b/doric-Qt/doric/resources/qtquickcontrols2.conf similarity index 100% rename from doric-Qt/doric/qtquickcontrols2.conf rename to doric-Qt/doric/resources/qtquickcontrols2.conf diff --git a/doric-Qt/doric/stack.qml b/doric-Qt/doric/resources/stack.qml similarity index 100% rename from doric-Qt/doric/stack.qml rename to doric-Qt/doric/resources/stack.qml diff --git a/doric-Qt/doric/resources/view.qml b/doric-Qt/doric/resources/view.qml new file mode 100644 index 00000000..ce7c59d6 --- /dev/null +++ b/doric-Qt/doric/resources/view.qml @@ -0,0 +1,6 @@ +import QtQuick 2.12 +import QtQuick.Controls 2.5 + +StackView { + +} diff --git a/doric-Qt/doric/shader/DoricStackNode.cpp b/doric-Qt/doric/shader/DoricStackNode.cpp index 0e536a7b..c63542ee 100644 --- a/doric-Qt/doric/shader/DoricStackNode.cpp +++ b/doric-Qt/doric/shader/DoricStackNode.cpp @@ -3,7 +3,7 @@ QQuickItem *DoricStackNode::build() { QQmlComponent component; - const QUrl url(QStringLiteral("qrc:/stack.qml")); + const QUrl url(QStringLiteral("qrc:/doric/qml/stack.qml")); component.loadUrl(url); QQuickItem *item = qobject_cast(component.create()); diff --git a/doric-Qt/doric/shader/DoricStackNode.h b/doric-Qt/doric/shader/DoricStackNode.h index 249cd37a..9843582f 100644 --- a/doric-Qt/doric/shader/DoricStackNode.h +++ b/doric-Qt/doric/shader/DoricStackNode.h @@ -3,14 +3,13 @@ #include "DoricGroupNode.h" -class DoricStackNode : public DoricGroupNode -{ +class DoricStackNode : public DoricGroupNode { public: - using DoricGroupNode::DoricGroupNode; + using DoricGroupNode::DoricGroupNode; - QQuickItem *build() override; + QQuickItem *build() override; - void blendLayoutConfig() override; + void blendLayoutConfig() override; }; #endif // DORICSTACKNODE_H diff --git a/doric-Qt/doric/shader/DoricSuperNode.h b/doric-Qt/doric/shader/DoricSuperNode.h index 3605f3a2..413617d7 100644 --- a/doric-Qt/doric/shader/DoricSuperNode.h +++ b/doric-Qt/doric/shader/DoricSuperNode.h @@ -4,11 +4,14 @@ #include "DoricViewNode.h" class DoricSuperNode : public DoricViewNode { + protected: void blendSubLayoutConfig(DoricViewNode *viewNode); public: using DoricViewNode::DoricViewNode; + + bool mReusable = false; }; #endif // DORICSUPERNODE_H diff --git a/doric-Qt/doric/shader/DoricViewNode.cpp b/doric-Qt/doric/shader/DoricViewNode.cpp index 79f17b45..2e879e41 100644 --- a/doric-Qt/doric/shader/DoricViewNode.cpp +++ b/doric-Qt/doric/shader/DoricViewNode.cpp @@ -1,5 +1,22 @@ #include "DoricViewNode.h" +#include "../utils/DoricUtils.h" +#include "DoricSuperNode.h" -void DoricViewNode::blendLayoutConfig(){ +void DoricViewNode::blendLayoutConfig() {} +void DoricViewNode::setLayoutConfig(QJSValue layoutConfig) {} + +void DoricViewNode::init(DoricSuperNode *superNode) { + if (DoricUtils:: instanceof (this)) { + DoricSuperNode *thiz = dynamic_cast(this); + thiz->mReusable = superNode->mReusable; + } + this->mSuperNode = superNode; + this->mView = build(); } + +QString DoricViewNode::getId() { return mId; } + +void DoricViewNode::setId(QString id) { mId = id; } + +void DoricViewNode::blend(QJSValue jsValue) { qDebug() << jsValue.toString(); } diff --git a/doric-Qt/doric/shader/DoricViewNode.h b/doric-Qt/doric/shader/DoricViewNode.h index 91cbe2e5..28322f4f 100644 --- a/doric-Qt/doric/shader/DoricViewNode.h +++ b/doric-Qt/doric/shader/DoricViewNode.h @@ -5,20 +5,34 @@ #include "../utils/DoricContextHolder.h" +class DoricSuperNode; + class DoricViewNode : public DoricContextHolder { protected: QQuickItem *mView; + DoricSuperNode *mSuperNode; + virtual QQuickItem *build() = 0; virtual void blendLayoutConfig(); + void setLayoutConfig(QJSValue layoutConfig); + private: QString mId; QString mType; public: using DoricContextHolder::DoricContextHolder; + + void init(DoricSuperNode *superNode); + + QString getId(); + + void setId(QString id); + + void blend(QJSValue jsValue); }; #endif // DORICVIEWNODE_H diff --git a/doric-Qt/doric/utils/DoricUtils.h b/doric-Qt/doric/utils/DoricUtils.h index 61e57773..785b8e53 100644 --- a/doric-Qt/doric/utils/DoricUtils.h +++ b/doric-Qt/doric/utils/DoricUtils.h @@ -20,6 +20,11 @@ public: return content; } + + template + static inline bool instanceof (const T *) { + return std::is_base_of::value; + } }; #endif // UTILS_H