VR-Forces 4.2 Class Documentation
recursiveTimer.h
Go to the documentation of this file.
1 /*******************************************************************************
2 ** Copyright (c) 2007 MAK Technologies, Inc.
3 ** All rights reserved.
4 *******************************************************************************/
5 /*******************************************************************************
6 ** $RCSfile: recursiveTimer.h,v $ $Revision: 1.3 $ $State: Exp $
7 *******************************************************************************/
8 
11 
12 #include <QTimer>
13 #include <QPointer>
14 #include <QMap>
15 #include <QTimerEvent>
16 #include <QApplication>
17 
18 #include "tmqt/tmqtDefines.h"
19 
27 
29 {
30  Q_OBJECT
31 public:
32  DtRecursiveTimer(QObject *parent = 0) : QObject(parent), counter(-1)
33  {
34  }
35 
37  {
38  qDeleteAll(timers.keys());
39  timers.clear();
40  }
41 
46  int start(QObject *object, int duration, bool singleShot = false)
47  {
48  TimerInfo info;
49  info.id = --counter;
50  info.object = object;
51  info.singleShot = singleShot;
52  QTimer *timer = new QTimer(this);
53  connect(timer, SIGNAL(timeout()), this, SLOT(onTimerInvoked()));
54  timers[timer] = info;
55  timer->start(duration);
56  return info.id;
57  }
58 
59  void stop(int id)
60  {
61  QMap<QTimer *, TimerInfo>::iterator iter = timers.begin();
62 
63  while(iter != timers.end())
64  {
65  if ((*iter).id == id)
66  {
67  QTimer* timer = iter.key();
68  timers.erase(iter);
69  delete timer;
70 
71  return;
72  }
73 
74  ++iter;
75  }
76  }
77 
78 private slots:
80  void onTimerInvoked()
81  {
82  QTimer *timer = qobject_cast<QTimer*>(sender());
83  Q_ASSERT(timers.contains(timer));
84  const TimerInfo &info = timers.value(timer);
85  if (info.object)
86  {
87  QTimerEvent *ev = new QTimerEvent(info.id);
88  qApp->postEvent(info.object, ev);
89  if (!info.singleShot)
90  {
91  return;
92  }
93  }
94  timers.remove(timer);
95  delete timer;
96  }
97 
98 private:
99 
100  struct TimerInfo
101  {
102  int id;
103  QPointer<QObject> object;
105  };
106 
107  QMap<QTimer *, TimerInfo> timers;
108  int counter;
109 };

Document ID: Generated on Sun Nov 24 19:49:21 EST 2013 from SVN revision 133924
Copyright © 2005-2013 VT MÄK. All Rights Reserved (www.mak.com)