Skip to content

Commit a774499

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
1 parent 32b6599 commit a774499

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;
@@ -1412,5 +1413,19 @@ void TestQgsLayoutTable::dataDefinedSource()
14121413
QCOMPARE( table->contents().at( 0 ), QVector< QVariant >() << 1 << 2 << 3 );
14131414
}
14141415

1416+
void TestQgsLayoutTable::wrappedText()
1417+
{
1418+
QgsProject p;
1419+
QgsLayout l( &p );
1420+
QgsLayoutItemAttributeTable *t = new QgsLayoutItemAttributeTable( &l );
1421+
t->setWrapBehavior( QgsLayoutTable::WrapText );
1422+
1423+
QFont f;
1424+
QString sourceText( "Lorem ipsum dolor sit amet, consectetur adipisici elit, sed eiusmod tempor incidunt ut labore et dolore magna aliqua" );
1425+
QString wrapText = t->wrappedText( sourceText, 100 /*columnWidth*/, f );
1426+
//there should be no line break before the last word (bug #20546)
1427+
QVERIFY( !wrapText.endsWith( "\naliqua" ) );
1428+
}
1429+
14151430
QGSTEST_MAIN( TestQgsLayoutTable )
14161431
#include "testqgslayouttable.moc"

0 commit comments

Comments
 (0)