⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 qtexttable.cpp

📁 奇趣公司比较新的qt/emd版本
💻 CPP
📖 第 1 页 / 共 3 页
字号:
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;}/*!    \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();}/*!    \overload    \since 4.1    Merges the cells selected by the provided \a cursor.        \sa splitCell()*/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 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();}/*!    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()*/void QTextTable::setFormat(const QTextTableFormat &format){    QTextTableFormat fmt = format;    // don't try to change the number of table columns from here    fmt.setColumns(columns());    QTextObject::setFormat(fmt);}/*!    \fn QTextTableFormat QTextTable::format() const    Returns the table's format.    \sa setFormat()*/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -