00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include "qtimer.h"
00025 #include "qabstracteventdispatcher.h"
00026 #include "qcoreapplication.h"
00027
00113 static const int INV_TIMER = -1;
00114
00119 QTimer::QTimer(QObject *parent)
00120 : QObject(parent), id(INV_TIMER), inter(0), del(0), single(0), nulltimer(0)
00121 {
00122 }
00123
00124
00125 #ifdef QT3_SUPPORT
00126
00130 QTimer::QTimer(QObject *parent, const char *name)
00131 : QObject(parent), id(INV_TIMER), single(0), nulltimer(0)
00132 {
00133 setObjectName(QString::fromAscii(name));
00134 }
00135 #endif
00136
00141 QTimer::~QTimer()
00142 {
00143 if (id != INV_TIMER)
00144 stop();
00145 }
00146
00147
00177 void QTimer::start()
00178 {
00179 if (id != INV_TIMER)
00180 stop();
00181 nulltimer = (!inter && single);
00182 id = QObject::startTimer(inter);
00183 }
00184
00189 void QTimer::start(int msec)
00190 {
00191 setInterval(msec);
00192 start();
00193 }
00194
00195
00196 #ifdef QT3_SUPPORT
00197
00202 int QTimer::start(int msec, bool sshot)
00203 {
00204 if (id >=0 && nulltimer && !msec && sshot)
00205 return id;
00206 stop();
00207 setInterval(msec);
00208 setSingleShot(sshot);
00209 start();
00210 return timerId();
00211 }
00212 #endif
00213
00214
00221 void QTimer::stop()
00222 {
00223 if (id != INV_TIMER) {
00224 QObject::killTimer(id);
00225 id = INV_TIMER;
00226 }
00227 }
00228
00229
00233 void QTimer::timerEvent(QTimerEvent *e)
00234 {
00235 if (e->timerId() == id) {
00236 if (single)
00237 stop();
00238 emit timeout();
00239 }
00240 }
00241
00242 class QSingleShotTimer : public QObject
00243 {
00244 Q_OBJECT
00245 int timerId;
00246 public:
00247 ~QSingleShotTimer();
00248 QSingleShotTimer(int msec, QObject *r, const char * m);
00249 signals:
00250 void timeout();
00251 protected:
00252 void timerEvent(QTimerEvent *);
00253 };
00254
00255 QSingleShotTimer::QSingleShotTimer(int msec, QObject *receiver, const char *member)
00256 : QObject(QAbstractEventDispatcher::instance())
00257 {
00258 connect(this, SIGNAL(timeout()), receiver, member);
00259 timerId = startTimer(msec);
00260 }
00261
00262 QSingleShotTimer::~QSingleShotTimer()
00263 {
00264 if (timerId > 0)
00265 killTimer(timerId);
00266 }
00267
00268 void QSingleShotTimer::timerEvent(QTimerEvent *)
00269 {
00270
00271
00272 if (timerId > 0)
00273 killTimer(timerId);
00274 timerId = -1;
00275 emit timeout();
00276 delete this;
00277 }
00278
00279 #include "qtimer.moc"
00280
00312 void QTimer::singleShot(int msec, QObject *receiver, const char *member)
00313 {
00314 if (receiver && member)
00315 (void) new QSingleShotTimer(msec, receiver, member);
00316 }
00317
00340 void QTimer::setInterval(int msec)
00341 {
00342 inter = msec;
00343 if (id != INV_TIMER) {
00344 QObject::killTimer(id);
00345 id = QObject::startTimer(msec);
00346 }
00347 }
00348