]> git.lyx.org Git - lyx.git/blob - src/support/Timeout.cpp
add onoff support for "inset-modify changetype xxx" in include inset
[lyx.git] / src / support / Timeout.cpp
1 /**
2  * \file Timeout.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author John Levon
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "support/lassert.h"
14 #include "support/Timeout.h"
15 #include "support/debug.h"
16
17 #include <QObject>
18 #include <QTimerEvent>
19
20 using namespace std;
21
22 namespace lyx {
23
24 /**
25  * This class executes the callback when the timeout expires
26  * using Qt mechanisms
27  */
28 class Timeout::Impl : QObject {
29 public:
30         ///
31         Impl(Timeout & owner) : owner_(owner), timeout_id(-1) {}
32         ///
33         bool running() const { return timeout_id != -1; }
34         /// start the timer
35         void start();
36         /// stop the timer
37         void stop();
38         /// reset
39         void reset();
40         ///
41         unsigned int timeout_ms() const { return owner_.timeout_ms; }
42
43 protected:
44         ///
45         void timerEvent(QTimerEvent *) { owner_.emit(); }
46
47 private:
48         ///
49         Timeout & owner_;
50         /// timout id
51         int timeout_id;
52 };
53
54
55 void Timeout::Impl::reset()
56 {
57         if (timeout_id != -1)
58                 killTimer(timeout_id);
59         timeout_id = -1;
60 }
61
62
63 void Timeout::Impl::start()
64 {
65         if (running())
66                 lyxerr << "Timeout::start: already running!" << endl;
67         timeout_id = startTimer(timeout_ms());
68 }
69
70
71 void Timeout::Impl::stop()
72 {
73         if (running())
74                 reset();
75 }
76
77
78 //
79 // Timeout
80 //
81
82 Timeout::Timeout(unsigned int msec, Type t)
83         : pimpl_(new Impl(*this)), type(t), timeout_ms(msec)
84 {}
85
86
87 Timeout::~Timeout()
88 {
89         pimpl_->stop();
90         delete pimpl_;
91 }
92
93
94 bool Timeout::running() const
95 {
96         return pimpl_->running();
97 }
98
99
100 void Timeout::start()
101 {
102         pimpl_->start();
103 }
104
105
106 void Timeout::stop()
107 {
108         pimpl_->stop();
109 }
110
111
112 void Timeout::restart()
113 {
114         pimpl_->stop();
115         pimpl_->start();
116 }
117
118
119 void Timeout::emit()
120 {
121         pimpl_->reset();
122         timeout();
123         if (type == CONTINUOUS)
124                 pimpl_->start();
125 }
126
127
128 Timeout & Timeout::setType(Type t)
129 {
130         type = t;
131         return *this;
132 }
133
134
135 Timeout & Timeout::setTimeout(unsigned int msec)
136 {
137         // Can't have a timeout of zero!
138         LASSERT(msec, /**/);
139
140         timeout_ms = msec;
141         return *this;
142 }
143
144
145 } // namespace lyx