Skip to content

Commit 6c02d05

Browse files
committedMay 16, 2018
Super-basic title-case conversion method
No grammatical parsing, so only useful for very simple cases
1 parent ff35e69 commit 6c02d05

File tree

3 files changed

+33
-0
lines changed

3 files changed

+33
-0
lines changed
 

‎python/core/auto_generated/qgsstringutils.sip.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ class QgsStringUtils
174174
AllUppercase,
175175
AllLowercase,
176176
ForceFirstLetterToCapital,
177+
TitleCase,
177178
};
178179

179180
static QString capitalize( const QString &string, Capitalization capitalization );

‎src/core/qgsstringutils.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,37 @@ QString QgsStringUtils::capitalize( const QString &string, QgsStringUtils::Capit
5656
return temp;
5757
}
5858

59+
case TitleCase:
60+
{
61+
// yes, this is MASSIVELY simplifying the problem!!
62+
63+
static QStringList smallWords;
64+
if ( smallWords.empty() )
65+
{
66+
smallWords = QObject::tr( "a|an|and|as|at|but|by|en|for|if|in|nor|of|on|or|per|the|to|vs.|vs|via" ).split( '|' );
67+
}
68+
69+
const QStringList parts = string.split( ' ' );
70+
QString result;
71+
bool firstWord = true;
72+
for ( const QString &word : qgis::as_const( parts ) )
73+
{
74+
if ( word.isEmpty() )
75+
{
76+
result += ' ';
77+
}
78+
else if ( firstWord || !smallWords.contains( word ) )
79+
{
80+
result += ' ' + word.at( 0 ).toUpper() + word.mid( 1 );
81+
firstWord = false;
82+
}
83+
else
84+
{
85+
result += ' ' + word;
86+
}
87+
}
88+
return result;
89+
}
5990
}
6091
// no warnings
6192
return string;

‎src/core/qgsstringutils.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@ class CORE_EXPORT QgsStringUtils
188188
AllUppercase = QFont::AllUppercase, //!< Convert all characters to uppercase
189189
AllLowercase = QFont::AllLowercase, //!< Convert all characters to lowercase
190190
ForceFirstLetterToCapital = QFont::Capitalize, //!< Convert just the first letter of each word to uppercase, leave the rest untouched
191+
TitleCase = QFont::Capitalize + 1000, //!< Simple title case conversion - does not fully grammatically parse the text and uses simple rules only. Note that this method does not convert any characters to lowercase, it only uppercases required letters. Callers must ensure that input strings are already lowercased.
191192
};
192193

193194
/**

0 commit comments

Comments
 (0)
Please sign in to comment.