pyqtgraph/widgets/ProgressDialog.py
2012-05-08 22:56:44 -04:00

112 lines
3.4 KiB
Python

# -*- coding: utf-8 -*-
from pyqtgraph.Qt import QtGui, QtCore
__all__ = ['ProgressDialog']
class ProgressDialog(QtGui.QProgressDialog):
"""
Extends QProgressDialog for use in 'with' statements.
Example::
with ProgressDialog("Processing..", minVal, maxVal) as dlg:
# do stuff
dlg.setValue(i) ## could also use dlg += 1
if dlg.wasCanceled():
raise Exception("Processing canceled by user")
"""
def __init__(self, labelText, minimum=0, maximum=100, cancelText='Cancel', parent=None, wait=250, busyCursor=False):
"""
============== ================================================================
**Arguments:**
labelText (required)
cancelText Text to display on cancel button, or None to disable it.
minimum
maximum
parent
wait Length of time (im ms) to wait before displaying dialog
busyCursor If True, show busy cursor until dialog finishes
============== ================================================================
"""
isGuiThread = QtCore.QThread.currentThread() == QtCore.QCoreApplication.instance().thread()
if not isGuiThread:
self.disabled = True
return
self.disabled = False
noCancel = False
if cancelText is None:
cancelText = ''
noCancel = True
self.busyCursor = busyCursor
QtGui.QProgressDialog.__init__(self, labelText, cancelText, minimum, maximum, parent)
self.setMinimumDuration(wait)
self.setWindowModality(QtCore.Qt.WindowModal)
self.setValue(self.minimum())
if noCancel:
self.setCancelButton(None)
def __enter__(self):
if self.disabled:
return self
if self.busyCursor:
QtGui.QApplication.setOverrideCursor(QtGui.QCursor(QtCore.Qt.WaitCursor))
return self
def __exit__(self, exType, exValue, exTrace):
if self.disabled:
return
if self.busyCursor:
QtGui.QApplication.restoreOverrideCursor()
self.setValue(self.maximum())
def __iadd__(self, val):
"""Use inplace-addition operator for easy incrementing."""
if self.disabled:
return self
self.setValue(self.value()+val)
return self
## wrap all other functions to make sure they aren't being called from non-gui threads
def setValue(self, val):
if self.disabled:
return
QtGui.QProgressDialog.setValue(self, val)
def setLabelText(self, val):
if self.disabled:
return
QtGui.QProgressDialog.setLabelText(self, val)
def setMaximum(self, val):
if self.disabled:
return
QtGui.QProgressDialog.setMaximum(self, val)
def setMinimum(self, val):
if self.disabled:
return
QtGui.QProgressDialog.setMinimum(self, val)
def wasCanceled(self):
if self.disabled:
return False
return QtGui.QProgressDialog.wasCanceled(self)
def maximum(self):
if self.disabled:
return 0
return QtGui.QProgressDialog.maximum(self)
def minimum(self):
if self.disabled:
return 0
return QtGui.QProgressDialog.minimum(self)