Qt 4.8
Public Functions | Properties | List of all members
QTextFragment Class Reference

The QTextFragment class holds a piece of text in a QTextDocument with a single QTextCharFormat. More...

#include <qtextobject.h>

Public Functions

QTextCharFormat charFormat () const
 Returns the text fragment's character format. More...
 
int charFormatIndex () const
 Returns an index into the document's internal list of character formats for the text fragment's character format. More...
 
bool contains (int position) const
 Returns true if the text fragment contains the text at the given position in the document; otherwise returns false. More...
 
QList< QGlyphRunglyphRuns () const
 Returns the glyphs of this text fragment. More...
 
bool isValid () const
 Returns true if this is a valid text fragment (i. More...
 
int length () const
 Returns the number of characters in the text fragment. More...
 
bool operator!= (const QTextFragment &o) const
 Returns true if this text fragment is different (at a different position) from the other text fragment; otherwise returns false. More...
 
bool operator< (const QTextFragment &o) const
 Returns true if this text fragment appears earlier in the document than the other text fragment; otherwise returns false. More...
 
QTextFragmentoperator= (const QTextFragment &o)
 Assigns the content (text and format) of the other text fragment to this text fragment. More...
 
bool operator== (const QTextFragment &o) const
 Returns true if this text fragment is the same (at the same position) as the other text fragment; otherwise returns false. More...
 
int position () const
 Returns the position of this text fragment in the document. More...
 
 QTextFragment (const QTextDocumentPrivate *priv, int f, int fe)
 
 QTextFragment ()
 Creates a new empty text fragment. More...
 
 QTextFragment (const QTextFragment &o)
 Copies the content (text and format) of the other text fragment to this text fragment. More...
 
QString text () const
 Returns the text fragment's as plain text. More...
 

Properties

int n
 
int ne
 
const QTextDocumentPrivatep
 

Detailed Description

The QTextFragment class holds a piece of text in a QTextDocument with a single QTextCharFormat.

Note
This class or function is reentrant.

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.

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.

See also
QTextDocument, {Rich Text Document Structure}

Definition at line 297 of file qtextobject.h.

Constructors and Destructors

◆ QTextFragment() [1/3]

QTextFragment::QTextFragment ( const QTextDocumentPrivate priv,
int  f,
int  fe 
)
inline
Warning
This function is not part of the public interface.

Definition at line 300 of file qtextobject.h.

300 : p(priv), n(f), ne(fe) {}
const QTextDocumentPrivate * p
Definition: qtextobject.h:324

◆ QTextFragment() [2/3]

QTextFragment::QTextFragment ( )
inline

Creates a new empty text fragment.

Definition at line 301 of file qtextobject.h.

301 : p(0), n(0), ne(0) {}
const QTextDocumentPrivate * p
Definition: qtextobject.h:324

◆ QTextFragment() [3/3]

QTextFragment::QTextFragment ( const QTextFragment o)
inline

Copies the content (text and format) of the other text fragment to this text fragment.

Definition at line 302 of file qtextobject.h.

302 : p(o.p), n(o.n), ne(o.ne) {}
const QTextDocumentPrivate * p
Definition: qtextobject.h:324

Functions

◆ charFormat()

QTextCharFormat QTextFragment::charFormat ( ) const

Returns the text fragment's character format.

See also
text()

Definition at line 1884 of file qtextobject.cpp.

Referenced by QTextControlPrivate::activateLinkUnderCursor(), QTextControl::anchorPosition(), QTextHtmlExporter::emitFragment(), QTextControl::findNextPrevAnchor(), and QTextOdfWriter::writeInlineCharacter().

1885 {
1886  if (!p || !n)
1887  return QTextCharFormat();
1889  return p->formatCollection()->charFormat(data->format);
1890 }
QTextCharFormat charFormat(int index) const
Definition: qtextformat_p.h:85
The QTextCharFormat class provides formatting information for characters in a QTextDocument.
Definition: qtextformat.h:372
QTextFormatCollection * formatCollection()
static const char * data(const QByteArray &arr)
Fragment * fragment(uint index)
const QTextDocumentPrivate * p
Definition: qtextobject.h:324
const FragmentMap & fragmentMap() const

◆ charFormatIndex()

int QTextFragment::charFormatIndex ( ) const

Returns an index into the document's internal list of character formats for the text fragment's character format.

See also
QTextDocument::allFormats()

Definition at line 1898 of file qtextobject.cpp.

1899 {
1900  if (!p || !n)
1901  return -1;
1903  return data->format;
1904 }
static const char * data(const QByteArray &arr)
Fragment * fragment(uint index)
const QTextDocumentPrivate * p
Definition: qtextobject.h:324
const FragmentMap & fragmentMap() const

◆ contains()

bool QTextFragment::contains ( int  position) const

Returns true if the text fragment contains the text at the given position in the document; otherwise returns false.

Definition at line 1871 of file qtextobject.cpp.

1872 {
1873  if (!p || !n)
1874  return false;
1875  int pos = this->position();
1876  return position >= pos && position < pos + length();
1877 }
int length() const
Returns the number of characters in the text fragment.
int position() const
Returns the position of this text fragment in the document.
const QTextDocumentPrivate * p
Definition: qtextobject.h:324

◆ glyphRuns()

QList< QGlyphRun > QTextFragment::glyphRuns ( ) const

Returns the glyphs of this text fragment.

The positions of the glyphs are relative to the position of the QTextBlock's layout.

See also
QGlyphRun, QTextBlock::layout(), QTextLayout::position(), QPainter::drawGlyphRun()

Definition at line 1812 of file qtextobject.cpp.

1813 {
1814  if (!p || !n)
1815  return QList<QGlyphRun>();
1816 
1817  int pos = position();
1818  int len = length();
1819  if (len == 0)
1820  return QList<QGlyphRun>();
1821 
1822  int blockNode = p->blockMap().findNode(pos);
1823 
1824  const QTextBlockData *blockData = p->blockMap().fragment(blockNode);
1825  QTextLayout *layout = blockData->layout;
1826 
1827  QList<QGlyphRun> ret;
1828  for (int i=0; i<layout->lineCount(); ++i) {
1829  QTextLine textLine = layout->lineAt(i);
1830  ret += textLine.glyphs(pos, len);
1831  }
1832 
1833  return ret;
1834 }
QList< QGlyphRun > glyphs(int from, int length) const
Returns the glyph indexes and positions for all glyphs in this QTextLine which reside in QScriptItems...
The QTextLine class represents a line of text inside a QTextLayout.
Definition: qtextlayout.h:197
const char * layout
QTextLayout * layout
const BlockMap & blockMap() const
int length() const
Returns the number of characters in the text fragment.
QTextLine lineAt(int i) const
Returns the {i}-th line of text in this text layout.
Fragment * fragment(uint index)
int position() const
Returns the position of this text fragment in the document.
const QTextDocumentPrivate * p
Definition: qtextobject.h:324
The QTextLayout class is used to lay out and render text.
Definition: qtextlayout.h:105
uint findNode(int k, uint field=0) const
int lineCount() const
Returns the number of lines in this text layout.
The QList class is a template class that provides lists.
Definition: qdatastream.h:62

◆ isValid()

bool QTextFragment::isValid ( ) const
inline

Returns true if this is a valid text fragment (i.

e. has a valid position in a document); otherwise returns false.

Definition at line 305 of file qtextobject.h.

305 { return p && n; }
const QTextDocumentPrivate * p
Definition: qtextobject.h:324

◆ length()

int QTextFragment::length ( ) const

Returns the number of characters in the text fragment.

See also
text()

Definition at line 1853 of file qtextobject.cpp.

Referenced by QTextControlPrivate::activateLinkUnderCursor(), and QTextControl::findNextPrevAnchor().

1854 {
1855  if (!p || !n)
1856  return 0;
1857 
1858  int len = 0;
1859  int f = n;
1860  while (f != ne) {
1861  len += p->fragmentMap().size(f);
1862  f = p->fragmentMap().next(f);
1863  }
1864  return len;
1865 }
uint next(uint n) const
uint size(uint node, uint field=0) const
const QTextDocumentPrivate * p
Definition: qtextobject.h:324
const FragmentMap & fragmentMap() const

◆ operator!=()

bool QTextFragment::operator!= ( const QTextFragment o) const
inline

Returns true if this text fragment is different (at a different position) from the other text fragment; otherwise returns false.

Definition at line 308 of file qtextobject.h.

308 { return p != o.p || n != o.n; }
const QTextDocumentPrivate * p
Definition: qtextobject.h:324

◆ operator<()

bool QTextFragment::operator< ( const QTextFragment o) const
inline

Returns true if this text fragment appears earlier in the document than the other text fragment; otherwise returns false.

Definition at line 309 of file qtextobject.h.

309 { return position() < o.position(); }
int position() const
Returns the position of this text fragment in the document.

◆ operator=()

QTextFragment & QTextFragment::operator= ( const QTextFragment o)
inline

Assigns the content (text and format) of the other text fragment to this text fragment.

Definition at line 303 of file qtextobject.h.

303 { p = o.p; n = o.n; ne = o.ne; return *this; }
const QTextDocumentPrivate * p
Definition: qtextobject.h:324

◆ operator==()

bool QTextFragment::operator== ( const QTextFragment o) const
inline

Returns true if this text fragment is the same (at the same position) as the other text fragment; otherwise returns false.

Definition at line 307 of file qtextobject.h.

307 { return p == o.p && n == o.n; }
const QTextDocumentPrivate * p
Definition: qtextobject.h:324

◆ position()

int QTextFragment::position ( ) const

Returns the position of this text fragment in the document.

Definition at line 1840 of file qtextobject.cpp.

Referenced by QTextControlPrivate::activateLinkUnderCursor(), QTextControl::anchorPosition(), QTextControl::findNextPrevAnchor(), and operator<().

1841 {
1842  if (!p || !n)
1843  return 0; // ### -1 instead?
1844 
1845  return p->fragmentMap().position(n);
1846 }
uint position(uint node, uint field=0) const
const QTextDocumentPrivate * p
Definition: qtextobject.h:324
const FragmentMap & fragmentMap() const

◆ text()

QString QTextFragment::text ( ) const

Returns the text fragment's as plain text.

See also
length(), charFormat()

Definition at line 1911 of file qtextobject.cpp.

Referenced by QTextHtmlExporter::emitFragment().

1912 {
1913  if (!p || !n)
1914  return QString();
1915 
1916  QString result;
1917  QString buffer = p->buffer();
1918  int f = n;
1919  while (f != ne) {
1920  const QTextFragmentData * const frag = p->fragmentMap().fragment(f);
1921  result += QString(buffer.constData() + frag->stringPosition, frag->size_array[0]);
1922  f = p->fragmentMap().next(f);
1923  }
1924  return result;
1925 }
The QString class provides a Unicode character string.
Definition: qstring.h:83
uint next(uint n) const
Fragment * fragment(uint index)
const QTextDocumentPrivate * p
Definition: qtextobject.h:324
QString buffer() const
quint32 size_array[N]
const FragmentMap & fragmentMap() const
const QChar * constData() const
Returns a pointer to the data stored in the QString.
Definition: qstring.h:712

Properties

◆ n

int QTextFragment::n
private

Definition at line 325 of file qtextobject.h.

Referenced by operator!=(), operator=(), and operator==().

◆ ne

int QTextFragment::ne
private

Definition at line 326 of file qtextobject.h.

Referenced by operator=().

◆ p

const QTextDocumentPrivate* QTextFragment::p
private

Definition at line 324 of file qtextobject.h.

Referenced by operator!=(), operator=(), and operator==().


The documentation for this class was generated from the following files: