2021-02-09 15:54:22 +08:00
|
|
|
#include "DoricGroupNode.h"
|
2021-02-19 19:01:31 +08:00
|
|
|
|
2021-04-02 20:47:15 +08:00
|
|
|
void DoricGroupNode::blend(QQuickItem *view, QString name, QJsonValue prop) {
|
2021-02-19 19:01:31 +08:00
|
|
|
if (name == "children") {
|
|
|
|
mChildViewIds.clear();
|
|
|
|
if (prop.isArray()) {
|
2021-04-02 20:47:15 +08:00
|
|
|
QJsonArray array = prop.toArray();
|
|
|
|
const int length = array.size();
|
2021-02-20 16:20:18 +08:00
|
|
|
for (int i = 0; i < length; ++i) {
|
2021-04-02 20:47:15 +08:00
|
|
|
QJsonValue value = array.at(i);
|
2021-02-20 16:20:18 +08:00
|
|
|
if (value.isString()) {
|
|
|
|
mChildViewIds.append(value.toString());
|
|
|
|
}
|
|
|
|
}
|
2021-02-19 19:01:31 +08:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
DoricSuperNode::blend(view, name, prop);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-02 20:47:15 +08:00
|
|
|
void DoricGroupNode::blend(QJsonValue jsValue) {
|
2021-02-19 19:01:31 +08:00
|
|
|
DoricViewNode::blend(jsValue);
|
|
|
|
configChildNode();
|
|
|
|
}
|
|
|
|
|
2021-02-20 16:20:18 +08:00
|
|
|
void DoricGroupNode::configChildNode() {
|
2021-03-15 15:30:17 +08:00
|
|
|
QQuickItem *parent = nullptr;
|
|
|
|
if (mType.isEmpty() || mType == "Stack") {
|
|
|
|
parent = mView;
|
|
|
|
} else {
|
2021-03-19 16:13:02 +08:00
|
|
|
parent = mView->childItems().at(1);
|
2021-03-15 15:30:17 +08:00
|
|
|
}
|
2021-02-20 16:20:18 +08:00
|
|
|
for (int idx = 0; idx < mChildViewIds.size(); idx++) {
|
|
|
|
QString id = mChildViewIds.at(idx);
|
2021-04-02 20:47:15 +08:00
|
|
|
QJsonValue model = getSubModel(id);
|
2021-02-20 16:20:18 +08:00
|
|
|
if (model.isUndefined()) {
|
2021-02-20 17:02:26 +08:00
|
|
|
DoricRegistry *registry = getContext()->getDriver()->getRegistry();
|
2021-02-22 16:42:19 +08:00
|
|
|
qCritical() << "model.isUndefined()";
|
2021-02-20 16:20:18 +08:00
|
|
|
continue;
|
|
|
|
}
|
2021-04-02 20:47:15 +08:00
|
|
|
QString type = model["type"].toString();
|
2021-02-20 16:20:18 +08:00
|
|
|
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) {
|
2021-02-22 16:42:19 +08:00
|
|
|
if (oldNode->getType() == type) {
|
2021-02-23 10:27:28 +08:00
|
|
|
// Same type,can be reused
|
2021-02-22 16:42:19 +08:00
|
|
|
oldNode->setId(id);
|
2021-04-02 20:47:15 +08:00
|
|
|
oldNode->blend(model["props"]);
|
2021-02-22 16:42:19 +08:00
|
|
|
} else {
|
2021-02-23 10:27:28 +08:00
|
|
|
// Replace this view
|
2021-02-22 16:42:19 +08:00
|
|
|
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);
|
|
|
|
|
2021-02-24 14:10:22 +08:00
|
|
|
if (idx >= mChildNodes.size()) {
|
|
|
|
mChildNodes.append(newNode);
|
2021-03-15 15:30:17 +08:00
|
|
|
newNode->getNodeView()->setParentItem(parent);
|
2021-02-24 14:10:22 +08:00
|
|
|
} else {
|
|
|
|
mChildNodes.insert(idx, newNode);
|
2021-03-15 15:30:17 +08:00
|
|
|
newNode->getNodeView()->setParentItem(parent);
|
2021-02-24 14:10:22 +08:00
|
|
|
newNode->getNodeView()->stackBefore(
|
2021-03-15 15:30:17 +08:00
|
|
|
parent->childItems().at(idx));
|
2021-02-24 14:10:22 +08:00
|
|
|
}
|
2021-03-15 15:30:17 +08:00
|
|
|
|
2021-04-02 20:47:15 +08:00
|
|
|
newNode->blend(model["props"]);
|
2021-02-22 16:42:19 +08:00
|
|
|
}
|
|
|
|
}
|
2021-02-20 16:20:18 +08:00
|
|
|
} else {
|
2021-02-22 16:42:19 +08:00
|
|
|
// 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
|
|
|
|
mChildNodes.swapItemsAt(position, idx);
|
2021-03-15 15:30:17 +08:00
|
|
|
parent->childItems().swapItemsAt(position, idx);
|
2021-02-23 10:27:28 +08:00
|
|
|
} else {
|
|
|
|
// Not found,insert
|
|
|
|
DoricViewNode *newNode = DoricViewNode::create(getContext(), type);
|
|
|
|
if (newNode != nullptr) {
|
|
|
|
newNode->setId(id);
|
|
|
|
newNode->init(this);
|
2021-02-22 16:42:19 +08:00
|
|
|
|
2021-02-24 14:10:22 +08:00
|
|
|
if (idx >= mChildNodes.size()) {
|
|
|
|
mChildNodes.append(newNode);
|
2021-03-15 15:30:17 +08:00
|
|
|
newNode->getNodeView()->setParentItem(parent);
|
2021-02-24 14:10:22 +08:00
|
|
|
} else {
|
|
|
|
mChildNodes.insert(idx, newNode);
|
2021-03-15 15:30:17 +08:00
|
|
|
newNode->getNodeView()->setParentItem(parent);
|
2021-02-24 14:10:22 +08:00
|
|
|
newNode->getNodeView()->stackBefore(
|
2021-03-15 15:30:17 +08:00
|
|
|
parent->childItems().at(idx));
|
2021-02-24 14:10:22 +08:00
|
|
|
}
|
2021-03-15 15:30:17 +08:00
|
|
|
|
2021-04-02 20:47:15 +08:00
|
|
|
newNode->blend(model["props"]);
|
2021-02-22 16:42:19 +08:00
|
|
|
}
|
|
|
|
}
|
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);
|
|
|
|
|
2021-02-24 14:10:22 +08:00
|
|
|
if (idx >= mChildNodes.size()) {
|
|
|
|
mChildNodes.append(newNode);
|
2021-03-15 15:30:17 +08:00
|
|
|
newNode->getNodeView()->setParentItem(parent);
|
2021-02-24 14:10:22 +08:00
|
|
|
} else {
|
|
|
|
mChildNodes.insert(idx, newNode);
|
2021-03-15 15:30:17 +08:00
|
|
|
newNode->getNodeView()->setParentItem(parent);
|
|
|
|
newNode->getNodeView()->stackBefore(parent->childItems().at(idx));
|
2021-02-24 14:10:22 +08:00
|
|
|
}
|
2021-03-15 15:30:17 +08:00
|
|
|
|
2021-04-02 20:47:15 +08:00
|
|
|
newNode->blend(model["props"]);
|
2021-02-23 10:27:28 +08:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2021-04-02 20:47:15 +08:00
|
|
|
void DoricGroupNode::blendSubNode(QJsonValue subProperties) {
|
|
|
|
QString subNodeId = subProperties["id"].toString();
|
2021-02-20 16:20:18 +08:00
|
|
|
for (DoricViewNode *node : mChildNodes) {
|
|
|
|
if (subNodeId == node->getId()) {
|
2021-04-02 20:47:15 +08:00
|
|
|
node->blend(subProperties["props"]);
|
2021-02-20 16:20:18 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|