/*! \fn void QTextTable::insertColumns(int index, int columns) Inserts a number of \a columns before the column with the specified \a index. \sa insertRows() resize() removeRows() removeColumns() appendRows() appendColumns() */ void QTextTable::insertColumns(int pos, int num) { Q_D(QTextTable); if (num <= 0) return; if (d->dirty) d->update(); if (pos > d->nCols || pos < 0) pos = d->nCols; // qDebug() << "-------- insertCols" << pos << num; QTextDocumentPrivate *p = d->pieceTable; QTextFormatCollection *c = p->formatCollection(); p->beginEditBlock(); for (int i = 0; i < d->nRows; ++i) { int cell; if (i == d->nRows - 1 && pos == d->nCols) cell = d->fragment_end; else cell = d->grid[i*d->nCols + pos]; QTextDocumentPrivate::FragmentIterator it(&p->fragmentMap(), cell); QTextCharFormat fmt = c->charFormat(it->format); if (pos > 0 && pos < d->nCols && cell == d->grid[i*d->nCols + pos - 1]) { // cell spans the insertion place, extend it fmt.setTableCellColumnSpan(fmt.tableCellColumnSpan() + num); p->setCharFormat(it.position(), 1, fmt); } else { fmt.setTableCellRowSpan(1); fmt.setTableCellColumnSpan(1); Q_ASSERT(fmt.objectIndex() == objectIndex()); int position = it.position(); int cfmt = p->formatCollection()->indexForFormat(fmt); int bfmt = p->formatCollection()->indexForFormat(QTextBlockFormat()); for (int i = 0; i < num; ++i) p->insertBlock(QTextBeginningOfFrame, position, bfmt, cfmt, QTextUndoCommand::MoveCursor); } } QTextTableFormat tfmt = format(); tfmt.setColumns(tfmt.columns()+num); QVector<QTextLength> columnWidths = tfmt.columnWidthConstraints(); if (! columnWidths.isEmpty()) { for (int i = num; i > 0; --i) columnWidths.insert(pos, columnWidths[qMax(0, pos-1)]); } tfmt.setColumnWidthConstraints (columnWidths); QTextObject::setFormat(tfmt); // qDebug() << "-------- end insertCols" << pos << num; p->endEditBlock(); }
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 ); }
/*! \fn void QTextTable::removeColumns(int index, int columns) Removes a number of \a columns starting with the column at the specified \a index. \sa insertRows(), insertColumns(), removeRows(), resize(), appendRows(), appendColumns() */ void QTextTable::removeColumns(int pos, int num) { Q_D(QTextTable); // qDebug() << "-------- removeCols" << pos << num; if (num <= 0 || pos < 0) return; if (d->dirty) d->update(); if (pos >= d->nCols) return; if (pos + num > d->nCols) pos = d->nCols - num; QTextDocumentPrivate *p = d->pieceTable; QTextFormatCollection *collection = p->formatCollection(); p->beginEditBlock(); // delete whole table? if (pos == 0 && num == d->nCols) { const int pos = p->fragmentMap().position(d->fragment_start); p->remove(pos, p->fragmentMap().position(d->fragment_end) - pos + 1); p->endEditBlock(); return; } p->aboutToRemoveCell(cellAt(0, pos).firstPosition(), cellAt(d->nRows - 1, pos + num - 1).lastPosition()); QList<int> touchedCells; for (int r = 0; r < d->nRows; ++r) { for (int c = pos; c < pos + num; ++c) { int cell = d->grid[r*d->nCols + c]; QTextDocumentPrivate::FragmentIterator it(&p->fragmentMap(), cell); QTextCharFormat fmt = collection->charFormat(it->format); int span = fmt.tableCellColumnSpan(); if (touchedCells.contains(cell) && span <= 1) continue; touchedCells << cell; if (span > 1) { fmt.setTableCellColumnSpan(span - 1); p->setCharFormat(it.position(), 1, fmt); } else { // remove cell int index = d->cells.indexOf(cell) + 1; int f_end = index < d->cells.size() ? d->cells.at(index) : d->fragment_end; p->remove(it.position(), p->fragmentMap().position(f_end) - it.position()); } } } QTextTableFormat tfmt = format(); tfmt.setColumns(tfmt.columns()-num); QVector<QTextLength> columnWidths = tfmt.columnWidthConstraints(); if (columnWidths.count() > pos) { columnWidths.remove(pos, num); tfmt.setColumnWidthConstraints (columnWidths); } QTextObject::setFormat(tfmt); p->endEditBlock(); // qDebug() << "-------- end removeCols" << pos << num; }
/*! \fn void QTextTable::insertColumns(int index, int columns) Inserts a number of \a columns before the column with the specified \a index. \sa insertRows(), resize(), removeRows(), removeColumns(), appendRows(), appendColumns() */ void QTextTable::insertColumns(int pos, int num) { Q_D(QTextTable); if (num <= 0) return; if (d->dirty) d->update(); if (pos > d->nCols || pos < 0) pos = d->nCols; // qDebug() << "-------- insertCols" << pos << num; QTextDocumentPrivate *p = d->pieceTable; QTextFormatCollection *c = p->formatCollection(); p->beginEditBlock(); QList<int> extendedSpans; for (int i = 0; i < d->nRows; ++i) { int cell; if (i == d->nRows - 1 && pos == d->nCols) { cell = d->fragment_end; } else { int logicalGridIndexBeforePosition = pos > 0 ? d->findCellIndex(d->grid[i*d->nCols + pos - 1]) : -1; // Search for the logical insertion point by skipping past cells which are not the first // cell in a rowspan. This means any cell for which the logical grid index is // less than the logical cell index of the cell before the insertion. int logicalGridIndex; int gridArrayOffset = i*d->nCols + pos; do { cell = d->grid[gridArrayOffset]; logicalGridIndex = d->findCellIndex(cell); gridArrayOffset++; } while (logicalGridIndex < logicalGridIndexBeforePosition && gridArrayOffset < d->nRows*d->nCols); if (logicalGridIndex < logicalGridIndexBeforePosition && gridArrayOffset == d->nRows*d->nCols) cell = d->fragment_end; } if (pos > 0 && pos < d->nCols && cell == d->grid[i*d->nCols + pos - 1]) { // cell spans the insertion place, extend it if (!extendedSpans.contains(cell)) { QTextDocumentPrivate::FragmentIterator it(&p->fragmentMap(), cell); QTextCharFormat fmt = c->charFormat(it->format); fmt.setTableCellColumnSpan(fmt.tableCellColumnSpan() + num); p->setCharFormat(it.position(), 1, fmt); d->dirty = true; extendedSpans << cell; } } else { /* If the next cell is spanned from the row above, we need to find the right position to insert to */ if (i > 0 && pos < d->nCols && cell == d->grid[(i-1) * d->nCols + pos]) { int gridIndex = i*d->nCols + pos; const int gridEnd = d->nRows * d->nCols - 1; while (gridIndex < gridEnd && cell == d->grid[gridIndex]) { ++gridIndex; } if (gridIndex == gridEnd) cell = d->fragment_end; else cell = d->grid[gridIndex]; } QTextDocumentPrivate::FragmentIterator it(&p->fragmentMap(), cell); QTextCharFormat fmt = c->charFormat(it->format); fmt.setTableCellRowSpan(1); fmt.setTableCellColumnSpan(1); Q_ASSERT(fmt.objectIndex() == objectIndex()); int position = it.position(); int cfmt = p->formatCollection()->indexForFormat(fmt); int bfmt = p->formatCollection()->indexForFormat(QTextBlockFormat()); for (int i = 0; i < num; ++i) p->insertBlock(QTextBeginningOfFrame, position, bfmt, cfmt, QTextUndoCommand::MoveCursor); } } QTextTableFormat tfmt = format(); tfmt.setColumns(tfmt.columns()+num); QVector<QTextLength> columnWidths = tfmt.columnWidthConstraints(); if (! columnWidths.isEmpty()) { for (int i = num; i > 0; --i) columnWidths.insert(pos, columnWidths[qMax(0, pos-1)]); } tfmt.setColumnWidthConstraints (columnWidths); QTextObject::setFormat(tfmt); // qDebug() << "-------- end insertCols" << pos << num; p->endEditBlock(); }
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>" ); }