mirror of
https://github.com/qgis/QGIS.git
synced 2025-04-13 00:03:09 -04:00
Super-basic title-case conversion method
No grammatical parsing, so only useful for very simple cases
This commit is contained in:
parent
ff35e694bb
commit
6c02d0534a
@ -174,6 +174,7 @@ class QgsStringUtils
|
||||
AllUppercase,
|
||||
AllLowercase,
|
||||
ForceFirstLetterToCapital,
|
||||
TitleCase,
|
||||
};
|
||||
|
||||
static QString capitalize( const QString &string, Capitalization capitalization );
|
||||
|
@ -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;
|
||||
|
@ -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.
|
||||
};
|
||||
|
||||
/**
|
||||
|
Loading…
x
Reference in New Issue
Block a user