]> git.lyx.org Git - lyx.git/blob - src/support/Timeout.cpp
Fix text direction issue for InsetInfo in RTL context
[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/lassert.h"
14 #include "support/Timeout.h"
15 #include "support/debug.h"
16
17 #include <QObject>
18 #include <QTimerEvent>
19
20 #include <ctime>
21 #include <iomanip>
22 #include <sstream>
23
24 using namespace std;
25
26 namespace lyx {
27
28 /**
29  * This class executes the callback when the timeout expires
30  * using Qt mechanisms
31  */
32 class Timeout::Impl : QObject {
33 public:
34         ///
35         Impl(Timeout & owner) : owner_(owner), timeout_id(-1) {}
36         ///
37         bool running() const { return timeout_id != -1; }
38         /// start the timer
39         void start();
40         /// stop the timer
41         void stop();
42         /// reset
43         void reset();
44         ///
45         unsigned int timeout_ms() const { return owner_.timeout_ms; }
46
47 protected:
48         ///
49         void timerEvent(QTimerEvent *) { owner_.emit(); }
50
51 private:
52         ///
53         Timeout & owner_;
54         /// timout id
55         int timeout_id;
56 };
57
58
59 void Timeout::Impl::reset()
60 {
61         if (timeout_id != -1)
62                 killTimer(timeout_id);
63         timeout_id = -1;
64 }
65
66
67 void Timeout::Impl::start()
68 {
69         if (running())
70                 lyxerr << "Timeout::start: already running!" << endl;
71         timeout_id = startTimer(timeout_ms());
72 }
73
74
75 void Timeout::Impl::stop()
76 {
77         if (running())
78                 reset();
79 }
80
81
82 //
83 // Timeout
84 //
85
86 Timeout::Timeout(unsigned int msec, Type t)
87         : pimpl_(new Impl(*this)), type(t), timeout_ms(msec)
88 {}
89
90
91 Timeout::~Timeout()
92 {
93         pimpl_->stop();
94         delete pimpl_;
95 }
96
97
98 bool Timeout::running() const
99 {
100         return pimpl_->running();
101 }
102
103
104 void Timeout::start()
105 {
106         pimpl_->start();
107 }
108
109
110 void Timeout::stop()
111 {
112         pimpl_->stop();
113 }
114
115
116 void Timeout::restart()
117 {
118         pimpl_->stop();
119         pimpl_->start();
120 }
121
122
123 void Timeout::emit()
124 {
125         pimpl_->reset();
126         timeout();
127         if (type == CONTINUOUS)
128                 pimpl_->start();
129 }
130
131
132 Timeout & Timeout::setType(Type t)
133 {
134         type = t;
135         return *this;
136 }
137
138
139 Timeout & Timeout::setTimeout(unsigned int msec)
140 {
141         // Can't have a timeout of zero!
142         LASSERT(msec, msec = 1000);
143
144         timeout_ms = msec;
145         return *this;
146 }
147
148
149 struct Timer::Private
150 {
151         time_t start_time;
152 };
153
154
155 Timer::Timer() : d(new Private)
156 {
157         restart();
158 }
159
160
161 Timer::~Timer()
162 {
163         delete d;
164 }
165
166
167 void Timer::restart()
168 {
169         time(&d->start_time);
170 }
171
172
173 int Timer::elapsed() const
174 {
175         time_t end_time;
176         time(&end_time);
177         double diff = difftime(end_time, d->start_time);
178         return int(diff);
179 }
180
181
182 string Timer::timeStr(char separator) const
183 {
184         tm * timeinfo = localtime(&d->start_time);
185         // With less flexibility we could also use:
186         //strftime(buffer, 10, "%X", timeinfo);
187         ostringstream out;
188         out << setw(2) << setfill('0');
189         if (separator) {
190                 out << separator << setw(2) << setfill('0') << timeinfo->tm_hour
191                     << separator << setw(2) << setfill('0') << timeinfo->tm_min
192                     << separator << setw(2) << setfill('0') << timeinfo->tm_sec;
193         } else {
194                 out << setw(2) << setfill('0') << timeinfo->tm_hour
195                     << setw(2) << setfill('0') << timeinfo->tm_min
196                     << setw(2) << setfill('0') << timeinfo->tm_sec;
197         }
198         return out.str();
199 }
200
201
202 string Timer::dateStr(char separator) const
203 {
204         tm * timeinfo = localtime(&d->start_time);
205         // With less flexibility we could also use:
206         //res = strftime(buffer, 10, "%d%m%y", timeinfo);
207         ostringstream out;
208         out << setw(2) << setfill('0') << timeinfo->tm_mday;
209         if (separator)
210                 out << separator;
211         out << setw(2) << setfill('0') << timeinfo->tm_mon;
212         if (separator)
213                 out << separator;
214         out << setw(2) << setfill('0') << timeinfo->tm_year - 100;
215         return out.str();
216 }
217
218
219 string Timer::toStr() const
220 {
221         tm * timeinfo = localtime(&d->start_time);
222         return asctime(timeinfo);
223 }
224
225
226 string Timer::currentToStr()
227 {
228         time_t current_time;
229         time(&current_time);
230         tm * timeinfo = localtime(&current_time);
231         return asctime(timeinfo);
232 }
233
234 } // namespace lyx