QGIS API Documentation 3.37.0-Master (08041ddf763)
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
65{
66 mFragments.append( fragment );
67}
68
70{
71 mFragments.push_back( fragment );
72}
73
75{
76 mFragments.clear();
77}
78
80{
81 return mFragments.empty();
82}
83
85{
86 return mFragments.size();
87}
88
90{
91 for ( QgsTextFragment &fragment : mFragments )
92 {
93 fragment.applyCapitalization( capitalization );
94 }
95}
96
97const QgsTextFragment &QgsTextBlock::at( int index ) const
98{
99 return mFragments.at( index );
100}
101
103{
104 return mFragments[ index ];
105}
106
108QVector< QgsTextFragment >::const_iterator QgsTextBlock::begin() const
109{
110 return mFragments.begin();
111}
112
113QVector< QgsTextFragment >::const_iterator QgsTextBlock::end() const
114{
115 return mFragments.end();
116}
Capitalization
String capitalization options.
Definition qgis.h:2884
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.
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 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 text along with formatting overrides to be used when rendering the fragment.