Emit selectionChanged signals when input method alters the selection.
authorAndrew den Exter <andrew.den-exter@nokia.com>
Mon, 25 Jul 2011 05:48:01 +0000 (15:48 +1000)
committerQt by Nokia <qt-info@nokia.com>
Mon, 25 Jul 2011 23:35:59 +0000 (01:35 +0200)
Check if the input method removes the selection and force emit
selectionChanged if it sets a new selection.

Task-number: QTBUG-19727
Reviewed-by: Martin Jones
Change-Id: Ic8ea1044d0917aac4e52368f431ac9e5c7db7c56
Reviewed-on: http://codereview.qt.nokia.com/2076
Reviewed-by: Qt Sanity Bot <qt_sanity_bot@ovi.com>
Reviewed-by: Andrew den Exter <andrew.den-exter@nokia.com>
src/gui/text/qtextcontrol.cpp
tests/auto/qtextedit/tst_qtextedit.cpp

index aacac04..424d197 100644 (file)
@@ -1918,6 +1918,7 @@ void QTextControlPrivate::inputMethodEvent(QInputMethodEvent *e)
     bool isGettingInput = !e->commitString().isEmpty()
             || e->preeditString() != cursor.block().layout()->preeditAreaText()
             || e->replacementLength() > 0;
+    bool forceSelectionChanged = false;
 
     cursor.beginEditBlock();
     if (isGettingInput) {
@@ -1941,6 +1942,7 @@ void QTextControlPrivate::inputMethodEvent(QInputMethodEvent *e)
             cursor.setPosition(blockStart + a.length, QTextCursor::KeepAnchor);
             q->ensureCursorVisible();
             repaintOldAndNewSelection(oldCursor);
+            forceSelectionChanged = true;
         }
     }
 
@@ -1974,6 +1976,7 @@ void QTextControlPrivate::inputMethodEvent(QInputMethodEvent *e)
         cursor.d->setX();
     if (oldPreeditCursor != preeditCursor)
         emit q->microFocusChanged();
+    selectionChanged(forceSelectionChanged);
 }
 
 QVariant QTextControl::inputMethodQuery(Qt::InputMethodQuery property) const
index 65158d3..7208861 100644 (file)
@@ -207,6 +207,8 @@ private slots:
     void bidiLogicalMovement_data();
     void bidiLogicalMovement();
 
+    void inputMethodSelection();
+
 private:
     void createSelection();
     int blockCount() const;
@@ -2365,5 +2367,30 @@ void tst_QTextEdit::bidiLogicalMovement()
     } while (moved && i >= 0);
 }
 
+void tst_QTextEdit::inputMethodSelection()
+{
+    ed->setText("Lorem ipsum dolor sit amet, consectetur adipiscing elit.");
+
+    QSignalSpy selectionSpy(ed, SIGNAL(selectionChanged()));
+    QTextCursor cursor = ed->textCursor();
+    cursor.setPosition(0);
+    cursor.setPosition(5, QTextCursor::KeepAnchor);
+    ed->setTextCursor(cursor);
+
+    QCOMPARE(selectionSpy.count(), 1);
+    QCOMPARE(ed->textCursor().selectionStart(), 0);
+    QCOMPARE(ed->textCursor().selectionEnd(), 5);
+
+    QList<QInputMethodEvent::Attribute> attributes;
+    attributes << QInputMethodEvent::Attribute(QInputMethodEvent::Selection, 12, 5, QVariant());
+    QInputMethodEvent event("", attributes);
+    QApplication::sendEvent(ed, &event);
+
+    QCOMPARE(selectionSpy.count(), 2);
+    QCOMPARE(ed->textCursor().selectionStart(), 12);
+    QCOMPARE(ed->textCursor().selectionEnd(), 17);
+}
+
+
 QTEST_MAIN(tst_QTextEdit)
 #include "tst_qtextedit.moc"