2021-04-08 19:50:39 +08:00
|
|
|
#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());
|
2021-04-16 10:06:15 +08:00
|
|
|
this->createLayouts(item);
|
|
|
|
|
2021-04-08 19:50:39 +08:00
|
|
|
item->setProperty("wrapper", QString::number((qint64)this));
|
|
|
|
return item;
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-16 10:06:15 +08:00
|
|
|
void DoricScrollerNode::afterBlended(QJsonValue jsValue) {
|
2021-04-08 19:50:39 +08:00
|
|
|
QJsonValue contentModel = getSubModel(mChildViewId);
|
|
|
|
if (contentModel == QJsonValue::Undefined) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString viewId = contentModel["id"].toString();
|
|
|
|
QString type = contentModel["type"].toString();
|
|
|
|
QJsonValue props = contentModel["props"];
|
|
|
|
|
2021-04-16 10:06:15 +08:00
|
|
|
QQuickItem *parent = mView;
|
2021-04-08 19:50:39 +08:00
|
|
|
|
|
|
|
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);
|
|
|
|
|
2021-04-16 16:44:08 +08:00
|
|
|
QQmlListProperty<QQuickItem> contentChildren =
|
|
|
|
qvariant_cast<QQmlListProperty<QQuickItem>>(
|
|
|
|
parent->property("contentChildren"));
|
|
|
|
contentChildren.append(&contentChildren, mChildNode->getNodeView());
|
2021-04-08 19:50:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
mChildNode = DoricViewNode::create(getContext(), type);
|
|
|
|
mChildNode->setId(viewId);
|
|
|
|
mChildNode->init(this);
|
|
|
|
mChildNode->blend(props);
|
|
|
|
|
2021-04-16 16:44:08 +08:00
|
|
|
QQmlListProperty<QQuickItem> contentChildren =
|
|
|
|
qvariant_cast<QQmlListProperty<QQuickItem>>(
|
|
|
|
parent->property("contentChildren"));
|
|
|
|
contentChildren.append(&contentChildren, mChildNode->getNodeView());
|
2021-04-08 19:50:39 +08:00
|
|
|
}
|
|
|
|
}
|
2021-04-16 10:06:15 +08:00
|
|
|
|
|
|
|
void DoricScrollerNode::requestLayout() {
|
|
|
|
this->mChildNode->requestLayout();
|
2021-04-16 16:44:08 +08:00
|
|
|
DoricLayouts *layout = (DoricLayouts *)(mChildNode->getNodeView()
|
|
|
|
->property("doricLayout")
|
|
|
|
.toULongLong());
|
|
|
|
if (layout != nullptr) {
|
2021-04-19 19:22:10 +08:00
|
|
|
layout->apply(QSizeF(mView->width(), mView->height()));
|
2021-04-16 16:44:08 +08:00
|
|
|
|
|
|
|
mView->setProperty("contentWidth", layout->getMeasuredWidth());
|
|
|
|
mView->setProperty("contentHeight", layout->getMeasuredHeight());
|
|
|
|
}
|
2021-04-16 10:06:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void DoricScrollerNode::blendSubNode(QJsonValue subProperties) {
|
|
|
|
if (mChildNode != nullptr) {
|
|
|
|
mChildNode->blend(subProperties["props"]);
|
|
|
|
}
|
|
|
|
}
|
2021-04-19 21:17:40 +08:00
|
|
|
|
|
|
|
QSizeF DoricScrollerNode::sizeThatFits(QSizeF size) {
|
|
|
|
DoricLayouts *layout = (DoricLayouts *)mChildNode->getNodeView()
|
|
|
|
->property("doricLayout")
|
|
|
|
.toULongLong();
|
|
|
|
|
2021-06-15 17:36:01 +08:00
|
|
|
if (!layout->getResolved()) {
|
2021-05-28 13:27:11 +08:00
|
|
|
layout->apply(size);
|
|
|
|
}
|
|
|
|
|
2021-06-15 17:36:01 +08:00
|
|
|
return QSizeF(layout->getMeasuredWidth(), layout->getMeasuredHeight());
|
2021-04-19 21:17:40 +08:00
|
|
|
}
|
2021-05-07 18:17:07 +08:00
|
|
|
|
|
|
|
DoricViewNode *DoricScrollerNode::getSubNodeById(QString id) {
|
|
|
|
return id == mChildNode->getId() ? mChildNode : nullptr;
|
|
|
|
}
|