X-Git-Url: https://git.lyx.org/gitweb/?a=blobdiff_plain;f=src%2Fsupport%2FTimeout.cpp;h=449fee033b77e1a6fc698b7595ac3d432ed47ec3;hb=b96ce9a9c101a711ef8a1cdd5d6fe812a18966da;hp=e2123ac7bcdbc0c7f0e080035da62e4622f74cbe;hpb=848c89564fce9c4bfec79b915746dc6bc47abc3d;p=lyx.git diff --git a/src/support/Timeout.cpp b/src/support/Timeout.cpp index e2123ac7bc..449fee033b 100644 --- a/src/support/Timeout.cpp +++ b/src/support/Timeout.cpp @@ -1,5 +1,5 @@ /** - * \file qtTimeout.cpp + * \file Timeout.cpp * This file is part of LyX, the document processor. * Licence details can be found in the file COPYING. * @@ -10,12 +10,18 @@ #include +#include "support/lassert.h" +#include "support/Timeout.h" +#include "support/debug.h" + #include #include -#include "debug.h" -#include "Timeout.h" +#include +#include +#include +using namespace std; namespace lyx { @@ -23,46 +29,34 @@ namespace lyx { * This class executes the callback when the timeout expires * using Qt mechanisms */ -class qtTimeout : QObject, public Timeout::Impl { +class Timeout::Impl : QObject { public: /// - qtTimeout(Timeout & owner_); + Impl(Timeout & owner) : owner_(owner), timeout_id(-1) {} /// - virtual bool running() const; + bool running() const { return timeout_id != -1; } /// start the timer - virtual void start(); + void start(); /// stop the timer - virtual void stop(); + void stop(); /// reset - virtual void reset(); + void reset(); + /// + unsigned int timeout_ms() const { return owner_.timeout_ms; } protected: - /// slot - virtual void timerEvent(QTimerEvent *); + /// + void timerEvent(QTimerEvent *) { owner_.emit(); } private: + /// + Timeout & owner_; /// timout id int timeout_id; }; -Timeout::Timeout(unsigned int msec, Type t) - : pimpl_(new qtTimeout(*this)), type(t), timeout_ms(msec) -{} - - -qtTimeout::qtTimeout(Timeout & owner) - : Timeout::Impl(owner), timeout_id(-1) -{} - - -void qtTimeout::timerEvent(QTimerEvent *) -{ - emit(); -} - - -void qtTimeout::reset() +void Timeout::Impl::reset() { if (timeout_id != -1) killTimer(timeout_id); @@ -70,21 +64,15 @@ void qtTimeout::reset() } -bool qtTimeout::running() const -{ - return timeout_id != -1; -} - - -void qtTimeout::start() +void Timeout::Impl::start() { if (running()) - lyxerr << "Timeout::start: already running!" << std::endl; + lyxerr << "Timeout::start: already running!" << endl; timeout_id = startTimer(timeout_ms()); } -void qtTimeout::stop() +void Timeout::Impl::stop() { if (running()) reset(); @@ -92,12 +80,18 @@ void qtTimeout::stop() // -// Timeoute +// Timeout // +Timeout::Timeout(unsigned int msec, Type t) + : pimpl_(new Impl(*this)), type(t), timeout_ms(msec) +{} + + Timeout::~Timeout() { pimpl_->stop(); + delete pimpl_; } @@ -138,18 +132,103 @@ void Timeout::emit() Timeout & Timeout::setType(Type t) { type = t; - return * this; + return *this; } Timeout & Timeout::setTimeout(unsigned int msec) { // Can't have a timeout of zero! - BOOST_ASSERT(msec); + LASSERT(msec, msec = 1000); timeout_ms = msec; - return * this; + return *this; +} + + +struct Timer::Private +{ + time_t start_time; +}; + + +Timer::Timer() : d(new Private) +{ + restart(); +} + + +Timer::~Timer() +{ + delete d; } +void Timer::restart() +{ + time(&d->start_time); +} + + +int Timer::elapsed() const +{ + time_t end_time; + time(&end_time); + double diff = difftime(end_time, d->start_time); + return int(diff); +} + + +string Timer::timeStr(char separator) const +{ + tm * timeinfo = localtime(&d->start_time); + // With less flexibility we could also use: + //strftime(buffer, 10, "%X", timeinfo); + ostringstream out; + out << setw(2) << setfill('0'); + if (separator) { + out << separator << setw(2) << setfill('0') << timeinfo->tm_hour + << separator << setw(2) << setfill('0') << timeinfo->tm_min + << separator << setw(2) << setfill('0') << timeinfo->tm_sec; + } else { + out << setw(2) << setfill('0') << timeinfo->tm_hour + << setw(2) << setfill('0') << timeinfo->tm_min + << setw(2) << setfill('0') << timeinfo->tm_sec; + } + return out.str(); +} + + +string Timer::dateStr(char separator) const +{ + tm * timeinfo = localtime(&d->start_time); + // With less flexibility we could also use: + //res = strftime(buffer, 10, "%d%m%y", timeinfo); + ostringstream out; + out << setw(2) << setfill('0') << timeinfo->tm_mday; + if (separator) + out << separator; + out << setw(2) << setfill('0') << timeinfo->tm_mon; + if (separator) + out << separator; + out << setw(2) << setfill('0') << timeinfo->tm_year - 100; + return out.str(); +} + + +string Timer::toStr() const +{ + tm * timeinfo = localtime(&d->start_time); + return asctime(timeinfo); +} + + +string Timer::currentToStr() +{ + time_t current_time; + time(¤t_time); + tm * timeinfo = localtime(¤t_time); + return asctime(timeinfo); +} + } // namespace lyx