Introduce QTextBlock::textFormats()

A convenient way to obtain the block format ranges that should be
applied to the block text.

Change-Id: I220429b7c9c592c4880357c0d7b1b21f6c1c11f3
Reviewed-by: Lars Knoll <lars.knoll@digia.com>
This commit is contained in:
Konstantin Ritt 2014-01-30 01:59:00 +02:00 committed by The Qt Project
parent d8225fab8f
commit 2e1609f3e3
2 changed files with 53 additions and 1 deletions

View File

@ -1233,6 +1233,56 @@ QString QTextBlock::text() const
return text;
}
/*!
\since 5.3
Returns the block's text format options as a list of continuous ranges
of QTextCharFormat. The range's character format is used when inserting text
within the range boundaries.
\sa charFormat(), blockFormat()
*/
QList<QTextLayout::FormatRange> QTextBlock::textFormats() const
{
QList<QTextLayout::FormatRange> formats;
if (!p || !n)
return formats;
const QTextFormatCollection *formatCollection = p->formatCollection();
int start = 0;
int cur = start;
int format = -1;
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();
if (format != it.value()->format) {
if (cur - start > 0) {
QTextLayout::FormatRange range;
range.start = start;
range.length = cur - start;
range.format = formatCollection->charFormat(format);
formats.append(range);
}
format = frag->format;
start = cur;
}
cur += frag->size_array[0];
}
if (cur - start > 0) {
QTextLayout::FormatRange range;
range.start = start;
range.length = cur - start;
range.format = formatCollection->charFormat(format);
formats.append(range);
}
return formats;
}
/*!
Returns the text document this text block belongs to, or 0 if the

View File

@ -44,6 +44,7 @@
#include <QtCore/qobject.h>
#include <QtGui/qtextformat.h>
#include <QtGui/qtextlayout.h>
#include <QtGui/qglyphrun.h>
QT_BEGIN_NAMESPACE
@ -55,7 +56,6 @@ class QTextDocumentPrivate;
class QTextCursor;
class QTextBlock;
class QTextFragment;
class QTextLayout;
class QTextList;
class Q_GUI_EXPORT QTextObject : public QObject
@ -223,6 +223,8 @@ public:
QString text() const;
QList<QTextLayout::FormatRange> textFormats() const;
const QTextDocument *document() const;
QTextList *textList() const;