📄 qtextcursor.cpp
字号:
{ 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 \a text, for example: \code cursor.clearSelection(); cursor.movePosition(QTextCursor::NextWord, QTextCursor::KeepAnchor); cursor.insertText("Hello World"); \endcode This clears any existing selection, selects the word at the cursor (i.e. from position() forward), and replaces the selection with the phrase "Hello World". Any ASCII linefeed characters (\\n) in the inserted text are transformed into unicode block separators, corresponding to insertBlock() calls. \sa charFormat() hasSelection()*/void QTextCursor::insertText(const QString &text){ QTextCharFormat fmt = charFormat(); fmt.clearProperty(QTextFormat::ObjectType); insertText(text, fmt);}/*! \fn void QTextCursor::insertText(const QString &text, const QTextCharFormat &format) \overload Inserts \a text at the current position with the given \a format.*/void QTextCursor::insertText(const QString &text, const QTextCharFormat &_format){ if (!d || !d->priv) return; Q_ASSERT(_format.isValid()); QTextCharFormat format = _format; format.clearProperty(QTextFormat::ObjectIndex); d->priv->beginEditBlock(); d->remove(); if (!text.isEmpty()) { QTextFormatCollection *formats = d->priv->formatCollection(); int formatIdx = formats->indexForFormat(format); Q_ASSERT(formats->format(formatIdx).isCharFormat()); QTextBlockFormat blockFmt = blockFormat(); int textStart = 0; for (int i = 0; i < text.length(); ++i) { QChar ch = text.at(i); const int textEnd = i; if (ch == QLatin1Char('\r') && (i + 1) < text.length() && text.at(i + 1) == QLatin1Char('\n')) { ++i; ch = text.at(i); } if (ch == QLatin1Char('\n') || ch == QChar::ParagraphSeparator || ch == QLatin1Char('\r')) { if (textEnd > textStart) d->priv->insert(d->position, QString(text.unicode() + textStart, textEnd - textStart), formatIdx); textStart = i + 1; d->insertBlock(blockFmt, format); } } if (textStart < text.length()) d->priv->insert(d->position, QString(text.unicode() + textStart, text.length() - textStart), formatIdx); } d->priv->endEditBlock(); if (!d->priv->isInEditBlock()) d->setX();}/*! If there is no selected text, deletes the character \e at the current cursor position; otherwise deletes the selected text. \sa deletePreviousChar() hasSelection() clearSelection()*/void QTextCursor::deleteChar(){ if (!d || !d->priv) return; if (d->position == d->anchor) { if (!d->canDelete(d->position)) return; d->adjusted_anchor = d->anchor = d->priv->nextCursorPosition(d->anchor, QTextLayout::SkipCharacters); } d->remove(); d->setX();}/*! If there is no selected text, deletes the character \e before the current cursor position; otherwise deletes the selected text. \sa deleteChar() hasSelection() clearSelection()*/void QTextCursor::deletePreviousChar(){ if (!d || !d->priv) return; if (d->position == d->anchor) { if (d->anchor < 1 || !d->canDelete(d->anchor-1)) return; d->anchor--; QTextDocumentPrivate::FragmentIterator fragIt = d->priv->find(d->anchor); const QTextFragmentData * const frag = fragIt.value(); int fpos = fragIt.position(); QChar uc = d->priv->buffer().at(d->anchor - fpos + frag->stringPosition); if (d->anchor > fpos && uc.unicode() >= 0xdc00 && uc.unicode() < 0xe000) { // second half of a surrogate, check if we have the first half as well, // if yes delete both at once uc = d->priv->buffer().at(d->anchor - 1 - fpos + frag->stringPosition); if (uc.unicode() >= 0xd800 && uc.unicode() < 0xdc00) --d->anchor; } d->adjusted_anchor = d->anchor; } d->remove(); d->setX();}/*! Selects text in the document according to the given \a selection.*/void QTextCursor::select(SelectionType selection){ if (!d || !d->priv) return; clearSelection(); const QTextBlock block = d->block(); switch (selection) { case LineUnderCursor: movePosition(StartOfLine); movePosition(EndOfLine, KeepAnchor); break; case WordUnderCursor: movePosition(StartOfWord); movePosition(EndOfWord, KeepAnchor); break; case BlockUnderCursor: movePosition(StartOfBlock); // also select the paragraph separator if (movePosition(PreviousBlock)) { movePosition(EndOfBlock); movePosition(NextBlock, KeepAnchor); } movePosition(EndOfBlock, KeepAnchor); break; case Document: movePosition(Start); movePosition(End, KeepAnchor); break; }}/*! Returns true if the cursor contains a selection; otherwise returns false.*/bool QTextCursor::hasSelection() const{ return !!d && d->position != d->anchor;}/*! Returns true if the cursor contains a selection that is not simply a range from selectionStart() to selectionEnd(); otherwise returns false. Complex selections are ones that span at least two cells in a table; their extent is specified by selectedTableCells().*/bool QTextCursor::hasComplexSelection() const{ if (!d) return false; return d->complexSelectionTable() != 0;}/*! If the selection spans over table cells, \a firstRow is populated with the number of the first row in the selection, \a firstColumn with the number of the first column in the selection, and \a numRows and \a numColumns with the number of rows and columns in the selection. If the selection does not span any table cells the results are harmless but undefined.*/void QTextCursor::selectedTableCells(int *firstRow, int *numRows, int *firstColumn, int *numColumns) const{ *firstRow = -1; *firstColumn = -1; *numRows = -1; *numColumns = -1; if (!d || d->position == d->anchor) return; d->selectedTableCells(firstRow, numRows, firstColumn, numColumns);}/*! Clears the current selection by setting the anchor to the cursor position. Note that it does \bold{not} delete the text of the selection. \sa removeSelectedText() hasSelection()*/void QTextCursor::clearSelection(){ if (!d) return; d->adjusted_anchor = d->anchor = d->position; d->currentCharFormat = -1;}/*! If there is a selection, its content is deleted; otherwise does nothing. \sa hasSelection()*/void QTextCursor::removeSelectedText(){ if (!d || !d->priv || d->position == d->anchor) return; d->remove(); d->setX();}/*! Returns the start of the selection or position() if the cursor doesn't have a selection. \sa selectionEnd() position() anchor()*/int QTextCursor::selectionStart() const{ if (!d || !d->priv) return -1; return qMin(d->position, d->adjusted_anchor);}/*! Returns the end of the selection or position() if the cursor doesn't have a selection. \sa selectionStart() position() anchor()*/int QTextCursor::selectionEnd() const{ if (!d || !d->priv)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -