Skip to content

Commit 484ba6f

Browse files
committedSep 16, 2020
[labeling] Add a new capitalization option for "Title Case", and
rename the confusing "Capitalize First Letter" option to "Force First Letter to Capital" This change is intended to clarify the role of the "capitalize first letter" option, and to provide an option which actually does what users expect the "capitalize first letter" option to do. Fixes #16539
1 parent 6bd3dc5 commit 484ba6f

File tree

10 files changed

+134
-26
lines changed

10 files changed

+134
-26
lines changed
 

‎python/core/auto_generated/textrenderer/qgstextformat.sip.in

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,24 @@ Sets the ``orientation`` for the text.
368368
.. seealso:: :py:func:`orientation`
369369

370370
.. versionadded:: 3.10
371+
%End
372+
373+
QgsStringUtils::Capitalization capitalization() const;
374+
%Docstring
375+
Returns the text capitalization style.
376+
377+
.. seealso:: :py:func:`setCapitalization`
378+
379+
.. versionadded:: 3.16
380+
%End
381+
382+
void setCapitalization( QgsStringUtils::Capitalization capitalization );
383+
%Docstring
384+
Sets the text ``capitalization`` style.
385+
386+
.. seealso:: :py:func:`capitalization`
387+
388+
.. versionadded:: 3.16
371389
%End
372390

373391
bool allowHtmlFormatting() const;

‎src/core/labeling/qgspallabeling.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ void QgsPalLayerSettings::initPropertyDefinitions()
129129
{ QgsPalLayerSettings::FontSizeUnit, QgsPropertyDefinition( "FontSizeUnit", QObject::tr( "Font size units" ), QgsPropertyDefinition::RenderUnits, origin ) },
130130
{ QgsPalLayerSettings::FontTransp, QgsPropertyDefinition( "FontTransp", QObject::tr( "Text transparency" ), QgsPropertyDefinition::Opacity, origin ) },
131131
{ QgsPalLayerSettings::FontOpacity, QgsPropertyDefinition( "FontOpacity", QObject::tr( "Text opacity" ), QgsPropertyDefinition::Opacity, origin ) },
132-
{ QgsPalLayerSettings::FontCase, QgsPropertyDefinition( "FontCase", QgsPropertyDefinition::DataTypeString, QObject::tr( "Font case" ), QObject::tr( "string " ) + QStringLiteral( "[<b>NoChange</b>|<b>Upper</b>|<br><b>Lower</b>|<b>Capitalize</b>]" ), origin ) },
132+
{ QgsPalLayerSettings::FontCase, QgsPropertyDefinition( "FontCase", QgsPropertyDefinition::DataTypeString, QObject::tr( "Font case" ), QObject::tr( "string " ) + QStringLiteral( "[<b>NoChange</b>|<b>Upper</b>|<br><b>Lower</b>|<b>Title</b>|<b>Capitalize</b>]" ), origin ) },
133133
{ QgsPalLayerSettings::FontLetterSpacing, QgsPropertyDefinition( "FontLetterSpacing", QObject::tr( "Letter spacing" ), QgsPropertyDefinition::Double, origin ) },
134134
{ QgsPalLayerSettings::FontWordSpacing, QgsPropertyDefinition( "FontWordSpacing", QObject::tr( "Word spacing" ), QgsPropertyDefinition::Double, origin ) },
135135
{ QgsPalLayerSettings::FontBlendMode, QgsPropertyDefinition( "FontBlendMode", QObject::tr( "Text blend mode" ), QgsPropertyDefinition::BlendMode, origin ) },
@@ -1834,9 +1834,9 @@ void QgsPalLayerSettings::registerFeature( const QgsFeature &f, QgsRenderContext
18341834
}
18351835

