add scroller node

This commit is contained in:
王劲鹏
2021-04-08 19:50:39 +08:00
committed by osborn
parent b8440d9fb7
commit 7402fc2c0c
6 changed files with 199 additions and 0 deletions

View File

@@ -0,0 +1,90 @@
#include "DoricScrollerNode.h"
QQuickItem *DoricScrollerNode::build() {
QQmlComponent component(getContext()->getQmlEngine());
const QUrl url(QStringLiteral("qrc:/doric/qml/scroller.qml"));
component.loadUrl(url);
if (component.isError()) {
qCritical() << component.errorString();
}
QQuickItem *item = qobject_cast<QQuickItem *>(component.create());
item->setProperty("wrapper", QString::number((qint64)this));
return item;
}
void DoricScrollerNode::blendSubNode(QJsonValue subProperties) {
if (mChildNode != nullptr) {
mChildNode->blend(subProperties["props"]);
}
}
void DoricScrollerNode::blend(QQuickItem *view, QString name, QJsonValue prop) {
if (name == "content") {
if (!prop.isString()) {
return;
}
mChildViewId = prop.toString();
} else if (name == "onScroll") {
if (!prop.isString()) {
return;
}
onScrollFuncId = prop.toString();
} else if (name == "onScrollEnd") {
if (!prop.isString()) {
return;
}
onScrollEndFuncId = prop.toString();
} else {
DoricSuperNode::blend(view, name, prop);
}
}
void DoricScrollerNode::blend(QJsonValue jsValue) {
DoricViewNode::blend(jsValue);
QJsonValue contentModel = getSubModel(mChildViewId);
if (contentModel == QJsonValue::Undefined) {
return;
}
QString viewId = contentModel["id"].toString();
QString type = contentModel["type"].toString();
QJsonValue props = contentModel["props"];
QQuickItem *parent = mView->childItems().at(1);
if (mChildNode != nullptr) {
if (viewId == mChildNode->getId()) {
// skip
} else {
if (mReusable && mChildNode->getType() == type) {
mChildNode->setId(viewId);
mChildNode->blend(props);
} else {
// remove all views
for (int i = 0; i != parent->childItems().size(); i++) {
parent->childItems().at(i)->setParent(nullptr);
parent->childItems().at(i)->setParentItem(nullptr);
parent->childItems().at(i)->deleteLater();
}
mChildNode = DoricViewNode::create(getContext(), type);
mChildNode->setId(viewId);
mChildNode->init(this);
mChildNode->blend(props);
mChildNode->getNodeView()->setParentItem(parent);
}
}
} else {
mChildNode = DoricViewNode::create(getContext(), type);
mChildNode->setId(viewId);
mChildNode->init(this);
mChildNode->blend(props);
mChildNode->getNodeView()->setParentItem(parent);
}
}

View File

@@ -0,0 +1,26 @@
#ifndef DORICSCROLLERNODE_H
#define DORICSCROLLERNODE_H
#include "DoricSuperNode.h"
class DoricScrollerNode : public DoricSuperNode {
private:
DoricViewNode *mChildNode = nullptr;
QString mChildViewId;
QString onScrollFuncId;
QString onScrollEndFuncId;
public:
using DoricSuperNode::DoricSuperNode;
QQuickItem *build() override;
virtual void blend(QQuickItem *view, QString name, QJsonValue prop) override;
virtual void blend(QJsonValue jsValue) override;
virtual void blendSubNode(QJsonValue subProperties) override;
};
#endif // DORICSCROLLERNODE_H