Added generic progress bar handling for active panes.

This commit is contained in:
David Soulayrol 2010-11-21 11:20:49 +01:00
parent f19ddd3148
commit be86c9df08

View file

@ -212,8 +212,43 @@ class ActivePane(Pane):
"""Constructor."""
Pane.__init__(self, frame)
self._progress_box = gtk.VBox()
self.pack_end(self._progress_box, False)
self._thread = None
def update_progress_bar(self, fraction=None, text=None):
"""Update this pane's progress bar.
The progress bar, when used, shows up at the bottom of the
pane. When this method is called, it is first displayed is
absent, and the updated with the given fraction, between 0.0
and 1.0, and the given text if any. In the special case where
the fraction is not provided, the ProgressBar.pulse method is
invoked.
Returns True, so that it can be used in a GObject timer.
This method is non-GTK thread safe, which means that any
thread can call it.
"""
gobject.idle_add(self._do_update_progress_bar, fraction, text)
return True
def hide_progress_bar(self):
"""Hide this pane's progress bar.
The progress bar is in fact destroyed, so its state is lost.
This method is non-GTK thread safe, which means that any
thread can call it. If no progress bar is currently displayed,
it does nothing.
"""
if len(self._progress_box.get_children()):
print '#Hide'
gobject.idle_add(self._progress_box.remove,
self._progress_box.get_children()[0])
def enter(self):
"""Proceed to this pane initialization.
@ -246,6 +281,20 @@ class ActivePane(Pane):
self._runner()
gobject.idle_add(self._frame.unlock)
def _do_update_progress_bar(self, fraction, text):
"""An internal facility to update the progress bar.
This private method is intended to be called in the GTK main
thread.
"""
if not len(self._progress_box.get_children()):
self._progress_box.pack_start(gtk.ProgressBar())
self._progress_box.show_all()
bar = self._progress_box.get_children()[0]
if text:
bar.set_text(text)
bar.pulse() if fraction is None else bar.set_fraction(fraction)
class Frame(gtk.Dialog):
"""The Wizard window.
@ -435,29 +484,30 @@ class ActiveTestPane(ActivePane):
self.remove(self._inner_pane)
self._inner_pane = gtk.VBox(False, 5)
self._inner_pane.pack_start(gtk.ProgressBar(), True, False)
self.pack_start(self._inner_pane, True, True)
# Start thread
ActivePane.enter(self)
def _runner(self):
# The True value below is here so that the timer is maintained.
timer = gobject.timeout_add(
100, lambda: self._inner_pane.get_children()[0].pulse() or True)
gobject.idle_add(self._inner_pane.get_children()[0].set_text, 'Connecting...')
timer = gobject.timeout_add(100, lambda: self.update_progress_bar())
self.update_progress_bar(text='Connecting...')
c = httplib.HTTPSConnection('www.google.com')
gobject.idle_add(self._inner_pane.get_children()[0].set_text, 'Searching...')
self.update_progress_bar(text='Searching...')
c.request(
'GET', self.PATH + urllib.urlencode({'q': self.subject}),
None, {'GData-Version': '2'})
# Simulate a longer task.
gobject.idle_add(self._inner_pane.get_children()[0].set_text, 'Computing...')
time.sleep(3)
gobject.source_remove(timer)
self.update_progress_bar(0, 'Computing...')
for i in range(100):
self.update_progress_bar(i / 100.)
time.sleep(.02)
self.hide_progress_bar()
gobject.idle_add(self._show_response, c.getresponse())
def _show_response(self, r):