예제 #1
0
bool ModelNode::setObjectValue(const QScriptValue& valuemap, bool writeToCache)
{
    bool emitItemsChanged = false;

    QScriptValueIterator it(valuemap);
    while (it.hasNext()) {
        it.next();
        ModelNode *prev = properties.value(it.name());
        ModelNode *value = new ModelNode(m_model);
        QScriptValue v = it.value();

        if (v.isArray()) {
            value->isArray = true;
            value->setListValue(v);
            if (writeToCache && objectCache)
                objectCache->setValue(it.name().toUtf8(), QVariant::fromValue(value->model(m_model)));
            emitItemsChanged = true;    // for now, too inefficient to check whether list and sublists have changed
        } else {
            value->values << v.toVariant();
            if (writeToCache && objectCache)
                objectCache->setValue(it.name().toUtf8(), value->values.last());
            if (!emitItemsChanged && prev && prev->values.count() == 1
                    && prev->values[0] != value->values.last()) {
                emitItemsChanged = true;
            }
        }
        if (properties.contains(it.name()))
            delete properties[it.name()];
        properties.insert(it.name(), value);
    }
    return emitItemsChanged;
}
void ModelNode::setListValue(const QScriptValue& valuelist) {
    QScriptValueIterator it(valuelist);
    values.clear();
    int size = valuelist.property(QLatin1String("length")).toInt32();
    for (int i=0; i<size; i++) {
        ModelNode *value = new ModelNode;
        QScriptValue v = valuelist.property(i);
        if (v.isArray()) {
            value->isArray = true;
            value->setListValue(v);
        } else if (v.isObject()) {
            value->setObjectValue(v);
        } else {
            value->values << v.toVariant();
        }
        values.append(qVariantFromValue(value));
    }
}
void ModelNode::setObjectValue(const QScriptValue& valuemap) {
    QScriptValueIterator it(valuemap);
    while (it.hasNext()) {
        it.next();
        ModelNode *value = new ModelNode;
        QScriptValue v = it.value();
        if (v.isArray()) {
            value->isArray = true;
            value->setListValue(v);
        } else {
            value->values << v.toVariant();
            if (objectCache)
                objectCache->setValue(it.name().toUtf8(), value->values.last());
        }
        if (properties.contains(it.name()))
            delete properties[it.name()];
        properties.insert(it.name(), value);
    }
}