Skip to content

Commit d1c61a0

Browse files
committedFeb 21, 2014
[expression] rework the wordwrap to allow for maximum / minimum line length to wrap, fix test and add two more
1 parent 3cfee81 commit d1c61a0

File tree

3 files changed

+24
-7
lines changed

3 files changed

+24
-7
lines changed
 

‎resources/function_help/wordwrap

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@
22
Returns a string wrapped to a minimum number of characters.
33

44
<p><h4>Syntax</h4>
5-
replace(<i>string,delimiter_string,minimum_characters</i>)</p>
5+
replace(<i>string,delimiter_string,wrap_characters</i>)</p>
66

77
<p><h4>Arguments</h4>
88
<!-- List args for functions here-->
99
<i> string</i> &rarr; is string. The string to be wrapped.<br>
1010
<i> delimiter_string</i> &rarr; is string. The delimiter character(s) to break to a new line.<br>
11-
<i> minimum_characters</i> &rarr; is number. The minimum number of characters required to allow for break to a new line<br></p>
11+
<i> wrap_characters</i> &rarr; is number. IF positive, the number represents the ideal maximum number of characters to wrap; if negative, the number represents the minimum number of characters to wrap.<br></p>
1212

1313
<p><h4>Example</h4>
1414
<!-- Show example of function.-->
15-
wordwrap('UNIVERSITY OF QGIS',' ',3) &rarr; 'UNIVERSITY\nOF QGIS'</p>
15+
wordwrap('UNIVERSITY OF QGIS',' ',13) &rarr; 'UNIVERSITY OF\nQGIS'<br>
16+
wordwrap('UNIVERSITY OF QGIS',' ',-3) &rarr; 'UNIVERSITY\nOF QGIS'</p>

‎src/core/qgsexpression.cpp

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -639,20 +639,34 @@ static QVariant fcnWordwrap( const QVariantList& values, const QgsFeature* , Qgs
639639
QString delimiterstr = getStringValue ( values.at(1), parent );
640640
int delimiterlength = delimiterstr.length();
641641

642-
int wrapmin = getIntValue( values.at(2), parent );
642+
//if wrap value is positive, wrap is (mostly) the maximum width before wrap on delimiter
643+
//if wrap value is negative, wrap is the minimum width before permitting wrap delimiter
644+
int wrap = getIntValue( values.at(2), parent );
643645

644646
QStringList lines = str.split( "\n" );
645-
int strlength, strcurrent, strhit;
647+
int strlength, strcurrent, strhit, lasthit;
646648

647649
for ( int i = 0; i < lines.size(); i++ )
648650
{
649651
strlength = lines[i].length();
650652
strcurrent = 0;
651653
strhit = 0;
654+
lasthit = 0;
652655

653656
while (strcurrent < strlength)
654657
{
655-
strhit = lines[i].indexOf( delimiterstr, strcurrent + wrapmin );
658+
if (wrap > 0)
659+
{
660+
//first try to locate delimiter backwards
661+
strhit = lines[i].lastIndexOf( delimiterstr, strcurrent + wrap);
662+
if (strhit == lasthit || strhit == -1) {
663+
//if no new backward delimiter found, try to locate forward
664+
strhit = lines[i].indexOf( delimiterstr, strcurrent + qAbs(wrap) );
665+
}
666+
lasthit = strhit;
667+
} else {
668+
strhit = lines[i].indexOf( delimiterstr, strcurrent + qAbs(wrap) );
669+
}
656670
if (strhit > -1) {
657671
newstr.append( lines[i].midRef( strcurrent , strhit - strcurrent ) );
658672
newstr.append( "\n" );

‎tests/src/core/testqgsexpression.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,9 @@ 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" );
318+
QTest::newRow( "wordwrap" ) << "wordwrap('university of qgis',' ',13)" << false << QVariant( "university of\nqgis" );
319+
QTest::newRow( "wordwrap" ) << "wordwrap('university of qgis',' ',-3)" << false << QVariant( "university\nof qgis" );
320+
QTest::newRow( "wordwrap" ) << "wordwrap('university of qgis\nsupports many multiline',' ',-5)" << false << QVariant( "university\nof qgis\nsupports\nmany multiline" );
319321
QTest::newRow( "format" ) << "format('%1 %2 %3 %1', 'One', 'Two', 'Three')" << false << QVariant( "One Two Three One" );
320322

321323
// implicit conversions

0 commit comments

Comments
 (0)
Please sign in to comment.