|
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectcom.trolltech.qt.QSignalEmitter
com.trolltech.qt.QtJambiObject
com.trolltech.qt.gui.QTextLayout
public class QTextLayout
The QTextLayout class is used to lay out and paint a single paragraph of text.
It offers most features expected from a modern text layout engine, including Unicode compliant rendering, line breaking and handling of cursor positioning. It can also produce and render device independent layout, something that is important for WYSIWYG applications.
The class has a rather low level API and unless you intend to implement your own text rendering for some specialized widget, you probably won't need to use it directly.
QTextLayout can currently deal with plain text and rich text paragraphs that are part of a QTextDocument.
QTextLayout can be used to create a sequence of QTextLine's with given widths and can position them independently on the screen. Once the layout is done, these lines can be drawn on a paint device.
Here's some pseudo code that presents the layout phase:
int leading = fontMetrics.leading(); int height = 0; qreal widthUsed = 0; textLayout.beginLayout(); while (1) { QTextLine line = textLayout.createLine(); if (!line.isValid()) break; line.setLineWidth(lineWidth); height += leading; line.setPosition(QPoint(0, height)); height += line.height(); widthUsed = qMax(widthUsed, line.naturalTextWidth()); } textLayout.endLayout();
The text can be drawn by calling the layout's draw function:
QPainter painter(this); textLayout.draw(&painter, QPoint(0, 0));
The text layout's text is set in the constructor or with setText. The layout can be seen as a sequence of QTextLine objects; use lineAt or lineForTextPosition to get a QTextLine, createLine to create one. For a given position in the text you can find a valid cursor position with isValidCursorPosition, nextCursorPosition, and previousCursorPosition. The layout itself can be positioned with setPosition; it has a boundingRect, and a minimumWidth and a maximumWidth. A text layout can be drawn on a painter device using draw.
Nested Class Summary | |
---|---|
static class |
QTextLayout.CursorMode
Press link for info on QTextLayout.CursorMode |
Nested classes/interfaces inherited from class com.trolltech.qt.QSignalEmitter |
---|
QSignalEmitter.AbstractSignal, QSignalEmitter.Signal0, QSignalEmitter.Signal1<A>, QSignalEmitter.Signal2<A,B>, QSignalEmitter.Signal3<A,B,C>, QSignalEmitter.Signal4<A,B,C,D>, QSignalEmitter.Signal5<A,B,C,D,E>, QSignalEmitter.Signal6<A,B,C,D,E,F>, QSignalEmitter.Signal7<A,B,C,D,E,F,G>, QSignalEmitter.Signal8<A,B,C,D,E,F,G,H>, QSignalEmitter.Signal9<A,B,C,D,E,F,G,H,I> |
Constructor Summary | |
---|---|
QTextLayout()
Constructs an empty text layout. |
|
QTextLayout(QTextBlock b)
|
|
QTextLayout(java.lang.String text)
Constructs a text layout to lay out the given text. |
|
QTextLayout(java.lang.String text,
QFont font)
Equivalent to QTextLayout(text, font, 0). |
|
QTextLayout(java.lang.String text,
QFont font,
QPaintDeviceInterface paintdevice)
Constructs a text layout to lay out the given text with the specified font. |
Method Summary | |
---|---|
java.util.List<QTextLayout_FormatRange> |
additionalFormats()
Returns the list of additional formats supported by the text layout. |
void |
beginLayout()
Begins the layout process. |
QRectF |
boundingRect()
The smallest rectangle that contains all the lines in the layout. |
boolean |
cacheEnabled()
Returns true if the complete layout information is cached; otherwise returns false. |
void |
clearAdditionalFormats()
Clears the list of additional formats supported by the text layout. |
QTextLine |
createLine()
Returns a new text line to be laid out if there is text to be inserted into the layout; otherwise returns an invalid text line. |
void |
draw(QPainter p,
QPointF pos,
java.util.List<QTextLayout_FormatRange> selections)
Draws the whole layout on the painter p at the position specified by pos. |
void |
draw(QPainter p,
QPointF pos,
java.util.List<QTextLayout_FormatRange> selections,
QRectF clip)
Draws the whole layout on the painter p at the position specified by pos. |
void |
drawCursor(QPainter p,
QPointF pos,
int cursorPosition)
Draws a text cursor with the current pen at the given pos using the p specified. |
void |
drawCursor(QPainter p,
QPointF pos,
int cursorPosition,
int width)
Draws a text cursor with the current pen and the specified width at the given pos using the p specified. |
void |
endLayout()
Ends the layout process. |
QFont |
font()
Returns the current font that is used for the layout, or a default font if none is set. |
static QTextLayout |
fromNativePointer(QNativePointer nativePointer)
This function returns the QTextLayout instance pointed to by nativePointer |
boolean |
isValidCursorPosition(int pos)
Returns true if position pos is a valid cursor position. |
QTextLine |
lineAt(int i)
Returns the i-th line of text in this text layout. |
int |
lineCount()
Returns the number of lines in this text layout. |
QTextLine |
lineForTextPosition(int pos)
Returns the line that contains the cursor position specified by pos. |
double |
maximumWidth()
The maximum width the layout could expand to; this is essentially the width of the entire text. |
double |
minimumWidth()
The minimum width the layout needs. |
int |
nextCursorPosition(int oldPos)
Equivalent to nextCursorPosition(oldPos, SkipCharacters). |
int |
nextCursorPosition(int oldPos,
QTextLayout.CursorMode mode)
Returns the next valid cursor position after oldPos that respects the given cursor mode. |
QPointF |
position()
The global position of the layout. |
int |
preeditAreaPosition()
Returns the position of the area in the text layout that will be processed before editing occurs. |
java.lang.String |
preeditAreaText()
Returns the text that is inserted in the layout before editing occurs. |
int |
previousCursorPosition(int oldPos)
Equivalent to previousCursorPosition(oldPos, SkipCharacters). |
int |
previousCursorPosition(int oldPos,
QTextLayout.CursorMode mode)
Returns the first valid cursor position before oldPos that respects the given cursor mode. |
void |
setAdditionalFormats(java.util.List<QTextLayout_FormatRange> overrides)
Sets the additional formats supported by the text layout to overrides. |
void |
setCacheEnabled(boolean enable)
Enables caching of the complete layout information if enable is true; otherwise disables layout caching. |
void |
setFont(QFont f)
Sets the layout's font to the given f. |
void |
setPosition(QPointF p)
Moves the text layout to point p. |
void |
setPreeditArea(int position,
java.lang.String text)
Sets the position and text of the area in the layout that is processed before editing occurs. |
void |
setText(java.lang.String string)
Sets the layout's text to the given string. |
void |
setTextOption(QTextOption option)
Sets the text option structure that controls the layout process to the given option. |
java.lang.String |
text()
Returns the layout's text. |
QTextOption |
textOption()
Returns the current text option used to control the layout process. |
Methods inherited from class com.trolltech.qt.QtJambiObject |
---|
dispose, disposed, finalize, reassignNativeResources, tr, tr, tr |
Methods inherited from class com.trolltech.qt.QSignalEmitter |
---|
blockSignals, disconnect, disconnect, signalsBlocked, signalSender, thread |
Methods inherited from class java.lang.Object |
---|
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Methods inherited from interface com.trolltech.qt.QtJambiInterface |
---|
disableGarbageCollection, nativeId, nativePointer, reenableGarbageCollection, setJavaOwnership |
Constructor Detail |
---|
public QTextLayout()
Constructs an empty text layout.
public QTextLayout(java.lang.String text, QFont font)
Equivalent to QTextLayout(text, font, 0).
public QTextLayout(java.lang.String text, QFont font, QPaintDeviceInterface paintdevice)
Constructs a text layout to lay out the given text with the specified font.
All the metric and layout calculations will be done in terms of the paint device, paintdevice. If paintdevice is 0 the calculations will be done in screen metrics.
public QTextLayout(java.lang.String text)
Constructs a text layout to lay out the given text.
public QTextLayout(QTextBlock b)
Method Detail |
---|
public final java.util.List<QTextLayout_FormatRange> additionalFormats()
Returns the list of additional formats supported by the text layout.
public final void beginLayout()
Begins the layout process.
public final QRectF boundingRect()
The smallest rectangle that contains all the lines in the layout.
public final boolean cacheEnabled()
Returns true if the complete layout information is cached; otherwise returns false.
public final void clearAdditionalFormats()
Clears the list of additional formats supported by the text layout.
public final QTextLine createLine()
Returns a new text line to be laid out if there is text to be inserted into the layout; otherwise returns an invalid text line.
The text layout creates a new line object that starts after the last line in the layout, or at the beginning if the layout is empty. The layout maintains an internal cursor, and each line is filled with text from the cursor position onwards when the QTextLine::setLineWidth() function is called.
Once QTextLine::setLineWidth() is called, a new line can be created and filled with text. Repeating this process will lay out the whole block of text contained in the QTextLayout. If there is no text left to be inserted into the layout, the QTextLine returned will not be valid (isValid() will return false).
public final void draw(QPainter p, QPointF pos, java.util.List<QTextLayout_FormatRange> selections)
public final void draw(QPainter p, QPointF pos, java.util.List<QTextLayout_FormatRange> selections, QRectF clip)
Draws the whole layout on the painter p at the position specified by pos. The rendered layout includes the given selections and is clipped within the rectangle specified by clip.
public final void drawCursor(QPainter p, QPointF pos, int cursorPosition, int width)
Draws a text cursor with the current pen and the specified width at the given pos using the p specified. The corresponding position within the text is specified by cursorPosition.
public final void drawCursor(QPainter p, QPointF pos, int cursorPosition)
Draws a text cursor with the current pen at the given pos using the p specified. The corresponding position within the text is specified by cursorPosition.
public final void endLayout()
Ends the layout process.
public final QFont font()
Returns the current font that is used for the layout, or a default font if none is set.
public final boolean isValidCursorPosition(int pos)
Returns true if position pos is a valid cursor position.
In a Unicode context some positions in the text are not valid cursor positions, because the position is inside a Unicode surrogate or a grapheme cluster.
A grapheme cluster is a sequence of two or more Unicode characters that form one indivisible entity on the screen. For example the latin character `Ä' can be represented in Unicode by two characters, `A' (0x41), and the combining diaresis (0x308). A text cursor can only validly be positioned before or after these two characters, never between them since that wouldn't make sense. In indic languages every syllable forms a grapheme cluster.
public final QTextLine lineAt(int i)
Returns the i-th line of text in this text layout.
public final int lineCount()
Returns the number of lines in this text layout.
public final QTextLine lineForTextPosition(int pos)
Returns the line that contains the cursor position specified by pos.
public final double maximumWidth()
The maximum width the layout could expand to; this is essentially the width of the entire text.
Warning: This function only returns a valid value after the layout has been done.
public final double minimumWidth()
The minimum width the layout needs. This is the width of the layout's smallest non-breakable substring.
Warning: This function only returns a valid value after the layout has been done.
public final int nextCursorPosition(int oldPos)
Equivalent to nextCursorPosition(oldPos, SkipCharacters).
public final int nextCursorPosition(int oldPos, QTextLayout.CursorMode mode)
Returns the next valid cursor position after oldPos that respects the given cursor mode.
public final QPointF position()
The global position of the layout. This is independent of the bounding rectangle and of the layout process.
public final int preeditAreaPosition()
Returns the position of the area in the text layout that will be processed before editing occurs.
public final java.lang.String preeditAreaText()
Returns the text that is inserted in the layout before editing occurs.
public final int previousCursorPosition(int oldPos)
Equivalent to previousCursorPosition(oldPos, SkipCharacters).
public final int previousCursorPosition(int oldPos, QTextLayout.CursorMode mode)
Returns the first valid cursor position before oldPos that respects the given cursor mode.
public final void setAdditionalFormats(java.util.List<QTextLayout_FormatRange> overrides)
Sets the additional formats supported by the text layout to overrides.
public final void setCacheEnabled(boolean enable)
Enables caching of the complete layout information if enable is true; otherwise disables layout caching. Usually QTextLayout throws most of the layouting information away after a call to endLayout to reduce memory consumption. If you however want to draw the laid out text directly afterwards enabling caching might speed up drawing significantly.
public final void setFont(QFont f)
Sets the layout's font to the given f. The layout is invalidated and must be laid out again.
public final void setPosition(QPointF p)
Moves the text layout to point p.
public final void setPreeditArea(int position, java.lang.String text)
Sets the position and text of the area in the layout that is processed before editing occurs.
public final void setText(java.lang.String string)
Sets the layout's text to the given string. The layout is invalidated and must be laid out again.
public final void setTextOption(QTextOption option)
Sets the text option structure that controls the layout process to the given option.
public final java.lang.String text()
Returns the layout's text.
public final QTextOption textOption()
Returns the current text option used to control the layout process.
public static QTextLayout fromNativePointer(QNativePointer nativePointer)
nativePointer
- the QNativePointer of which object should be returned.
|
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |