Skip to content
Permalink
Browse files

Super-basic title-case conversion method

No grammatical parsing, so only useful for very simple cases
  • Loading branch information
nyalldawson committed May 15, 2018
1 parent ff35e69 commit 6c02d0534adebfe73e1580a8bb6dcbee3708348d
Showing with 33 additions and 0 deletions.
  1. +1 −0 python/core/auto_generated/qgsstringutils.sip.in
  2. +31 −0 src/core/qgsstringutils.cpp
  3. +1 −0 src/core/qgsstringutils.h
@@ -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.
};

/**

0 comments on commit 6c02d05

Please sign in to comment.
You can’t perform that action at this time.