QGIS API Documentation 3.41.0-Master (fda2aa46e9a)
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#include "qgstextfragment.h"
18
19#include <QSizeF>
20
22{
23 mFragments.append( fragment );
24}
25
27{
28 if ( text.contains( '\t' ) )
29 {
30 // split line by tab characters, each tab should be a
31 // fragment by itself
32 QgsTextBlock block;
33 const QStringList tabSplit = text.split( '\t' );
34 int index = 0;
35 for ( const QString &part : tabSplit )
36 {
37 if ( !part.isEmpty() )
38 block.append( QgsTextFragment( part, format ) );
39 if ( index != tabSplit.size() - 1 )
40 {
41 block.append( QgsTextFragment( QString( '\t' ), format ) );
42 }
43
44 index++;
45 }
46 return block;
47 }
48 else
49 {
50 return QgsTextBlock( QgsTextFragment( text, format ) );
51 }
52}
53
55{
56 QString res;
57 for ( const QgsTextFragment &fragment : mFragments )
58 {
59 res.append( fragment.text() );
60 }
61 return res;
62}
63
64void QgsTextBlock::reserve( int count )
65{
66 mFragments.reserve( count );
67}
68
70{
71 mFragments.append( fragment );
72}
73
75{
76 mFragments.push_back( fragment );
77}
78
79void QgsTextBlock::insert( int index, const QgsTextFragment &fragment )
80{
81 mFragments.insert( index, fragment );
82}
83
84void QgsTextBlock::insert( int index, QgsTextFragment &&fragment )
85{
86 mFragments.insert( index, fragment );
87}
88
90{
91 mFragments.clear();
92}
93
95{
96 return mFragments.empty();
97}
98
100{
101 return mFragments.size();
102}
103
105{
106 mBlockFormat = format;
107}
108
110{
111 for ( QgsTextFragment &fragment : mFragments )
112 {
113 fragment.applyCapitalization( capitalization );
114 }
115}
116
117const QgsTextFragment &QgsTextBlock::at( int index ) const
118{
119 return mFragments.at( index );
120}
121
123{
124 return mFragments[ index ];
125}
126
128QVector< QgsTextFragment >::const_iterator QgsTextBlock::begin() const
129{
130 return mFragments.begin();
131}
132
133QVector< QgsTextFragment >::const_iterator QgsTextBlock::end() const
134{
135 return mFragments.end();
136}
Capitalization
String capitalization options.
Definition qgis.h:3132
Stores information relating to individual block formatting.
Represents a block of text consisting of one or more QgsTextFragment objects.
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.
Stores information relating to individual character formatting.
Stores a fragment of document along with formatting overrides to be used when rendering the fragment.