Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

import time 

from PythonQt import QtCore 

import traceback 

 

class TimerCallback(object): 

 

    def __init__(self, targetFps=30, callback=None): 

        ''' 

        Construct TimerCallback.  The targetFps defines frames per second, the 

        frequency for the ticks() callback method. 

        ''' 

        self.targetFps = targetFps 

        self.timer = QtCore.QTimer() 

        self.useScheduledTimer = True 

        self.timer.setSingleShot(True) 

 

        self.singleShotTimer = QtCore.QTimer() 

        self.singleShotTimer.setSingleShot(True) 

        self.callback = callback 

 

    def start(self): 

        ''' 

        Start the timer. 

        ''' 

        if not self.timer.isActive(): 

            self.timer.connect('timeout()', self._timerEvent) 

 

        self.startTime = time.time() 

        self.lastTickTime = self.startTime 

 

        if self.useScheduledTimer: 

            self.timer.start(0) 

        else: 

            self.timer.start(int(1000.0 / self.targetFps)) 

 

    def stop(self): 

        ''' 

        Stop the timer. 

        ''' 

        self.timer.stop() 

        self.timer.disconnect('timeout()', self._timerEvent) 

 

    def tick(self): 

        ''' 

        Timer event callback method.  Subclasses can override this method. 

        ''' 

        if self.callback: 

            return self.callback() 

 

    def isActive(self): 

        ''' 

        Return whether or not the timer is active. 

        ''' 

        return self.timer.isActive() 

 

    def disableScheduledTimer(self): 

        self.useScheduledTimer = False 

        self.timer.setSingleShot(False) 

 

    def singleShot(self, timeoutInSeconds): 

        if not self.singleShotTimer.isActive(): 

            self.singleShotTimer.connect('timeout()', self._singleShotTimerEvent) 

        self.singleShotTimer.start(int(timeoutInSeconds * 1000)) 

 

    def _singleShotTimerEvent(self): 

        self.tick() 

        self.singleShotTimer.disconnect('timeout()', self._singleShotTimerEvent) 

 

    def _schedule(self, elapsedTimeInSeconds): 

        ''' 

        This method is given an elapsed time since the start of the last 

        call to ticks().  It schedules a timer event to acheive the targetFps. 

        ''' 

        fpsDelayMilliseconds = int(1000.0 / self.targetFps) 

        elapsedMilliseconds = int(elapsedTimeInSeconds*1000.0) 

        waitMilliseconds = fpsDelayMilliseconds - elapsedMilliseconds 

        self.timer.start(waitMilliseconds if waitMilliseconds > 0 else 1) 

 

    def _timerEvent(self): 

        ''' 

        Internal timer callback method.  Calls tick() and measures elapsed time. 

        ''' 

        startTime = time.time() 

        self.elapsed = startTime - self.lastTickTime 

 

        try: 

            result = self.tick() 

        except: 

            self.stop() 

            raise 

 

        if result is not False: 

            self.lastTickTime = startTime 

            if self.useScheduledTimer: 

                self._schedule(time.time() - startTime) 

        else: 

            self.stop()