2012-12-21 08:51:59 +01:00
|
|
|
# Copyright: Damien Elmes <anki@ichi2.net>
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
|
|
|
|
|
|
|
import time
|
|
|
|
from aqt.qt import *
|
|
|
|
|
|
|
|
# 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
|
|
|
|
##########################################################################
|
|
|
|
|
|
|
|
class ProgressManager(object):
|
|
|
|
|
|
|
|
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
|
|
|
|
try:
|
|
|
|
db.set_progress_handler(self._dbProgress, 10000)
|
|
|
|
except:
|
2016-05-12 06:45:35 +02:00
|
|
|
print("""\
|
|
|
|
Your pysqlite2 is too old. Anki will appear frozen during long operations.""")
|
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:
|
|
|
|
self._maybeShow()
|
|
|
|
self.app.processEvents(QEventLoop.ExcludeUserInputEvents)
|
2012-12-21 08:51:59 +01:00
|
|
|
self.inDB = False
|
|
|
|
|
|
|
|
# DB-safe timers
|
|
|
|
##########################################################################
|
|
|
|
# QTimer may fire in processEvents(). We provide a custom timer which
|
|
|
|
# automatically defers until the DB is not busy.
|
|
|
|
|
|
|
|
def timer(self, ms, func, repeat):
|
|
|
|
def handler():
|
|
|
|
if self.inDB:
|
|
|
|
# retry in 100ms
|
2013-05-24 02:08:36 +02:00
|
|
|
self.timer(100, func, False)
|
2012-12-21 08:51:59 +01:00
|
|
|
else:
|
|
|
|
func()
|
|
|
|
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
|
|
|
|
##########################################################################
|
|
|
|
|
|
|
|
class ProgressNoCancel(QProgressDialog):
|
|
|
|
def closeEvent(self, evt):
|
|
|
|
evt.ignore()
|
|
|
|
def keyPressEvent(self, evt):
|
|
|
|
if evt.key() == Qt.Key_Escape:
|
|
|
|
evt.ignore()
|
|
|
|
|
2017-01-17 08:15:50 +01:00
|
|
|
class ProgressCancellable(QProgressDialog):
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
QProgressDialog.__init__(self, *args, **kwargs)
|
|
|
|
self.ankiCancel = False
|
|
|
|
def closeEvent(self, evt):
|
|
|
|
# avoid standard Qt flag as we don't want to close until we're ready
|
|
|
|
self.ankiCancel = True
|
|
|
|
evt.ignore()
|
|
|
|
def keyPressEvent(self, evt):
|
|
|
|
if evt.key() == Qt.Key_Escape:
|
|
|
|
evt.ignore()
|
|
|
|
self.ankiCancel = True
|
|
|
|
|
|
|
|
def start(self, max=0, min=0, label=None, parent=None, immediate=False, cancellable=False):
|
2012-12-21 08:51:59 +01:00
|
|
|
self._levels += 1
|
|
|
|
if self._levels > 1:
|
|
|
|
return
|
|
|
|
# setup window
|
|
|
|
parent = parent or self.app.activeWindow() or self.mw
|
|
|
|
label = label or _("Processing...")
|
2017-01-17 08:15:50 +01:00
|
|
|
if cancellable:
|
|
|
|
klass = self.ProgressCancellable
|
|
|
|
else:
|
|
|
|
klass = self.ProgressNoCancel
|
|
|
|
self._win = klass(label, "", min, max, parent)
|
2012-12-21 08:51:59 +01:00
|
|
|
self._win.setWindowTitle("Anki")
|
|
|
|
self._win.setCancelButton(None)
|
|
|
|
self._win.setAutoClose(False)
|
|
|
|
self._win.setAutoReset(False)
|
|
|
|
self._win.setWindowModality(Qt.ApplicationModal)
|
|
|
|
# we need to manually manage minimum time to show, as qt gets confused
|
|
|
|
# by the db handler
|
|
|
|
self._win.setMinimumDuration(100000)
|
|
|
|
if immediate:
|
2017-01-25 07:50:57 +01:00
|
|
|
self._showWin()
|
2012-12-21 08:51:59 +01:00
|
|
|
else:
|
|
|
|
self._shown = False
|
|
|
|
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-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):
|
|
|
|
#print self._min, self._counter, self._max, label, time.time() - self._lastTime
|
|
|
|
if maybeShow:
|
|
|
|
self._maybeShow()
|
2016-02-26 01:01:46 +01:00
|
|
|
elapsed = time.time() - self._lastUpdate
|
2012-12-21 08:51:59 +01:00
|
|
|
if label:
|
|
|
|
self._win.setLabelText(label)
|
|
|
|
if self._max and self._shown:
|
|
|
|
self._counter = value or (self._counter+1)
|
|
|
|
self._win.setValue(self._counter)
|
2016-02-26 01:01:46 +01:00
|
|
|
if process and elapsed >= 0.2:
|
2012-12-21 08:51:59 +01:00
|
|
|
self.app.processEvents(QEventLoop.ExcludeUserInputEvents)
|
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)
|
|
|
|
if self._levels == 0 and self._win:
|
2017-01-25 07:50:57 +01:00
|
|
|
self._closeWin()
|
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()
|
|
|
|
self._setBusy()
|
|
|
|
|
|
|
|
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()
|
|
|
|
self._unsetBusy()
|
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
|