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-13 21:14:17 +08:00
|
|
|
void DoricViewNode::blendLayoutConfig(QJsonValue jsValue) {
|
|
|
|
QJsonObject jsObject = jsValue.toObject();
|
|
|
|
if (jsObject.contains("widthSpec"))
|
|
|
|
getLayouts()->setWidthSpec(jsObject["widthSpec"].toInt());
|
|
|
|
|
|
|
|
if (jsObject.contains("heightSpec"))
|
|
|
|
getLayouts()->setHeightSpec(jsObject["heightSpec"].toInt());
|
|
|
|
|
|
|
|
if (jsObject.contains("margin")) {
|
|
|
|
QJsonObject margin = jsObject["margin"].toObject();
|
|
|
|
|
|
|
|
if (margin.contains("left"))
|
2021-04-15 10:20:55 +08:00
|
|
|
getLayouts()->setMarginLeft(margin["left"].toDouble());
|
2021-04-13 21:14:17 +08:00
|
|
|
|
|
|
|
if (margin.contains("top"))
|
2021-04-15 10:20:55 +08:00
|
|
|
getLayouts()->setMarginTop(margin["top"].toDouble());
|
2021-04-13 21:14:17 +08:00
|
|
|
|
|
|
|
if (margin.contains("right"))
|
2021-04-15 10:20:55 +08:00
|
|
|
getLayouts()->setMarginRight(margin["right"].toDouble());
|
2021-04-13 21:14:17 +08:00
|
|
|
|
|
|
|
if (margin.contains("bottom"))
|
2021-04-15 10:20:55 +08:00
|
|
|
getLayouts()->setMarginBottom(margin["bottom"].toDouble());
|
2021-03-04 17:44:54 +08:00
|
|
|
}
|
|
|
|
|
2021-04-13 21:14:17 +08:00
|
|
|
if (jsObject.contains("alignment"))
|
|
|
|
getLayouts()->setAlignment(jsObject["alignment"].toInt());
|
|
|
|
|
|
|
|
if (jsObject.contains("weight"))
|
|
|
|
getLayouts()->setWeight(jsObject["weight"].toInt());
|
|
|
|
|
|
|
|
if (jsObject.contains("maxWidth"))
|
2021-04-15 10:20:55 +08:00
|
|
|
getLayouts()->setMaxWidth(jsObject["maxWidth"].toDouble());
|
2021-04-13 21:14:17 +08:00
|
|
|
|
|
|
|
if (jsObject.contains("maxHeight"))
|
2021-04-15 10:20:55 +08:00
|
|
|
getLayouts()->setMaxHeight(jsObject["maxHeight"].toDouble());
|
2021-04-13 21:14:17 +08:00
|
|
|
|
|
|
|
if (jsObject.contains("minWidth"))
|
2021-04-15 10:20:55 +08:00
|
|
|
getLayouts()->setMinWidth(jsObject["minWidth"].toDouble());
|
2021-04-13 21:14:17 +08:00
|
|
|
|
|
|
|
if (jsObject.contains("minHeight"))
|
2021-04-15 10:20:55 +08:00
|
|
|
getLayouts()->setMinHeight(jsObject["minHeight"].toDouble());
|
2021-04-13 21:14:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void DoricViewNode::createLayouts(QQuickItem *view) {
|
|
|
|
if (mLayouts == nullptr) {
|
|
|
|
mLayouts = new DoricLayouts();
|
|
|
|
mLayouts->setWidth(view->width());
|
|
|
|
mLayouts->setHeight(view->height());
|
|
|
|
mLayouts->setView(view);
|
|
|
|
|
|
|
|
view->setProperty("doricLayout", QString::number((qint64)mLayouts));
|
2021-03-02 17:18:23 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-13 21:14:17 +08:00
|
|
|
DoricLayouts *DoricViewNode::getLayouts() { return mLayouts; }
|
|
|
|
|
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;
|
|
|
|
this->mView = build();
|
2021-04-13 21:14:17 +08:00
|
|
|
getLayouts();
|
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-13 21:14:17 +08:00
|
|
|
|
|
|
|
this->afterBlended(jsValue);
|
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-15 10:20:55 +08:00
|
|
|
getLayouts()->setWidth(prop.toDouble());
|
2021-02-19 19:01:31 +08:00
|
|
|
} else if (name == "height") {
|
2021-04-15 10:20:55 +08:00
|
|
|
getLayouts()->setHeight(prop.toDouble());
|
2021-02-22 16:42:19 +08:00
|
|
|
} else if (name == "backgroundColor") {
|
2021-04-30 19:56:05 +08:00
|
|
|
if (prop.isDouble()) {
|
|
|
|
view->setProperty("backgroundColorIsObject", false);
|
|
|
|
view->setProperty(
|
|
|
|
"backgroundColor",
|
|
|
|
QVariant::fromValue(DoricUtils::doricColor(prop.toInt())));
|
|
|
|
} else if (prop.isObject()) {
|
|
|
|
QJsonObject dict = prop.toObject();
|
|
|
|
view->setProperty("backgroundColorIsObject", true);
|
|
|
|
|
|
|
|
QJsonValue orientation = prop["orientation"];
|
|
|
|
if (orientation.isDouble()) {
|
|
|
|
view->setProperty("orientation", orientation.toInt());
|
|
|
|
}
|
|
|
|
if (dict.keys().contains("colors")) {
|
|
|
|
QVector<int> colors;
|
|
|
|
QJsonArray colorArray = dict["colors"].toArray();
|
|
|
|
for (int i = 0; i != colorArray.size(); i++) {
|
|
|
|
colors.append(colorArray.at(i).toInt());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dict.keys().contains("locations")) {
|
|
|
|
QJsonValue locationsValue = dict["locations"];
|
|
|
|
if (locationsValue.isArray()) {
|
|
|
|
QVector<double> locations;
|
|
|
|
QJsonArray locationArray = locationsValue.toArray();
|
|
|
|
for (int i = 0; i != locationArray.size(); i++) {
|
|
|
|
locations.append(locationArray.at(i).toDouble());
|
|
|
|
}
|
|
|
|
view->setProperty("gradientLocations",
|
|
|
|
QVariant::fromValue(locations));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
view->setProperty("gradientColors", QVariant::fromValue(colors));
|
|
|
|
} else {
|
|
|
|
if (dict.keys().contains("start") && dict.keys().contains("end")) {
|
|
|
|
int start = dict["start"].toInt();
|
|
|
|
int end = dict["end"].toInt();
|
|
|
|
|
|
|
|
QVector<int> colors;
|
|
|
|
colors.append(start);
|
|
|
|
colors.append(end);
|
|
|
|
|
|
|
|
view->setProperty("gradientColors", QVariant::fromValue(colors));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-02-24 15:47:57 +08:00
|
|
|
} else if (name == "x") {
|
2021-04-15 10:20:55 +08:00
|
|
|
getLayouts()->setMarginLeft(prop.toDouble());
|
2021-02-24 15:47:57 +08:00
|
|
|
} else if (name == "y") {
|
2021-04-15 10:20:55 +08:00
|
|
|
getLayouts()->setMarginTop(prop.toDouble());
|
2021-02-24 16:23:45 +08:00
|
|
|
} else if (name == "corners") {
|
2021-04-30 09:47:04 +08:00
|
|
|
if (prop.isDouble()) {
|
|
|
|
view->setProperty("radius", prop.toDouble());
|
|
|
|
} else if (prop.isObject()) {
|
|
|
|
view->setProperty("radiusLeftTop", prop["leftTop"].toDouble());
|
|
|
|
view->setProperty("radiusRightTop", prop["rightTop"].toDouble());
|
|
|
|
view->setProperty("radiusLeftBottom", prop["leftBottom"].toDouble());
|
|
|
|
view->setProperty("radiusRightBottom", prop["rightBottom"].toDouble());
|
|
|
|
}
|
2021-03-19 18:02:17 +08:00
|
|
|
} else if (name == "onClick") {
|
|
|
|
if (prop.isString())
|
|
|
|
clickFunction = prop.toString();
|
2021-04-13 21:14:17 +08:00
|
|
|
} else if (name == "padding") {
|
2021-04-15 10:20:55 +08:00
|
|
|
getLayouts()->setPaddingLeft(prop["left"].toDouble());
|
|
|
|
getLayouts()->setPaddingRight(prop["right"].toDouble());
|
|
|
|
getLayouts()->setPaddingTop(prop["top"].toDouble());
|
|
|
|
getLayouts()->setPaddingBottom(prop["bottom"].toDouble());
|
2021-04-13 21:14:17 +08:00
|
|
|
} else if (name == "hidden") {
|
|
|
|
getLayouts()->setDisabled(prop.toBool());
|
2021-05-27 18:34:01 +08:00
|
|
|
mView->setVisible(!prop.toBool());
|
2021-04-19 14:05:21 +08:00
|
|
|
} else if (name == "border") {
|
|
|
|
qreal borderWidth = prop["width"].toDouble();
|
|
|
|
QString borderColor = DoricUtils::doricColor(prop["color"].toInt()).name();
|
|
|
|
view->setProperty("borderWidth", borderWidth);
|
|
|
|
view->setProperty("borderColor", borderColor);
|
2021-04-27 10:32:42 +08:00
|
|
|
} else if (name == "shadow") {
|
|
|
|
view->setProperty("shadowColor", QVariant::fromValue(DoricUtils::doricColor(
|
|
|
|
prop["color"].toInt())));
|
|
|
|
view->setProperty("shadowRadius", prop["radius"].toDouble());
|
|
|
|
view->setProperty("shadowOffsetX", prop["offsetX"].toDouble());
|
|
|
|
view->setProperty("shadowOffsetY", prop["offsetY"].toDouble());
|
|
|
|
view->setProperty("shadowOpacity", prop["opacity"].toDouble());
|
2021-04-07 10:41:34 +08:00
|
|
|
} else if (name != "layoutConfig") {
|
2021-05-31 20:10:42 +08:00
|
|
|
qCritical() << mType << " unhandled prop: " << name << ": "
|
|
|
|
<< prop.toString();
|
2021-02-19 19:01:31 +08:00
|
|
|
}
|
|
|
|
}
|
2021-03-19 18:02:17 +08:00
|
|
|
|
2021-04-13 21:14:17 +08:00
|
|
|
void DoricViewNode::afterBlended(QJsonValue prop) {}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2021-04-13 21:14:17 +08:00
|
|
|
void DoricViewNode::requestLayout() {}
|
|
|
|
|
2021-05-12 11:08:40 +08:00
|
|
|
std::shared_ptr<DoricAsyncResult>
|
|
|
|
DoricViewNode::callJSResponse(QString funcId, QVariantList args) {
|
2021-03-19 18:02:17 +08:00
|
|
|
QVariantList nArgs;
|
|
|
|
QList<QString> idList = getIdList();
|
2021-04-26 21:21:59 +08:00
|
|
|
nArgs.append(QVariant(idList));
|
2021-03-19 18:02:17 +08:00
|
|
|
nArgs.append(funcId);
|
|
|
|
foreach (const QVariant &arg, args)
|
|
|
|
nArgs.append(arg);
|
|
|
|
|
|
|
|
return getContext()->callEntity(DoricConstant::DORIC_ENTITY_RESPONSE, nArgs);
|
|
|
|
}
|
|
|
|
|
2021-05-12 11:08:40 +08:00
|
|
|
std::shared_ptr<DoricAsyncResult>
|
|
|
|
DoricViewNode::pureCallJSResponse(QString funcId, QVariantList args) {
|
2021-05-10 16:09:29 +08:00
|
|
|
QVariantList nArgs;
|
|
|
|
nArgs.append(getContext()->getContextId());
|
|
|
|
nArgs.append(DoricConstant::DORIC_ENTITY_RESPONSE);
|
|
|
|
QList<QString> idList = getIdList();
|
|
|
|
nArgs.append(QVariant(idList));
|
|
|
|
nArgs.append(funcId);
|
|
|
|
foreach (const QVariant &arg, args)
|
|
|
|
nArgs.append(arg);
|
|
|
|
|
2021-05-31 16:00:06 +08:00
|
|
|
return getContext()->getDriver()->invokeDoricMethod(
|
|
|
|
DoricConstant::DORIC_CONTEXT_INVOKE_PURE, nArgs);
|
2021-05-10 16:09:29 +08:00
|
|
|
}
|
|
|
|
|
2021-03-19 18:02:17 +08:00
|
|
|
void DoricViewNode::onClick() {
|
|
|
|
if (clickFunction.isEmpty()) {
|
2021-06-01 19:44:13 +08:00
|
|
|
if (mSuperNode != nullptr)
|
|
|
|
mSuperNode->onClick();
|
2021-03-19 18:02:17 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
QVariantList args;
|
|
|
|
callJSResponse(clickFunction, args);
|
|
|
|
}
|
2021-05-27 10:56:58 +08:00
|
|
|
|
|
|
|
QSizeF DoricViewNode::sizeThatFits(QSizeF size) {
|
|
|
|
return QSizeF(qMin(size.width(), mView->width()),
|
|
|
|
qMin(size.height(), mView->height()));
|
|
|
|
}
|