Skip to content

Commit

Permalink
Add method to convert a QgsTextBlock to plain text
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Jul 9, 2020
1 parent 49a3c10 commit 52f7741
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 0 deletions.
7 changes: 7 additions & 0 deletions python/core/auto_generated/textrenderer/qgstextblock.sip.in
Expand Up @@ -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 );
Expand Down
10 changes: 10 additions & 0 deletions src/core/textrenderer/qgstextblock.cpp
Expand Up @@ -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 );
Expand Down
7 changes: 7 additions & 0 deletions src/core/textrenderer/qgstextblock.h
Expand Up @@ -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.
*/
Expand Down
3 changes: 3 additions & 0 deletions tests/src/python/test_qgstextblock.py
Expand Up @@ -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()
Expand All @@ -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'))
Expand Down

0 comments on commit 52f7741

Please sign in to comment.