Ensure the cursor delegate position is correct when wrapping.

When inserting text into a TextEdit with implicit width and wrapping
the cursor delegate position has to be updated after the width is
expanded to the implicit width otherwise it will be positioned in a
wrapped location.

Change-Id: Ibcb709ec1b4f6827ea8ae919f2e0c932c7372869
Reviewed-by: Martin Jones <martin.jones@nokia.com>
This commit is contained in:
Andrew den Exter 2012-04-18 17:20:24 +10:00 committed by Qt by Nokia
parent 458eb3cce9
commit 7bd68f6447
12 changed files with 164 additions and 41 deletions

View File

@ -110,7 +110,8 @@ QQuickTextControlPrivate::QQuickTextControlPrivate()
isEnabled(true),
hadSelectionOnMousePress(false),
wordSelectionEnabled(false),
hasImState(false)
hasImState(false),
cursorRectangleChanged(false)
{}
bool QQuickTextControlPrivate::cursorMoveKeyEvent(QKeyEvent *e)
@ -253,7 +254,7 @@ bool QQuickTextControlPrivate::cursorMoveKeyEvent(QKeyEvent *e)
if (moved) {
if (cursor.position() != oldCursorPos)
emit q->cursorPositionChanged();
emit q->cursorRectangleChanged();
q->updateCursorRectangle(true);
} else if (isNavigationEvent && oldSelection.anchor() == cursor.anchor()) {
return false;
}
@ -275,7 +276,7 @@ void QQuickTextControlPrivate::updateCurrentCharFormat()
lastCharFormat = fmt;
emit q->currentCharFormatChanged(fmt);
emit q->cursorRectangleChanged();
cursorRectangleChanged = true;
}
void QQuickTextControlPrivate::init(Qt::TextFormat format, const QString &text, QTextDocument *document)
@ -366,7 +367,7 @@ void QQuickTextControlPrivate::setContent(Qt::TextFormat format, const QString &
if (!document)
doc->setModified(false);
emit q->cursorRectangleChanged();
q->updateCursorRectangle(true);
emit q->cursorPositionChanged();
}
@ -443,7 +444,7 @@ void QQuickTextControlPrivate::selectionChanged(bool forceEmitSelectionChanged /
qGuiApp->inputMethod()->update(Qt::ImCurrentSelection);
emit q->selectionChanged();
}
emit q->cursorRectangleChanged();
q->updateCursorRectangle(true);
}
void QQuickTextControlPrivate::_q_updateCurrentCharFormatAndSelection()
@ -469,7 +470,7 @@ void QQuickTextControlPrivate::_q_emitCursorPosChanged(const QTextCursor &someCu
Q_Q(QQuickTextControl);
if (someCursor.isCopyOf(cursor)) {
emit q->cursorPositionChanged();
emit q->cursorRectangleChanged();
cursorRectangleChanged = true;
}
}
@ -592,7 +593,7 @@ void QQuickTextControl::undo()
d->doc->undo(&d->cursor);
if (d->cursor.position() != oldCursorPos)
emit cursorPositionChanged();
emit cursorRectangleChanged();
updateCursorRectangle(true);
}
void QQuickTextControl::redo()
@ -601,9 +602,9 @@ void QQuickTextControl::redo()
d->repaintSelection();
const int oldCursorPos = d->cursor.position();
d->doc->redo(&d->cursor);
if (d->cursor.position() != oldCursorPos)
if (d->cursor.position() != oldCursorPos)
emit cursorPositionChanged();
emit cursorRectangleChanged();
updateCursorRectangle(true);
}
QQuickTextControl::QQuickTextControl(QTextDocument *doc, QObject *parent)
@ -623,6 +624,15 @@ QTextDocument *QQuickTextControl::document() const
return d->doc;
}
void QQuickTextControl::updateCursorRectangle(bool force)
{
Q_D(QQuickTextControl);
const bool update = d->cursorRectangleChanged || force;
d->cursorRectangleChanged = false;
if (update)
emit cursorRectangleChanged();
}
void QQuickTextControl::setTextCursor(const QTextCursor &cursor)
{
Q_D(QQuickTextControl);
@ -633,7 +643,7 @@ void QQuickTextControl::setTextCursor(const QTextCursor &cursor)
d->cursor = cursor;
d->cursorOn = d->hasFocus && (d->interactionFlags & Qt::TextEditable);
d->_q_updateCurrentCharFormatAndSelection();
emit cursorRectangleChanged();
updateCursorRectangle(true);
d->repaintOldAndNewSelection(oldSelection);
if (posChanged)
emit cursorPositionChanged();
@ -956,8 +966,7 @@ process:
e->accept();
cursorOn = true;
emit q->cursorRectangleChanged();
q->updateCursorRectangle(true);
updateCurrentCharFormat();
}
@ -1198,14 +1207,14 @@ void QQuickTextControlPrivate::mousePressEvent(QMouseEvent *e, const QPointF &po
}
if (interactionFlags & Qt::TextEditable) {
emit q->cursorRectangleChanged();
q->updateCursorRectangle(true);
if (cursor.position() != oldCursorPos)
emit q->cursorPositionChanged();
_q_updateCurrentCharFormatAndSelection();
} else {
if (cursor.position() != oldCursorPos) {
emit q->cursorPositionChanged();
emit q->cursorRectangleChanged();
q->updateCursorRectangle(true);
}
selectionChanged();
}
@ -1316,7 +1325,7 @@ void QQuickTextControlPrivate::mouseReleaseEvent(QMouseEvent *e, const QPointF &
if (cursor.position() != oldCursorPos) {
emit q->cursorPositionChanged();
emit q->cursorRectangleChanged();
q->updateCursorRectangle(true);
}
if (interactionFlags & Qt::LinksAccessibleByMouse) {
@ -1435,7 +1444,6 @@ void QQuickTextControlPrivate::inputMethodEvent(QInputMethodEvent *e)
int blockStart = a.start + cursor.block().position();
cursor.setPosition(blockStart, QTextCursor::MoveAnchor);
cursor.setPosition(blockStart + a.length, QTextCursor::KeepAnchor);
emit q->cursorRectangleChanged();
repaintOldAndNewSelection(oldCursor);
forceSelectionChanged = true;
}
@ -1475,8 +1483,7 @@ void QQuickTextControlPrivate::inputMethodEvent(QInputMethodEvent *e)
QTextCursorPrivate *cursor_d = QTextCursorPrivate::getPrivate(&cursor);
if (cursor_d)
cursor_d->setX();
if (oldPreeditCursor != preeditCursor)
emit q->cursorRectangleChanged();
q->updateCursorRectangle(oldPreeditCursor != preeditCursor || forceSelectionChanged);
selectionChanged(forceSelectionChanged);
}
@ -1609,7 +1616,7 @@ void QQuickTextControl::moveCursor(QTextCursor::MoveOperation op, QTextCursor::M
const QTextCursor oldSelection = d->cursor;
const bool moved = d->cursor.movePosition(op, mode);
d->_q_updateCurrentCharFormatAndSelection();
emit cursorRectangleChanged();
updateCursorRectangle(true);
d->repaintOldAndNewSelection(oldSelection);
if (moved)
emit cursorPositionChanged();
@ -1690,7 +1697,7 @@ void QQuickTextControl::insertFromMimeData(const QMimeData *source)
if (hasData)
d->cursor.insertFragment(fragment);
emit cursorRectangleChanged();
updateCursorRectangle(true);
}
void QQuickTextControlPrivate::activateLinkUnderCursor(QString href)

