Skip to content
Permalink
Browse files

[expression] rework the wordwrap to allow for maximum / minimum line …

…length to wrap, fix test and add two more
  • Loading branch information
nirvn committed Feb 17, 2014
1 parent 3cfee81 commit d1c61a02c1f0cc953ba82481b95cda08963323a5
Showing with 24 additions and 7 deletions.
  1. +4 −3 resources/function_help/wordwrap
  2. +17 −3 src/core/qgsexpression.cpp
  3. +3 −1 tests/src/core/testqgsexpression.cpp
@@ -2,14 +2,15 @@
Returns a string wrapped to a minimum number of characters.

<p><h4>Syntax</h4>
replace(<i>string,delimiter_string,minimum_characters</i>)</p>
replace(<i>string,delimiter_string,wrap_characters</i>)</p>

<p><h4>Arguments</h4>
<!-- List args for functions here-->
<i> string</i> &rarr; is string. The string to be wrapped.<br>
<i> delimiter_string</i> &rarr; is string. The delimiter character(s) to break to a new line.<br>
<i> minimum_characters</i> &rarr; is number. The minimum number of characters required to allow for break to a new line<br></p>
<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>

<p><h4>Example</h4>
<!-- Show example of function.-->
wordwrap('UNIVERSITY OF QGIS',' ',3) &rarr; 'UNIVERSITY\nOF QGIS'</p>
wordwrap('UNIVERSITY OF QGIS',' ',13) &rarr; 'UNIVERSITY OF\nQGIS'<br>
wordwrap('UNIVERSITY OF QGIS',' ',-3) &rarr; 'UNIVERSITY\nOF QGIS'</p>
@@ -639,20 +639,34 @@ static QVariant fcnWordwrap( const QVariantList& values, const QgsFeature* , Qgs
QString delimiterstr = getStringValue ( values.at(1), parent );
int delimiterlength = delimiterstr.length();

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

QStringList lines = str.split( "\n" );
int strlength, strcurrent, strhit;
int strlength, strcurrent, strhit, lasthit;

for ( int i = 0; i < lines.size(); i++ )
{
strlength = lines[i].length();
strcurrent = 0;
strhit = 0;
lasthit = 0;

while (strcurrent < strlength)
{
strhit = lines[i].indexOf( delimiterstr, strcurrent + wrapmin );
if (wrap > 0)
{
//first try to locate delimiter backwards
strhit = lines[i].lastIndexOf( delimiterstr, strcurrent + wrap);
if (strhit == lasthit || strhit == -1) {
//if no new backward delimiter found, try to locate forward
strhit = lines[i].indexOf( delimiterstr, strcurrent + qAbs(wrap) );
}
lasthit = strhit;
} else {
strhit = lines[i].indexOf( delimiterstr, strcurrent + qAbs(wrap) );
}
if (strhit > -1) {
newstr.append( lines[i].midRef( strcurrent , strhit - strcurrent ) );
newstr.append( "\n" );
@@ -315,7 +315,9 @@ class TestQgsExpression: public QObject
QTest::newRow( "title" ) << "title(' HeLlO WORLD ')" << false << QVariant( " Hello World " );
QTest::newRow( "trim" ) << "trim(' Test String ')" << false << QVariant( "Test String" );
QTest::newRow( "trim empty string" ) << "trim('')" << false << QVariant( "" );
QTest::newRow( "wordwrap" ) << "wordwrap('university of qgis')" << false << QVariant( "university\nof qgis" );
QTest::newRow( "wordwrap" ) << "wordwrap('university of qgis',' ',13)" << false << QVariant( "university of\nqgis" );
QTest::newRow( "wordwrap" ) << "wordwrap('university of qgis',' ',-3)" << false << QVariant( "university\nof qgis" );
QTest::newRow( "wordwrap" ) << "wordwrap('university of qgis\nsupports many multiline',' ',-5)" << false << QVariant( "university\nof qgis\nsupports\nmany multiline" );
QTest::newRow( "format" ) << "format('%1 %2 %3 %1', 'One', 'Two', 'Three')" << false << QVariant( "One Two Three One" );

// implicit conversions

0 comments on commit d1c61a0

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