2019-02-05 04:59:03 +01:00
|
|
|
# Copyright: Ankitects Pty Ltd and contributors
|
2012-12-21 08:51:59 +01:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
|
|
|
|
|
|
|
import time
|
2019-12-20 10:19:03 +01:00
|
|
|
|
2017-12-28 09:31:05 +01:00
|
|
|
import aqt.forms
|
2019-03-04 02:58:34 +01:00
|
|
|
from anki.lang import _
|
2019-12-20 10:19:03 +01:00
|
|
|
from aqt.qt import *
|
2012-12-21 08:51:59 +01:00
|
|
|
|
|
|
|
# fixme: if mw->subwindow opens a progress dialog with mw as the parent, mw
|
|
|
|
# gets raised on finish on compiz. perhaps we should be using the progress
|
|
|
|
# dialog as the parent?
|
|
|
|
|
|
|
|
# Progress info
|
|
|
|
##########################################################################
|
|
|
|
|
|
|
|
|
2019-12-23 01:34:10 +01:00
|
|
|
class ProgressManager:
|
2012-12-21 08:51:59 +01:00
|
|
|
def __init__(self, mw):
|
|
|
|
self.mw = mw
|
|
|
|
self.app = QApplication.instance()
|
|
|
|
self.inDB = False
|
2013-01-28 22:45:29 +01:00
|
|
|
self.blockUpdates = False
|
2012-12-21 08:51:59 +01:00
|
|
|
self._win = None
|
|
|
|
self._levels = 0
|
|
|
|
|
|
|
|
# SQLite progress handler
|
|
|
|
##########################################################################
|
|
|
|
|
|
|
|
def setupDB(self, db):
|
|
|
|
"Install a handler in the current DB."
|
|
|
|
self.lastDbProgress = 0
|
|
|
|
self.inDB = False
|
2017-08-16 03:50:00 +02:00
|
|
|
db.set_progress_handler(self._dbProgress, 10000)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
|
|
|
def _dbProgress(self):
|
|
|
|
"Called from SQLite."
|
|
|
|
# do nothing if we don't have a progress window
|
|
|
|
if not self._win:
|
|
|
|
return
|
|
|
|
# make sure we're not executing too frequently
|
|
|
|
if (time.time() - self.lastDbProgress) < 0.01:
|
|
|
|
return
|
|
|
|
self.lastDbProgress = time.time()
|
|
|
|
# and we're in the main thread
|
|
|
|
if not self.mw.inMainThread():
|
|
|
|
return
|
|
|
|
# ensure timers don't fire
|
|
|
|
self.inDB = True
|
|
|
|
# handle GUI events
|
2013-01-28 22:45:29 +01:00
|
|
|
if not self.blockUpdates:
|
2019-03-04 07:54:22 +01:00
|
|
|
self._maybeShow()
|
|
|
|
self.app.processEvents(QEventLoop.ExcludeUserInputEvents)
|
2012-12-21 08:51:59 +01:00
|
|
|
self.inDB = False
|
|
|
|
|
2017-12-28 09:31:05 +01:00
|
|
|
# Safer timers
|
2012-12-21 08:51:59 +01:00
|
|
|
##########################################################################
|
|
|
|
# QTimer may fire in processEvents(). We provide a custom timer which
|
2017-12-28 09:31:05 +01:00
|
|
|
# automatically defers until the DB is not busy, and avoids running
|
|
|
|
# while a progress window is visible.
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2019-02-06 02:37:01 +01:00
|
|
|
def timer(self, ms, func, repeat, requiresCollection=True):
|
2012-12-21 08:51:59 +01:00
|
|
|
def handler():
|
2017-12-28 09:31:05 +01:00
|
|
|
if self.inDB or self._levels:
|
2012-12-21 08:51:59 +01:00
|
|
|
# retry in 100ms
|
2019-02-06 02:37:01 +01:00
|
|
|
self.timer(100, func, False, requiresCollection)
|
|
|
|
elif not self.mw.col and requiresCollection:
|
|
|
|
# ignore timer events that fire after collection has been
|
|
|
|
# unloaded
|
|
|
|
print("Ignored progress func as collection unloaded: %s" % repr(func))
|
2012-12-21 08:51:59 +01:00
|
|
|
else:
|
|
|
|
func()
|
2019-12-23 01:34:10 +01:00
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
t = QTimer(self.mw)
|
|
|
|
if not repeat:
|
|
|
|
t.setSingleShot(True)
|
2016-05-31 10:51:40 +02:00
|
|
|
t.timeout.connect(handler)
|
2012-12-21 08:51:59 +01:00
|
|
|
t.start(ms)
|
|
|
|
return t
|
|
|
|
|
|
|
|
# Creating progress dialogs
|
|
|
|
##########################################################################
|
|
|
|
|
2017-12-28 09:31:05 +01:00
|
|
|
class ProgressDialog(QDialog):
|
|
|
|
def __init__(self, parent):
|
|
|
|
QDialog.__init__(self, parent)
|
|
|
|
self.form = aqt.forms.progress.Ui_Dialog()
|
|
|
|
self.form.setupUi(self)
|
|
|
|
self._closingDown = False
|
|
|
|
self.wantCancel = False
|
|
|
|
|
|
|
|
def cancel(self):
|
|
|
|
self._closingDown = True
|
2018-01-14 10:29:56 +01:00
|
|
|
self.hide()
|
2017-12-28 09:31:05 +01:00
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
def closeEvent(self, evt):
|
2017-12-28 09:31:05 +01:00
|
|
|
if self._closingDown:
|
|
|
|
evt.accept()
|
|
|
|
else:
|
2019-12-23 01:34:10 +01:00
|
|
|
self.wantCancel = True
|
2012-12-21 08:51:59 +01:00
|
|
|
evt.ignore()
|
|
|
|
|
2017-01-17 08:15:50 +01:00
|
|
|
def keyPressEvent(self, evt):
|
|
|
|
if evt.key() == Qt.Key_Escape:
|
|
|
|
evt.ignore()
|
2019-12-23 01:34:10 +01:00
|
|
|
self.wantCancel = True
|
2017-01-17 08:15:50 +01:00
|
|
|
|
2020-01-30 23:47:05 +01:00
|
|
|
# note: immediate is no longer used
|
2017-12-28 09:31:05 +01:00
|
|
|
def start(self, max=0, min=0, label=None, parent=None, immediate=False):
|
2012-12-21 08:51:59 +01:00
|
|
|
self._levels += 1
|
|
|
|
if self._levels > 1:
|
|
|
|
return
|
|
|
|
# setup window
|
2017-08-16 05:20:29 +02:00
|
|
|
parent = parent or self.app.activeWindow()
|
|
|
|
if not parent and self.mw.isVisible():
|
|
|
|
parent = self.mw
|
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
label = label or _("Processing...")
|
2017-12-28 09:31:05 +01:00
|
|
|
self._win = self.ProgressDialog(parent)
|
|
|
|
self._win.form.progressBar.setMinimum(min)
|
|
|
|
self._win.form.progressBar.setMaximum(max)
|
2018-01-14 09:05:43 +01:00
|
|
|
self._win.form.progressBar.setTextVisible(False)
|
2017-12-28 09:31:05 +01:00
|
|
|
self._win.form.label.setText(label)
|
2012-12-21 08:51:59 +01:00
|
|
|
self._win.setWindowTitle("Anki")
|
2017-10-20 05:25:38 +02:00
|
|
|
self._win.setWindowModality(Qt.ApplicationModal)
|
2017-08-16 05:20:29 +02:00
|
|
|
self._win.setMinimumWidth(300)
|
2020-01-30 23:47:05 +01:00
|
|
|
self._setBusy()
|
|
|
|
self._shown = False
|
2012-12-21 08:51:59 +01:00
|
|
|
self._counter = min
|
|
|
|
self._min = min
|
|
|
|
self._max = max
|
|
|
|
self._firstTime = time.time()
|
2016-02-26 01:01:46 +01:00
|
|
|
self._lastUpdate = time.time()
|
2017-07-26 07:21:27 +02:00
|
|
|
self._updating = False
|
2017-01-17 08:15:50 +01:00
|
|
|
return self._win
|
2012-12-21 08:51:59 +01:00
|
|
|
|
|
|
|
def update(self, label=None, value=None, process=True, maybeShow=True):
|
2019-12-23 01:34:10 +01:00
|
|
|
# print self._min, self._counter, self._max, label, time.time() - self._lastTime
|
2017-07-26 07:21:27 +02:00
|
|
|
if self._updating:
|
|
|
|
return
|
2012-12-21 08:51:59 +01:00
|
|
|
if maybeShow:
|
|
|
|
self._maybeShow()
|
2018-01-14 03:16:47 +01:00
|
|
|
if not self._shown:
|
|
|
|
return
|
2016-02-26 01:01:46 +01:00
|
|
|
elapsed = time.time() - self._lastUpdate
|
2012-12-21 08:51:59 +01:00
|
|
|
if label:
|
2017-12-28 09:31:05 +01:00
|
|
|
self._win.form.label.setText(label)
|
2018-01-14 03:16:47 +01:00
|
|
|
if self._max:
|
2019-12-23 01:34:10 +01:00
|
|
|
self._counter = value or (self._counter + 1)
|
2017-12-28 09:31:05 +01:00
|
|
|
self._win.form.progressBar.setValue(self._counter)
|
2016-02-26 01:01:46 +01:00
|
|
|
if process and elapsed >= 0.2:
|
2017-07-26 07:21:27 +02:00
|
|
|
self._updating = True
|
2012-12-21 08:51:59 +01:00
|
|
|
self.app.processEvents(QEventLoop.ExcludeUserInputEvents)
|
2017-07-26 07:21:27 +02:00
|
|
|
self._updating = False
|
2016-02-26 01:01:46 +01:00
|
|
|
self._lastUpdate = time.time()
|
2012-12-21 08:51:59 +01:00
|
|
|
|
|
|
|
def finish(self):
|
|
|
|
self._levels -= 1
|
|
|
|
self._levels = max(0, self._levels)
|
2020-01-30 23:47:05 +01:00
|
|
|
if self._levels == 0:
|
|
|
|
if self._win:
|
|
|
|
self._closeWin()
|
|
|
|
self._unsetBusy()
|
2012-12-21 08:51:59 +01:00
|
|
|
|
|
|
|
def clear(self):
|
|
|
|
"Restore the interface after an error."
|
|
|
|
if self._levels:
|
|
|
|
self._levels = 1
|
|
|
|
self.finish()
|
|
|
|
|
|
|
|
def _maybeShow(self):
|
|
|
|
if not self._levels:
|
|
|
|
return
|
|
|
|
if self._shown:
|
|
|
|
self.update(maybeShow=False)
|
|
|
|
return
|
|
|
|
delta = time.time() - self._firstTime
|
|
|
|
if delta > 0.5:
|
2017-01-25 07:50:57 +01:00
|
|
|
self._showWin()
|
|
|
|
|
|
|
|
def _showWin(self):
|
|
|
|
self._shown = time.time()
|
|
|
|
self._win.show()
|
|
|
|
|
|
|
|
def _closeWin(self):
|
|
|
|
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.ExcludeUserInputEvents)
|
|
|
|
self._win.cancel()
|
2017-12-28 09:31:05 +01:00
|
|
|
self._win = None
|
2018-01-14 03:16:47 +01:00
|
|
|
self._shown = False
|
2012-12-21 08:51:59 +01:00
|
|
|
|
|
|
|
def _setBusy(self):
|
|
|
|
self.mw.app.setOverrideCursor(QCursor(Qt.WaitCursor))
|
|
|
|
|
|
|
|
def _unsetBusy(self):
|
|
|
|
self.app.restoreOverrideCursor()
|
|
|
|
|
|
|
|
def busy(self):
|
|
|
|
"True if processing."
|
|
|
|
return self._levels
|