| 1 | /* |
| 2 | * Copyright 2015 Canonical Ltd. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU Lesser General Public License as published by |
| 6 | * the Free Software Foundation; version 3. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | * GNU Lesser General Public License for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU Lesser General Public License |
| 14 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 15 | */ |
| 16 | |
| 17 | #include "timer_p.h" |
| 18 | |
| 19 | UG_NAMESPACE_BEGIN |
| 20 | |
| 21 | Timer::Timer(QObject *parent) : AbstractTimer(parent) |
| 22 | { |
| 23 | m_timer.setSingleShot(false); |
| 24 | connect(&m_timer, &QTimer::timeout, this, &AbstractTimer::timeout); |
| 25 | } |
| 26 | |
| 27 | int Timer::interval() const |
| 28 | { |
| 29 | return m_timer.interval(); |
| 30 | } |
| 31 | |
| 32 | void Timer::setInterval(int msecs) |
| 33 | { |
| 34 | m_timer.setInterval(msecs); |
| 35 | } |
| 36 | |
| 37 | void Timer::start() |
| 38 | { |
| 39 | m_timer.start(); |
| 40 | AbstractTimer::start(); |
| 41 | } |
| 42 | |
| 43 | void Timer::stop() |
| 44 | { |
| 45 | m_timer.stop(); |
| 46 | AbstractTimer::stop(); |
| 47 | } |
| 48 | |
| 49 | bool Timer::isSingleShot() const |
| 50 | { |
| 51 | return m_timer.isSingleShot(); |
| 52 | } |
| 53 | |
| 54 | void Timer::setSingleShot(bool value) |
| 55 | { |
| 56 | m_timer.setSingleShot(value); |
| 57 | } |
| 58 | |
| 59 | /////////////////////////////////// FakeTimer ////////////////////////////////// |
| 60 | |
| 61 | FakeTimer::FakeTimer(const SharedTimeSource &timeSource, QObject *parent) |
| 62 | : UG_PREPEND_NAMESPACE(AbstractTimer(parent)) |
| 63 | , m_interval(0) |
| 64 | , m_singleShot(false) |
| 65 | , m_timeSource(timeSource) |
| 66 | , m_nextTimeoutTime(0) |
| 67 | { |
| 68 | } |
| 69 | |
| 70 | void FakeTimer::update() |
| 71 | { |
| 72 | if (!isRunning()) { |
| 73 | return; |
| 74 | } |
| 75 | |
| 76 | if (m_nextTimeoutTime <= m_timeSource->msecsSinceReference()) { |
| 77 | if (isSingleShot()) { |
| 78 | stop(); |
| 79 | } else { |
| 80 | m_nextTimeoutTime += interval(); |
| 81 | } |
| 82 | Q_EMIT timeout(); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | void FakeTimer::start() |
| 87 | { |
| 88 | AbstractTimer::start(); |
| 89 | m_nextTimeoutTime = m_timeSource->msecsSinceReference() + (qint64)interval(); |
| 90 | } |
| 91 | |
| 92 | int FakeTimer::interval() const |
| 93 | { |
| 94 | return m_interval; |
| 95 | } |
| 96 | |
| 97 | void FakeTimer::setInterval(int msecs) |
| 98 | { |
| 99 | m_interval = msecs; |
| 100 | } |
| 101 | |
| 102 | bool FakeTimer::isSingleShot() const |
| 103 | { |
| 104 | return m_singleShot; |
| 105 | } |
| 106 | |
| 107 | void FakeTimer::setSingleShot(bool value) |
| 108 | { |
| 109 | m_singleShot = value; |
| 110 | } |
| 111 | |
| 112 | /////////////////////////////////// FakeTimerFactory ////////////////////////////////// |
| 113 | |
| 114 | FakeTimerFactory::FakeTimerFactory() |
| 115 | { |
| 116 | m_timeSource.reset(new FakeTimeSource); |
| 117 | } |
| 118 | |
| 119 | void FakeTimerFactory::updateTime(qint64 targetTime) |
| 120 | { |
| 121 | qint64 minTimeoutTime = targetTime; |
| 122 | |
| 123 | for (int i = 0; i < timers.count(); ++i) { |
| 124 | FakeTimer *timer = timers[i].data(); |
| 125 | if (timer && timer->isRunning() && timer->nextTimeoutTime() < minTimeoutTime) { |
| 126 | minTimeoutTime = timer->nextTimeoutTime(); |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | m_timeSource->m_msecsSinceReference = minTimeoutTime; |
| 131 | |
| 132 | for (int i = 0; i < timers.count(); ++i) { |
| 133 | FakeTimer *timer = timers[i].data(); |
| 134 | if (timer) { |
| 135 | timer->update(); |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | if (m_timeSource->msecsSinceReference() < targetTime) { |
| 140 | updateTime(targetTime); |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | AbstractTimer *FakeTimerFactory::createTimer(QObject *parent) |
| 145 | { |
| 146 | FakeTimer *fakeTimer = new FakeTimer(m_timeSource, parent); |
| 147 | |
| 148 | timers.append(fakeTimer); |
| 149 | |
| 150 | return fakeTimer; |
| 151 | } |
| 152 | |
| 153 | UG_NAMESPACE_END |