18361836
// apply capitalization
1837-
QgsStringUtils::Capitalization capitalization = QgsStringUtils::MixedCase;
1837+
QgsStringUtils::Capitalization capitalization = mFormat.capitalization();
18381838
// maintain API - capitalization may have been set in textFont
1839-
if ( mFormat.font().capitalization() != QFont::MixedCase )
1839+
if ( capitalization == QgsStringUtils::MixedCase && mFormat.font().capitalization() != QFont::MixedCase )
18401840
{
18411841
capitalization = static_cast< QgsStringUtils::Capitalization >( mFormat.font().capitalization() );
18421842
}
@@ -1867,6 +1867,10 @@ void QgsPalLayerSettings::registerFeature( const QgsFeature &f, QgsRenderContext
18671867
{
18681868
capitalization = QgsStringUtils::ForceFirstLetterToCapital;
18691869
}
1870+
else if ( fcase.compare( QLatin1String( "Title" ), Qt::CaseInsensitive ) == 0 )
1871+
{
1872+
capitalization = QgsStringUtils::TitleCase;
1873+
}
18701874
}
18711875
}
18721876
}

‎src/core/labeling/qgsvectorlayerlabeling.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -269,15 +269,18 @@ void QgsAbstractVectorLayerLabeling::writeTextSymbolizer( QDomNode &parent, QgsP
269269
}
270270
else
271271
{
272-
if ( font.capitalization() == QFont::AllUppercase )
272+
QgsStringUtils::Capitalization capitalization = format.capitalization();
273+
if ( capitalization == QgsStringUtils::MixedCase && font.capitalization() != QFont::MixedCase )
274+
capitalization = static_cast< QgsStringUtils::Capitalization >( font.capitalization() );
275+
if ( capitalization == QgsStringUtils::AllUppercase )
273276
{
274277
appendSimpleFunction( doc, labelElement, QStringLiteral( "strToUpperCase" ), settings.fieldName );
275278
}
276-
else if ( font.capitalization() == QFont::AllLowercase )
279+
else if ( capitalization == QgsStringUtils::AllLowercase )
277280
{
278281
appendSimpleFunction( doc, labelElement, QStringLiteral( "strToLowerCase" ), settings.fieldName );
279282
}
280-
else if ( font.capitalization() == QFont::Capitalize )
283+
else if ( capitalization == QgsStringUtils::ForceFirstLetterToCapital )
281284
{
282285
appendSimpleFunction( doc, labelElement, QStringLiteral( "strCapitalize" ), settings.fieldName );
283286
}

‎src/core/textrenderer/qgstextformat.cpp

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ bool QgsTextFormat::operator==( const QgsTextFormat &other ) const
7474
|| d->orientation != other.orientation()
7575
|| d->previewBackgroundColor != other.previewBackgroundColor()
7676
|| d->allowHtmlFormatting != other.allowHtmlFormatting()
77+
|| d->capitalization != other.capitalization()
7778
|| mBufferSettings != other.mBufferSettings
7879
|| mBackgroundSettings != other.mBackgroundSettings
7980
|| mShadowSettings != other.mShadowSettings
@@ -283,6 +284,19 @@ void QgsTextFormat::setOrientation( TextOrientation orientation )
283284
d->orientation = orientation;
284285
}
285286

