Skip to content

Commit 1fcb4f0

Browse files
committedFeb 21, 2014
[expression] more work on wordwrap function based on wonder-dk comments
1 parent 6319add commit 1fcb4f0

File tree

2 files changed

+26
-15
lines changed

2 files changed

+26
-15
lines changed
 

‎src/core/qgsexpression.cpp

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -634,25 +634,35 @@ static QVariant fcnTrim( const QVariantList& values, const QgsFeature* , QgsExpr
634634
static QVariant fcnWordwrap( const QVariantList& values, const QgsFeature* , QgsExpression* parent )
635635
{
636636
QString str = getStringValue( values.at( 0 ), parent );
637-
QString delimiterstr = getStringValue ( values.at(1), parent );
638-
QString wrapstr = "\n";
639637
QString newstr;
640638

641-
int length = str.length();
642-
int min = getIntValue( values.at(2), parent );
643-
int current = 0;
644-
int hit = 0;
645-
646-
while (current < length) {
647-
hit = str.indexOf( delimiterstr, current + min );
648-
if (hit > -1) {
649-
newstr.append( str.midRef( current , hit - current ) );
650-
newstr.append( wrapstr );
651-
current = hit + 1;
639+
QString delimiterstr = getStringValue ( values.at(1), parent );
640+
int delimiterlength = delimiterstr.length();
641+
642+
int wrapmin = getIntValue( values.at(2), parent );
643+
644+
QStringList lines = str.split( "\n" );
645+
int strlength, strcurrent, strhit;
646+
647+
for ( int i = 0; i < lines.size(); i++ )
648+
{
649+
strlength = lines[i].length();
650+
strcurrent = 0;
651+
strhit = 0;
652+
653+
while (strcurrent < strlength)
654+
{
655+
strhit = lines[i].indexOf( delimiterstr, strcurrent + wrapmin );
656+
if (strhit > -1) {
657+
newstr.append( lines[i].midRef( strcurrent , strhit - strcurrent ) );
658+
newstr.append( "\n" );
659+
strcurrent = strhit + delimiterlength;
652660
} else {
653-
newstr.append( str.midRef( current ) );
654-
current = length;
661+
newstr.append( lines[i].midRef( strcurrent ) );
662+
strcurrent = strlength;
655663
}
664+
}
665+
if (i < lines.size() - 1) newstr.append( "\n" );
656666
}
657667
return QVariant( newstr );
658668
}

‎tests/src/core/testqgsexpression.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,7 @@ class TestQgsExpression: public QObject
315315
QTest::newRow( "title" ) << "title(' HeLlO WORLD ')" << false << QVariant( " Hello World " );
316316
QTest::newRow( "trim" ) << "trim(' Test String ')" << false << QVariant( "Test String" );
317317
QTest::newRow( "trim empty string" ) << "trim('')" << false << QVariant( "" );
318+
QTest::newRow( "wordwrap" ) << "wordwrap('university of qgis')" << false << QVariant( "university\nof qgis" );
318319
QTest::newRow( "format" ) << "format('%1 %2 %3 %1', 'One', 'Two', 'Three')" << false << QVariant( "One Two Three One" );
319320

320321
// implicit conversions

0 commit comments

Comments
 (0)
Please sign in to comment.