]> git.lyx.org Git - lyx.git/blob - src/support/Timeout.cpp
some de-boostification
[lyx.git] / src / support / Timeout.cpp
1 /**
2  * \file Timeout.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author John Levon
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "support/Timeout.h"
14 #include "support/debug.h"
15
16 #include <QObject>
17 #include <QTimerEvent>
18
19 namespace lyx {
20
21 /**
22  * This class executes the callback when the timeout expires
23  * using Qt mechanisms
24  */
25 class Timeout::Impl : QObject {
26 public:
27         ///
28         Impl(Timeout & owner) : owner_(owner), timeout_id(-1) {}
29         ///
30         bool running() const { return timeout_id != -1; } 
31         /// start the timer
32         void start();
33         /// stop the timer
34         void stop();
35         /// reset
36         void reset();
37         ///
38         unsigned int timeout_ms() const { return owner_.timeout_ms; }
39
40 protected:
41         /// 
42         void timerEvent(QTimerEvent *) { owner_.emit(); }
43
44 private:
45         ///
46         Timeout & owner_;
47         /// timout id
48         int timeout_id;
49 };
50
51
52 void Timeout::Impl::reset()
53 {
54         if (timeout_id != -1)
55                 killTimer(timeout_id);
56         timeout_id = -1;
57 }
58
59
60 void Timeout::Impl::start()
61 {
62         if (running())
63                 lyxerr << "Timeout::start: already running!" << std::endl;
64         timeout_id = startTimer(timeout_ms());
65 }
66
67
68 void Timeout::Impl::stop()
69 {
70         if (running())
71                 reset();
72 }
73
74
75 //
76 // Timeout
77 //
78
79 Timeout::Timeout(unsigned int msec, Type t)
80         : pimpl_(new Impl(*this)), type(t), timeout_ms(msec)
81 {}
82
83
84 Timeout::~Timeout()
85 {
86         pimpl_->stop();
87         delete pimpl_;
88 }
89
90
91 bool Timeout::running() const
92 {
93         return pimpl_->running();
94 }
95
96
97 void Timeout::start()
98 {
99         pimpl_->start();
100 }
101
102
103 void Timeout::stop()
104 {
105         pimpl_->stop();
106 }
107
108
109 void Timeout::restart()
110 {
111         pimpl_->stop();
112         pimpl_->start();
113 }
114
115
116 void Timeout::emit()
117 {
118         pimpl_->reset();
119         timeout();
120         if (type == CONTINUOUS)
121                 pimpl_->start();
122 }
123
124
125 Timeout & Timeout::setType(Type t)
126 {
127         type = t;
128         return *this;
129 }
130
131
132 Timeout & Timeout::setTimeout(unsigned int msec)
133 {
134         // Can't have a timeout of zero!
135         BOOST_ASSERT(msec);
136
137         timeout_ms = msec;
138         return *this;
139 }
140
141
142 } // namespace lyx