Skip to content

Commit

Permalink
[expression] implement a wordwrap function (feature request #9412)
Browse files Browse the repository at this point in the history
  • Loading branch information
nirvn committed Feb 21, 2014
1 parent 882dd0c commit 6319add
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/core/qgsexpression.cpp
Expand Up @@ -631,6 +631,32 @@ static QVariant fcnTrim( const QVariantList& values, const QgsFeature* , QgsExpr
return QVariant( str.trimmed() );
}

static QVariant fcnWordwrap( const QVariantList& values, const QgsFeature* , QgsExpression* parent )
{
QString str = getStringValue( values.at( 0 ), parent );
QString delimiterstr = getStringValue ( values.at(1), parent );
QString wrapstr = "\n";
QString newstr;

int length = str.length();
int min = getIntValue( values.at(2), parent );
int current = 0;
int hit = 0;

while (current < length) {
hit = str.indexOf( delimiterstr, current + min );
if (hit > -1) {
newstr.append( str.midRef( current , hit - current ) );
newstr.append( wrapstr );
current = hit + 1;
} else {
newstr.append( str.midRef( current ) );
current = length;
}
}
return QVariant( newstr );
}

static QVariant fcnLength( const QVariantList& values, const QgsFeature* , QgsExpression* parent )
{
QString str = getStringValue( values.at( 0 ), parent );
Expand Down Expand Up @@ -1484,6 +1510,7 @@ const QList<QgsExpression::Function*> &QgsExpression::Functions()
<< new StaticFunction( "upper", 1, fcnUpper, "String" )
<< new StaticFunction( "title", 1, fcnTitle, "String" )
<< new StaticFunction( "trim", 1, fcnTrim, "String" )
<< new StaticFunction( "wordwrap", 3, fcnWordwrap, "String" )
<< new StaticFunction( "length", 1, fcnLength, "String" )
<< new StaticFunction( "replace", 3, fcnReplace, "String" )
<< new StaticFunction( "regexp_replace", 3, fcnRegexpReplace, "String" )
Expand Down

0 comments on commit 6319add

Please sign in to comment.