📄 qtexttable.cpp
字号:
Resizes the table to contain the required number of \a rows and \a columns. \sa insertRows() insertColumns() removeRows() removeColumns()*/void QTextTable::resize(int rows, int cols){ Q_D(QTextTable); if (d->dirty) d->update(); d->pieceTable->beginEditBlock(); int nRows = this->rows(); int nCols = this->columns(); if (rows == nRows && cols == nCols) return; if (nCols < cols) insertColumns(nCols, cols - nCols); else if (nCols > cols) removeColumns(cols, nCols - cols); if (nRows < rows) insertRows(nRows, rows-nRows); else if (nRows > rows) removeRows(rows, nRows-rows); d->pieceTable->endEditBlock();}/*! \fn void QTextTable::insertRows(int index, int rows) Inserts a number of \a rows before the row with the specified \a index. \sa resize() insertColumns() removeRows() removeColumns()*/void QTextTable::insertRows(int pos, int num){ Q_D(QTextTable); if (num <= 0) return; if (d->dirty) d->update(); if (pos > d->nRows || pos < 0) pos = d->nRows;// qDebug() << "-------- insertRows" << pos << num; QTextDocumentPrivate *p = d->pieceTable; QTextFormatCollection *c = p->formatCollection(); p->beginEditBlock(); int extended = 0; int insert_before = 0; if (pos > 0 && pos < d->nRows) { for (int i = 0; i < d->nCols; ++i) { int cell = d->grid[pos*d->nCols + i]; if (cell == d->grid[(pos-1)*d->nCols+i]) { // cell spans the insertion place, extend it QTextDocumentPrivate::FragmentIterator it(&p->fragmentMap(), cell); QTextCharFormat fmt = c->charFormat(it->format); fmt.setTableCellRowSpan(fmt.tableCellRowSpan() + num); p->setCharFormat(it.position(), 1, fmt); extended++; } else if (!insert_before) { insert_before = cell; } } } else { insert_before = (pos == 0 ? d->grid[0] : d->fragment_end); } if (extended < d->nCols) { Q_ASSERT(insert_before); QTextDocumentPrivate::FragmentIterator it(&p->fragmentMap(), insert_before); QTextCharFormat fmt = c->charFormat(it->format); fmt.setTableCellRowSpan(1); fmt.setTableCellColumnSpan(1); Q_ASSERT(fmt.objectIndex() == objectIndex()); int pos = it.position(); int cfmt = p->formatCollection()->indexForFormat(fmt); int bfmt = p->formatCollection()->indexForFormat(QTextBlockFormat());// qDebug("inserting %d cells, nCols=%d extended=%d", num*(d->nCols-extended), d->nCols, extended); for (int i = 0; i < num*(d->nCols-extended); ++i) p->insertBlock(QTextBeginningOfFrame, pos, bfmt, cfmt, QTextUndoCommand::MoveCursor); }// qDebug() << "-------- end insertRows" << pos << num; p->endEditBlock();}/*! \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()*/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); setFormat(tfmt);// qDebug() << "-------- end insertCols" << pos << num; p->endEditBlock();}/*! \fn void QTextTable::removeRows(int index, int rows) Removes a number of \a rows starting with the row at the specified \a index. \sa insertRows(), insertColumns(), resize(), removeColumns()*/void QTextTable::removeRows(int pos, int num){ Q_D(QTextTable);// qDebug() << "-------- removeRows" << pos << num; if (num <= 0 || pos < 0) return; if (d->dirty) d->update(); if (pos >= d->nRows) return; if (pos+num > d->nRows) num = d->nRows - pos; QTextDocumentPrivate *p = d->pieceTable; QTextFormatCollection *collection = p->formatCollection(); p->beginEditBlock(); // delete whole table? if (pos == 0 && num == d->nRows) { 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 = pos; r < pos + num; ++r) { for (int c = 0; c < d->nCols; ++c) { int cell = d->grid[r*d->nCols + c]; QTextDocumentPrivate::FragmentIterator it(&p->fragmentMap(), cell); QTextCharFormat fmt = collection->charFormat(it->format); int span = fmt.tableCellRowSpan(); if (span > 1) { fmt.setTableCellRowSpan(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()); } } } p->endEditBlock();// qDebug() << "-------- end removeRows" << 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()*/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); setFormat(tfmt); p->endEditBlock();// qDebug() << "-------- end removeCols" << pos << num;}/*! \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.*/void QTextTable::mergeCells(int row, int column, int numRows, int numCols){ Q_D(QTextTable); if (d->dirty) d->update(); QTextDocumentPrivate *p = d->pieceTable; 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(); numRows = qMin(numRows, rows() - cell.row()); numCols = qMin(numCols, columns() - cell.column()); // nothing to merge? if (numRows < rowSpan || numCols < colSpan) return; p->beginEditBlock(); const int origCellPosition = cell.firstPosition() - 1; QVarLengthArray<int> cellMarkersToDelete((numCols - colSpan) * rowSpan + (numRows - rowSpan) * numCols); int idx = 0; for (int r = row; r < row + rowSpan; ++r) for (int c = column + colSpan; c < column + numCols; ++c) { const int cell = d->grid[r * d->nCols + c]; QTextDocumentPrivate::FragmentIterator it(&p->fragmentMap(), cell); cellMarkersToDelete[idx++] = it.position(); } for (int r = row + rowSpan; r < row + numRows; ++r) for (int c = column; c < column + numCols; ++c) { const int cell = d->grid[r * d->nCols + c]; QTextDocumentPrivate::FragmentIterator it(&p->fragmentMap(), cell); cellMarkersToDelete[idx++] = it.position(); } for (int i = 0; i < cellMarkersToDelete.size(); ++i) { p->remove(cellMarkersToDelete[i] - i, 1); } fmt.setTableCellRowSpan(numRows); fmt.setTableCellColumnSpan(numCols); p->setCharFormat(origCellPosition, 1, fmt); p->endEditBlock();}/*! \overload \since 4.1 Merges the cells selected by the provided \a cursor.*/void QTextTable::mergeCells(const QTextCursor &cursor){ if (!cursor.hasComplexSelection()) return; int firstRow, numRows, firstColumn, numColumns; cursor.selectedTableCells(&firstRow, &numRows, &firstColumn, &numColumns); mergeCells(firstRow, firstColumn, numRows, numColumns);}/*! \since 4.1 Splits the specfied cell at \a row and \a column into an array of multiple cells with dimensions specified by \a numRows and \a numCols.*/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); for (int r = row; r < row + numRows; ++r) { const QTextTableCell cell = cellAt(r, column + numCols); rowPositions[r - row] = cell.lastPosition(); } for (int r = row + numRows; r < row + rowSpan; ++r) { const QTextTableCell cell = cellAt(r, column); rowPositions[r - row] = cell.lastPosition(); } 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();}/*! Returns the number of rows in the table. \sa columns()*/int QTextTable::rows() const{ Q_D(const QTextTable); if (d->dirty) d->update(); return d->nRows;}/*! Returns the number of columns in the table. \sa rows()*/int QTextTable::columns() const{ Q_D(const QTextTable); if (d->dirty) d->update(); return d->nCols;}#if 0void QTextTable::mergeCells(const QTextCursor &selection){}#endif/*! \fn QTextCursor QTextTable::rowStart(const QTextCursor &cursor) const Returns a cursor pointing to the start of the row that contains the given \a cursor. \sa rowEnd()*/QTextCursor QTextTable::rowStart(const QTextCursor &c) const{ Q_D(const QTextTable); QTextTableCell cell = cellAt(c); if (!cell.isValid()) return QTextCursor(); int row = cell.row(); QTextDocumentPrivate *p = d->pieceTable; QTextDocumentPrivate::FragmentIterator it(&p->fragmentMap(), d->grid[row*d->nCols]); return QTextCursor(p, it.position());}/*! \fn QTextCursor QTextTable::rowEnd(const QTextCursor &cursor) const Returns a cursor pointing to the end of the row that contains the given \a cursor. \sa rowStart()*/QTextCursor QTextTable::rowEnd(const QTextCursor &c) const{ Q_D(const QTextTable); QTextTableCell cell = cellAt(c); if (!cell.isValid()) return QTextCursor(); int row = cell.row() + 1; int fragment = row < d->nRows ? d->grid[row*d->nCols] : d->fragment_end; QTextDocumentPrivate *p = d->pieceTable; QTextDocumentPrivate::FragmentIterator it(&p->fragmentMap(), fragment); return QTextCursor(p, it.position() - 1);}/*! \fn void QTextTable::setFormat(const QTextTableFormat &format) Sets the table's \a format. \sa format()*//*! \fn QTextTableFormat QTextTable::format() const Returns the table's format. \sa setFormat()*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -