12 |
12 |
13 class AutoSaver(QObject): |
13 class AutoSaver(QObject): |
14 """ |
14 """ |
15 Class implementing the auto saver. |
15 Class implementing the auto saver. |
16 """ |
16 """ |
|
17 |
17 AUTOSAVE_IN = 1000 * 3 |
18 AUTOSAVE_IN = 1000 * 3 |
18 MAXWAIT = 1000 * 15 |
19 MAXWAIT = 1000 * 15 |
19 |
20 |
20 def __init__(self, parent, save): |
21 def __init__(self, parent, save): |
21 """ |
22 """ |
22 Constructor |
23 Constructor |
23 |
24 |
24 @param parent reference to the parent object (QObject) |
25 @param parent reference to the parent object (QObject) |
25 @param save slot to be called to perform the save operation |
26 @param save slot to be called to perform the save operation |
26 @exception RuntimeError raised, if no parent is given |
27 @exception RuntimeError raised, if no parent is given |
27 """ |
28 """ |
28 super().__init__(parent) |
29 super().__init__(parent) |
29 |
30 |
30 if parent is None: |
31 if parent is None: |
31 raise RuntimeError("AutoSaver: parent must not be None.") |
32 raise RuntimeError("AutoSaver: parent must not be None.") |
32 |
33 |
33 self.__save = save |
34 self.__save = save |
34 |
35 |
35 self.__timer = QBasicTimer() |
36 self.__timer = QBasicTimer() |
36 self.__firstChange = None |
37 self.__firstChange = None |
37 |
38 |
38 def changeOccurred(self): |
39 def changeOccurred(self): |
39 """ |
40 """ |
40 Public slot handling a change. |
41 Public slot handling a change. |
41 """ |
42 """ |
42 if self.__firstChange is None: |
43 if self.__firstChange is None: |
43 self.__firstChange = QTime.currentTime() |
44 self.__firstChange = QTime.currentTime() |
44 |
45 |
45 if self.__firstChange.msecsTo(QTime.currentTime()) > self.MAXWAIT: |
46 if self.__firstChange.msecsTo(QTime.currentTime()) > self.MAXWAIT: |
46 self.saveIfNeccessary() |
47 self.saveIfNeccessary() |
47 else: |
48 else: |
48 self.__timer.start(self.AUTOSAVE_IN, self) |
49 self.__timer.start(self.AUTOSAVE_IN, self) |
49 |
50 |
50 def timerEvent(self, evt): |
51 def timerEvent(self, evt): |
51 """ |
52 """ |
52 Protected method handling timer events. |
53 Protected method handling timer events. |
53 |
54 |
54 @param evt reference to the timer event (QTimerEvent) |
55 @param evt reference to the timer event (QTimerEvent) |
55 """ |
56 """ |
56 if evt.timerId() == self.__timer.timerId(): |
57 if evt.timerId() == self.__timer.timerId(): |
57 self.saveIfNeccessary() |
58 self.saveIfNeccessary() |
58 else: |
59 else: |
59 super().timerEvent(evt) |
60 super().timerEvent(evt) |
60 |
61 |
61 def saveIfNeccessary(self): |
62 def saveIfNeccessary(self): |
62 """ |
63 """ |
63 Public method to activate the save operation. |
64 Public method to activate the save operation. |
64 """ |
65 """ |
65 if not self.__timer.isActive(): |
66 if not self.__timer.isActive(): |
66 return |
67 return |
67 |
68 |
68 self.__timer.stop() |
69 self.__timer.stop() |
69 self.__firstChange = None |
70 self.__firstChange = None |
70 self.__save() |
71 self.__save() |