Last row/column style should apply when merged cells touch last row/col

This commit is contained in:
Nyall Dawson 2024-09-04 12:01:38 +10:00
parent a53185387c
commit a9c2edddc8
4 changed files with 71 additions and 7 deletions

View File

@ -510,14 +510,14 @@ void QgsLayoutTable::render( QgsLayoutItemRenderContext &context, const QRectF &
double cellHeight = 0;
double cellWidth = 0;
const int rowsSpan = rowSpan( row, col );
const int colsSpan = columnSpan( row, col );
if ( spannedCells.find( std::make_pair( row, col ) ) != spannedCells.end() )
{
isSpanned = true;
}
else
{
const int rowsSpan = rowSpan( row, col );
const int colsSpan = columnSpan( row, col );
for ( int spannedRow = row; spannedRow < row + rowsSpan; ++spannedRow )
{
cellHeight += mMaxRowHeightMap[spannedRow + 1] + 2 * mCellMargin
@ -541,7 +541,7 @@ void QgsLayoutTable::render( QgsLayoutItemRenderContext &context, const QRectF &
//draw background
p->save();
p->setPen( Qt::NoPen );
p->setBrush( backgroundColor( row, col ) );
p->setBrush( backgroundColor( row, col, rowsSpan, colsSpan ) );
p->drawRect( fullCell );
p->restore();
}
@ -1458,7 +1458,7 @@ void QgsLayoutTable::drawHorizontalGridLines( QgsLayoutItemRenderContext &contex
painter->drawLine( QPointF( 0, currentY ), QPointF( mTableSize.width(), currentY ) );
}
QColor QgsLayoutTable::backgroundColor( int row, int column ) const
QColor QgsLayoutTable::backgroundColor( int row, int column, int rowSpan, int columnSpan ) const
{
QColor color = mBackgroundColor;
if ( QgsLayoutTableStyle *style = mCellStyles.value( OddColumns ) )
@ -1477,7 +1477,7 @@ QColor QgsLayoutTable::backgroundColor( int row, int column ) const
if ( style->enabled && column == 0 )
color = style->cellBackgroundColor;
if ( QgsLayoutTableStyle *style = mCellStyles.value( LastColumn ) )
if ( style->enabled && column == mColumns.count() - 1 )
if ( style->enabled && ( column + columnSpan == mColumns.count() ) )
color = style->cellBackgroundColor;
if ( QgsLayoutTableStyle *style = mCellStyles.value( HeaderRow ) )
if ( style->enabled && row == -1 )
@ -1486,7 +1486,7 @@ QColor QgsLayoutTable::backgroundColor( int row, int column ) const
if ( style->enabled && row == 0 )
color = style->cellBackgroundColor;
if ( QgsLayoutTableStyle *style = mCellStyles.value( LastRow ) )
if ( style->enabled && row == mTableContents.count() - 1 )
if ( style->enabled && ( row + rowSpan == mTableContents.count() ) )
color = style->cellBackgroundColor;
if ( row >= 0 )

View File

@ -794,9 +794,11 @@ class CORE_EXPORT QgsLayoutTable: public QgsLayoutMultiFrame
* Returns the calculated background color for a row and column combination.
* \param row row number, where -1 is the header row, and 0 is the first body row
* \param column column number, where 0 is the first column
* \param rowSpan number of rows spanned by cell
* \param columnSpan number of columns spanned by cell
* \returns background color, or invalid QColor if no background should be drawn
*/
QColor backgroundColor( int row, int column ) const;
QColor backgroundColor( int row, int column, int rowSpan = 1, int columnSpan = 1 ) const;
friend class TestQgsLayoutTable;
friend class TestQgsLayoutManualTable;

View File

@ -53,6 +53,7 @@ class TestQgsLayoutManualTable : public QgsTest
void mergedCells();
void mergedCellsVertOnly();
void mergedCellsHozOnly();
void mergedCellsBackgroundColor();
private:
@ -665,5 +666,66 @@ void TestQgsLayoutManualTable::mergedCellsHozOnly()
QGSVERIFYLAYOUTCHECK( QStringLiteral( "manualtable_merged_hoz_only" ), &l );
}
void TestQgsLayoutManualTable::mergedCellsBackgroundColor()
{
QgsLayout l( QgsProject::instance() );
l.initializeDefaults();
QgsLayoutItemManualTable *table = new QgsLayoutItemManualTable( &l );
QgsLayoutFrame *frame1 = new QgsLayoutFrame( &l, table );
frame1->attemptSetSceneRect( QRectF( 5, 5, 100, 60 ) );
frame1->setFrameEnabled( true );
table->addFrame( frame1 );
table->setBackgroundColor( Qt::yellow );
table->setContentTextFormat( QgsTextFormat::fromQFont( QgsFontUtils::getStandardTestFont( QStringLiteral( "Bold" ) ) ) );
frame1->setFrameEnabled( false );
table->setShowGrid( true );
table->setHorizontalGrid( true );
table->setVerticalGrid( true );
QgsLayoutTableStyle lastColumnStyle;
lastColumnStyle.enabled = true;
lastColumnStyle.cellBackgroundColor = QColor( 200, 250, 220 );
table->setCellStyle( QgsLayoutTable::CellStyleGroup::LastColumn, lastColumnStyle );
QgsLayoutTableStyle lastRowStyle;
lastRowStyle.enabled = true;
lastRowStyle.cellBackgroundColor = QColor( 220, 220, 250 );
table->setCellStyle( QgsLayoutTable::CellStyleGroup::LastRow, lastRowStyle );
QgsLayoutTableStyle oddColumnStyle;
oddColumnStyle.enabled = true;
oddColumnStyle.cellBackgroundColor = QColor( 100, 250, 220 );
table->setCellStyle( QgsLayoutTable::CellStyleGroup::OddColumns, oddColumnStyle );
QgsLayoutTableStyle oddRowStyle;
oddRowStyle.enabled = true;
oddRowStyle.cellBackgroundColor = QColor( 200, 150, 220 );
table->setCellStyle( QgsLayoutTable::CellStyleGroup::OddRows, oddRowStyle );
QgsTableCell c1( QStringLiteral( "Jet" ) );
c1.setSpan( 2, 2 );
QgsTableCell c3( QStringLiteral( "Plane" ) );
c3.setHorizontalAlignment( Qt::AlignCenter );
c3.setVerticalAlignment( Qt::AlignTop );
c3.setSpan( 3, 1 );
QgsTableCell c4( QStringLiteral( "Plane" ) );
c4.setHorizontalAlignment( Qt::AlignCenter );
c4.setVerticalAlignment( Qt::AlignTop );
c4.setSpan( 2, 1 );
table->setTableContents( QgsTableContents() << ( QgsTableRow() << c1 << QgsTableCell( QStringLiteral( "A2" ) ) << c1 << QgsTableCell( QStringLiteral( "Something" ) ) )
<< ( QgsTableRow() << QgsTableCell( QStringLiteral( "B" ) ) << QgsTableCell( QStringLiteral( "B2" ) ) << QgsTableCell( QStringLiteral( "hidden by span" ) ) << QgsTableCell( QStringLiteral( "hidden by span" ) ) )
<< ( QgsTableRow() << QgsTableCell( QStringLiteral( "C" ) ) << QgsTableCell( QStringLiteral( "C2" ) ) << QgsTableCell( QStringLiteral( "C3" ) ) << c3 )
<< ( QgsTableRow() << c4 << QgsTableCell( QStringLiteral( "E" ) ) << QgsTableCell( QStringLiteral( "F" ) ) << QgsTableCell( QStringLiteral( "hidden by span" ) ) )
<< ( QgsTableRow() << QgsTableCell( QStringLiteral( "hidden" ) ) << QgsTableCell( QStringLiteral( "D2" ) ) << QgsTableCell( QStringLiteral( "D3" ) ) << QgsTableCell( QStringLiteral( "G" ) ) ) );
table->setColumnWidths( QList< double >() << 30 << 50.0 << 40.0 << 25.0 );
QGSVERIFYLAYOUTCHECK( QStringLiteral( "manualtable_merged_background_color" ), &l );
}
QGSTEST_MAIN( TestQgsLayoutManualTable )
#include "testqgslayoutmanualtable.moc"