2007-08-11 22:37:09 +00:00
|
|
|
/**
|
2007-11-12 20:18:19 +00:00
|
|
|
* \file Timeout.cpp
|
2007-08-11 22:37:09 +00:00
|
|
|
* This file is part of LyX, the document processor.
|
|
|
|
* Licence details can be found in the file COPYING.
|
|
|
|
*
|
|
|
|
* \author John Levon
|
|
|
|
*
|
|
|
|
* Full author contact details are available in file CREDITS.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
2008-04-11 12:12:57 +00:00
|
|
|
#include "support/assert.h"
|
2007-11-29 07:04:28 +00:00
|
|
|
#include "support/Timeout.h"
|
|
|
|
#include "support/debug.h"
|
|
|
|
|
2007-08-11 22:37:09 +00:00
|
|
|
#include <QObject>
|
|
|
|
#include <QTimerEvent>
|
|
|
|
|
2007-12-12 19:28:07 +00:00
|
|
|
using namespace std;
|
|
|
|
|
2007-08-11 22:37:09 +00:00
|
|
|
namespace lyx {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This class executes the callback when the timeout expires
|
|
|
|
* using Qt mechanisms
|
|
|
|
*/
|
2007-11-12 20:18:19 +00:00
|
|
|
class Timeout::Impl : QObject {
|
2007-08-11 22:37:09 +00:00
|
|
|
public:
|
|
|
|
///
|
2007-11-12 20:18:19 +00:00
|
|
|
Impl(Timeout & owner) : owner_(owner), timeout_id(-1) {}
|
2007-08-11 22:37:09 +00:00
|
|
|
///
|
2008-04-11 12:12:57 +00:00
|
|
|
bool running() const { return timeout_id != -1; }
|
2007-08-11 22:37:09 +00:00
|
|
|
/// start the timer
|
2007-11-12 20:18:19 +00:00
|
|
|
void start();
|
2007-08-11 22:37:09 +00:00
|
|
|
/// stop the timer
|
2007-11-12 20:18:19 +00:00
|
|
|
void stop();
|
2007-08-11 22:37:09 +00:00
|
|
|
/// reset
|
2007-11-12 20:18:19 +00:00
|
|
|
void reset();
|
|
|
|
///
|
|
|
|
unsigned int timeout_ms() const { return owner_.timeout_ms; }
|
2007-08-11 22:37:09 +00:00
|
|
|
|
|
|
|
protected:
|
2008-04-11 12:12:57 +00:00
|
|
|
///
|
2007-11-12 20:18:19 +00:00
|
|
|
void timerEvent(QTimerEvent *) { owner_.emit(); }
|
2007-08-11 22:37:09 +00:00
|
|
|
|
|
|
|
private:
|
2007-11-12 20:18:19 +00:00
|
|
|
///
|
|
|
|
Timeout & owner_;
|
2007-08-11 22:37:09 +00:00
|
|
|
/// timout id
|
|
|
|
int timeout_id;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2007-11-12 20:18:19 +00:00
|
|
|
void Timeout::Impl::reset()
|
2007-08-11 22:37:09 +00:00
|
|
|
{
|
|
|
|
if (timeout_id != -1)
|
|
|
|
killTimer(timeout_id);
|
|
|
|
timeout_id = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-11-12 20:18:19 +00:00
|
|
|
void Timeout::Impl::start()
|
2007-08-11 22:37:09 +00:00
|
|
|
{
|
|
|
|
if (running())
|
2007-12-12 19:28:07 +00:00
|
|
|
lyxerr << "Timeout::start: already running!" << endl;
|
2007-08-11 22:37:09 +00:00
|
|
|
timeout_id = startTimer(timeout_ms());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-11-12 20:18:19 +00:00
|
|
|
void Timeout::Impl::stop()
|
2007-08-11 22:37:09 +00:00
|
|
|
{
|
|
|
|
if (running())
|
|
|
|
reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//
|
2007-11-12 20:18:19 +00:00
|
|
|
// Timeout
|
2007-08-11 22:37:09 +00:00
|
|
|
//
|
|
|
|
|
2007-11-12 20:18:19 +00:00
|
|
|
Timeout::Timeout(unsigned int msec, Type t)
|
|
|
|
: pimpl_(new Impl(*this)), type(t), timeout_ms(msec)
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
2007-08-11 22:37:09 +00:00
|
|
|
Timeout::~Timeout()
|
|
|
|
{
|
|
|
|
pimpl_->stop();
|
2007-11-12 20:18:19 +00:00
|
|
|
delete pimpl_;
|
2007-08-11 22:37:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool Timeout::running() const
|
|
|
|
{
|
|
|
|
return pimpl_->running();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Timeout::start()
|
|
|
|
{
|
|
|
|
pimpl_->start();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Timeout::stop()
|
|
|
|
{
|
|
|
|
pimpl_->stop();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Timeout::restart()
|
|
|
|
{
|
|
|
|
pimpl_->stop();
|
|
|
|
pimpl_->start();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Timeout::emit()
|
|
|
|
{
|
|
|
|
pimpl_->reset();
|
|
|
|
timeout();
|
|
|
|
if (type == CONTINUOUS)
|
|
|
|
pimpl_->start();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Timeout & Timeout::setType(Type t)
|
|
|
|
{
|
|
|
|
type = t;
|
2007-11-12 20:18:19 +00:00
|
|
|
return *this;
|
2007-08-11 22:37:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Timeout & Timeout::setTimeout(unsigned int msec)
|
|
|
|
{
|
|
|
|
// Can't have a timeout of zero!
|
2008-04-10 21:49:34 +00:00
|
|
|
LASSERT(msec, /**/);
|
2007-08-11 22:37:09 +00:00
|
|
|
|
|
|
|
timeout_ms = msec;
|
2007-11-12 20:18:19 +00:00
|
|
|
return *this;
|
2007-08-11 22:37:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
} // namespace lyx
|