VR-Forces 4.0.4 Class Documentation
include/tmqt/recursiveTimer.h
Go to the documentation of this file.
00001 /*******************************************************************************
00002 ** Copyright (c) 2007 MAK Technologies, Inc.
00003 ** All rights reserved.
00004 *******************************************************************************/
00005 /*******************************************************************************
00006 ** $RCSfile: recursiveTimer.h,v $ $Revision: 1.3 $ $State: Exp $
00007 *******************************************************************************/
00008 
00011 
00012 #include <QTimer>
00013 #include <QPointer>
00014 #include <QMap>
00015 #include <QTimerEvent>
00016 #include <QApplication>
00017 
00018 #include "tmqt/tmqtDefines.h"
00019 
00027 
00028 class DT_DLL_tmqt DtRecursiveTimer : public QObject
00029 {
00030    Q_OBJECT
00031 public:
00032    DtRecursiveTimer(QObject *parent = 0) : QObject(parent), counter(-1)
00033    {
00034    }
00035 
00036    ~DtRecursiveTimer()
00037    {
00038       qDeleteAll(timers.keys());
00039       timers.clear();
00040    }
00041 
00046    int start(QObject *object, int duration, bool singleShot = false)
00047    {
00048       TimerInfo info;
00049       info.id = --counter;
00050       info.object = object;
00051       info.singleShot = singleShot;
00052       QTimer *timer = new QTimer(this);
00053       connect(timer, SIGNAL(timeout()), this, SLOT(onTimerInvoked()));
00054       timers[timer] = info;
00055       timer->start(duration);
00056       return info.id;
00057    }
00058    
00059    void stop(int id)
00060    {
00061       QMap<QTimer *, TimerInfo>::iterator iter = timers.begin();
00062 
00063       while(iter != timers.end())
00064       {
00065          if ((*iter).id == id)
00066          {
00067             QTimer* timer = iter.key();
00068             timers.erase(iter);
00069             delete timer;
00070 
00071             return;
00072          }
00073 
00074          ++iter;
00075       }
00076    }
00077 
00078 private slots:
00080    void onTimerInvoked()
00081    {
00082       QTimer *timer = qobject_cast<QTimer*>(sender());
00083       Q_ASSERT(timers.contains(timer));
00084       const TimerInfo &info = timers.value(timer);
00085       if (info.object)
00086       {
00087          QTimerEvent *ev = new QTimerEvent(info.id);
00088          qApp->postEvent(info.object, ev);
00089          if (!info.singleShot)
00090          {
00091             return;
00092          }
00093       }
00094       timers.remove(timer);
00095       delete timer;
00096    }
00097    
00098 private:
00099 
00100    struct TimerInfo
00101    {
00102       int id;
00103       QPointer<QObject> object;
00104       bool singleShot;
00105    };
00106 
00107    QMap<QTimer *, TimerInfo> timers;
00108    int counter;
00109 };

Document ID: Generated on Fri Jun 29 16:33:32 EDT 2012 from SVN revision 116588
Copyright © 2005-2012 VT MÄK Inc. All Rights Reserved (www.mak.com)