Super-basic title-case conversion method

No grammatical parsing, so only useful for very simple cases
This commit is contained in:
Nyall Dawson 2018-05-16 07:08:01 +10:00
parent ff35e694bb
commit 6c02d0534a
3 changed files with 33 additions and 0 deletions

View File

@ -174,6 +174,7 @@ class QgsStringUtils
AllUppercase,
AllLowercase,
ForceFirstLetterToCapital,
TitleCase,
};
static QString capitalize( const QString &string, Capitalization capitalization );

View File

@ -56,6 +56,37 @@ QString QgsStringUtils::capitalize( const QString &string, QgsStringUtils::Capit
return temp;
}
case TitleCase:
{
// yes, this is MASSIVELY simplifying the problem!!
static QStringList smallWords;
if ( smallWords.empty() )
{
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( '|' );
}
const QStringList parts = string.split( ' ' );
QString result;
bool firstWord = true;
for ( const QString &word : qgis::as_const( parts ) )
{
if ( word.isEmpty() )
{
result += ' ';
}
else if ( firstWord || !smallWords.contains( word ) )
{
result += ' ' + word.at( 0 ).toUpper() + word.mid( 1 );
firstWord = false;
}
else
{
result += ' ' + word;
}
}
return result;
}
}
// no warnings
return string;

View File

@ -188,6 +188,7 @@ class CORE_EXPORT QgsStringUtils
AllUppercase = QFont::AllUppercase, //!< Convert all characters to uppercase
AllLowercase = QFont::AllLowercase, //!< Convert all characters to lowercase
ForceFirstLetterToCapital = QFont::Capitalize, //!< Convert just the first letter of each word to uppercase, leave the rest untouched
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.
};
/**