void KDReports::TextDocumentData::scaleFontsBy( qreal factor )
{
    QTextCursor cursor( m_document );
    qreal currentPointSize = -1.0;
    QTextCursor lastCursor( m_document );
    Q_FOREVER {
        qreal cursorFontPointSize = cursor.charFormat().fontPointSize();
        //qDebug() << cursorFontPointSize << "last=" << currentPointSize << cursor.block().text() << "position=" << cursor.position();
        if ( cursorFontPointSize != currentPointSize ) {
            if ( currentPointSize != -1.0 ) {
                setFontSizeHelper( lastCursor, cursor.position() - 1, currentPointSize, factor );
                lastCursor.setPosition( cursor.position() - 1, QTextCursor::MoveAnchor );
            }
            currentPointSize = cursorFontPointSize;
        }
        if ( cursor.atEnd() )
            break;
        cursor.movePosition( QTextCursor::NextCharacter );
    }
    if ( currentPointSize != -1.0 ) {
        setFontSizeHelper( lastCursor, cursor.position(), currentPointSize, factor );
    }

    // Also adjust the padding in the cells so that it remains proportional,
    // and the column constraints.
    Q_FOREACH( QTextTable* table, m_tables ) {
        QTextTableFormat format = table->format();
        format.setCellPadding( format.cellPadding() * factor );

        QVector<QTextLength> constraints = format.columnWidthConstraints();
        for ( int i = 0; i < constraints.size(); ++i ) {
            if ( constraints[i].type() == QTextLength::FixedLength ) {
                constraints[i] = QTextLength( QTextLength::FixedLength, constraints[i].rawValue() * factor );
            }
        }
        format.setColumnWidthConstraints( constraints );

        table->setFormat( format );
    }
Example #2
0
void HtmlExporter::emitTable( const QTextTable *table )
{
    //qDebug() << "emitTable" << html;
    QTextTableFormat format = table->format();

    html += QLatin1String( "\n<table" );

    if ( format.hasProperty( QTextFormat::FrameBorder ) ) {
        emitAttribute( "border", QString::number( format.border() ) );
    }

    emitFloatStyle( format.position() );
    emitAlignment( format.alignment() );
    emitTextLength( "width", format.width() );

    if ( format.hasProperty( QTextFormat::TableCellSpacing ) ) {
        emitAttribute( "cellspacing", QString::number( format.cellSpacing() ) );
    }
    if ( format.hasProperty( QTextFormat::TableCellPadding ) ) {
        emitAttribute( "cellpadding", QString::number( format.cellPadding() ) );
    }

    QBrush bg = format.background();
    if ( bg != Qt::NoBrush ) {
        emitAttribute( "bgcolor", bg.color().name() );
    }

    html += QLatin1Char( '>' );

    const int rows = table->rows();
    const int columns = table->columns();

    QVector<QTextLength> columnWidths = format.columnWidthConstraints();
    if ( columnWidths.isEmpty() ) {
        columnWidths.resize( columns );
        columnWidths.fill( QTextLength() );
    }
//    Q_ASSERT(columnWidths.count() == columns);

    QVarLengthArray<bool> widthEmittedForColumn( columns );
    for ( int i = 0; i < columns; ++i ) {
        widthEmittedForColumn[i] = false;
    }

    const int headerRowCount = qMin( format.headerRowCount(), rows );
    if ( headerRowCount > 0 ) {
        html += QLatin1String( "<thead>" );
    }

    for ( int row = 0; row < rows; ++row ) {
        html += QLatin1String( "\n<tr>" );

        for ( int col = 0; col < columns; ++col ) {
            const QTextTableCell cell = table->cellAt( row, col );

            // for col/rowspans
            if ( cell.row() != row ) {
                continue;
            }

            if ( cell.column() != col ) {
                continue;
            }

            html += QLatin1String( "\n<td" );

            if ( !widthEmittedForColumn[col] ) {
                emitTextLength( "width", columnWidths.at( col ) );
                widthEmittedForColumn[col] = true;
            }

            if ( cell.columnSpan() > 1 ) {
                emitAttribute( "colspan", QString::number( cell.columnSpan() ) );
            }

            if ( cell.rowSpan() > 1 ) {
                emitAttribute( "rowspan", QString::number( cell.rowSpan() ) );
            }

            const QTextCharFormat cellFormat = cell.format();
            QBrush bg = cellFormat.background();
            if ( bg != Qt::NoBrush ) {
                emitAttribute( "bgcolor", bg.color().name() );
            }

            html += QLatin1Char( '>' );

            emitFrame( cell.begin() );

            html += QLatin1String( "</td>" );
        }

        html += QLatin1String( "</tr>" );
        if ( headerRowCount > 0 && row == headerRowCount - 1 ) {
            html += QLatin1String( "</thead>" );
        }
    }

    html += QLatin1String( "</table>" );
}