22#include <QTextDocument>
32 mBlocks.append( block );
43 document.
reserve( lines.size() );
44 for (
const QString &line : lines )
53 document.
reserve( lines.size() );
54 for (
const QString &line : lines )
59 QTextDocument sourceDoc;
60 sourceDoc.setHtml( line );
62 QTextBlock sourceBlock = sourceDoc.firstBlock();
65 auto it = sourceBlock.begin();
69 const QTextFragment fragment = it.fragment();
70 if ( fragment.isValid() )
79 sourceBlock = sourceBlock.next();
80 if ( !sourceBlock.isValid() )
89 mBlocks.append( block );
94 mBlocks.push_back( block );
99 mBlocks.reserve( count );
104 return mBlocks.at( i );
114 return mBlocks.size();
119 QStringList textLines;
120 textLines.reserve( mBlocks.size() );
126 line.append( fragment.text() );
135 const QVector< QgsTextBlock > prevBlocks = mBlocks;
137 mBlocks.reserve( prevBlocks.size() );
143 QStringList thisParts;
144 if ( !wrapCharacter.isEmpty() && wrapCharacter != QLatin1String(
"\n" ) )
147 const QStringList lines = fragment.text().split( wrapCharacter );
148 for (
const QString &line : lines )
150 thisParts.
append( line.split(
'\n' ) );
155 thisParts = fragment.text().split(
'\n' );
159 if ( autoWrapLength != 0 )
161 QStringList autoWrappedLines;
162 autoWrappedLines.reserve( thisParts.count() );
163 for (
const QString &line : std::as_const( thisParts ) )
165 autoWrappedLines.append(
QgsStringUtils::wordWrap( line, autoWrapLength, useMaxLineLengthWhenAutoWrapping ).split(
'\n' ) );
167 thisParts = autoWrappedLines;
170 if ( thisParts.empty() )
172 else if ( thisParts.size() == 1 )
173 destinationBlock.
append( fragment );
176 if ( !thisParts.at( 0 ).isEmpty() )
179 append( destinationBlock );
180 destinationBlock.
clear();
181 for (
int i = 1 ; i < thisParts.size() - 1; ++i )
185 destinationBlock.
append(
QgsTextFragment( thisParts.at( thisParts.size() - 1 ), fragment.characterFormat() ) );
188 append( destinationBlock );
196 block.applyCapitalization( capitalization );
201QVector< QgsTextBlock >::const_iterator QgsTextDocument::begin()
const
203 return mBlocks.begin();
206QVector< QgsTextBlock >::const_iterator QgsTextDocument::end()
const
208 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.
bool empty() const
Returns true if the block is empty.
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.