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/Timeout.h"
15 #include "support/lassert.h"
16 #include "support/debug.h"
19 #include <QTimerEvent>
30 * This class executes the callback when the timeout expires
33 class Timeout::Impl : QObject {
36 Impl(Timeout & owner) : owner_(owner), timeout_id(-1) {}
38 bool running() const { return timeout_id != -1; }
46 unsigned int timeout_ms() const { return owner_.timeout_ms; }
50 void timerEvent(QTimerEvent *) override { owner_.emit(); }
60 void Timeout::Impl::reset()
63 killTimer(timeout_id);
68 void Timeout::Impl::start()
71 lyxerr << "Timeout::start: already running!" << endl;
72 timeout_id = startTimer(timeout_ms());
76 void Timeout::Impl::stop()
87 Timeout::Timeout(unsigned int msec, Type t)
88 : pimpl_(new Impl(*this)), type(t), timeout_ms(msec)
99 bool Timeout::running() const
101 return pimpl_->running();
105 void Timeout::start()
117 void Timeout::restart()
128 if (type == CONTINUOUS)
133 Timeout & Timeout::setType(Type t)
140 Timeout & Timeout::setTimeout(unsigned int msec)
142 // Can't have a timeout of zero!
143 LASSERT(msec, msec = 1000);
150 struct Timer::Private
156 Timer::Timer() : d(new Private)
168 void Timer::restart()
170 time(&d->start_time);
174 int Timer::elapsed() const
178 double diff = difftime(end_time, d->start_time);
183 string Timer::timeStr(char separator) const
185 tm * timeinfo = localtime(&d->start_time);
186 // With less flexibility we could also use:
187 //strftime(buffer, 10, "%X", timeinfo);
189 out << setw(2) << setfill('0');
191 out << separator << setw(2) << setfill('0') << timeinfo->tm_hour
192 << separator << setw(2) << setfill('0') << timeinfo->tm_min
193 << separator << setw(2) << setfill('0') << timeinfo->tm_sec;
195 out << setw(2) << setfill('0') << timeinfo->tm_hour
196 << setw(2) << setfill('0') << timeinfo->tm_min
197 << setw(2) << setfill('0') << timeinfo->tm_sec;
203 string Timer::dateStr(char separator) const
205 tm * timeinfo = localtime(&d->start_time);
206 // With less flexibility we could also use:
207 //res = strftime(buffer, 10, "%d%m%y", timeinfo);
209 out << setw(2) << setfill('0') << timeinfo->tm_mday;
212 out << setw(2) << setfill('0') << timeinfo->tm_mon;
215 out << setw(2) << setfill('0') << timeinfo->tm_year - 100;
220 string Timer::toStr() const
222 tm * timeinfo = localtime(&d->start_time);
223 return asctime(timeinfo);
227 string Timer::currentToStr()
231 tm * timeinfo = localtime(¤t_time);
232 return asctime(timeinfo);