287+
QgsStringUtils::Capitalization QgsTextFormat::capitalization() const
288+
{
289+
// bit of complexity here to maintain API..
290+
return d->capitalization == QgsStringUtils::MixedCase && d->textFont.capitalization() != QFont::MixedCase ? static_cast< QgsStringUtils::Capitalization >( d->textFont.capitalization() ) : d->capitalization ;
291+
}
292+
293+
void QgsTextFormat::setCapitalization( QgsStringUtils::Capitalization capitalization )
294+
{
295+
d->isValid = true;
296+
d->capitalization = capitalization;
297+
d->textFont.setCapitalization( QFont::MixedCase );
298+
}
299+
286300
bool QgsTextFormat::allowHtmlFormatting() const
287301
{
288302
return d->allowHtmlFormatting;
@@ -365,7 +379,7 @@ void QgsTextFormat::readFromLayer( QgsVectorLayer *layer )
365379
d->textFont = QFont( fontFamily, d->fontSize, fontWeight, fontItalic );
366380
d->textNamedStyle = QgsFontUtils::translateNamedStyle( layer->customProperty( QStringLiteral( "labeling/namedStyle" ), QVariant( "" ) ).toString() );
367381
QgsFontUtils::updateFontViaStyle( d->textFont, d->textNamedStyle ); // must come after textFont.setPointSizeF()
368-
d->textFont.setCapitalization( static_cast< QFont::Capitalization >( layer->customProperty( QStringLiteral( "labeling/fontCapitals" ), QVariant( 0 ) ).toUInt() ) );
382+
d->capitalization = static_cast< QgsStringUtils::Capitalization >( layer->customProperty( QStringLiteral( "labeling/fontCapitals" ), QVariant( 0 ) ).toUInt() );
369383
d->textFont.setUnderline( layer->customProperty( QStringLiteral( "labeling/fontUnderline" ) ).toBool() );
370384
d->textFont.setStrikeOut( layer->customProperty( QStringLiteral( "labeling/fontStrikeout" ) ).toBool() );
371385
d->textFont.setLetterSpacing( QFont::AbsoluteSpacing, layer->customProperty( QStringLiteral( "labeling/fontLetterSpacing" ), QVariant( 0.0 ) ).toDouble() );
@@ -453,7 +467,6 @@ void QgsTextFormat::readXml( const QDomElement &elem, const QgsReadWriteContext
453467
d->textFont.setPointSizeF( d->fontSize ); //double precision needed because of map units
454468
d->textNamedStyle = QgsFontUtils::translateNamedStyle( textStyleElem.attribute( QStringLiteral( "namedStyle" ) ) );
455469
QgsFontUtils::updateFontViaStyle( d->textFont, d->textNamedStyle ); // must come after textFont.setPointSizeF()
456-
d->textFont.setCapitalization( static_cast< QFont::Capitalization >( textStyleElem.attribute( QStringLiteral( "fontCapitals" ), QStringLiteral( "0" ) ).toUInt() ) );
457470
d->textFont.setUnderline( textStyleElem.attribute( QStringLiteral( "fontUnderline" ) ).toInt() );
458471
d->textFont.setStrikeOut( textStyleElem.attribute( QStringLiteral( "fontStrikeout" ) ).toInt() );
459472
d->textFont.setKerning( textStyleElem.attribute( QStringLiteral( "fontKerning" ), QStringLiteral( "1" ) ).toInt() );
@@ -484,6 +497,11 @@ void QgsTextFormat::readXml( const QDomElement &elem, const QgsReadWriteContext
484497
d->multilineHeight = textStyleElem.attribute( QStringLiteral( "multilineHeight" ), QStringLiteral( "1" ) ).toDouble();
485498
}
486499

500+
if ( textStyleElem.hasAttribute( QStringLiteral( "capitalization" ) ) )
501+
d->capitalization = static_cast< QgsStringUtils::Capitalization >( textStyleElem.attribute( QStringLiteral( "capitalization" ), QString::number( QgsStringUtils::MixedCase ) ).toInt() );
502+
else
503+
d->capitalization = static_cast< QgsStringUtils::Capitalization >( textStyleElem.attribute( QStringLiteral( "fontCapitals" ), QStringLiteral( "0" ) ).toUInt() );
504+
487505
d->allowHtmlFormatting = textStyleElem.attribute( QStringLiteral( "allowHtml" ), QStringLiteral( "0" ) ).toInt();
488506

489507
if ( textStyleElem.firstChildElement( QStringLiteral( "text-buffer" ) ).isNull() )
@@ -549,7 +567,6 @@ QDomElement QgsTextFormat::writeXml( QDomDocument &doc, const QgsReadWriteContex
549567
textStyleElem.setAttribute( QStringLiteral( "fontUnderline" ), d->textFont.underline() );
550568
textStyleElem.setAttribute( QStringLiteral( "textColor" ), QgsSymbolLayerUtils::encodeColor( d->textColor ) );
551569
textStyleElem.setAttribute( QStringLiteral( "previewBkgrdColor" ), QgsSymbolLayerUtils::encodeColor( d->previewBackgroundColor ) );
552-
textStyleElem.setAttribute( QStringLiteral( "fontCapitals" ), static_cast< unsigned int >( d->textFont.capitalization() ) );
553570
textStyleElem.setAttribute( QStringLiteral( "fontLetterSpacing" ), d->textFont.letterSpacing() );
554571
textStyleElem.setAttribute( QStringLiteral( "fontWordSpacing" ), d->textFont.wordSpacing() );
555572
textStyleElem.setAttribute( QStringLiteral( "fontKerning" ), d->textFont.kerning() );
@@ -558,6 +575,7 @@ QDomElement QgsTextFormat::writeXml( QDomDocument &doc, const QgsReadWriteContex
558575
textStyleElem.setAttribute( QStringLiteral( "blendMode" ), QgsPainting::getBlendModeEnum( d->blendMode ) );
559576
textStyleElem.setAttribute( QStringLiteral( "multilineHeight" ), d->multilineHeight );
560577
textStyleElem.setAttribute( QStringLiteral( "allowHtml" ), d->allowHtmlFormatting ? QStringLiteral( "1" ) : QStringLiteral( "0" ) );
578+
textStyleElem.setAttribute( QStringLiteral( "capitalization" ), QString::number( static_cast< int >( d->capitalization ) ) );
561579

562580
QDomElement ddElem = doc.createElement( QStringLiteral( "dd_properties" ) );
563581
d->mDataDefinedProperties.writeXml( ddElem, QgsPalLayerSettings::propertyDefinitions() );

‎src/core/textrenderer/qgstextformat.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "qgstextbackgroundsettings.h"
2424
#include "qgstextshadowsettings.h"
2525
#include "qgstextmasksettings.h"
26+
#include "qgsstringutils.h"
2627

2728
#include <QSharedDataPointer>
2829

@@ -339,6 +340,22 @@ class CORE_EXPORT QgsTextFormat
339340
*/
340341
void setOrientation( TextOrientation orientation );
341342

343+
/**
344+
* Returns the text capitalization style.
345+
*
346+
* \see setCapitalization()
347+
* \since QGIS 3.16
348+
*/
349+
QgsStringUtils::Capitalization capitalization() const;
350+
351+
/**
352+
* Sets the text \a capitalization style.
353+
*
354+
* \see capitalization()
355+
* \since QGIS 3.16
356+
*/
357+
void setCapitalization( QgsStringUtils::Capitalization capitalization );
358+
342359
/**
343360
* Returns TRUE if text should be treated as a HTML document and HTML tags should be used for formatting
344361
* the rendered text.

‎src/core/textrenderer/qgstextrenderer_p.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
#include "qgsapplication.h"
2929
#include "qgspainteffect.h"
3030
#include "qgssymbollayerreference.h"
31+
#include "qgsstringutils.h"
32+
3133
#include <QSharedData>
3234
#include <QPainter>
3335

@@ -271,6 +273,7 @@ class QgsTextSettingsPrivate : public QSharedData
271273
, orientation( other.orientation )
272274
, previewBackgroundColor( other.previewBackgroundColor )
273275
, allowHtmlFormatting( other.allowHtmlFormatting )
276+
, capitalization( other.capitalization )
274277
, mDataDefinedProperties( other.mDataDefinedProperties )
275278
{
276279
}
@@ -287,8 +290,8 @@ class QgsTextSettingsPrivate : public QSharedData
287290
double multilineHeight = 1.0 ; //0.0 to 10.0, leading between lines as multiplyer of line height
288291
QgsTextFormat::TextOrientation orientation = QgsTextFormat::HorizontalOrientation;
289292
QColor previewBackgroundColor = Qt::white;
290-
291293
bool allowHtmlFormatting = false;
294+
QgsStringUtils::Capitalization capitalization = QgsStringUtils::MixedCase;
292295

293296
//! Property collection for data defined settings
294297
QgsPropertyCollection mDataDefinedProperties;

‎src/gui/qgstextformatwidget.cpp

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@ void QgsTextFormatWidget::initWidget()
7070

7171
connect( mShapeSVGPathLineEdit, &QLineEdit::textChanged, this, &QgsTextFormatWidget::mShapeSVGPathLineEdit_textChanged );
7272
connect( mFontSizeSpinBox, static_cast < void ( QDoubleSpinBox::* )( double ) > ( &QDoubleSpinBox::valueChanged ), this, &QgsTextFormatWidget::mFontSizeSpinBox_valueChanged );
73-
connect( mFontCapitalsComboBox, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsTextFormatWidget::mFontCapitalsComboBox_currentIndexChanged );
7473
connect( mFontFamilyCmbBx, &QFontComboBox::currentFontChanged, this, &QgsTextFormatWidget::mFontFamilyCmbBx_currentFontChanged );
7574
connect( mFontStyleComboBox, &QComboBox::currentTextChanged, this, &QgsTextFormatWidget::mFontStyleComboBox_currentIndexChanged );
7675
connect( mFontUnderlineBtn, &QToolButton::toggled, this, &QgsTextFormatWidget::mFontUnderlineBtn_toggled );
@@ -890,6 +889,7 @@ void QgsTextFormatWidget::updateWidgetForFormat( const QgsTextFormat &format )
890889
mFontLetterSpacingSpinBox->setValue( format.font().letterSpacing() );
891890
whileBlocking( mKerningCheckBox )->setChecked( format.font().kerning() );
892891

892+
whileBlocking( mFontCapitalsComboBox )->setCurrentIndex( mFontCapitalsComboBox->findData( format.capitalization() ) );
893893
QgsFontUtils::updateFontViaStyle( mRefFont, format.namedStyle() );
894894
updateFont( mRefFont );
895895

@@ -1025,6 +1025,7 @@ QgsTextFormat QgsTextFormatWidget::format( bool includeDataDefinedProperties ) c
10251025
format.setPreviewBackgroundColor( mPreviewBackgroundColor );
10261026
format.setOrientation( static_cast< QgsTextFormat::TextOrientation >( mTextOrientationComboBox->currentData().toInt() ) );
10271027
format.setAllowHtmlFormatting( mHtmlFormattingCheckBox->isChecked( ) );
1028+
format.setCapitalization( static_cast< QgsStringUtils::Capitalization >( mFontCapitalsComboBox->currentData().toInt() ) );
10281029

10291030
// buffer
10301031
QgsTextBufferSettings buffer;
@@ -1218,8 +1219,6 @@ void QgsTextFormatWidget::updateFont( const QFont &font )
12181219
blockFontChangeSignals( true );
12191220
mFontFamilyCmbBx->setCurrentFont( mRefFont );
12201221
populateFontStyleComboBox();
1221-
int idx = mFontCapitalsComboBox->findData( QVariant( static_cast< unsigned int >( mRefFont.capitalization() ) ) );
1222-
mFontCapitalsComboBox->setCurrentIndex( idx == -1 ? 0 : idx );
12231222
mFontUnderlineBtn->setChecked( mRefFont.underline() );
12241223
mFontStrikethroughBtn->setChecked( mRefFont.strikeOut() );
12251224
mKerningCheckBox->setChecked( mRefFont.kerning() );
@@ -1417,13 +1416,14 @@ void QgsTextFormatWidget::updatePlacementWidgets()
14171416

14181417
void QgsTextFormatWidget::populateFontCapitalsComboBox()
14191418
{
1420-
mFontCapitalsComboBox->addItem( tr( "No Change" ), QVariant( 0 ) );
1421-
mFontCapitalsComboBox->addItem( tr( "All Uppercase" ), QVariant( 1 ) );
1422-
mFontCapitalsComboBox->addItem( tr( "All Lowercase" ), QVariant( 2 ) );
1419+
mFontCapitalsComboBox->addItem( tr( "No Change" ), QgsStringUtils::MixedCase );
1420+
mFontCapitalsComboBox->addItem( tr( "All Uppercase" ), QgsStringUtils::AllUppercase );
1421+
mFontCapitalsComboBox->addItem( tr( "All Lowercase" ), QgsStringUtils::AllLowercase );
14231422
// Small caps doesn't work right with QPainterPath::addText()
14241423
// https://bugreports.qt.io/browse/QTBUG-13965
14251424
// mFontCapitalsComboBox->addItem( tr( "Small Caps" ), QVariant( 3 ) );
1426-
mFontCapitalsComboBox->addItem( tr( "Capitalize First Letter" ), QVariant( 4 ) );
1425+
mFontCapitalsComboBox->addItem( tr( "Title Case" ), QgsStringUtils::TitleCase );
1426+
mFontCapitalsComboBox->addItem( tr( "Force First Letter to Capital" ), QgsStringUtils::ForceFirstLetterToCapital );
14271427
}
14281428

14291429
void QgsTextFormatWidget::populateFontStyleComboBox()
@@ -1459,13 +1459,6 @@ void QgsTextFormatWidget::mFontSizeSpinBox_valueChanged( double d )
14591459
updateFont( mRefFont );
14601460
}
14611461

1462-
void QgsTextFormatWidget::mFontCapitalsComboBox_currentIndexChanged( int index )
1463-
{
1464-
int capitalsindex = mFontCapitalsComboBox->itemData( index ).toInt();
1465-
mRefFont.setCapitalization( static_cast< QFont::Capitalization >( capitalsindex ) );
1466-
updateFont( mRefFont );
1467-
}
1468-
14691462
void QgsTextFormatWidget::mFontFamilyCmbBx_currentFontChanged( const QFont &f )
14701463
{
14711464
mRefFont.setFamily( f.family() );

‎src/gui/qgstextformatwidget.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,6 @@ class GUI_EXPORT QgsTextFormatWidget : public QWidget, public QgsExpressionConte
272272
void onSubstitutionsChanged( const QgsStringReplacementCollection &substitutions );
273273
void previewScaleChanged( double scale );
274274
void mFontSizeSpinBox_valueChanged( double d );
275-
void mFontCapitalsComboBox_currentIndexChanged( int index );
276275
void mFontFamilyCmbBx_currentFontChanged( const QFont &f );
277276
void mFontStyleComboBox_currentIndexChanged( const QString &text );
278277
void mFontUnderlineBtn_toggled( bool ckd );

‎tests/src/core/testqgslabelingengine.cpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -650,8 +650,19 @@ void TestQgsLabelingEngine::testCapitalization()
650650
provider2->registerFeature( f, context );
651651
QCOMPARE( provider2->mLabels.at( 0 )->labelText(), QString( "A TEST LABEL" ) );
652652

653+
font.setCapitalization( QFont::MixedCase );
654+
format.setCapitalization( QgsStringUtils::AllUppercase );
655+
format.setFont( font );
656+
settings.setFormat( format );
657+
QgsVectorLayerLabelProvider *provider2b = new QgsVectorLayerLabelProvider( vl, QStringLiteral( "test2" ), true, &settings );
658+
engine.addProvider( provider2b );
659+
provider2b->prepare( context, attributes );
660+
provider2b->registerFeature( f, context );
661+
QCOMPARE( provider2b->mLabels.at( 0 )->labelText(), QString( "A TEST LABEL" ) );
662+
653663
//lowercase
654664
font.setCapitalization( QFont::AllLowercase );
665+
format.setCapitalization( QgsStringUtils::MixedCase );
655666
format.setFont( font );
656667
settings.setFormat( format );
657668
QgsVectorLayerLabelProvider *provider3 = new QgsVectorLayerLabelProvider( vl, QStringLiteral( "test3" ), true, &settings );
@@ -660,15 +671,46 @@ void TestQgsLabelingEngine::testCapitalization()
660671
provider3->registerFeature( f, context );
661672
QCOMPARE( provider3->mLabels.at( 0 )->labelText(), QString( "a test label" ) );
662673

674+
font.setCapitalization( QFont::MixedCase );
675+
format.setCapitalization( QgsStringUtils::AllLowercase );
676+
format.setFont( font );
677+
settings.setFormat( format );
678+
QgsVectorLayerLabelProvider *provider3b = new QgsVectorLayerLabelProvider( vl, QStringLiteral( "test3" ), true, &settings );
679+
engine.addProvider( provider3b );
680+
provider3b->prepare( context, attributes );
681+
provider3b->registerFeature( f, context );
682+
QCOMPARE( provider3b->mLabels.at( 0 )->labelText(), QString( "a test label" ) );
683+
663684
//first letter uppercase
664685
font.setCapitalization( QFont::Capitalize );
686+
format.setCapitalization( QgsStringUtils::MixedCase );
665687
format.setFont( font );
666688
settings.setFormat( format );
667689
QgsVectorLayerLabelProvider *provider4 = new QgsVectorLayerLabelProvider( vl, QStringLiteral( "test4" ), true, &settings );
668690
engine.addProvider( provider4 );
669691
provider4->prepare( context, attributes );
670692
provider4->registerFeature( f, context );
671693
QCOMPARE( provider4->mLabels.at( 0 )->labelText(), QString( "A TeSt LABEL" ) );
694+
695+
font.setCapitalization( QFont::MixedCase );
696+
format.setCapitalization( QgsStringUtils::ForceFirstLetterToCapital );
697+
format.setFont( font );
698+
settings.setFormat( format );
699+
QgsVectorLayerLabelProvider *provider4b = new QgsVectorLayerLabelProvider( vl, QStringLiteral( "test4" ), true, &settings );
700+
engine.addProvider( provider4b );
701+
provider4b->prepare( context, attributes );
702+
provider4b->registerFeature( f, context );
703+
QCOMPARE( provider4b->mLabels.at( 0 )->labelText(), QString( "A TeSt LABEL" ) );
704+
705+
settings.fieldName = QStringLiteral( "'A TEST LABEL'" );
706+
format.setCapitalization( QgsStringUtils::TitleCase );
707+
format.setFont( font );
708+
settings.setFormat( format );
709+
QgsVectorLayerLabelProvider *provider5 = new QgsVectorLayerLabelProvider( vl, QStringLiteral( "test4" ), true, &settings );
710+
engine.addProvider( provider5 );
711+
provider5->prepare( context, attributes );
712+
provider5->registerFeature( f, context );
713+
QCOMPARE( provider5->mLabels.at( 0 )->labelText(), QString( "A Test Label" ) );
672714
}
673715

674716
void TestQgsLabelingEngine::testNumberFormat()

‎tests/src/python/test_qgstextrenderer.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@
3333
QgsProperty,
3434
QgsFontUtils,
3535
QgsSymbolLayerId,
36-
QgsSymbolLayerReference)
36+
QgsSymbolLayerReference,
37+
QgsStringUtils)
3738
from qgis.PyQt.QtGui import (QColor, QPainter, QFont, QImage, QBrush, QPen, QFontMetricsF)
3839
from qgis.PyQt.QtCore import (Qt, QSizeF, QPointF, QRectF, QDir, QSize)
3940
from qgis.PyQt.QtXml import QDomDocument
@@ -141,6 +142,10 @@ def testValid(self):
141142
t.setOrientation(QgsTextFormat.VerticalOrientation)
142143
self.assertTrue(t.isValid())
143144

145+
t = QgsTextFormat()
146+
t.setCapitalization(QgsStringUtils.TitleCase)
147+
self.assertTrue(t.isValid())
148+
144149
t = QgsTextFormat()
145150
t.setAllowHtmlFormatting(True)
146151
self.assertTrue(t.isValid())
@@ -681,6 +686,7 @@ def createFormatSettings(self):
681686
font = getTestFont()
682687
font.setKerning(False)
683688
s.setFont(font)
689+
s.setCapitalization(QgsStringUtils.TitleCase)
684690
s.setNamedStyle('Italic')
685691
s.setSize(5)
686692
s.setSizeUnit(QgsUnitTypes.RenderPoints)
@@ -782,6 +788,10 @@ def testFormatEquality(self):
782788
self.assertNotEqual(s, s2)
783789
s = self.createFormatSettings()
784790

791+
s.setCapitalization(QgsStringUtils.ForceFirstLetterToCapital)
792+
self.assertNotEqual(s, s2)
793+
s = self.createFormatSettings()
794+
785795
s.dataDefinedProperties().setProperty(QgsPalLayerSettings.Bold, QgsProperty.fromExpression('1>3'))
786796
self.assertNotEqual(s, s2)
787797

@@ -807,6 +817,7 @@ def checkTextFormat(self, s):
807817
self.assertEqual(s.lineHeight(), 5)
808818
self.assertEqual(s.previewBackgroundColor().name(), '#6496c8')
809819
self.assertEqual(s.orientation(), QgsTextFormat.VerticalOrientation)
820+
self.assertEqual(s.capitalization(), QgsStringUtils.TitleCase)
810821
self.assertTrue(s.allowHtmlFormatting())
811822
self.assertEqual(s.dataDefinedProperties().property(QgsPalLayerSettings.Bold).expressionString(), '1>2')
812823

0 commit comments

Comments
 (0)
Please sign in to comment.