Exemplo n.º 1
0
static void parseHeaderFooterAttribute( KDReports::Header& header, const QDomElement& element )
{
    if ( element.hasAttribute( QLatin1String( "font" ) ) ||
        element.hasAttribute( QLatin1String( "pointsize" ) ) ) {
        const QFont font = parseFontAttributes( element );
        header.setDefaultFont( font );
    }
}
bool QDeclarativeStyledTextPrivate::parseTag(const QChar *&ch, const QString &textIn, QString &textOut, QTextCharFormat &format)
{
    skipSpace(ch);

    int tagStart = ch - textIn.constData();
    int tagLength = 0;
    while (!ch->isNull()) {
        if (*ch == greaterThan) {
            QStringRef tag(&textIn, tagStart, tagLength);
            const QChar char0 = tag.at(0);
            if (char0 == QLatin1Char('b')) {
                if (tagLength == 1)
                    format.setFontWeight(QFont::Bold);
                else if (tagLength == 2 && tag.at(1) == QLatin1Char('r')) {
                    textOut.append(QChar(QChar::LineSeparator));
                    return false;
                }
            } else if (char0 == QLatin1Char('i')) {
                if (tagLength == 1)
                    format.setFontItalic(true);
            }
            return true;
        } else if (ch->isSpace()) {
            // may have params.
            QStringRef tag(&textIn, tagStart, tagLength);
            if (tag == QLatin1String("font"))
                return parseFontAttributes(ch, textIn, format);
            if (*ch == greaterThan || ch->isNull())
                continue;
        } else if (*ch != slash){
            tagLength++;
        }
        ++ch;
    }

    return false;
}
Exemplo n.º 3
0
bool KDReports::XmlParser::processDocument( const QDomDocument& doc, KDReports::ReportBuilder* builder )
{
    // Top-level element should be <report>
    QDomElement docElement = doc.documentElement();
    if( docElement.tagName() != QLatin1String( "report" ) ) {
        error( QObject::tr("Expected \"<report>\" as the topmost element, but found \"<%1>\"").arg( docElement.tagName() ) );
        return false;
    }

    // Support for default page orientation
    if ( docElement.hasAttribute( QLatin1String( "orientation" ) ) ) {
        const QString orientation = docElement.attribute( QLatin1String( "orientation" ) );
        if ( orientation == QLatin1String( "landscape" ) )
            m_report->setOrientation( QPrinter::Landscape );
        else if ( orientation == QLatin1String( "portrait" ) )
            m_report->setOrientation( QPrinter::Portrait );
    }

    // Support for margins
    double marginTop = 20.0; // defaults are duplicated in KDReportsReport.cpp
    if ( docElement.hasAttribute( QLatin1String( "margin-top" ) ) )
        marginTop = docElement.attribute( QLatin1String( "margin-top" ) ).toDouble();
    double marginLeft = 20.0;
    if ( docElement.hasAttribute( QLatin1String( "margin-left" ) ) )
        marginLeft = docElement.attribute( QLatin1String( "margin-left" ) ).toDouble();
    double marginBottom = 20.0;
    if ( docElement.hasAttribute( QLatin1String( "margin-bottom" ) ) )
        marginBottom = docElement.attribute( QLatin1String( "margin-bottom" ) ).toDouble();
    double marginRight = 20.0;
    if ( docElement.hasAttribute( QLatin1String( "margin-right" ) ) )
        marginRight = docElement.attribute( QLatin1String( "margin-right" ) ).toDouble();
    m_report->setMargins( marginTop, marginLeft, marginBottom, marginRight );

    if ( docElement.hasAttribute( QLatin1String( "header-body-spacing" ) ) )
        m_report->setHeaderBodySpacing( docElement.attribute( QLatin1String( "header-body-spacing" ) ).toDouble());
    if ( docElement.hasAttribute( QLatin1String( "footer-body-spacing" ) ) )
        m_report->setFooterBodySpacing( docElement.attribute( QLatin1String( "footer-body-spacing" ) ).toDouble());

    // Support for default font
    if ( docElement.hasAttribute( QLatin1String( "font" ) ) ||
        docElement.hasAttribute( QLatin1String( "pointsize" ) ) ) {
        const QFont font = parseFontAttributes( docElement );
        m_report->setDefaultFont( font );
    }

    if ( m_xmlElementHandler
         && !m_xmlElementHandler->startReport( *m_report, docElement ) ) {
        if ( m_errorDetails )
            *m_errorDetails = m_xmlElementHandler->errorDetails();

        return false;
    }

    if ( !processNode( docElement, builder, false, false ) )
        return false;

    if ( m_xmlElementHandler )
        m_xmlElementHandler->endReport( *m_report, docElement );

    if ( testForErrorAndFillErrorDetails() )
        return false;

    return true;
}
bool QQuickStyledTextPrivate::parseTag(const QChar *&ch, const QString &textIn, QString &textOut, QTextCharFormat &format)
{
    skipSpace(ch);

    int tagStart = ch - textIn.constData();
    int tagLength = 0;
    while (!ch->isNull()) {
        if (*ch == greaterThan) {
            if (tagLength == 0)
                return false;
            QStringRef tag(&textIn, tagStart, tagLength);
            const QChar char0 = tag.at(0);
            if (char0 == QLatin1Char('b')) {
                if (tagLength == 1) {
                    format.setFontWeight(QFont::Bold);
                    return true;
                } else if (tagLength == 2 && tag.at(1) == QLatin1Char('r')) {
                    textOut.append(QChar(QChar::LineSeparator));
                    hasSpace = true;
                    prependSpace = false;
                    return false;
                }
            } else if (char0 == QLatin1Char('i')) {
                if (tagLength == 1) {
                    format.setFontItalic(true);
                    return true;
                }
            } else if (char0 == QLatin1Char('p')) {
                if (tagLength == 1) {
                    if (!hasNewLine)
                        textOut.append(QChar::LineSeparator);
                    hasSpace = true;
                    prependSpace = false;
                } else if (tag == QLatin1String("pre")) {
                    preFormat = true;
                    if (!hasNewLine)
                        textOut.append(QChar::LineSeparator);
                    format.setFontFamily(QString::fromLatin1("Courier New,courier"));
                    format.setFontFixedPitch(true);
                    return true;
                }
            } else if (char0 == QLatin1Char('u')) {
                if (tagLength == 1) {
                    format.setFontUnderline(true);
                    return true;
                } else if (tag == QLatin1String("ul")) {
                    List listItem;
                    listItem.level = 0;
                    listItem.type = Unordered;
                    listItem.format = Bullet;
                    listStack.push(listItem);
                }
            } else if (char0 == QLatin1Char('h') && tagLength == 2) {
                int level = tag.at(1).digitValue();
                if (level >= 1 && level <= 6) {
                    if (!hasNewLine)
                        textOut.append(QChar::LineSeparator);
                    hasSpace = true;
                    prependSpace = false;
                    setFontSize(7 - level, format);
                    format.setFontWeight(QFont::Bold);
                    return true;
                }
            } else if (tag == QLatin1String("strong")) {
                format.setFontWeight(QFont::Bold);
                return true;
            } else if (tag == QLatin1String("ol")) {
                List listItem;
                listItem.level = 0;
                listItem.type = Ordered;
                listItem.format = Decimal;
                listStack.push(listItem);
            } else if (tag == QLatin1String("li")) {
                if (!hasNewLine)
                    textOut.append(QChar(QChar::LineSeparator));
                if (!listStack.isEmpty()) {
                    int count = ++listStack.top().level;
                    for (int i = 0; i < listStack.size(); ++i)
                        textOut += QString(tabsize, QChar::Nbsp);
                    switch (listStack.top().format) {
                    case Decimal:
                        textOut += QString::number(count) % QLatin1Char('.');
                        break;
                    case LowerAlpha:
                        textOut += toAlpha(count, false) % QLatin1Char('.');
                        break;
                    case UpperAlpha:
                        textOut += toAlpha(count, true) % QLatin1Char('.');
                        break;
                    case LowerRoman:
                        textOut += toRoman(count, false) % QLatin1Char('.');
                        break;
                    case UpperRoman:
                        textOut += toRoman(count, true) % QLatin1Char('.');
                        break;
                    case Bullet:
                        textOut += bullet;
                        break;
                    case Disc:
                        textOut += disc;
                        break;
                    case Square:
                        textOut += square;
                        break;
                    }
                    textOut += QString(2, QChar::Nbsp);
                }
            }
            return false;
        } else if (ch->isSpace()) {
            // may have params.
            QStringRef tag(&textIn, tagStart, tagLength);
            if (tag == QLatin1String("font"))
                return parseFontAttributes(ch, textIn, format);
            if (tag == QLatin1String("ol")) {
                parseOrderedListAttributes(ch, textIn);
                return false; // doesn't modify format
            }
            if (tag == QLatin1String("ul")) {
                parseUnorderedListAttributes(ch, textIn);
                return false; // doesn't modify format
            }
            if (tag == QLatin1String("a")) {
                return parseAnchorAttributes(ch, textIn, format);
            }
            if (tag == QLatin1String("img")) {
                parseImageAttributes(ch, textIn, textOut);
                return false;
            }
            if (*ch == greaterThan || ch->isNull())
                continue;
        } else if (*ch != slash) {
            tagLength++;
        }
        ++ch;
    }
    return false;
}