diff options
author | Ivan Solovev <ivan.solovev@qt.io> | 2020-12-10 18:46:31 +0100 |
---|---|---|
committer | Fabian Kosmale <fabian.kosmale@qt.io> | 2021-01-26 21:47:50 +0100 |
commit | 709a0942aa5adeea8057a92a301dee85fc575528 (patch) | |
tree | 8b76cb93284c6f4bb479f001244ec27c37217e81 | |
parent | 519420641fabeeb7a372ed6157a7bc01f43b1b3b (diff) |
QTimer: port to new property system
Task-number: QTBUG-85520
Change-Id: I1f92b86619f2ca186c505251b21463f396af0ac6
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
Reviewed-by: Andreas Buhr <andreas.buhr@qt.io>
-rw-r--r-- | src/corelib/kernel/qtimer.cpp | 39 | ||||
-rw-r--r-- | src/corelib/kernel/qtimer.h | 12 | ||||
-rw-r--r-- | tests/auto/corelib/kernel/qtimer/tst_qtimer.cpp | 88 |
3 files changed, 131 insertions, 8 deletions
diff --git a/src/corelib/kernel/qtimer.cpp b/src/corelib/kernel/qtimer.cpp index 6e3d34214f..b553bbd5d3 100644 --- a/src/corelib/kernel/qtimer.cpp +++ b/src/corelib/kernel/qtimer.cpp @@ -44,6 +44,7 @@ #include "qobject_p.h" #include "qthread.h" #include "qcoreapplication_p.h" +#include "qproperty_p.h" QT_BEGIN_NAMESPACE @@ -51,12 +52,18 @@ static constexpr int INV_TIMER = -1; // invalid timer id class QTimerPrivate : public QObjectPrivate { + Q_DECLARE_PUBLIC(QTimer) public: + void setInterval(int msec) { q_func()->setInterval(msec); } + bool isActiveActualCalculation() const { return id >= 0; } + int id = INV_TIMER; - int inter = 0; - bool single = false; + Q_OBJECT_COMPAT_PROPERTY_WITH_ARGS(QTimerPrivate, int, inter, &QTimerPrivate::setInterval, 0) + Q_OBJECT_BINDABLE_PROPERTY_WITH_ARGS(QTimerPrivate, bool, single, false) bool nulltimer = false; - Qt::TimerType type = Qt::CoarseTimer; + Q_OBJECT_BINDABLE_PROPERTY_WITH_ARGS(QTimerPrivate, Qt::TimerType, type, Qt::CoarseTimer) + Q_OBJECT_COMPUTED_PROPERTY(QTimerPrivate, bool, isActiveData, + &QTimerPrivate::isActiveActualCalculation) }; /*! @@ -198,7 +205,12 @@ QTimer::~QTimer() */ bool QTimer::isActive() const { - return d_func()->id >= 0; + return d_func()->isActiveData.value(); +} + +QBindable<bool> QTimer::bindableActive() +{ + return QBindable<bool>(&d_func()->isActiveData); } /*! @@ -229,6 +241,7 @@ void QTimer::start() stop(); d->nulltimer = (!d->inter && d->single); d->id = QObject::startTimer(d->inter, d->type); + d->isActiveData.markDirty(); } /*! @@ -262,6 +275,7 @@ void QTimer::stop() if (d->id != INV_TIMER) { QObject::killTimer(d->id); d->id = INV_TIMER; + d->isActiveData.markDirty(); } } @@ -718,6 +732,11 @@ bool QTimer::isSingleShot() const return d_func()->single; } +QBindable<bool> QTimer::bindableSingleShot() +{ + return QBindable<bool>(&d_func()->single); +} + /*! \property QTimer::interval \brief the timeout interval in milliseconds @@ -737,6 +756,8 @@ void QTimer::setInterval(int msec) if (d->id != INV_TIMER) { // create new timer QObject::killTimer(d->id); // restart timer d->id = QObject::startTimer(msec, d->type); + // No need to call markDirty() for d->isActiveData here, + // as timer state actually does not change } } @@ -745,6 +766,11 @@ int QTimer::interval() const return d_func()->inter; } +QBindable<int> QTimer::bindableInterval() +{ + return QBindable<int>(&d_func()->inter); +} + /*! \property QTimer::remainingTime \since 5.0 @@ -784,6 +810,11 @@ Qt::TimerType QTimer::timerType() const return d_func()->type; } +QBindable<Qt::TimerType> QTimer::bindableTimerType() +{ + return QBindable<Qt::TimerType>(&d_func()->type); +} + QT_END_NAMESPACE #include "qtimer.moc" diff --git a/src/corelib/kernel/qtimer.h b/src/corelib/kernel/qtimer.h index d4da91e172..2b262f6311 100644 --- a/src/corelib/kernel/qtimer.h +++ b/src/corelib/kernel/qtimer.h @@ -57,28 +57,32 @@ class QTimerPrivate; class Q_CORE_EXPORT QTimer : public QObject { Q_OBJECT - Q_PROPERTY(bool singleShot READ isSingleShot WRITE setSingleShot) - Q_PROPERTY(int interval READ interval WRITE setInterval) + Q_PROPERTY(bool singleShot READ isSingleShot WRITE setSingleShot BINDABLE bindableSingleShot) + Q_PROPERTY(int interval READ interval WRITE setInterval BINDABLE bindableInterval) Q_PROPERTY(int remainingTime READ remainingTime) - Q_PROPERTY(Qt::TimerType timerType READ timerType WRITE setTimerType) - Q_PROPERTY(bool active READ isActive) + Q_PROPERTY(Qt::TimerType timerType READ timerType WRITE setTimerType BINDABLE bindableTimerType) + Q_PROPERTY(bool active READ isActive STORED false BINDABLE bindableActive) public: explicit QTimer(QObject *parent = nullptr); ~QTimer(); bool isActive() const; + QBindable<bool> bindableActive(); int timerId() const; void setInterval(int msec); int interval() const; + QBindable<int> bindableInterval(); int remainingTime() const; void setTimerType(Qt::TimerType atype); Qt::TimerType timerType() const; + QBindable<Qt::TimerType> bindableTimerType(); void setSingleShot(bool singleShot); bool isSingleShot() const; + QBindable<bool> bindableSingleShot(); static void singleShot(int msec, const QObject *receiver, const char *member); static void singleShot(int msec, Qt::TimerType timerType, const QObject *receiver, const char *member); diff --git a/tests/auto/corelib/kernel/qtimer/tst_qtimer.cpp b/tests/auto/corelib/kernel/qtimer/tst_qtimer.cpp index 586ffc85ed..cd0a0d03d9 100644 --- a/tests/auto/corelib/kernel/qtimer/tst_qtimer.cpp +++ b/tests/auto/corelib/kernel/qtimer/tst_qtimer.cpp @@ -89,6 +89,9 @@ private slots: void dontBlockEvents(); void postedEventsShouldNotStarveTimers(); void callOnTimeout(); + + void bindToTimer(); + void bindTimer(); }; void tst_QTimer::zeroTimer() @@ -1083,6 +1086,91 @@ void tst_QTimer::callOnTimeout() QVERIFY(!connection); } +void tst_QTimer::bindToTimer() +{ + QTimer timer; + + // singleShot property + QProperty<bool> singleShot; + singleShot.setBinding(timer.bindableSingleShot().makeBinding()); + QCOMPARE(timer.isSingleShot(), singleShot); + + timer.setSingleShot(true); + QVERIFY(singleShot); + timer.setSingleShot(false); + QVERIFY(!singleShot); + + // interval property + QProperty<int> interval; + interval.setBinding([&](){ return timer.interval(); }); + QCOMPARE(timer.interval(), interval); + + timer.setInterval(10); + QCOMPARE(interval, 10); + timer.setInterval(100); + QCOMPARE(interval, 100); + + // timerType property + QProperty<Qt::TimerType> timerType; + timerType.setBinding(timer.bindableTimerType().makeBinding()); + QCOMPARE(timer.timerType(), timerType); + + timer.setTimerType(Qt::PreciseTimer); + QCOMPARE(timerType, Qt::PreciseTimer); + + timer.setTimerType(Qt::VeryCoarseTimer); + QCOMPARE(timerType, Qt::VeryCoarseTimer); + + // active property + QProperty<bool> active; + active.setBinding([&](){ return timer.isActive(); }); + QCOMPARE(active, timer.isActive()); + + timer.start(1000); + QVERIFY(active); + + timer.stop(); + QVERIFY(!active); +} + +void tst_QTimer::bindTimer() +{ + QTimer timer; + + // singleShot property + QVERIFY(!timer.isSingleShot()); + + QProperty<bool> singleShot; + timer.bindableSingleShot().setBinding(Qt::makePropertyBinding(singleShot)); + + singleShot = true; + QVERIFY(timer.isSingleShot()); + singleShot = false; + QVERIFY(!timer.isSingleShot()); + + // interval property + QCOMPARE(timer.interval(), 0); + + QProperty<int> interval; + timer.bindableInterval().setBinding(Qt::makePropertyBinding(interval)); + + interval = 10; + QCOMPARE(timer.interval(), 10); + interval = 100; + QCOMPARE(timer.interval(), 100); + + // timerType property + QCOMPARE(timer.timerType(), Qt::CoarseTimer); + + QProperty<Qt::TimerType> timerType; + timer.bindableTimerType().setBinding(Qt::makePropertyBinding(timerType)); + + timerType = Qt::PreciseTimer; + QCOMPARE(timer.timerType(), Qt::PreciseTimer); + timerType = Qt::VeryCoarseTimer; + QCOMPARE(timer.timerType(), Qt::VeryCoarseTimer); +} + class OrderHelper : public QObject { Q_OBJECT |