Skip to content

Commit b79e9e6

Browse files
mhugentnyalldawson
authored andcommitted
Don't add extra linebreak in print composer tables
Check if remaining text is short enough to go in one line. Fixes #20546 (cherry picked from commit a774499)
1 parent 2979a8f commit b79e9e6

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

src/core/layout/qgslayouttable.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1157,6 +1157,12 @@ QString QgsLayoutTable::wrappedText( const QString &value, double columnWidth, c
11571157
int lastPos = remainingText.lastIndexOf( ' ' );
11581158
while ( lastPos > -1 )
11591159
{
1160+
//check if remaining text is short enough to go in one line
1161+
if ( !textRequiresWrapping( remainingText, columnWidth, font ) )
1162+
{
1163+
break;
1164+
}
1165+
11601166
if ( !textRequiresWrapping( remainingText.left( lastPos ), columnWidth, font ) )
11611167
{
11621168
outLines << remainingText.left( lastPos );

tests/src/core/testqgslayouttable.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ class TestQgsLayoutTable : public QObject
7171
void cellStyles(); //test cell styles
7272
void cellStylesRender(); //test rendering cell styles
7373
void dataDefinedSource();
74+
void wrappedText();
7475

7576
private:
7677
QgsVectorLayer *mVectorLayer = nullptr;
@@ -1388,5 +1389,19 @@ void TestQgsLayoutTable::dataDefinedSource()
13881389
QCOMPARE( table->contents().at( 0 ), QVector< QVariant >() << 1 << 2 << 3 );
13891390
}
13901391

1392+
void TestQgsLayoutTable::wrappedText()
1393+
{
1394+
QgsProject p;
1395+
QgsLayout l( &p );
1396+
QgsLayoutItemAttributeTable *t = new QgsLayoutItemAttributeTable( &l );
1397+
t->setWrapBehavior( QgsLayoutTable::WrapText );
1398+
1399+
QFont f;
1400+
QString sourceText( "Lorem ipsum dolor sit amet, consectetur adipisici elit, sed eiusmod tempor incidunt ut labore et dolore magna aliqua" );
1401+
QString wrapText = t->wrappedText( sourceText, 100 /*columnWidth*/, f );
1402+
//there should be no line break before the last word (bug #20546)
1403+
QVERIFY( !wrapText.endsWith( "\naliqua" ) );
1404+
}
1405+
13911406
QGSTEST_MAIN( TestQgsLayoutTable )
13921407
#include "testqgslayouttable.moc"

0 commit comments

Comments
 (0)