23#include <QTextDocument>
33 mBlocks.append( block );
44 document.
reserve( lines.size() );
45 for (
const QString &line : lines )
55#define TAB_REPLACEMENT_MARKER " ignore_me_i_am_a_tab "
61 document.
reserve( lines.size() );
63 for (
const QString &l : std::as_const( lines ) )
70 QTextDocument sourceDoc;
81 const thread_local QRegularExpression sRxWordSpacingFix( QStringLiteral(
"word-spacing:\\s*(-?\\d+(?:\\.\\d+)?)([a-zA-Z]*)" ) );
82 line.replace( sRxWordSpacingFix, QStringLiteral(
"word-spacing: \\1px" ) );
84 sourceDoc.setHtml( line );
86 QTextBlock sourceBlock = sourceDoc.firstBlock();
90 const int headingLevel = sourceBlock.blockFormat().headingLevel();
92 if ( headingLevel > 0 )
94 switch ( headingLevel )
119 auto it = sourceBlock.begin();
122 while ( !it.atEnd() )
124 const QTextFragment fragment = it.fragment();
125 if ( fragment.isValid() )
128 const QString fragmentText = fragment.text();
129 if ( fragmentText.contains( QStringLiteral(
"\u2028" ) ) )
132 const QStringList
splitLines = fragmentText.split( QStringLiteral(
"\u2028" ), Qt::SplitBehaviorFlags::SkipEmptyParts );
134 for (
const QString &splitLine : std::as_const(
splitLines ) )
141 previousFormat = &document.
at( document.
size() - 1 ).
at( 0 ).characterFormat();
151 if ( previousFormat )
154 newFormat.overrideWith( *previousFormat );
161 for (
const QString &part : tabSplit )
163 if ( !part.isEmpty() )
166 block.
append( splitFragment );
168 if ( index != tabSplit.size() - 1 )
178 splitFragment.
setText( splitLine );
182 if ( previousFormat )
185 newFormat.overrideWith( *previousFormat );
189 block.
append( splitFragment );
209 for (
const QString &part : tabSplit )
211 if ( !part.isEmpty() )
214 block.
append( tmpFragment );
216 if ( index != tabSplit.size() - 1 )
230 block.
append( tmpFragment );
236 if ( !block.
empty() )
239 sourceBlock = sourceBlock.next();
240 if ( !sourceBlock.isValid() )
259 if ( doc.
size() > 0 )
266 mBlocks.append( block );
271 mBlocks.push_back( block );
276 mBlocks.insert( index, block );
281 mBlocks.insert( index, block );
286 mBlocks.reserve( count );
291 return mBlocks.at( i );
301 return mBlocks.size();
306 QStringList textLines;
307 textLines.reserve( mBlocks.size() );
313 line.append( fragment.text() );
322 const QVector< QgsTextBlock > prevBlocks = mBlocks;
324 mBlocks.reserve( prevBlocks.size() );
331 QStringList thisParts;
332 if ( !wrapCharacter.isEmpty() && wrapCharacter != QLatin1String(
"\n" ) )
335 const QStringList lines = fragment.text().split( wrapCharacter );
336 for (
const QString &line : lines )
338 thisParts.append( line.split(
'\n' ) );
343 thisParts = fragment.text().split(
'\n' );
347 if ( autoWrapLength != 0 )
349 QStringList autoWrappedLines;
350 autoWrappedLines.reserve( thisParts.count() );
351 for (
const QString &line : std::as_const( thisParts ) )
353 autoWrappedLines.append(
QgsStringUtils::wordWrap( line, autoWrapLength, useMaxLineLengthWhenAutoWrapping ).split(
'\n' ) );
355 thisParts = autoWrappedLines;
358 if ( thisParts.empty() )
360 else if ( thisParts.size() == 1 )
361 destinationBlock.
append( fragment );
364 if ( !thisParts.at( 0 ).isEmpty() )
367 append( destinationBlock );
368 destinationBlock.
clear();
369 for (
int i = 1 ; i < thisParts.size() - 1; ++i )
375 destinationBlock.
append(
QgsTextFragment( thisParts.at( thisParts.size() - 1 ), fragment.characterFormat() ) );
378 append( destinationBlock );
386 block.applyCapitalization( capitalization );
391QVector< QgsTextBlock >::const_iterator QgsTextDocument::begin()
const
393 return mBlocks.begin();
396QVector< QgsTextBlock >::const_iterator QgsTextDocument::end()
const
398 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.
Stores information relating to individual block formatting.
Represents a block of text consisting of one or more QgsTextFragment objects.
void clear()
Clears the block, removing all its contents.
static QgsTextBlock fromPlainText(const QString &text, const QgsTextCharacterFormat &format=QgsTextCharacterFormat())
Constructor for QgsTextBlock consisting of a plain text, and optional character format.
void setBlockFormat(const QgsTextBlockFormat &format)
Sets the block format for the fragment.
void append(const QgsTextFragment &fragment)
Appends a fragment to the block.
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...
void setFontPercentageSize(double size)
Sets the font percentage size (as fraction of inherited font size).
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 insert(int index, const QgsTextBlock &block)
Inserts a block into the document, at the specified index.
static QgsTextDocument fromTextAndFormat(const QStringList &lines, const QgsTextFormat &format)
Constructor for QgsTextDocument consisting of a set of lines, respecting settings from a text format.
void applyCapitalization(Qgis::Capitalization capitalization)
Applies a capitalization style to the document's text.
Container for all settings relating to text rendering.
Qgis::Capitalization capitalization() const
Returns the text capitalization style.
bool allowHtmlFormatting() const
Returns true if text should be treated as a HTML document and HTML tags should be used for formatting...
Stores a fragment of document 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.
#define TAB_REPLACEMENT_MARKER