void KDReports::SpreadsheetReportLayout::paintPageContent(int pageNumber, QPainter &painter)
{
    //qDebug() << "painting with" << m_tableLayout.scaledFont();
    QAbstractItemModel* model = m_tableLayout.m_model;
    const qreal padding = m_tableLayout.scaledCellPadding();
    const QRect cellCoords = m_pageRects[pageNumber];
    //qDebug() << "painting page" << pageNumber << "cellCoords=" << cellCoords;
    qreal y = 0 /*m_topMargin*/; // in pixels
    const qreal rowHeight = m_tableLayout.rowHeight();

    if ( m_tableLayout.m_horizontalHeaderVisible ) {
        qreal x = 0 /*m_leftMargin*/;
        if ( m_tableLayout.m_verticalHeaderVisible ) {
            x += m_tableLayout.vHeaderWidth();
        }
        for ( int col = cellCoords.left(); col <= cellCoords.right(); ++col )
        {
            const QRectF cellRect( x, y, m_tableLayout.m_columnWidths[ col ], m_tableLayout.hHeaderHeight() );
            paintTableHorizontalHeader( cellRect, painter, col );
            x += cellRect.width();
        }
        y += m_tableLayout.hHeaderHeight();
    }

    const int firstRow = cellCoords.top();
    const int firstColumn = cellCoords.left();
    const int numRows = cellCoords.height();
    const int numColumns = cellCoords.width();

    // This won't work across page breaks....
    QVector<QBitArray> coveredCells;
    coveredCells.resize( numRows );
    for ( int row = firstRow; row <= cellCoords.bottom(); ++row )
        coveredCells[row - firstRow].resize( numColumns );

    for ( int row = firstRow; row <= cellCoords.bottom(); ++row )
    {
        qreal x = 0 /*m_leftMargin*/;
        if ( m_tableLayout.m_verticalHeaderVisible ) {
            x = paintTableVerticalHeader( x, y, painter, row );
        }
        painter.setFont( m_tableLayout.scaledFont() );
        for ( int col = cellCoords.left(); col <= cellCoords.right(); ++col )
        {
            if (coveredCells[row - firstRow].testBit(col - firstColumn)) {
                x += m_tableLayout.m_columnWidths[ col ];
                continue;
            }

            const QModelIndex index = model->index( row, col );

            const QSize span = model->span( index );
            if (span.isValid()) {
                for (int r = row; r < row + span.height() && r < numRows; ++r) {
                    for (int c = col; c < col + span.width() && c < numColumns; ++c) {
                        coveredCells[r - firstRow].setBit(c - firstColumn);
                    }
                }
            }

            const QRectF cellRect( x, y, cellWidth( col, span.width() ), qMax(1, span.height()) * rowHeight );
            const QRectF cellContentsRect = cellRect.adjusted( padding, padding, -padding, -padding );
            //qDebug() << "cell" << row << col << "rect=" << cellRect;

            const QString cellText = model->data( index, Qt::DisplayRole ).toString();
            const QColor foreground = qvariant_cast<QColor>( model->data( index, Qt::ForegroundRole ) );
            const QColor background = qvariant_cast<QColor>( model->data( index, Qt::BackgroundRole ) );
            const Qt::Alignment alignment( model->data( index, Qt::TextAlignmentRole ).toInt() );
            const QVariant decorationAlignment( model->data( index, KDReports::AutoTableElement::DecorationAlignmentRole ) );
            const QVariant cellDecoration( model->data( index, Qt::DecorationRole ) );

            if ( background.isValid() ) {
                painter.fillRect( cellRect, QBrush( background ) );
            } else if ( span.isValid() ) {
                painter.fillRect( cellRect, Qt::white );
            }
            drawBorder(cellRect, painter);

            // Per-cell font is not supported, on purpose. All rows use the same font,
            // otherwise the calculations for making things fit into a number of pages
            // become quite complex and slow.
            //const QVariant cellFont = model->data( index, Qt::FontRole );
            //if ( cellFont.isValid() )
            //    painter.setFont( qvariant_cast<QFont>( cellFont ) );
            //else
            //    painter.setFont( scaledFont );

            if ( foreground.isValid() )
                painter.setPen( foreground );

            paintTextAndIcon( painter, cellContentsRect, cellText, cellDecoration, decorationAlignment, alignment );

            if ( foreground.isValid() )
                painter.setPen( Qt::black );

            x += m_tableLayout.m_columnWidths[ col ];
        }
        y += rowHeight;
    }
}