@@ -470,13 +470,7 @@ void QgsLayoutTable::render( QgsLayoutItemRenderContext &context, const QRectF &
470
470
471
471
const QRectF textCell = QRectF ( currentX, currentY + mCellMargin , mMaxColumnWidthMap [col], cellHeaderHeight - 2 * mCellMargin );
472
472
473
- // disable text clipping to target text rectangle, because we manually clip to the full cell bounds below
474
- // and it's ok if text overlaps into the margin (e.g. extenders or italicized text)
475
- QStringList str = column.heading ().split ( ' \n ' );
476
- if ( ( mWrapBehavior != TruncateText || column.width () > 0 ) && textRequiresWrapping ( context.renderContext (), column.heading (), column.width (), headerFormat ) )
477
- {
478
- str = wrappedText ( context.renderContext (), column.heading (), column.width (), headerFormat );
479
- }
473
+ const QStringList str = column.heading ().split ( ' \n ' );
480
474
481
475
// scale to dots
482
476
{
@@ -485,7 +479,9 @@ void QgsLayoutTable::render( QgsLayoutItemRenderContext &context, const QRectF &
485
479
textCell.top () * context.renderContext ().scaleFactor (),
486
480
textCell.width () * context.renderContext ().scaleFactor (),
487
481
textCell.height () * context.renderContext ().scaleFactor () ), 0 ,
488
- headerAlign, str, context.renderContext (), headerFormat, true , QgsTextRenderer::AlignVCenter );
482
+ headerAlign, str, context.renderContext (), headerFormat, true , QgsTextRenderer::AlignVCenter,
483
+ mWrapBehavior == WrapText ? Qgis::TextRendererFlag::WrapLines : Qgis::TextRendererFlags ()
484
+ );
489
485
}
490
486
491
487
currentX += mMaxColumnWidthMap [ col ];
@@ -514,6 +510,7 @@ void QgsLayoutTable::render( QgsLayoutItemRenderContext &context, const QRectF &
514
510
515
511
for ( const QgsLayoutTableColumn &column : std::as_const ( mColumns ) )
516
512
{
513
+ ( void )column;
517
514
const QRectF fullCell ( currentX, currentY, mMaxColumnWidthMap [col] + 2 * mCellMargin , rowHeight );
518
515
// draw background
519
516
p->save ();
@@ -527,19 +524,12 @@ void QgsLayoutTable::render( QgsLayoutItemRenderContext &context, const QRectF &
527
524
528
525
QVariant cellContents = mTableContents .at ( row ).at ( col );
529
526
const QString localizedString { QgsExpressionUtils::toLocalizedString ( cellContents ) };
530
- QStringList str = localizedString.split ( ' \n ' );
527
+ const QStringList str = localizedString.split ( ' \n ' );
531
528
532
529
QgsTextFormat cellFormat = textFormatForCell ( row, col );
533
530
QgsExpressionContextScopePopper popper ( context.renderContext ().expressionContext (), scopeForCell ( row, col ) );
534
531
cellFormat.updateDataDefinedProperties ( context.renderContext () );
535
532
536
- // disable text clipping to target text rectangle, because we manually clip to the full cell bounds below
537
- // and it's ok if text overlaps into the margin (e.g. extenders or italicized text)
538
- if ( ( mWrapBehavior != TruncateText || column.width () > 0 ) && textRequiresWrapping ( context.renderContext (), localizedString, column.width (), cellFormat ) )
539
- {
540
- str = wrappedText ( context.renderContext (), localizedString, column.width (), cellFormat );
541
- }
542
-
543
533
p->save ();
544
534
p->setClipRect ( fullCell );
545
535
const QRectF textCell = QRectF ( currentX, currentY + mCellMargin , mMaxColumnWidthMap [col], rowHeight - 2 * mCellMargin );
@@ -559,7 +549,8 @@ void QgsLayoutTable::render( QgsLayoutItemRenderContext &context, const QRectF &
559
549
textCell.width () * context.renderContext ().scaleFactor (),
560
550
textCell.height () * context.renderContext ().scaleFactor () ), 0 ,
561
551
QgsTextRenderer::convertQtHAlignment ( horizontalAlignmentForCell ( row, col ) ), str, context.renderContext (), cellFormat, true ,
562
- QgsTextRenderer::convertQtVAlignment ( verticalAlignmentForCell ( row, col ) ) );
552
+ QgsTextRenderer::convertQtVAlignment ( verticalAlignmentForCell ( row, col ) ),
553
+ mWrapBehavior == WrapText ? Qgis::TextRendererFlag::WrapLines : Qgis::TextRendererFlags () );
563
554
}
564
555
p->restore ();
565
556
@@ -1189,16 +1180,16 @@ bool QgsLayoutTable::calculateMaxRowHeights()
1189
1180
{
1190
1181
heights[i] = 0 ;
1191
1182
}
1192
- else if ( textRequiresWrapping ( context, col.heading (), mColumns .at ( i ).width (), cellFormat ) )
1193
- {
1194
- // contents too wide for cell, need to wrap
1195
- heights[i] = QgsTextRenderer::textHeight ( context, cellFormat, wrappedText ( context, col.heading (), mColumns .at ( i ).width (), cellFormat ), QgsTextRenderer::Rect )
1196
- / context.convertToPainterUnits ( 1 , QgsUnitTypes::RenderMillimeters )
1197
- - headerDescentMm;
1198
- }
1199
1183
else
1200
1184
{
1201
- heights[i] = QgsTextRenderer::textHeight ( context, cellFormat, QStringList () << col.heading (), QgsTextRenderer::Rect ) / context.convertToPainterUnits ( 1 , QgsUnitTypes::RenderMillimeters )
1185
+ heights[i] = QgsTextRenderer::textHeight ( context,
1186
+ cellFormat,
1187
+ QStringList () << col.heading (), QgsTextRenderer::Rect,
1188
+ nullptr ,
1189
+ mWrapBehavior == WrapText ? Qgis::TextRendererFlag::WrapLines : Qgis::TextRendererFlags (),
1190
+ context.convertToPainterUnits ( mColumns .at ( i ).width (), QgsUnitTypes::RenderMillimeters )
1191
+ )
1192
+ / context.convertToPainterUnits ( 1 , QgsUnitTypes::RenderMillimeters )
1202
1193
- headerDescentMm;
1203
1194
}
1204
1195
i++;
@@ -1219,15 +1210,14 @@ bool QgsLayoutTable::calculateMaxRowHeights()
1219
1210
const double contentDescentMm = QgsTextRenderer::fontMetrics ( context, cellFormat, QgsTextRenderer::FONT_WORKAROUND_SCALE ).descent () / QgsTextRenderer::FONT_WORKAROUND_SCALE / context.convertToPainterUnits ( 1 , QgsUnitTypes::RenderMillimeters );
1220
1211
const QString localizedString { QgsExpressionUtils::toLocalizedString ( *colIt ) };
1221
1212
1222
- if ( textRequiresWrapping ( context, localizedString, mColumns .at ( i ).width (), cellFormat ) )
1223
- {
1224
- // contents too wide for cell, need to wrap
1225
- heights[ row * cols + i ] = QgsTextRenderer::textHeight ( context, cellFormat, wrappedText ( context, localizedString, mColumns .at ( i ).width (), cellFormat ), QgsTextRenderer::Rect ) / context.convertToPainterUnits ( 1 , QgsUnitTypes::RenderMillimeters ) - contentDescentMm;
1226
- }
1227
- else
1228
- {
1229
- heights[ row * cols + i ] = QgsTextRenderer::textHeight ( context, cellFormat, QStringList () << localizedString.split ( ' \n ' ), QgsTextRenderer::Rect ) / context.convertToPainterUnits ( 1 , QgsUnitTypes::RenderMillimeters ) - contentDescentMm;
1230
- }
1213
+ heights[ row * cols + i ] = QgsTextRenderer::textHeight ( context,
1214
+ cellFormat,
1215
+ QStringList () << localizedString.split ( ' \n ' ),
1216
+ QgsTextRenderer::Rect,
1217
+ nullptr ,
1218
+ mWrapBehavior == WrapText ? Qgis::TextRendererFlag::WrapLines : Qgis::TextRendererFlags (),
1219
+ context.convertToPainterUnits ( mColumns .at ( i ).width (), QgsUnitTypes::RenderMillimeters )
1220
+ ) / context.convertToPainterUnits ( 1 , QgsUnitTypes::RenderMillimeters ) - contentDescentMm;
1231
1221
1232
1222
i++;
1233
1223
}
@@ -1375,83 +1365,6 @@ void QgsLayoutTable::drawHorizontalGridLines( QgsLayoutItemRenderContext &contex
1375
1365
painter->drawLine ( QPointF ( halfGridStrokeWidth, currentY ), QPointF ( mTableSize .width () - halfGridStrokeWidth, currentY ) );
1376
1366
}
1377
1367
1378
- bool QgsLayoutTable::textRequiresWrapping ( QgsRenderContext &context, const QString &text, double columnWidth, const QgsTextFormat &format ) const
1379
- {
1380
- if ( qgsDoubleNear ( columnWidth, 0.0 ) || mWrapBehavior != WrapText )
1381
- return false ;
1382
-
1383
- const QStringList multiLineSplit = text.split ( ' \n ' );
1384
- const double currentTextWidth = QgsTextRenderer::textWidth ( context, format, multiLineSplit ) / context.convertToPainterUnits ( 1 , QgsUnitTypes::RenderMillimeters );
1385
- return currentTextWidth > columnWidth;
1386
- }
1387
-
1388
- QStringList QgsLayoutTable::wrappedText ( QgsRenderContext &context, const QString &value, double columnWidth, const QgsTextFormat &format ) const
1389
- {
1390
- QStringList lines = value.split ( ' \n ' );
1391
- QStringList outLines;
1392
- const auto constLines = lines;
1393
- for ( const QString &line : constLines )
1394
- {
1395
- if ( textRequiresWrapping ( context, line, columnWidth, format ) )
1396
- {
1397
- // first step is to identify words which must be on their own line (too long to fit)
1398
- QStringList words = line.split ( ' ' );
1399
- QStringList linesToProcess;
1400
- QString wordsInCurrentLine;
1401
- const auto constWords = words;
1402
- for ( const QString &word : constWords )
1403
- {
1404
- if ( textRequiresWrapping ( context, word, columnWidth, format ) )
1405
- {
1406
- // too long to fit
1407
- if ( !wordsInCurrentLine.isEmpty () )
1408
- linesToProcess << wordsInCurrentLine;
1409
- wordsInCurrentLine.clear ();
1410
- linesToProcess << word;
1411
- }
1412
- else
1413
- {
1414
- if ( !wordsInCurrentLine.isEmpty () )
1415
- wordsInCurrentLine.append ( ' ' );
1416
- wordsInCurrentLine.append ( word );
1417
- }
1418
- }
1419
- if ( !wordsInCurrentLine.isEmpty () )
1420
- linesToProcess << wordsInCurrentLine;
1421
-
1422
- const auto constLinesToProcess = linesToProcess;
1423
- for ( const QString &line : constLinesToProcess )
1424
- {
1425
- QString remainingText = line;
1426
- int lastPos = remainingText.lastIndexOf ( ' ' );
1427
- while ( lastPos > -1 )
1428
- {
1429
- // check if remaining text is short enough to go in one line
1430
- if ( !textRequiresWrapping ( context, remainingText, columnWidth, format ) )
1431
- {
1432
- break ;
1433
- }
1434
-
1435
- if ( !textRequiresWrapping ( context, remainingText.left ( lastPos ), columnWidth, format ) )
1436
- {
1437
- outLines << remainingText.left ( lastPos );
1438
- remainingText = remainingText.mid ( lastPos + 1 );
1439
- lastPos = 0 ;
1440
- }
1441
- lastPos = remainingText.lastIndexOf ( ' ' , lastPos - 1 );
1442
- }
1443
- outLines << remainingText;
1444
- }
1445
- }
1446
- else
1447
- {
1448
- outLines << line;
1449
- }
1450
- }
1451
-
1452
- return outLines;
1453
- }
1454
-
1455
1368
QColor QgsLayoutTable::backgroundColor ( int row, int column ) const
1456
1369
{
1457
1370
QColor color = mBackgroundColor ;
0 commit comments