📄 qtextobject.cpp
字号:
int pos = p->blockMap().position(n); int len = p->blockMap().size(n); return position >= pos && position < pos + len;}/*! Returns the QTextLayout that is used to lay out and display the block's contents. Note that the returned QTextLayout object can only be modified from the documentChanged implementation of a QAbstractTextDocumentLayout subclass. Any changes applied from the outside cause undefined behavior. */QTextLayout *QTextBlock::layout() const{ if (!p || !n) return 0; const QTextBlockData *b = p->blockMap().fragment(n); if (!b->layout) b->layout = new QTextLayout(*this); return b->layout;}/*! Returns the QTextBlockFormat that describes block-specific properties. \sa charFormat() */QTextBlockFormat QTextBlock::blockFormat() const{ if (!p || !n) return QTextFormat().toBlockFormat(); return p->formatCollection()->blockFormat(p->blockMap().fragment(n)->format);}/*! Returns an index into the document's internal list of block formats for the text block's format. \sa QTextDocument::object()*/int QTextBlock::blockFormatIndex() const{ if (!p || !n) return -1; return p->blockMap().fragment(n)->format;}/*! Returns the QTextCharFormat that describes the block's character format. The block's character format is used when inserting text into an empty block. \sa blockFormat() */QTextCharFormat QTextBlock::charFormat() const{ if (!p || !n) return QTextFormat().toCharFormat(); return p->formatCollection()->charFormat(charFormatIndex());}/*! Returns an index into the document's internal list of character formats for the text block's character format. \sa QTextDocument::object()*/int QTextBlock::charFormatIndex() const{ if (!p || !n) return -1; return p->blockCharFormatIndex(n);}/*! Returns the block's contents as plain text. \sa length() charFormat() blockFormat() */QString QTextBlock::text() const{ if (!p || !n) return QString(); const QString buffer = p->buffer(); QString text; text.reserve(length()); const int pos = position(); QTextDocumentPrivate::FragmentIterator it = p->find(pos); QTextDocumentPrivate::FragmentIterator end = p->find(pos + length() - 1); // -1 to omit the block separator char for (; it != end; ++it) { const QTextFragmentData * const frag = it.value(); text += QString::fromRawData(buffer.constData() + frag->stringPosition, frag->size); } return text;}/*! Returns the text document this text block belongs to, or 0 if the text block does not belong to any document.*/const QTextDocument *QTextBlock::document() const{ return p ? p->document() : 0;}/*! If the block represents a list item, returns the list that the item belongs to; otherwise returns 0.*/QTextList *QTextBlock::textList() const{ if (!isValid()) return 0; const QTextBlockFormat fmt = blockFormat(); QTextObject *obj = p->document()->objectForFormat(fmt); return qobject_cast<QTextList *>(obj);}/*! \since 4.1 Returns a pointer to a QTextBlockUserData object if previously set with setUserData() or a null pointer.*/QTextBlockUserData *QTextBlock::userData() const{ if (!p || !n) return 0; const QTextBlockData *b = p->blockMap().fragment(n); return b->userData;}/*! \since 4.1 Attaches the given \a data object to the text block. QTextBlockUserData can be used to store custom settings. The ownership is passed to the underlying text document, i.e. the provided QTextBlockUserData object will be deleted if the corresponding text block gets deleted. The user data object is not stored in the undo history, so it will not be available after undoing the deletion of a text block. For example, if you write a programming editor in an IDE, you may want to let your user set breakpoints visually in your code for an integrated debugger. In a programming editor a line of text usually corresponds to one QTextBlock. The QTextBlockUserData interface allows the developer to store data for each QTextBlock, like for example in which lines of the source code the user has a breakpoint set. Of course this could also be stored externally, but by storing it inside the QTextDocument, it will for example be automatically deleted when the user deletes the associated line. It's really just a way to store custom information in the QTextDocument without using custom properties in QTextFormat which would affect the undo/redo stack.*/void QTextBlock::setUserData(QTextBlockUserData *data){ if (!p || !n) return; const QTextBlockData *b = p->blockMap().fragment(n); b->userData = data;}/*! \since 4.1 Returns the integer value previously set with setUserState() or -1.*/int QTextBlock::userState() const{ if (!p || !n) return -1; const QTextBlockData *b = p->blockMap().fragment(n); return b->userState;}/*! \since 4.1 Stores the specified \a state integer value in the text block. This may be useful for example in a syntax highlighter to store a text parsing state.*/void QTextBlock::setUserState(int state){ if (!p || !n) return; const QTextBlockData *b = p->blockMap().fragment(n); b->userState = state;}/*! Returns a text block iterator pointing to the beginning of the text block. \sa end()*/QTextBlock::iterator QTextBlock::begin() const{ if (!p || !n) return iterator(); int pos = position(); int len = length() - 1; // exclude the fragment that holds the paragraph separator int b = p->fragmentMap().findNode(pos); int e = p->fragmentMap().findNode(pos+len); return iterator(p, b, e, b);}/*! Returns a text block iterator pointing to the end of the text block. \sa begin() next() previous()*/QTextBlock::iterator QTextBlock::end() const{ if (!p || !n) return iterator(); int pos = position(); int len = length() - 1; // exclude the fragment that holds the paragraph separator int b = p->fragmentMap().findNode(pos); int e = p->fragmentMap().findNode(pos+len); return iterator(p, b, e, e);}/*! Returns the text block in the document after this block, or an empty text block if this is the last one. Note that the next block may be in a different frame or table to this block. \sa previous() begin() end()*/QTextBlock QTextBlock::next() const{ if (!p) return QTextBlock(); return QTextBlock(p, p->blockMap().next(n));}/*! Returns the text block in the document before this block, or an empty text block if this is the first one. Note that the next block may be in a different frame or table to this block. \sa next() begin() end()*/QTextBlock QTextBlock::previous() const{ if (!p) return QTextBlock(); return QTextBlock(p, p->blockMap().previous(n));}/*! Returns the text fragment the iterator currently points to.*/QTextFragment QTextBlock::iterator::fragment() const{ int ne = n; int formatIndex = p->fragmentMap().fragment(n)->format; do { ne = p->fragmentMap().next(ne); } while (ne != e && p->fragmentMap().fragment(ne)->format == formatIndex); return QTextFragment(p, n, ne);}/*! The prefix ++ operator (\c{++i}) advances the iterator to the next item in the hash and returns an iterator to the new current item.*/QTextBlock::iterator &QTextBlock::iterator::operator++(){ int ne = n; int formatIndex = p->fragmentMap().fragment(n)->format; do { ne = p->fragmentMap().next(ne); } while (ne != e && p->fragmentMap().fragment(ne)->format == formatIndex); n = ne; return *this;}/*! The prefix -- operator (\c{--i}) makes the preceding item current and returns an iterator pointing to the new current item.*/QTextBlock::iterator &QTextBlock::iterator::operator--(){ n = p->fragmentMap().previous(n); if (n == b) return *this; int formatIndex = p->fragmentMap().fragment(n)->format; int last = n; while (n != b && p->fragmentMap().fragment(n)->format != formatIndex) { last = n; n = p->fragmentMap().previous(n); } n = last; return *this;}/*! \class QTextFragment \brief The QTextFragment class holds a piece of text in a QTextDocument with a single QTextCharFormat. \ingroup text A text fragment describes a piece of text that is stored with a single character format. Text in which the character format changes can be represented by sequences of text fragments with different formats. If the user edits the text in a fragment and introduces a different character format, the fragment's text will be split at each point where the format changes, and new fragments will be created. For example, changing the style of some text in the middle of a sentence will cause the fragment to be broken into three separate fragments: the first and third with the same format as before, and the second with the new style. The first fragment will contain the text from the beginning of the sentence, the second will contain the text from the middle, and the third takes the text from the end of the sentence. \img qtextfragment-split.png A fragment's text and character format can be obtained with the text() and charFormat() functions. The length() function gives the length of the text in the fragment. position() gives the position in the document of the start of the fragment. To determine whether the fragment contains a particular position within the document, use the contains() function. \sa QTextDocument, {Rich Text Document Structure}*//*! \fn QTextFragment::QTextFragment(const QTextDocumentPrivate *priv, int f, int fe) \internal*//*! \fn QTextFragment::QTextFragment() Creates a new empty text fragment.*//*! \fn QTextFragment::QTextFragment(const QTextFragment &other) Copies the content (text and format) of the \a other text fragment to this text fragment.*//*! \fn QTextFragment &QTextFragment::operator=(const QTextFragment &other) Assigns the content (text and format) of the \a other text fragment to this text fragment.*//*! \fn bool QTextFragment::isValid() const Returns true if this is a valid text fragment (i.e. has a valid position in a document); otherwise returns false.*//*! \fn bool QTextFragment::operator==(const QTextFragment &other) const Returns true if this text fragment is the same (at the same position) as the \a other text fragment; otherwise returns false.*//*! \fn bool QTextFragment::operator!=(const QTextFragment &other) const Returns true if this text fragment is different (at a different position) from the \a other text fragment; otherwise returns false.*//*! \fn bool QTextFragment::operator<(const QTextFragment &other) const Returns true if this text fragment appears earlier in the document than the \a other text fragment; otherwise returns false.*//*! Returns the position of this text fragment in the document.*/int QTextFragment::position() const{ if (!p || !n) return 0; // ### -1 instead? return p->fragmentMap().position(n);}/*! Returns the number of characters in the text fragment. \sa text()*/int QTextFragment::length() const{ if (!p || !n) return 0; int len = 0; int f = n; while (f != ne) { len += p->fragmentMap().size(f); f = p->fragmentMap().next(f); } return len;}/*! Returns true if the text fragment contains the text at the given \a position in the document; otherwise returns false.*/bool QTextFragment::contains(int position) const{ if (!p || !n) return false; int pos = this->position(); return position >= pos && position < pos + length();}/*! Returns the text fragment's character format. \sa text()*/QTextCharFormat QTextFragment::charFormat() const{ if (!p || !n) return QTextCharFormat(); const QTextFragmentData *data = p->fragmentMap().fragment(n); return p->formatCollection()->charFormat(data->format);}/*! Returns an index into the document's internal list of character formats for the text fragment's character format. \sa QTextDocument::object()*/int QTextFragment::charFormatIndex() const{ if (!p || !n) return -1; const QTextFragmentData *data = p->fragmentMap().fragment(n); return data->format;}/*! Returns the text fragment's as plain text. \sa length(), charFormat()*/QString QTextFragment::text() const{ if (!p || !n) return QString(); QString result; QString buffer = p->buffer(); int f = n; while (f != ne) { const QTextFragmentData * const frag = p->fragmentMap().fragment(f); result += QString(buffer.constData() + frag->stringPosition, frag->size); f = p->fragmentMap().next(f); } return result;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -