/*! \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(); }
/*! \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() */ 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; } 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 (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); QTextObject::setFormat(tfmt); p->endEditBlock(); // qDebug() << "-------- end removeCols" << pos << num; }
/*! \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(); }