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/DoricGroupNode.cpp

143 lines
4.8 KiB
C++
Raw Normal View History

2021-02-09 15:54:22 +08:00
#include "DoricGroupNode.h"
2021-02-19 19:01:31 +08:00
void DoricGroupNode::blend(QQuickItem *view, QString name, QJSValue prop) {
if (name == "children") {
mChildViewIds.clear();
if (prop.isArray()) {
2021-02-20 16:20:18 +08:00
const int length = prop.property("length").toInt();
for (int i = 0; i < length; ++i) {
QJSValue value = prop.property(i);
if (value.isString()) {
mChildViewIds.append(value.toString());
}
}
2021-02-19 19:01:31 +08:00
}
} else {
DoricSuperNode::blend(view, name, prop);
}
}
void DoricGroupNode::blend(QJSValue jsValue) {
DoricViewNode::blend(jsValue);
configChildNode();
}
2021-02-20 16:20:18 +08:00
void DoricGroupNode::configChildNode() {
for (int idx = 0; idx < mChildViewIds.size(); idx++) {
QString id = mChildViewIds.at(idx);
QJSValue model = getSubModel(id);
if (model.isUndefined()) {
2021-02-20 17:02:26 +08:00
DoricRegistry *registry = getContext()->getDriver()->getRegistry();
qCritical() << "model.isUndefined()";
2021-02-20 16:20:18 +08:00
continue;
}
QString type = model.property("type").toString();
if (idx < mChildNodes.size()) {
DoricViewNode *oldNode = mChildNodes.at(idx);
if (id == oldNode->getId()) {
// The same, skip
2021-02-23 10:27:28 +08:00
} else {
2021-02-20 16:20:18 +08:00
if (mReusable) {
if (oldNode->getType() == type) {
2021-02-23 10:27:28 +08:00
// Same type,can be reused
oldNode->setId(id);
oldNode->blend(model.property("props"));
} else {
2021-02-23 10:27:28 +08:00
// Replace this view
mChildNodes.remove(idx);
oldNode->getNodeView()->setParent(nullptr);
oldNode->getNodeView()->setParentItem(nullptr);
oldNode->getNodeView()->deleteLater();
DoricViewNode *newNode = DoricViewNode::create(getContext(), type);
if (newNode != nullptr) {
newNode->setId(id);
newNode->init(this);
newNode->blend(model.property("props"));
mChildNodes.insert(idx, newNode);
int minIndex = qMin(idx, mView->childItems().size());
newNode->getNodeView()->setParentItem(mView);
newNode->getNodeView()->stackBefore(
mView->childItems().at(minIndex));
}
}
2021-02-20 16:20:18 +08:00
} else {
// Find in remain nodes
int position = -1;
for (int start = idx + 1; start < mChildNodes.size(); start++) {
DoricViewNode *node = mChildNodes.at(start);
if (id == node->getId()) {
// Found
position = start;
break;
}
2021-02-23 10:27:28 +08:00
}
if (position >= 0) {
// Found swap idx,position
DoricViewNode *reused = mChildNodes.at(position);
DoricViewNode *abandoned = mChildNodes.at(idx);
mChildNodes.swapItemsAt(position, idx);
// View swap index
reused->getNodeView()->setParentItem(nullptr);
int minIndex = qMin(idx, mView->childItems().size());
reused->getNodeView()->setParentItem(mView);
reused->getNodeView()->stackBefore(
mView->childItems().at(minIndex));
2021-02-23 10:27:28 +08:00
abandoned->getNodeView()->stackBefore(
mView->childItems().at(position));
} else {
// Not found,insert
DoricViewNode *newNode = DoricViewNode::create(getContext(), type);
if (newNode != nullptr) {
newNode->setId(id);
newNode->init(this);
newNode->blend(model.property("props"));
mChildNodes.insert(idx, newNode);
int minIndex = qMin(idx, mView->childItems().size());
2021-02-23 10:27:28 +08:00
newNode->getNodeView()->setParentItem(mView);
newNode->getNodeView()->stackBefore(
mView->childItems().at(minIndex));
}
}
2021-02-20 16:20:18 +08:00
}
}
} else {
// Insert
2021-02-23 10:27:28 +08:00
DoricViewNode *newNode = DoricViewNode::create(getContext(), type);
if (newNode != nullptr) {
newNode->setId(id);
newNode->init(this);
newNode->blend(model.property("props"));
mChildNodes.append(newNode);
int minIndex = qMin(idx, mView->childItems().size());
newNode->getNodeView()->setParentItem(mView);
newNode->getNodeView()->stackBefore(mView->childItems().at(minIndex));
}
2021-02-20 16:20:18 +08:00
}
}
2021-02-23 10:27:28 +08:00
int size = mChildNodes.size();
for (int idx = mChildViewIds.size(); idx < size; idx++) {
DoricViewNode *viewNode = mChildNodes.at(mChildViewIds.size());
mChildNodes.remove(mChildViewIds.size());
viewNode->getNodeView()->setParent(nullptr);
viewNode->getNodeView()->setParentItem(nullptr);
viewNode->getNodeView()->deleteLater();
}
2021-02-20 16:20:18 +08:00
}
void DoricGroupNode::blendSubNode(QJSValue subProperties) {
QString subNodeId = subProperties.property("id").toString();
for (DoricViewNode *node : mChildNodes) {
if (subNodeId == node->getId()) {
node->blend(subProperties.property("props"));
break;
}
}
}