Package dtk :: Package ui :: Module progressbar

Source Code for Module dtk.ui.progressbar

  1  #! /usr/bin/env python 
  2  # -*- coding: utf-8 -*- 
  3   
  4  # Copyright (C) 2011 ~ 2012 Deepin, Inc. 
  5  #               2011 ~ 2012 Wang Yong 
  6  #  
  7  # Author:     Wang Yong <lazycat.manatee@gmail.com> 
  8  # Maintainer: Wang Yong <lazycat.manatee@gmail.com> 
  9  #  
 10  # This program is free software: you can redistribute it and/or modify 
 11  # it under the terms of the GNU General Public License as published by 
 12  # the Free Software Foundation, either version 3 of the License, or 
 13  # any later version. 
 14  #  
 15  # This program is distributed in the hope that it will be useful, 
 16  # but WITHOUT ANY WARRANTY; without even the implied warranty of 
 17  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 18  # GNU General Public License for more details. 
 19  #  
 20  # You should have received a copy of the GNU General Public License 
 21  # along with this program.  If not, see <http://www.gnu.org/licenses/>. 
 22   
 23  from draw import draw_round_rectangle, draw_vlinear, draw_text, draw_radial_round 
 24  from theme import ui_theme 
 25  from utils import alpha_color_hex_to_cairo, cairo_state, propagate_expose 
 26  import cairo 
 27  import gobject 
 28  import gtk 
 29  import pango 
 30   
31 -class ProgressBar(gtk.Button):
32 ''' 33 Progress bar. 34 35 @undocumented: expose_progressbar 36 @undocumented: update_light_ticker 37 ''' 38
39 - def __init__(self):
40 ''' 41 Initialize progress bar. 42 ''' 43 # Init. 44 gtk.Button.__init__(self) 45 self.progress = 0 46 self.light_ticker = 0 47 self.test_ticker = 0.0 48 49 # Expose callback. 50 self.connect("expose-event", self.expose_progressbar) 51 gtk.timeout_add(20, self.update_light_ticker)
52
53 - def expose_progressbar(self, widget, event):
54 ''' 55 Internal callback for `expose` signal. 56 ''' 57 # Init. 58 cr = widget.window.cairo_create() 59 rect = widget.allocation 60 61 # Draw frame. 62 cr.set_source_rgba(*alpha_color_hex_to_cairo(ui_theme.get_alpha_color("progressbar_frame").get_color_info())) 63 cr.set_operator(cairo.OPERATOR_OVER) 64 draw_round_rectangle(cr, rect.x, rect.y, rect.width, rect.height, 1) 65 cr.stroke() 66 67 # Draw background. 68 draw_vlinear(cr, rect.x, rect.y, rect.width, rect.height, 69 ui_theme.get_shadow_color("progressbar_background").get_color_info(), 70 1) 71 72 # Draw foreground. 73 draw_vlinear(cr, rect.x, rect.y, rect.width * self.progress / 100.0, rect.height, 74 ui_theme.get_shadow_color("progressbar_foreground").get_color_info(), 75 1) 76 77 # Draw font. 78 draw_text(cr, str(self.progress) + "%", 79 rect.x, rect.y, rect.width, rect.height, 80 rect.height - 5, "#000000", 81 alignment=pango.ALIGN_CENTER) 82 83 # Draw light. 84 light_radius = rect.height * 4 85 light_offset_x = min(self.light_ticker % 150, 100) / 100.0 * (rect.width + light_radius * 2) 86 with cairo_state(cr): 87 cr.rectangle(rect.x, rect.y, rect.width * self.progress / 100.0, rect.height) 88 cr.clip() 89 draw_radial_round(cr, rect.x + light_offset_x - light_radius, rect.y - light_radius / 2, light_radius, 90 ui_theme.get_shadow_color("progressbar_light").get_color_info()) 91 92 # Propagate expose. 93 propagate_expose(widget, event) 94 95 return True
96
97 - def update_light_ticker(self):
98 ''' 99 Internal function to update light ticker. 100 ''' 101 self.light_ticker += 1 102 return True
103
104 - def test_progressbar(self):
105 '''Test prorgressbar.''' 106 self.test_ticker += 1 107 self.progress = self.test_ticker % 101 108 self.queue_draw() 109 return True
110 111 gobject.type_register(ProgressBar) 112 113 if __name__ == "__main__": 114 import pseudo_skin 115 116 window = gtk.Window() 117 progressbar = ProgressBar() 118 progressbar.set_size_request(200, 14) 119 progressbar_align = gtk.Alignment() 120 progressbar_align.set(0.5, 0.5, 0.0, 0.0) 121 progressbar_align.add(progressbar) 122 window.add(progressbar_align) 123 window.set_size_request(300, 300) 124 window.connect("destroy", lambda w: gtk.main_quit()) 125 126 window.show_all() 127 gtk.timeout_add(100, progressbar.test_progressbar) 128 gtk.main() 129