diff options
author | Warwick Allison <warwick.allison@nokia.com> | 2010-05-28 01:55:11 (GMT) |
---|---|---|
committer | Warwick Allison <warwick.allison@nokia.com> | 2010-05-30 23:57:20 (GMT) |
commit | e66fe7ea65b218dd320cb553bf6669d2a2021657 (patch) | |
tree | a25bb5c271ba5ea6e6826e3900607813db52b6e7 /src | |
parent | 81b73d11e3d147f97d20af220721617bcf3c412f (diff) | |
download | Qt-e66fe7ea65b218dd320cb553bf6669d2a2021657.zip Qt-e66fe7ea65b218dd320cb553bf6669d2a2021657.tar.gz Qt-e66fe7ea65b218dd320cb553bf6669d2a2021657.tar.bz2 |
Add selection methods to TextEdit
Sufficient to allow different selection look and feel (see whacky example)
Task-number: QTBUG-10968
Reviewed-by: Michael Brasser
Reviewed-by: Alan Alpert
Diffstat (limited to 'src')
5 files changed, 129 insertions, 3 deletions
diff --git a/src/declarative/QmlChanges.txt b/src/declarative/QmlChanges.txt index 142920c..0df5f10 100644 --- a/src/declarative/QmlChanges.txt +++ b/src/declarative/QmlChanges.txt @@ -14,6 +14,8 @@ Component: status property instead - errorsString() renamed to errorString() +TextInput xToPosition -> positionAt (to match TextEdit.positionAt) + QList<QObject*> models no longer provide properties in model object. The properties are now updated when the object changes. An object's property "foo" may now be accessed as "foo", modelData.foo" or model.modelData.foo" diff --git a/src/declarative/graphicsitems/qdeclarativetextedit.cpp b/src/declarative/graphicsitems/qdeclarativetextedit.cpp index 9ccb8d2..f8876f2 100644 --- a/src/declarative/graphicsitems/qdeclarativetextedit.cpp +++ b/src/declarative/graphicsitems/qdeclarativetextedit.cpp @@ -89,6 +89,13 @@ TextEdit { A particular look-and-feel might use smooth scrolling (eg. using SmoothedFollow), might have a visible scrollbar, or a scrollbar that fades in to show location, etc. + Clipboard support is provided by the cut(), copy(), and paste() functions, and the selection can + be handled in a traditional "mouse" mechanism by setting selectByMouse, or handled completely + from QML by manipulating selectionStart and selectionEnd, or using selectAll() or selectWord(). + + You can translate between cursor positions (characters from the start of the document) and pixel + points using positionAt() and positionToRectangle(). + \sa Text */ @@ -531,6 +538,70 @@ qreal QDeclarativeTextEdit::paintedHeight() const return implicitHeight(); } +/*! + \qmlmethod rectangle TextEdit::positionToRectangle(position) + + Returns the rectangle at the given \a position in the text. The x, y, + and height properties correspond to the cursor that would describe + that position. +*/ +QRectF QDeclarativeTextEdit::positionToRectangle(int pos) const +{ + Q_D(const QDeclarativeTextEdit); + QTextCursor c(d->document); + c.setPosition(pos); + return d->control->cursorRect(c); + +} + +/*! + \qmlmethod int TextEdit::positionAt(x,y) + + Returns the text position closest to pixel position (\a x,\a y). + + Position 0 is before the first character, position 1 is after the first character + but before the second, and so on until position text.length, which is after all characters. +*/ +int QDeclarativeTextEdit::positionAt(int x, int y) const +{ + Q_D(const QDeclarativeTextEdit); + int r = d->document->documentLayout()->hitTest(QPoint(x,y-d->yoff), Qt::FuzzyHit); + return r; +} + +/*! + \qmlmethod int TextEdit::moveCursorSeletion(int pos) + + Moves the cursor to \a position and updates the selection accordingly. + (To only move the cursor, set the \l cursorPosition property.) + + When this method is called it additionally sets either the + selectionStart or the selectionEnd (whichever was at the previous cursor position) + to the specified position. This allows you to easily extend and contract the selected + text range. + + For example, take this sequence of calls: + + \code + cursorPosition = 5 + moveCursorSelection(9) + moveCursorSelection(7) + \endcode + + This moves the cursor to position 5, extend the selection end from 5 to 9 + and then retract the selection end from 9 to 7, leaving the text from position 5 to 7 + selected (the 6th and 7th characters). +*/ +void QDeclarativeTextEdit::moveCursorSelection(int pos) +{ + //Note that this is the same as setCursorPosition but with the KeepAnchor flag set + Q_D(QDeclarativeTextEdit); + QTextCursor cursor = d->control->textCursor(); + if (cursor.position() == pos) + return; + cursor.setPosition(pos, QTextCursor::KeepAnchor); + d->control->setTextCursor(cursor); +} /*! \qmlproperty bool TextEdit::cursorVisible @@ -956,6 +1027,51 @@ void QDeclarativeTextEdit::selectAll() } /*! + Causes the word closest to the current cursor position to be selected. +*/ +void QDeclarativeTextEdit::selectWord() +{ + Q_D(QDeclarativeTextEdit); + QTextCursor c = d->control->textCursor(); + c.select(QTextCursor::WordUnderCursor); + d->control->setTextCursor(c); +} + +/*! + \qmlmethod TextEdit::cut() + + Moves the currently selected text to the system clipboard. +*/ +void QDeclarativeTextEdit::cut() +{ + Q_D(QDeclarativeTextEdit); + d->control->cut(); +} + +/*! + \qmlmethod TextEdit::copy() + + Copies the currently selected text to the system clipboard. +*/ +void QDeclarativeTextEdit::copy() +{ + Q_D(QDeclarativeTextEdit); + d->control->copy(); +} + +/*! + \qmlmethod TextEdit::paste() + + Relaces the currently selected text by the contents of the system clipboard. +*/ +void QDeclarativeTextEdit::paste() +{ + Q_D(QDeclarativeTextEdit); + d->control->paste(); +} + + +/*! \overload Handles the given mouse \a event. */ diff --git a/src/declarative/graphicsitems/qdeclarativetextedit_p.h b/src/declarative/graphicsitems/qdeclarativetextedit_p.h index 47174f4..a83b3db 100644 --- a/src/declarative/graphicsitems/qdeclarativetextedit_p.h +++ b/src/declarative/graphicsitems/qdeclarativetextedit_p.h @@ -198,6 +198,10 @@ public: qreal paintedWidth() const; qreal paintedHeight() const; + Q_INVOKABLE QRectF positionToRectangle(int) const; + Q_INVOKABLE int positionAt(int x, int y) const; + Q_INVOKABLE void moveCursorSelection(int pos); + Q_SIGNALS: void textChanged(const QString &); void paintedSizeChanged(); @@ -225,6 +229,10 @@ Q_SIGNALS: public Q_SLOTS: void selectAll(); + void selectWord(); + void cut(); + void copy(); + void paste(); private Q_SLOTS: void updateImgCache(const QRectF &rect); diff --git a/src/declarative/graphicsitems/qdeclarativetextinput.cpp b/src/declarative/graphicsitems/qdeclarativetextinput.cpp index 9c70ea9..9a6a070 100644 --- a/src/declarative/graphicsitems/qdeclarativetextinput.cpp +++ b/src/declarative/graphicsitems/qdeclarativetextinput.cpp @@ -833,7 +833,7 @@ void QDeclarativeTextInput::moveCursor() } /*! - \qmlmethod int TextInput::xToPosition(int x) + \qmlmethod int TextInput::positionAt(int x) This function returns the character position at x pixels from the left of the textInput. Position 0 is before the @@ -843,7 +843,7 @@ void QDeclarativeTextInput::moveCursor() This means that for all x values before the first character this function returns 0, and for all x values after the last character this function returns text.length. */ -int QDeclarativeTextInput::xToPosition(int x) +int QDeclarativeTextInput::positionAt(int x) { Q_D(const QDeclarativeTextInput); return d->control->xToPos(x - d->hscroll); diff --git a/src/declarative/graphicsitems/qdeclarativetextinput_p.h b/src/declarative/graphicsitems/qdeclarativetextinput_p.h index 438293a..6bb94c2 100644 --- a/src/declarative/graphicsitems/qdeclarativetextinput_p.h +++ b/src/declarative/graphicsitems/qdeclarativetextinput_p.h @@ -110,7 +110,7 @@ public: }; //Auxilliary functions needed to control the TextInput from QML - Q_INVOKABLE int xToPosition(int x); + Q_INVOKABLE int positionAt(int x); Q_INVOKABLE void moveCursorSelection(int pos); Q_INVOKABLE void openSoftwareInputPanel(); |