📄 qtextcursor.cpp
字号:
if (cell_pos == cell_anchor) t = 0; } return t;}void QTextCursorPrivate::selectedTableCells(int *firstRow, int *numRows, int *firstColumn, int *numColumns) const{ *firstRow = -1; *firstColumn = -1; *numRows = -1; *numColumns = -1; if (position == anchor) return; QTextTable *t = qobject_cast<QTextTable *>(priv->frameAt(position)); if (!t) return; QTextTableCell cell_pos = t->cellAt(position); QTextTableCell cell_anchor = t->cellAt(adjusted_anchor); Q_ASSERT(cell_anchor.isValid()); if (cell_pos == cell_anchor) return; *firstRow = qMin(cell_pos.row(), cell_anchor.row()); *firstColumn = qMin(cell_pos.column(), cell_anchor.column()); *numRows = qMax(cell_pos.row() + cell_pos.rowSpan(), cell_anchor.row() + cell_anchor.rowSpan()) - *firstRow; *numColumns = qMax(cell_pos.column() + cell_pos.columnSpan(), cell_anchor.column() + cell_anchor.columnSpan()) - *firstColumn;}static void setBlockCharFormat(QTextDocumentPrivate *priv, int pos1, int pos2, const QTextCharFormat &format, QTextDocumentPrivate::FormatChangeMode changeMode){ QTextBlock it = priv->blocksFind(pos1); QTextBlock end = priv->blocksFind(pos2); if (end.isValid()) end = end.next(); for (; it != end; it = it.next()) { priv->setCharFormat(it.position() - 1, 1, format, changeMode); }}void QTextCursorPrivate::setBlockCharFormat(const QTextCharFormat &format, QTextDocumentPrivate::FormatChangeMode changeMode){ priv->beginEditBlock(); QTextTable *table = complexSelectionTable(); if (table) { int row_start, col_start, num_rows, num_cols; selectedTableCells(&row_start, &num_rows, &col_start, &num_cols); 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); int rspan = cell.rowSpan(); 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; } int pos1 = cell.firstPosition(); int pos2 = cell.lastPosition(); ::setBlockCharFormat(priv, pos1, pos2, format, changeMode); } } } else { int pos1 = position; int pos2 = adjusted_anchor; if (pos1 > pos2) { pos1 = adjusted_anchor; pos2 = position; } ::setBlockCharFormat(priv, pos1, pos2, format, changeMode); } priv->endEditBlock();}void QTextCursorPrivate::setBlockFormat(const QTextBlockFormat &format, QTextDocumentPrivate::FormatChangeMode changeMode){ QTextTable *table = complexSelectionTable(); if (table) { priv->beginEditBlock(); int row_start, col_start, num_rows, num_cols; selectedTableCells(&row_start, &num_rows, &col_start, &num_cols); 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); int rspan = cell.rowSpan(); 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; } int pos1 = cell.firstPosition(); int pos2 = cell.lastPosition(); priv->setBlockFormat(priv->blocksFind(pos1), priv->blocksFind(pos2), format, changeMode); } } priv->endEditBlock(); } else { int pos1 = position; int pos2 = adjusted_anchor; if (pos1 > pos2) { pos1 = adjusted_anchor; pos2 = position; } priv->setBlockFormat(priv->blocksFind(pos1), priv->blocksFind(pos2), format, changeMode); }}void QTextCursorPrivate::setCharFormat(const QTextCharFormat &format, QTextDocumentPrivate::FormatChangeMode changeMode){ Q_ASSERT(position != anchor); QTextTable *table = complexSelectionTable(); if (table) { priv->beginEditBlock(); int row_start, col_start, num_rows, num_cols; selectedTableCells(&row_start, &num_rows, &col_start, &num_cols); 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); int rspan = cell.rowSpan(); 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; } int pos1 = cell.firstPosition(); int pos2 = cell.lastPosition(); priv->setCharFormat(pos1, pos2-pos1, format, changeMode); } } priv->endEditBlock(); } else { int pos1 = position; int pos2 = adjusted_anchor; if (pos1 > pos2) { pos1 = adjusted_anchor; pos2 = position; } priv->setCharFormat(pos1, pos2-pos1, format, changeMode); }}/*! \class QTextCursor qtextcursor.h \brief The QTextCursor class offers an API to access and modify QTextDocuments. \ingroup text \mainclass Text cursors are objects that are used to access and modify the contents and underlying structure of text documents via a programming interface that mimics the behavior of a cursor in a text editor. QTextCursor contains information about both the cursor's position within a QTextDocument and any selection that it has made. QTextCursor is modeled on the way a text cursor behaves in a text editor, providing a programmatic means of performing standard actions through the user interface. A document can be thought of as a single string of characters with the cursor's position() being \e between any two characters (or at the very beginning or very end of the document). Documents can also contain tables, lists, images, and other objects in addition to text but, from the developer's point of view, the document can be treated as one long string. Some portions of that string can be considered to lie within particular blocks (e.g. paragraphs), or within a table's cell, or a list's item, or other structural elements. When we refer to "current character" we mean the character immediately after the cursor position() in the document; similarly the "current block" is the block that contains the cursor position(). A QTextCursor also has an anchor() position. The text that is between the anchor() and the position() is the selection. If anchor() == position() there is no selection. The cursor position can be changed programmatically using setPosition() and movePosition(); the latter can also be used to select text. For selections see selectionStart(), selectionEnd(), hasSelection(), clearSelection(), and removeSelectedText(). If the position() is at the start of a block atBlockStart() returns true; and if it is at the end of a block atBlockEnd() returns true. The format of the current character is returned by charFormat(), and the format of the current block is returned by blockFormat(). Formatting can be applied to the current character (the character immedately after position()) using applyCharFormatModifier(), and to the current block (the block that contains position()) using setBlockFormat() and applyBlockFormatModifier(). The text at the current character position can be turned into a list using createList(). Deletions can be achieved using deleteChar(), deletePreviousChar(), and removeSelectedText(). Text strings can be inserted into the document with the insertText() function, blocks (representing new paragraphs) can be inserted with insertBlock(). Existing fragments of text can be inserted with insertFragment() but, if you want to insert pieces of text in various formats, it is usually still easier to use insertText() and supply a character format. Various types of higher-level structure can also be inserted into the document with the cursor: \list \i Lists are ordered sequences of block elements that are decorated with bullet points or symbols. These are inserted in a specified format with insertList(). \i Tables are inserted with the insertTable() function, and can be given an optional format. These contain an array of cells that can be traversed using the cursor. \i Inline images are inserted with insertImage(). The image to be used can be specified in an image format, or by name. \i Frames are inserted by calling insertFrame() with a specified format. \endlist Actions can be grouped (i.e. treated as a single action for undo/redo) using beginEditBlock() and endEditBlock(). Cursor movements are limited to valid cursor positions. In Latin writing this is usually after every character in the text. In some other writing systems cursor movements are limited to "clusters" (e.g. a syllable in Devanagari, or a base letter plus diacritics). Functions such as movePosition() and deleteChar() limit cursor movement to these valid positions. \sa \link richtext.html Rich Text Processing\endlink*//*! \enum QTextCursor::MoveOperation \value NoMove Keep the cursor where it is \value Start Move to the start of the document. \value StartOfLine Move to the start of the current line. \value StartOfBlock Move to the start of the current block. \value StartOfWord Move to the start of the current word. \value PreviousBlock Move to the start of the previous block. \value PreviousCharacter Move to the previous character. \value PreviousWord Move to the beginning of the previous word. \value Up Move up one line. \value Left Move left one character. \value WordLeft Move left one word. \value End Move to the end of the document. \value EndOfLine Move to the end of the current line. \value EndOfWord Move to the end of the current word. \value EndOfBlock Move to the end of the current block. \value NextBlock Move to the beginning of the next block. \value NextCharacter Move to the next character. \value NextWord Move to the next word. \value Down Move down one line. \value Right Move right one character. \value WordRight Move right one word. \sa movePosition()*//*! \enum QTextCursor::MoveMode \value MoveAnchor Moves the anchor to the same position as the cursor itself. \value KeepAnchor Keeps the anchor where it is. If the anchor() is kept where it is and the position() is moved, the text in between will be selected.*//*! \enum QTextCursor::SelectionType \value WordUnderCursor Selects the word under the cursor. If the cursor is not positioned within a string of selectable characters, no text is selected. \value LineUnderCursor Selects the line of text under the cursor. \value BlockUnderCursor Selects the block of text under the cursor.*//*! Constructs a null cursor. */QTextCursor::QTextCursor() : d(0){}/*! Constructs a cursor pointing to the beginning of the \a document. */QTextCursor::QTextCursor(QTextDocument *document) : d(new QTextCursorPrivate(document->docHandle())){}/*! Constructs a cursor pointing to the beginning of the \a frame.*/QTextCursor::QTextCursor(QTextFrame *frame) : d(new QTextCursorPrivate(frame->document()->docHandle())){ d->adjusted_anchor = d->anchor = d->position = frame->firstPosition();}/*! Constructs a cursor pointing to the beginning of the \a block.*/QTextCursor::QTextCursor(const QTextBlock &block) : d(new QTextCursorPrivate(block.docHandle())){ d->adjusted_anchor = d->anchor = d->position = block.position();}/*! \internal */QTextCursor::QTextCursor(QTextDocumentPrivate *p, int pos) : d(new QTextCursorPrivate(p)){ d->adjusted_anchor = d->anchor = d->position = pos; d->setX();}/*! \internal*/QTextCursor::QTextCursor(QTextCursorPrivate *d){ Q_ASSERT(d); this->d = d;}/*! Constructs a new cursor that is a copy of \a cursor. */QTextCursor::QTextCursor(const QTextCursor &cursor){ d = cursor.d;}/*! Makes a copy of \a cursor and assigns it to this QTextCursor. */QTextCursor &QTextCursor::operator=(const QTextCursor &cursor){ d = cursor.d; return *this;}/*! Destroys the QTextCursor. */QTextCursor::~QTextCursor(){}/*! Returns true if the cursor is null; otherwise returns false. A null cursor is created by the default constructor. */bool QTextCursor::isNull() const{ return !d || !d->priv;}/*! Moves the cursor to the absolute position in the document specified by \a pos using a \c MoveMode specified by \a m. The cursor is positioned between characters. \sa position() movePosition() anchor()*/void QTextCursor::setPosition(int pos, MoveMode m){ if (!d || !d->priv) return; if (pos < 0 || pos >= d->priv->length()) { qWarning("QTextCursor::setPosition: position '%d' out of range", pos); return; } d->setPosition(pos); if (m == MoveAnchor) { d->anchor = pos; d->adjusted_anchor = pos; } else { // keep anchor QTextCursor::MoveOperation op; if (pos < d->anchor) op = QTextCursor::Left; else op = QTextCursor::Right; d->adjustCursor(op); } d->setX();}/*! Returns the absolute position of the cursor within the document. The cursor is positioned between characters. \sa setPosition() movePosition() anchor()*/int QTextCursor::position() const{ if (!d || !d->priv) return -1; return d->position;}/*! Returns the anchor position; this is the same as position() unless there is a selection in which case position() marks one end of the selection and anchor() marks the other end. Just like the cursor position, the anchor position is between characters. \sa position() setPosition() movePosition() selectionStart() selectionEnd()*/int QTextCursor::anchor() const{ if (!d || !d->priv) return -1; return d->anchor;}/*! \fn bool QTextCursor::movePosition(MoveOperation operation, MoveMode mode, int n) Moves the cursor by performing the given \a operation \a n times, using the specified \a mode, and returns true if all operations were completed successfully; otherwise returns false. For example, if this function is repeatedly used to seek to the end of the next word, it will eventually fail when the end of the document is reached. By default, the move operation is performed once (\a n = 1). If \a mode is \c KeepAnchor, the cursor selects the text it moves over. This is the same effect that the user achieves when they hold down the Shift key and move the cursor with the cursor keys.*/bool QTextCursor::movePosition(MoveOperation op, MoveMode mode, int n){ if (!d || !d->priv) return false; switch (op) { case Start: case StartOfLine: case End: case EndOfLine: n = 1; break; default: break; } for (; n > 0; --n) { if (!d->movePosition(op, mode)) return false; } return true;}/*! Inserts \a text at the current position, using the current character format. If there is a selection, the selection is deleted and replaced by
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -