This repository has been archived on 2024-07-22. You can view files and clone it, but cannot push or open issues or pull requests.
Doric/doric-Qt/doric/shader/DoricScrollerNode.cpp

112 lines
3.3 KiB
C++
Raw Normal View History

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);
getLayouts()->setLayoutType(DoricLayoutType::DoricStack);
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) {
layout->apply(mView->width(), mView->height());
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"]);
}
}