summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/declarative/graphicsitems/qdeclarativetextedit.cpp89
-rw-r--r--src/declarative/graphicsitems/qdeclarativetextedit_p.h7
-rw-r--r--src/declarative/graphicsitems/qdeclarativetextinput.cpp101
-rw-r--r--src/declarative/graphicsitems/qdeclarativetextinput_p.h7
-rw-r--r--tests/auto/declarative/qdeclarativetextedit/tst_qdeclarativetextedit.cpp164
-rw-r--r--tests/auto/declarative/qdeclarativetextinput/tst_qdeclarativetextinput.cpp163
6 files changed, 512 insertions, 19 deletions
diff --git a/src/declarative/graphicsitems/qdeclarativetextedit.cpp b/src/declarative/graphicsitems/qdeclarativetextedit.cpp
index 6a51a3c..7ea3dec 100644
--- a/src/declarative/graphicsitems/qdeclarativetextedit.cpp
+++ b/src/declarative/graphicsitems/qdeclarativetextedit.cpp
@@ -579,37 +579,108 @@ int QDeclarativeTextEdit::positionAt(int x, int y) const
return r;
}
+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);
+}
+
/*!
- \qmlmethod int TextEdit::moveCursorSelection(int pos)
+ \qmlmethod void TextEdit::moveCursorSelection(int position, SelectionMode mode = TextEdit.SelectCharacters)
+ \since Quick 1.1
- Moves the cursor to \a position and updates the selection accordingly.
- (To only move the cursor, set the \l cursorPosition property.)
+ Moves the cursor to \a position and updates the selection according to the optional \a mode
+ parameter. (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.
+ The selection mode specifies whether the selection is updated on a per character or a per word
+ basis. If not specified the selection mode will default to TextEdit.SelectCharacters.
+
+ \list
+ \o TextEdit.SelectCharacters - Sets either the selectionStart or selectionEnd (whichever was at
+ the previous cursor position) to the specified position.
+ \o TextEdit.SelectWords - Sets the selectionStart and selectionEnd to include all
+ words between the specified postion and the previous cursor position. Words partially in the
+ range are included.
+ \endlist
+
For example, take this sequence of calls:
\code
cursorPosition = 5
- moveCursorSelection(9)
- moveCursorSelection(7)
+ moveCursorSelection(9, TextEdit.SelectCharacters)
+ moveCursorSelection(7, TextEdit.SelectCharacters)
\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).
+
+ The same sequence with TextEdit.SelectWords will extend the selection start to a word boundary
+ before or on position 5 and extend the selection end to a word boundary past position 9, and
+ then if there is a word boundary between position 7 and 8 retract the selection end to that
+ boundary. If there is whitespace at position 7 the selection will be retracted further.
*/
-void QDeclarativeTextEdit::moveCursorSelection(int pos)
+void QDeclarativeTextEdit::moveCursorSelection(int pos, SelectionMode mode)
{
- //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);
+ if (mode == SelectCharacters) {
+ cursor.setPosition(pos, QTextCursor::KeepAnchor);
+ } else if (cursor.anchor() < pos) {
+ cursor.setPosition(cursor.anchor(), QTextCursor::MoveAnchor);
+ cursor.movePosition(QTextCursor::EndOfWord, QTextCursor::KeepAnchor);
+ if (cursor.position() == cursor.anchor()) {
+ cursor.movePosition(QTextCursor::NextWord, QTextCursor::MoveAnchor);
+ } else {
+ cursor.movePosition(QTextCursor::PreviousCharacter, QTextCursor::MoveAnchor);
+ cursor.movePosition(QTextCursor::StartOfWord, QTextCursor::MoveAnchor);
+ }
+ cursor.setPosition(pos, QTextCursor::KeepAnchor);
+ cursor.movePosition(QTextCursor::StartOfWord, QTextCursor::KeepAnchor);
+ if (cursor.position() == pos) {
+ cursor.movePosition(QTextCursor::PreviousWord, QTextCursor::KeepAnchor);
+ cursor.movePosition(QTextCursor::EndOfWord, QTextCursor::KeepAnchor);
+
+ if (cursor.anchor() > cursor.position())
+ cursor.setPosition(pos, QTextCursor::MoveAnchor);
+ } else {
+ cursor.movePosition(QTextCursor::EndOfWord, QTextCursor::KeepAnchor);
+ }
+ } else if (cursor.anchor() > pos) {
+ cursor.setPosition(cursor.anchor(), QTextCursor::MoveAnchor);
+ cursor.movePosition(QTextCursor::StartOfWord, QTextCursor::KeepAnchor);
+ if (cursor.position() == cursor.anchor()) {
+ cursor.movePosition(QTextCursor::PreviousWord, QTextCursor::MoveAnchor);
+ cursor.movePosition(QTextCursor::EndOfWord, QTextCursor::MoveAnchor);
+ } else {
+ cursor.movePosition(QTextCursor::EndOfWord, QTextCursor::MoveAnchor);
+ }
+ cursor.setPosition(pos, QTextCursor::KeepAnchor);
+ cursor.movePosition(QTextCursor::EndOfWord, QTextCursor::KeepAnchor);
+ if (cursor.position() == pos) {
+ cursor.movePosition(QTextCursor::NextWord, QTextCursor::KeepAnchor);
+
+ if (cursor.anchor() < cursor.position())
+ cursor.setPosition(pos, QTextCursor::MoveAnchor);
+ } else {
+ cursor.movePosition(QTextCursor::PreviousCharacter, QTextCursor::KeepAnchor);
+ cursor.movePosition(QTextCursor::StartOfWord, QTextCursor::KeepAnchor);
+ }
+ } else {
+ cursor.setPosition(pos, QTextCursor::MoveAnchor);
+ }
d->control->setTextCursor(cursor);
}
@@ -655,7 +726,7 @@ void QDeclarativeTextEdit::setCursorPosition(int pos)
if (pos < 0 || pos > d->text.length())
return;
QTextCursor cursor = d->control->textCursor();
- if (cursor.position() == pos)
+ if (cursor.position() == pos && cursor.anchor() == pos)
return;
cursor.setPosition(pos);
d->control->setTextCursor(cursor);
diff --git a/src/declarative/graphicsitems/qdeclarativetextedit_p.h b/src/declarative/graphicsitems/qdeclarativetextedit_p.h
index 37b24f7..6a05aa8 100644
--- a/src/declarative/graphicsitems/qdeclarativetextedit_p.h
+++ b/src/declarative/graphicsitems/qdeclarativetextedit_p.h
@@ -65,6 +65,7 @@ class Q_AUTOTEST_EXPORT QDeclarativeTextEdit : public QDeclarativePaintedItem
Q_ENUMS(HAlignment)
Q_ENUMS(TextFormat)
Q_ENUMS(WrapMode)
+ Q_ENUMS(SelectionMode)
Q_PROPERTY(QString text READ text WRITE setText NOTIFY textChanged)
Q_PROPERTY(QColor color READ color WRITE setColor NOTIFY colorChanged)
@@ -120,6 +121,11 @@ public:
Wrap = QTextOption::WrapAtWordBoundaryOrAnywhere
};
+ enum SelectionMode {
+ SelectCharacters,
+ SelectWords
+ };
+
Q_INVOKABLE void openSoftwareInputPanel();
Q_INVOKABLE void closeSoftwareInputPanel();
@@ -195,6 +201,7 @@ public:
Q_INVOKABLE QRectF positionToRectangle(int) const;
Q_INVOKABLE int positionAt(int x, int y) const;
Q_INVOKABLE void moveCursorSelection(int pos);
+ Q_INVOKABLE Q_REVISION(1) void moveCursorSelection(int pos, SelectionMode mode);
QRectF boundingRect() const;
diff --git a/src/declarative/graphicsitems/qdeclarativetextinput.cpp b/src/declarative/graphicsitems/qdeclarativetextinput.cpp
index ac07d4b..491d219c4 100644
--- a/src/declarative/graphicsitems/qdeclarativetextinput.cpp
+++ b/src/declarative/graphicsitems/qdeclarativetextinput.cpp
@@ -50,6 +50,7 @@
#include <QApplication>
#include <QFontMetrics>
#include <QPainter>
+#include <QTextBoundaryFinder>
#ifndef QT_NO_LINEEDIT
@@ -1320,35 +1321,119 @@ void QDeclarativeTextInput::setSelectByMouse(bool on)
}
}
+void QDeclarativeTextInput::moveCursorSelection(int position)
+{
+ Q_D(QDeclarativeTextInput);
+ d->control->moveCursor(position, true);
+ d->updateHorizontalScroll();
+}
/*!
- \qmlmethod void TextInput::moveCursorSelection(int position)
+ \qmlmethod void TextInput::moveCursorSelection(int position, SelectionMode mode = TextInput.SelectCharacters)
+ \since Quick 1.1
- Moves the cursor to \a position and updates the selection accordingly.
- (To only move the cursor, set the \l cursorPosition property.)
+ Moves the cursor to \a position and updates the selection according to the optional \a mode
+ parameter. (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.
+ The selection mode specifies whether the selection is updated on a per character or a per word
+ basis. If not specified the selection mode will default to TextInput.SelectCharacters.
+
+ \list
+ \o TextEdit.SelectCharacters - Sets either the selectionStart or selectionEnd (whichever was at
+ the previous cursor position) to the specified position.
+ \o TextEdit.SelectWords - Sets the selectionStart and selectionEnd to include all
+ words between the specified postion and the previous cursor position. Words partially in the
+ range are included.
+ \endlist
+
For example, take this sequence of calls:
\code
cursorPosition = 5
- moveCursorSelection(9)
- moveCursorSelection(7)
+ moveCursorSelection(9, TextInput.SelectCharacters)
+ moveCursorSelection(7, TextInput.SelectCharacters)
\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).
+
+ The same sequence with TextInput.SelectWords will extend the selection start to a word boundary
+ before or on position 5 and extend the selection end to a word boundary past position 9, and
+ then if there is a word boundary between position 7 and 8 retract the selection end to that
+ boundary. If there is whitespace at position 7 the selection will be retracted further.
*/
-void QDeclarativeTextInput::moveCursorSelection(int position)
+void QDeclarativeTextInput::moveCursorSelection(int pos, SelectionMode mode)
{
Q_D(QDeclarativeTextInput);
- d->control->moveCursor(position, true);
- d->updateHorizontalScroll();
+
+ if (mode == SelectCharacters) {
+ d->control->moveCursor(pos, true);
+ } else if (pos != d->control->cursor()){
+ int anchor;
+ if (!d->control->hasSelectedText())
+ anchor = d->control->cursor();
+ else if (d->control->selectionStart() == d->control->cursor())
+ anchor = d->control->selectionEnd();
+ else
+ anchor = d->control->selectionStart();
+
+ if (anchor < pos) {
+ QTextBoundaryFinder finder(QTextBoundaryFinder::Word, d->control->text());
+ finder.setPosition(anchor);
+
+ if (!(finder.boundaryReasons() & QTextBoundaryFinder::StartWord)) {
+ finder.toNextBoundary();
+ if (finder.boundaryReasons() != QTextBoundaryFinder::StartWord)
+ finder.toPreviousBoundary();
+ }
+ anchor = finder.position();
+
+ finder.setPosition(pos);
+ if (!(finder.boundaryReasons() & QTextBoundaryFinder::EndWord)) {
+ finder.toPreviousBoundary();
+ if (finder.boundaryReasons() != QTextBoundaryFinder::EndWord)
+ finder.toNextBoundary();
+ }
+ int cursor = finder.position();
+
+ if (anchor < cursor)
+ d->control->setSelection(anchor, cursor - anchor);
+ else
+ d->control->moveCursor(pos, false);
+
+ } else if (anchor > pos) {
+ QTextBoundaryFinder finder(QTextBoundaryFinder::Word, d->control->text());
+
+ finder.setPosition(anchor);
+ if (!(finder.boundaryReasons() & QTextBoundaryFinder::EndWord)) {
+ finder.toPreviousBoundary();
+ if (finder.boundaryReasons() != QTextBoundaryFinder::EndWord)
+ finder.toNextBoundary();
+ }
+ anchor = finder.position();
+
+ finder.setPosition(pos);
+ if (!(finder.boundaryReasons() & QTextBoundaryFinder::StartWord)) {
+ finder.toNextBoundary();
+ if (finder.boundaryReasons() != QTextBoundaryFinder::StartWord)
+ finder.toPreviousBoundary();
+ }
+ int cursor = finder.position();
+
+ if (anchor > cursor)
+ d->control->setSelection(anchor, cursor - anchor);
+ else
+ d->control->moveCursor(pos, false);
+ } else {
+ d->control->moveCursor(pos, false);
+ }
+ }
}
/*!
diff --git a/src/declarative/graphicsitems/qdeclarativetextinput_p.h b/src/declarative/graphicsitems/qdeclarativetextinput_p.h
index 878f040..25b8e66 100644
--- a/src/declarative/graphicsitems/qdeclarativetextinput_p.h
+++ b/src/declarative/graphicsitems/qdeclarativetextinput_p.h
@@ -63,6 +63,7 @@ class Q_AUTOTEST_EXPORT QDeclarativeTextInput : public QDeclarativePaintedItem
Q_OBJECT
Q_ENUMS(HAlignment)
Q_ENUMS(EchoMode)
+ Q_ENUMS(SelectionMode)
Q_PROPERTY(QString text READ text WRITE setText NOTIFY textChanged)
Q_PROPERTY(QColor color READ color WRITE setColor NOTIFY colorChanged)
@@ -112,10 +113,16 @@ public:
AlignHCenter = Qt::AlignHCenter
};
+ enum SelectionMode {
+ SelectCharacters,
+ SelectWords
+ };
+
//Auxilliary functions needed to control the TextInput from QML
Q_INVOKABLE int positionAt(int x) const;
Q_INVOKABLE QRectF positionToRectangle(int pos) const;
Q_INVOKABLE void moveCursorSelection(int pos);
+ Q_INVOKABLE Q_REVISION(1) void moveCursorSelection(int pos, SelectionMode mode);
Q_INVOKABLE void openSoftwareInputPanel();
Q_INVOKABLE void closeSoftwareInputPanel();
diff --git a/tests/auto/declarative/qdeclarativetextedit/tst_qdeclarativetextedit.cpp b/tests/auto/declarative/qdeclarativetextedit/tst_qdeclarativetextedit.cpp
index 23c8a1b..7af008f 100644
--- a/tests/auto/declarative/qdeclarativetextedit/tst_qdeclarativetextedit.cpp
+++ b/tests/auto/declarative/qdeclarativetextedit/tst_qdeclarativetextedit.cpp
@@ -66,6 +66,8 @@
#define SRCDIR "."
#endif
+Q_DECLARE_METATYPE(QDeclarativeTextEdit::SelectionMode)
+
QString createExpectedFileIfNotFound(const QString& filebasename, const QImage& actual)
{
// XXX This will be replaced by some clever persistent platform image store.
@@ -107,6 +109,8 @@ private slots:
void persistentSelection();
void focusOnPress();
void selection();
+ void moveCursorSelection_data();
+ void moveCursorSelection();
void mouseSelection_data();
void mouseSelection();
void inputMethodHints();
@@ -144,7 +148,9 @@ private:
tst_qdeclarativetextedit::tst_qdeclarativetextedit()
{
standard << "the quick brown fox jumped over the lazy dog"
- << "the quick brown fox\n jumped over the lazy dog";
+ << "the quick brown fox\n jumped over the lazy dog"
+ << "Hello, world!"
+ << "!dlrow ,olleH";
richText << "<i>the <b>quick</b> brown <a href=\\\"http://www.google.com\\\">fox</a> jumped over the <b>lazy</b> dog</i>"
<< "<i>the <b>quick</b> brown <a href=\\\"http://www.google.com\\\">fox</a><br>jumped over the <b>lazy</b> dog</i>";
@@ -711,6 +717,162 @@ void tst_qdeclarativetextedit::selection()
QVERIFY(textEditObject->selectedText().isNull());
}
+void tst_qdeclarativetextedit::moveCursorSelection_data()
+{
+ QTest::addColumn<QString>("testStr");
+ QTest::addColumn<int>("cursorPosition");
+ QTest::addColumn<int>("movePosition");
+ QTest::addColumn<QDeclarativeTextEdit::SelectionMode>("mode");
+ QTest::addColumn<int>("selectionStart");
+ QTest::addColumn<int>("selectionEnd");
+ QTest::addColumn<bool>("reversible");
+
+ QTest::newRow("(t)he|characters")
+ << standard[0] << 0 << 1 << QDeclarativeTextEdit::SelectCharacters << 0 << 1 << true;
+ QTest::newRow("do(g)|characters")
+ << standard[0] << 43 << 44 << QDeclarativeTextEdit::SelectCharacters << 43 << 44 << true;
+ QTest::newRow("jum(p)ed|characters")
+ << standard[0] << 23 << 24 << QDeclarativeTextEdit::SelectCharacters << 23 << 24 << true;
+ QTest::newRow("jumped( )over|characters")
+ << standard[0] << 26 << 27 << QDeclarativeTextEdit::SelectCharacters << 26 << 27 << true;
+ QTest::newRow("(the )|characters")
+ << standard[0] << 0 << 4 << QDeclarativeTextEdit::SelectCharacters << 0 << 4 << true;
+ QTest::newRow("( dog)|characters")
+ << standard[0] << 40 << 44 << QDeclarativeTextEdit::SelectCharacters << 40 << 44 << true;
+ QTest::newRow("( jumped )|characters")
+ << standard[0] << 19 << 27 << QDeclarativeTextEdit::SelectCharacters << 19 << 27 << true;
+ QTest::newRow("th(e qu)ick|characters")
+ << standard[0] << 2 << 6 << QDeclarativeTextEdit::SelectCharacters << 2 << 6 << true;
+ QTest::newRow("la(zy d)og|characters")
+ << standard[0] << 38 << 42 << QDeclarativeTextEdit::SelectCharacters << 38 << 42 << true;
+ QTest::newRow("jum(ped ov)er|characters")
+ << standard[0] << 23 << 29 << QDeclarativeTextEdit::SelectCharacters << 23 << 29 << true;
+ QTest::newRow("()the|characters")
+ << standard[0] << 0 << 0 << QDeclarativeTextEdit::SelectCharacters << 0 << 0 << true;
+ QTest::newRow("dog()|characters")
+ << standard[0] << 44 << 44 << QDeclarativeTextEdit::SelectCharacters << 44 << 44 << true;
+ QTest::newRow("jum()ped|characters")
+ << standard[0] << 23 << 23 << QDeclarativeTextEdit::SelectCharacters << 23 << 23 << true;
+
+ QTest::newRow("(t)he|words")
+ << standard[0] << 0 << 1 << QDeclarativeTextEdit::SelectWords << 0 << 3 << true;
+ QTest::newRow("do(g)|words")
+ << standard[0] << 43 << 44 << QDeclarativeTextEdit::SelectWords << 41 << 44 << true;
+ QTest::newRow("jum(p)ed|words")
+ << standard[0] << 23 << 24 << QDeclarativeTextEdit::SelectWords << 20 << 26 << true;
+ QTest::newRow("jumped( )over|words")
+ << standard[0] << 26 << 27 << QDeclarativeTextEdit::SelectWords << 27 << 27 << false;
+ QTest::newRow("jumped( )over|words,reversed")
+ << standard[0] << 27 << 26 << QDeclarativeTextEdit::SelectWords << 26 << 26 << false;
+ QTest::newRow("(the )|words")
+ << standard[0] << 0 << 4 << QDeclarativeTextEdit::SelectWords << 0 << 3 << true;
+ QTest::newRow("( dog)|words")
+ << standard[0] << 40 << 44 << QDeclarativeTextEdit::SelectWords << 41 << 44 << true;
+ QTest::newRow("( jumped )|words")
+ << standard[0] << 19 << 27 << QDeclarativeTextEdit::SelectWords << 20 << 26 << true;
+ QTest::newRow("th(e qu)ick|words")
+ << standard[0] << 2 << 6 << QDeclarativeTextEdit::SelectWords << 0 << 9 << true;
+ QTest::newRow("la(zy d)og|words")
+ << standard[0] << 38 << 42 << QDeclarativeTextEdit::SelectWords << 36 << 44 << true;
+ QTest::newRow("jum(ped ov)er|words")
+ << standard[0] << 23 << 29 << QDeclarativeTextEdit::SelectWords << 20 << 31 << true;
+ QTest::newRow("()the|words")
+ << standard[0] << 0 << 0 << QDeclarativeTextEdit::SelectWords << 0 << 0 << true;
+ QTest::newRow("dog()|words")
+ << standard[0] << 44 << 44 << QDeclarativeTextEdit::SelectWords << 44 << 44 << true;
+ QTest::newRow("jum()ped|words")
+ << standard[0] << 23 << 23 << QDeclarativeTextEdit::SelectWords << 23 << 23 << true;
+
+ QTest::newRow("Hello(,) |words")
+ << standard[2] << 5 << 6 << QDeclarativeTextEdit::SelectWords << 5 << 6 << true;
+ QTest::newRow("Hello(, )|words")
+ << standard[2] << 5 << 7 << QDeclarativeTextEdit::SelectWords << 5 << 6 << true;
+ QTest::newRow("Hel(lo, )|words")
+ << standard[2] << 3 << 7 << QDeclarativeTextEdit::SelectWords << 0 << 6 << true;
+ QTest::newRow("Hel(lo),|words")
+ << standard[2] << 3 << 5 << QDeclarativeTextEdit::SelectWords << 0 << 5 << true;
+ QTest::newRow("Hello(),|words")
+ << standard[2] << 5 << 5 << QDeclarativeTextEdit::SelectWords << 5 << 5 << true;
+ QTest::newRow("Hello,()|words")
+ << standard[2] << 6 << 6 << QDeclarativeTextEdit::SelectWords << 6 << 6 << true;
+ QTest::newRow("Hello,( )|words")
+ << standard[2] << 6 << 7 << QDeclarativeTextEdit::SelectWords << 7 << 7 << false;
+ QTest::newRow("Hello,( )|words")
+ << standard[2] << 7 << 6 << QDeclarativeTextEdit::SelectWords << 6 << 6 << false;
+ QTest::newRow("Hello,( world)|words")
+ << standard[2] << 6 << 12 << QDeclarativeTextEdit::SelectWords << 7 << 12 << true;
+ QTest::newRow("Hello,( world!)|words")
+ << standard[2] << 6 << 13 << QDeclarativeTextEdit::SelectWords << 7 << 13 << true;
+ QTest::newRow("Hello(, world!)|words")
+ << standard[2] << 5 << 13 << QDeclarativeTextEdit::SelectWords << 5 << 13 << true;
+ QTest::newRow("world(!)|words")
+ << standard[2] << 12 << 13 << QDeclarativeTextEdit::SelectWords << 12 << 13 << true;
+ QTest::newRow("world!())|words")
+ << standard[2] << 13 << 13 << QDeclarativeTextEdit::SelectWords << 13 << 13 << true;
+ QTest::newRow("world()!)|words")
+ << standard[2] << 12 << 12 << QDeclarativeTextEdit::SelectWords << 12 << 12 << true;
+
+ QTest::newRow("(,)olleH |words")
+ << standard[3] << 7 << 8 << QDeclarativeTextEdit::SelectWords << 7 << 8 << true;
+ QTest::newRow("( ,)olleH|words")
+ << standard[3] << 6 << 8 << QDeclarativeTextEdit::SelectWords << 7 << 8 << true;
+ QTest::newRow("( ,ol)leH|words")
+ << standard[3] << 6 << 10 << QDeclarativeTextEdit::SelectWords << 7 << 13 << true;
+ QTest::newRow(",(ol)leH,|words")
+ << standard[3] << 8 << 10 << QDeclarativeTextEdit::SelectWords << 8 << 13 << true;
+ QTest::newRow(",()olleH|words")
+ << standard[3] << 8 << 8 << QDeclarativeTextEdit::SelectWords << 8 << 8 << true;
+ QTest::newRow("(),olleH|words")
+ << standard[3] << 7 << 7 << QDeclarativeTextEdit::SelectWords << 7 << 7 << true;
+ QTest::newRow("( ),olleH|words")
+ << standard[3] << 6 << 7 << QDeclarativeTextEdit::SelectWords << 7 << 7 << false;
+ QTest::newRow("( ),olleH|words,reversed")
+ << standard[3] << 7 << 6 << QDeclarativeTextEdit::SelectWords << 6 << 6 << false;
+ QTest::newRow("(dlrow ),olleH|words")
+ << standard[3] << 1 << 7 << QDeclarativeTextEdit::SelectWords << 1 << 6 << true;
+ QTest::newRow("(!dlrow ),olleH|words")
+ << standard[3] << 0 << 7 << QDeclarativeTextEdit::SelectWords << 0 << 6 << true;
+ QTest::newRow("(!dlrow ,)olleH|words")
+ << standard[3] << 0 << 8 << QDeclarativeTextEdit::SelectWords << 0 << 8 << true;
+ QTest::newRow("(!)dlrow|words")
+ << standard[3] << 0 << 1 << QDeclarativeTextEdit::SelectWords << 0 << 1 << true;
+ QTest::newRow("()!dlrow|words")
+ << standard[3] << 0 << 0 << QDeclarativeTextEdit::SelectWords << 0 << 0 << true;
+ QTest::newRow("!()dlrow|words")
+ << standard[3] << 1 << 1 << QDeclarativeTextEdit::SelectWords << 1 << 1 << true;
+}
+
+void tst_qdeclarativetextedit::moveCursorSelection()
+{
+ QFETCH(QString, testStr);
+ QFETCH(int, cursorPosition);
+ QFETCH(int, movePosition);
+ QFETCH(QDeclarativeTextEdit::SelectionMode, mode);
+ QFETCH(int, selectionStart);
+ QFETCH(int, selectionEnd);
+ QFETCH(bool, reversible);
+
+ QString componentStr = "import QtQuick 1.1\nTextEdit { text: \""+ testStr +"\"; }";
+ QDeclarativeComponent textinputComponent(&engine);
+ textinputComponent.setData(componentStr.toLatin1(), QUrl());
+ QDeclarativeTextEdit *texteditObject = qobject_cast<QDeclarativeTextEdit*>(textinputComponent.create());
+ QVERIFY(texteditObject != 0);
+
+ texteditObject->setCursorPosition(cursorPosition);
+ texteditObject->moveCursorSelection(movePosition, mode);
+
+ QCOMPARE(texteditObject->selectionStart(), selectionStart);
+ QCOMPARE(texteditObject->selectionEnd(), selectionEnd);
+
+ if (reversible) {
+ texteditObject->setCursorPosition(movePosition);
+ texteditObject->moveCursorSelection(cursorPosition, mode);
+
+ QCOMPARE(texteditObject->selectionStart(), selectionStart);
+ QCOMPARE(texteditObject->selectionEnd(), selectionEnd);
+ }
+}
+
void tst_qdeclarativetextedit::mouseSelection_data()
{
QTest::addColumn<QString>("qmlfile");
diff --git a/tests/auto/declarative/qdeclarativetextinput/tst_qdeclarativetextinput.cpp b/tests/auto/declarative/qdeclarativetextinput/tst_qdeclarativetextinput.cpp
index 160c3b6..1de695e 100644
--- a/tests/auto/declarative/qdeclarativetextinput/tst_qdeclarativetextinput.cpp
+++ b/tests/auto/declarative/qdeclarativetextinput/tst_qdeclarativetextinput.cpp
@@ -57,6 +57,8 @@
#define SRCDIR "."
#endif
+Q_DECLARE_METATYPE(QDeclarativeTextInput::SelectionMode)
+
QString createExpectedFileIfNotFound(const QString& filebasename, const QImage& actual)
{
// XXX This will be replaced by some clever persistent platform image store.
@@ -87,6 +89,8 @@ private slots:
void font();
void color();
void selection();
+ void moveCursorSelection_data();
+ void moveCursorSelection();
void horizontalAlignment_data();
void horizontalAlignment();
@@ -124,7 +128,8 @@ tst_qdeclarativetextinput::tst_qdeclarativetextinput()
{
standard << "the quick brown fox jumped over the lazy dog"
<< "It's supercalifragisiticexpialidocious!"
- << "Hello, world!";
+ << "Hello, world!"
+ << "!dlrow ,olleH";
colorStrings << "aliceblue"
<< "antiquewhite"
@@ -415,6 +420,162 @@ void tst_qdeclarativetextinput::selection()
delete textinputObject;
}
+void tst_qdeclarativetextinput::moveCursorSelection_data()
+{
+ QTest::addColumn<QString>("testStr");
+ QTest::addColumn<int>("cursorPosition");
+ QTest::addColumn<int>("movePosition");
+ QTest::addColumn<QDeclarativeTextInput::SelectionMode>("mode");
+ QTest::addColumn<int>("selectionStart");
+ QTest::addColumn<int>("selectionEnd");
+ QTest::addColumn<bool>("reversible");
+
+ QTest::newRow("(t)he|characters")
+ << standard[0] << 0 << 1 << QDeclarativeTextInput::SelectCharacters << 0 << 1 << true;
+ QTest::newRow("do(g)|characters")
+ << standard[0] << 43 << 44 << QDeclarativeTextInput::SelectCharacters << 43 << 44 << true;
+ QTest::newRow("jum(p)ed|characters")
+ << standard[0] << 23 << 24 << QDeclarativeTextInput::SelectCharacters << 23 << 24 << true;
+ QTest::newRow("jumped( )over|characters")
+ << standard[0] << 26 << 27 << QDeclarativeTextInput::SelectCharacters << 26 << 27 << true;
+ QTest::newRow("(the )|characters")
+ << standard[0] << 0 << 4 << QDeclarativeTextInput::SelectCharacters << 0 << 4 << true;
+ QTest::newRow("( dog)|characters")
+ << standard[0] << 40 << 44 << QDeclarativeTextInput::SelectCharacters << 40 << 44 << true;
+ QTest::newRow("( jumped )|characters")
+ << standard[0] << 19 << 27 << QDeclarativeTextInput::SelectCharacters << 19 << 27 << true;
+ QTest::newRow("th(e qu)ick|characters")
+ << standard[0] << 2 << 6 << QDeclarativeTextInput::SelectCharacters << 2 << 6 << true;
+ QTest::newRow("la(zy d)og|characters")
+ << standard[0] << 38 << 42 << QDeclarativeTextInput::SelectCharacters << 38 << 42 << true;
+ QTest::newRow("jum(ped ov)er|characters")
+ << standard[0] << 23 << 29 << QDeclarativeTextInput::SelectCharacters << 23 << 29 << true;
+ QTest::newRow("()the|characters")
+ << standard[0] << 0 << 0 << QDeclarativeTextInput::SelectCharacters << 0 << 0 << true;
+ QTest::newRow("dog()|characters")
+ << standard[0] << 44 << 44 << QDeclarativeTextInput::SelectCharacters << 44 << 44 << true;
+ QTest::newRow("jum()ped|characters")
+ << standard[0] << 23 << 23 << QDeclarativeTextInput::SelectCharacters << 23 << 23 << true;
+
+ QTest::newRow("(t)he|words")
+ << standard[0] << 0 << 1 << QDeclarativeTextInput::SelectWords << 0 << 3 << true;
+ QTest::newRow("do(g)|words")
+ << standard[0] << 43 << 44 << QDeclarativeTextInput::SelectWords << 41 << 44 << true;
+ QTest::newRow("jum(p)ed|words")
+ << standard[0] << 23 << 24 << QDeclarativeTextInput::SelectWords << 20 << 26 << true;
+ QTest::newRow("jumped( )over|words")
+ << standard[0] << 26 << 27 << QDeclarativeTextInput::SelectWords << 27 << 27 << false;
+ QTest::newRow("jumped( )over|words,reversed")
+ << standard[0] << 27 << 26 << QDeclarativeTextInput::SelectWords << 26 << 26 << false;
+ QTest::newRow("(the )|words")
+ << standard[0] << 0 << 4 << QDeclarativeTextInput::SelectWords << 0 << 3 << true;
+ QTest::newRow("( dog)|words")
+ << standard[0] << 40 << 44 << QDeclarativeTextInput::SelectWords << 41 << 44 << true;
+ QTest::newRow("( jumped )|words")
+ << standard[0] << 19 << 27 << QDeclarativeTextInput::SelectWords << 20 << 26 << true;
+ QTest::newRow("th(e qu)ick|words")
+ << standard[0] << 2 << 6 << QDeclarativeTextInput::SelectWords << 0 << 9 << true;
+ QTest::newRow("la(zy d)og|words")
+ << standard[0] << 38 << 42 << QDeclarativeTextInput::SelectWords << 36 << 44 << true;
+ QTest::newRow("jum(ped ov)er|words")
+ << standard[0] << 23 << 29 << QDeclarativeTextInput::SelectWords << 20 << 31 << true;
+ QTest::newRow("()the|words")
+ << standard[0] << 0 << 0 << QDeclarativeTextInput::SelectWords << 0 << 0 << true;
+ QTest::newRow("dog()|words")
+ << standard[0] << 44 << 44 << QDeclarativeTextInput::SelectWords << 44 << 44 << true;
+ QTest::newRow("jum()ped|words")
+ << standard[0] << 23 << 23 << QDeclarativeTextInput::SelectWords << 23 << 23 << true;
+
+ QTest::newRow("Hello(,) |words")
+ << standard[2] << 5 << 6 << QDeclarativeTextInput::SelectWords << 5 << 6 << true;
+ QTest::newRow("Hello(, )|words")
+ << standard[2] << 5 << 7 << QDeclarativeTextInput::SelectWords << 5 << 6 << true;
+ QTest::newRow("Hel(lo, )|words")
+ << standard[2] << 3 << 7 << QDeclarativeTextInput::SelectWords << 0 << 6 << true;
+ QTest::newRow("Hel(lo),|words")
+ << standard[2] << 3 << 5 << QDeclarativeTextInput::SelectWords << 0 << 5 << true;
+ QTest::newRow("Hello(),|words")
+ << standard[2] << 5 << 5 << QDeclarativeTextInput::SelectWords << 5 << 5 << true;
+ QTest::newRow("Hello,()|words")
+ << standard[2] << 6 << 6 << QDeclarativeTextInput::SelectWords << 6 << 6 << true;
+ QTest::newRow("Hello,( )|words")
+ << standard[2] << 6 << 7 << QDeclarativeTextInput::SelectWords << 7 << 7 << false;
+ QTest::newRow("Hello,( )|words,reversed")
+ << standard[2] << 7 << 6 << QDeclarativeTextInput::SelectWords << 6 << 6 << false;
+ QTest::newRow("Hello,( world)|words")
+ << standard[2] << 6 << 12 << QDeclarativeTextInput::SelectWords << 7 << 12 << true;
+ QTest::newRow("Hello,( world!)|words")
+ << standard[2] << 6 << 13 << QDeclarativeTextInput::SelectWords << 7 << 13 << true;
+ QTest::newRow("Hello(, world!)|words")
+ << standard[2] << 5 << 13 << QDeclarativeTextInput::SelectWords << 5 << 13 << true;
+ QTest::newRow("world(!)|words")
+ << standard[2] << 12 << 13 << QDeclarativeTextInput::SelectWords << 12 << 13 << true;
+ QTest::newRow("world!())|words")
+ << standard[2] << 13 << 13 << QDeclarativeTextInput::SelectWords << 13 << 13 << true;
+ QTest::newRow("world()!)|words")
+ << standard[2] << 12 << 12 << QDeclarativeTextInput::SelectWords << 12 << 12 << true;
+
+ QTest::newRow("(,)olleH |words")
+ << standard[3] << 7 << 8 << QDeclarativeTextInput::SelectWords << 7 << 8 << true;
+ QTest::newRow("( ,)olleH|words")
+ << standard[3] << 6 << 8 << QDeclarativeTextInput::SelectWords << 7 << 8 << true;
+ QTest::newRow("( ,ol)leH|words")
+ << standard[3] << 6 << 10 << QDeclarativeTextInput::SelectWords << 7 << 13 << true;
+ QTest::newRow(",(ol)leH,|words")
+ << standard[3] << 8 << 10 << QDeclarativeTextInput::SelectWords << 8 << 13 << true;
+ QTest::newRow(",()olleH|words")
+ << standard[3] << 8 << 8 << QDeclarativeTextInput::SelectWords << 8 << 8 << true;
+ QTest::newRow("(),olleH|words")
+ << standard[3] << 7 << 7 << QDeclarativeTextInput::SelectWords << 7 << 7 << true;
+ QTest::newRow("( ),olleH|words")
+ << standard[3] << 6 << 7 << QDeclarativeTextInput::SelectWords << 7 << 7 << false;
+ QTest::newRow("( ),olleH|words,reversed")
+ << standard[3] << 7 << 6 << QDeclarativeTextInput::SelectWords << 6 << 6 << false;
+ QTest::newRow("(dlrow ),olleH|words")
+ << standard[3] << 1 << 7 << QDeclarativeTextInput::SelectWords << 1 << 6 << true;
+ QTest::newRow("(!dlrow ),olleH|words")
+ << standard[3] << 0 << 7 << QDeclarativeTextInput::SelectWords << 0 << 6 << true;
+ QTest::newRow("(!dlrow ,)olleH|words")
+ << standard[3] << 0 << 8 << QDeclarativeTextInput::SelectWords << 0 << 8 << true;
+ QTest::newRow("(!)dlrow|words")
+ << standard[3] << 0 << 1 << QDeclarativeTextInput::SelectWords << 0 << 1 << true;
+ QTest::newRow("()!dlrow|words")
+ << standard[3] << 0 << 0 << QDeclarativeTextInput::SelectWords << 0 << 0 << true;
+ QTest::newRow("!()dlrow|words")
+ << standard[3] << 1 << 1 << QDeclarativeTextInput::SelectWords << 1 << 1 << true;
+}
+
+void tst_qdeclarativetextinput::moveCursorSelection()
+{
+ QFETCH(QString, testStr);
+ QFETCH(int, cursorPosition);
+ QFETCH(int, movePosition);
+ QFETCH(QDeclarativeTextInput::SelectionMode, mode);
+ QFETCH(int, selectionStart);
+ QFETCH(int, selectionEnd);
+ QFETCH(bool, reversible);
+
+ QString componentStr = "import QtQuick 1.1\nTextInput { text: \""+ testStr +"\"; }";
+ QDeclarativeComponent textinputComponent(&engine);
+ textinputComponent.setData(componentStr.toLatin1(), QUrl());
+ QDeclarativeTextInput *textinputObject = qobject_cast<QDeclarativeTextInput*>(textinputComponent.create());
+ QVERIFY(textinputObject != 0);
+
+ textinputObject->setCursorPosition(cursorPosition);
+ textinputObject->moveCursorSelection(movePosition, mode);
+
+ QCOMPARE(textinputObject->selectionStart(), selectionStart);
+ QCOMPARE(textinputObject->selectionEnd(), selectionEnd);
+
+ if (reversible) {
+ textinputObject->setCursorPosition(movePosition);
+ textinputObject->moveCursorSelection(cursorPosition, mode);
+
+ QCOMPARE(textinputObject->selectionStart(), selectionStart);
+ QCOMPARE(textinputObject->selectionEnd(), selectionEnd);
+ }
+}
+
void tst_qdeclarativetextinput::horizontalAlignment_data()
{
QTest::addColumn<int>("hAlign");