diff options
author | Warwick Allison <warwick.allison@nokia.com> | 2010-05-31 05:45:01 (GMT) |
---|---|---|
committer | Warwick Allison <warwick.allison@nokia.com> | 2010-05-31 05:49:01 (GMT) |
commit | b8b1e9784583e3b5960b1966328299f8a1bec440 (patch) | |
tree | 26b147cdf82bde0b3d97610a8449fcc1599d2246 /src | |
parent | c00e6b499e89e4b872e9148c5f8a37f9050d9c84 (diff) | |
download | Qt-b8b1e9784583e3b5960b1966328299f8a1bec440.zip Qt-b8b1e9784583e3b5960b1966328299f8a1bec440.tar.gz Qt-b8b1e9784583e3b5960b1966328299f8a1bec440.tar.bz2 |
Simplify selection setting. Make TextInput more like TextEdit.
By making selectionStart/End read-only, and adding adding select().
Task-number: QTBUG-11056
Diffstat (limited to 'src')
4 files changed, 85 insertions, 61 deletions
diff --git a/src/declarative/graphicsitems/qdeclarativetextedit.cpp b/src/declarative/graphicsitems/qdeclarativetextedit.cpp index f8876f2..935b431 100644 --- a/src/declarative/graphicsitems/qdeclarativetextedit.cpp +++ b/src/declarative/graphicsitems/qdeclarativetextedit.cpp @@ -717,12 +717,9 @@ void QDeclarativeTextEdit::loadCursorDelegate() \qmlproperty int TextEdit::selectionStart The cursor position before the first character in the current selection. - Setting this and selectionEnd allows you to specify a selection in the - text edit. - Note that if selectionStart == selectionEnd then there is no current - selection. If you attempt to set selectionStart to a value outside of - the current text, selectionStart will not be changed. + This property is read-only. To change the selection, use select(start,end), + selectAll(), or selectWord(). \sa selectionEnd, cursorPosition, selectedText */ @@ -732,25 +729,13 @@ int QDeclarativeTextEdit::selectionStart() const return d->control->textCursor().selectionStart(); } -void QDeclarativeTextEdit::setSelectionStart(int s) -{ - Q_D(QDeclarativeTextEdit); - if(d->lastSelectionStart == s || s < 0 || s > text().length()) - return; - d->lastSelectionStart = s; - d->updateSelection();// Will emit the relevant signals -} - /*! \qmlproperty int TextEdit::selectionEnd The cursor position after the last character in the current selection. - Setting this and selectionStart allows you to specify a selection in the - text edit. - Note that if selectionStart == selectionEnd then there is no current - selection. If you attempt to set selectionEnd to a value outside of - the current text, selectionEnd will not be changed. + This property is read-only. To change the selection, use select(start,end), + selectAll(), or selectWord(). \sa selectionStart, cursorPosition, selectedText */ @@ -760,15 +745,6 @@ int QDeclarativeTextEdit::selectionEnd() const return d->control->textCursor().selectionEnd(); } -void QDeclarativeTextEdit::setSelectionEnd(int s) -{ - Q_D(QDeclarativeTextEdit); - if(d->lastSelectionEnd == s || s < 0 || s > text().length()) - return; - d->lastSelectionEnd = s; - d->updateSelection();// Will emit the relevant signals -} - /*! \qmlproperty string TextEdit::selectedText @@ -1018,6 +994,8 @@ void QDeclarativeTextEditPrivate::focusChanged(bool hasFocus) } /*! + \qmlmethod void TextEdit::selectAll() + Causes all text to be selected. */ void QDeclarativeTextEdit::selectAll() @@ -1027,6 +1005,8 @@ void QDeclarativeTextEdit::selectAll() } /*! + \qmlmethod void TextEdit::selectWord() + Causes the word closest to the current cursor position to be selected. */ void QDeclarativeTextEdit::selectWord() @@ -1038,6 +1018,35 @@ void QDeclarativeTextEdit::selectWord() } /*! + \qmlmethod void TextEdit::select(start,end) + + Causes the text from \a start to \a end to be selected. + + If either start or end is out of range, the selection is not changed. + + After calling this, selectionStart will become the lesser + and selectionEnd will become the greater (regardless of the order passed + to this method). + + \sa selectionStart, selectionEnd +*/ +void QDeclarativeTextEdit::select(int start, int end) +{ + Q_D(QDeclarativeTextEdit); + if (start < 0 || end < 0 || start > d->text.length() || end > d->text.length()) + return; + QTextCursor cursor = d->control->textCursor(); + cursor.beginEditBlock(); + cursor.setPosition(start, QTextCursor::MoveAnchor); + cursor.setPosition(end, QTextCursor::KeepAnchor); + cursor.endEditBlock(); + d->control->setTextCursor(cursor); + + // QTBUG-11100 + updateSelectionMarkers(); +} + +/*! \qmlmethod TextEdit::cut() Moves the currently selected text to the system clipboard. @@ -1254,7 +1263,6 @@ void QDeclarativeTextEditPrivate::updateSelection() QTextCursor cursor = control->textCursor(); bool startChange = (lastSelectionStart != cursor.selectionStart()); bool endChange = (lastSelectionEnd != cursor.selectionEnd()); - //### Is it worth calculating a more minimal set of movements? cursor.beginEditBlock(); cursor.setPosition(lastSelectionStart, QTextCursor::MoveAnchor); cursor.setPosition(lastSelectionEnd, QTextCursor::KeepAnchor); @@ -1264,8 +1272,6 @@ void QDeclarativeTextEditPrivate::updateSelection() q->selectionStartChanged(); if(endChange) q->selectionEndChanged(); - startChange = (lastSelectionStart != control->textCursor().selectionStart()); - endChange = (lastSelectionEnd != control->textCursor().selectionEnd()); } void QDeclarativeTextEdit::updateSelectionMarkers() diff --git a/src/declarative/graphicsitems/qdeclarativetextedit_p.h b/src/declarative/graphicsitems/qdeclarativetextedit_p.h index a83b3db..3abfc35 100644 --- a/src/declarative/graphicsitems/qdeclarativetextedit_p.h +++ b/src/declarative/graphicsitems/qdeclarativetextedit_p.h @@ -82,8 +82,8 @@ class Q_DECLARATIVE_EXPORT QDeclarativeTextEdit : public QDeclarativePaintedItem Q_PROPERTY(int cursorPosition READ cursorPosition WRITE setCursorPosition NOTIFY cursorPositionChanged) Q_PROPERTY(QRect cursorRectangle READ cursorRectangle NOTIFY cursorRectangleChanged) Q_PROPERTY(QDeclarativeComponent* cursorDelegate READ cursorDelegate WRITE setCursorDelegate NOTIFY cursorDelegateChanged) - Q_PROPERTY(int selectionStart READ selectionStart WRITE setSelectionStart NOTIFY selectionStartChanged) - Q_PROPERTY(int selectionEnd READ selectionEnd WRITE setSelectionEnd NOTIFY selectionEndChanged) + Q_PROPERTY(int selectionStart READ selectionStart NOTIFY selectionStartChanged) + Q_PROPERTY(int selectionEnd READ selectionEnd NOTIFY selectionEndChanged) Q_PROPERTY(QString selectedText READ selectedText NOTIFY selectionChanged) Q_PROPERTY(bool focusOnPress READ focusOnPress WRITE setFocusOnPress NOTIFY focusOnPressChanged) Q_PROPERTY(bool showInputPanelOnFocus READ showInputPanelOnFocus WRITE setShowInputPanelOnFocus NOTIFY showInputPanelOnFocusChanged) @@ -160,10 +160,7 @@ public: void setCursorDelegate(QDeclarativeComponent*); int selectionStart() const; - void setSelectionStart(int); - int selectionEnd() const; - void setSelectionEnd(int); QString selectedText() const; @@ -230,6 +227,7 @@ Q_SIGNALS: public Q_SLOTS: void selectAll(); void selectWord(); + void select(int start, int end); void cut(); void copy(); void paste(); diff --git a/src/declarative/graphicsitems/qdeclarativetextinput.cpp b/src/declarative/graphicsitems/qdeclarativetextinput.cpp index 9a6a070..25a2b49 100644 --- a/src/declarative/graphicsitems/qdeclarativetextinput.cpp +++ b/src/declarative/graphicsitems/qdeclarativetextinput.cpp @@ -46,6 +46,7 @@ #include <qdeclarativeinfo.h> #include <QValidator> +#include <QTextCursor> #include <QApplication> #include <QFontMetrics> #include <QPainter> @@ -429,10 +430,12 @@ void QDeclarativeTextInput::setCursorPosition(int cp) Returns a Rect which encompasses the cursor, but which may be larger than is required. Ignores custom cursor delegates. */ -QRect QDeclarativeTextInput::cursorRect() const +QRect QDeclarativeTextInput::cursorRectangle() const { Q_D(const QDeclarativeTextInput); - return d->control->cursorRect(); + QRect r = d->control->cursorRect(); + r.setHeight(r.height()-1); // Make consistent with TextEdit (QLineControl inexplicably adds 1) + return r; } /*! @@ -454,15 +457,6 @@ int QDeclarativeTextInput::selectionStart() const return d->lastSelectionStart; } -void QDeclarativeTextInput::setSelectionStart(int s) -{ - Q_D(QDeclarativeTextInput); - if(d->lastSelectionStart == s || s < 0 || s > text().length()) - return; - d->lastSelectionStart = s; - d->control->setSelection(s, d->lastSelectionEnd - s); -} - /*! \qmlproperty int TextInput::selectionEnd @@ -482,13 +476,12 @@ int QDeclarativeTextInput::selectionEnd() const return d->lastSelectionEnd; } -void QDeclarativeTextInput::setSelectionEnd(int s) +void QDeclarativeTextInput::select(int start, int end) { Q_D(QDeclarativeTextInput); - if(d->lastSelectionEnd == s || s < 0 || s > text().length()) + if (start < 0 || end < 0 || start > d->control->text().length() || end > d->control->text().length()) return; - d->lastSelectionEnd = s; - d->control->setSelection(d->lastSelectionStart, s - d->lastSelectionStart); + d->control->setSelection(start, end-start); } /*! @@ -833,6 +826,19 @@ void QDeclarativeTextInput::moveCursor() } /*! + \qmlmethod rect TextInput::positionToRectangle(int x) +*/ +QRectF QDeclarativeTextInput::positionToRectangle(int x) const +{ + Q_D(const QDeclarativeTextInput); + QFontMetrics fm = QFontMetrics(d->font); + return QRectF(d->control->cursorToX(x)-d->hscroll, + 0.0, + d->control->cursorWidth(), + cursorRectangle().height()); +} + +/*! \qmlmethod int TextInput::positionAt(int x) This function returns the character position at @@ -843,7 +849,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::positionAt(int x) +int QDeclarativeTextInput::positionAt(int x) const { Q_D(const QDeclarativeTextInput); return d->control->xToPos(x - d->hscroll); @@ -1019,7 +1025,6 @@ void QDeclarativeTextInput::drawContents(QPainter *p, const QRect &r) d->hscroll -= minLB; offset = QPoint(d->hscroll, 0); } - d->control->draw(p, offset, r, flags); p->restore(); @@ -1057,12 +1062,27 @@ QVariant QDeclarativeTextInput::inputMethodQuery(Qt::InputMethodQuery property) } } +/*! + \qmlmethod void TextInput::selectAll() + + Causes all text to be selected. +*/ void QDeclarativeTextInput::selectAll() { Q_D(QDeclarativeTextInput); d->control->setSelection(0, d->control->text().length()); } +/*! + \qmlmethod void TextInput::selectWord() + + Causes the word closest to the current cursor position to be selected. +*/ +void QDeclarativeTextInput::selectWord() +{ + Q_D(QDeclarativeTextInput); + d->control->selectWordAtPos(d->control->cursor()); +} /*! \qmlproperty bool TextInput::smooth diff --git a/src/declarative/graphicsitems/qdeclarativetextinput_p.h b/src/declarative/graphicsitems/qdeclarativetextinput_p.h index 6bb94c2..0b7ddd5 100644 --- a/src/declarative/graphicsitems/qdeclarativetextinput_p.h +++ b/src/declarative/graphicsitems/qdeclarativetextinput_p.h @@ -72,10 +72,10 @@ class Q_DECLARATIVE_EXPORT QDeclarativeTextInput : public QDeclarativePaintedIte Q_PROPERTY(bool readOnly READ isReadOnly WRITE setReadOnly NOTIFY readOnlyChanged) Q_PROPERTY(bool cursorVisible READ isCursorVisible WRITE setCursorVisible NOTIFY cursorVisibleChanged) Q_PROPERTY(int cursorPosition READ cursorPosition WRITE setCursorPosition NOTIFY cursorPositionChanged) - Q_PROPERTY(QRect cursorRect READ cursorRect NOTIFY cursorPositionChanged) + Q_PROPERTY(QRect cursorRectangle READ cursorRectangle NOTIFY cursorPositionChanged) Q_PROPERTY(QDeclarativeComponent *cursorDelegate READ cursorDelegate WRITE setCursorDelegate NOTIFY cursorDelegateChanged) - Q_PROPERTY(int selectionStart READ selectionStart WRITE setSelectionStart NOTIFY selectionStartChanged) - Q_PROPERTY(int selectionEnd READ selectionEnd WRITE setSelectionEnd NOTIFY selectionEndChanged) + Q_PROPERTY(int selectionStart READ selectionStart NOTIFY selectionStartChanged) + Q_PROPERTY(int selectionEnd READ selectionEnd NOTIFY selectionEndChanged) Q_PROPERTY(QString selectedText READ selectedText NOTIFY selectedTextChanged) Q_PROPERTY(int maximumLength READ maxLength WRITE setMaxLength NOTIFY maximumLengthChanged) @@ -110,7 +110,8 @@ public: }; //Auxilliary functions needed to control the TextInput from QML - Q_INVOKABLE int positionAt(int x); + Q_INVOKABLE int positionAt(int x) const; + Q_INVOKABLE QRectF positionToRectangle(int x) const; Q_INVOKABLE void moveCursorSelection(int pos); Q_INVOKABLE void openSoftwareInputPanel(); @@ -143,13 +144,10 @@ public: int cursorPosition() const; void setCursorPosition(int cp); - QRect cursorRect() const; + QRect cursorRectangle() const; int selectionStart() const; - void setSelectionStart(int); - int selectionEnd() const; - void setSelectionEnd(int); QString selectedText() const; @@ -231,6 +229,8 @@ protected: public Q_SLOTS: void selectAll(); + void selectWord(); + void select(int start, int end); private Q_SLOTS: void updateSize(bool needsRedraw = true); |