showProgress handles reentrant calls by completing these calls non-interactively.
This commit is contained in:
parent
457c6add01
commit
156dfa2016
@ -8,6 +8,7 @@ from PySide.QtCore import Qt
|
||||
import time
|
||||
import itertools
|
||||
from mcedit2.util.worldloader import LoaderTimer
|
||||
from mceditlib.util import exhaust
|
||||
from mceditlib.util.progress import rescaleProgress
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
@ -32,6 +33,8 @@ class MCEProgressDialog(QtGui.QProgressDialog):
|
||||
event.ignore()
|
||||
|
||||
|
||||
_progressBarActive = False
|
||||
|
||||
def showProgress(text, *tasks, **kwargs):
|
||||
"""
|
||||
Show a progress dialog for the given task(s). Each task should be an iterable,
|
||||
@ -47,54 +50,64 @@ def showProgress(text, *tasks, **kwargs):
|
||||
:return:
|
||||
:rtype:
|
||||
"""
|
||||
global _progressBarActive
|
||||
|
||||
if _progressBarActive:
|
||||
for task in tasks:
|
||||
exhaust(task)
|
||||
return
|
||||
|
||||
progress = None
|
||||
cancel = kwargs.pop('cancel', None)
|
||||
start = time.time()
|
||||
shown = False
|
||||
with LoaderTimer.stopCtx():
|
||||
try:
|
||||
with LoaderTimer.stopCtx():
|
||||
|
||||
dialog = MCEProgressDialog(QtGui.qApp.mainWindow)
|
||||
if not cancel:
|
||||
dialog.setCancelButtonText(None)
|
||||
dialog.setWindowTitle(text)
|
||||
dialog.setWindowModality(Qt.WindowModal)
|
||||
log.info("Starting progress: %d tasks." % len(tasks))
|
||||
totalMaximum = 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
|
||||
maximum = 0
|
||||
status = progress
|
||||
elif isinstance(progress, tuple):
|
||||
if len(progress) > 2:
|
||||
current, maximum, status = progress[:3]
|
||||
dialog = MCEProgressDialog(QtGui.qApp.mainWindow)
|
||||
if not cancel:
|
||||
dialog.setCancelButtonText(None)
|
||||
dialog.setWindowTitle(text)
|
||||
dialog.setWindowModality(Qt.WindowModal)
|
||||
log.info("Starting progress: %d tasks." % len(tasks))
|
||||
totalMaximum = 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
|
||||
maximum = 0
|
||||
status = progress
|
||||
elif isinstance(progress, tuple):
|
||||
if len(progress) > 2:
|
||||
current, maximum, status = progress[:3]
|
||||
else:
|
||||
current, maximum = progress
|
||||
status = ""
|
||||
else:
|
||||
current, maximum = progress
|
||||
current = 0
|
||||
maximum = 0
|
||||
status = ""
|
||||
else:
|
||||
current = 0
|
||||
maximum = 0
|
||||
status = ""
|
||||
|
||||
dialog.setValue(current)
|
||||
if maximum == 0:
|
||||
# Task progress is indeterminate
|
||||
dialog.setMaximum(0)
|
||||
else:
|
||||
dialog.setMaximum(totalMaximum)
|
||||
dialog.setLabelText(status)
|
||||
if time.time() > start + timeBeforeDialog:
|
||||
if not shown:
|
||||
dialog.show()
|
||||
shown = True
|
||||
QtGui.QApplication.processEvents()
|
||||
dialog.setValue(current)
|
||||
if maximum == 0:
|
||||
# Task progress is indeterminate
|
||||
dialog.setMaximum(0)
|
||||
else:
|
||||
dialog.setMaximum(totalMaximum)
|
||||
dialog.setLabelText(status)
|
||||
if time.time() > start + timeBeforeDialog:
|
||||
if not shown:
|
||||
dialog.show()
|
||||
shown = True
|
||||
QtGui.QApplication.processEvents()
|
||||
|
||||
if dialog.wasCanceled():
|
||||
return False
|
||||
if dialog.wasCanceled():
|
||||
return False
|
||||
|
||||
dialog.reset()
|
||||
return progress
|
||||
dialog.reset()
|
||||
return progress
|
||||
finally:
|
||||
_progressBarActive = False
|
||||
|
||||
|
Reference in New Issue
Block a user