Skip to content
Permalink
Browse files

Add method to convert a QgsTextBlock to plain text

  • Loading branch information
nyalldawson committed Jul 9, 2020
1 parent 49a3c10 commit 52f774158e25888e10fbcbcd4436dcfa3228a76d
@@ -34,6 +34,13 @@ Constructor for an empty text block.
explicit QgsTextBlock( const QgsTextFragment &fragment );
%Docstring
Constructor for a QgsTextBlock consisting of a single text ``fragment``.
%End

QString toPlainText() const;
%Docstring
Converts the block to plain text.

.. versionadded:: 3.16
%End

void append( const QgsTextFragment &fragment );
@@ -21,6 +21,16 @@ QgsTextBlock::QgsTextBlock( const QgsTextFragment &fragment )
mFragments.append( fragment );
}

QString QgsTextBlock::toPlainText() const
{
QString res;
for ( const QgsTextFragment &fragment : mFragments )
{
res.append( fragment.text() );
}
return res;
}

void QgsTextBlock::append( const QgsTextFragment &fragment )
{
mFragments.append( fragment );
@@ -46,6 +46,13 @@ class CORE_EXPORT QgsTextBlock
*/
explicit QgsTextBlock( const QgsTextFragment &fragment );

/**
* Converts the block to plain text.
*
* \since QGIS 3.16
*/
QString toPlainText() const;

/**
* Appends a \a fragment to the block.
*/
@@ -35,6 +35,7 @@ def testConstructors(self):
block = QgsTextBlock(fragment)
self.assertEqual(len(block), 1)
self.assertEqual(block[0].text(), fragment.text())
self.assertEqual(block.toPlainText(), 'ludicrous gibs!')

def testAppend(self):
block = QgsTextBlock()
@@ -50,6 +51,8 @@ def testAppend(self):
self.assertEqual(block[0].text(), 'a')
self.assertEqual(block[1].text(), 'b')

self.assertEqual(block.toPlainText(), 'ab')

def testAt(self):
block = QgsTextBlock()
block.append(QgsTextFragment('a'))

0 comments on commit 52f7741

Please sign in to comment.
You can’t perform that action at this time.