📄 qtextcursor.cpp
字号:
return -1; return qMax(d->position, d->adjusted_anchor);}static void getText(QString &text, QTextDocumentPrivate *priv, const QString &docText, int pos, int end){ while (pos < end) { QTextDocumentPrivate::FragmentIterator fragIt = priv->find(pos); const QTextFragmentData * const frag = fragIt.value(); const int offsetInFragment = qMax(0, pos - fragIt.position()); const int len = qMin(int(frag->size - offsetInFragment), end - pos); text += QString(docText.constData() + frag->stringPosition + offsetInFragment, len); pos += len; }}/*! Returns the current selection's text (which may be empty). This only returns the text, with no rich text formatting information. If you want a document fragment (i.e. formatted rich text) use selection() instead. \note If the selection obtained from an editor spans a line break, the text will contain a Unicode U+2029 paragraph separator character instead of a newline \c{\n} character. Use QString::replace() to replace these characters with newlines.*/QString QTextCursor::selectedText() const{ if (!d || !d->priv || d->position == d->anchor) return QString(); const QString docText = d->priv->buffer(); QString text; QTextTable *table = d->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; } getText(text, d->priv, docText, cell.firstPosition(), cell.lastPosition()); } } } else { getText(text, d->priv, docText, selectionStart(), selectionEnd()); } return text;}/*! Returns the current selection (which may be empty) with all its formatting information. If you just want the selected text (i.e. plain text) use selectedText() instead. \note Unlike QTextDocumentFragment::toPlainText(), selectedText() may include special unicode characters such as QChar::ParagraphSeparator. \sa QTextDocumentFragment::toPlainText()*/QTextDocumentFragment QTextCursor::selection() const{ return QTextDocumentFragment(*this);}/*! Returns the block that contains the cursor.*/QTextBlock QTextCursor::block() const{ if (!d || !d->priv) return QTextBlock(); return d->block();}/*! Returns the block format of the block the cursor is in. \sa setBlockFormat() charFormat() */QTextBlockFormat QTextCursor::blockFormat() const{ if (!d || !d->priv) return QTextBlockFormat(); return d->block().blockFormat();}/*! Sets the block format of the current block (or all blocks that are contained in the selection) to \a format. \sa blockFormat(), mergeBlockFormat()*/void QTextCursor::setBlockFormat(const QTextBlockFormat &format){ if (!d || !d->priv) return; d->setBlockFormat(format, QTextDocumentPrivate::SetFormat);}/*! Modifies the block format of the current block (or all blocks that are contained in the selection) with the block format specified by \a modifier. \sa setBlockFormat(), blockFormat()*/void QTextCursor::mergeBlockFormat(const QTextBlockFormat &modifier){ if (!d || !d->priv) return; d->setBlockFormat(modifier, QTextDocumentPrivate::MergeFormat);}/*! Returns the block character format of the block the cursor is in. The block char format is the format used when inserting text at the beginning of an empty block. \sa setBlockCharFormat() */QTextCharFormat QTextCursor::blockCharFormat() const{ if (!d || !d->priv) return QTextCharFormat(); return d->block().charFormat();}/*! Sets the block char format of the current block (or all blocks that are contained in the selection) to \a format. \sa blockCharFormat()*/void QTextCursor::setBlockCharFormat(const QTextCharFormat &format){ if (!d || !d->priv) return; d->setBlockCharFormat(format, QTextDocumentPrivate::SetFormatAndPreserveObjectIndices);}/*! Modifies the block char format of the current block (or all blocks that are contained in the selection) with the block format specified by \a modifier. \sa setBlockCharFormat()*/void QTextCursor::mergeBlockCharFormat(const QTextCharFormat &modifier){ if (!d || !d->priv) return; d->setBlockCharFormat(modifier, QTextDocumentPrivate::MergeFormat);}/*! Returns the format of the character immediately before the cursor position(). If the cursor is positioned at the beginning of a text block that is not empty then the format of the character immediately after the cursor is returned. \sa insertText(), blockFormat() */QTextCharFormat QTextCursor::charFormat() const{ if (!d || !d->priv) return QTextCharFormat(); int idx = d->currentCharFormat; if (idx == -1) { QTextBlock block = d->block(); int pos; if (d->position == block.position() && block.length() > 1) pos = d->position; else pos = d->position - 1; if (pos == -1) { idx = d->priv->blockCharFormatIndex(d->priv->blockMap().firstNode()); } else { Q_ASSERT(pos >= 0 && pos < d->priv->length()); QTextDocumentPrivate::FragmentIterator it = d->priv->find(pos); Q_ASSERT(!it.atEnd()); idx = it.value()->format; } } QTextCharFormat cfmt = d->priv->formatCollection()->charFormat(idx); cfmt.clearProperty(QTextFormat::ObjectIndex); Q_ASSERT(cfmt.isValid()); return cfmt;}/*! Sets the cursor's current character format to the given \a format. If the cursor has a selection, the given \a format is applied to the current selection. \sa hasSelection(), mergeCharFormat()*/void QTextCursor::setCharFormat(const QTextCharFormat &format){ if (!d || !d->priv) return; if (d->position == d->anchor) { d->currentCharFormat = d->priv->formatCollection()->indexForFormat(format); return; } d->setCharFormat(format, QTextDocumentPrivate::SetFormatAndPreserveObjectIndices);}/*! Merges the cursor's current character format with the properties described by format \a modifier. If the cursor has a selection, this function applies all the properties set in \a modifier to all the character formats that are part of the selection. \sa hasSelection(), setCharFormat()*/void QTextCursor::mergeCharFormat(const QTextCharFormat &modifier){ if (!d || !d->priv) return; if (d->position == d->anchor) { QTextCharFormat format = charFormat(); format.merge(modifier); d->currentCharFormat = d->priv->formatCollection()->indexForFormat(format); return; } d->setCharFormat(modifier, QTextDocumentPrivate::MergeFormat);}/*! Returns true if the cursor is at the start of a block; otherwise returns false. \sa atBlockEnd(), atStart()*/bool QTextCursor::atBlockStart() const{ if (!d || !d->priv) return false; return d->position == d->block().position();}/*! Returns true if the cursor is at the end of a block; otherwise returns false. \sa atBlockStart(), atEnd()*/bool QTextCursor::atBlockEnd() const{ if (!d || !d->priv) return false; return d->position == d->block().position() + d->block().length() - 1;}/*! Returns true if the cursor is at the start of the document; otherwise returns false. \sa atBlockStart(), atEnd()*/bool QTextCursor::atStart() const{ if (!d || !d->priv) return false; return d->position == 0;}/*! Returns true if the cursor is at the end of the document; otherwise returns false. \sa atStart(), atBlockEnd()*/bool QTextCursor::atEnd() const{ if (!d || !d->priv) return false; return d->position == d->priv->length() - 1;}/*! Inserts a new empty block at the cursor position() with the current blockFormat() and charFormat(). \sa setBlockFormat()*/void QTextCursor::insertBlock(){ insertBlock(blockFormat());}/*! \overload Inserts a new empty block at the cursor position() with block format \a format and the current charFormat() as block char format. \sa setBlockFormat()*/void QTextCursor::insertBlock(const QTextBlockFormat &format){ QTextCharFormat charFmt = charFormat(); charFmt.clearProperty(QTextFormat::ObjectType); insertBlock(format, charFmt);}/*! \fn void QTextCursor::insertBlock(const QTextBlockFormat &format, const QTextCharFormat &charFormat) \overload Inserts a new empty block at the cursor position() with block format \a format and \a charFormat as block char format. \sa setBlockFormat()*/void QTextCursor::insertBlock(const QTextBlockFormat &format, const QTextCharFormat &_charFormat){ if (!d || !d->priv) return; QTextCharFormat charFormat = _charFormat; charFormat.clearProperty(QTextFormat::ObjectIndex); d->priv->beginEditBlock(); d->remove(); d->insertBlock(format, charFormat); d->priv->endEditBlock();}/*! Inserts a new block at the current position and makes it the first list item of a newly created list with the given \a format. Returns the created list. \sa currentList() createList() insertBlock() */QTextList *QTextCursor::insertList(const QTextListFormat &format){ insertBlock(); return createList(format);}/*! \overload Inserts a new block at the current position and makes it the first list item of a newly created list with the given \a style. Returns the created list. \sa currentList(), createList(), insertBlock() */QTextList *QTextCursor::insertList(QTextListFormat::Style style){ insertBlock(); return createList(style);}/*! Creates and returns a new list with the given \a format, and makes the current paragraph the cursor is in the first list item. \sa insertList() currentList() */QTextList *QTextCursor::createList(const QTextListFormat &format){ if (!d || !d->priv) return 0; QTextList *list = static_cast<QTextList *>(d->priv->createObject(format)); QTextBlockFormat modifier; modifier.setObjectIndex(list->objectIndex()); mergeBlockFormat(modifier); return list;}/*! \overload Creates and returns a new list with the given \a style, making the cursor's current paragraph the first list item. The style to be used is defined by the QTextListFormat::Style enum. \sa insertList() currentList() */QTextList *QTextCursor::createList(QTextListFormat::Style style){ QTextListFormat fmt; fmt.setStyle(style); return createList(fmt);}/*! Returns the current list if the cursor position() is inside a block that is part of a list; otherwise returns 0. \sa insertList() createList() */QTextList *QTextCursor::currentList() const{ if (!d || !d->priv)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -