From 2123a7ff6a04aec331fada5e03aa99395a0de1db Mon Sep 17 00:00:00 2001 From: Andrew den Exter Date: Wed, 2 Mar 2011 15:53:48 +1000 Subject: Make the TextInput cursorRectangle relative to the item. The rectangle returned was relative to the text and didn't adjust for horizontal scrolling. Change-Id: I09227d73bbd8b32d830744d5911d785246051c2f Reviewed-by: Martin Jones --- .../graphicsitems/qdeclarativetextinput.cpp | 1 + .../tst_qdeclarativetextinput.cpp | 42 ++++++++++++++++++++-- 2 files changed, 40 insertions(+), 3 deletions(-) diff --git a/src/declarative/graphicsitems/qdeclarativetextinput.cpp b/src/declarative/graphicsitems/qdeclarativetextinput.cpp index c873172..dc44bfe 100644 --- a/src/declarative/graphicsitems/qdeclarativetextinput.cpp +++ b/src/declarative/graphicsitems/qdeclarativetextinput.cpp @@ -482,6 +482,7 @@ QRect QDeclarativeTextInput::cursorRectangle() const Q_D(const QDeclarativeTextInput); QRect r = d->control->cursorRect(); r.setHeight(r.height()-1); // Make consistent with TextEdit (QLineControl inexplicably adds 1) + r.moveLeft(r.x() - d->hscroll); return r; } diff --git a/tests/auto/declarative/qdeclarativetextinput/tst_qdeclarativetextinput.cpp b/tests/auto/declarative/qdeclarativetextinput/tst_qdeclarativetextinput.cpp index 045f60e..0d228e2 100644 --- a/tests/auto/declarative/qdeclarativetextinput/tst_qdeclarativetextinput.cpp +++ b/tests/auto/declarative/qdeclarativetextinput/tst_qdeclarativetextinput.cpp @@ -111,6 +111,7 @@ private slots: void passwordCharacter(); void cursorDelegate(); void cursorVisible(); + void cursorRectangle(); void navigation(); void copyAndPaste(); void readOnly(); @@ -1414,6 +1415,41 @@ void tst_qdeclarativetextinput::cursorVisible() #endif } +void tst_qdeclarativetextinput::cursorRectangle() +{ + QString text = "Hello World!"; + + QDeclarativeTextInput input; + input.setText(text); + QFontMetricsF fm(input.font()); + input.setWidth(fm.width(text.mid(0, 5))); + + QRect r; + + for (int i = 0; i <= 5; ++i) { + input.setCursorPosition(i); + r = input.cursorRectangle(); + int textWidth = fm.width(text.mid(0, i)); + + QVERIFY(r.left() < textWidth); + QVERIFY(r.right() > textWidth); + } + + // Check the cursor rectangle remains within the input bounding rect when auto scrolling. + QVERIFY(r.left() < input.boundingRect().width()); + QVERIFY(r.right() >= input.width()); + + for (int i = 6; i < text.length(); ++i) { + input.setCursorPosition(i); + QCOMPARE(r, input.cursorRectangle()); + } + + for (int i = text.length() - 2; i >= 0; --i) { + input.setCursorPosition(i); + QVERIFY(r.right() >= 0); + } +} + void tst_qdeclarativetextinput::readOnly() { QDeclarativeView *canvas = createView(SRCDIR "/data/readOnly.qml"); @@ -1882,7 +1918,7 @@ void tst_qdeclarativetextinput::preeditAutoScroll() // test the text is scrolled so the preedit is visible. ic.sendPreeditText(preeditText.mid(0, 3), 1); QVERIFY(input.positionAt(0) != 0); - QVERIFY(input.cursorRectangle().x() - 1 <= input.width()); + QVERIFY(input.cursorRectangle().left() < input.boundingRect().width()); // test the text is scrolled back when the preedit is removed. ic.sendEvent(QInputMethodEvent()); @@ -1894,13 +1930,13 @@ void tst_qdeclarativetextinput::preeditAutoScroll() qreal x = input.positionToRectangle(0).x(); for (int i = 0; i < 3; ++i) { ic.sendPreeditText(preeditText, i + 1); - QVERIFY(input.cursorRectangle().x() >= fm.width(preeditText.at(i))); + QVERIFY(input.cursorRectangle().right() >= fm.width(preeditText.at(i))); QVERIFY(input.positionToRectangle(0).x() < x); x = input.positionToRectangle(0).x(); } for (int i = 1; i >= 0; --i) { ic.sendPreeditText(preeditText, i + 1); - QVERIFY(input.cursorRectangle().x() >= fm.width(preeditText.at(i))); + QVERIFY(input.cursorRectangle().right() >= fm.width(preeditText.at(i))); QVERIFY(input.positionToRectangle(0).x() > x); x = input.positionToRectangle(0).x(); } -- cgit v0.12