int KDReports::SpreadsheetReportLayout::numberOfPages()
{
    ensureLayouted();
    return m_pageRects.count();
}
示例#2
0
void KDReports::ReportPrivate::paintPage( int pageNumber, QPainter& painter )
{
    ensureLayouted();

    const int pageCount = m_layout->numberOfPages();
    KDReports::Header* header = m_headers.headerForPage( pageNumber + 1, pageCount );
    if ( header ) {
        header->preparePaintingPage( pageNumber + m_firstPageNumber - 1 );
    }
    KDReports::Header* footer = m_footers.headerForPage( pageNumber + 1, pageCount );
    if ( footer ) {
        footer->preparePaintingPage( pageNumber + m_firstPageNumber - 1 );
    }

    const qreal textDocWidth = m_paperSize.width() - mmToPixels( m_marginLeft + m_marginRight );
    const qreal textDocHeight = mainTextDocHeight();

    const int left = qRound( mmToPixels( m_marginLeft ) );
    const int top = qRound( mmToPixels( m_marginTop ) );
    //const int right = qRound( mmToPixels( m_marginRight ) );
    const int bottom = qRound( mmToPixels( m_marginBottom ) );
    const bool skipHeadersFooters = this->skipHeadersFooters();
    const int headerHeightWithSpacing = qRound( ( skipHeadersFooters ? 0 : m_headers.height() ) + mmToPixels( m_headerBodySpacing ) );
    const int footerHeight = skipHeadersFooters ? 0 : qRound( m_footers.height() );
    //const int footerHeightWithspacing = qRound( m_footers.height() + mmToPixels( m_footerBodySpacing ) );
    //const QRect paperRect( 0, 0, qRound( m_paperSize.width() ), qRound( m_paperSize.height() ) );
    //const QRect textDocRect = paperRect.adjusted( left, top + headerHeightWithSpacing,
    //                                              -right, - bottom - footerHeightWithspacing);
    const QRect textDocRect( left, top + headerHeightWithSpacing,
                             textDocWidth, textDocHeight );

    /*qDebug() << "paintPage: in pixels: top=" << top << " headerHeight=" << headerHeightWithSpacing
             << " textDoc size:" << textDocRect.size()
             << " bottom=" << bottom << " footerHeight=" << footerHeight;*/

    if( !m_watermarkText.isEmpty() ) {
        painter.save();
        painter.translate( textDocRect.center() );
        painter.rotate( m_watermarkRotation );
        painter.setFont( m_watermarkFont );
        painter.setPen( m_watermarkColor );
        const QSize textSize( painter.fontMetrics().size( Qt::TextSingleLine, m_watermarkText ) );
        const QRect textRect( -textSize.width() / 2, -textSize.height()/2, textSize.width(), textSize.height() );
        painter.drawText( textRect, Qt::AlignCenter, m_watermarkText );
        painter.restore();
    }

    if( !m_watermarkImage.isNull() ) {
        // We paint it without scaling it, for better quality.
        // But this means the actual size depends on the zoom level or printer resolution...
        //
        // It also means the image could end up being bigger than the page, and we don't want that.
        // So we scale down if necessary. But never up.
        QImage img = m_watermarkImage;
        if ( m_watermarkImage.width() > textDocRect.width() ||
             m_watermarkImage.height() > textDocRect.height() ) {
            // should probably be cached?
            img = m_watermarkImage.scaled( textDocRect.size(), Qt::KeepAspectRatio, Qt::SmoothTransformation );
        }
        const QRect imageRect = QStyle::alignedRect( Qt::LeftToRight, Qt::AlignCenter, img.size(), textDocRect );
        //qDebug() << "textDocRect=" << textDocRect << "size=" << img.size() << "-> imageRect=" << imageRect;
        painter.drawImage( imageRect.topLeft(), img );
    }


    painter.save();
    //painter.setClipRect( textDocRect, Qt::IntersectClip ); // triggers a Qt-Windows bug when printing
    painter.setClipRect( textDocRect );
    painter.translate( left, top + headerHeightWithSpacing );
    m_layout->paintPageContent( pageNumber, painter );
    painter.restore();

    QAbstractTextDocumentLayout::PaintContext ctx;
    ctx.palette.setColor(QPalette::Text, Qt::black);
    if ( header && !skipHeadersFooters ) {
        painter.save();
        painter.translate( left, top );
        ctx.clip = painter.clipRegion().boundingRect();
        header->doc().contentDocument().documentLayout()->draw(&painter, ctx);
        painter.restore();
    }
    if ( footer && !skipHeadersFooters ) {
        painter.save();
        painter.translate( left, m_paperSize.height() - bottom - footerHeight );
        ctx.clip = painter.clipRegion().boundingRect();
        footer->doc().contentDocument().documentLayout()->draw(&painter, ctx);
        painter.restore();
    }
}