src/corelib/kernel/qtimer.cpp

Go to the documentation of this file.
00001 /****************************************************************************
00002 **
00003 ** Copyright (C) 1992-2006 Trolltech ASA. All rights reserved.
00004 **
00005 ** This file is part of the QtCore module of the Qt Toolkit.
00006 **
00007 ** This file may be used under the terms of the GNU General Public
00008 ** License version 2.0 as published by the Free Software Foundation
00009 ** and appearing in the file LICENSE.GPL included in the packaging of
00010 ** this file.  Please review the following information to ensure GNU
00011 ** General Public Licensing requirements will be met:
00012 ** http://www.trolltech.com/products/qt/opensource.html
00013 **
00014 ** If you are unsure which license is appropriate for your use, please
00015 ** review the following information:
00016 ** http://www.trolltech.com/products/qt/licensing.html or contact the
00017 ** sales department at sales@trolltech.com.
00018 **
00019 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
00020 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
00021 **
00022 ****************************************************************************/
00023 
00024 #include "qtimer.h"
00025 #include "qabstracteventdispatcher.h"
00026 #include "qcoreapplication.h"
00027 
00113 static const int INV_TIMER = -1;                // invalid timer id
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)                        // stop running timer
00144         stop();
00145 }
00146 
00147 
00177 void QTimer::start()
00178 {
00179     if (id != INV_TIMER)                        // stop running 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     // need to kill the timer _before_ we emit timeout() in case the
00271     // slot connected to timeout calls processEvents()
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) {                        // create new timer
00344         QObject::killTimer(id);                        // restart timer
00345         id = QObject::startTimer(msec);
00346     }
00347 }
00348 

Generated on Thu Mar 15 11:53:34 2007 for Qt 4.2 User's Guide by  doxygen 1.5.1