21 #include <QTextDocument>
30 mBlocks.append( block );
41 document.
reserve( lines.size() );
42 for (
const QString &line : lines )
51 document.
reserve( lines.size() );
52 for (
const QString &line : lines )
57 QTextDocument sourceDoc;
58 sourceDoc.setHtml( line );
60 QTextBlock sourceBlock = sourceDoc.firstBlock();
63 auto it = sourceBlock.begin();
67 const QTextFragment fragment = it.fragment();
68 if ( fragment.isValid() )
77 sourceBlock = sourceBlock.next();
78 if ( !sourceBlock.isValid() )
87 mBlocks.append( block );
92 mBlocks.push_back( block );
97 mBlocks.reserve( count );
102 return mBlocks.at( i );
112 return mBlocks.size();
117 QStringList textLines;
118 textLines.reserve( mBlocks.size() );
124 line.append( fragment.text() );
133 const QVector< QgsTextBlock > prevBlocks = mBlocks;
135 mBlocks.reserve( prevBlocks.size() );
141 QStringList thisParts;
142 if ( !wrapCharacter.isEmpty() && wrapCharacter != QLatin1String(
"\n" ) )
145 const QStringList lines = fragment.text().split( wrapCharacter );
146 for (
const QString &line : lines )
148 thisParts.
append( line.split(
'\n' ) );
153 thisParts = fragment.text().split(
'\n' );
157 if ( autoWrapLength != 0 )
159 QStringList autoWrappedLines;
160 autoWrappedLines.reserve( thisParts.count() );
161 for (
const QString &line : std::as_const( thisParts ) )
163 autoWrappedLines.append(
QgsStringUtils::wordWrap( line, autoWrapLength, useMaxLineLengthWhenAutoWrapping ).split(
'\n' ) );
165 thisParts = autoWrappedLines;
168 if ( thisParts.empty() )
170 else if ( thisParts.size() == 1 )
171 destinationBlock.
append( fragment );
174 if ( !thisParts.at( 0 ).isEmpty() )
177 append( destinationBlock );
178 destinationBlock.
clear();
179 for (
int i = 1 ; i < thisParts.size() - 1; ++i )
183 destinationBlock.
append(
QgsTextFragment( thisParts.at( thisParts.size() - 1 ), fragment.characterFormat() ) );
186 append( destinationBlock );
194 block.applyCapitalization( capitalization );
199 QVector< QgsTextBlock >::const_iterator QgsTextDocument::begin()
const
201 return mBlocks.begin();
204 QVector< QgsTextBlock >::const_iterator QgsTextDocument::end()
const
206 return mBlocks.end();
Capitalization
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.
void applyCapitalization(QgsStringUtils::Capitalization capitalization)
Applies a capitalization style to the document's text.
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.
Stores a fragment of text along with formatting overrides to be used when rendering the fragment.