void KoTextWriter::Private::writeBlocks(QTextDocument *document, int from, int to, QHash<QTextList *, QString> &listStyles, QTextTable *currentTable, QTextList *currentList)
{
    pairedInlineObjectsStackStack.push(currentPairedInlineObjectsStack);
    currentPairedInlineObjectsStack = new QStack<KoInlineObject*>();
    QTextBlock block = document->findBlock(from);
    int sectionLevel = 0;

    while (block.isValid() && ((to == -1) || (block.position() <= to))) {

        QTextCursor cursor(block);

        int frameType = cursor.currentFrame()->format().intProperty(KoText::SubFrameType);
        if (frameType == KoText::AuxillaryFrameType) {
            break; // we've reached the "end" (end/footnotes saved by themselves)
                   // note how NoteFrameType passes through here so the notes can
                   // call writeBlocks to save their contents.
        }

        QTextBlockFormat format = block.blockFormat();
        if (format.hasProperty(KoParagraphStyle::SectionStartings)) {
            QVariant v = format.property(KoParagraphStyle::SectionStartings);
            QList<QVariant> sectionStarts = v.value<QList<QVariant> >();

            foreach (const QVariant &sv, sectionStarts) {
                KoSection* section = (KoSection*)(sv.value<void*>());
                if (section) {
                    ++sectionLevel;
                    section->saveOdf(context);
                }
            }
        }
void TextContentsModelImpl::documentLayoutFinished()
{
    QTextBlock block = d->textDocument->firstBlock();
    d->entries.clear();

    while (block.isValid())
    {
        QTextBlockFormat format = block.blockFormat();
        if (format.hasProperty(KoParagraphStyle::OutlineLevel))
        {
            ContentsEntry entry;
            entry.title = block.text();
            entry.level = format.intProperty(KoParagraphStyle::OutlineLevel);

            auto rootArea = d->layout->rootAreaForPosition(block.position());
            if (rootArea) {
                if (rootArea->page()) {
                    entry.pageNumber = rootArea->page()->visiblePageNumber();
                    entry.page = static_cast<KWPage*>(rootArea->page());
                }
            }

            d->entries.append(entry);
        }
        block = block.next();
    }

    emit listContentsCompleted();
}
Example #3
0
void tst_QTextFormat::defaultAlignment()
{
    QTextBlockFormat fmt;
    QVERIFY(!fmt.hasProperty(QTextFormat::BlockAlignment));
    QCOMPARE(fmt.intProperty(QTextFormat::BlockAlignment), 0);
    QVERIFY(fmt.alignment() == Qt::AlignLeft);
}
void TextBrowserHelpWidget::setSource(const QUrl &name)
{
    QTextBrowser::setSource(name);

    QTextCursor cursor(document());
    while (!cursor.atEnd()) {
        QTextBlockFormat fmt = cursor.blockFormat();
        if (fmt.hasProperty(QTextFormat::LineHeightType) && fmt.lineHeightType() == QTextBlockFormat::FixedHeight) {
           fmt.setProperty(QTextFormat::LineHeightType, QTextBlockFormat::MinimumHeight);
           cursor.setBlockFormat(fmt);
        }
        if (!cursor.movePosition(QTextCursor::NextBlock))
            break;
    }
}
void CreateBlogMsg::blockQuote()
{
    QTextBlockFormat blockFormat = ui.msgEdit->textCursor().blockFormat();
    QTextBlockFormat f;

    if ( blockFormat.hasProperty( TextFormat::IsBlockQuote ) && 
         blockFormat.boolProperty( TextFormat::IsBlockQuote ) ) {
        f.setProperty( TextFormat::IsBlockQuote, QVariant( false ) );
        f.setLeftMargin( 0 );
        f.setRightMargin( 0 );
    } else {
        f.setProperty( TextFormat::IsBlockQuote, QVariant( true ) );
        f.setLeftMargin( 40 );
        f.setRightMargin( 40 );
    }
    ui.msgEdit->textCursor().mergeBlockFormat( f );
}
Example #6
0
void TestStyles::testUnapplyStyle()
{
    // Used to test OverlineColor style
    QColor testOverlineColor(255, 128, 64);
    KoCharacterStyle::LineWeight testOverlineWeight = KoCharacterStyle::ThickLineWeight;
    qreal testOverlineWidth = 1.5;

    // in this test we should avoid testing any of the hardcodedDefaultProperties; see KoCharacterStyle for details!
    KoParagraphStyle headers;
    headers.setOverlineColor(testOverlineColor);
    headers.setOverlineMode(KoCharacterStyle::ContinuousLineMode);
    headers.setOverlineStyle(KoCharacterStyle::DottedLine);
    headers.setOverlineType(KoCharacterStyle::DoubleLine);
    headers.setOverlineWidth(testOverlineWeight, testOverlineWidth);
    headers.setFontWeight(QFont::Bold);
    headers.setAlignment(Qt::AlignCenter);
    KoParagraphStyle head1;
    head1.setParentStyle(&headers);
    head1.setLeftMargin(QTextLength(QTextLength::FixedLength, 40));

    QTextDocument doc;
    doc.setPlainText("abc");
    QTextBlock block = doc.begin();
    head1.applyStyle(block);

    QTextCursor cursor(block);
    QTextBlockFormat bf = cursor.blockFormat();
    KoParagraphStyle bfStyle (bf, cursor.charFormat());
    QCOMPARE(bf.alignment(), Qt::AlignCenter);
    QCOMPARE(bfStyle.leftMargin(), 40.);
    QTextCharFormat cf = cursor.charFormat();
    QCOMPARE(cf.colorProperty(KoCharacterStyle::OverlineColor), testOverlineColor);
    QCOMPARE(cf.intProperty(KoCharacterStyle::OverlineMode), (int) KoCharacterStyle::ContinuousLineMode);
    QCOMPARE(cf.intProperty(KoCharacterStyle::OverlineStyle), (int) KoCharacterStyle::DottedLine);
    QCOMPARE(cf.intProperty(KoCharacterStyle::OverlineType), (int) KoCharacterStyle::DoubleLine);
    QCOMPARE(cf.intProperty(KoCharacterStyle::OverlineWeight), (int) testOverlineWeight);
    QCOMPARE(cf.doubleProperty(KoCharacterStyle::OverlineWidth), testOverlineWidth);

    head1.unapplyStyle(block);
    bf = cursor.blockFormat();
    QCOMPARE(bf.hasProperty(QTextFormat::BlockAlignment), false);
    QCOMPARE(bf.hasProperty(QTextFormat::BlockLeftMargin), false);
    cf = cursor.charFormat();
    QCOMPARE(cf.hasProperty(KoCharacterStyle::OverlineColor), false);
    QCOMPARE(cf.hasProperty(KoCharacterStyle::OverlineMode), false);
    QCOMPARE(cf.hasProperty(KoCharacterStyle::OverlineStyle), false);
    QCOMPARE(cf.hasProperty(KoCharacterStyle::OverlineType), false);
    QCOMPARE(cf.hasProperty(KoCharacterStyle::OverlineWeight), false);
    QCOMPARE(cf.hasProperty(KoCharacterStyle::OverlineWidth), false);

    doc.clear();
    block = doc.begin();
    head1.applyStyle(block);
    bf = cursor.blockFormat();
    KoParagraphStyle bfStyle2 (bf, cursor.charFormat());
    QCOMPARE(bf.alignment(), Qt::AlignCenter);
    QCOMPARE(bfStyle2.leftMargin(), 40.);
    cf = cursor.charFormat();
    //QCOMPARE(cf.fontOverline(), true);
    QCOMPARE(cf.colorProperty(KoCharacterStyle::OverlineColor), testOverlineColor);
    QCOMPARE(cf.intProperty(KoCharacterStyle::OverlineMode), (int) KoCharacterStyle::ContinuousLineMode);
    QCOMPARE(cf.intProperty(KoCharacterStyle::OverlineStyle), (int) KoCharacterStyle::DottedLine);
    QCOMPARE(cf.intProperty(KoCharacterStyle::OverlineType), (int) KoCharacterStyle::DoubleLine);
    QCOMPARE(cf.intProperty(KoCharacterStyle::OverlineWeight), (int) testOverlineWeight);
    QCOMPARE(cf.doubleProperty(KoCharacterStyle::OverlineWidth), testOverlineWidth);


    head1.unapplyStyle(block);
    bf = cursor.blockFormat();
    QCOMPARE(bf.hasProperty(QTextFormat::BlockAlignment), false);
    QCOMPARE(bf.hasProperty(QTextFormat::BlockLeftMargin), false);
    cf = cursor.charFormat();
    //QCOMPARE(cf.hasProperty(QTextFormat::FontOverline), false);

    doc.setHtml("bla bla<i>italic</i>enzo");

    block = doc.begin();
    head1.applyStyle(block);
    bf = cursor.blockFormat();
    KoParagraphStyle bfStyle3(bf, cursor.charFormat());
    QCOMPARE(bf.alignment(), Qt::AlignCenter);
    QCOMPARE(bfStyle3.leftMargin(), 40.);
    cf = cursor.charFormat();
    //QCOMPARE(cf.fontOverline(), true);
    QCOMPARE(cf.hasProperty(KoCharacterStyle::OverlineColor), true);
    QCOMPARE(cf.hasProperty(KoCharacterStyle::OverlineMode), true);
    QCOMPARE(cf.hasProperty(KoCharacterStyle::OverlineStyle), true);
    QCOMPARE(cf.hasProperty(KoCharacterStyle::OverlineType), true);
    QCOMPARE(cf.hasProperty(KoCharacterStyle::OverlineWeight), true);
    QCOMPARE(cf.hasProperty(KoCharacterStyle::OverlineWidth), true);

    cursor.setPosition(7);
    cursor.setPosition(12, QTextCursor::KeepAnchor);
    QTextCharFormat italic;
    italic.setFontItalic(true);
    cursor.mergeCharFormat(italic);
    cursor.setPosition(8);
    cf = cursor.charFormat();
    QCOMPARE(cf.fontItalic(), true);
    cursor.setPosition(0);

    head1.unapplyStyle(block);
    cursor.setPosition(0);
    bf = cursor.blockFormat();
    QCOMPARE(bf.hasProperty(QTextFormat::BlockAlignment), false);
    QCOMPARE(bf.hasProperty(QTextFormat::BlockLeftMargin), false);
    cf = cursor.charFormat();
    //QCOMPARE(cf.hasProperty(QTextFormat::FontOverline), false);
    QCOMPARE(cf.hasProperty(KoCharacterStyle::OverlineColor), false);
    QCOMPARE(cf.hasProperty(KoCharacterStyle::OverlineMode), false);
    QCOMPARE(cf.hasProperty(KoCharacterStyle::OverlineStyle), false);
    QCOMPARE(cf.hasProperty(KoCharacterStyle::OverlineType), false);
    QCOMPARE(cf.hasProperty(KoCharacterStyle::OverlineWeight), false);
    QCOMPARE(cf.hasProperty(KoCharacterStyle::OverlineWidth), false);


    cursor.setPosition(8);
    cf = cursor.charFormat();
    //QCOMPARE(cf.hasProperty(QTextFormat::FontOverline), false);
    QCOMPARE(cf.hasProperty(KoCharacterStyle::OverlineColor), false);
    QCOMPARE(cf.hasProperty(KoCharacterStyle::OverlineMode), false);
    QCOMPARE(cf.hasProperty(KoCharacterStyle::OverlineStyle), false);
    QCOMPARE(cf.hasProperty(KoCharacterStyle::OverlineType), false);
    QCOMPARE(cf.hasProperty(KoCharacterStyle::OverlineWeight), false);
    QCOMPARE(cf.hasProperty(KoCharacterStyle::OverlineWidth), false);
    QCOMPARE(cf.fontItalic(), true);
    cursor.setPosition(12);
    cf = cursor.charFormat();
    //QCOMPARE(cf.hasProperty(QTextFormat::FontOverline), false);
    QCOMPARE(cf.hasProperty(KoCharacterStyle::OverlineColor), false);
    QCOMPARE(cf.hasProperty(KoCharacterStyle::OverlineMode), false);
    QCOMPARE(cf.hasProperty(KoCharacterStyle::OverlineStyle), false);
    QCOMPARE(cf.hasProperty(KoCharacterStyle::OverlineType), false);
    QCOMPARE(cf.hasProperty(KoCharacterStyle::OverlineWeight), false);
    QCOMPARE(cf.hasProperty(KoCharacterStyle::OverlineWidth), false);
    QCOMPARE(cf.fontItalic(), true);

    cursor.setPosition(13);
    cf = cursor.charFormat();
    //QCOMPARE(cf.hasProperty(QTextFormat::FontOverline), false);
    QCOMPARE(cf.hasProperty(KoCharacterStyle::OverlineColor), false);
    QCOMPARE(cf.hasProperty(KoCharacterStyle::OverlineMode), false);
    QCOMPARE(cf.hasProperty(KoCharacterStyle::OverlineStyle), false);
    QCOMPARE(cf.hasProperty(KoCharacterStyle::OverlineType), false);
    QCOMPARE(cf.hasProperty(KoCharacterStyle::OverlineWeight), false);
    QCOMPARE(cf.hasProperty(KoCharacterStyle::OverlineWidth), false);
    QCOMPARE(cf.hasProperty(QTextFormat::FontWeight), false);
    QCOMPARE(cf.hasProperty(QTextFormat::FontItalic), false);
}
Example #7
0
void QTextOdfWriter::writeBlockFormat(QXmlStreamWriter &writer, QTextBlockFormat format, int formatIndex) const
{
    writer.writeStartElement(styleNS, QString::fromLatin1("style"));
    writer.writeAttribute(styleNS, QString::fromLatin1("name"), QString::fromLatin1("p%1").arg(formatIndex));
    writer.writeAttribute(styleNS, QString::fromLatin1("family"), QString::fromLatin1("paragraph"));
    writer.writeStartElement(styleNS, QString::fromLatin1("paragraph-properties"));

    if (format.hasProperty(QTextFormat::BlockAlignment)) {
        const Qt::Alignment alignment = format.alignment() & Qt::AlignHorizontal_Mask;
        QString value;
        if (alignment == Qt::AlignLeading)
            value = QString::fromLatin1("start");
        else if (alignment == Qt::AlignTrailing)
            value = QString::fromLatin1("end");
        else if (alignment == (Qt::AlignLeft | Qt::AlignAbsolute))
            value = QString::fromLatin1("left");
        else if (alignment == (Qt::AlignRight | Qt::AlignAbsolute))
            value = QString::fromLatin1("right");
        else if (alignment == Qt::AlignHCenter)
            value = QString::fromLatin1("center");
        else if (alignment == Qt::AlignJustify)
            value = QString::fromLatin1("justify");
        else
            qWarning() << "QTextOdfWriter: unsupported paragraph alignment; " << format.alignment();
        if (! value.isNull())
            writer.writeAttribute(foNS, QString::fromLatin1("text-align"), value);
    }

    if (format.hasProperty(QTextFormat::BlockTopMargin))
        writer.writeAttribute(foNS, QString::fromLatin1("margin-top"), pixelToPoint(qMax(qreal(0.), format.topMargin())) );
    if (format.hasProperty(QTextFormat::BlockBottomMargin))
        writer.writeAttribute(foNS, QString::fromLatin1("margin-bottom"), pixelToPoint(qMax(qreal(0.), format.bottomMargin())) );
    if (format.hasProperty(QTextFormat::BlockLeftMargin) || format.hasProperty(QTextFormat::BlockIndent))
        writer.writeAttribute(foNS, QString::fromLatin1("margin-left"), pixelToPoint(qMax(qreal(0.),
            format.leftMargin() + format.indent())));
    if (format.hasProperty(QTextFormat::BlockRightMargin))
        writer.writeAttribute(foNS, QString::fromLatin1("margin-right"), pixelToPoint(qMax(qreal(0.), format.rightMargin())) );
    if (format.hasProperty(QTextFormat::TextIndent))
        writer.writeAttribute(foNS, QString::fromLatin1("text-indent"), pixelToPoint(format.textIndent()));
    if (format.hasProperty(QTextFormat::PageBreakPolicy)) {
        if (format.pageBreakPolicy() & QTextFormat::PageBreak_AlwaysBefore)
            writer.writeAttribute(foNS, QString::fromLatin1("break-before"), QString::fromLatin1("page"));
        if (format.pageBreakPolicy() & QTextFormat::PageBreak_AlwaysAfter)
            writer.writeAttribute(foNS, QString::fromLatin1("break-after"), QString::fromLatin1("page"));
    }
    if (format.hasProperty(QTextFormat::BackgroundBrush)) {
        QBrush brush = format.background();
        writer.writeAttribute(foNS, QString::fromLatin1("background-color"), brush.color().name());
    }
    if (format.hasProperty(QTextFormat::BlockNonBreakableLines))
        writer.writeAttribute(foNS, QString::fromLatin1("keep-together"),
                format.nonBreakableLines() ? QString::fromLatin1("true") : QString::fromLatin1("false"));
    if (format.hasProperty(QTextFormat::TabPositions)) {
        QList<QTextOption::Tab> tabs = format.tabPositions();
        writer.writeStartElement(styleNS, QString::fromLatin1("tab-stops"));
        QList<QTextOption::Tab>::Iterator iterator = tabs.begin();
        while(iterator != tabs.end()) {
            writer.writeEmptyElement(styleNS, QString::fromLatin1("tab-stop"));
            writer.writeAttribute(styleNS, QString::fromLatin1("position"), pixelToPoint(iterator->position) );
            QString type;
            switch(iterator->type) {
            case QTextOption::DelimiterTab: type = QString::fromLatin1("char"); break;
            case QTextOption::LeftTab: type = QString::fromLatin1("left"); break;
            case QTextOption::RightTab: type = QString::fromLatin1("right"); break;
            case QTextOption::CenterTab: type = QString::fromLatin1("center"); break;
            }
            writer.writeAttribute(styleNS, QString::fromLatin1("type"), type);
            if (iterator->delimiter != 0)
                writer.writeAttribute(styleNS, QString::fromLatin1("char"), iterator->delimiter);
            ++iterator;
        }

        writer.writeEndElement(); // tab-stops
    }

    writer.writeEndElement(); // paragraph-properties
    writer.writeEndElement(); // style
}
bool QTextHtmlImporter::appendNodeText()
{
    const int initialCursorPosition = cursor.position();
    QTextCharFormat format = currentNode->charFormat;

    if (isPreservingWhitespaceMode(wsm))
        compressNextWhitespace = false;

    QString text = currentNode->text;

    QString textToInsert;
    textToInsert.reserve(text.size());

    for (int i = 0; i < text.length(); ++i) {
        QChar ch = text.at(i);

        if (ch.isSpace()
            && ch != QChar::Nbsp
            && ch != QChar::ParagraphSeparator) {

            if (compressNextWhitespace)
                continue;

            if (wsm == QTextHtmlParserNode::WhiteSpacePre
                || textEditMode
               ) {
                if (ch == QLatin1Char('\n')) {
                    if (textEditMode)
                        continue;
                } else if (ch == QLatin1Char('\r')) {
                    continue;
                }
            } else if (wsm != QTextHtmlParserNode::WhiteSpacePreWrap) {
                compressNextWhitespace = true;
                if (wsm == QTextHtmlParserNode::WhiteSpaceNoWrap)
                    ch = QChar::Nbsp;
                else
                    ch = QLatin1Char(' ');
            }
        } else {
            compressNextWhitespace = false;
        }

        if (ch == QLatin1Char('\n')
            || ch == QChar::ParagraphSeparator) {

            if (!textToInsert.isEmpty()) {
                cursor.insertText(textToInsert, format);
                textToInsert.clear();
            }

            QTextBlockFormat fmt = cursor.blockFormat();

            if (fmt.hasProperty(QTextFormat::BlockBottomMargin)) {
                QTextBlockFormat tmp = fmt;
                tmp.clearProperty(QTextFormat::BlockBottomMargin);
                cursor.setBlockFormat(tmp);
            }

            fmt.clearProperty(QTextFormat::BlockTopMargin);
            appendBlock(fmt, cursor.charFormat());
        } else {
            if (!namedAnchors.isEmpty()) {
                if (!textToInsert.isEmpty()) {
                    cursor.insertText(textToInsert, format);
                    textToInsert.clear();
                }

                format.setAnchor(true);
                format.setAnchorNames(namedAnchors);
                cursor.insertText(ch, format);
                namedAnchors.clear();
                format.clearProperty(QTextFormat::IsAnchor);
                format.clearProperty(QTextFormat::AnchorName);
            } else {
                textToInsert += ch;
            }
        }
    }

    if (!textToInsert.isEmpty()) {
        cursor.insertText(textToInsert, format);
    }

    return cursor.position() != initialCursorPosition;
}
bool IndexGeneratorManager::generate()
{
    if (m_state == Resting || m_state == FirstRunLayouting || m_state == SecondRunLayouting) {
        return false;
    }

    if (m_state == FirstRun || m_state == SecondRun) {
        return true;
    }

    if (m_document->characterCount() < 2) {
        return false;
    }

    if (m_state == FirstRunNeeded) {
        m_state = FirstRun;
    }

    if (m_state == SecondRunNeeded) {
        m_state = SecondRun;
    }

    QTextBlock block = m_document->firstBlock();

    bool success = true;
    while (block.isValid()) {
        QTextBlockFormat format = block.blockFormat();

        if (format.hasProperty(KoParagraphStyle::TableOfContentsData)) {
            QVariant data = format.property(KoParagraphStyle::TableOfContentsData);
            KoTableOfContentsGeneratorInfo *tocInfo = data.value<KoTableOfContentsGeneratorInfo *>();

            data = format.property(KoParagraphStyle::GeneratedDocument);
            QTextDocument *tocDocument = data.value<QTextDocument *>();

            ToCGenerator *generator = m_generators[tocInfo];
            if (!generator) {
                generator = new ToCGenerator(tocDocument, tocInfo);
                m_generators[tocInfo] = generator;
            }

            generator->setBlock(block);
            success &= generator->generate();
        }
        block = block.next();
    }


    if (m_state == FirstRun) {
        m_state = FirstRunLayouting;
    }

    if (m_state == SecondRun) {
        if (success) {
            m_state = SecondRunLayouting;
        } else {
            m_state = FirstRunLayouting;
        }
    }

    return false;
}
Example #10
0
void HtmlExporter::emitBlock( const QTextBlock &block )
{
    // save and later restore, in case we 'change' the default format by
    // emitting block char format information

    // NOTE the bottom line is commented, to use default charFormat, which can be set from outside.
    //QTextCharFormat oldDefaultCharFormat = defaultCharFormat;

    QString blockTag;
    bool isBlockQuote = false;
    const QTextBlockFormat blockFormat = block.blockFormat();

    if ( blockFormat.hasProperty( BilboTextFormat::IsBlockQuote ) && 
         blockFormat.boolProperty( BilboTextFormat::IsBlockQuote ) ) {
        isBlockQuote = true;
    }
    QTextList *list = block.textList();
    if ( list ) {
        if ( list->itemNumber( block ) == 0 ) { // first item? emit <ul> or appropriate
//    qDebug() << "first item" << endl;
            if ( isBlockQuote ) {
                html += QLatin1String( "<blockquote>" );
            }
            const QTextListFormat format = list->format();
            const int style = format.style();
            switch ( style ) {
                case QTextListFormat::ListDecimal:
                    html += QLatin1String( "<ol" );
                    break;
                case QTextListFormat::ListDisc:
                    html += QLatin1String( "<ul" );
                    break;
                case QTextListFormat::ListCircle:
                    html += QLatin1String( "<ul type=\"circle\"" );
                    break;
                case QTextListFormat::ListSquare:
                    html += QLatin1String( "<ul type=\"square\"" );
                    break;
                case QTextListFormat::ListLowerAlpha:
                    html += QLatin1String( "<ol type=\"a\"" );
                    break;
                case QTextListFormat::ListUpperAlpha:
                    html += QLatin1String( "<ol type=\"A\"" );
                    break;
                default:
                    html += QLatin1String( "<ul" ); // ### should not happen
                    //qDebug() << html;
            }
            /*
            if (format.hasProperty(QTextFormat::ListIndent)) {
                html += QLatin1String(" style=\"-qt-list-indent: ");
                html += QString::number(format.indent());
                html += QLatin1String(";\"");
            }*/

            html += QLatin1Char( '>' );
        }
        blockTag = QLatin1String( "li" );
//         html += QLatin1String( "<li " );
    }

//     const QTextBlockFormat blockFormat = block.blockFormat();
    if ( blockFormat.hasProperty( QTextFormat::BlockTrailingHorizontalRulerWidth ) ) { 
        if ( ( blockFormat.hasProperty( BilboTextFormat::IsHtmlTagSign ) ) && 
            ( blockFormat.boolProperty( BilboTextFormat::IsHtmlTagSign ) ) ) {
            html += QLatin1String( "<!--split-->" );
            return;
        } else {
            html += QLatin1String( "<hr" );
    
            QTextLength width = blockFormat.lengthProperty( QTextFormat::BlockTrailingHorizontalRulerWidth );
            if ( width.type() != QTextLength::VariableLength ) {
                emitTextLength( "width", width );
            } else {
                html += QLatin1Char( ' ' );
            }
    
            html += QLatin1String( "/>" );
            return;
        }
    }

    const bool pre = blockFormat.nonBreakableLines();
    if ( pre ) {
//   qDebug() << "NonBreakable lines" << endl;
//         if (list) {
//             html += QLatin1Char('>');
//   }
//         html += QLatin1String( "<pre" );
//         emitBlockAttributes( block );
//         html += QLatin1Char( '>' );
        blockTag = QLatin1String( "pre" );

    } else {
        if (!list) {
            if ( isBlockQuote ) {
                html += QLatin1String( "<blockquote>" );
            }

            if ( ( blockFormat.hasProperty( BilboTextFormat::HtmlHeading ) ) && (
                blockFormat.intProperty( BilboTextFormat::HtmlHeading ) ) ) {
                const int index = blockFormat.intProperty( BilboTextFormat::HtmlHeading );
                blockTag = QLatin1Char( 'h' ) + QString::number( index );
            } else {
                //html += QLatin1String("<div");
//                 html += QLatin1String( "<p" );
                blockTag = QLatin1String( "p" );
            }
        }
    }
    if ( !blockTag.isEmpty() ) {
        html += QLatin1Char( '<' ) + blockTag;
        emitBlockAttributes( block );
        html += QLatin1Char( '>' );
    }

    QTextBlock::Iterator it = block.begin();

    for ( ; !it.atEnd(); ++it ) {
        emitFragment( it.fragment(), blockFormat );
    }

    if ( !blockTag.isEmpty() ) {
        html += QLatin1String( "</" ) + blockTag + QLatin1String( ">\n" );
    }

//     if ( pre ) {
//         html += QLatin1String( "</pre>\n" );
//     } else {
//         if ( list ) {
//             html += QLatin1String( "</li>\n" );
//         } else {
//             if ( blockFormat::boolProperty( BilboTextFormat::IsHtmlHeading ) ) {
//                 const int index = format.intProperty( QTextFormat::FontSizeAdjustment );
//                 switch ( index ) {
//                     case -2:
//                        html += QLatin1String( "</h6>" );
//                        break;
//                     case -1:
//                        html += QLatin1String( "</h5>" );
//                        break;
//                     case 0:
//                        html += QLatin1String( "</h4>" );
//                        break;
//                     case 1:
//                        html += QLatin1String( "</h3>" );
//                        break;
//                     case 2:
//                        html += QLatin1String( "<h2" );
//                        break;
//                     case 3:
//                        html += QLatin1String( "<h1" );
//                        break;
//                 }
//             } else {
//                 html += QLatin1String( "</p>\n" );
//             }
//         }
//     }
    // HACK html.replace( QRegExp("<br[\\s]*/>[\\n]*<br[\\s]*/>[\\n]*"),"<br />&nbsp;<br />" );

    if ( list ) {
        if ( list->itemNumber( block ) == list->count() - 1 ) { // last item? close list
            if ( isOrderedList( list->format().style() ) ) {
                html += QLatin1String( "</ol>\n" );
            } else {
                html += QLatin1String( "</ul>\n" );
            }
            if ( isBlockQuote ) {
                html += QLatin1String( "</blockquote>\n" );
            }
        }
    } else {
        if ( isBlockQuote ) {
            html += QLatin1String( "</blockquote>\n" );
        }
    }
    // NOTE the bottom line is commented, to use default charFormat, which can be set from outside.
    //defaultCharFormat = oldDefaultCharFormat;
}
Example #11
0
void HtmlExporter::emitBlockAttributes( const QTextBlock &block )
{
//     kDebug() << "html" << html;
    QTextBlockFormat format = block.blockFormat();

    if (format.hasProperty( QTextFormat::LayoutDirection ) ) {
    Qt::LayoutDirection dir = format.layoutDirection();
//  if (dir == Qt::LeftToRight) {
//   mDefaultBlockFormat.setAlignment(Qt::AlignLeft);
//  } else {
//   mDefaultBlockFormat.setAlignment(Qt::AlignRight);
//  }

//     if ( dir != mDefaultBlockFormat.layoutDirection() ) {
        // assume default to not bloat the html too much
        if ( dir == Qt::LeftToRight ) {
            html += QLatin1String( " dir=\"ltr\"" );
//    mDefaultBlockFormat.setAlignment(Qt::AlignLeft);
        } else {
            html += QLatin1String( " dir=\"rtl\"" );
//    mDefaultBlockFormat.setAlignment(Qt::AlignRight);
        }
    }

    if ( format.hasProperty( QTextFormat::BlockAlignment ) ) {
        emitAlignment( format.alignment() );
    }

    bool attributesEmitted = false;
    QLatin1String style( " style=\"" );
    //html += style;

//     if (block.begin().atEnd()) {
//         html += QLatin1String("-qt-paragraph-type:empty;");
//     }

    if ( format.hasProperty( QTextBlockFormat::FrameMargin ) ) {
        if ( !attributesEmitted ) {
            html += style;
            attributesEmitted = true;
        }
        emitMargins( QString::number( format.topMargin() ),
                     QString::number( format.bottomMargin() ),
                     QString::number( format.leftMargin() ),
                     QString::number( format.rightMargin() ) );
    }

    if ( format.hasProperty( QTextBlockFormat::BlockIndent ) ) {
//  if (format.indent() == 0) {
        if ( format.indent() == mDefaultBlockFormat.indent() ) {
            // assume default not to bloat the html too much
        } else {
            if ( !attributesEmitted ) {
                html += style;
                attributesEmitted = true;
            }
            html += QLatin1String( " -qt-block-indent:" );
            html += QString::number( format.indent() );
            html += QLatin1Char( ';' );
        }
    }

    if ( format.hasProperty( QTextBlockFormat::TextIndent ) ) {
//  if (format.textIndent() == 0) {
        if ( format.textIndent() == mDefaultBlockFormat.textIndent() ) {
            // assume default not to bloat the html too much
        } else {
            if ( !attributesEmitted ) {
                html += style;
                attributesEmitted = true;
            }
            html += QLatin1String( " text-indent:" );
            html += QString::number( format.textIndent() );
            html += QLatin1String( "px;" );
        }
    }

    //QTextCharFormat diff = formatDifference(defaultCharFormat, block.charFormat()).toCharFormat();
    //if (!diff.properties().isEmpty())
    //emitCharFormatStyle(diff);

    if ( attributesEmitted ) {
        html += QLatin1Char( '"' );
    }

//     QBrush bg = format.background();
//     if (bg != Qt::NoBrush)
//         emitAttribute("bgcolor", bg.color().name());
}
Example #12
0
QList<HtmlExporter::tag> HtmlExporter::emitCharFormatStyle( const QTextCharFormat 
                         &charFormat, const QTextBlockFormat &blockFormat  )
{
//     kDebug() << "html" << html;

    QList<HtmlExporter::tag> tags;
    bool attributesEmitted = false;
    QLatin1String styleTag( "<span style=\"" );

    const QString family = charFormat.fontFamily();

    //if (!family.isEmpty() && family != defaultCharFormat.fontFamily()) {
    // NOTE the above line replaced with the bottom line to use default charFormat, which can be set from outside.
    if ( charFormat.hasProperty( BilboTextFormat::HasCodeStyle ) && 
         charFormat.boolProperty( BilboTextFormat::HasCodeStyle ) ) {
        tags << code;

    } else if ( !family.isEmpty() && family != mDefaultCharFormat.fontFamily() ) {
//         if ( family.right( 7 ) == "courier" ) {
//             tags << code;
//         } else {
            if ( ! attributesEmitted ) {
                html += styleTag;
            }
            html += QLatin1String( " font-family:'" );
            html += family;
            html += QLatin1String( "';" );
            attributesEmitted = true;
//         }
    }


//     if (format.hasProperty(QTextFormat::FontPointSize)
//             && format.fontPointSize() != defaultCharFormat.fontPointSize()) {
    // NOTE the above line replaced with the bottom line to use default charFormat, which can be set from outside.
    if ( charFormat.hasProperty( QTextFormat::FontPointSize )
         && charFormat.fontPointSize() != mDefaultCharFormat.fontPointSize() ) {
        if ( ! attributesEmitted ) {
            html += styleTag;
        }
        html += QLatin1String( " font-size:" );
        html += QString::number( charFormat.fontPointSize() );
        html += QLatin1String( "pt;" );
        attributesEmitted = true;
    } else if ( charFormat.hasProperty( QTextFormat::FontSizeAdjustment ) && 
               !( blockFormat.hasProperty( BilboTextFormat::HtmlHeading ) &&
                  blockFormat.intProperty( BilboTextFormat::HtmlHeading ) ) ) {

        ///To use <h1-5> tags for font size
//         const int idx = format.intProperty(QTextFormat::FontSizeAdjustment) + 1;
//
//         switch (idx) {
//         case 0: tags << h5; break;
//             //case 1: tags << h4; break; //NOTE h4 will be the normal text!
//         case 2: tags << h3; break;
//         case 3: tags << h2; break;
//         case 4: tags << h1; break;
//         }

        ///To use <span> tags for font size
        static const char * const sizeNames[] = {
            "small", "medium", "large", "x-large", "xx-large"
        };
        const char *name = 0;
        const int idx = charFormat.intProperty( QTextFormat::FontSizeAdjustment ) + 1;
        if ( idx == 1 ) {
            // assume default to not bloat the html too much
        } else if ( idx >= 0 && idx <= 4 ) {
            name = sizeNames[idx];
        }
        if ( name ) {
            if ( ! attributesEmitted ) {
                html += styleTag;
            }
            html += QLatin1String( " font-size:" );
            html += QLatin1String( name );
            html += QLatin1Char( ';' );
            attributesEmitted = true;
        }
    }


//    if (format.fontWeight() > defaultCharFormat.fontWeight()) {
    // NOTE the above line replaced with the bottom line to use default charFormat, which can be set from outside.
    if ( charFormat.fontWeight() > mDefaultCharFormat.fontWeight() && 
        !( blockFormat.hasProperty( BilboTextFormat::HtmlHeading ) &&
           blockFormat.intProperty( BilboTextFormat::HtmlHeading ) ) ) {
        tags << strong;
        /*if (! attributesEmitted ) html += styleTag;
        html += QLatin1String(" font-weight:");
        html += QString::number(format.fontWeight() * 8);
        html += QLatin1Char(';');
        attributesEmitted = true;*/
    }

//    if (format.fontItalic() != defaultCharFormat.fontItalic()) {
    // NOTE the above line replaced with the bottom line to use default charFormat, which can be set from outside.
    if ( charFormat.fontItalic() != mDefaultCharFormat.fontItalic() ) {
        tags << em;
        /*
        if (! attributesEmitted ) html += styleTag;
        html += QLatin1String(" font-style:");
        html += (format.fontItalic() ? QLatin1String("italic") : QLatin1String("normal"));
        html += QLatin1Char(';');
        attributesEmitted = true;*/
    }

//    if (format.fontUnderline() != defaultCharFormat.fontUnderline()) {
    // NOTE the above line replaced with the bottom line to use default charFormat, which can be set from outside.
    if ( charFormat.fontUnderline() != mDefaultCharFormat.fontUnderline() ) {
        tags << u;
    }


//    if (format.fontStrikeOut() != defaultCharFormat.fontStrikeOut()) {
    // NOTE the above line replaced with the bottom line to use default charFormat, which can be set from outside.
    if ( charFormat.fontStrikeOut() != mDefaultCharFormat.fontStrikeOut() ) {
        tags << s;
    }

//    if (format.fontOverline() != defaultCharFormat.fontOverline()) {
    // NOTE the above line replaced with the bottom line to use default charFormat, which can be set from outside.
    if ( charFormat.fontOverline() != mDefaultCharFormat.fontOverline() ) {
        if ( charFormat.fontOverline() ) {
            if ( ! attributesEmitted ) {
                html += styleTag;
            }
            html += QLatin1String( " text-decoration: overline;" );
            attributesEmitted = true;
        }
    }

    /*
    bool hasDecoration = false;
    bool atLeastOneDecorationSet = false;
    QLatin1String decorationTag(" text-decoration:");

    if (format.fontUnderline() != defaultCharFormat.fontUnderline() ||
    format.fontOverline() != defaultCharFormat.fontOverline() ||
    format.fontStrikeOut() != defaultCharFormat.fontStrikeOut() )
    {
      if (! attributesEmitted ) html += styleTag;
      html += decorationTag;
    }

    if (format.fontUnderline() != defaultCharFormat.fontUnderline()) {
        hasDecoration = true;
        if (format.fontUnderline()) {
            html += QLatin1String(" underline");
            atLeastOneDecorationSet = true;
        }
    }

    if (format.fontOverline() != defaultCharFormat.fontOverline()) {
        hasDecoration = true;
        if (format.fontOverline()) {
            html += QLatin1String(" overline");
            atLeastOneDecorationSet = true;
        }
    }

    if (format.fontStrikeOut() != defaultCharFormat.fontStrikeOut()) {
        hasDecoration = true;
        if (format.fontStrikeOut()) {
            html += QLatin1String(" line-through");
            atLeastOneDecorationSet = true;
        }
    }

    if (hasDecoration) {
        if (!atLeastOneDecorationSet)
            html += QLatin1String("none");
        html += QLatin1Char(';');
        attributesEmitted = true;
    }*/
    /* else {
        html.chop(qstrlen(decorationTag.latin1()));
    }*/

//     QBrush linkColor = KColorScheme(QPalette::Active, KColorScheme::Window).foreground(KColorScheme::LinkText);

//    if ( format.foreground() != defaultCharFormat.foreground() &&
//            format.foreground().style() != Qt::NoBrush) {
    // NOTE the above line replaced with the bottom line to use default charFormat, which can be set from outside.
    if ( charFormat.foreground() != mDefaultCharFormat.foreground() &&
            charFormat.foreground().style() != Qt::NoBrush && !charFormat.isAnchor() ) {
        //         if ( format.foreground() != linkColor ) {
//    if ( format.anchorHref().isNull() ) {
        if ( ! attributesEmitted ) {
            html += styleTag;
            attributesEmitted = true;
        }
//    } else {
//     html += QLatin1String(" style=\"");
//    }
        html += QLatin1String( " color:" );
        html += charFormat.foreground().color().name();
        html += QLatin1Char( ';' );
//    if ( !format.anchorHref().isNull() ) {
//     html += QLatin1String("\"");
//    }
//    attributesEmitted = true;
        //         }
    }

//    if (format.background() != defaultCharFormat.background()
//            && format.background().style() != Qt::NoBrush) {
    // NOTE the above line replaced with the bottom line to use default charFormat, which can be set from outside.
    if ( !( charFormat.hasProperty( BilboTextFormat::HasCodeStyle ) && 
         charFormat.boolProperty( BilboTextFormat::HasCodeStyle ) ) ) {
        if ( charFormat.background() != mDefaultCharFormat.background()
                && charFormat.background().style() != Qt::NoBrush ) {
            if ( ! attributesEmitted ) {
                html += styleTag;
            }
            html += QLatin1String( " background-color:" );
            html += charFormat.background().color().name();
            html += QLatin1Char( ';' );
            attributesEmitted = true;
        }
    }
//    if (format.verticalAlignment() != defaultCharFormat.verticalAlignment()) { //TODO
    // NOTE the above line replaced with the bottom line to use default charFormat, which can be set from outside.
    if ( charFormat.verticalAlignment() != mDefaultCharFormat.verticalAlignment() ) { //TODO
        if ( ! attributesEmitted ) {
            html += styleTag;
        }
        html += QLatin1String( " vertical-align:" );

        QTextCharFormat::VerticalAlignment valign = charFormat.verticalAlignment();
        if ( valign == QTextCharFormat::AlignSubScript ) {
            html += QLatin1String( "sub" );
        } else if ( valign == QTextCharFormat::AlignSuperScript ) {
            html += QLatin1String( "super" );
        }

        html += QLatin1Char( ';' );
        attributesEmitted = true;
    }

    //Append close span Tag
    if ( attributesEmitted ) {
        html += QLatin1String( "\">" );
        tags.prepend( span );
    }

//     kDebug() << "html=>" << html << tags;
    return tags;
}
void QTextHtmlImporter::import()
{
    cursor.beginEditBlock();
    hasBlock = true;
    forceBlockMerging = false;
    compressNextWhitespace = RemoveWhiteSpace;
    blockTagClosed = false;
    for (currentNodeIdx = 0; currentNodeIdx < count(); ++currentNodeIdx) {
        currentNode = &at(currentNodeIdx);
        wsm = textEditMode ? QTextHtmlParserNode::WhiteSpacePreWrap : currentNode->wsm;

        /*
         * process each node in three stages:
         * 1) check if the hierarchy changed and we therefore passed the
         *    equivalent of a closing tag -> we may need to finish off
         *    some structures like tables
         *
         * 2) check if the current node is a special node like a
         *    <table>, <ul> or <img> tag that requires special processing
         *
         * 3) if the node should result in a QTextBlock create one and
         *    finally insert text that may be attached to the node
         */

        /* emit 'closing' table blocks or adjust current indent level
         * if we
         *  1) are beyond the first node
         *  2) the current node not being a child of the previous node
         *      means there was a tag closing in the input html
         */
        if (currentNodeIdx > 0 && (currentNode->parent != currentNodeIdx - 1)) {
            blockTagClosed = closeTag();
            // visually collapse subsequent block tags, but if the element after the closed block tag
            // is for example an inline element (!isBlock) we have to make sure we start a new paragraph by setting
            // hasBlock to false.
            if (blockTagClosed
                && !currentNode->isBlock()
                && currentNode->id != Html_unknown)
            {
                hasBlock = false;
            } else if (hasBlock) {
                // when collapsing subsequent block tags we need to clear the block format
                QTextBlockFormat blockFormat = currentNode->blockFormat;
                blockFormat.setIndent(indent);

                QTextBlockFormat oldFormat = cursor.blockFormat();
                if (oldFormat.hasProperty(QTextFormat::PageBreakPolicy)) {
                    QTextFormat::PageBreakFlags pageBreak = oldFormat.pageBreakPolicy();
                    if (pageBreak == QTextFormat::PageBreak_AlwaysAfter)
                        /* We remove an empty paragrah that requested a page break after.
                           moving that request to the next paragraph means we also need to make
                            that a pagebreak before to keep the same visual appearance.
                        */
                        pageBreak = QTextFormat::PageBreak_AlwaysBefore;
                    blockFormat.setPageBreakPolicy(pageBreak);
                }

                cursor.setBlockFormat(blockFormat);
            }
        }

        if (currentNode->displayMode == QTextHtmlElement::DisplayNone) {
            if (currentNode->id == Html_title)
                doc->setMetaInformation(QTextDocument::DocumentTitle, currentNode->text);
            // ignore explicitly 'invisible' elements
            continue;
        }

        if (processSpecialNodes() == ContinueWithNextNode)
            continue;

        // make sure there's a block for 'Blah' after <ul><li>foo</ul>Blah
        if (blockTagClosed
            && !hasBlock
            && !currentNode->isBlock()
            && !currentNode->text.isEmpty() && !currentNode->hasOnlyWhitespace()
            && currentNode->displayMode == QTextHtmlElement::DisplayInline) {

            QTextBlockFormat block = currentNode->blockFormat;
            block.setIndent(indent);

            appendBlock(block, currentNode->charFormat);

            hasBlock = true;
        }

        if (currentNode->isBlock()) {
            if (processBlockNode() == ContinueWithNextNode)
                continue;
        }

        if (currentNode->charFormat.isAnchor() && !currentNode->charFormat.anchorName().isEmpty()) {
            namedAnchors.append(currentNode->charFormat.anchorName());
        }

        if (appendNodeText())
            hasBlock = false; // if we actually appended text then we don't
                              // have an empty block anymore
    }

    cursor.endEditBlock();
}