]> granicus.if.org Git - python/commitdiff
Patch by Dean Draayer: support for indeterminate progress bars. You
authorJack Jansen <jack.jansen@cwi.nl>
Mon, 27 Aug 2001 15:24:07 +0000 (15:24 +0000)
committerJack Jansen <jack.jansen@cwi.nl>
Mon, 27 Aug 2001 15:24:07 +0000 (15:24 +0000)
get these by specifying maxval=0, which is now also the default.

Untested.

Mac/Lib/EasyDialogs.py

index 68ad58eb350d38f9fce7a1512ad230d0bc698e4b..277d6d0201925da45c40b08c1a3c4b8684107cac 100644 (file)
@@ -214,19 +214,19 @@ screenbounds = Qd.qd.screenBits.bounds
 screenbounds = screenbounds[0]+4, screenbounds[1]+4, \
        screenbounds[2]-4, screenbounds[3]-4
 
-                               
+kControlProgressBarIndeterminateTag = 'inde'   # from Controls.py
+
+
 class ProgressBar:
-       def __init__(self, title="Working...", maxval=100, label="", id=263):
+       def __init__(self, title="Working...", maxval=0, label="", id=263):
                self.w = None
                self.d = None
-               self.maxval = maxval
-               self.curval = -1
                self.d = GetNewDialog(id, -1)
                self.w = self.d.GetDialogWindow()
                self.label(label)
-               self._update(0)
-               self.d.AutoSizeDialog()
                self.title(title)
+               self.set(0, maxval)
+               self.d.AutoSizeDialog()
                self.w.ShowWindow()
                self.d.DrawDialog()
 
@@ -248,24 +248,23 @@ class ProgressBar:
                if newstr:
                        self._label = lf2cr(newstr[0])
                text_h = self.d.GetDialogItemAsControl(2)
-               SetDialogItemText(text_h, self._label)          
-                               
+               SetDialogItemText(text_h, self._label)
+
        def _update(self, value):
                maxval = self.maxval
-               if maxval == 0:
-                       # XXXX Quick fix. Should probably display an unknown duration
-                       value = 0
-                       maxval = 1
-               if maxval > 32767:
-                       value = int(value/(maxval/32767.0))
-                       maxval = 32767
-               progbar = self.d.GetDialogItemAsControl(3)
-               progbar.SetControlMaximum(maxval)
-               progbar.SetControlValue(value)  
+               if maxval == 0:         # an indeterminate bar
+                       Ctl.IdleControls(self.w)        # spin the barber pole
+               else:                           # a determinate bar
+                       if maxval > 32767:
+                               value = int(value/(maxval/32767.0))
+                               maxval = 32767
+                       progbar = self.d.GetDialogItemAsControl(3)
+                       progbar.SetControlMaximum(maxval)
+                       progbar.SetControlValue(value)  # set the bar length
+
                # Test for cancel button
-               
                ready, ev = Evt.WaitNextEvent( Events.mDownMask, 1  )
-               if ready : 
+               if ready :
                        what,msg,when,where,mod = ev
                        part = Win.FindWindow(where)[0]
                        if Dlg.IsDialogEvent(ev):
@@ -286,8 +285,15 @@ class ProgressBar:
                """set(value) - Set progress bar position"""
                if max != None:
                        self.maxval = max
-               if value < 0: value = 0
-               if value > self.maxval: value = self.maxval
+                       bar = self.d.GetDialogItemAsControl(3)
+                       if max <= 0:    # indeterminate bar
+                               bar.SetControlData(0,kControlProgressBarIndeterminateTag,'\x01')
+                       else:                   # determinate bar
+                               bar.SetControlData(0,kControlProgressBarIndeterminateTag,'\x00')
+               if value < 0:
+                       value = 0
+               elif value > self.maxval:
+                       value = self.maxval
                self.curval = value
                self._update(value)
 
@@ -540,18 +546,22 @@ def test():
                        Message("%s has no secret nickname"%s)
                else:
                        Message("Hello everybody!!\nThe secret nickname of %s is %s!!!"%(s, s2))
-       text = ( "Working Hard...", "Hardly Working..." , 
+       text = ( "Working Hard...", "Hardly Working..." ,
                        "So far, so good!", "Keep on truckin'" )
-       bar = ProgressBar("Progress, progress...", 100)
+       bar = ProgressBar("Progress, progress...", 0, label="Ramping up...")
        try:
                appsw = MacOS.SchedParams(1, 0)
-               for i in range(100):
+               for i in xrange(20):
+                       bar.inc()
+                       time.sleep(0.05)
+               bar.set(0,100)
+               for i in xrange(100):
                        bar.set(i)
-                       time.sleep(0.1)
+                       time.sleep(0.05)
                        if i % 10 == 0:
                                bar.label(text[(i/10) % 4])
                bar.label("Done.")
-               time.sleep(0.3)         # give'em a chance to see the done.
+               time.sleep(1.0)         # give'em a chance to see "Done."
        finally:
                del bar
                apply(MacOS.SchedParams, appsw)