/
progress.py
240 lines (207 loc) · 7.86 KB
/
progress.py
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
# Copyright: Ankitects Pty Ltd and contributors
# License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
from __future__ import annotations
import time
import aqt.forms
from aqt.qt import *
from aqt.utils import disable_help_button, tr
# Progress info
##########################################################################
class ProgressManager:
def __init__(self, mw: aqt.AnkiQt) -> None:
self.mw = mw
self.app = mw.app
self.inDB = False
self.blockUpdates = False
self._show_timer: QTimer | None = None
self._busy_cursor_timer: QTimer | None = None
self._win: ProgressDialog | None = None
self._levels = 0
# Safer timers
##########################################################################
# A custom timer which avoids firing while a progress dialog is active
# (likely due to some long-running DB operation)
def timer(
self, ms: int, func: Callable, repeat: bool, requiresCollection: bool = True
) -> QTimer:
"""Create and start a standard Anki timer.
If the timer fires while a progress window is shown:
- if it is a repeating timer, it will wait the same delay again
- if it is non-repeating, it will try again in 100ms
If requiresCollection is True, the timer will not fire if the
collection has been unloaded. Setting it to False will allow the
timer to fire even when there is no collection, but will still
only fire when there is no current progress dialog."""
def handler() -> None:
if requiresCollection and not self.mw.col:
# no current collection; timer is no longer valid
print(f"Ignored progress func as collection unloaded: {repr(func)}")
return
if not self._levels:
# no current progress; safe to fire
func()
else:
if repeat:
# skip this time; we'll fire again
pass
else:
# retry in 100ms
self.timer(100, func, False, requiresCollection)
t = QTimer(self.mw)
if not repeat:
t.setSingleShot(True)
qconnect(t.timeout, handler)
t.start(ms)
return t
# Creating progress dialogs
##########################################################################
def start(
self,
max: int = 0,
min: int = 0,
label: str | None = None,
parent: QWidget | None = None,
immediate: bool = False,
) -> ProgressDialog | None:
self._levels += 1
if self._levels > 1:
return None
# setup window
parent = parent or self.app.activeWindow()
if not parent and self.mw.isVisible():
parent = self.mw
label = label or tr.qt_misc_processing()
self._win = ProgressDialog(parent)
self._win.form.progressBar.setMinimum(min)
self._win.form.progressBar.setMaximum(max)
self._win.form.progressBar.setTextVisible(False)
self._win.form.label.setText(label)
self._win.setWindowTitle("Anki")
self._win.setWindowModality(Qt.WindowModality.ApplicationModal)
self._win.setMinimumWidth(300)
self._busy_cursor_timer = QTimer(self.mw)
self._busy_cursor_timer.setSingleShot(True)
self._busy_cursor_timer.start(300)
qconnect(self._busy_cursor_timer.timeout, self._set_busy_cursor)
self._shown: float = 0
self._counter = min
self._min = min
self._max = max
self._firstTime = time.time()
self._show_timer = QTimer(self.mw)
self._show_timer.setSingleShot(True)
self._show_timer.start(immediate and 100 or 600)
qconnect(self._show_timer.timeout, self._on_show_timer)
return self._win
def update(
self,
label: str | None = None,
value: int | None = None,
process: bool = True,
maybeShow: bool = True,
max: int | None = None,
) -> None:
# print self._min, self._counter, self._max, label, time.time() - self._lastTime
if not self.mw.inMainThread():
print("progress.update() called on wrong thread")
return
if maybeShow:
self._maybeShow()
if not self._shown:
return
if label:
self._win.form.label.setText(label)
self._max = max or 0
self._win.form.progressBar.setMaximum(self._max)
if self._max:
self._counter = value or (self._counter + 1)
self._win.form.progressBar.setValue(self._counter)
def finish(self) -> None:
self._levels -= 1
self._levels = max(0, self._levels)
if self._levels == 0:
if self._win:
self._closeWin()
if self._busy_cursor_timer:
self._busy_cursor_timer.stop()
self._busy_cursor_timer = None
self._restore_cursor()
if self._show_timer:
self._show_timer.stop()
self._show_timer = None
def clear(self) -> None:
"Restore the interface after an error."
if self._levels:
self._levels = 1
self.finish()
def _maybeShow(self) -> None:
if not self._levels:
return
if self._shown:
return
delta = time.time() - self._firstTime
if delta > 0.5:
self._showWin()
def _showWin(self) -> None:
self._shown = time.time()
self._win.show()
def _closeWin(self) -> None:
if self._shown:
while True:
# give the window system a second to present
# window before we close it again - fixes
# progress window getting stuck, especially
# on ubuntu 16.10+
elap = time.time() - self._shown
if elap >= 0.5:
break
self.app.processEvents(QEventLoop.ProcessEventsFlag.ExcludeUserInputEvents) # type: ignore #possibly related to https://github.com/python/mypy/issues/6910
# if the parent window has been deleted, the progress dialog may have
# already been dropped; delete it if it hasn't been
if not sip.isdeleted(self._win):
self._win.cancel()
self._win = None
self._shown = 0
def _set_busy_cursor(self) -> None:
self.mw.app.setOverrideCursor(QCursor(Qt.CursorShape.WaitCursor))
def _restore_cursor(self) -> None:
self.app.restoreOverrideCursor()
def busy(self) -> int:
"True if processing."
return self._levels
def _on_show_timer(self) -> None:
self._show_timer = None
self._showWin()
def want_cancel(self) -> bool:
win = self._win
if win:
return win.wantCancel
else:
return False
def set_title(self, title: str) -> None:
win = self._win
if win:
win.setWindowTitle(title)
class ProgressDialog(QDialog):
def __init__(self, parent: QWidget) -> None:
QDialog.__init__(self, parent)
disable_help_button(self)
self.form = aqt.forms.progress.Ui_Dialog()
self.form.setupUi(self)
self._closingDown = False
self.wantCancel = False
# required for smooth progress bars
self.form.progressBar.setStyleSheet("QProgressBar::chunk { width: 1px; }")
def cancel(self) -> None:
self._closingDown = True
self.hide()
def closeEvent(self, evt: QCloseEvent) -> None:
if self._closingDown:
evt.accept()
else:
self.wantCancel = True
evt.ignore()
def keyPressEvent(self, evt: QKeyEvent) -> None:
if evt.key() == Qt.Key.Key_Escape:
evt.ignore()
self.wantCancel = True