diff --git a/GUI/coregui/Views/CommonWidgets/UpdateTimer.cpp b/GUI/coregui/Views/CommonWidgets/UpdateTimer.cpp index aaf6b79ccef0626c6f5be176daea358c5bea7e0b..c931d64e71498533d8d093363e99853cd742e275 100644 --- a/GUI/coregui/Views/CommonWidgets/UpdateTimer.cpp +++ b/GUI/coregui/Views/CommonWidgets/UpdateTimer.cpp @@ -16,66 +16,57 @@ #include "UpdateTimer.h" #include <QTimer> +#include <QDebug> -namespace { -const int default_timer_interval_in_msec = 2; -} - -UpdateTimer::UpdateTimer(int accumulateDuring, QObject *parent) +UpdateTimer::UpdateTimer(int timerInterval, QObject* parent) : QObject(parent) - , m_accumulate_updates_during(accumulateDuring) , m_update_request_count(0) - , m_timer_interval(default_timer_interval_in_msec) - , m_remaining_time_to_update(0) + , m_timer_interval(timerInterval) + , m_is_busy(false) , m_timer(new QTimer(this)) { m_timer->setInterval(m_timer_interval); + m_timer->setSingleShot(true); connect(m_timer, SIGNAL(timeout()), this, SLOT(onTimerTimeout())); } -//! Sets time period in msec, during which updates will be accumulated. - -void UpdateTimer::setAccumulateDuring(int accumulateDuring) +void UpdateTimer::reset() { - m_accumulate_updates_during = accumulateDuring; -} - -//! Sets timer interval in msec to check if it is time to propagate update requests back. - -void UpdateTimer::setTimerInterval(int timerInterval) -{ - m_timer_interval = timerInterval; + m_update_request_count = 0; + m_timer->stop(); + m_is_busy = false; } -//! Schedule subsequent update. - void UpdateTimer::scheduleUpdate() { + qDebug() << "UpdateTimer::scheduleUpdate() m_is_busy" << m_is_busy << m_update_request_count; + if(m_is_busy) + return; + ++m_update_request_count; + qDebug() << "UpdateTimer::scheduleUpdate()" << m_update_request_count; + if(!m_timer->isActive()) { + qDebug() << " AccumulateTimer::scheduleUpdate() -> starting timer"; m_timer->start(m_timer_interval); - m_remaining_time_to_update = m_accumulate_updates_during; } } void UpdateTimer::onTimerTimeout() { - m_remaining_time_to_update -= m_timer_interval; + m_is_busy = true; + qDebug() << "UpdateTimer::onTimerTimeout()" << m_update_request_count; - if(m_remaining_time_to_update <= 0) { - m_timer->stop(); - processUpdates(); + if(m_update_request_count > 0) { + m_update_request_count = 0; + qDebug() << " emiting timeToUpdate()"; + emit timeToUpdate(); } + + m_is_busy = false; } -void UpdateTimer::processUpdates() -{ - Q_ASSERT(!m_timer->isActive()); - if(m_update_request_count > 0) - emit timeToUpdate(); - m_update_request_count = 0; -} diff --git a/GUI/coregui/Views/CommonWidgets/UpdateTimer.h b/GUI/coregui/Views/CommonWidgets/UpdateTimer.h index 8cf95bcf583d94c905f0fa1592c4669a70b06dd5..ef7c10dcc372705ad41577a2b0a5f4a03dccc4aa 100644 --- a/GUI/coregui/Views/CommonWidgets/UpdateTimer.h +++ b/GUI/coregui/Views/CommonWidgets/UpdateTimer.h @@ -29,12 +29,10 @@ class QTimer; class BA_CORE_API_ UpdateTimer : public QObject { Q_OBJECT - public: - explicit UpdateTimer(int accumulateDuring, QObject *parent = 0); + explicit UpdateTimer(int timerInterval, QObject* parent = 0); - void setAccumulateDuring(int accumulateDuring); - void setTimerInterval(int timerInterval); + void reset(); signals: void timeToUpdate(); @@ -46,21 +44,10 @@ private slots: void onTimerTimeout(); private: - void processUpdates(); - - //!< Interval in msec during which all update requests will be accumulated. - int m_accumulate_updates_during; - - //!< Number of requests accumulated so far. - int m_update_request_count; - - //!< Timer interval to check what is going on. - int m_timer_interval; - - //!< Remaining time to to emit timeToUpdate signal - int m_remaining_time_to_update; - - QTimer *m_timer; + qint64 m_update_request_count; //!< Number of requests accumulated so far. + int m_timer_interval; //!< Timer in msec. + bool m_is_busy; + QTimer* m_timer; }; #endif // UPDATETIMER_H diff --git a/Tests/UnitTests/GUI/TestUpdateTimer.h b/Tests/UnitTests/GUI/TestUpdateTimer.h new file mode 100644 index 0000000000000000000000000000000000000000..c1728c0ec667d9b20e654f74ebcc3ecf24e12d61 --- /dev/null +++ b/Tests/UnitTests/GUI/TestUpdateTimer.h @@ -0,0 +1,39 @@ +#include <QtTest> +#include "UpdateTimer.h" +#include <QSignalSpy> +#include <QDebug> + +class TestUpdateTimer : public QObject +{ + Q_OBJECT + +private slots: + void test_updateTimerShort(); +}; + +inline void TestUpdateTimer::test_updateTimerShort() +{ + const int timer_interval(100); + UpdateTimer timer(timer_interval); + + QSignalSpy spy(&timer, SIGNAL(timeToUpdate())); + + for (int i = 0; i < 10; ++i) + timer.scheduleUpdate(); + + // Checks that after time bigger than timer interval, we have a valid signal + QVERIFY(spy.wait(timer_interval * 2)); + QCOMPARE(spy.count(), 1); + + // once again + timer.scheduleUpdate(); + QVERIFY(spy.wait(timer_interval * 1.5)); + QCOMPARE(spy.count(), 2); + + // Checks that after time smaller than timer interval, we have no signals + for (int i = 0; i < 10; ++i) + timer.scheduleUpdate(); + + QVERIFY(spy.wait(timer_interval / 2) == false); + QCOMPARE(spy.count(), 2); +}