void EntryAttributes::copyCustomKeysFrom(const EntryAttributes* other)
{
    if (!areCustomKeysDifferent(other)) {
        return;
    }

    Q_EMIT aboutToBeReset();

    // remove all non-default keys
    Q_FOREACH (const QString& key, keys()) {
        if (!isDefaultAttribute(key)) {
            m_attributes.remove(key);
            m_protectedAttributes.remove(key);
        }
    }

    Q_FOREACH (const QString& key, other->keys()) {
        if (!isDefaultAttribute(key)) {
            m_attributes.insert(key, other->value(key));
            if (other->isProtected(key)) {
                m_protectedAttributes.insert(key);
            }
        }
    }

    Q_EMIT reset();
    Q_EMIT modified();
}
void EntryAttributes::rename(const QString& oldKey, const QString& newKey)
{
    Q_ASSERT(!isDefaultAttribute(oldKey));
    Q_ASSERT(!isDefaultAttribute(newKey));

    if (!m_attributes.contains(oldKey)) {
        Q_ASSERT(false);
        return;
    }

    if (m_attributes.contains(newKey)) {
        Q_ASSERT(false);
        return;
    }

    QString data = value(oldKey);
    bool protect = isProtected(oldKey);

    Q_EMIT aboutToRename(oldKey, newKey);

    m_attributes.remove(oldKey);
    m_attributes.insert(newKey, data);
    if (protect) {
        m_protectedAttributes.remove(oldKey);
        m_protectedAttributes.insert(newKey);
    }

    Q_EMIT modified();
    Q_EMIT renamed(oldKey, newKey);
}
QList<QString> EntryAttributes::customKeys()
{
    QList<QString> customKeys;
    Q_FOREACH (const QString& key, keys()) {
        if (!isDefaultAttribute(key)) {
            customKeys.append(key);
        }
    }
    return customKeys;
}
void EntryAttributes::remove(const QString& key)
{
    Q_ASSERT(!isDefaultAttribute(key));

    if (!m_attributes.contains(key)) {
        Q_ASSERT(false);
        return;
    }

    Q_EMIT aboutToBeRemoved(key);

    m_attributes.remove(key);
    m_protectedAttributes.remove(key);

    Q_EMIT removed(key);
    Q_EMIT modified();
}
bool EntryAttributes::areCustomKeysDifferent(const EntryAttributes* other)
{
    // check if they are equal ignoring the order of the keys
    if (keys().toSet() != other->keys().toSet()) {
        return true;
    }

    Q_FOREACH (const QString& key, keys()) {
        if (isDefaultAttribute(key)) {
            continue;
        }

        if (isProtected(key) != other->isProtected(key) || value(key) != other->value(key)) {
            return true;
        }
    }

    return false;
}
void EntryAttributes::set(const QString& key, const QString& value, bool protect)
{
    bool emitModified = false;

    bool addAttribute = !m_attributes.contains(key);
    bool changeValue = !addAttribute && (m_attributes.value(key) != value);
    bool defaultAttribute = isDefaultAttribute(key);

    if (addAttribute && !defaultAttribute) {
        Q_EMIT aboutToBeAdded(key);
    }

    if (addAttribute || changeValue) {
        m_attributes.insert(key, value);
        emitModified = true;
    }

    if (protect) {
        if (!m_protectedAttributes.contains(key)) {
            emitModified = true;
        }
        m_protectedAttributes.insert(key);
    }
    else if (m_protectedAttributes.remove(key)) {
        emitModified = true;
    }

    if (emitModified) {
        Q_EMIT modified();
    }

    if (defaultAttribute && changeValue) {
        Q_EMIT defaultKeyModified();
    }
    else if (addAttribute) {
        Q_EMIT added(key);
    }
    else if (emitModified) {
        Q_EMIT customKeyModified(key);
    }
}
示例#7
0
void SvgParser::parseElement()
{
    bool hasAttributes = true;
    int nameLength = 0;
    while (!atEnd()) {
        // check is element name ends with end tag
        // namely do not have child elements and attributes
        EndTagType endType = isEndTag(false);
        if (endType != NotEnd) {
            hasAttributes = false;
            m_name = QString(str-nameLength, nameLength);
            if (endType == EndType1)
                str++;
            else if (endType == EndType2) {
                str += 2;
                m_isPrevElemEnded = true;
            }
            break;
        }
        // if char is space than node name is ended
        if (StringWalker::isSpace(str->unicode())) {
            m_name = QString(str-nameLength, nameLength);

            // check is element has end char after spaces
            // and not attributes
            skipSpaces();
            endType = isEndTag();
            if (endType != NotEnd) {
                if (endType == EndType2)
                    m_isPrevElemEnded = true;
                hasAttributes = false;
            }
            break;
        }
        nameLength++;
        str++;
    }

    if (!hasAttributes)
        return;

    // parse attributes

    // reserve memory for attributes
    // 6 - is average attributes count
    m_attrHash.reserve(6);
    QChar quote;
    QString attrName;
    while (!atEnd()) {
        nameLength = 0;
        skipSpaces();
        // data between ' ' and '=' is attribute name
        while (!atEnd() && *str != QL1C('=')) {
            nameLength++;
            ++str;
        }
        // ignore spaces in attribute name
        attrName.clear();

        uint attrId = hash(str-nameLength, nameLength);
        if (!isDefaultAttribute(attrId))
            attrName = QString(str-nameLength, nameLength);

        // skip '='
        str++;

        skipSpaces();

        if (!atEnd() && (*str == QL1C('\"') || *str == QL1C('\''))) {
            quote = *str;
            str++;
        }
        // data between quotes is attribute value
        nameLength = 0;
        while (!atEnd() && *str != quote) {
            nameLength++;
            str++;
        }

        // ignore empty attributes
        if (nameLength > 0) {
            if (   attrId == AttrId::transform
                || attrId == AttrId::gradientTransform
                || attrId == AttrId::patternTransform)
            {
                Transform ts(str-nameLength, nameLength);
                if (ts.isValid())
                    m_attrHash.insert(AttrId::transform, SvgAttribute(ts));
            } else {
                QString attrValue = QString(str-nameLength, nameLength);
                if (attrName.isEmpty()) {
                    if (attrId == AttrId::d)
                        m_attrHash.insert(AttrId::d,
                                          SvgAttribute(PathSegmentList(), attrValue));
                    else
                        m_attrHash.insert(attrId, SvgAttribute(attrId, attrValue));
                } else {
                    m_attrHash.insert(attrId, SvgAttribute(attrName, attrValue));
                }
            }
        }

        // skip quote char
        str++;
        skipSpaces();

        EndTagType endType = isEndTag();
        if (endType != NotEnd) {
            if (endType == EndType2)
                m_isPrevElemEnded = true;
            break;
        }
    }
}