From: Andrew den Exter Date: Fri, 20 Jan 2012 06:10:23 +0000 (+1000) Subject: Add a persistentSelection property to TextInput. X-Git-Tag: qt-v5.0.0-alpha1~536 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=054a1e942f20e5b64626729f72023ac78dd0ce38;p=profile%2Fivi%2Fqtdeclarative.git Add a persistentSelection property to TextInput. Improves feature parity with TextEdit. Task-number: QTBUG-16355 Change-Id: I3919c71454a4f4574a1ee35ad38969459beb8363 Reviewed-by: Martin Jones --- diff --git a/src/quick/items/qquicktextcontrol.cpp b/src/quick/items/qquicktextcontrol.cpp index 3d4244c..06ab5c3 100644 --- a/src/quick/items/qquicktextcontrol.cpp +++ b/src/quick/items/qquicktextcontrol.cpp @@ -1547,6 +1547,7 @@ void QQuickTextControlPrivate::focusEvent(QFocusEvent *e) { Q_Q(QQuickTextControl); emit q->updateRequest(q->selectionRect()); + hasFocus = e->gotFocus(); if (e->gotFocus()) { setBlinkingCursorEnabled(interactionFlags & (Qt::TextEditable | Qt::TextSelectableByKeyboard)); } else { @@ -1558,9 +1559,9 @@ void QQuickTextControlPrivate::focusEvent(QFocusEvent *e) && e->reason() != Qt::PopupFocusReason && cursor.hasSelection()) { cursor.clearSelection(); + emit q->selectionChanged(); } } - hasFocus = e->gotFocus(); } QString QQuickTextControlPrivate::anchorForCursor(const QTextCursor &anchorCursor) const diff --git a/src/quick/items/qquicktextedit.cpp b/src/quick/items/qquicktextedit.cpp index f544226..4d5776d 100644 --- a/src/quick/items/qquicktextedit.cpp +++ b/src/quick/items/qquicktextedit.cpp @@ -1806,6 +1806,7 @@ void QQuickTextEditPrivate::init() control->setView(q); control->setTextInteractionFlags(Qt::LinksAccessibleByMouse | Qt::TextSelectableByKeyboard | Qt::TextEditable); control->setAcceptRichText(false); + control->setCursorIsFocusIndicator(true); // QQuickTextControl follows the default text color // defined by the platform, declarative text diff --git a/src/quick/items/qquicktextedit_p_p.h b/src/quick/items/qquicktextedit_p_p.h index 6a4cd9f..41b64cc 100644 --- a/src/quick/items/qquicktextedit_p_p.h +++ b/src/quick/items/qquicktextedit_p_p.h @@ -72,7 +72,7 @@ public: QQuickTextEditPrivate() : color("black"), hAlign(QQuickTextEdit::AlignLeft), vAlign(QQuickTextEdit::AlignTop), documentDirty(true), dirty(false), richText(false), cursorVisible(false), focusOnPress(true), - persistentSelection(true), requireImplicitWidth(false), selectByMouse(false), canPaste(false), + persistentSelection(false), requireImplicitWidth(false), selectByMouse(false), canPaste(false), canPasteValid(false), hAlignImplicit(true), rightToLeftText(false), useImageFallback(false), textCached(false), textMargin(0.0), lastSelectionStart(0), lastSelectionEnd(0), cursorComponent(0), cursor(0), diff --git a/src/quick/items/qquicktextinput.cpp b/src/quick/items/qquicktextinput.cpp index 3007ce1..5311b2d 100644 --- a/src/quick/items/qquicktextinput.cpp +++ b/src/quick/items/qquicktextinput.cpp @@ -2185,6 +2185,28 @@ void QQuickTextInput::setMouseSelectionMode(SelectionMode mode) } /*! + \qmlproperty bool QtQuick2::TextInput::persistentSelection + + Whether the TextInput should keep its selection when it loses active focus to another + item in the scene. By default this is set to false; +*/ + +bool QQuickTextInput::persistentSelection() const +{ + Q_D(const QQuickTextInput); + return d->persistentSelection; +} + +void QQuickTextInput::setPersistentSelection(bool on) +{ + Q_D(QQuickTextInput); + if (d->persistentSelection == on) + return; + d->persistentSelection = on; + emit persistentSelectionChanged(); +} + +/*! \qmlproperty bool QtQuick2::TextInput::canPaste Returns true if the TextInput is writable and the content of the clipboard is @@ -2438,7 +2460,8 @@ void QQuickTextInput::itemChange(ItemChange change, const ItemChangeData &value) if (!hasFocus) { d->commitPreedit(); - d->deselect(); + if (!d->persistentSelection) + d->deselect(); disconnect(qApp->inputPanel(), SIGNAL(inputDirectionChanged(Qt::LayoutDirection)), this, SLOT(q_updateAlignment())); } else { diff --git a/src/quick/items/qquicktextinput_p.h b/src/quick/items/qquicktextinput_p.h index 3f3e926..70a2f0a 100644 --- a/src/quick/items/qquicktextinput_p.h +++ b/src/quick/items/qquicktextinput_p.h @@ -98,6 +98,7 @@ class Q_AUTOTEST_EXPORT QQuickTextInput : public QQuickImplicitSizeItem Q_PROPERTY(bool autoScroll READ autoScroll WRITE setAutoScroll NOTIFY autoScrollChanged) Q_PROPERTY(bool selectByMouse READ selectByMouse WRITE setSelectByMouse NOTIFY selectByMouseChanged) Q_PROPERTY(SelectionMode mouseSelectionMode READ mouseSelectionMode WRITE setMouseSelectionMode NOTIFY mouseSelectionModeChanged) + Q_PROPERTY(bool persistentSelection READ persistentSelection WRITE setPersistentSelection NOTIFY persistentSelectionChanged) Q_PROPERTY(bool canPaste READ canPaste NOTIFY canPasteChanged) Q_PROPERTY(bool canUndo READ canUndo NOTIFY canUndoChanged) Q_PROPERTY(bool canRedo READ canRedo NOTIFY canRedoChanged) @@ -233,6 +234,9 @@ public: SelectionMode mouseSelectionMode() const; void setMouseSelectionMode(SelectionMode mode); + bool persistentSelection() const; + void setPersistentSelection(bool persist); + bool hasAcceptableInput() const; QVariant inputMethodQuery(Qt::InputMethodQuery property) const; @@ -279,6 +283,7 @@ Q_SIGNALS: void autoScrollChanged(bool autoScroll); void selectByMouseChanged(bool selectByMouse); void mouseSelectionModeChanged(SelectionMode mode); + void persistentSelectionChanged(); void canPasteChanged(); void canUndoChanged(); void canRedoChanged(); diff --git a/src/quick/items/qquicktextinput_p_p.h b/src/quick/items/qquicktextinput_p_p.h index 40cba8d..a124dbb 100644 --- a/src/quick/items/qquicktextinput_p_p.h +++ b/src/quick/items/qquicktextinput_p_p.h @@ -117,6 +117,7 @@ public: , hAlignImplicit(true) , selectPressed(false) , textLayoutDirty(true) + , persistentSelection(false) , m_hideCursor(false) , m_separator(0) , m_readOnly(0) @@ -244,6 +245,7 @@ public: bool hAlignImplicit:1; bool selectPressed:1; bool textLayoutDirty:1; + bool persistentSelection:1; uint m_hideCursor : 1; // used to hide the m_cursor inside preedit areas uint m_separator : 1; diff --git a/tests/auto/qtquick2/qquicktextedit/data/persistentSelection.qml b/tests/auto/qtquick2/qquicktextedit/data/persistentSelection.qml new file mode 100644 index 0000000..fb2fe0c --- /dev/null +++ b/tests/auto/qtquick2/qquicktextedit/data/persistentSelection.qml @@ -0,0 +1,8 @@ +import QtQuick 2.0 + +TextEdit { + property string selected: selectedText + + text: "Hello World!" + focus: true +} diff --git a/tests/auto/qtquick2/qquicktextedit/tst_qquicktextedit.cpp b/tests/auto/qtquick2/qquicktextedit/tst_qquicktextedit.cpp index bcf4f60..07b8f10 100644 --- a/tests/auto/qtquick2/qquicktextedit/tst_qquicktextedit.cpp +++ b/tests/auto/qtquick2/qquicktextedit/tst_qquicktextedit.cpp @@ -946,23 +946,47 @@ void tst_qquicktextedit::textMargin() void tst_qquicktextedit::persistentSelection() { - { - QString componentStr = "import QtQuick 2.0\nTextEdit { persistentSelection: true; text: \"Hello World\" }"; - QDeclarativeComponent texteditComponent(&engine); - texteditComponent.setData(componentStr.toLatin1(), QUrl()); - QQuickTextEdit *textEditObject = qobject_cast(texteditComponent.create()); - QVERIFY(textEditObject != 0); - QCOMPARE(textEditObject->persistentSelection(), true); - } + QQuickView canvas(testFileUrl("persistentSelection.qml")); + canvas.show(); + canvas.requestActivateWindow(); + QTest::qWaitForWindowShown(&canvas); + QTRY_COMPARE(&canvas, qGuiApp->focusWindow()); + canvas.requestActivateWindow(); + + QQuickTextEdit *edit = qobject_cast(canvas.rootObject()); + QVERIFY(edit); + QVERIFY(edit->hasActiveFocus()); + + QSignalSpy spy(edit, SIGNAL(persistentSelectionChanged(bool))); + + QCOMPARE(edit->persistentSelection(), false); + + edit->setPersistentSelection(false); + QCOMPARE(edit->persistentSelection(), false); + QCOMPARE(spy.count(), 0); + + edit->select(1, 4); + QCOMPARE(edit->property("selected").toString(), QLatin1String("ell")); + + edit->setFocus(false); + QCOMPARE(edit->property("selected").toString(), QString()); + + edit->setFocus(true); + QCOMPARE(edit->property("selected").toString(), QString()); + + edit->setPersistentSelection(true); + QCOMPARE(edit->persistentSelection(), true); + QCOMPARE(spy.count(), 1); + + edit->select(1, 4); + QCOMPARE(edit->property("selected").toString(), QLatin1String("ell")); + + edit->setFocus(false); + QCOMPARE(edit->property("selected").toString(), QLatin1String("ell")); + + edit->setFocus(true); + QCOMPARE(edit->property("selected").toString(), QLatin1String("ell")); - { - QString componentStr = "import QtQuick 2.0\nTextEdit { persistentSelection: false; text: \"Hello World\" }"; - QDeclarativeComponent texteditComponent(&engine); - texteditComponent.setData(componentStr.toLatin1(), QUrl()); - QQuickTextEdit *textEditObject = qobject_cast(texteditComponent.create()); - QVERIFY(textEditObject != 0); - QCOMPARE(textEditObject->persistentSelection(), false); - } } void tst_qquicktextedit::focusOnPress() diff --git a/tests/auto/qtquick2/qquicktextinput/data/persistentSelection.qml b/tests/auto/qtquick2/qquicktextinput/data/persistentSelection.qml new file mode 100644 index 0000000..dea6e48 --- /dev/null +++ b/tests/auto/qtquick2/qquicktextinput/data/persistentSelection.qml @@ -0,0 +1,8 @@ +import QtQuick 2.0 + +TextInput { + property string selected: selectedText + + text: "Hello World!" + focus: true +} diff --git a/tests/auto/qtquick2/qquicktextinput/tst_qquicktextinput.cpp b/tests/auto/qtquick2/qquicktextinput/tst_qquicktextinput.cpp index 13936d4..5e45214 100644 --- a/tests/auto/qtquick2/qquicktextinput/tst_qquicktextinput.cpp +++ b/tests/auto/qtquick2/qquicktextinput/tst_qquicktextinput.cpp @@ -109,6 +109,7 @@ private slots: void color(); void wrap(); void selection(); + void persistentSelection(); void isRightToLeft_data(); void isRightToLeft(); void moveCursorSelection_data(); @@ -628,6 +629,50 @@ void tst_qquicktextinput::selection() delete textinputObject; } +void tst_qquicktextinput::persistentSelection() +{ + QQuickView canvas(testFileUrl("persistentSelection.qml")); + canvas.show(); + canvas.requestActivateWindow(); + QTest::qWaitForWindowShown(&canvas); + QTRY_COMPARE(&canvas, qGuiApp->focusWindow()); + canvas.requestActivateWindow(); + + QQuickTextInput *input = qobject_cast(canvas.rootObject()); + QVERIFY(input); + QVERIFY(input->hasActiveFocus()); + + QSignalSpy spy(input, SIGNAL(persistentSelectionChanged())); + + QCOMPARE(input->persistentSelection(), false); + + input->setPersistentSelection(false); + QCOMPARE(input->persistentSelection(), false); + QCOMPARE(spy.count(), 0); + + input->select(1, 4); + QCOMPARE(input->property("selected").toString(), QLatin1String("ell")); + + input->setFocus(false); + QCOMPARE(input->property("selected").toString(), QString()); + + input->setFocus(true); + QCOMPARE(input->property("selected").toString(), QString()); + + input->setPersistentSelection(true); + QCOMPARE(input->persistentSelection(), true); + QCOMPARE(spy.count(), 1); + + input->select(1, 4); + QCOMPARE(input->property("selected").toString(), QLatin1String("ell")); + + input->setFocus(false); + QCOMPARE(input->property("selected").toString(), QLatin1String("ell")); + + input->setFocus(true); + QCOMPARE(input->property("selected").toString(), QLatin1String("ell")); +} + void tst_qquicktextinput::isRightToLeft_data() { QTest::addColumn("text");