comparison demos/browser/autosaver.d @ 73:7bfd46c330dc

more porting
author mandel
date Fri, 22 May 2009 10:59:00 +0000
parents 71b382c10ef6
children 37caa90ce503
comparison
equal deleted inserted replaced
72:b149ef2cb18b 73:7bfd46c330dc
38 ** $QT_END_LICENSE$ 38 ** $QT_END_LICENSE$
39 ** 39 **
40 ****************************************************************************/ 40 ****************************************************************************/
41 module autosaver; 41 module autosaver;
42 42
43 import QtCore.QObject;
44 import QtCore.QBasicTimer;
45 import QtCore.QTime;
46 43
47 import QtCore.QDir; 44 import qt.core.QObject;
48 import QtCore.QCoreApplication; 45 import qt.core.QBasicTimer;
49 import QtCore.QMetaObject; 46 import qt.core.QTime;
47 import qt.core.QDir;
48 import qt.core.QCoreApplication;
49 import qt.core.QMetaObject;
50
50 import QtDebug; 51 import QtDebug;
52
51 53
52 const uint AUTOSAVE_IN = 1000 * 3 // seconds 54 const uint AUTOSAVE_IN = 1000 * 3 // seconds
53 const uint MAXWAIT = 1000 * 15 // seconds 55 const uint MAXWAIT = 1000 * 15 // seconds
54 56
57
55 /* 58 /*
56 This class will call the save() slot on the parent object when the parent changes. 59 This class will call the save() slot on the parent object when the parent changes.
57 It will wait several seconds after changed() to combining multiple changes and 60 It will wait several seconds after changed() to combining multiple changes and
58 prevent continuous writing to disk. 61 prevent continuous writing to disk.
59 */ 62 */
63
60 class AutoSaver : public QObject { 64 class AutoSaver : public QObject {
61 65
62 Q_OBJECT 66 public:
67
68 this(QObject parent)
69 {
70 super(parent);
71 assert(parent);
72 }
73
74 ~this();
75 {
76 if (m_timer.isActive())
77 qWarning() << "AutoSaver: still active when destroyed, changes not saved.";
78 }
79
80 void saveIfNeccessary()
81 {
82 if (!m_timer.isActive())
83 return;
84 m_timer.stop();
85 m_firstChange = QTime();
86 if (!QMetaObject.invokeMethod(parent(), "save", Qt.DirectConnection)) {
87 qWarning() << "AutoSaver: error invoking slot save() on parent";
88 }
89 }
63 90
64 public: 91 public:
65 this(QObject *parent) 92
66 { 93 void changeOccurred();
67 super(parent); 94 {
68 Q_ASSERT(parent); 95 if (m_firstChange.isNull())
69 } 96 m_firstChange.start();
70 ~this(); 97
71 { 98 if (m_firstChange.elapsed() > MAXWAIT) {
72 if (m_timer.isActive()) 99 saveIfNeccessary();
73 qWarning() << "AutoSaver: still active when destroyed, changes not saved."; 100 } else {
74 } 101 m_timer.start(AUTOSAVE_IN, this);
75 void saveIfNeccessary() 102 }
76 { 103 }
77 if (!m_timer.isActive()) 104
78 return; 105 protected:
79 m_timer.stop(); 106
80 m_firstChange = QTime(); 107 void timerEvent(QTimerEvent event)
81 if (!QMetaObject::invokeMethod(parent(), "save", Qt::DirectConnection)) { 108 {
82 qWarning() << "AutoSaver: error invoking slot save() on parent"; 109 if (event.timerId() == m_timer.timerId()) {
83 } 110 saveIfNeccessary();
111 } else {
112 QObject.timerEvent(event);
113 }
114 }
115
116 private:
117
118 QBasicTimer m_timer;
119 QTime m_firstChange;
84 } 120 }
85 121
86 public slots:
87 void changeOccurred();
88 {
89 if (m_firstChange.isNull())
90 m_firstChange.start();
91
92 if (m_firstChange.elapsed() > MAXWAIT) {
93 saveIfNeccessary();
94 } else {
95 m_timer.start(AUTOSAVE_IN, this);
96 }
97 }
98
99 protected:
100 void timerEvent(QTimerEvent *event)
101 {
102 if (event.timerId() == m_timer.timerId()) {
103 saveIfNeccessary();
104 } else {
105 QObject::timerEvent(event);
106 }
107 }
108
109
110 private:
111 QBasicTimer m_timer;
112 QTime m_firstChange;
113 }
114