void QTextOdfWriter::writeFrame(QXmlStreamWriter &writer, const QTextFrame *frame) { Q_ASSERT(frame); const QTextTable *table = qobject_cast<const QTextTable*> (frame); if (table) { // Start a table. writer.writeStartElement(tableNS, QString::fromLatin1("table")); writer.writeEmptyElement(tableNS, QString::fromLatin1("table-column")); writer.writeAttribute(tableNS, QString::fromLatin1("number-columns-repeated"), QString::number(table->columns())); } else if (frame->document() && frame->document()->rootFrame() != frame) { // start a section writer.writeStartElement(textNS, QString::fromLatin1("section")); } QTextFrame::iterator iterator = frame->begin(); QTextFrame *child = 0; int tableRow = -1; while (! iterator.atEnd()) { if (iterator.currentFrame() && child != iterator.currentFrame()) writeFrame(writer, iterator.currentFrame()); else { // no frame, its a block QTextBlock block = iterator.currentBlock(); if (table) { QTextTableCell cell = table->cellAt(block.position()); if (tableRow < cell.row()) { if (tableRow >= 0) writer.writeEndElement(); // close table row tableRow = cell.row(); writer.writeStartElement(tableNS, QString::fromLatin1("table-row")); } writer.writeStartElement(tableNS, QString::fromLatin1("table-cell")); if (cell.columnSpan() > 1) writer.writeAttribute(tableNS, QString::fromLatin1("number-columns-spanned"), QString::number(cell.columnSpan())); if (cell.rowSpan() > 1) writer.writeAttribute(tableNS, QString::fromLatin1("number-rows-spanned"), QString::number(cell.rowSpan())); if (cell.format().isTableCellFormat()) { writer.writeAttribute(tableNS, QString::fromLatin1("style-name"), QString::fromLatin1("T%1").arg(cell.tableCellFormatIndex())); } } writeBlock(writer, block); if (table) writer.writeEndElement(); // table-cell } child = iterator.currentFrame(); ++iterator; } if (tableRow >= 0) writer.writeEndElement(); // close table-row if (table || (frame->document() && frame->document()->rootFrame() != frame)) writer.writeEndElement(); // close table or section element }
void QTextCopyHelper::copy() { if (cursor.hasComplexSelection()) { QTextTable *table = cursor.currentTable(); int row_start, col_start, num_rows, num_cols; cursor.selectedTableCells(&row_start, &num_rows, &col_start, &num_cols); QTextTableFormat tableFormat = table->format(); tableFormat.setColumns(num_cols); tableFormat.clearColumnWidthConstraints(); const int objectIndex = dst->formatCollection()->createObjectIndex(tableFormat); Q_ASSERT(row_start != -1); for (int r = row_start; r < row_start + num_rows; ++r) { for (int c = col_start; c < col_start + num_cols; ++c) { QTextTableCell cell = table->cellAt(r, c); const int rspan = cell.rowSpan(); const int cspan = cell.columnSpan(); if (rspan != 1) { int cr = cell.row(); if (cr != r) continue; } if (cspan != 1) { int cc = cell.column(); if (cc != c) continue; } // add the QTextBeginningOfFrame QTextCharFormat cellFormat = cell.format(); if (r + rspan >= row_start + num_rows) { cellFormat.setTableCellRowSpan(row_start + num_rows - r); } if (c + cspan >= col_start + num_cols) { cellFormat.setTableCellColumnSpan(col_start + num_cols - c); } const int charFormatIndex = convertFormatIndex(cellFormat, objectIndex); int blockIdx = -2; const int cellPos = cell.firstPosition(); QTextBlock block = src->blocksFind(cellPos); if (block.position() == cellPos) { blockIdx = convertFormatIndex(block.blockFormat()); } dst->insertBlock(QTextBeginningOfFrame, insertPos, blockIdx, charFormatIndex); ++insertPos; // nothing to add for empty cells if (cell.lastPosition() > cellPos) { // add the contents appendFragments(cellPos, cell.lastPosition()); } } } // add end of table int end = table->lastPosition(); appendFragment(end, end+1, objectIndex); } else { appendFragments(cursor.selectionStart(), cursor.selectionEnd()); } }
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>" ); }