|
|||||||||
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.QTextCursor
public class QTextCursor
The QTextCursor
class offers an API to access and modify QTextDocuments
. Text cursors are objects that are used to access and modify the contents and underlying structure of text documents via a programming interface that mimics the behavior of a cursor in a text editor. QTextCursor
contains information about both the cursor's position within a QTextDocument
and any selection that it has made.
QTextCursor
is modeled on the way a text cursor behaves in a text editor, providing a programmatic means of performing standard actions through the user interface. A document can be thought of as a single string of characters with the cursor's position()
being between any two characters (or at the very beginning or very end of the document). Documents can also contain tables, lists, images, and other objects in addition to text but, from the developer's point of view, the document can be treated as one long string. Some portions of that string can be considered to lie within particular blocks (e.g. paragraphs), or within a table's cell, or a list's item, or other structural elements. When we refer to "current character" we mean the character immediately after the cursor position()
in the document; similarly the "current block" is the block that contains the cursor position()
.
A QTextCursor
also has an anchor()
position. The text that is between the anchor()
and the position()
is the selection. If anchor()
== position()
there is no selection.
The cursor position can be changed programmatically using setPosition()
and movePosition()
; the latter can also be used to select text. For selections see selectionStart()
, selectionEnd()
, hasSelection()
, clearSelection()
, and removeSelectedText()
.
If the position()
is at the start of a block atBlockStart()
returns true; and if it is at the end of a block atBlockEnd()
returns true. The format of the current character is returned by charFormat()
, and the format of the current block is returned by blockFormat()
.
Formatting can be applied to the current text document using the setCharFormat()
, mergeCharFormat()
, setBlockFormat()
and mergeBlockFormat()
functions. The 'set' functions will replace the cursor's current character or block format, while the 'merge' functions add the given format properties to the cursor's current format. If the cursor has a selection the given format is applied to the current selection. Note that when only parts of a block is selected the block format is applied to the entire block. The text at the current character position can be turned into a list using createList()
.
Deletions can be achieved using deleteChar()
, deletePreviousChar()
, and removeSelectedText()
.
Text strings can be inserted into the document with the insertText()
function, blocks (representing new paragraphs) can be inserted with insertBlock()
.
Existing fragments of text can be inserted with insertFragment()
but, if you want to insert pieces of text in various formats, it is usually still easier to use insertText()
and supply a character format.
Various types of higher-level structure can also be inserted into the document with the cursor:
insertList()
.insertTable()
function, and can be given an optional format. These contain an array of cells that can be traversed using the cursor.insertImage()
. The image to be used can be specified in an image format, or by name.insertFrame()
with a specified format.beginEditBlock()
and endEditBlock()
. Cursor movements are limited to valid cursor positions. In Latin writing this is usually after every character in the text. In some other writing systems cursor movements are limited to "clusters" (e.g. a syllable in Devanagari, or a base letter plus diacritics). Functions such as movePosition()
and deleteChar()
limit cursor movement to these valid positions.
Nested Class Summary | |
---|---|
static class |
QTextCursor.MoveMode
If the anchor() is kept where it is and the position() is moved, the text in between will be selected. |
static class |
QTextCursor.MoveOperation
See also. movePosition() . |
static class |
QTextCursor.SelectionType
This enum describes the types of selection that can be applied with the select() function. |
Nested classes/interfaces inherited from class com.trolltech.qt.QSignalEmitter |
---|
QSignalEmitter.Signal0, QSignalEmitter.Signal1, QSignalEmitter.Signal2, QSignalEmitter.Signal3, QSignalEmitter.Signal4, QSignalEmitter.Signal5, QSignalEmitter.Signal6, QSignalEmitter.Signal7, QSignalEmitter.Signal8, QSignalEmitter.Signal9 |
Constructor Summary | |
---|---|
QTextCursor()
Constructs a null cursor. |
|
QTextCursor(QTextBlock block)
Constructs a cursor pointing to the beginning of the block. |
|
QTextCursor(QTextCursor cursor)
Constructs a new cursor that is a copy of cursor. |
|
QTextCursor(QTextDocument document)
Constructs a cursor pointing to the beginning of the document. |
|
QTextCursor(QTextFrame frame)
Constructs a cursor pointing to the beginning of the frame. |
Method Summary | |
---|---|
int |
anchor()
Returns the anchor position; this is the same as position() unless there is a selection in which case position() marks one end of the selection and anchor() marks the other end. |
boolean |
atBlockEnd()
Returns true if the cursor is at the end of a block; otherwise returns false. |
boolean |
atBlockStart()
Returns true if the cursor is at the start of a block; otherwise returns false. |
boolean |
atEnd()
Returns true if the cursor is at the end of the document; otherwise returns false. |
boolean |
atStart()
Returns true if the cursor is at the start of the document; otherwise returns false. |
void |
beginEditBlock()
Indicates the start of a block of editing operations on the document that should appear as a single operation from an undo/redo point of view. |
QTextBlock |
block()
Returns the block that contains the cursor. |
QTextCharFormat |
blockCharFormat()
Returns the block character format of the block the cursor is in. |
QTextBlockFormat |
blockFormat()
Returns the block format of the block the cursor is in. |
int |
blockNumber()
Returns the number of the block the cursor is in, or 0 if the cursor is invalid. |
QTextCharFormat |
charFormat()
Returns the format of the character immediately before the cursor position() . |
void |
clearSelection()
Clears the current selection by setting the anchor to the cursor position. |
QTextCursor |
clone()
This method is reimplemented for internal reasons |
int |
columnNumber()
Returns the position of the cursor within its containing line. |
QTextList |
createList(QTextListFormat.Style style)
Creates and returns a new list with the given style, making the cursor's current paragraph the first list item. |
QTextList |
createList(QTextListFormat format)
Creates and returns a new list with the given format, and makes the current paragraph the cursor is in the first list item. |
QTextFrame |
currentFrame()
Returns a pointer to the current frame. |
QTextList |
currentList()
Returns the current list if the cursor position() is inside a block that is part of a list; otherwise returns 0. |
QTextTable |
currentTable()
Returns a pointer to the current table if the cursor position() is inside a block that is part of a table; otherwise returns 0. |
void |
deleteChar()
If there is no selected text, deletes the character at the current cursor position; otherwise deletes the selected text. |
void |
deletePreviousChar()
If there is no selected text, deletes the character before the current cursor position; otherwise deletes the selected text. |
void |
endEditBlock()
Indicates the end of a block of editing operations on the document that should appear as a single operation from an undo/redo point of view. |
static QTextCursor |
fromNativePointer(QNativePointer nativePointer)
|
boolean |
hasComplexSelection()
Returns true if the cursor contains a selection that is not simply a range from selectionStart() to selectionEnd() ; otherwise returns false. |
boolean |
hasSelection()
Returns true if the cursor contains a selection; otherwise returns false. |
void |
insertBlock()
Inserts a new empty block at the cursor position() with the current blockFormat() and charFormat() . |
void |
insertBlock(QTextBlockFormat format)
Inserts a new empty block at the cursor position() with block format format and the current charFormat() as block char format. |
void |
insertBlock(QTextBlockFormat format,
QTextCharFormat charFormat)
Inserts a new empty block at the cursor position() with block format format and charFormat as block char format. |
void |
insertFragment(QTextDocumentFragment fragment)
Inserts the text fragment at the current position() . |
QTextFrame |
insertFrame(QTextFrameFormat format)
Inserts a frame with the given format at the current cursor position() , moves the cursor position() inside the frame, and returns the frame. |
void |
insertHtml(java.lang.String html)
Inserts the text html at the current position() . |
void |
insertImage(QTextImageFormat format)
Inserts the image defined by format at the current position() . |
void |
insertImage(QTextImageFormat format,
QTextFrameFormat.Position alignment)
Inserts the image defined by the given format at the cursor's current position with the specified alignment. |
void |
insertImage(java.lang.String name)
Convenience method for inserting the image with the given name at the current position() . |
QTextList |
insertList(QTextListFormat.Style style)
Inserts a new block at the current position and makes it the first list item of a newly created list with the given style. |
QTextList |
insertList(QTextListFormat format)
Inserts a new block at the current position and makes it the first list item of a newly created list with the given format. |
QTextTable |
insertTable(int rows,
int cols)
Creates a new table with the given number of rows and columns, inserts it at the current cursor position() in the document, and returns the table object. |
QTextTable |
insertTable(int rows,
int cols,
QTextTableFormat format)
Creates a new table with the given number of rows and columns in the specified format, inserts it at the current cursor position() in the document, and returns the table object. |
void |
insertText(java.lang.String text)
Inserts text at the current position, using the current character format. |
void |
insertText(java.lang.String text,
QTextCharFormat format)
Inserts text at the current position with the given format. |
boolean |
isCopyOf(QTextCursor other)
Returns true if this cursor and other are copies of each other, i.e. |
boolean |
isNull()
Returns true if the cursor is null; otherwise returns false. |
void |
joinPreviousEditBlock()
Like beginEditBlock() indicates the start of a block of editing operations that should appear as a single operation for undo/redo. |
void |
mergeBlockCharFormat(QTextCharFormat modifier)
Modifies the block char format of the current block (or all blocks that are contained in the selection) with the block format specified by modifier. |
void |
mergeBlockFormat(QTextBlockFormat modifier)
Modifies the block format of the current block (or all blocks that are contained in the selection) with the block format specified by modifier. |
void |
mergeCharFormat(QTextCharFormat modifier)
Merges the cursor's current character format with the properties described by format modifier. |
boolean |
movePosition(QTextCursor.MoveOperation op)
Moves the cursor by performing the given operationn times, using the specified mode, and returns true if all operations were completed successfully; otherwise returns false. |
boolean |
movePosition(QTextCursor.MoveOperation op,
QTextCursor.MoveMode arg__2)
Moves the cursor by performing the given operationn times, using the specified mode, and returns true if all operations were completed successfully; otherwise returns false. |
boolean |
movePosition(QTextCursor.MoveOperation op,
QTextCursor.MoveMode arg__2,
int n)
Moves the cursor by performing the given operationn times, using the specified mode, and returns true if all operations were completed successfully; otherwise returns false. |
static QNativePointer |
nativePointerArray(QTextCursor[] array)
|
int |
position()
Returns the absolute position of the cursor within the document. |
void |
removeSelectedText()
If there is a selection, its content is deleted; otherwise does nothing. |
void |
select(QTextCursor.SelectionType selection)
Selects text in the document according to the given selection. |
QTableArea |
selectedTableCells()
Returns the selected table cells as a QTableArea. |
java.lang.String |
selectedText()
Returns the current selection's text (which may be empty). |
QTextDocumentFragment |
selection()
Returns the current selection (which may be empty) with all its formatting information. |
int |
selectionEnd()
Returns the end of the selection or position() if the cursor doesn't have a selection. |
int |
selectionStart()
Returns the start of the selection or position() if the cursor doesn't have a selection. |
void |
setBlockCharFormat(QTextCharFormat format)
Sets the block char format of the current block (or all blocks that are contained in the selection) to format. |
void |
setBlockFormat(QTextBlockFormat format)
Sets the block format of the current block (or all blocks that are contained in the selection) to format. |
void |
setCharFormat(QTextCharFormat format)
Sets the cursor's current character format to the given format. |
void |
setPosition(int pos)
Moves the cursor to the absolute position in the document specified by pos using a MoveMode specified by m. |
void |
setPosition(int pos,
QTextCursor.MoveMode mode)
Moves the cursor to the absolute position in the document specified by pos using a MoveMode specified by m. |
void |
setVisualNavigation(boolean b)
Sets visual navigation to b. |
boolean |
visualNavigation()
Returns true if the cursor does visual navigation; otherwise returns false. |
Methods inherited from class com.trolltech.qt.QtJambiObject |
---|
dispose, disposed, equals, 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 |
---|
getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Methods inherited from interface java.lang.Comparable |
---|
compareTo |
Methods inherited from interface com.trolltech.qt.QtJambiInterface |
---|
disableGarbageCollection, nativeId, nativePointer, reenableGarbageCollection, setJavaOwnership |
Constructor Detail |
---|
public QTextCursor()
public QTextCursor(QTextDocument document)
public QTextCursor(QTextFrame frame)
public QTextCursor(QTextBlock block)
public QTextCursor(QTextCursor cursor)
Method Detail |
---|
public final int anchor()
position()
unless there is a selection in which case position()
marks one end of the selection and anchor()
marks the other end. Just like the cursor position, the anchor position is between characters. position()
, setPosition()
, movePosition()
, selectionStart()
, and selectionEnd()
.
public final boolean atBlockEnd()
atBlockStart()
, and atEnd()
.
public final boolean atBlockStart()
atBlockEnd()
, and atStart()
.
public final boolean atEnd()
atStart()
, and atBlockEnd()
.
public final boolean atStart()
atBlockStart()
, and atEnd()
.
public final void beginEditBlock()
For example:
QTextCursor cursor = new QTextCursor(textDocument); cursor.beginEditBlock(); cursor.insertText("Hello"); cursor.insertText("World"); cursor.endEditBlock(); textDocument.undo();The call to
undo()
will cause both insertions to be undone, causing both "World" and "Hello" to be removed. It is possible to nest calls to beginEditBlock
and endEditBlock
. The top-most pair will determine the scope of the undo/redo operation.
endEditBlock()
.
public final QTextBlock block()
public final QTextCharFormat blockCharFormat()
The block char format is the format used when inserting text at the beginning of an empty block.
setBlockCharFormat()
.
public final QTextBlockFormat blockFormat()
setBlockFormat()
, and charFormat()
.
public final int blockNumber()
Note that this function only makes sense in documents without complex objects such as tables or frames.
public final QTextCharFormat charFormat()
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. setCharFormat()
, insertText()
, and blockFormat()
.
public final void clearSelection()
Note that it does not delete the text of the selection.
removeSelectedText()
, and hasSelection()
.
public final int columnNumber()
public final QTextList createList(QTextListFormat.Style style)
The style to be used is defined by the QTextListFormat::Style
enum.
insertList()
, and currentList()
.
public final QTextList createList(QTextListFormat format)
insertList()
, and currentList()
.
public final QTextFrame currentFrame()
insertFrame()
.
public final QTextList currentList()
position()
is inside a block that is part of a list; otherwise returns 0. insertList()
, and createList()
.
public final QTextTable currentTable()
position()
is inside a block that is part of a table; otherwise returns 0. insertTable()
.
public final void deleteChar()
deletePreviousChar()
, hasSelection()
, and clearSelection()
.
public final void deletePreviousChar()
deleteChar()
, hasSelection()
, and clearSelection()
.
public final void endEditBlock()
beginEditBlock()
.
public final boolean hasComplexSelection()
selectionStart()
to selectionEnd()
; otherwise returns false. Complex selections are ones that span at least two cells in a table; their extent is specified by selectedTableCells()
.
public final boolean hasSelection()
public final void insertBlock()
position()
with the current blockFormat()
and charFormat()
. setBlockFormat()
.
public final void insertBlock(QTextBlockFormat format)
position()
with block format format and the current charFormat()
as block char format. setBlockFormat()
.
public final void insertBlock(QTextBlockFormat format, QTextCharFormat charFormat)
position()
with block format format and charFormat as block char format. setBlockFormat()
.
public final void insertFragment(QTextDocumentFragment fragment)
position()
.
public final QTextFrame insertFrame(QTextFrameFormat format)
position()
, moves the cursor position()
inside the frame, and returns the frame. If the cursor holds a selection, the whole selection is moved inside the frame.
hasSelection()
.
public final void insertHtml(java.lang.String html)
position()
. The text is interpreted as HTML. Note: When using this function with a style sheet, the style sheet will only apply to the current block in the document. In order to apply a style sheet throughout a document, use QTextDocument::setDefaultStyleSheet()
instead.
public final void insertImage(java.lang.String name)
position()
. QImage img = new QImage(); textDocument.addResource(QTextDocument.ResourceType.ImageResource.value(), new QUrl("myimage"), img); cursor.insertImage("myimage");
public final void insertImage(QTextImageFormat format)
position()
.
public final void insertImage(QTextImageFormat format, QTextFrameFormat.Position alignment)
position()
.
public final QTextList insertList(QTextListFormat.Style style)
currentList()
, createList()
, and insertBlock()
.
public final QTextList insertList(QTextListFormat format)
currentList()
, createList()
, and insertBlock()
.
public final QTextTable insertTable(int rows, int cols)
position()
in the document, and returns the table object. The cursor is moved to the beginning of the first cell. There must be at least one row and one column in the table.
currentTable()
.
public final QTextTable insertTable(int rows, int cols, QTextTableFormat format)
position()
in the document, and returns the table object. The cursor is moved to the beginning of the first cell. There must be at least one row and one column in the table.
currentTable()
.
public final void insertText(java.lang.String text)
If there is a selection, the selection is deleted and replaced by text, for example:
cursor.clearSelection(); cursor.movePosition(QTextCursor.MoveOperation.NextWord, QTextCursor.MoveMode.KeepAnchor); cursor.insertText("Hello World");This clears any existing selection, selects the word at the cursor (i.e. from
position()
forward), and replaces the selection with the phrase "Hello World". Any ASCII linefeed characters (\n) in the inserted text are transformed into unicode block separators, corresponding to insertBlock()
calls.
charFormat()
, and hasSelection()
.
public final void insertText(java.lang.String text, QTextCharFormat format)
public final boolean isCopyOf(QTextCursor other)
public final boolean isNull()
public final void joinPreviousEditBlock()
beginEditBlock()
indicates the start of a block of editing operations that should appear as a single operation for undo/redo. However unlike beginEditBlock()
it does not start a new block but reverses the previous call to endEditBlock()
and therefore makes following operations part of the previous edit block created. For example:
QTextCursor cursor = new QTextCursor(textDocument); cursor.beginEditBlock(); cursor.insertText("Hello"); cursor.insertText("World"); cursor.endEditBlock(); // ... cursor.joinPreviousEditBlock(); cursor.insertText("Hey"); cursor.endEditBlock(); textDocument.undo();The call to
undo()
will cause all three insertions to be undone. beginEditBlock()
, and endEditBlock()
.
public final void mergeBlockCharFormat(QTextCharFormat modifier)
setBlockCharFormat()
.
public final void mergeBlockFormat(QTextBlockFormat modifier)
setBlockFormat()
, and blockFormat()
.
public final void mergeCharFormat(QTextCharFormat modifier)
hasSelection()
, and setCharFormat()
.
public final boolean movePosition(QTextCursor.MoveOperation op, QTextCursor.MoveMode arg__2)
For example, if this function is repeatedly used to seek to the end of the next word, it will eventually fail when the end of the document is reached.
By default, the move operation is performed once (n = 1).
If mode is KeepAnchor, the cursor selects the text it moves over. This is the same effect that the user achieves when they hold down the Shift key and move the cursor with the cursor keys.
setVisualNavigation()
.
public final boolean movePosition(QTextCursor.MoveOperation op)
For example, if this function is repeatedly used to seek to the end of the next word, it will eventually fail when the end of the document is reached.
By default, the move operation is performed once (n = 1).
If mode is KeepAnchor, the cursor selects the text it moves over. This is the same effect that the user achieves when they hold down the Shift key and move the cursor with the cursor keys.
setVisualNavigation()
.
public final boolean movePosition(QTextCursor.MoveOperation op, QTextCursor.MoveMode arg__2, int n)
For example, if this function is repeatedly used to seek to the end of the next word, it will eventually fail when the end of the document is reached.
By default, the move operation is performed once (n = 1).
If mode is KeepAnchor, the cursor selects the text it moves over. This is the same effect that the user achieves when they hold down the Shift key and move the cursor with the cursor keys.
setVisualNavigation()
.
public final int position()
setPosition()
, movePosition()
, and anchor()
.
public final void removeSelectedText()
hasSelection()
.
public final void select(QTextCursor.SelectionType selection)
public final java.lang.String selectedText()
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 \n character. Use QString::replace() to replace these characters with newlines.
public final QTextDocumentFragment selection()
selectedText()
instead. Note: Unlike QTextDocumentFragment::toPlainText()
, selectedText()
may include special unicode characters such as QChar::ParagraphSeparator
.
QTextDocumentFragment::toPlainText()
.
public final int selectionEnd()
position()
if the cursor doesn't have a selection. selectionStart()
, position()
, and anchor()
.
public final int selectionStart()
position()
if the cursor doesn't have a selection. selectionEnd()
, position()
, and anchor()
.
public final void setBlockCharFormat(QTextCharFormat format)
blockCharFormat()
.
public final void setBlockFormat(QTextBlockFormat format)
blockFormat()
, and mergeBlockFormat()
.
public final void setCharFormat(QTextCharFormat format)
charFormat()
, hasSelection()
, and mergeCharFormat()
.
public final void setPosition(int pos)
position()
, movePosition()
, and anchor()
.
public final void setPosition(int pos, QTextCursor.MoveMode mode)
position()
, movePosition()
, and anchor()
.
public final void setVisualNavigation(boolean b)
Visual navigation means skipping over hidden text pragraphs. The default is false.
visualNavigation()
, and movePosition()
.
public final boolean visualNavigation()
Visual navigation means skipping over hidden text pragraphs. The default is false.
setVisualNavigation()
, and movePosition()
.
public static QTextCursor fromNativePointer(QNativePointer nativePointer)
public static QNativePointer nativePointerArray(QTextCursor[] array)
public final QTableArea selectedTableCells()
public QTextCursor clone()
clone
in class java.lang.Object
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |