From a2c0390468b91a3f8a97a3bbabc2f9c98e0d105a Mon Sep 17 00:00:00 2001 From: Charles Yin Date: Tue, 26 Jul 2011 13:46:54 +1000 Subject: Add notify signals for QIntvalidator, QDoubleValidator, QRegExpValidator Task-number:QTBUG-19956 Change-Id: I5ab5e4494189ece5b0eb1f63e73e49cb2c4e9656 Reviewed-by:Michael Brasser Reviewed-on: http://codereview.qt.nokia.com/2147 Reviewed-by: Qt Sanity Bot Reviewed-by: Michael Brasser --- src/gui/widgets/qvalidator.cpp | 101 +++++++++++++++++++-- src/gui/widgets/qvalidator.h | 27 ++++-- .../auto/qdoublevalidator/tst_qdoublevalidator.cpp | 57 ++++++++++++ tests/auto/qintvalidator/tst_qintvalidator.cpp | 33 ++++++- .../auto/qregexpvalidator/tst_qregexpvalidator.cpp | 3 + 5 files changed, 204 insertions(+), 17 deletions(-) diff --git a/src/gui/widgets/qvalidator.cpp b/src/gui/widgets/qvalidator.cpp index b32bea8b7a..43251a8829 100644 --- a/src/gui/widgets/qvalidator.cpp +++ b/src/gui/widgets/qvalidator.cpp @@ -131,6 +131,69 @@ QT_BEGIN_NAMESPACE \omitvalue Valid */ + +/*! + \fn void QIntValidator::topChanged(int top) + + This signal is emitted after the top property changed. + + \sa QIntValidator::top(), QIntValidator::setTop(), QIntValidator::bottom(), QIntValidator::setBottom() + \internal +*/ + +/*! + \fn void QIntValidator::bottomChanged(int bottom) + + This signal is emitted after the bottom property changed. + + \sa QIntValidator::top(), QIntValidator::setTop(), QIntValidator::bottom(), QIntValidator::setBottom() + \internal +*/ + +/*! + \fn void QDoubleValidator::topChanged(int top) + + This signal is emitted after the top property changed. + + \sa QDoubleValidator::top(), QDoubleValidator::setTop(), QDoubleValidator::bottom(), QDoubleValidator::setBottom() + \internal +*/ + +/*! + \fn void QDoubleValidator::bottomChanged(int bottom) + + This signal is emitted after the bottom property changed. + + \sa QDoubleValidator::top(), QDoubleValidator::setTop(), QDoubleValidator::bottom(), QDoubleValidator::setBottom() + \internal +*/ + +/*! + \fn void QDoubleValidator::decimalsChanged(int decimals) + + This signal is emitted after the decimals property changed. + + \internal +*/ + +/*! + \fn void QDoubleValidator::notationChanged(QDoubleValidator::Notation notation) + + This signal is emitted after the notation property changed. + + QDoubleValidator::Notation is not a registered metatype, so for queued connections, + you will have to register it with Q_DECLARE_METATYPE() and qRegisterMetaType(). + + \internal +*/ + +/*! + \fn void QRegExpValidator::regExpChanged(const QRegExp ®Exp) + + This signal is emitted after the regExp property changed. + \internal +*/ + class QValidatorPrivate : public QObjectPrivate{ Q_DECLARE_PUBLIC(QValidator) public: @@ -436,8 +499,15 @@ void QIntValidator::fixup(QString &input) const void QIntValidator::setRange(int bottom, int top) { - b = bottom; - t = top; + if (b != bottom) { + b = bottom; + emit bottomChanged(b); + } + + if (t != top) { + t = top; + emit topChanged(t); + } } @@ -710,9 +780,20 @@ QValidator::State QDoubleValidatorPrivate::validateWithLocale(QString &input, QL void QDoubleValidator::setRange(double minimum, double maximum, int decimals) { - b = minimum; - t = maximum; - dec = decimals; + if (b != minimum) { + b = minimum; + emit bottomChanged(b); + } + + if (t != maximum) { + t = maximum; + emit topChanged(t); + } + + if (dec != decimals) { + dec = decimals; + emit decimalsChanged(dec); + } } /*! @@ -771,7 +852,10 @@ void QDoubleValidator::setDecimals(int decimals) void QDoubleValidator::setNotation(Notation newNotation) { Q_D(QDoubleValidator); - d->notation = newNotation; + if (d->notation != newNotation) { + d->notation = newNotation; + emit notationChanged(d->notation); + } } QDoubleValidator::Notation QDoubleValidator::notation() const @@ -915,7 +999,10 @@ QValidator::State QRegExpValidator::validate(QString &input, int& pos) const void QRegExpValidator::setRegExp(const QRegExp& rx) { - r = rx; + if (r != rx) { + r = rx; + emit regExpChanged(r); + } } #endif diff --git a/src/gui/widgets/qvalidator.h b/src/gui/widgets/qvalidator.h index 70f656e6a8..cb0436cc3e 100644 --- a/src/gui/widgets/qvalidator.h +++ b/src/gui/widgets/qvalidator.h @@ -96,8 +96,8 @@ private: class Q_GUI_EXPORT QIntValidator : public QValidator { Q_OBJECT - Q_PROPERTY(int bottom READ bottom WRITE setBottom) - Q_PROPERTY(int top READ top WRITE setTop) + Q_PROPERTY(int bottom READ bottom WRITE setBottom NOTIFY bottomChanged) + Q_PROPERTY(int top READ top WRITE setTop NOTIFY topChanged) public: explicit QIntValidator(QObject * parent = 0); @@ -113,7 +113,9 @@ public: int bottom() const { return b; } int top() const { return t; } - +Q_SIGNALS: + void bottomChanged(int bottom); + void topChanged(int top); #ifdef QT3_SUPPORT public: QT3_SUPPORT_CONSTRUCTOR QIntValidator(QObject * parent, const char *name); @@ -134,11 +136,11 @@ class QDoubleValidatorPrivate; class Q_GUI_EXPORT QDoubleValidator : public QValidator { Q_OBJECT - Q_PROPERTY(double bottom READ bottom WRITE setBottom) - Q_PROPERTY(double top READ top WRITE setTop) - Q_PROPERTY(int decimals READ decimals WRITE setDecimals) + Q_PROPERTY(double bottom READ bottom WRITE setBottom NOTIFY bottomChanged) + Q_PROPERTY(double top READ top WRITE setTop NOTIFY topChanged) + Q_PROPERTY(int decimals READ decimals WRITE setDecimals NOTIFY decimalsChanged) Q_ENUMS(Notation) - Q_PROPERTY(Notation notation READ notation WRITE setNotation) + Q_PROPERTY(Notation notation READ notation WRITE setNotation NOTIFY notationChanged) public: explicit QDoubleValidator(QObject * parent = 0); @@ -149,7 +151,6 @@ public: StandardNotation, ScientificNotation }; - QValidator::State validate(QString &, int &) const; virtual void setRange(double bottom, double top, int decimals = 0); @@ -163,6 +164,12 @@ public: int decimals() const { return dec; } Notation notation() const; +Q_SIGNALS: + void bottomChanged(double bottom); + void topChanged(double top); + void decimalsChanged(int decimals); + void notationChanged(QDoubleValidator::Notation notation); + #ifdef QT3_SUPPORT public: QT3_SUPPORT_CONSTRUCTOR QDoubleValidator(QObject * parent, const char *name); @@ -182,7 +189,7 @@ private: class Q_GUI_EXPORT QRegExpValidator : public QValidator { Q_OBJECT - Q_PROPERTY(QRegExp regExp READ regExp WRITE setRegExp) + Q_PROPERTY(QRegExp regExp READ regExp WRITE setRegExp NOTIFY regExpChanged) public: explicit QRegExpValidator(QObject *parent = 0); @@ -194,6 +201,8 @@ public: void setRegExp(const QRegExp& rx); const QRegExp& regExp() const { return r; } // ### make inline for 5.0 +Q_SIGNALS: + void regExpChanged(const QRegExp& regExp); #ifdef QT3_SUPPORT public: QT3_SUPPORT_CONSTRUCTOR QRegExpValidator(QObject *parent, const char *name); diff --git a/tests/auto/qdoublevalidator/tst_qdoublevalidator.cpp b/tests/auto/qdoublevalidator/tst_qdoublevalidator.cpp index 76d4494a39..6014ce5165 100644 --- a/tests/auto/qdoublevalidator/tst_qdoublevalidator.cpp +++ b/tests/auto/qdoublevalidator/tst_qdoublevalidator.cpp @@ -55,6 +55,7 @@ private slots: void validateThouSep(); void validateIntEquiv_data(); void validateIntEquiv(); + void notifySignals(); }; Q_DECLARE_METATYPE(QValidator::State); @@ -244,6 +245,62 @@ void tst_QDoubleValidator::validate() dv.setNotation(QDoubleValidator::StandardNotation); QCOMPARE((int)dv.validate(value, dummy), (int)standard_state); } +void tst_QDoubleValidator::notifySignals() +{ + QDoubleValidator dv(0.1, 0.9, 10, 0); + QSignalSpy topSpy(&dv, SIGNAL(topChanged(double))); + QSignalSpy bottomSpy(&dv, SIGNAL(bottomChanged(double))); + QSignalSpy decSpy(&dv, SIGNAL(decimalsChanged(int))); + + qRegisterMetaType("QDoubleValidator::Notation"); + QSignalSpy notSpy(&dv, SIGNAL(notationChanged(QDoubleValidator::Notation))); + + dv.setTop(0.8); + QCOMPARE(topSpy.count(), 1); + QVERIFY(dv.top() == 0.8); + dv.setBottom(0.2); + QCOMPARE(bottomSpy.count(), 1); + QVERIFY(dv.bottom() == 0.2); + + dv.setRange(0.2, 0.7); + QCOMPARE(topSpy.count(), 2); + QCOMPARE(bottomSpy.count(), 1); + QCOMPARE(decSpy.count(), 1); + QVERIFY(dv.bottom() == 0.2); + QVERIFY(dv.top() == 0.7); + QVERIFY(dv.decimals() == 0.); + + dv.setRange(0.3, 0.7); + QCOMPARE(topSpy.count(), 2); + QCOMPARE(bottomSpy.count(), 2); + QVERIFY(dv.bottom() == 0.3); + QVERIFY(dv.top() == 0.7); + QVERIFY(dv.decimals() == 0.); + + dv.setRange(0.4, 0.6); + QCOMPARE(topSpy.count(), 3); + QCOMPARE(bottomSpy.count(), 3); + QVERIFY(dv.bottom() == 0.4); + QVERIFY(dv.top() == 0.6); + QVERIFY(dv.decimals() == 0.); + + dv.setDecimals(10); + QCOMPARE(decSpy.count(), 2); + QVERIFY(dv.decimals() == 10.); + + + dv.setRange(0.4, 0.6, 100); + QCOMPARE(topSpy.count(), 3); + QCOMPARE(bottomSpy.count(), 3); + QCOMPARE(decSpy.count(), 3); + QVERIFY(dv.bottom() == 0.4); + QVERIFY(dv.top() == 0.6); + QVERIFY(dv.decimals() == 100.); + + dv.setNotation(QDoubleValidator::StandardNotation); + QCOMPARE(notSpy.count(), 1); + QVERIFY(dv.notation() == QDoubleValidator::StandardNotation); +} void tst_QDoubleValidator::validateIntEquiv_data() { diff --git a/tests/auto/qintvalidator/tst_qintvalidator.cpp b/tests/auto/qintvalidator/tst_qintvalidator.cpp index d53763566d..369e3254e8 100644 --- a/tests/auto/qintvalidator/tst_qintvalidator.cpp +++ b/tests/auto/qintvalidator/tst_qintvalidator.cpp @@ -51,6 +51,7 @@ private slots: void validate(); void validateArabic(); void validateFrench(); + void notifySignals(); }; Q_DECLARE_METATYPE(QValidator::State); @@ -189,7 +190,6 @@ void tst_QIntValidator::validateFrench() QIntValidator validator(-2000, 2000, 0); validator.setLocale(QLocale::French); int i; - QString s = QLatin1String("1 "); QCOMPARE(validator.validate(s, i), QValidator::Acceptable); validator.fixup(s); @@ -220,5 +220,36 @@ void tst_QIntValidator::validate() QCOMPARE((int)iv.validate(value, dummy), (int)state); } +void tst_QIntValidator::notifySignals() +{ + QIntValidator iv(0, 10, 0); + QSignalSpy topSpy(&iv, SIGNAL(topChanged(int))); + QSignalSpy bottomSpy(&iv, SIGNAL(bottomChanged(int))); + iv.setTop(9); + QCOMPARE(topSpy.count(), 1); + QVERIFY(iv.top() == 9); + iv.setBottom(1); + QCOMPARE(bottomSpy.count(), 1); + QVERIFY(iv.bottom() == 1); + + iv.setRange(1, 8); + QCOMPARE(topSpy.count(), 2); + QCOMPARE(bottomSpy.count(), 1); + QVERIFY(iv.top() == 8); + QVERIFY(iv.bottom() == 1); + + iv.setRange(2, 8); + QCOMPARE(topSpy.count(), 2); + QCOMPARE(bottomSpy.count(), 2); + QVERIFY(iv.top() == 8); + QVERIFY(iv.bottom() == 2); + + iv.setRange(3, 7); + QCOMPARE(topSpy.count(), 3); + QCOMPARE(bottomSpy.count(), 3); + QVERIFY(iv.top() == 7); + QVERIFY(iv.bottom() == 3); +} + QTEST_MAIN(tst_QIntValidator) #include "tst_qintvalidator.moc" diff --git a/tests/auto/qregexpvalidator/tst_qregexpvalidator.cpp b/tests/auto/qregexpvalidator/tst_qregexpvalidator.cpp index d8ef92a439..23cd5b1ef1 100644 --- a/tests/auto/qregexpvalidator/tst_qregexpvalidator.cpp +++ b/tests/auto/qregexpvalidator/tst_qregexpvalidator.cpp @@ -115,9 +115,12 @@ void tst_QRegExpValidator::validate() QFETCH( int, state ); QRegExpValidator rv( 0 ); + QSignalSpy spy(&rv, SIGNAL(regExpChanged(const QRegExp&))); + rv.setRegExp( QRegExp( rx ) ); int dummy; QCOMPARE( (int)rv.validate( value, dummy ), state ); + QCOMPARE(spy.count(), 1); } QTEST_MAIN(tst_QRegExpValidator) -- cgit v1.2.3