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>
This commit is contained in:
Ivan Solovev 2020-12-10 18:46:31 +01:00 committed by Fabian Kosmale
parent 519420641f
commit 709a0942aa
3 changed files with 131 additions and 8 deletions

View File

@ -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"

View File

@ -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);

View File

@ -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