Added a ProgressBar() class (which shows a progress bar). Needs new

resource.
This commit is contained in:
Jack Jansen 1995-11-14 10:13:49 +00:00
parent e48aa966c1
commit 3a87f5bb09

View file

@ -3,6 +3,8 @@
Message(msg) -- display a message and an OK button. Message(msg) -- display a message and an OK button.
AskString(prompt, default) -- ask for a string, display OK and Cancel buttons. AskString(prompt, default) -- ask for a string, display OK and Cancel buttons.
AskYesNoCancel(question, default) -- display a question and Yes, No and Cancel buttons. AskYesNoCancel(question, default) -- display a question and Yes, No and Cancel buttons.
bar = Progress(label, maxvalue) -- Display a progress bar
bar.set(value) -- Set value
More documentation in each function. More documentation in each function.
This module uses DLOG resources 256, 257 and 258. This module uses DLOG resources 256, 257 and 258.
@ -10,6 +12,12 @@ Based upon STDWIN dialogs with the same names and functions.
""" """
from Dlg import GetNewDialog, SetDialogItemText, GetDialogItemText, ModalDialog from Dlg import GetNewDialog, SetDialogItemText, GetDialogItemText, ModalDialog
import Qd
import addpack
addpack.addpack('Tools')
addpack.addpack('bgen')
addpack.addpack('qd')
import QuickDraw
def Message(msg): def Message(msg):
@ -27,6 +35,7 @@ def Message(msg):
return return
tp, h, rect = d.GetDialogItem(2) tp, h, rect = d.GetDialogItem(2)
SetDialogItemText(h, msg) SetDialogItemText(h, msg)
d.SetDialogDefaultItem(1)
while 1: while 1:
n = ModalDialog(None) n = ModalDialog(None)
if n == 1: if n == 1:
@ -56,6 +65,8 @@ def AskString(prompt, default = ""):
tp, h, rect = d.GetDialogItem(4) tp, h, rect = d.GetDialogItem(4)
SetDialogItemText(h, default) SetDialogItemText(h, default)
# d.SetDialogItem(4, 0, 255) # d.SetDialogItem(4, 0, 255)
d.SetDialogDefaultItem(1)
d.SetDialogCancelItem(2)
while 1: while 1:
n = ModalDialog(None) n = ModalDialog(None)
if n == 1: if n == 1:
@ -90,12 +101,60 @@ def AskYesNoCancel(question, default = 0):
# The question string is item 5 # The question string is item 5
tp, h, rect = d.GetDialogItem(5) tp, h, rect = d.GetDialogItem(5)
SetDialogItemText(h, question) SetDialogItemText(h, question)
d.SetDialogCancelItem(4)
if default in (2, 3, 4):
d.SetDialogDefaultItem(default)
while 1: while 1:
n = ModalDialog(None) n = ModalDialog(None)
if n == 1: return default if n == 1: return default
if n == 2: return 1 if n == 2: return 1
if n == 3: return 0 if n == 3: return 0
if n == 4: return -1 if n == 4: return -1
class ProgressBar:
def __init__(self, label="Working...", maxval=100):
self.label = label
self.maxval = maxval
self.curval = -1
self.d = GetNewDialog(259, -1)
tp, text_h, rect = self.d.GetDialogItem(2)
SetDialogItemText(text_h, "Progress...")
self._update(0)
def _update(self, value):
tp, h, bar_rect = self.d.GetDialogItem(3)
Qd.SetPort(self.d)
Qd.FrameRect(bar_rect) # Draw outline
inner_rect = Qd.InsetRect(bar_rect, 1, 1)
Qd.ForeColor(QuickDraw.whiteColor)
Qd.BackColor(QuickDraw.whiteColor)
Qd.PaintRect(inner_rect) # Clear internal
l, t, r, b = inner_rect
r = int(l + (r-l)*value/self.maxval)
inner_rect = l, t, r, b
Qd.ForeColor(QuickDraw.blackColor)
Qd.BackColor(QuickDraw.blackColor)
Qd.PaintRect(inner_rect) # Draw bar
# Restore settings
Qd.ForeColor(QuickDraw.blackColor)
Qd.BackColor(QuickDraw.whiteColor)
# Test for cancel button
if ModalDialog(self._filterfunc) == 1:
raise KeyboardInterrupt
def _filterfunc(self, d, e, *more):
return 2 # XXXX For now, this disables the cancel button
def set(self, value):
if value < 0: value = 0
if value > self.maxval: value = self.maxval
self._update(value)
def test(): def test():
@ -104,6 +163,10 @@ def test():
if ok > 0: if ok > 0:
s = AskString("Enter your first name") s = AskString("Enter your first name")
Message("Thank you,\015%s" % `s`) Message("Thank you,\015%s" % `s`)
bar = ProgressBar("Counting...", 100)
for i in range(100):
bar.set(i)
del bar
if __name__ == '__main__': if __name__ == '__main__':