Beispiel #1
0
void KDReports::AutoTableElement::build( ReportBuilder& builder ) const
{
    if( !d->m_tableModel ) {
        return;
    }
    QTextDocument& textDoc = builder.currentDocument();
    QTextCursor& textDocCursor = builder.cursor();
    textDocCursor.beginEditBlock();

    QTextTableFormat tableFormat;
    const int headerRowCount = d->m_horizontalHeaderVisible ? 1 : 0;
    const int headerColumnCount = d->m_verticalHeaderVisible ? 1 : 0;
    tableFormat.setHeaderRowCount( headerRowCount );
    tableFormat.setProperty( KDReports::HeaderColumnsProperty, headerColumnCount );

    tableFormat.setAlignment( textDocCursor.blockFormat().alignment() );
    fillTableFormat( tableFormat, textDocCursor );

    while (d->m_tableModel->canFetchMore(QModelIndex()))
        d->m_tableModel->fetchMore(QModelIndex());

    const int rows = d->m_tableModel->rowCount();
    const int columns = d->m_tableModel->columnCount();

    QTextTable* textTable = textDocCursor.insertTable( rows + headerRowCount,
                                                       columns + headerColumnCount,
                                                       tableFormat );

    QTextCharFormat tableHeaderFormat;
    tableHeaderFormat.setBackground( d->m_headerBackground );
    //qDebug( "rows = %d, columns = %d", textTable->rows(), textTable->columns() );

    if( d->m_horizontalHeaderVisible ) {
        for( int column = 0; column < columns; column++ ) {
            QTextTableCell cell = textTable->cellAt( 0, column + headerColumnCount );
            Q_ASSERT( cell.isValid() );
            cell.setFormat( tableHeaderFormat );
            d->fillCellFromHeaderData( column, Qt::Horizontal, cell, textDoc, textTable, builder );
        }
    }

    if( d->m_verticalHeaderVisible ) {
        for( int row = 0; row < rows; row++ ) {
            QTextTableCell cell = textTable->cellAt( row + headerRowCount, 0 );
            Q_ASSERT( cell.isValid() );
            cell.setFormat( tableHeaderFormat );
            d->fillCellFromHeaderData( row, Qt::Vertical, cell, textDoc, textTable, builder );
        }
    }

    QVector<QBitArray> coveredCells;
    coveredCells.resize(rows);
    for( int row = 0; row < rows; row++ )
        coveredCells[row].resize(columns);

    // The normal data
    for( int row = 0; row < rows; row++ ) {
        for( int column = 0; column < columns; column++ ) {
            if (coveredCells[row].testBit(column))
                continue;
            QTextTableCell cell = textTable->cellAt( row + headerRowCount, column + headerColumnCount );
            Q_ASSERT( cell.isValid() );
            const QSize span = d->fillTableCell( row, column, cell, textDoc, textTable, builder );
            if (span.isValid()) {
                for (int r = row; r < row + span.height() && r < rows; ++r) {
                    for (int c = column; c < column + span.width() && c < columns; ++c) {
                        coveredCells[r].setBit(c);
                    }
                }
            }
        }
    }

    textDocCursor.movePosition( QTextCursor::End );
    textDocCursor.endEditBlock();

    builder.currentDocumentData().registerAutoTable( textTable, this );
}