2021-04-02 20:47:15 +08:00
|
|
|
#include "DoricViewNode.h"
|
2021-03-19 18:02:17 +08:00
|
|
|
#include "../utils/DoricConstant.h"
|
2021-02-19 15:43:09 +08:00
|
|
|
#include "../utils/DoricUtils.h"
|
|
|
|
#include "DoricSuperNode.h"
|
2021-02-09 15:54:22 +08:00
|
|
|
|
2021-04-02 20:47:15 +08:00
|
|
|
void DoricViewNode::blendLayoutConfig(QJsonValue jsObject) {
|
2021-03-15 15:30:17 +08:00
|
|
|
this->mLayoutConfig = jsObject;
|
|
|
|
|
2021-04-02 20:47:15 +08:00
|
|
|
QJsonValue margin = jsObject["margin"];
|
|
|
|
QJsonValue widthSpec = jsObject["widthSpec"];
|
|
|
|
QJsonValue heightSpec = jsObject["heightSpec"];
|
2021-02-09 15:54:22 +08:00
|
|
|
|
2021-04-02 20:47:15 +08:00
|
|
|
if (widthSpec.isDouble()) {
|
2021-03-02 17:18:23 +08:00
|
|
|
switch (widthSpec.toInt()) {
|
2021-04-06 11:19:48 +08:00
|
|
|
case SpecMode::JUST:
|
|
|
|
mView->setProperty("widthSpec", SpecMode::JUST);
|
2021-03-15 15:30:17 +08:00
|
|
|
break;
|
2021-04-06 11:19:48 +08:00
|
|
|
case SpecMode::FIT:
|
|
|
|
mView->setProperty("widthSpec", SpecMode::FIT);
|
2021-03-02 17:18:23 +08:00
|
|
|
break;
|
2021-04-06 11:19:48 +08:00
|
|
|
case SpecMode::MOST:
|
|
|
|
mView->setProperty("widthSpec", SpecMode::MOST);
|
2021-03-04 17:44:54 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-02 20:47:15 +08:00
|
|
|
if (heightSpec.isDouble()) {
|
2021-03-04 17:44:54 +08:00
|
|
|
switch (heightSpec.toInt()) {
|
2021-04-06 11:19:48 +08:00
|
|
|
case SpecMode::JUST:
|
|
|
|
mView->setProperty("heightSpec", SpecMode::JUST);
|
2021-03-15 15:30:17 +08:00
|
|
|
break;
|
2021-04-06 11:19:48 +08:00
|
|
|
case SpecMode::FIT:
|
|
|
|
mView->setProperty("heightSpec", SpecMode::FIT);
|
2021-03-02 17:18:23 +08:00
|
|
|
break;
|
2021-04-06 11:19:48 +08:00
|
|
|
case SpecMode::MOST:
|
|
|
|
mView->setProperty("heightSpec", SpecMode::MOST);
|
2021-03-02 17:18:23 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-02 20:47:15 +08:00
|
|
|
void DoricViewNode::setLayoutConfig(QJsonValue layoutConfig) {
|
2021-03-02 17:18:23 +08:00
|
|
|
if (mSuperNode != nullptr) {
|
|
|
|
mSuperNode->blendSubLayoutConfig(this, layoutConfig);
|
|
|
|
} else {
|
|
|
|
blendLayoutConfig(layoutConfig);
|
|
|
|
}
|
|
|
|
}
|
2021-02-19 15:43:09 +08:00
|
|
|
|
|
|
|
void DoricViewNode::init(DoricSuperNode *superNode) {
|
|
|
|
if (DoricUtils:: instanceof <DoricSuperNode *>(this)) {
|
|
|
|
DoricSuperNode *thiz = dynamic_cast<DoricSuperNode *>(this);
|
|
|
|
thiz->mReusable = superNode->mReusable;
|
|
|
|
}
|
|
|
|
this->mSuperNode = superNode;
|
2021-04-02 20:47:15 +08:00
|
|
|
this->mLayoutConfig = superNode->generateDefaultLayoutConfig();
|
2021-02-19 15:43:09 +08:00
|
|
|
this->mView = build();
|
2021-02-09 15:54:22 +08:00
|
|
|
}
|
2021-02-19 15:43:09 +08:00
|
|
|
|
|
|
|
QString DoricViewNode::getId() { return mId; }
|
|
|
|
|
|
|
|
void DoricViewNode::setId(QString id) { mId = id; }
|
|
|
|
|
2021-02-22 16:42:19 +08:00
|
|
|
QString DoricViewNode::getType() { return mType; }
|
|
|
|
|
|
|
|
QQuickItem *DoricViewNode::getNodeView() { return mView; }
|
|
|
|
|
2021-04-02 20:47:15 +08:00
|
|
|
void DoricViewNode::blend(QJsonValue jsValue) {
|
|
|
|
QJsonValue value = jsValue["layoutConfig"];
|
2021-03-02 17:18:23 +08:00
|
|
|
if (value.isObject()) {
|
|
|
|
setLayoutConfig(value);
|
|
|
|
}
|
2021-04-02 20:47:15 +08:00
|
|
|
|
|
|
|
foreach (const QString &key, jsValue.toObject().keys()) {
|
|
|
|
QJsonValue value = jsValue[key];
|
|
|
|
blend(mView, key, value);
|
2021-02-19 19:01:31 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-02 20:47:15 +08:00
|
|
|
void DoricViewNode::blend(QQuickItem *view, QString name, QJsonValue prop) {
|
2021-02-19 19:01:31 +08:00
|
|
|
if (name == "width") {
|
2021-04-02 20:47:15 +08:00
|
|
|
if (!prop.isDouble()) {
|
2021-02-23 18:26:00 +08:00
|
|
|
return;
|
|
|
|
}
|
2021-03-15 15:30:17 +08:00
|
|
|
if (this->mLayoutConfig.isUndefined()) {
|
|
|
|
view->setWidth(prop.toInt());
|
|
|
|
} else {
|
2021-04-02 20:47:15 +08:00
|
|
|
QJsonValue widthSpec = this->mLayoutConfig["widthSpec"];
|
|
|
|
if (widthSpec.isDouble()) {
|
2021-04-06 11:19:48 +08:00
|
|
|
if (widthSpec.toInt() == SpecMode::JUST) {
|
2021-03-15 15:30:17 +08:00
|
|
|
view->setWidth(prop.toInt());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-02-19 19:01:31 +08:00
|
|
|
} else if (name == "height") {
|
2021-04-02 20:47:15 +08:00
|
|
|
if (!prop.isDouble()) {
|
2021-02-23 18:26:00 +08:00
|
|
|
return;
|
|
|
|
}
|
2021-03-15 15:30:17 +08:00
|
|
|
if (this->mLayoutConfig.isUndefined()) {
|
|
|
|
view->setHeight(prop.toInt());
|
|
|
|
} else {
|
2021-04-02 20:47:15 +08:00
|
|
|
QJsonValue heightSpec = this->mLayoutConfig["heightSpec"];
|
|
|
|
if (heightSpec.isDouble()) {
|
2021-04-06 11:19:48 +08:00
|
|
|
if (heightSpec.toInt() == SpecMode::JUST) {
|
2021-03-15 15:30:17 +08:00
|
|
|
view->setHeight(prop.toInt());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-02-22 16:42:19 +08:00
|
|
|
} else if (name == "backgroundColor") {
|
2021-04-02 20:47:15 +08:00
|
|
|
QString color = DoricUtils::doricColor(prop.toInt()).name();
|
2021-02-24 10:50:41 +08:00
|
|
|
view->setProperty("color", color);
|
2021-02-24 15:47:57 +08:00
|
|
|
} else if (name == "x") {
|
|
|
|
view->setProperty("x", prop.toInt());
|
|
|
|
} else if (name == "y") {
|
|
|
|
view->setProperty("y", prop.toInt());
|
2021-02-24 16:23:45 +08:00
|
|
|
} else if (name == "corners") {
|
|
|
|
view->setProperty("radius", prop.toInt());
|
2021-03-19 18:02:17 +08:00
|
|
|
} else if (name == "onClick") {
|
|
|
|
if (prop.isString())
|
|
|
|
clickFunction = prop.toString();
|
2021-04-07 10:41:34 +08:00
|
|
|
} else if (name != "layoutConfig") {
|
2021-02-24 15:47:57 +08:00
|
|
|
qCritical() << name << ": " << prop.toString();
|
2021-02-19 19:01:31 +08:00
|
|
|
}
|
|
|
|
}
|
2021-03-19 18:02:17 +08:00
|
|
|
|
|
|
|
QList<QString> DoricViewNode::getIdList() {
|
|
|
|
QList<QString> ids;
|
|
|
|
|
|
|
|
DoricViewNode *viewNode = this;
|
|
|
|
do {
|
2021-04-06 16:14:43 +08:00
|
|
|
ids.insert(0, viewNode->mId);
|
2021-03-19 18:02:17 +08:00
|
|
|
viewNode = viewNode->mSuperNode;
|
|
|
|
} while (viewNode != nullptr);
|
|
|
|
|
|
|
|
return ids;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DoricViewNode::callJSResponse(QString funcId, QVariantList args) {
|
|
|
|
QVariantList nArgs;
|
|
|
|
QList<QString> idList = getIdList();
|
|
|
|
nArgs.append(idList);
|
|
|
|
nArgs.append(funcId);
|
|
|
|
foreach (const QVariant &arg, args)
|
|
|
|
nArgs.append(arg);
|
|
|
|
|
|
|
|
return getContext()->callEntity(DoricConstant::DORIC_ENTITY_RESPONSE, nArgs);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DoricViewNode::onClick() {
|
|
|
|
if (clickFunction.isEmpty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
QVariantList args;
|
|
|
|
callJSResponse(clickFunction, args);
|
|
|
|
}
|