Add a changed() signal to QValidator.

This provides a general notification of changes that may change the
validity of previously validated input.

Change-Id: I5ec6f127af60fdca68605fee903a08758bc01360
Reviewed-by: Jonas Gastal <jgastal@profusion.mobi>
Reviewed-by: Lars Knoll <lars.knoll@nokia.com>
This commit is contained in:
Andrew den Exter 2012-01-18 13:04:21 +10:00 committed by Qt by Nokia
parent 4830b790fe
commit c0d30db45c
5 changed files with 76 additions and 1 deletions

View File

@ -131,6 +131,12 @@ QT_BEGIN_NAMESPACE
\omitvalue Valid
*/
/*!
\fn void QValidator::changed()
This signal is emitted when any property that may affect the validity of
a string has changed.
*/
/*!
\fn void QIntValidator::topChanged(int top)
@ -247,7 +253,10 @@ QLocale QValidator::locale() const
void QValidator::setLocale(const QLocale &locale)
{
Q_D(QValidator);
if (d->locale != locale) {
d->locale = locale;
emit changed();
}
}
/*!
@ -452,15 +461,21 @@ void QIntValidator::fixup(QString &input) const
void QIntValidator::setRange(int bottom, int top)
{
bool rangeChanged = false;
if (b != bottom) {
b = bottom;
rangeChanged = true;
emit bottomChanged(b);
}
if (t != top) {
t = top;
rangeChanged = true;
emit topChanged(t);
}
if (rangeChanged)
emit changed();
}
@ -697,20 +712,26 @@ QValidator::State QDoubleValidatorPrivate::validateWithLocale(QString &input, QL
void QDoubleValidator::setRange(double minimum, double maximum, int decimals)
{
bool rangeChanged = false;
if (b != minimum) {
b = minimum;
rangeChanged = true;
emit bottomChanged(b);
}
if (t != maximum) {
t = maximum;
rangeChanged = true;
emit topChanged(t);
}
if (dec != decimals) {
dec = decimals;
rangeChanged = true;
emit decimalsChanged(dec);
}
if (rangeChanged)
emit changed();
}
/*!
@ -772,6 +793,7 @@ void QDoubleValidator::setNotation(Notation newNotation)
if (d->notation != newNotation) {
d->notation = newNotation;
emit notationChanged(d->notation);
emit changed();
}
}
@ -888,6 +910,7 @@ void QRegExpValidator::setRegExp(const QRegExp& rx)
if (r != rx) {
r = rx;
emit regExpChanged(r);
emit changed();
}
}

View File

@ -76,6 +76,9 @@ public:
virtual State validate(QString &, int &) const = 0;
virtual void fixup(QString &) const;
Q_SIGNALS:
void changed();
protected:
QValidator(QObjectPrivate &d, QObject *parent);
QValidator(QValidatorPrivate &d, QObject *parent);

View File

@ -247,25 +247,31 @@ void tst_QDoubleValidator::validate()
}
void tst_QDoubleValidator::notifySignals()
{
QLocale::setDefault(QLocale("C"));
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)));
QSignalSpy changedSpy(&dv, SIGNAL(changed()));
qRegisterMetaType<QDoubleValidator::Notation>("QDoubleValidator::Notation");
QSignalSpy notSpy(&dv, SIGNAL(notationChanged(QDoubleValidator::Notation)));
dv.setTop(0.8);
QCOMPARE(topSpy.count(), 1);
QCOMPARE(changedSpy.count(), 1);
QVERIFY(dv.top() == 0.8);
dv.setBottom(0.2);
QCOMPARE(bottomSpy.count(), 1);
QCOMPARE(changedSpy.count(), 2);
QVERIFY(dv.bottom() == 0.2);
dv.setRange(0.2, 0.7);
QCOMPARE(topSpy.count(), 2);
QCOMPARE(bottomSpy.count(), 1);
QCOMPARE(decSpy.count(), 1);
QCOMPARE(changedSpy.count(), 3);
QVERIFY(dv.bottom() == 0.2);
QVERIFY(dv.top() == 0.7);
QVERIFY(dv.decimals() == 0.);
@ -273,6 +279,7 @@ void tst_QDoubleValidator::notifySignals()
dv.setRange(0.3, 0.7);
QCOMPARE(topSpy.count(), 2);
QCOMPARE(bottomSpy.count(), 2);
QCOMPARE(changedSpy.count(), 4);
QVERIFY(dv.bottom() == 0.3);
QVERIFY(dv.top() == 0.7);
QVERIFY(dv.decimals() == 0.);
@ -280,12 +287,14 @@ void tst_QDoubleValidator::notifySignals()
dv.setRange(0.4, 0.6);
QCOMPARE(topSpy.count(), 3);
QCOMPARE(bottomSpy.count(), 3);
QCOMPARE(changedSpy.count(), 5);
QVERIFY(dv.bottom() == 0.4);
QVERIFY(dv.top() == 0.6);
QVERIFY(dv.decimals() == 0.);
dv.setDecimals(10);
QCOMPARE(decSpy.count(), 2);
QCOMPARE(changedSpy.count(), 6);
QVERIFY(dv.decimals() == 10.);
@ -293,13 +302,31 @@ void tst_QDoubleValidator::notifySignals()
QCOMPARE(topSpy.count(), 3);
QCOMPARE(bottomSpy.count(), 3);
QCOMPARE(decSpy.count(), 3);
QCOMPARE(changedSpy.count(), 7);
QVERIFY(dv.bottom() == 0.4);
QVERIFY(dv.top() == 0.6);
QVERIFY(dv.decimals() == 100.);
dv.setNotation(QDoubleValidator::StandardNotation);
QCOMPARE(notSpy.count(), 1);
QCOMPARE(changedSpy.count(), 8);
QVERIFY(dv.notation() == QDoubleValidator::StandardNotation);
dv.setRange(dv.bottom(), dv.top(), dv.decimals());
QCOMPARE(topSpy.count(), 3);
QCOMPARE(bottomSpy.count(), 3);
QCOMPARE(decSpy.count(), 3);
QCOMPARE(changedSpy.count(), 8);
dv.setNotation(dv.notation());
QCOMPARE(notSpy.count(), 1);
QCOMPARE(changedSpy.count(), 8);
dv.setLocale(QLocale("C"));
QCOMPARE(changedSpy.count(), 8);
dv.setLocale(QLocale("en"));
QCOMPARE(changedSpy.count(), 9);
}
void tst_QDoubleValidator::validateIntEquiv_data()

View File

@ -231,33 +231,53 @@ void tst_QIntValidator::validate()
void tst_QIntValidator::notifySignals()
{
QLocale::setDefault(QLocale("C"));
QIntValidator iv(0, 10, 0);
QSignalSpy topSpy(&iv, SIGNAL(topChanged(int)));
QSignalSpy bottomSpy(&iv, SIGNAL(bottomChanged(int)));
QSignalSpy changedSpy(&iv, SIGNAL(changed()));
iv.setTop(9);
QCOMPARE(topSpy.count(), 1);
QCOMPARE(changedSpy.count(), 1);
QVERIFY(iv.top() == 9);
iv.setBottom(1);
QCOMPARE(bottomSpy.count(), 1);
QCOMPARE(changedSpy.count(), 2);
QVERIFY(iv.bottom() == 1);
iv.setRange(1, 8);
QCOMPARE(topSpy.count(), 2);
QCOMPARE(bottomSpy.count(), 1);
QCOMPARE(changedSpy.count(), 3);
QVERIFY(iv.top() == 8);
QVERIFY(iv.bottom() == 1);
iv.setRange(2, 8);
QCOMPARE(topSpy.count(), 2);
QCOMPARE(bottomSpy.count(), 2);
QCOMPARE(changedSpy.count(), 4);
QVERIFY(iv.top() == 8);
QVERIFY(iv.bottom() == 2);
iv.setRange(3, 7);
QCOMPARE(topSpy.count(), 3);
QCOMPARE(bottomSpy.count(), 3);
QCOMPARE(changedSpy.count(), 5);
QVERIFY(iv.top() == 7);
QVERIFY(iv.bottom() == 3);
iv.setRange(3, 7);
QCOMPARE(topSpy.count(), 3);
QCOMPARE(bottomSpy.count(), 3);
QCOMPARE(changedSpy.count(), 5);
iv.setLocale(QLocale("C"));
QCOMPARE(changedSpy.count(), 5);
iv.setLocale(QLocale("en"));
QCOMPARE(changedSpy.count(), 6);
}
QTEST_MAIN(tst_QIntValidator)

View File

@ -113,11 +113,13 @@ void tst_QRegExpValidator::validate()
QRegExpValidator rv( 0 );
QSignalSpy spy(&rv, SIGNAL(regExpChanged(const QRegExp&)));
QSignalSpy changedSpy(&rv, SIGNAL(changed()));
rv.setRegExp( QRegExp( rx ) );
int dummy;
QCOMPARE( (int)rv.validate( value, dummy ), state );
QCOMPARE(spy.count(), 1);
QCOMPARE(changedSpy.count(), 1);
}
QTEST_MAIN(tst_QRegExpValidator)