2007-02-16 18:36:49 +05:30
|
|
|
"""
|
|
|
|
This module provides a progess dialog for displaying the status of
|
|
|
|
long running operations.
|
|
|
|
"""
|
|
|
|
|
|
|
|
import gtk
|
|
|
|
|
2007-02-17 01:36:11 +05:30
|
|
|
class _GtkProgressBar(gtk.VBox):
|
2007-02-17 03:32:10 +05:30
|
|
|
"""This widget displays the progress bar and labels for a progress
|
|
|
|
indicator. It provides an interface to updating the progress bar.
|
|
|
|
"""
|
2007-02-16 18:36:49 +05:30
|
|
|
|
2007-02-17 01:36:11 +05:30
|
|
|
def __init__(self, long_op_status):
|
2007-02-17 03:32:10 +05:30
|
|
|
"""@param long_op_status: the status of the operation.
|
|
|
|
@type long_op_status: L{GrampsDb.LongOpStatus}
|
|
|
|
"""
|
2007-02-17 01:36:11 +05:30
|
|
|
gtk.VBox.__init__(self)
|
|
|
|
|
|
|
|
msg = long_op_status.get_msg()
|
|
|
|
self._old_val = -1
|
|
|
|
self._lbl = gtk.Label(msg)
|
|
|
|
self._lbl.set_use_markup(True)
|
2007-02-17 03:32:10 +05:30
|
|
|
#self.set_border_width(24)
|
2007-02-17 01:36:11 +05:30
|
|
|
|
|
|
|
self._pbar = gtk.ProgressBar()
|
|
|
|
self._hbox = gtk.HBox()
|
|
|
|
|
2007-02-17 03:32:10 +05:30
|
|
|
# Only display the cancel button is the operation
|
|
|
|
# can be canceled.
|
2007-02-17 01:36:11 +05:30
|
|
|
if long_op_status.can_cancel():
|
|
|
|
self._cancel = gtk.Button(stock=gtk.STOCK_CANCEL)
|
|
|
|
self._cancel.connect("clicked",
|
|
|
|
lambda x: long_op_status.cancel())
|
2007-02-17 03:32:10 +05:30
|
|
|
self._cancel.show()
|
2007-02-17 01:36:11 +05:30
|
|
|
self._hbox.pack_end(self._cancel)
|
|
|
|
|
|
|
|
self._hbox.pack_start(self._pbar)
|
|
|
|
|
|
|
|
self.pack_start(self._lbl, expand=False, fill=False)
|
|
|
|
self.pack_start(self._hbox, expand=False, fill=False)
|
2007-02-17 03:32:10 +05:30
|
|
|
|
2007-02-17 01:36:11 +05:30
|
|
|
|
|
|
|
self._pbar_max = (long_op_status.get_total_steps()/
|
|
|
|
long_op_status.get_interval())
|
|
|
|
self._pbar_index = 0.0
|
2007-02-18 01:29:21 +05:30
|
|
|
self._pbar.set_fraction(((100/float(long_op_status.get_total_steps())*
|
|
|
|
float(long_op_status.get_interval())))/
|
2007-02-17 01:36:11 +05:30
|
|
|
100.0)
|
2007-02-17 03:32:10 +05:30
|
|
|
|
|
|
|
if msg != '':
|
|
|
|
self._lbl.show()
|
|
|
|
self._pbar.show()
|
|
|
|
self._hbox.show()
|
2007-02-17 01:36:11 +05:30
|
|
|
|
|
|
|
def step(self):
|
2007-02-17 03:32:10 +05:30
|
|
|
"""Move the progress bar on a step.
|
|
|
|
"""
|
2007-02-17 01:36:11 +05:30
|
|
|
self._pbar_index = self._pbar_index + 1.0
|
|
|
|
|
|
|
|
if self._pbar_index > self._pbar_max:
|
|
|
|
self._pbar_index = self._pbar_max
|
|
|
|
|
|
|
|
try:
|
|
|
|
val = int(100*self._pbar_index/self._pbar_max)
|
|
|
|
except ZeroDivisionError:
|
|
|
|
val = 0
|
|
|
|
|
|
|
|
if val != self._old_val:
|
|
|
|
self._pbar.set_text("%d%%" % val)
|
|
|
|
self._pbar.set_fraction(val/100.0)
|
|
|
|
self._pbar.old_val = val
|
2007-02-16 18:36:49 +05:30
|
|
|
|
2007-02-18 01:29:21 +05:30
|
|
|
class GtkProgressDialog(gtk.Dialog):
|
2007-02-16 18:36:49 +05:30
|
|
|
"""A gtk window to display the status of a long running
|
|
|
|
process."""
|
|
|
|
|
2007-02-18 01:29:21 +05:30
|
|
|
def __init__(self, window_params, title):
|
2007-02-17 03:32:10 +05:30
|
|
|
"""@param title: The title to display on the top of the window.
|
|
|
|
@type title: string
|
|
|
|
"""
|
2007-02-18 01:29:21 +05:30
|
|
|
gtk.Dialog.__init__(self, *window_params)
|
2007-02-17 03:32:10 +05:30
|
|
|
self.connect('delete_event', self._warn)
|
2007-02-16 18:36:49 +05:30
|
|
|
self.set_has_separator(False)
|
|
|
|
self.set_title(title)
|
2007-02-18 03:42:56 +05:30
|
|
|
#self.set_resize_mode(gtk.RESIZE_IMMEDIATE)
|
|
|
|
#self.show()
|
2007-02-16 18:36:49 +05:30
|
|
|
|
|
|
|
self._progress_bars = []
|
|
|
|
|
|
|
|
def add(self,long_op_status):
|
2007-02-17 03:32:10 +05:30
|
|
|
"""Add a new status object to the progress dialog.
|
|
|
|
|
|
|
|
@param long_op_status: the status object.
|
|
|
|
@type long_op_status: L{GrampsDb.LongOpStatus}
|
|
|
|
@return: a key that can be used as the L{pbar_idx}
|
|
|
|
to the other methods.
|
|
|
|
@rtype: int
|
|
|
|
"""
|
2007-02-17 01:36:11 +05:30
|
|
|
pbar = _GtkProgressBar(long_op_status)
|
|
|
|
|
|
|
|
self.vbox.pack_start(pbar, expand=False, fill=False)
|
2007-02-16 18:36:49 +05:30
|
|
|
|
2007-02-17 03:32:10 +05:30
|
|
|
pbar.show()
|
|
|
|
|
2007-02-16 18:36:49 +05:30
|
|
|
self.resize_children()
|
2007-02-17 03:32:10 +05:30
|
|
|
self._process_events()
|
2007-02-16 18:36:49 +05:30
|
|
|
|
|
|
|
self._progress_bars.append(pbar)
|
|
|
|
return len(self._progress_bars)-1
|
|
|
|
|
|
|
|
def remove(self, pbar_idx):
|
2007-02-17 03:32:10 +05:30
|
|
|
"""Remove the specified status object from the progress dialog.
|
|
|
|
|
|
|
|
@param pbar_idx: the index as returned from L{add}
|
|
|
|
@type pbar_idx: int
|
|
|
|
"""
|
2007-02-16 18:36:49 +05:30
|
|
|
pbar = self._progress_bars[pbar_idx]
|
2007-02-17 01:36:11 +05:30
|
|
|
self.vbox.remove(pbar)
|
2007-02-16 18:36:49 +05:30
|
|
|
del self._progress_bars[pbar_idx]
|
|
|
|
|
|
|
|
def step(self, pbar_idx):
|
|
|
|
"""Click the progress bar over to the next value. Be paranoid
|
2007-02-17 03:32:10 +05:30
|
|
|
and insure that it doesn't go over 100%.
|
|
|
|
|
|
|
|
@param pbar_idx: the index as returned from L{add}
|
|
|
|
@type pbar_idx: int
|
|
|
|
"""
|
2007-02-16 18:36:49 +05:30
|
|
|
|
2007-02-17 01:36:11 +05:30
|
|
|
self._progress_bars[pbar_idx].step()
|
2007-02-17 03:32:10 +05:30
|
|
|
self._process_events()
|
2007-02-16 18:36:49 +05:30
|
|
|
|
2007-02-17 03:32:10 +05:30
|
|
|
def _process_events(self):
|
2007-02-16 18:36:49 +05:30
|
|
|
while gtk.events_pending():
|
|
|
|
gtk.main_iteration()
|
|
|
|
|
|
|
|
def show(self):
|
2007-02-17 03:32:10 +05:30
|
|
|
"""Show the dialog and process any events.
|
|
|
|
"""
|
2007-02-16 18:36:49 +05:30
|
|
|
gtk.Dialog.show(self)
|
2007-02-17 03:32:10 +05:30
|
|
|
self._process_events()
|
2007-02-16 18:36:49 +05:30
|
|
|
|
|
|
|
def hide(self):
|
2007-02-17 03:32:10 +05:30
|
|
|
"""Hide the dialog and process any events.
|
|
|
|
"""
|
2007-02-16 18:36:49 +05:30
|
|
|
gtk.Dialog.hide(self)
|
2007-02-17 03:32:10 +05:30
|
|
|
self._process_events()
|
2007-02-16 18:36:49 +05:30
|
|
|
|
2007-02-17 03:32:10 +05:30
|
|
|
def _warn(self,x,y):
|
2007-02-16 18:36:49 +05:30
|
|
|
return True
|
|
|
|
|
|
|
|
def close(self):
|
|
|
|
self.destroy()
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
import time
|
2007-02-17 03:32:10 +05:30
|
|
|
from GrampsDb import LongOpStatus, ProgressMonitor
|
2007-02-16 18:36:49 +05:30
|
|
|
|
|
|
|
def test(a,b):
|
2007-02-18 01:29:21 +05:30
|
|
|
d = ProgressMonitor(GtkProgressDialog)
|
2007-02-16 18:36:49 +05:30
|
|
|
|
|
|
|
s = LongOpStatus("Doing very long operation", 100, 10)
|
|
|
|
|
|
|
|
d.add_op(s)
|
|
|
|
|
|
|
|
for i in xrange(0, 99):
|
|
|
|
time.sleep(0.1)
|
|
|
|
if i == 30:
|
2007-02-17 01:36:11 +05:30
|
|
|
t = LongOpStatus("doing a shorter one", 100, 10,
|
|
|
|
can_cancel=True)
|
2007-02-16 18:36:49 +05:30
|
|
|
d.add_op(t)
|
|
|
|
for j in xrange(0, 99):
|
2007-02-17 01:36:11 +05:30
|
|
|
if t.should_cancel():
|
|
|
|
break
|
2007-02-16 18:36:49 +05:30
|
|
|
time.sleep(0.1)
|
|
|
|
t.heartbeat()
|
|
|
|
t.end()
|
|
|
|
if i == 60:
|
|
|
|
t = LongOpStatus("doing another shorter one", 100, 10)
|
|
|
|
d.add_op(t)
|
|
|
|
for j in xrange(0, 99):
|
|
|
|
time.sleep(0.1)
|
|
|
|
t.heartbeat()
|
|
|
|
t.end()
|
|
|
|
s.heartbeat()
|
|
|
|
s.end()
|
|
|
|
|
|
|
|
w = gtk.Window(gtk.WINDOW_TOPLEVEL)
|
|
|
|
w.connect('destroy', gtk.main_quit)
|
|
|
|
button = gtk.Button("Test")
|
|
|
|
button.connect("clicked", test, None)
|
|
|
|
w.add(button)
|
|
|
|
button.show()
|
|
|
|
w.show()
|
|
|
|
gtk.main()
|
|
|
|
print 'done'
|
|
|
|
|