3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
8 * Full author contact details are available in file CREDITS.
13 #include "support/lassert.h"
14 #include "support/Timeout.h"
15 #include "support/debug.h"
18 #include <QTimerEvent>
29 * This class executes the callback when the timeout expires
32 class Timeout::Impl : QObject {
35 Impl(Timeout & owner) : owner_(owner), timeout_id(-1) {}
37 bool running() const { return timeout_id != -1; }
45 unsigned int timeout_ms() const { return owner_.timeout_ms; }
49 void timerEvent(QTimerEvent *) { owner_.emit(); }
59 void Timeout::Impl::reset()
62 killTimer(timeout_id);
67 void Timeout::Impl::start()
70 lyxerr << "Timeout::start: already running!" << endl;
71 timeout_id = startTimer(timeout_ms());
75 void Timeout::Impl::stop()
86 Timeout::Timeout(unsigned int msec, Type t)
87 : pimpl_(new Impl(*this)), type(t), timeout_ms(msec)
98 bool Timeout::running() const
100 return pimpl_->running();
104 void Timeout::start()
116 void Timeout::restart()
127 if (type == CONTINUOUS)
132 Timeout & Timeout::setType(Type t)
139 Timeout & Timeout::setTimeout(unsigned int msec)
141 // Can't have a timeout of zero!
149 struct Timer::Private
155 Timer::Timer() : d(new Private)
161 void Timer::restart()
163 time(&d->start_time);
167 int Timer::elapsed() const
171 double diff = difftime(end_time, d->start_time);
176 string Timer::timeStr(char separator) const
178 tm * timeinfo = localtime(&d->start_time);
179 // With less flexibility we could also use:
180 //strftime(buffer, 10, "%X", timeinfo);
182 out << setw(2) << setfill('0');
184 out << separator << setw(2) << setfill('0') << timeinfo->tm_hour
185 << separator << setw(2) << setfill('0') << timeinfo->tm_min
186 << separator << setw(2) << setfill('0') << timeinfo->tm_sec;
188 out << setw(2) << setfill('0') << timeinfo->tm_hour
189 << setw(2) << setfill('0') << timeinfo->tm_min
190 << setw(2) << setfill('0') << timeinfo->tm_sec;
196 string Timer::dateStr(char separator) const
198 tm * timeinfo = localtime(&d->start_time);
199 // With less flexibility we could also use:
200 //res = strftime(buffer, 10, "%d%m%y", timeinfo);
202 out << setw(2) << setfill('0') << timeinfo->tm_mday;
205 out << setw(2) << setfill('0') << timeinfo->tm_mon;
208 out << setw(2) << setfill('0') << timeinfo->tm_year - 100;
213 string Timer::toStr() const
215 tm * timeinfo = localtime(&d->start_time);
216 return asctime(timeinfo);
220 string Timer::currentToStr()
224 tm * timeinfo = localtime(¤t_time);
225 return asctime(timeinfo);