/*!
    \since 4.1

    Splits the specified cell at \a row and \a column into an array of multiple
    cells with dimensions specified by \a numRows and \a numCols.

    \note It is only possible to split cells that span multiple rows or columns, such as rows
    that have been merged using mergeCells().

    \sa mergeCells()
*/
void QTextTable::splitCell(int row, int column, int numRows, int numCols)
{
    Q_D(QTextTable);

    if (d->dirty)
        d->update();

    QTextDocumentPrivate *p = d->pieceTable;
    QTextFormatCollection *c = p->formatCollection();

    const QTextTableCell cell = cellAt(row, column);
    if (!cell.isValid())
        return;
    row = cell.row();
    column = cell.column();

    QTextCharFormat fmt = cell.format();
    const int rowSpan = fmt.tableCellRowSpan();
    const int colSpan = fmt.tableCellColumnSpan();

    // nothing to split?
    if (numRows > rowSpan || numCols > colSpan)
        return;

    p->beginEditBlock();

    const int origCellPosition = cell.firstPosition() - 1;

    QVarLengthArray<int> rowPositions(rowSpan);

    rowPositions[0] = cell.lastPosition();

    for (int r = row + 1; r < row + rowSpan; ++r) {
        // find the cell before which to insert the new cell markers
        int gridIndex = r * d->nCols + column;
        QVector<int>::iterator it = qUpperBound(d->cellIndices.begin(), d->cellIndices.end(), gridIndex);
        int cellIndex = it - d->cellIndices.begin();
        int fragment = d->cells.value(cellIndex, d->fragment_end);
        rowPositions[r - row] = p->fragmentMap().position(fragment);
    }

    fmt.setTableCellColumnSpan(1);
    fmt.setTableCellRowSpan(1);
    const int fmtIndex = c->indexForFormat(fmt);
    const int blockIndex = p->blockMap().find(cell.lastPosition())->format;

    int insertAdjustement = 0;
    for (int i = 0; i < numRows; ++i) {
        for (int c = 0; c < colSpan - numCols; ++c)
            p->insertBlock(QTextBeginningOfFrame, rowPositions[i] + insertAdjustement + c, blockIndex, fmtIndex);
        insertAdjustement += colSpan - numCols;
    }

    for (int i = numRows; i < rowSpan; ++i) {
        for (int c = 0; c < colSpan; ++c)
            p->insertBlock(QTextBeginningOfFrame, rowPositions[i] + insertAdjustement + c, blockIndex, fmtIndex);
        insertAdjustement += colSpan;
    }

    fmt.setTableCellRowSpan(numRows);
    fmt.setTableCellColumnSpan(numCols);
    p->setCharFormat(origCellPosition, 1, fmt);

    p->endEditBlock();
}
/*!
    \since 4.1

    Merges the cell at the specified \a row and \a column with the adjacent cells
    into one cell. The new cell will span \a numRows rows and \a numCols columns.
    If \a numRows or \a numCols is less than the current number of rows or columns
    the cell spans then this method does nothing.

    \sa splitCell()
*/
void QTextTable::mergeCells(int row, int column, int numRows, int numCols)
{
    Q_D(QTextTable);

    if (d->dirty)
        d->update();

    QTextDocumentPrivate *p = d->pieceTable;
    QTextFormatCollection *fc = p->formatCollection();

    const QTextTableCell cell = cellAt(row, column);
    if (!cell.isValid() || row != cell.row() || column != cell.column())
        return;

    QTextCharFormat fmt = cell.format();
    const int rowSpan = fmt.tableCellRowSpan();
    const int colSpan = fmt.tableCellColumnSpan();

    numRows = qMin(numRows, rows() - cell.row());
    numCols = qMin(numCols, columns() - cell.column());

    // nothing to merge?
    if (numRows < rowSpan || numCols < colSpan)
        return;

    // check the edges of the merge rect to make sure no cell spans the edge
    for (int r = row; r < row + numRows; ++r) {
        if (cellAt(r, column) == cellAt(r, column - 1))
            return;
        if (cellAt(r, column + numCols) == cellAt(r, column + numCols - 1))
            return;
    }

    for (int c = column; c < column + numCols; ++c) {
        if (cellAt(row, c) == cellAt(row - 1, c))
            return;
        if (cellAt(row + numRows, c) == cellAt(row + numRows - 1, c))
            return;
    }

    p->beginEditBlock();

    const int origCellPosition = cell.firstPosition() - 1;

    const int cellFragment = d->grid[row * d->nCols + column];

    // find the position at which to insert the contents of the merged cells
    QFragmentFindHelper helper(origCellPosition, p->fragmentMap());
    QList<int>::Iterator it = qBinaryFind(d->cells.begin(), d->cells.end(), helper);
    Q_ASSERT(it != d->cells.end());
    Q_ASSERT(*it == cellFragment);
    const int insertCellIndex = it - d->cells.begin();
    int insertFragment = d->cells.value(insertCellIndex + 1, d->fragment_end);
    uint insertPos = p->fragmentMap().position(insertFragment);

    d->blockFragmentUpdates = true;

    bool rowHasText = cell.firstCursorPosition().block().length();
    bool needsParagraph = rowHasText && colSpan == numCols;

    // find all cells that will be erased by the merge
    for (int r = row; r < row + numRows; ++r) {
        int firstColumn = r < row + rowSpan ? column + colSpan : column;

        // don't recompute the cell index for the first row
        int firstCellIndex = r == row ? insertCellIndex + 1 : -1;
        int cellIndex = firstCellIndex;

        for (int c = firstColumn; c < column + numCols; ++c) {
            const int fragment = d->grid[r * d->nCols + c];

            // already handled?
            if (fragment == cellFragment)
                continue;

            QTextDocumentPrivate::FragmentIterator it(&p->fragmentMap(), fragment);
            uint pos = it.position();

            if (firstCellIndex == -1) {
                QFragmentFindHelper helper(pos, p->fragmentMap());
                QList<int>::Iterator it = qBinaryFind(d->cells.begin(), d->cells.end(), helper);
                Q_ASSERT(it != d->cells.end());
                Q_ASSERT(*it == fragment);
                firstCellIndex = cellIndex = it - d->cells.begin();
            }

            ++cellIndex;

            QTextCharFormat fmt = fc->charFormat(it->format);

            const int cellRowSpan = fmt.tableCellRowSpan();
            const int cellColSpan = fmt.tableCellColumnSpan();

            // update the grid for this cell
            for (int i = r; i < r + cellRowSpan; ++i)
                for (int j = c; j < c + cellColSpan; ++j)
                    d->grid[i * d->nCols + j] = cellFragment;

            // erase the cell marker
            p->remove(pos, 1);

            const int nextFragment = d->cells.value(cellIndex, d->fragment_end);
            const uint nextPos = p->fragmentMap().position(nextFragment);

            Q_ASSERT(nextPos >= pos);

            // merge the contents of the cell (if not empty)
            if (nextPos > pos) {
                if (needsParagraph) {
                    needsParagraph = false;
                    QTextCursor(p, insertPos++).insertBlock();
                    p->move(pos + 1, insertPos, nextPos - pos);
                } else if (rowHasText) {
                    QTextCursor(p, insertPos++).insertText(QLatin1String(" "));
                    p->move(pos + 1, insertPos, nextPos - pos);
                } else {
                    p->move(pos, insertPos, nextPos - pos);
                }

                insertPos += nextPos - pos;
                rowHasText = true;
            }
        }

        if (rowHasText) {
            needsParagraph = true;
            rowHasText = false;
        }

        // erase cells from last row
        if (firstCellIndex >= 0) {
            d->cellIndices.remove(firstCellIndex, cellIndex - firstCellIndex);
            d->cells.erase(d->cells.begin() + firstCellIndex, d->cells.begin() + cellIndex);
        }
    }

    d->fragment_start = d->cells.first();

    fmt.setTableCellRowSpan(numRows);
    fmt.setTableCellColumnSpan(numCols);
    p->setCharFormat(origCellPosition, 1, fmt);

    d->blockFragmentUpdates = false;
    d->dirty = false;

    p->endEditBlock();
}
Пример #3
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>" );
}
KoTableCellStyle KoTextLayoutTableArea::Private::effectiveCellStyle(const QTextTableCell &tableCell)
{
    QTextTableFormat tableFormat = table->format();
    KoTableCellStyle cellStyle(tableCell.format().toTableCellFormat());
    if (documentLayout->styleManager() && table->format().hasProperty(KoTableStyle::TableTemplate)) {
        if (KoTextTableTemplate *tableTemplate = documentLayout->styleManager()->tableTemplate(table->format().intProperty(KoTableStyle::TableTemplate))) {
            //priorities according to ODF 1.2, 16.18 - table:table-template
            if (tableCell.column() == 0 && tableTemplate->firstColumn()
                    && tableFormat.boolProperty(KoTableStyle::UseFirstColumnStyles)) {
                cellStyle = *(documentLayout->styleManager()->tableCellStyle(tableTemplate->firstColumn()));
                return cellStyle;
            }

            if (tableCell.column() == (table->columns() - 1) && tableTemplate->lastColumn()
                    && tableFormat.boolProperty(KoTableStyle::UseLastColumnStyles)) {
                cellStyle = *(documentLayout->styleManager()->tableCellStyle(tableTemplate->lastColumn()));
                return cellStyle;
            }

            if (tableCell.row() == 0 && tableTemplate->firstRow()
                    && tableFormat.boolProperty(KoTableStyle::UseFirstRowStyles)) {
                cellStyle = *(documentLayout->styleManager()->tableCellStyle(tableTemplate->firstRow()));
                return cellStyle;
            }

            if (tableCell.row() == (table->rows() - 1) && tableTemplate->lastRow()
                    && tableFormat.boolProperty(KoTableStyle::UseLastRowStyles)) {
                cellStyle = *(documentLayout->styleManager()->tableCellStyle(tableTemplate->lastRow()));
                return cellStyle;
            }

            if (((tableCell.row() + 1) % 2) == 0 && tableTemplate->evenRows()
                    && tableFormat.boolProperty(KoTableStyle::UseBandingRowStyles)) {
                cellStyle = *(documentLayout->styleManager()->tableCellStyle(tableTemplate->evenRows()));
                return cellStyle;
            }

            if (((tableCell.row() + 1) % 2) != 0 && tableTemplate->oddRows()
                    && tableFormat.boolProperty(KoTableStyle::UseBandingRowStyles)) {
                cellStyle = *(documentLayout->styleManager()->tableCellStyle(tableTemplate->oddRows()));
                return cellStyle;
            }

            if (((tableCell.column() + 1) % 2) == 0 && tableTemplate->evenColumns()
                    && tableFormat.boolProperty(KoTableStyle::UseBandingColumnStyles)) {
                cellStyle = *(documentLayout->styleManager()->tableCellStyle(tableTemplate->evenColumns()));
                return cellStyle;
            }

            if (((tableCell.column() + 1) % 2) != 0 && tableTemplate->oddColumns()
                    && tableFormat.boolProperty(KoTableStyle::UseBandingColumnStyles)) {
                cellStyle = *(documentLayout->styleManager()->tableCellStyle(tableTemplate->oddColumns()));
                return cellStyle;
            }

            if (tableTemplate->body()) {
                cellStyle = *(documentLayout->styleManager()->tableCellStyle(tableTemplate->body()));
            }
        }
    }

    return cellStyle;
}
KoPointedAt KoTextLayoutTableArea::hitTest(const QPointF &point, Qt::HitTestAccuracy accuracy) const
{
    int firstRow = qMax(d->startOfArea->row, d->headerRows);
    int lastRow = d->endOfArea->row;

    if (d->lastRowHasSomething == false) {
        --lastRow;
    }

    if (lastRow <  d->startOfArea->row) {
        return KoPointedAt(); // empty
    }

    // Test normal cells.
    if (point.y() > d->rowPositions[firstRow] - 3.0 && point.y() < d->rowPositions[lastRow + 1] + 3.0) {
        QVector<qreal>::const_iterator start = d->rowPositions.constBegin() + firstRow;
        QVector<qreal>::const_iterator end = d->rowPositions.constBegin() + lastRow + 1;
        int row = qLowerBound(start, end, point.y()) - d->rowPositions.constBegin() - 1;
        int column = qLowerBound(d->columnPositions, point.x()) - d->columnPositions.constBegin() - 1;
        if (point.y() < d->rowPositions[firstRow]) {
            ++row;
        }
        column = qBound(0, column, d->table->columns() - 1);
        KoPointedAt pointedAt;
        if (qAbs(d->columnPositions[column] - point.x()) < 3.0) {
            pointedAt.tableHit = KoPointedAt::ColumnDivider;
        } else if (qAbs(d->columnPositions[column+1] - point.x()) < 3.0) {
            pointedAt.tableHit = KoPointedAt::ColumnDivider;
            ++column;
        } else if (d->columnPositions[0] < point.x()
                            && point.x() < d->columnPositions[d->table->columns()]
                            && qAbs(d->rowPositions[row] - point.y()) < 3.0) {
            pointedAt.tableHit = KoPointedAt::RowDivider;
        } else if (d->columnPositions[0] < point.x()
                            && point.x() < d->columnPositions[d->table->columns()]
                            && qAbs(d->rowPositions[row+1] - point.y()) < 3.0) {
            pointedAt.tableHit = KoPointedAt::RowDivider;
            ++row;
        } else {
            QTextTableCell cell = d->table->cellAt(row, column);
            pointedAt = d->cellAreas[cell.row()][cell.column()]->hitTest(point, accuracy);
        }

        if (pointedAt.tableHit == KoPointedAt::ColumnDivider) {
            if (column > 0) {
                pointedAt.tableLeadSize = d->columnPositions[column] - d->columnPositions[column-1];
            }
            if (column < d->table->columns()) {
                pointedAt.tableTrailSize = d->columnPositions[column+1] - d->columnPositions[column];
            }
        } else if (pointedAt.tableHit == KoPointedAt::RowDivider) {
            if (row > 0) {
                pointedAt.tableLeadSize = d->rowPositions[row] - d->rowPositions[row-1];
            }
            if (row < d->table->rows()) {
                pointedAt.tableTrailSize = d->rowPositions[row+1] - d->rowPositions[row];
            }
        }
        pointedAt.table = d->table;
        pointedAt.tableRowDivider = row;
        pointedAt.tableColumnDivider = column;
        pointedAt.tableDividerPos = QPointF(d->columnPositions[column],d->rowPositions[row]);
        return pointedAt;
    }

    // Test header row cells.
    QPointF headerPoint = point - QPointF(d->headerOffsetX, d->headerOffsetY);
    if (headerPoint.y() > d->headerRowPositions.first() && headerPoint.y() < d->headerRowPositions[d->headerRows]) {
        QVector<qreal>::const_iterator start = d->headerRowPositions.constBegin();
        QVector<qreal>::const_iterator end = d->headerRowPositions.constBegin() + d->headerRows;
        int row = qLowerBound(start, end, headerPoint.y()) - d->headerRowPositions.constBegin() - 1;
        int column = qLowerBound(d->columnPositions, headerPoint.x()) - d->columnPositions.constBegin() - 1;
        column = qBound(0, column, d->table->columns() - 1);
        KoPointedAt pointedAt;
        if (qAbs(d->columnPositions[column] - headerPoint.x()) < 3.0) {
            pointedAt.tableHit = KoPointedAt::ColumnDivider;
        } else if (qAbs(d->columnPositions[column+1] - headerPoint.x()) < 3.0) {
            pointedAt.tableHit = KoPointedAt::ColumnDivider;
            ++column;
        } else {
            QTextTableCell cell = d->table->cellAt(row, column);
            pointedAt = d->cellAreas[cell.row()][cell.column()]->hitTest(headerPoint, accuracy);
        }
        if (pointedAt.tableHit == KoPointedAt::ColumnDivider) {
            if (column > 0) {
                pointedAt.tableLeadSize = d->columnPositions[column] - d->columnPositions[column-1];
            }
            if (column < d->table->columns()) {
                pointedAt.tableTrailSize = d->columnPositions[column+1] - d->columnPositions[column];
            }
        }
        pointedAt.table = d->table;
        pointedAt.tableRowDivider = row;
        pointedAt.tableColumnDivider = column;
        pointedAt.tableDividerPos = QPointF(d->columnPositions[column],d->rowPositions[row]);
        return pointedAt;
    }

    return KoPointedAt();
}
Пример #6
0
void KoTextWriter::write(const QTextDocument *document, int from, int to)
{
    d->document = const_cast<QTextDocument*>(document);
    d->styleManager = KoTextDocument(document).styleManager();

    QTextBlock fromblock = document->findBlock(from);
    QTextBlock toblock = document->findBlock(to);

    QTextCursor fromcursor(fromblock);

    QTextTable *currentTable = fromcursor.currentTable();
    QTextList *currentList = fromcursor.currentList();

    // NOTE even better would be if we create a new table/list out of multiple selected
    // tablecells/listitems that contain only the selected cells/items. But following
    // at least enables copying a whole list/table while still being able to copy/paste
    // only parts of the text within a list/table (see also bug 275990).
    if (currentTable || currentList) {
        if (from == 0 && to < 0) {
            // save everything means also save current table and list
            currentTable = 0;
            currentList = 0;
        } else {
            QTextCursor tocursor(toblock);
            //fromcursor.setPosition(from, QTextCursor::KeepAnchor);
            tocursor.setPosition(to, QTextCursor::KeepAnchor);

            if (!fromcursor.movePosition(QTextCursor::PreviousCharacter, QTextCursor::KeepAnchor)) {
                fromcursor = QTextCursor();
            }
            if (!tocursor.movePosition(QTextCursor::NextCharacter, QTextCursor::KeepAnchor)) {
                tocursor = QTextCursor();
            }

            // save the whole table if all cells are selected
            if (currentTable) {
                QTextTableCell fromcell = currentTable->cellAt(from);
                QTextTableCell tocell = currentTable->cellAt(to);
                if ((fromcursor.isNull() || fromcursor.currentTable() != currentTable) &&
                    (tocursor.isNull() || tocursor.currentTable() != currentTable) &&
                    fromcell.column() == 0 && fromcell.row() == 0 &&
                    tocell.column() == currentTable->columns()-1 && tocell.row() == currentTable->rows()-1
                ) {
                    currentTable = 0;
                }
            }

            // save the whole list if all list-items are selected
            if (currentList) {
                int fromindex = currentList->itemNumber(fromblock);
                int toindex = currentList->itemNumber(toblock);
                if ((fromcursor.isNull() || fromcursor.currentList() != currentList) &&
                    (tocursor.isNull() || tocursor.currentList() != currentList) &&
                    fromindex <= 0 && (toindex < 0 || toindex == currentList->count()-1)
                ) {
                    currentList = 0;
                }
            }
        }
    }

    QHash<QTextList *, QString> listStyles = d->saveListStyles(fromblock, to);
    d->globalFrom = from;
    d->globalTo = to;
    d->writeBlocks(const_cast<QTextDocument *>(document), from, to, listStyles, currentTable, currentList);
}
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());
    }
}