diff --git a/python/core/auto_generated/textrenderer/qgstextblock.sip.in b/python/core/auto_generated/textrenderer/qgstextblock.sip.in index 497508ca838..6f34166648a 100644 --- a/python/core/auto_generated/textrenderer/qgstextblock.sip.in +++ b/python/core/auto_generated/textrenderer/qgstextblock.sip.in @@ -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; diff --git a/python/core/auto_generated/textrenderer/qgstextdocument.sip.in b/python/core/auto_generated/textrenderer/qgstextdocument.sip.in index 435b48a2c7d..d0d498432ed 100644 --- a/python/core/auto_generated/textrenderer/qgstextdocument.sip.in +++ b/python/core/auto_generated/textrenderer/qgstextdocument.sip.in @@ -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 + }; diff --git a/python/core/auto_generated/textrenderer/qgstextfragment.sip.in b/python/core/auto_generated/textrenderer/qgstextfragment.sip.in index 594e41595a9..a9054299ee6 100644 --- a/python/core/auto_generated/textrenderer/qgstextfragment.sip.in +++ b/python/core/auto_generated/textrenderer/qgstextfragment.sip.in @@ -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 }; diff --git a/src/core/textrenderer/qgstextblock.cpp b/src/core/textrenderer/qgstextblock.cpp index 673aebd5324..464bffcf970 100644 --- a/src/core/textrenderer/qgstextblock.cpp +++ b/src/core/textrenderer/qgstextblock.cpp @@ -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 ); diff --git a/src/core/textrenderer/qgstextblock.h b/src/core/textrenderer/qgstextblock.h index a879884e270..18840c35037 100644 --- a/src/core/textrenderer/qgstextblock.h +++ b/src/core/textrenderer/qgstextblock.h @@ -19,6 +19,7 @@ #include "qgis_sip.h" #include "qgis_core.h" #include "qgstextfragment.h" +#include "qgsstringutils.h" #include /** @@ -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 diff --git a/src/core/textrenderer/qgstextdocument.cpp b/src/core/textrenderer/qgstextdocument.cpp index cf25e7ae84c..0c9f4fbb873 100644 --- a/src/core/textrenderer/qgstextdocument.cpp +++ b/src/core/textrenderer/qgstextdocument.cpp @@ -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 { diff --git a/src/core/textrenderer/qgstextdocument.h b/src/core/textrenderer/qgstextdocument.h index 173bc557af8..43df966f88d 100644 --- a/src/core/textrenderer/qgstextdocument.h +++ b/src/core/textrenderer/qgstextdocument.h @@ -18,6 +18,7 @@ #include "qgis_sip.h" #include "qgis_core.h" +#include "qgsstringutils.h" #include @@ -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; diff --git a/src/core/textrenderer/qgstextfragment.cpp b/src/core/textrenderer/qgstextfragment.cpp index 8029a66c892..8bd412f1e46 100644 --- a/src/core/textrenderer/qgstextfragment.cpp +++ b/src/core/textrenderer/qgstextfragment.cpp @@ -68,3 +68,8 @@ double QgsTextFragment::horizontalAdvance( const QFont &font, bool fontHasBeenUp } } +void QgsTextFragment::applyCapitalization( QgsStringUtils::Capitalization capitalization ) +{ + mText = QgsStringUtils::capitalize( mText, capitalization ); +} + diff --git a/src/core/textrenderer/qgstextfragment.h b/src/core/textrenderer/qgstextfragment.h index ede938ef210..71a10f2023d 100644 --- a/src/core/textrenderer/qgstextfragment.h +++ b/src/core/textrenderer/qgstextfragment.h @@ -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; diff --git a/tests/src/python/test_qgstextblock.py b/tests/src/python/test_qgstextblock.py index 4dd73ce3b28..73ade38cee8 100644 --- a/tests/src/python/test_qgstextblock.py +++ b/tests/src/python/test_qgstextblock.py @@ -16,7 +16,8 @@ import qgis # NOQA from qgis.core import ( QgsTextBlock, - QgsTextFragment + QgsTextFragment, + QgsStringUtils ) from qgis.testing import start_app, unittest @@ -85,6 +86,13 @@ class TestQgsTextBlock(unittest.TestCase): 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() diff --git a/tests/src/python/test_qgstextdocument.py b/tests/src/python/test_qgstextdocument.py index 56c6a2b4bc3..a5a65f166c5 100644 --- a/tests/src/python/test_qgstextdocument.py +++ b/tests/src/python/test_qgstextdocument.py @@ -18,7 +18,8 @@ from qgis.core import ( QgsTextDocument, QgsTextBlock, QgsTextFragment, - QgsTextCharacterFormat + QgsTextCharacterFormat, + QgsStringUtils ) from qgis.testing import start_app, unittest @@ -160,6 +161,11 @@ class TestQgsTextDocument(unittest.TestCase): 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() diff --git a/tests/src/python/test_qgstextfragment.py b/tests/src/python/test_qgstextfragment.py index e53f8097d0d..a54a9585b95 100644 --- a/tests/src/python/test_qgstextfragment.py +++ b/tests/src/python/test_qgstextfragment.py @@ -16,7 +16,8 @@ import qgis # NOQA 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 @@ class TestQgsTextFragment(unittest.TestCase): 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()