]> git.lyx.org Git - lyx.git/blob - src/support/Timeout.cpp
Extend the otexstream class to also report about paragraph breaks.
[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 #include <ctime>
21 #include <iomanip>
22 #include <sstream>
23
24 using namespace std;
25
26 namespace lyx {
27
28 /**
29  * This class executes the callback when the timeout expires
30  * using Qt mechanisms
31  */
32 class Timeout::Impl : QObject {
33 public:
34         ///
35         Impl(Timeout & owner) : owner_(owner), timeout_id(-1) {}
36         ///
37         bool running() const { return timeout_id != -1; }
38         /// start the timer
39         void start();
40         /// stop the timer
41         void stop();
42         /// reset
43         void reset();
44         ///
45         unsigned int timeout_ms() const { return owner_.timeout_ms; }
46
47 protected:
48         ///
49         void timerEvent(QTimerEvent *) { owner_.emit(); }
50
51 private:
52         ///
53         Timeout & owner_;
54         /// timout id
55         int timeout_id;
56 };
57
58
59 void Timeout::Impl::reset()
60 {
61         if (timeout_id != -1)
62                 killTimer(timeout_id);
63         timeout_id = -1;
64 }
65
66
67 void Timeout::Impl::start()
68 {
69         if (running())
70                 lyxerr << "Timeout::start: already running!" << endl;
71         timeout_id = startTimer(timeout_ms());
72 }
73
74
75 void Timeout::Impl::stop()
76 {
77         if (running())
78                 reset();
79 }
80
81
82 //
83 // Timeout
84 //
85
86 Timeout::Timeout(unsigned int msec, Type t)
87         : pimpl_(new Impl(*this)), type(t), timeout_ms(msec)
88 {}
89
90
91 Timeout::~Timeout()
92 {
93         pimpl_->stop();
94         delete pimpl_;
95 }
96
97
98 bool Timeout::running() const
99 {
100         return pimpl_->running();
101 }
102
103
104 void Timeout::start()
105 {
106         pimpl_->start();
107 }
108
109
110 void Timeout::stop()
111 {
112         pimpl_->stop();
113 }
114
115
116 void Timeout::restart()
117 {
118         pimpl_->stop();
119         pimpl_->start();
120 }
121
122
123 void Timeout::emit()
124 {
125         pimpl_->reset();
126         timeout();
127         if (type == CONTINUOUS)
128                 pimpl_->start();
129 }
130
131
132 Timeout & Timeout::setType(Type t)
133 {
134         type = t;
135         return *this;
136 }
137
138
139 Timeout & Timeout::setTimeout(unsigned int msec)
140 {
141         // Can't have a timeout of zero!
142         LASSERT(msec, msec = 1000);
143
144         timeout_ms = msec;
145         return *this;
146 }
147
148
149 struct Timer::Private
150 {
151         time_t start_time;
152 };
153
154
155 Timer::Timer() : d(new Private)
156 {
157         restart();
158 }
159
160
161 void Timer::restart()
162 {
163         time(&d->start_time);
164 }
165
166
167 int Timer::elapsed() const
168 {
169         time_t end_time;
170         time(&end_time);
171         double diff = difftime(end_time, d->start_time);
172         return int(diff);
173 }
174
175
176 string Timer::timeStr(char separator) const
177 {
178         tm * timeinfo = localtime(&d->start_time);
179         // With less flexibility we could also use:
180         //strftime(buffer, 10, "%X", timeinfo);
181         ostringstream out;
182         out << setw(2) << setfill('0');
183         if (separator) {
184                 out << separator << setw(2) << setfill('0') << timeinfo->tm_hour
185                     << separator << setw(2) << setfill('0') << timeinfo->tm_min
186                     << separator << setw(2) << setfill('0') << timeinfo->tm_sec;
187         } else {
188                 out << setw(2) << setfill('0') << timeinfo->tm_hour
189                     << setw(2) << setfill('0') << timeinfo->tm_min
190                     << setw(2) << setfill('0') << timeinfo->tm_sec;
191         }
192         return out.str();
193 }
194
195
196 string Timer::dateStr(char separator) const
197 {
198         tm * timeinfo = localtime(&d->start_time);
199         // With less flexibility we could also use:
200         //res = strftime(buffer, 10, "%d%m%y", timeinfo);
201         ostringstream out;
202         out << setw(2) << setfill('0') << timeinfo->tm_mday;
203         if (separator)
204                 out << separator;
205         out << setw(2) << setfill('0') << timeinfo->tm_mon;
206         if (separator)
207                 out << separator;
208         out << setw(2) << setfill('0') << timeinfo->tm_year - 100;
209         return out.str();
210 }
211
212
213 string Timer::toStr() const
214 {
215         tm * timeinfo = localtime(&d->start_time);
216         return asctime(timeinfo);
217 }
218
219
220 string Timer::currentToStr()
221 {
222         time_t current_time;
223         time(&current_time);
224         tm * timeinfo = localtime(&current_time);
225         return asctime(timeinfo);
226 }
227
228 } // namespace lyx