-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprogress_gui.py
119 lines (101 loc) · 3.59 KB
/
progress_gui.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
# -*- coding:utf-8 -*-
#
# https://github.com/sirius-fan/FastWordQuery/blob/master/src/gui/progress.py
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# any later version; http://www.gnu.org/copyleft/gpl.html.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import time
from aqt.qt import *
__all__ = ["ProgressWindow"]
_INFO_TEMPLATE = "".join(
[
"<strong>" + "transferring" + "</strong>",
"<p>" + 45 * "-" + "</p>",
"<p>" + "{}" + " processed" + "</p>",
]
)
class ProgressWindow(object):
"""
Query progress window
"""
def __init__(self, mw):
self.mw = mw
self.app = QApplication.instance()
self._win = None
self._count = 0
self._last_update = 0
self._first_time = 0
self._disabled = False
def update_labels(self, count):
if self.abort():
return
self._count = count
number_info = _INFO_TEMPLATE.format(self._count)
self._update(number_info, self._count)
self._win.adjustSize()
self.app.processEvents()
def update_title(self, title):
if self.abort():
return
self._win.setWindowTitle(title)
def start(self, max=0, min=0, label=None, parent=None):
self._count = 0
# setup window
label = label or "Processing..."
parent = parent or self.app.activeWindow() or self.mw
self._win = QProgressDialog(label, "", min, max, parent)
self._win.setWindowModality(Qt.WindowModality.ApplicationModal)
self._win.setCancelButton(None)
self._win.canceled.connect(self.finish)
self._win.setWindowTitle("Processing")
# TODO
self._win.setModal(True)
self._win.setWindowFlags(
self._win.windowFlags() & ~Qt.WindowType.WindowContextHelpButtonHint
)
self._win.setAutoReset(True)
self._win.setAutoClose(True)
self._win.setMinimum(0)
self._win.setMaximum(max)
# we need to manually manage minimum time to show, as qt gets confused
# by the db handler
# self._win.setMinimumDuration(100000)
self._first_time = time.time()
self._last_update = time.time()
self._disabled = False
self._win.show()
self._win.setValue(0)
self.app.processEvents()
def abort(self):
# self.aborted = True
return self._win.wasCanceled()
def finish(self):
self._unset_busy()
if self._win:
self._win.hide()
self._win.destroy()
def _update(self, label, value, process=True):
elapsed = time.time() - self._last_update
if label:
self._win.setLabelText(label)
if value:
self._win.setValue(value)
if process and elapsed >= 0.2:
self.app.processEvents(QEventLoop.ProcessEventsFlag.ExcludeUserInputEvents)
self._last_update = time.time()
def _set_busy(self):
self._disabled = True
self.mw.app.setOverrideCursor(QCursor(Qt.WaitCursor))
def _unset_busy(self):
self._disabled = False
self.app.restoreOverrideCursor()