📄 qtextcursor.cpp
字号:
case QTextCursor::Down: { int i = line.lineNumber() + 1; if (i >= layout->lineCount()) { int blockPosition = blockIt.position() + blockIt.length() - 1; QTextTable *table = qobject_cast<QTextTable *>(priv->frameAt(blockPosition)); if (table) { QTextTableCell cell = table->cellAt(blockPosition); if (cell.lastPosition() == blockPosition) { int row = cell.row() + cell.rowSpan(); if (row < table->rows()) { blockPosition = table->cellAt(row, cell.column()).firstPosition(); } else { // move to line below the table blockPosition = table->lastPosition() + 1; } blockIt = priv->blocksFind(blockPosition); } else { blockIt = blockIt.next(); } } else { blockIt = blockIt.next(); } if (blockIt == priv->blocksEnd()) return false; layout = blockIt.layout(); i = 0; } if (layout->lineCount()) { QTextLine line = layout->lineAt(i); newPosition = line.xToCursor(x) + blockIt.position(); } else { newPosition = blockIt.position(); } adjustX = false; break; } } if (mode == QTextCursor::KeepAnchor) { QTextTable *table = qobject_cast<QTextTable *>(priv->frameAt(position)); if (table && ((op >= QTextCursor::PreviousBlock && op <= QTextCursor::WordLeft) || (op >= QTextCursor::NextBlock && op <= QTextCursor::WordRight))) { int oldColumn = table->cellAt(position).column(); const QTextTableCell otherCell = table->cellAt(newPosition); if (!otherCell.isValid()) return false; int newColumn = otherCell.column(); if ((oldColumn > newColumn && op >= QTextCursor::End) || (oldColumn < newColumn && op <= QTextCursor::WordLeft)) return false; } } const bool moved = setPosition(newPosition); if (mode == QTextCursor::MoveAnchor) { anchor = position; adjusted_anchor = position; } else { adjustCursor(op); } if (adjustX) setX(); return moved;}QTextTable *QTextCursorPrivate::complexSelectionTable() const{ if (position == anchor) return 0; QTextTable *t = qobject_cast<QTextTable *>(priv->frameAt(position)); if (t) { QTextTableCell cell_pos = t->cellAt(position); QTextTableCell cell_anchor = t->cellAt(adjusted_anchor); Q_ASSERT(cell_anchor.isValid()); 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(); QTextCharFormat format = _format; format.clearProperty(QTextFormat::ObjectIndex); 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); QTextCharFormat format = _format; format.clearProperty(QTextFormat::ObjectIndex); 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 \brief The QTextCursor class offers an API to access and modify QTextDocuments. \ingroup text \ingroup shared \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 text document using the setCharFormat(), mergeCharFormat(), setBlockFormat() and mergeBlockFormat() functions. The 'set' functions will replace the cursor's current character or block format, while the 'merge' functions add the given format properties to the cursor's current format. If the cursor has a selection the given format is applied to the current selection. Note that when only parts of a block is selected the block format is applied to the entire block. 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 This enum describes the types of selection that can be applied with the select() function. \value Document Selects the entire document. \value BlockUnderCursor Selects the block of text under the cursor. \value LineUnderCursor Selects the line of text under the cursor. \value WordUnderCursor Selects the word under the cursor. If the cursor is not positioned within a string of selectable characters, no text is selected.*//*! 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()))
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -