QTextCursor insert text with format problem after paragraphSeparator

108 Views Asked by At

Hy Guys, I have this code

#include <QTextBlock>
#include <QTextCursor>
#include <QTextDocument>

struct PartProperty
{
    enum e
    {
        Size = QTextFormat::UserProperty,
        Number,
        TagInfo,
    };
};

struct TagInfo
{
    inline TagInfo() : tagType( Unpaired ), tagCharType( TagMiddle )
    {
    }

    enum TagType
    {
        Unpaired,
        PairedOpen,
        PairedClose
    };

    enum TagCharType
    {
        TagBegin,
        TagMiddle,
        TagEnd
    };

    TagType tagType;
    TagCharType tagCharType;
};

void scanDocument( QTextDocument * document )
{
    if (!document)
        return;
    QTextCursor cursor( document );
    QTextCharFormat format = cursor.charFormat();
    auto text = document->toRawText();
    while( !cursor.atEnd() )
    {
        qDebug() << document->characterAt(cursor.position()) << ", " << text.at(cursor.position()) << ": " << (format.hasProperty( PartProperty::TagInfo ) ? QString("%1").arg(format.property( PartProperty::TagInfo ).value<TagInfo>().tagCharType) : "no tagInfo" );
        cursor.movePosition( QTextCursor::NextCharacter );
        format = cursor.charFormat();
    }
}

void writeExtendedTag( QTextCursor & cursor, TagInfo::TagType tagType)
{
    QTextCharFormat charFormat = cursor.document()->begin().charFormat();

    QTextCharFormat beginCharFormat = charFormat;
    TagInfo beginTagInfo;
    beginTagInfo.tagType = tagType;
    beginTagInfo.tagCharType = TagInfo::TagBegin;
    beginCharFormat.setProperty( PartProperty::TagInfo, QVariant::fromValue( beginTagInfo ) );

    cursor.insertText( QString( QChar::ObjectReplacementCharacter ), beginCharFormat );

    scanDocument(cursor.document());
}

int main(int argc, char *argv[])
{
    QTextDocument doc1;

    QTextCursor cursor1(&doc1);
    cursor1.insertText( " ");

    qDebug() << "first doc";

    writeExtendedTag( cursor1, TagInfo::Unpaired );


    QTextDocument doc2;

    QTextCursor cursor2(&doc2);
    cursor2.insertText( QString(QChar::ParagraphSeparator) );

    qDebug() << "second doc";

    writeExtendedTag( cursor2, TagInfo::Unpaired );

}

that outputs

first doc
' ' ,  ' ' :  "no tagInfo"
'\ufffc' ,  '\ufffc' :  "no tagInfo"
second doc
'\u2029' ,  '\u2029' :  "no tagInfo"
'\ufffc' ,  '\ufffc' :  "0"

What is the problem? Why does the paragraph separator works differently? I tried to debug but got inside QTextDocumentPrivate where things are pretty pretty complex at first sight.

Can you help me either understand what does QTextDocumentPrivate does with format in functions like split, either help how to insert properly formated object replacement char?

1

There are 1 best solutions below

0
Kapitan On

Okay, so the problem is in QTextCursor.

While inserting one of the following characters:

#define QTextBeginningOfFrame QChar(u'\xfdd0')
#define QTextEndOfFrame QChar(u'\xfdd1')

const QSet<QChar> textDocumentNewBlockCharsSet = {u'\n', QChar::ParagraphSeparator, QTextBeginningOfFrame, QTextEndOfFrame, u'\r'};

QTextCursor inserts block and denotes it with an aditional SegmentSeparator And doubles the inserted format on both chars.