]> git.lyx.org Git - lyx.git/blob - src/support/Timeout.cpp
fix warning on possibly(?) unused precompiled headers due to different -fPic settings...
[lyx.git] / src / support / Timeout.cpp
1 /**
2  * \file qtTimeout.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 <QObject>
14 #include <QTimerEvent>
15
16 #include "debug.h"
17 #include "Timeout.h"
18
19
20 namespace lyx {
21
22 /**
23  * This class executes the callback when the timeout expires
24  * using Qt mechanisms
25  */
26 class qtTimeout : QObject, public Timeout::Impl {
27 public:
28         ///
29         qtTimeout(Timeout & owner_);
30         ///
31         virtual bool running() const;
32         /// start the timer
33         virtual void start();
34         /// stop the timer
35         virtual void stop();
36         /// reset
37         virtual void reset();
38
39 protected:
40         /// slot
41         virtual void timerEvent(QTimerEvent *);
42
43 private:
44         /// timout id
45         int timeout_id;
46 };
47
48
49 Timeout::Timeout(unsigned int msec, Type t)
50         : pimpl_(new qtTimeout(*this)), type(t), timeout_ms(msec)
51 {}
52
53
54 qtTimeout::qtTimeout(Timeout & owner)
55         : Timeout::Impl(owner), timeout_id(-1)
56 {}
57
58
59 void qtTimeout::timerEvent(QTimerEvent *)
60 {
61         emit();
62 }
63
64
65 void qtTimeout::reset()
66 {
67         if (timeout_id != -1)
68                 killTimer(timeout_id);
69         timeout_id = -1;
70 }
71
72
73 bool qtTimeout::running() const
74 {
75         return timeout_id != -1;
76 }
77
78
79 void qtTimeout::start()
80 {
81         if (running())
82                 lyxerr << "Timeout::start: already running!" << std::endl;
83         timeout_id = startTimer(timeout_ms());
84 }
85
86
87 void qtTimeout::stop()
88 {
89         if (running())
90                 reset();
91 }
92
93
94 //
95 // Timeoute
96 //
97
98 Timeout::~Timeout()
99 {
100         pimpl_->stop();
101 }
102
103
104 bool Timeout::running() const
105 {
106         return pimpl_->running();
107 }
108
109
110 void Timeout::start()
111 {
112         pimpl_->start();
113 }
114
115
116 void Timeout::stop()
117 {
118         pimpl_->stop();
119 }
120
121
122 void Timeout::restart()
123 {
124         pimpl_->stop();
125         pimpl_->start();
126 }
127
128
129 void Timeout::emit()
130 {
131         pimpl_->reset();
132         timeout();
133         if (type == CONTINUOUS)
134                 pimpl_->start();
135 }
136
137
138 Timeout & Timeout::setType(Type t)
139 {
140         type = t;
141         return * this;
142 }
143
144
145 Timeout & Timeout::setTimeout(unsigned int msec)
146 {
147         // Can't have a timeout of zero!
148         BOOST_ASSERT(msec);
149
150         timeout_ms = msec;
151         return * this;
152 }
153
154
155 } // namespace lyx