22#include <QTextDocument>
32 mBlocks.append( block );
43 document.
reserve( lines.size() );
44 for (
const QString &line : lines )
54 document.
reserve( lines.size() );
56 for (
const QString &line : std::as_const( lines ) )
62 QTextDocument sourceDoc;
64 sourceDoc.setHtml( line );
66 QTextBlock sourceBlock = sourceDoc.firstBlock();
70 auto it = sourceBlock.begin();
74 const QTextFragment fragment = it.fragment();
75 if ( fragment.isValid() )
78 if ( fragment.text().contains( QStringLiteral(
"\u2028" ) ) )
89 const QStringList
splitLines = fragment.text().split( QStringLiteral(
"\u2028" ), Qt::SplitBehaviorFlags::SkipEmptyParts );
91 for (
const QString &splitLine : std::as_const(
splitLines ) )
96 splitFragment.
setText( splitLine );
106 if ( previousFormat )
114 splitLineBlock.
append( splitFragment );
115 document.
append( splitLineBlock );
127 if ( !block.
empty() )
130 sourceBlock = sourceBlock.next();
131 if ( !sourceBlock.isValid() )
141 mBlocks.append( block );
146 mBlocks.push_back( block );
151 mBlocks.reserve( count );
156 return mBlocks.at( i );
166 return mBlocks.size();
171 QStringList textLines;
172 textLines.reserve( mBlocks.size() );
178 line.append( fragment.text() );
187 const QVector< QgsTextBlock > prevBlocks = mBlocks;
189 mBlocks.reserve( prevBlocks.size() );
195 QStringList thisParts;
196 if ( !wrapCharacter.isEmpty() && wrapCharacter != QLatin1String(
"\n" ) )
199 const QStringList lines = fragment.text().split( wrapCharacter );
200 for (
const QString &line : lines )
202 thisParts.
append( line.split(
'\n' ) );
207 thisParts = fragment.text().split(
'\n' );
211 if ( autoWrapLength != 0 )
213 QStringList autoWrappedLines;
214 autoWrappedLines.reserve( thisParts.count() );
215 for (
const QString &line : std::as_const( thisParts ) )
217 autoWrappedLines.append(
QgsStringUtils::wordWrap( line, autoWrapLength, useMaxLineLengthWhenAutoWrapping ).split(
'\n' ) );
219 thisParts = autoWrappedLines;
222 if ( thisParts.empty() )
224 else if ( thisParts.size() == 1 )
225 destinationBlock.
append( fragment );
228 if ( !thisParts.at( 0 ).isEmpty() )
231 append( destinationBlock );
232 destinationBlock.
clear();
233 for (
int i = 1 ; i < thisParts.size() - 1; ++i )
237 destinationBlock.
append(
QgsTextFragment( thisParts.at( thisParts.size() - 1 ), fragment.characterFormat() ) );
240 append( destinationBlock );
248 block.applyCapitalization( capitalization );
253QVector< QgsTextBlock >::const_iterator QgsTextDocument::begin()
const
255 return mBlocks.begin();
258QVector< QgsTextBlock >::const_iterator QgsTextDocument::end()
const
260 return mBlocks.end();
Capitalization
String capitalization options.
static QString wordWrap(const QString &string, int length, bool useMaxLineLength=true, const QString &customDelimiter=QString())
Automatically wraps a string by inserting new line characters at appropriate locations in the string.
Represents a block of text consisting of one or more QgsTextFragment objects.
void clear()
Clears the block, removing all its contents.
void append(const QgsTextFragment &fragment)
Appends a fragment to the block.
const QgsTextFragment & at(int index) const
Returns the fragment at the specified index.
bool empty() const
Returns true if the block is empty.
Stores information relating to individual character formatting.
void overrideWith(const QgsTextCharacterFormat &other)
Override all the default/unset properties of the current character format with the settings from anot...
Represents a document consisting of one or more QgsTextBlock objects.
void splitLines(const QString &wrapCharacter, int autoWrapLength=0, bool useMaxLineLengthWhenAutoWrapping=true)
Splits lines of text in the document to separate lines, using a specified wrap character (wrapCharact...
QgsTextBlock & operator[](int index)
Returns the block at the specified index.
const QgsTextBlock & at(int index) const
Returns the block at the specified index.
void reserve(int count)
Reserves the specified count of blocks for optimised block appending.
QStringList toPlainText() const
Returns a list of plain text lines of text representing the document.
int size() const
Returns the number of blocks in the document.
static QgsTextDocument fromHtml(const QStringList &lines)
Constructor for QgsTextDocument consisting of a set of HTML formatted lines.
static QgsTextDocument fromPlainText(const QStringList &lines)
Constructor for QgsTextDocument consisting of a set of plain text lines.
void append(const QgsTextBlock &block)
Appends a block to the document.
void applyCapitalization(Qgis::Capitalization capitalization)
Applies a capitalization style to the document's text.
Stores a fragment of text along with formatting overrides to be used when rendering the fragment.
void setText(const QString &text)
Sets the text content of the fragment.
void setCharacterFormat(const QgsTextCharacterFormat &format)
Sets the character format for the fragment.
const QgsTextCharacterFormat & characterFormat() const
Returns the character formatting for the fragment.