View File

@ -119,6 +119,8 @@ public:
void setCursorIsFocusIndicator(bool b);
void setWordSelectionEnabled(bool enabled);
void updateCursorRectangle(bool force);
virtual int hitTest(const QPointF &point, Qt::HitTestAccuracy accuracy) const;
virtual QRectF blockBoundingRect(const QTextBlock &block) const;

View File

@ -162,6 +162,7 @@ public:
bool hadSelectionOnMousePress : 1;
bool wordSelectionEnabled : 1;
bool hasImState : 1;
bool cursorRectangleChanged : 1;
void _q_copyLink();
void _q_updateBlock(const QTextBlock &);

View File

@ -897,6 +897,7 @@ void QQuickTextEdit::setCursorPosition(int pos)
return;
cursor.setPosition(pos);
d->control->setTextCursor(cursor);
d->control->updateCursorRectangle(true);
}
/*!
@ -1143,9 +1144,12 @@ void QQuickTextEdit::geometryChanged(const QRectF &newGeometry,
const QRectF &oldGeometry)
{
Q_D(QQuickTextEdit);
if (newGeometry.width() != oldGeometry.width() && d->wrapMode != NoWrap && !d->inLayout)
if (newGeometry.width() != oldGeometry.width() && d->wrapMode != NoWrap && !d->inLayout) {
updateSize();
moveCursorDelegate();
}
QQuickImplicitSizeItem::geometryChanged(newGeometry, oldGeometry);
}
/*!
@ -2169,6 +2173,7 @@ void QQuickTextEdit::insert(int position, const QString &text)
} else {
cursor.insertText(text);
}
d->control->updateCursorRectangle(false);
}
/*!
@ -2186,6 +2191,7 @@ void QQuickTextEdit::remove(int start, int end)
cursor.setPosition(start, QTextCursor::MoveAnchor);
cursor.setPosition(end, QTextCursor::KeepAnchor);
cursor.removeSelectedText();
d->control->updateCursorRectangle(false);
}
QT_END_NAMESPACE

View File

@ -6,6 +6,7 @@ Rectangle { width: 300; height: 300; color: "white"
text: "Hello world!"
id: textEditObject;
objectName: "textEditObject"
wrapMode: TextEdit.WordWrap
cursorDelegate: Cursor {
id:cursorInstance;
objectName: "cursorInstance";

View File

@ -5,6 +5,7 @@ Rectangle { width: 300; height: 300; color: "white"
TextEdit {
text: "Hello world!"
id: textEditObject
wrapMode: TextEdit.Wrap
objectName: "textEditObject"
cursorDelegate: Item {
id:cursorInstance

View File

@ -2,4 +2,8 @@ import QtQuick 2.0
TextEdit {
focus: true
cursorDelegate: Item {
objectName: "cursor"
}
}

View File

@ -1944,10 +1944,6 @@ void tst_qquicktextedit::cursorDelegate()
QCOMPARE(textEditObject->cursorRectangle().x(), delegateObject->x());
QCOMPARE(textEditObject->cursorRectangle().y(), delegateObject->y());
}
// Clear preedit text;
QInputMethodEvent event;
QGuiApplication::sendEvent(&view, &event);
// Test delegate gets moved on mouse press.
textEditObject->setSelectByMouse(true);
@ -1992,6 +1988,25 @@ void tst_qquicktextedit::cursorDelegate()
textEditObject->setCursorPosition(0);
QCOMPARE(textEditObject->cursorRectangle().x(), delegateObject->x());
QCOMPARE(textEditObject->cursorRectangle().y(), delegateObject->y());
// Delegate moved when text is entered
textEditObject->setText(QString());
for (int i = 0; i < 20; ++i) {
QTest::keyClick(&view, Qt::Key_A);
QCOMPARE(textEditObject->cursorRectangle().x(), delegateObject->x());
QCOMPARE(textEditObject->cursorRectangle().y(), delegateObject->y());
}
// Delegate moved when text is entered by im.
textEditObject->setText(QString());
for (int i = 0; i < 20; ++i) {
QInputMethodEvent event;
event.setCommitString("a");
QGuiApplication::sendEvent(&view, &event);
QCOMPARE(textEditObject->cursorRectangle().x(), delegateObject->x());
QCOMPARE(textEditObject->cursorRectangle().y(), delegateObject->y());
}
//Test Delegate gets deleted
textEditObject->setCursorDelegate(0);
QVERIFY(!textEditObject->findChild<QQuickItem*>("cursorInstance"));
@ -2645,14 +2660,17 @@ void tst_qquicktextedit::preeditCursorRectangle()
QQuickTextEdit *edit = qobject_cast<QQuickTextEdit *>(view.rootObject());
QVERIFY(edit);
QQuickItem *cursor = edit->findChild<QQuickItem *>("cursor");
QVERIFY(cursor);
QSignalSpy editSpy(edit, SIGNAL(cursorRectangleChanged()));
QSignalSpy panelSpy(qGuiApp->inputMethod(), SIGNAL(cursorRectangleChanged()));
QRect currentRect;
QRectF currentRect;
QInputMethodQueryEvent query(Qt::ImCursorRectangle);
QCoreApplication::sendEvent(qGuiApp->focusObject(), &query);
QRect previousRect = query.value(Qt::ImCursorRectangle).toRect();
QRectF previousRect = query.value(Qt::ImCursorRectangle).toRectF();
// Verify that the micro focus rect is positioned the same for position 0 as
// it would be if there was no preedit text.
@ -2660,10 +2678,12 @@ void tst_qquicktextedit::preeditCursorRectangle()
<< QInputMethodEvent::Attribute(QInputMethodEvent::Cursor, 0, preeditText.length(), QVariant()));
QCoreApplication::sendEvent(qGuiApp->focusObject(), &imEvent);
QCoreApplication::sendEvent(qGuiApp->focusObject(), &query);
currentRect = query.value(Qt::ImCursorRectangle).toRect();
currentRect = query.value(Qt::ImCursorRectangle).toRectF();
QCOMPARE(edit->cursorRectangle(), currentRect);
QCOMPARE(cursor->pos(), currentRect.topLeft());
QCOMPARE(currentRect, previousRect);
QCOMPARE(editSpy.count(), 0);
QCOMPARE(panelSpy.count(), 0);
QCOMPARE(editSpy.count(), 0); editSpy.clear();
QCOMPARE(panelSpy.count(), 0); panelSpy.clear();
// Verify that the micro focus rect moves to the left as the cursor position
// is incremented.
@ -2672,10 +2692,12 @@ void tst_qquicktextedit::preeditCursorRectangle()
<< QInputMethodEvent::Attribute(QInputMethodEvent::Cursor, i, preeditText.length(), QVariant()));
QCoreApplication::sendEvent(qGuiApp->focusObject(), &imEvent);
QCoreApplication::sendEvent(qGuiApp->focusObject(), &query);
currentRect = query.value(Qt::ImCursorRectangle).toRect();
currentRect = query.value(Qt::ImCursorRectangle).toRectF();
QCOMPARE(edit->cursorRectangle(), currentRect);
QCOMPARE(cursor->pos(), currentRect.topLeft());
QVERIFY(previousRect.left() < currentRect.left());
QVERIFY(editSpy.count() > 0); editSpy.clear();
QVERIFY(panelSpy.count() > 0); panelSpy.clear();
QCOMPARE(editSpy.count(), 1); editSpy.clear();
QCOMPARE(panelSpy.count(), 1); panelSpy.clear();
previousRect = currentRect;
}
@ -2687,10 +2709,12 @@ void tst_qquicktextedit::preeditCursorRectangle()
{ QInputMethodEvent imEvent(preeditText, QList<QInputMethodEvent::Attribute>());
QCoreApplication::sendEvent(qGuiApp->focusObject(), &imEvent); }
QCoreApplication::sendEvent(qGuiApp->focusObject(), &query);
currentRect = query.value(Qt::ImCursorRectangle).toRect();
currentRect = query.value(Qt::ImCursorRectangle).toRectF();
QCOMPARE(edit->cursorRectangle(), currentRect);
QCOMPARE(cursor->pos(), currentRect.topLeft());
QCOMPARE(currentRect, previousRect);
QVERIFY(editSpy.count() > 0);
QVERIFY(panelSpy.count() > 0);
QCOMPARE(editSpy.count(), 1);
QCOMPARE(panelSpy.count(), 1);
}
void tst_qquicktextedit::inputMethodComposing()

View File

@ -6,6 +6,7 @@ Rectangle { width: 300; height: 300; color: "white"
text: "Hello world!"
id: textInputObject;
objectName: "textInputObject"
wrapMode: TextInput.Wrap
cursorDelegate: Cursor {
id:cursorInstance;
objectName: "cursorInstance";

View File

@ -6,6 +6,7 @@ Rectangle { width: 300; height: 300; color: "white"
text: "Hello world!"
id: textInputObject
objectName: "textInputObject"
wrapMode: TextInput.WordWrap
cursorDelegate: Item {
id:cursorInstance
objectName: "cursorInstance"

View File

@ -3,4 +3,6 @@ import QtQuick 2.0
TextInput {
focus: true
autoScroll: false
cursorDelegate: Item { objectName: "cursor" }
}

View File

@ -2359,6 +2359,70 @@ void tst_qquicktextinput::cursorDelegate()
textInputObject->setCursorPosition(0);
QCOMPARE(textInputObject->cursorRectangle().x(), delegateObject->x());
QCOMPARE(textInputObject->cursorRectangle().y(), delegateObject->y());
// Test delegate gets moved on mouse press.
textInputObject->setSelectByMouse(true);
textInputObject->setCursorPosition(0);
const QPoint point1 = textInputObject->positionToRectangle(5).center().toPoint();
QTest::qWait(400); //ensure this isn't treated as a double-click
QTest::mouseClick(&view, Qt::LeftButton, 0, point1);
QTest::qWait(50);
QTRY_VERIFY(textInputObject->cursorPosition() != 0);
QCOMPARE(textInputObject->cursorRectangle().x(), delegateObject->x());
QCOMPARE(textInputObject->cursorRectangle().y(), delegateObject->y());
// Test delegate gets moved on mouse drag
textInputObject->setCursorPosition(0);
const QPoint point2 = textInputObject->positionToRectangle(10).center().toPoint();
QTest::qWait(400); //ensure this isn't treated as a double-click
QTest::mousePress(&view, Qt::LeftButton, 0, point1);
QMouseEvent mv(QEvent::MouseMove, point2, Qt::LeftButton, Qt::LeftButton,Qt::NoModifier);
QGuiApplication::sendEvent(&view, &mv);
QTest::mouseRelease(&view, Qt::LeftButton, 0, point2);
QTest::qWait(50);
QTRY_COMPARE(textInputObject->cursorRectangle().x(), delegateObject->x());
QCOMPARE(textInputObject->cursorRectangle().y(), delegateObject->y());
textInputObject->setReadOnly(true);
textInputObject->setCursorPosition(0);
QTest::qWait(400); //ensure this isn't treated as a double-click
QTest::mouseClick(&view, Qt::LeftButton, 0, textInputObject->positionToRectangle(5).center().toPoint());
QTest::qWait(50);
QTRY_VERIFY(textInputObject->cursorPosition() != 0);
QCOMPARE(textInputObject->cursorRectangle().x(), delegateObject->x());
QCOMPARE(textInputObject->cursorRectangle().y(), delegateObject->y());
textInputObject->setCursorPosition(0);
QTest::qWait(400); //ensure this isn't treated as a double-click
QTest::mouseClick(&view, Qt::LeftButton, 0, textInputObject->positionToRectangle(5).center().toPoint());
QTest::qWait(50);
QTRY_VERIFY(textInputObject->cursorPosition() != 0);
QCOMPARE(textInputObject->cursorRectangle().x(), delegateObject->x());
QCOMPARE(textInputObject->cursorRectangle().y(), delegateObject->y());
textInputObject->setCursorPosition(0);
QCOMPARE(textInputObject->cursorRectangle().x(), delegateObject->x());
QCOMPARE(textInputObject->cursorRectangle().y(), delegateObject->y());
// Delegate moved when text is entered
textInputObject->setText(QString());
for (int i = 0; i < 20; ++i) {
QTest::keyClick(&view, Qt::Key_A);
QCOMPARE(textInputObject->cursorRectangle().x(), delegateObject->x());
QCOMPARE(textInputObject->cursorRectangle().y(), delegateObject->y());
}
// Delegate moved when text is entered by im.
textInputObject->setText(QString());
for (int i = 0; i < 20; ++i) {
QInputMethodEvent event;
event.setCommitString("a");
QGuiApplication::sendEvent(&view, &event);
QCOMPARE(textInputObject->cursorRectangle().x(), delegateObject->x());
QCOMPARE(textInputObject->cursorRectangle().y(), delegateObject->y());
}
//Test Delegate gets deleted
textInputObject->setCursorDelegate(0);
QVERIFY(!textInputObject->findChild<QQuickItem*>("cursorInstance"));
@ -3201,18 +3265,23 @@ void tst_qquicktextinput::preeditCursorRectangle()
QQuickTextInput *input = qobject_cast<QQuickTextInput *>(view.rootObject());
QVERIFY(input);
QRect currentRect;
QQuickItem *cursor = input->findChild<QQuickItem *>("cursor");
QVERIFY(cursor);
QRectF currentRect;
QInputMethodQueryEvent query(Qt::ImCursorRectangle);
QCoreApplication::sendEvent(qGuiApp->focusObject(), &query);
QRect previousRect = query.value(Qt::ImCursorRectangle).toRect();
QRectF previousRect = query.value(Qt::ImCursorRectangle).toRectF();
// Verify that the micro focus rect is positioned the same for position 0 as
// it would be if there was no preedit text.
sendPreeditText(preeditText, 0);
QCoreApplication::sendEvent(qGuiApp->focusObject(), &query);
currentRect = query.value(Qt::ImCursorRectangle).toRect();
currentRect = query.value(Qt::ImCursorRectangle).toRectF();
QCOMPARE(currentRect, previousRect);
QCOMPARE(input->cursorRectangle(), currentRect);
QCOMPARE(cursor->pos(), currentRect.topLeft());
QSignalSpy inputSpy(input, SIGNAL(cursorRectangleChanged()));
QSignalSpy panelSpy(qGuiApp->inputMethod(), SIGNAL(cursorRectangleChanged()));
@ -3222,8 +3291,10 @@ void tst_qquicktextinput::preeditCursorRectangle()
for (int i = 1; i <= 5; ++i) {
sendPreeditText(preeditText, i);
QCoreApplication::sendEvent(qGuiApp->focusObject(), &query);
currentRect = query.value(Qt::ImCursorRectangle).toRect();
currentRect = query.value(Qt::ImCursorRectangle).toRectF();
QVERIFY(previousRect.left() < currentRect.left());
QCOMPARE(input->cursorRectangle(), currentRect);
QCOMPARE(cursor->pos(), currentRect.topLeft());
QVERIFY(inputSpy.count() > 0); inputSpy.clear();
QVERIFY(panelSpy.count() > 0); panelSpy.clear();
previousRect = currentRect;
@ -3235,8 +3306,10 @@ void tst_qquicktextinput::preeditCursorRectangle()
QInputMethodEvent imEvent(preeditText, QList<QInputMethodEvent::Attribute>());
QCoreApplication::sendEvent(qGuiApp->focusObject(), &imEvent);
QCoreApplication::sendEvent(qGuiApp->focusObject(), &query);
currentRect = query.value(Qt::ImCursorRectangle).toRect();
currentRect = query.value(Qt::ImCursorRectangle).toRectF();
QCOMPARE(currentRect, previousRect);
QCOMPARE(input->cursorRectangle(), currentRect);
QCOMPARE(cursor->pos(), currentRect.topLeft());
QVERIFY(inputSpy.count() > 0);
QVERIFY(panelSpy.count() > 0);
}