diff options
author | Alan Alpert <alan.alpert@nokia.com> | 2010-04-06 13:36:17 (GMT) |
---|---|---|
committer | Alan Alpert <alan.alpert@nokia.com> | 2010-04-12 07:21:33 (GMT) |
commit | efc80209fb5e6ac9d0343b3c9f6d5a1548cf5556 (patch) | |
tree | 28b0cac83c4991b5e6b219c1277197c792fcd3ce /src/declarative/graphicsitems | |
parent | 3a632cb5f1c1da5c2e72ce167e35a42778867829 (diff) | |
download | Qt-efc80209fb5e6ac9d0343b3c9f6d5a1548cf5556.zip Qt-efc80209fb5e6ac9d0343b3c9f6d5a1548cf5556.tar.gz Qt-efc80209fb5e6ac9d0343b3c9f6d5a1548cf5556.tar.bz2 |
Add some TextInput properties and methods
Adds the properties
-passwordCharacter
-displayText
And the method
-moveCursorSelection(int pos)
These just provide a QML way to access existing QLineControl
functionality, and are necessary to create desktop style LineEdits (the
existing TextInput QML API was designed with a focus on touch input
LineEdits)
Includes tests and documentation.
Task-number: QT-321
Reviewed-by: Aaron Kennedy
Diffstat (limited to 'src/declarative/graphicsitems')
-rw-r--r-- | src/declarative/graphicsitems/qdeclarativetextinput.cpp | 83 | ||||
-rw-r--r-- | src/declarative/graphicsitems/qdeclarativetextinput_p.h | 15 |
2 files changed, 94 insertions, 4 deletions
diff --git a/src/declarative/graphicsitems/qdeclarativetextinput.cpp b/src/declarative/graphicsitems/qdeclarativetextinput.cpp index 29e43f9..e6d0948 100644 --- a/src/declarative/graphicsitems/qdeclarativetextinput.cpp +++ b/src/declarative/graphicsitems/qdeclarativetextinput.cpp @@ -634,7 +634,18 @@ void QDeclarativeTextInput::moveCursor() d->cursorItem->setX(d->control->cursorToX() - d->hscroll); } -int QDeclarativeTextInput::xToPos(int x) +/* + \qmlmethod int xToPosition(int x) + + This function returns the character position at + x pixels from the left of the textInput. 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. + + 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) { Q_D(const QDeclarativeTextInput); return d->control->xToPos(x - d->hscroll); @@ -645,6 +656,8 @@ void QDeclarativeTextInputPrivate::focusChanged(bool hasFocus) Q_Q(QDeclarativeTextInput); focused = hasFocus; q->setCursorVisible(hasFocus); + if(q->echoMode() == QDeclarativeTextInput::PasswordEchoOnEdit && !hasFocus) + control->updatePasswordEchoEditing(false);//QLineControl sets it on key events, but doesn't deal with focus events QDeclarativeItemPrivate::focusChanged(hasFocus); } @@ -809,6 +822,72 @@ void QDeclarativeTextInput::selectAll() filtering at the beginning of the animation and reenable it at the conclusion. */ +/* + \qmlproperty string TextInput::passwordCharacter + + This is the character displayed when echoMode is set to Password or + PasswordEchoOnEdit. By default it is an asterisk. + + Attempting to set this to more than one character will set it to + the first character in the string. Attempting to set this to less + than one character will fail. +*/ +QString QDeclarativeTextInput::passwordCharacter() const +{ + Q_D(const QDeclarativeTextInput); + return QString(d->control->passwordCharacter()); +} + +void QDeclarativeTextInput::setPasswordCharacter(const QString &str) +{ + Q_D(QDeclarativeTextInput); + if(str.length() < 1) + return; + emit passwordCharacterChanged(); + d->control->setPasswordCharacter(str.constData()[0]); +} + +/* + \qmlproperty string TextInput::displayText + + This is the actual text displayed in the TextInput. When + echoMode is set to TextInput::Normal this will be exactly + the same as the TextInput::text property. When echoMode + is set to something else, this property will contain the text + the user sees, while the text property will contain the + entered text. +*/ +QString QDeclarativeTextInput::displayText() const +{ + Q_D(const QDeclarativeTextInput); + return d->control->displayText(); +} + +/* + \qmlmethod void moveCursorSelection(int pos) + + This method allows you to move the cursor while modifying the selection accordingly. + To simply move the cursor, set the 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. + + Example: The sequence of calls + cursorPosition = 5 + moveCursorSelection(9) + moveCursorSelection(7) + would move 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 QDeclarativeTextInput::moveCursorSelection(int pos) +{ + Q_D(QDeclarativeTextInput); + d->control->moveCursor(pos, true); +} + void QDeclarativeTextInputPrivate::init() { Q_Q(QDeclarativeTextInput); @@ -824,6 +903,8 @@ void QDeclarativeTextInputPrivate::init() q->connect(control, SIGNAL(selectionChanged()), q, SLOT(selectionChanged())); q->connect(control, SIGNAL(textChanged(const QString &)), + q, SIGNAL(displayTextChanged(const QString &))); + q->connect(control, SIGNAL(textChanged(const QString &)), q, SLOT(q_textChanged())); q->connect(control, SIGNAL(accepted()), q, SIGNAL(accepted())); diff --git a/src/declarative/graphicsitems/qdeclarativetextinput_p.h b/src/declarative/graphicsitems/qdeclarativetextinput_p.h index 64aff7d..e453f5d 100644 --- a/src/declarative/graphicsitems/qdeclarativetextinput_p.h +++ b/src/declarative/graphicsitems/qdeclarativetextinput_p.h @@ -86,6 +86,8 @@ class Q_DECLARATIVE_EXPORT QDeclarativeTextInput : public QDeclarativePaintedIte Q_PROPERTY(bool acceptableInput READ hasAcceptableInput NOTIFY acceptableInputChanged) Q_PROPERTY(EchoMode echoMode READ echoMode WRITE setEchoMode NOTIFY echoModeChanged) Q_PROPERTY(bool focusOnPress READ focusOnPress WRITE setFocusOnPress NOTIFY focusOnPressChanged) + Q_PROPERTY(QString passwordCharacter READ passwordCharacter WRITE setPasswordCharacter NOTIFY passwordCharacterChanged) + Q_PROPERTY(QString displayText READ displayText NOTIFY displayTextChanged) public: QDeclarativeTextInput(QDeclarativeItem* parent=0); @@ -104,9 +106,9 @@ public: AlignHCenter = Qt::AlignHCenter }; - //### Should we have this function, x based properties, - //### or copy TextEdit with x instead of QTextCursor? - Q_INVOKABLE int xToPos(int x); + //Auxilliary functions needed to control the TextInput from QML + Q_INVOKABLE int xToPosition(int x); + Q_INVOKABLE void moveCursorSelection(int pos); QString text() const; void setText(const QString &); @@ -157,6 +159,11 @@ public: EchoMode echoMode() const; void setEchoMode(EchoMode echo); + QString passwordCharacter() const; + void setPasswordCharacter(const QString &str); + + QString displayText() const; + QDeclarativeComponent* cursorDelegate() const; void setCursorDelegate(QDeclarativeComponent*); @@ -188,6 +195,8 @@ Q_SIGNALS: void validatorChanged(); void inputMaskChanged(const QString &inputMask); void echoModeChanged(EchoMode echoMode); + void passwordCharacterChanged(); + void displayTextChanged(const QString &text); void focusOnPressChanged(bool focusOnPress); protected: |