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!
142 LASSERT(msec, msec = 1000);
149 struct Timer::Private
155 Timer::Timer() : d(new Private)
167 void Timer::restart()
169 time(&d->start_time);
173 int Timer::elapsed() const
177 double diff = difftime(end_time, d->start_time);
182 string Timer::timeStr(char separator) const
184 tm * timeinfo = localtime(&d->start_time);
185 // With less flexibility we could also use:
186 //strftime(buffer, 10, "%X", timeinfo);
188 out << setw(2) << setfill('0');
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;
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;
202 string Timer::dateStr(char separator) const
204 tm * timeinfo = localtime(&d->start_time);
205 // With less flexibility we could also use:
206 //res = strftime(buffer, 10, "%d%m%y", timeinfo);
208 out << setw(2) << setfill('0') << timeinfo->tm_mday;
211 out << setw(2) << setfill('0') << timeinfo->tm_mon;
214 out << setw(2) << setfill('0') << timeinfo->tm_year - 100;
219 string Timer::toStr() const
221 tm * timeinfo = localtime(&d->start_time);
222 return asctime(timeinfo);
226 string Timer::currentToStr()
230 tm * timeinfo = localtime(¤t_time);
231 return asctime(timeinfo);