summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlan Alpert <alan.alpert@nokia.com>2010-02-08 15:09:00 (GMT)
committerAlan Alpert <alan.alpert@nokia.com>2010-02-08 15:09:00 (GMT)
commita764c32ee5cab4b40a0ce6807e1e7c3efcc7bdc8 (patch)
treed863b90b7ca56df159593d2a74c35000097491df
parent3e8e1b369752d8a0b42f088f577f3d33eb29f8a9 (diff)
downloadQt-a764c32ee5cab4b40a0ce6807e1e7c3efcc7bdc8.zip
Qt-a764c32ee5cab4b40a0ce6807e1e7c3efcc7bdc8.tar.gz
Qt-a764c32ee5cab4b40a0ce6807e1e7c3efcc7bdc8.tar.bz2
Support the other validators
Note that a resolution will be required for QTBUG-8025 before the QRegExpValidator is useful. Task-number: QTBUG-7068
-rw-r--r--src/declarative/graphicsitems/qmlgraphicstextinput.cpp21
-rw-r--r--src/declarative/graphicsitems/qmlgraphicstextinput_p.h2
-rw-r--r--tests/auto/declarative/qmlgraphicstextinput/tst_qmlgraphicstextinput.cpp95
3 files changed, 112 insertions, 6 deletions
diff --git a/src/declarative/graphicsitems/qmlgraphicstextinput.cpp b/src/declarative/graphicsitems/qmlgraphicstextinput.cpp
index 427f9ff..6d79c7a 100644
--- a/src/declarative/graphicsitems/qmlgraphicstextinput.cpp
+++ b/src/declarative/graphicsitems/qmlgraphicstextinput.cpp
@@ -54,6 +54,8 @@ QT_BEGIN_NAMESPACE
QML_DEFINE_TYPE(Qt,4,6,TextInput,QmlGraphicsTextInput);
QML_DEFINE_NOCREATE_TYPE(QValidator);
QML_DEFINE_TYPE(Qt,4,6,QIntValidator,QIntValidator);
+QML_DEFINE_TYPE(Qt,4,6,QDoubleValidator,QDoubleValidator);
+QML_DEFINE_TYPE(Qt,4,6,QRegExpValidator,QRegExpValidator);
/*!
\qmlclass TextInput QmlGraphicsTextInput
@@ -437,6 +439,25 @@ void QmlGraphicsTextInput::setFocusOnPress(bool b)
an acceptable or intermediate state. The accepted signal will only be sent
if the text is in an acceptable state when enter is pressed.
+ Currently supported validators are QIntValidator, QDoubleValidator and
+ QRegExpValidator. For details, refer to their C++ documentation and remember
+ that all Q_PROPERTIES are accessible from Qml. A brief usage guide follows:
+
+ QIntValidator and QDoubleValidator both are controllable through two properties,
+ top and bottom. The difference is that for QIntValidator the top and bottom properties
+ should be integers, and for QDoubleValidator they should be doubles. QRegExpValidator
+ has a single string property, regExp, which should be set to the regular expression to
+ be used for validation. An example of using validators is shown below, which allows
+ input of integers between 11 and 31 into the text input:
+
+ \code
+ import Qt 4.6
+ TextInput{
+ validator: QIntValidator{bottom: 11; top: 31;}
+ focus: true
+ }
+ \endcode
+
\sa acceptableInput, inputMask
*/
QValidator* QmlGraphicsTextInput::validator() const
diff --git a/src/declarative/graphicsitems/qmlgraphicstextinput_p.h b/src/declarative/graphicsitems/qmlgraphicstextinput_p.h
index 56f16a5..68f28f8 100644
--- a/src/declarative/graphicsitems/qmlgraphicstextinput_p.h
+++ b/src/declarative/graphicsitems/qmlgraphicstextinput_p.h
@@ -220,6 +220,8 @@ QT_END_NAMESPACE
QML_DECLARE_TYPE(QmlGraphicsTextInput)
QML_DECLARE_TYPE(QValidator)
QML_DECLARE_TYPE(QIntValidator)
+QML_DECLARE_TYPE(QDoubleValidator)
+QML_DECLARE_TYPE(QRegExpValidator)
QT_END_HEADER
diff --git a/tests/auto/declarative/qmlgraphicstextinput/tst_qmlgraphicstextinput.cpp b/tests/auto/declarative/qmlgraphicstextinput/tst_qmlgraphicstextinput.cpp
index 7223ff9..7896464 100644
--- a/tests/auto/declarative/qmlgraphicstextinput/tst_qmlgraphicstextinput.cpp
+++ b/tests/auto/declarative/qmlgraphicstextinput/tst_qmlgraphicstextinput.cpp
@@ -397,13 +397,96 @@ void tst_qmlgraphicstextinput::masks()
void tst_qmlgraphicstextinput::validators()
{
- QString componentStr = "import Qt 4.6\nTextInput { maximumLength: 10; }";
- QmlComponent textinputComponent(&engine);
- textinputComponent.setData(componentStr.toLatin1(), QUrl());
- QmlGraphicsTextInput *textinputObject = qobject_cast<QmlGraphicsTextInput*>(textinputComponent.create());
- QVERIFY(textinputObject != 0);
+ // Note that this test assumes that the validators are working properly
+ // so you may need to run their tests first. All validators are checked
+ // here to ensure that their exposure to QML is working.
+
+ QmlView *canvas = createView(SRCDIR "/data/validators.qml");
+ canvas->execute();
+ canvas->show();
+ canvas->setFocus();
+
+ QVERIFY(canvas->root() != 0);
- //TODO: Me
+ QmlGraphicsTextInput *intInput = qobject_cast<QmlGraphicsTextInput *>(qvariant_cast<QObject *>(canvas->root()->property("intInput")));
+ QVERIFY(intInput);
+ intInput->setFocus(true);
+ QTRY_VERIFY(intInput->hasFocus());
+ QTest::keyPress(canvas, Qt::Key_1);
+ QTest::keyRelease(canvas, Qt::Key_1, Qt::NoModifier ,10);
+ QCOMPARE(intInput->text(), QLatin1String("1"));
+ QCOMPARE(intInput->hasAcceptableInput(), false);
+ QTest::keyPress(canvas, Qt::Key_2);
+ QTest::keyRelease(canvas, Qt::Key_2, Qt::NoModifier ,10);
+ QCOMPARE(intInput->text(), QLatin1String("1"));
+ QCOMPARE(intInput->hasAcceptableInput(), false);
+ QTest::keyPress(canvas, Qt::Key_1);
+ QTest::keyRelease(canvas, Qt::Key_1, Qt::NoModifier ,10);
+ QCOMPARE(intInput->text(), QLatin1String("11"));
+ QCOMPARE(intInput->hasAcceptableInput(), true);
+ QTest::keyPress(canvas, Qt::Key_0);
+ QTest::keyRelease(canvas, Qt::Key_0, Qt::NoModifier ,10);
+ QCOMPARE(intInput->text(), QLatin1String("11"));
+ QCOMPARE(intInput->hasAcceptableInput(), true);
+
+ QmlGraphicsTextInput *dblInput = qobject_cast<QmlGraphicsTextInput *>(qvariant_cast<QObject *>(canvas->root()->property("dblInput")));
+ QTRY_VERIFY(dblInput);
+ dblInput->setFocus(true);
+ QVERIFY(dblInput->hasFocus() == true);
+ QTest::keyPress(canvas, Qt::Key_1);
+ QTest::keyRelease(canvas, Qt::Key_1, Qt::NoModifier ,10);
+ QCOMPARE(dblInput->text(), QLatin1String("1"));
+ QCOMPARE(dblInput->hasAcceptableInput(), false);
+ QTest::keyPress(canvas, Qt::Key_2);
+ QTest::keyRelease(canvas, Qt::Key_2, Qt::NoModifier ,10);
+ QCOMPARE(dblInput->text(), QLatin1String("12"));
+ QCOMPARE(dblInput->hasAcceptableInput(), true);
+ QTest::keyPress(canvas, Qt::Key_Period);
+ QTest::keyRelease(canvas, Qt::Key_Period, Qt::NoModifier ,10);
+ QCOMPARE(dblInput->text(), QLatin1String("12."));
+ QCOMPARE(dblInput->hasAcceptableInput(), true);
+ QTest::keyPress(canvas, Qt::Key_1);
+ QTest::keyRelease(canvas, Qt::Key_1, Qt::NoModifier ,10);
+ QCOMPARE(dblInput->text(), QLatin1String("12.1"));
+ QCOMPARE(dblInput->hasAcceptableInput(), true);
+ QTest::keyPress(canvas, Qt::Key_1);
+ QTest::keyRelease(canvas, Qt::Key_1, Qt::NoModifier ,10);
+ QCOMPARE(dblInput->text(), QLatin1String("12.11"));
+ QCOMPARE(dblInput->hasAcceptableInput(), true);
+ QTest::keyPress(canvas, Qt::Key_1);
+ QTest::keyRelease(canvas, Qt::Key_1, Qt::NoModifier ,10);
+ QCOMPARE(dblInput->text(), QLatin1String("12.11"));
+ QCOMPARE(dblInput->hasAcceptableInput(), true);
+
+ QmlGraphicsTextInput *strInput = qobject_cast<QmlGraphicsTextInput *>(qvariant_cast<QObject *>(canvas->root()->property("strInput")));
+ QTRY_VERIFY(strInput);
+ strInput->setFocus(true);
+ QVERIFY(strInput->hasFocus() == true);
+ QTest::keyPress(canvas, Qt::Key_1);
+ QTest::keyRelease(canvas, Qt::Key_1, Qt::NoModifier ,10);
+ QEXPECT_FAIL("","Will not work until QTBUG-8025 is resolved", Abort);
+ QCOMPARE(strInput->text(), QLatin1String(""));
+ QCOMPARE(strInput->hasAcceptableInput(), false);
+ QTest::keyPress(canvas, Qt::Key_A);
+ QTest::keyRelease(canvas, Qt::Key_A, Qt::NoModifier ,10);
+ QCOMPARE(strInput->text(), QLatin1String("a"));
+ QCOMPARE(strInput->hasAcceptableInput(), false);
+ QTest::keyPress(canvas, Qt::Key_A);
+ QTest::keyRelease(canvas, Qt::Key_A, Qt::NoModifier ,10);
+ QCOMPARE(strInput->text(), QLatin1String("aa"));
+ QCOMPARE(strInput->hasAcceptableInput(), true);
+ QTest::keyPress(canvas, Qt::Key_A);
+ QTest::keyRelease(canvas, Qt::Key_A, Qt::NoModifier ,10);
+ QCOMPARE(strInput->text(), QLatin1String("aaa"));
+ QCOMPARE(strInput->hasAcceptableInput(), true);
+ QTest::keyPress(canvas, Qt::Key_A);
+ QTest::keyRelease(canvas, Qt::Key_A, Qt::NoModifier ,10);
+ QCOMPARE(strInput->text(), QLatin1String("aaaa"));
+ QCOMPARE(strInput->hasAcceptableInput(), true);
+ QTest::keyPress(canvas, Qt::Key_A);
+ QTest::keyRelease(canvas, Qt::Key_A, Qt::NoModifier ,10);
+ QCOMPARE(strInput->text(), QLatin1String("aaaa"));
+ QCOMPARE(strInput->hasAcceptableInput(), true);
}
/*