Skip to content
Permalink
Browse files

Add method to change capitalization of QgsTextDocument

  • Loading branch information
nyalldawson committed Sep 21, 2020
1 parent 772181b commit 620948f8096ab4f058be0f9c02da3a73b288b5eb
@@ -62,6 +62,13 @@ Returns ``True`` if the block is empty.
int size() const;
%Docstring
Returns the number of fragments in the block.
%End

void applyCapitalization( QgsStringUtils::Capitalization capitalization );
%Docstring
Applies a ``capitalization`` style to the block's text.

.. versionadded:: 3.16
%End

int __len__() const;
@@ -113,6 +113,13 @@ argument controls whether the lines should be wrapped to an ideal maximum of ``a
if ``False`` then the lines are wrapped to an ideal minimum length of ``autoWrapLength`` characters.
%End

void applyCapitalization( QgsStringUtils::Capitalization capitalization );
%Docstring
Applies a ``capitalization`` style to the document's text.

.. versionadded:: 3.16
%End


};

@@ -76,6 +76,13 @@ The optional ``scaleFactor`` parameter can specify a font size scaling factor. I
QgsTextRenderer.FONT_WORKAROUND_SCALE and then manually calculations
based on the resultant font metrics. Failure to do so will result in poor quality text rendering
at small font sizes.
%End

void applyCapitalization( QgsStringUtils::Capitalization capitalization );
%Docstring
Applies a ``capitalization`` style to the fragment's text.

.. versionadded:: 3.16
%End

};
@@ -56,6 +56,14 @@ int QgsTextBlock::size() const
return mFragments.size();
}

void QgsTextBlock::applyCapitalization( QgsStringUtils::Capitalization capitalization )
{
for ( QgsTextFragment &fragment : mFragments )
{
fragment.applyCapitalization( capitalization );
}
}

const QgsTextFragment &QgsTextBlock::at( int index ) const
{
return mFragments.at( index );
@@ -19,6 +19,7 @@
#include "qgis_sip.h"
#include "qgis_core.h"
#include "qgstextfragment.h"
#include "qgsstringutils.h"
#include <QVector>

/**
@@ -78,6 +79,13 @@ class CORE_EXPORT QgsTextBlock
*/
int size() const;

/**
* Applies a \a capitalization style to the block's text.
*
* \since QGIS 3.16
*/
void applyCapitalization( QgsStringUtils::Capitalization capitalization );

#ifdef SIP_RUN
int __len__() const;
% MethodCode
@@ -187,6 +187,14 @@ void QgsTextDocument::splitLines( const QString &wrapCharacter, int autoWrapLeng
}
}

void QgsTextDocument::applyCapitalization( QgsStringUtils::Capitalization capitalization )
{
for ( QgsTextBlock &block : mBlocks )
{
block.applyCapitalization( capitalization );
}
}

///@cond PRIVATE
QVector< QgsTextBlock >::const_iterator QgsTextDocument::begin() const
{
@@ -18,6 +18,7 @@

#include "qgis_sip.h"
#include "qgis_core.h"
#include "qgsstringutils.h"

#include <QVector>

@@ -137,6 +138,13 @@ class CORE_EXPORT QgsTextDocument
*/
void splitLines( const QString &wrapCharacter, int autoWrapLength = 0, bool useMaxLineLengthWhenAutoWrapping = true );

/**
* Applies a \a capitalization style to the document's text.
*
* \since QGIS 3.16
*/
void applyCapitalization( QgsStringUtils::Capitalization capitalization );

#ifndef SIP_RUN
///@cond PRIVATE
QVector< QgsTextBlock >::const_iterator begin() const;
@@ -68,3 +68,8 @@ double QgsTextFragment::horizontalAdvance( const QFont &font, bool fontHasBeenUp
}
}

void QgsTextFragment::applyCapitalization( QgsStringUtils::Capitalization capitalization )
{
mText = QgsStringUtils::capitalize( mText, capitalization );
}

@@ -19,6 +19,7 @@
#include "qgis_sip.h"
#include "qgis_core.h"
#include "qgstextcharacterformat.h"
#include "qgsstringutils.h"

class QTextFragment;

@@ -87,6 +88,13 @@ class CORE_EXPORT QgsTextFragment
*/
double horizontalAdvance( const QFont &font, bool fontHasBeenUpdatedForFragment = false, double scaleFactor = 1.0 ) const;

/**
* Applies a \a capitalization style to the fragment's text.
*
* \since QGIS 3.16
*/
void applyCapitalization( QgsStringUtils::Capitalization capitalization );

private:

QString mText;
@@ -16,7 +16,8 @@

from qgis.core import (
QgsTextBlock,
QgsTextFragment
QgsTextFragment,
QgsStringUtils
)
from qgis.testing import start_app, unittest

@@ -85,6 +86,13 @@ def testClear(self):
self.assertEqual(len(block), 0)
self.assertTrue(block.empty())

def testCapitalize(self):
fragment = QgsTextFragment('ludicrous gibs!')
block = QgsTextBlock(fragment)
block.append(QgsTextFragment('another part'))
block.applyCapitalization(QgsStringUtils.TitleCase)
self.assertEqual(block.toPlainText(), 'Ludicrous Gibs!Another Part')


if __name__ == '__main__':
unittest.main()
@@ -18,7 +18,8 @@
QgsTextDocument,
QgsTextBlock,
QgsTextFragment,
QgsTextCharacterFormat
QgsTextCharacterFormat,
QgsStringUtils
)
from qgis.testing import start_app, unittest

@@ -160,6 +161,11 @@ def testSplitLines(self):
self.assertEqual(len(doc[2]), 1)
self.assertEqual(doc[2][0].text(), 'red')

def testCapitalize(self):
doc = QgsTextDocument.fromPlainText(['abc def ghi', 'more text', 'another block'])
doc.applyCapitalization(QgsStringUtils.TitleCase)
self.assertEqual(doc.toPlainText(), ['Abc Def Ghi', 'More Text', 'Another Block'])


if __name__ == '__main__':
unittest.main()
@@ -16,7 +16,8 @@

from qgis.core import (
QgsTextFragment,
QgsTextCharacterFormat
QgsTextCharacterFormat,
QgsStringUtils
)
from qgis.PyQt.QtGui import QColor
from qgis.testing import start_app, unittest
@@ -49,6 +50,11 @@ def testSetCharacterFormat(self):
self.assertTrue(fragment.characterFormat().textColor().isValid())
self.assertEqual(fragment.characterFormat().textColor().name(), '#ff0000')

def testCapitalize(self):
fragment = QgsTextFragment('ludicrous gibs!')
fragment.applyCapitalization(QgsStringUtils.TitleCase)
self.assertEqual(fragment.text(), 'Ludicrous Gibs!')


if __name__ == '__main__':
unittest.main()

0 comments on commit 620948f

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