# emacs-mode: -*- python-*-
"""Class ProgressBarTW for displaying progress of some operation. TW means using TopWindow class. P.S. Sorry for my bad english :)"""
__name__ = 'ProgressBarTW class'
class ProgressBarTW:
    __module__ = __name__

    def __init__(self):
        import e32
        import topwindow
        import graphics
        from sysinfo import display_pixels
        from appuifw import app
        self.app = app
        self.sleep = e32.ao_sleep
        (screen_width, screen_height,) = display_pixels()
        self.pb_window = topwindow.TopWindow()
        self.pb_window.size = ((screen_width / 2),
         (screen_height * 0.10000000000000001))
        self.pb_window.background_color = 16777215
        self.pb_window.position = (((screen_width / 2) - (self.pb_window.size[0] / 2)),
         ((screen_height / 2) - (self.pb_window.size[1] / 2)))
        self.pb_window.shadow = 1
        self.pb_window.corner_type = 'square'
        self.pb_image = graphics.Image.new(self.pb_window.size)
        self.pb_window.add_image(self.pb_image, (0,
         0))



    def check_focus(self, fg):
        if fg:
            self.pb_window.show()
            self.foreground = True
        else:
            self.pb_window.hide()
            self.foreground = False



    def begin_progress(self, total = 100):
        self.total = total
        self.foreground = True
        self.app.focus = self.check_focus



    def end_progress(self):
        self.do_progress(self.total)
        self.app.focus = None
        self.sleep(0.5)
        self.pb_window.hide()



    def do_progress(self, current_value):
        if ((self.pb_window.visible == 0) and self.foreground):
            self.pb_window.show()
        self.sleep(0.001)
        self.pb_window.remove_image(self.pb_image)
        self.current_value = current_value
        cur_percent = ((100 * current_value) / self.total)
        if (cur_percent < 0):
            cur_percent = 0
        elif (cur_percent > 100):
            cur_percent = 100
        cur_x = (((self.pb_window.size[0] - 3) * cur_percent) / 100)
        self.pb_image.rectangle((0,
         0,
         (self.pb_window.size[0] - 1),
         (self.pb_window.size[1] - 1)), fill=16777215, outline=0)
        self.pb_image.rectangle((1,
         1,
         (self.pb_window.size[0] - 2),
         (self.pb_window.size[1] - 2)), fill=16777215, outline=12632256)
        self.pb_image.rectangle((2,
         2,
         cur_x,
         (self.pb_window.size[1] - 2)), fill=0, outline=12632256)
        percent_text = (u'%d %%' % cur_percent)
        self.pb_image.text((((self.pb_window.size[0] / 2) - (len(percent_text) * 3)),
         (self.pb_window.size[1] - 6)), percent_text, fill=15663104, font='legend')
        self.pb_window.add_image(self.pb_image, (1,
         1))




# local variables:
# tab-width: 4
