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

📄 qtexttable.cpp

📁 奇趣公司比较新的qt/emd版本
💻 CPP
📖 第 1 页 / 共 3 页
字号:
    Q_Q(const QTextTable);    nCols = q->format().columns();    nRows = (cells.size() + nCols-1)/nCols;//     qDebug(">>>> QTextTablePrivate::update, nRows=%d, nCols=%d", nRows, nCols);    grid = (int *)realloc(grid, nRows*nCols*sizeof(int));    memset(grid, 0, nRows*nCols*sizeof(int));    QTextDocumentPrivate *p = pieceTable;    QTextFormatCollection *c = p->formatCollection();    cellIndices.resize(cells.size());    int cell = 0;    for (int i = 0; i < cells.size(); ++i) {        int fragment = cells.at(i);        QTextCharFormat fmt = c->charFormat(QTextDocumentPrivate::FragmentIterator(&p->fragmentMap(), fragment)->format);        int rowspan = fmt.tableCellRowSpan();        int colspan = fmt.tableCellColumnSpan();        // skip taken cells        while (cell < nRows*nCols && grid[cell])            ++cell;        int r = cell/nCols;        int c = cell%nCols;        cellIndices[i] = cell;        if (r + rowspan > nRows) {            grid = (int *)realloc(grid, sizeof(int)*(r + rowspan)*nCols);            memset(grid + (nRows*nCols), 0, sizeof(int)*(r+rowspan-nRows)*nCols);            nRows = r + rowspan;        }        Q_ASSERT(c + colspan <= nCols);        for (int ii = 0; ii < rowspan; ++ii) {            for (int jj = 0; jj < colspan; ++jj) {                Q_ASSERT(grid[(r+ii)*nCols + c+jj] == 0);                grid[(r+ii)*nCols + c+jj] = fragment;//  		    qDebug("    setting cell %d span=%d/%d at %d/%d", fragment, rowspan, colspan, r+ii, c+jj);            }        }    }//     qDebug("<<<< end: nRows=%d, nCols=%d", nRows, nCols);    dirty = false;}/*!    \class QTextTable qtexttable.h    \brief The QTextTable class represents a table in a QTextDocument.    \ingroup text    A table is a group of cells ordered into rows and columns. Each table    contains at least one row and one column. Each cell contains a block, and    is surrounded by a frame.    Tables are usually created and inserted into a document with the    QTextCursor::insertTable() function.    For example, we can insert a table with three rows and two columns at the    current cursor position in an editor using the following lines of code:    \quotefromfile snippets/textdocument-tables/mainwindow.cpp    \skipto QTextCursor cursor(editor    \printuntil cursor.movePosition(QTextCursor::Start);    \skipto QTextTable *table = cursor    \printuntil QTextTable *table = cursor    The table format is either defined when the table is created or changed    later with setFormat().    The table currently being edited by the cursor is found with    QTextCursor::currentTable(). This allows its format or dimensions to be    changed after it has been inserted into a document.    A table's size can be changed with resize(), or by using    insertRows(), insertColumns(), removeRows(), or removeColumns().    Use cellAt() to retrieve table cells.    The starting and ending positions of table rows can be found by moving    a cursor within a table, and using the rowStart() and rowEnd() functions    to obtain cursors at the start and end of each row.    Rows and columns within a QTextTable can be merged and split using    the mergeCells() and splitCell() functions. However, only cells that span multiple    rows or columns can be split. (Merging or splitting does not increase or decrease    the number of rows and columns.)        \table 80%    \row        \o \inlineimage texttable-split.png Original Table        \o Suppose we have a 2x6 table of names and addresses. To merge both        columns in the first row we invoke mergeCells() with \a row = 0,        \a column = 0, \a numRows = 1 and \a numColumns = 2.        \quotefromfile snippets/textdocument-texttable/main.cpp        \skipto table->mergeCells        \printuntil );     \row        \o \inlineimage texttable-merge.png        \o  This gives us the following table. To split the first row of the table        back into two cells, we invoke the splitCell() function with \a numRows        and \a numCols = 1.        \skipto table->splitCell        \printuntil );    \row        \o \inlineimage texttable-split.png Split Table        \o This results in the original table.    \endtable        \sa QTextTableFormat*//*! \internal */QTextTable::QTextTable(QTextDocument *doc)    : QTextFrame(*new QTextTablePrivate, doc){}/*! \internalDestroys the table. */QTextTable::~QTextTable(){}/*!    \fn QTextTableCell QTextTable::cellAt(int row, int column) const    Returns the table cell at the given \a row and \a column in the table.    \sa columns() rows()*/QTextTableCell QTextTable::cellAt(int row, int col) const{    Q_D(const QTextTable);    if (d->dirty)        d->update();    if (row < 0 || row >= d->nRows || col < 0 || col >= d->nCols)        return QTextTableCell();    return QTextTableCell(this, d->grid[row*d->nCols + col]);}/*!    \overload    Returns the table cell that contains the character at the given \a position    in the document.*/QTextTableCell QTextTable::cellAt(int position) const{    Q_D(const QTextTable);    if (d->dirty)        d->update();    uint pos = (uint)position;    const QTextDocumentPrivate::FragmentMap &m = d->pieceTable->fragmentMap();    if (position < 0 || m.position(d->fragment_start) >= pos || m.position(d->fragment_end) < pos)        return QTextTableCell();    QFragmentFindHelper helper(position, m);    QList<int>::ConstIterator it = qLowerBound(d->cells.begin(), d->cells.end(), helper);    if (it != d->cells.begin())        --it;    return QTextTableCell(this, *it);}/*!    \fn QTextTableCell QTextTable::cellAt(const QTextCursor &cursor) const    \overload    Returns the table cell containing the given \a cursor.*/QTextTableCell QTextTable::cellAt(const QTextCursor &c) const{    return cellAt(c.position());}/*!    \fn void QTextTable::resize(int rows, int columns)    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);    QTextObject::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()*/

⌨️ 快捷键说明

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