anki-word-query/addons21/fastwq/gui/progress.py

136 lines
4.5 KiB
Python
Raw Normal View History

2019-02-10 11:44:25 +08:00
# -*- coding:utf-8 -*-
2018-07-02 01:25:53 +08:00
#
2018-07-27 17:57:00 +08:00
# Copyright (C) 2018 sthoo <sth201807@gmail.com>
2018-07-02 01:25:53 +08:00
#
# Support: Report an issue at https://github.com/sth2018/FastWordQuery/issues
#
# 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/>.
2018-07-01 10:55:30 +08:00
import time
from collections import defaultdict
2018-07-30 16:00:47 +08:00
from aqt.qt import *
2018-07-01 10:55:30 +08:00
2019-02-10 11:44:25 +08:00
from ..context import APP_ICON
from ..lang import _
2018-07-01 10:55:30 +08:00
2018-07-14 15:24:21 +08:00
__all__ = ['ProgressWindow']
2018-07-13 11:31:12 +08:00
2018-07-14 15:24:21 +08:00
_INFO_TEMPLATE = u''.join([
2018-07-31 14:33:24 +08:00
u'<strong>' + _('QUERIED') + u'</strong>',
u'<p>' + 45 * u'-' + u'</p>',
u'<p>' + _('SUCCESS') + u' <b>{}</b> ' + _('WORDS') + u'</p>',
u'<p>' + _('SKIPED') + u' <b>{}</b> ' + _('WORDS') + u'</p>',
2019-02-10 11:44:25 +08:00
u'<p>' + _('UPDATE') + u' <b>{}</b> ' + _('FIELDS') + u'</p>',
2018-07-31 14:33:24 +08:00
u'<p>' + _('FAILURE') + u' <b>{}</b> ' + _('WORDS') + u'</p>',
2018-07-13 11:19:29 +08:00
])
2018-07-02 01:16:20 +08:00
class ProgressWindow(object):
"""
Query progress window
"""
2018-07-01 10:55:30 +08:00
def __init__(self, mw):
self.mw = mw
2018-07-30 16:00:47 +08:00
self.app = QApplication.instance()
2018-07-01 10:55:30 +08:00
self._win = None
self._msg_count = defaultdict(int)
2018-07-27 10:21:31 +08:00
self._last_update = 0
self._first_time = 0
self._disabled = False
2018-07-01 10:55:30 +08:00
def update_labels(self, data):
if self.abort():
return
2018-07-01 18:12:31 +08:00
2018-07-01 10:55:30 +08:00
if data.type == 'count':
self._msg_count.update(data)
else:
2018-07-01 18:12:31 +08:00
return
2018-07-01 10:55:30 +08:00
2018-07-13 11:19:29 +08:00
words_number, fields_number, fails_number, skips_number = (
self._msg_count.get('words_number', 0),
self._msg_count.get('fields_number', 0),
self._msg_count.get('fails_number', 0),
2019-02-10 11:44:25 +08:00
self._msg_count.get('skips_number', 0))
number_info = _INFO_TEMPLATE.format(words_number, skips_number,
fields_number, fails_number)
self._update(
label=number_info,
value=words_number + skips_number + fails_number)
2018-07-01 10:55:30 +08:00
self._win.adjustSize()
2018-08-27 01:21:59 +08:00
self.app.processEvents()
2018-07-01 10:55:30 +08:00
def update_title(self, title):
2018-07-02 01:16:20 +08:00
if self.abort():
return
2018-07-01 10:55:30 +08:00
self._win.setWindowTitle(title)
2018-07-02 01:16:20 +08:00
def start(self, max=0, min=0, label=None, parent=None):
2018-07-01 10:55:30 +08:00
self._msg_count.clear()
# setup window
label = label or _("Processing...")
2018-07-02 01:16:20 +08:00
parent = parent or self.app.activeWindow() or self.mw
2018-07-30 16:00:47 +08:00
self._win = QProgressDialog(label, '', min, max, parent)
self._win.setWindowModality(Qt.WindowModality.ApplicationModal)
2018-07-02 01:16:20 +08:00
self._win.setCancelButton(None)
self._win.canceled.connect(self.finish)
2018-07-31 14:33:24 +08:00
self._win.setWindowTitle("FastWQ - Querying...")
2023-01-31 21:04:53 +08:00
# TODO
2018-07-31 14:33:24 +08:00
self._win.setModal(True)
self._win.setWindowFlags(
self._win.windowFlags() & ~Qt.WindowType.WindowContextHelpButtonHint)
2018-07-31 14:33:24 +08:00
self._win.setWindowIcon(APP_ICON)
2018-07-27 10:21:31 +08:00
self._win.setAutoReset(True)
self._win.setAutoClose(True)
self._win.setMinimum(0)
self._win.setMaximum(max)
2018-07-01 10:55:30 +08:00
# we need to manually manage minimum time to show, as qt gets confused
# by the db handler
2018-07-02 01:16:20 +08:00
# self._win.setMinimumDuration(100000)
2018-07-27 10:21:31 +08:00
self._first_time = time.time()
self._last_update = time.time()
2018-07-01 10:55:30 +08:00
self._disabled = False
2018-07-02 01:16:20 +08:00
self._win.show()
2018-07-27 10:21:31 +08:00
self._win.setValue(0)
2018-07-02 01:16:20 +08:00
self.app.processEvents()
2018-07-01 10:55:30 +08:00
2018-07-02 01:16:20 +08:00
def abort(self):
# self.aborted = True
return self._win.wasCanceled()
def finish(self):
self._win.hide()
2018-07-27 10:21:31 +08:00
self._unset_busy()
2018-07-02 01:16:20 +08:00
self._win.destroy()
2018-07-27 10:21:31 +08:00
def _update(self, label, value, process=True):
elapsed = time.time() - self._last_update
2018-07-01 10:55:30 +08:00
if label:
self._win.setLabelText(label)
2018-07-27 10:21:31 +08:00
if value:
self._win.setValue(value)
2018-07-01 10:55:30 +08:00
if process and elapsed >= 0.2:
self.app.processEvents(QEventLoop.ProcessEventsFlag.ExcludeUserInputEvents)
2018-07-27 10:21:31 +08:00
self._last_update = time.time()
2018-07-01 10:55:30 +08:00
2018-07-27 10:21:31 +08:00
def _set_busy(self):
2018-07-01 10:55:30 +08:00
self._disabled = True
2018-07-30 16:00:47 +08:00
self.mw.app.setOverrideCursor(QCursor(Qt.WaitCursor))
2018-07-01 10:55:30 +08:00
2018-07-27 10:21:31 +08:00
def _unset_busy(self):
2018-07-01 10:55:30 +08:00
self._disabled = False
self.app.restoreOverrideCursor()