Пример #1
0
void PQBaseItem::serializeProperty(QTextStream &stream, const QDeclarativeProperty &property, unsigned indentSize) const
{
    QString str;
    QTextStream outStream(&str);
    if (property.propertyTypeCategory() == QDeclarativeProperty::Normal) { // Normal value property
        const int propertyType = property.propertyType();
        const QString propertyValue = property.read().toString();
        if (propertyType >= QVariant::Bool && propertyType <= QVariant::Double) { // Numerical, unquoted values
            outStream << propertyValue;
        } else {
            if (!propertyValue.isEmpty()) {
                QString value = propertyValue;
                value.replace(QLatin1Char('\\'), QLatin1String("\\\\")) // escape backslashes in rich text
                     .replace(QLatin1Char('"'), QLatin1String("\\\"")); // escape quotes
                value = QLatin1Char('"') % value % QLatin1Char('"');
                outStream << value;
            }
        }
    } else if (property.propertyTypeCategory() == QDeclarativeProperty::Object) { // Complex Object type
        const QVariant variantValue = property.read();
        if (variantValue.isValid() && !variantValue.isNull()) {
            serializeObject(outStream, variantValue.value<QObject *>(), indentSize);
        }
    } else if (property.propertyTypeCategory() == QDeclarativeProperty::List) { // List of objects
        const QDeclarativeListReference list = qvariant_cast<QDeclarativeListReference>(property.read());

        outStream << QLatin1Char('[') << endl;
        for (int itemIndex = 0, c = list.count(); itemIndex < c; ++itemIndex) {
            QObject *item = list.at(itemIndex);
            serializeObject(outStream, item, indentSize+1);
            if (itemIndex + 1 != list.count()) {
                outStream << QLatin1Char(','); // separator
            }
            outStream << endl;
        }
        outStream << PQSerializer::indentStep().repeated(indentSize) << QLatin1String("]"); // close list
    }

    if (!str.isEmpty()) {
        stream << PQSerializer::indentStep().repeated(indentSize) 
               << property.name() << QLatin1String(": ") << str
               << endl;
    }
}
static void removeObjectFromList(const QDeclarativeProperty &property, QObject *objectToBeRemoved, QDeclarativeEngine * engine)
{
    QDeclarativeListReference listReference(property.object(), property.name().toLatin1(), engine);

    if (!hasFullImplementedListInterface(listReference)) {
        qWarning() << "Property list interface not fully implemented for Class " << property.property().typeName() << " in property " << property.name() << "!";
        return;
    }

    int count = listReference.count();

    QObjectList objectList;

    for(int i = 0; i < count; i ++) {
        QObject *listItem = listReference.at(i);
        if (listItem != objectToBeRemoved)
            objectList.append(listItem);
    }

    listReference.clear();

    foreach(QObject *object, objectList)
        listReference.append(object);
}
void ObjectNodeInstance::deleteObjectsInList(const QDeclarativeProperty &property)
{
    QObjectList objectList;
    QDeclarativeListReference list = qvariant_cast<QDeclarativeListReference>(property.read());

    if (!hasFullImplementedListInterface(list)) {
        qWarning() << "Property list interface not fully implemented for Class " << property.property().typeName() << " in property " << property.name() << "!";
        return;
    }

    for(int i = 0; i < list.count(); i++) {
        objectList += list.at(i);
    }

    list.clear();
}