QGIS API Documentation 3.99.0-Master (2fe06baccd8)
Loading...
Searching...
No Matches
qgstextblock.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgstextblock.cpp
3 ---------------
4 begin : May 2020
5 copyright : (C) Nyall Dawson
6 email : nyall dot dawson at gmail dot com
7 ***************************************************************************
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
13 * *
14 ***************************************************************************/
15
16#include "qgstextblock.h"
17
18#include "qgstextfragment.h"
19
20#include <QSizeF>
21
23{
24 mFragments.append( fragment );
25}
26
28{
29 if ( text.contains( '\t' ) )
30 {
31 // split line by tab characters, each tab should be a
32 // fragment by itself
33 QgsTextBlock block;
34 const QStringList tabSplit = text.split( '\t' );
35 int index = 0;
36 for ( const QString &part : tabSplit )
37 {
38 if ( !part.isEmpty() )
39 block.append( QgsTextFragment( part, format ) );
40 if ( index != tabSplit.size() - 1 )
41 {
42 block.append( QgsTextFragment( QString( '\t' ), format ) );
43 }
44
45 index++;
46 }
47 return block;
48 }
49 else
50 {
51 return QgsTextBlock( QgsTextFragment( text, format ) );
52 }
53}
54
56{
57 QString res;
58 for ( const QgsTextFragment &fragment : mFragments )
59 {
60 res.append( fragment.text() );
61 }
62 return res;
63}
64
65void QgsTextBlock::reserve( int count )
66{
67 mFragments.reserve( count );
68}
69
71{
72 mFragments.append( fragment );
73}
74
76{
77 mFragments.push_back( fragment );
78}
79
80void QgsTextBlock::insert( int index, const QgsTextFragment &fragment )
81{
82 mFragments.insert( index, fragment );
83}
84
85void QgsTextBlock::insert( int index, QgsTextFragment &&fragment )
86{
87 mFragments.insert( index, fragment );
88}
89
91{
92 mFragments.clear();
93}
94
96{
97 return mFragments.empty();
98}
99
101{
102 return mFragments.size();
103}
104
106{
107 mBlockFormat = format;
108}
109
111{
112 for ( QgsTextFragment &fragment : mFragments )
113 {
114 fragment.applyCapitalization( capitalization );
115 }
116}
117
119{
120 return mBlockFormat.hasBackground()
121 || std::any_of( mFragments.begin(), mFragments.end(), []( const QgsTextFragment & fragment ) { return fragment.characterFormat().hasBackground(); } );
122}
123
124const QgsTextFragment &QgsTextBlock::at( int index ) const
125{
126 return mFragments.at( index );
127}
128
130{
131 return mFragments[ index ];
132}
133
135QVector< QgsTextFragment >::const_iterator QgsTextBlock::begin() const
136{
137 return mFragments.begin();
138}
139
140QVector< QgsTextFragment >::const_iterator QgsTextBlock::end() const
141{
142 return mFragments.end();
143}
Capitalization
String capitalization options.
Definition qgis.h:3389
Stores information relating to individual block formatting.
bool hasBackground() const
Returns true if the block has a background set.
int size() const
Returns the number of fragments in the block.
void applyCapitalization(Qgis::Capitalization capitalization)
Applies a capitalization style to the block's text.
void insert(int index, const QgsTextFragment &fragment)
Inserts a fragment into the block, at the specified index.
QString toPlainText() const
Converts the block to plain text.
QgsTextFragment & operator[](int index)
Returns the fragment at the specified index.
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.
QgsTextBlock()=default
Constructor for an empty text block.
void reserve(int count)
Reserves the specified count of fragments for optimised fragment appending.
void setBlockFormat(const QgsTextBlockFormat &format)
Sets the block format for the fragment.
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.
bool hasBackgrounds() const
Returns true if the block or any of the fragments in the block have background brushes set.
Stores information relating to individual character formatting.
Stores a fragment of document along with formatting overrides to be used when rendering the fragment.