~loic.molinari/ubuntu-ui-toolkit/ubuntu-ui-toolkit-ubuntushape-new-button-style-support

« back to all changes in this revision

Viewing changes to src/Ubuntu/UbuntuGestures/timer.cpp

  • Committer: Tarmac
  • Author(s): Zsombor Egri, Zoltán Balogh, Benjamin Zeller
  • Date: 2015-11-17 14:44:49 UTC
  • mfrom: (1693.4.43 migrate_unity8_gestures)
  • Revision ID: tarmac-20151117144449-p0s2lj04nx4fuq80
Migrate DirectionalDragArea from Unity8, named as SwipeArea. Original code (from lp:unity8) by: Daniel d'Andrada <daniel.dandrada@canonical.com>.

Approved by PS Jenkins bot, Christian Dywan, Benjamin Zeller, Zsombor Egri, Daniel d'Andrada.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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.h"
 
18
 
 
19
namespace UbuntuGestures {
 
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
    : UbuntuGestures::AbstractTimer(parent)
 
63
    , m_interval(0)
 
64
    , m_singleShot(false)
 
65
    , m_timeSource(timeSource)
 
66
{
 
67
}
 
68
 
 
69
void FakeTimer::update()
 
70
{
 
71
    if (!isRunning()) {
 
72
        return;
 
73
    }
 
74
 
 
75
    if (m_nextTimeoutTime <= m_timeSource->msecsSinceReference()) {
 
76
        if (isSingleShot()) {
 
77
            stop();
 
78
        } else {
 
79
            m_nextTimeoutTime += interval();
 
80
        }
 
81
        Q_EMIT timeout();
 
82
    }
 
83
}
 
84
 
 
85
void FakeTimer::start()
 
86
{
 
87
    AbstractTimer::start();
 
88
    m_nextTimeoutTime = m_timeSource->msecsSinceReference() + (qint64)interval();
 
89
}
 
90
 
 
91
int FakeTimer::interval() const
 
92
{
 
93
    return m_interval;
 
94
}
 
95
 
 
96
void FakeTimer::setInterval(int msecs)
 
97
{
 
98
    m_interval = msecs;
 
99
}
 
100
 
 
101
bool FakeTimer::isSingleShot() const
 
102
{
 
103
    return m_singleShot;
 
104
}
 
105
 
 
106
void FakeTimer::setSingleShot(bool value)
 
107
{
 
108
    m_singleShot = value;
 
109
}
 
110
 
 
111
/////////////////////////////////// FakeTimerFactory //////////////////////////////////
 
112
 
 
113
FakeTimerFactory::FakeTimerFactory()
 
114
{
 
115
    m_timeSource.reset(new FakeTimeSource);
 
116
}
 
117
 
 
118
void FakeTimerFactory::updateTime(qint64 targetTime)
 
119
{
 
120
    qint64 minTimeoutTime = targetTime;
 
121
 
 
122
    for (int i = 0; i < timers.count(); ++i) {
 
123
        FakeTimer *timer = timers[i].data();
 
124
        if (timer && timer->isRunning() && timer->nextTimeoutTime() < minTimeoutTime) {
 
125
            minTimeoutTime = timer->nextTimeoutTime();
 
126
        }
 
127
    }
 
128
 
 
129
    m_timeSource->m_msecsSinceReference = minTimeoutTime;
 
130
 
 
131
    for (int i = 0; i < timers.count(); ++i) {
 
132
        FakeTimer *timer = timers[i].data();
 
133
        if (timer) {
 
134
            timer->update();
 
135
        }
 
136
    }
 
137
 
 
138
    if (m_timeSource->msecsSinceReference() < targetTime) {
 
139
        updateTime(targetTime);
 
140
    }
 
141
}
 
142
 
 
143
AbstractTimer *FakeTimerFactory::createTimer(QObject *parent)
 
144
{
 
145
    FakeTimer *fakeTimer = new FakeTimer(m_timeSource, parent);
 
146
 
 
147
    timers.append(fakeTimer);
 
148
 
 
149
    return fakeTimer;
 
150
}
 
151
 
 
152
} // namespace UbuntuGestures