showProgress is now able to combine multiple iterable tasks into one progress bar

This commit is contained in:
David Vierra 2015-09-10 16:27:05 -10:00
parent c7b009b59c
commit 10ce7746a9

View File

@ -6,16 +6,20 @@ import logging
from PySide import QtGui
from PySide.QtCore import Qt
import time
import itertools
from mcedit2.util.worldloader import LoaderTimer
from mceditlib.util.progress import rescaleProgress
log = logging.getLogger(__name__)
timeBeforeDialog = 0.5
timeBeforeDialog = 0.2
def showProgress(text, iter, cancel=False):
def showProgress(text, *tasks, **kwargs):
"""
Show a progress dialog for the given task. The task should be an iterable, yielding progress info as
(current, max) or (current, max, statusString) tuples. Return the last value yielded by the task.
Show a progress dialog for the given task(s). Each task should be an iterable,
yielding progress info as (current, max) or (current, max, statusString) tuples.
Return the last value yielded by the task.
:param text:
:type text:
:param iter:
@ -26,41 +30,38 @@ def showProgress(text, iter, cancel=False):
:rtype:
"""
progress = None
i = 0
start = time.time()
cancel = kwargs.pop('cancel', None)
with LoaderTimer.stopCtx():
for progress in iter:
if time.time() - start > timeBeforeDialog:
break
else:
return progress
dialog = QtGui.QProgressDialog(QtGui.qApp.mainWindow)
dialog.setWindowTitle(text)
dialog.setWindowModality(Qt.WindowModal)
dialog.show()
for progress in iter:
if isinstance(progress, basestring):
max = current = 0
status = progress
elif isinstance(progress, tuple):
if len(progress) > 2:
current, max, status = progress[:3]
log.info("Starting progress: %d tasks." % len(tasks))
maximum = len(tasks) * 100
for i, task in enumerate(tasks):
log.info("Task #%d", i)
task = rescaleProgress(task, i*100, i*100+100)
for progress in task:
if isinstance(progress, basestring):
current = 0
status = progress
elif isinstance(progress, tuple):
if len(progress) > 2:
current, _, status = progress[:3]
else:
current, _ = progress
status = ""
else:
current, max = progress
current = 1
status = ""
else:
current = max = 1
status = ""
dialog.setValue(current)
dialog.setMaximum(max)
dialog.setLabelText(status)
QtGui.QApplication.processEvents()
if dialog.wasCanceled():
return False
dialog.setValue(current)
dialog.setMaximum(maximum)
dialog.setLabelText(status)
QtGui.QApplication.processEvents()
if dialog.wasCanceled():
return False
dialog.close()
return progress