Package dtk :: Package ui :: Module scalebar

Source Code for Module dtk.ui.scalebar

  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 cache_pixbuf import CachePixbuf 
 24  from draw import draw_pixbuf 
 25  from utils import is_left_button 
 26  import gobject 
 27  import gtk 
 28   
29 -class HScalebar(gtk.HScale):
30 ''' 31 HScalebar. 32 33 @undocumented: expose_h_scalebar 34 @undocumented: press_volume_progressbar 35 ''' 36
37 - def __init__(self, 38 left_fg_dpixbuf, 39 left_bg_dpixbuf, 40 middle_fg_dpixbuf, 41 middle_bg_dpixbuf, 42 right_fg_dpixbuf, 43 right_bg_dpixbuf, 44 point_dpixbuf 45 ):
46 ''' 47 Init HScalebar class. 48 49 @param left_fg_dpixbuf: Left foreground pixbuf. 50 @param left_bg_dpixbuf: Left background pixbuf. 51 @param middle_fg_dpixbuf: Middle foreground pixbuf. 52 @param middle_bg_dpixbuf: Middle background pixbuf. 53 @param right_fg_dpixbuf: Right foreground pixbuf. 54 @param right_bg_dpixbuf: Right background pixbuf. 55 @param point_dpixbuf: Pointer pixbuf. 56 ''' 57 # Init. 58 gtk.HScale.__init__(self) 59 self.set_draw_value(False) 60 self.set_range(0, 100) 61 self.left_fg_dpixbuf = left_fg_dpixbuf 62 self.left_bg_dpixbuf = left_bg_dpixbuf 63 self.middle_fg_dpixbuf = middle_fg_dpixbuf 64 self.middle_bg_dpixbuf = middle_bg_dpixbuf 65 self.right_fg_dpixbuf = right_fg_dpixbuf 66 self.right_bg_dpixbuf = right_bg_dpixbuf 67 self.point_dpixbuf = point_dpixbuf 68 self.cache_bg_pixbuf = CachePixbuf() 69 self.cache_fg_pixbuf = CachePixbuf() 70 71 # Set size request. 72 self.set_size_request(-1, self.point_dpixbuf.get_pixbuf().get_height()) 73 74 # Redraw. 75 self.connect("expose-event", self.expose_h_scalebar) 76 self.connect("button-press-event", self.press_volume_progressbar)
77
78 - def expose_h_scalebar(self, widget, event):
79 ''' 80 Internal callback for `expose-event` signal. 81 ''' 82 # Init. 83 cr = widget.window.cairo_create() 84 rect = widget.allocation 85 86 # Init pixbuf. 87 left_fg_pixbuf = self.left_fg_dpixbuf.get_pixbuf() 88 left_bg_pixbuf = self.left_bg_dpixbuf.get_pixbuf() 89 middle_fg_pixbuf = self.middle_fg_dpixbuf.get_pixbuf() 90 middle_bg_pixbuf = self.middle_bg_dpixbuf.get_pixbuf() 91 right_fg_pixbuf = self.right_fg_dpixbuf.get_pixbuf() 92 right_bg_pixbuf = self.right_bg_dpixbuf.get_pixbuf() 93 point_pixbuf = self.point_dpixbuf.get_pixbuf() 94 95 # Init value. 96 upper = self.get_adjustment().get_upper() 97 lower = self.get_adjustment().get_lower() 98 total_length = max(upper - lower, 1) 99 side_width = left_bg_pixbuf.get_width() 100 point_width = point_pixbuf.get_width() 101 point_height = point_pixbuf.get_height() 102 x, y, w, h = rect.x + point_width / 2, rect.y, rect.width - point_width, rect.height 103 line_height = left_bg_pixbuf.get_height() 104 line_y = y + (point_height - line_height) / 2 105 value = int((self.get_value() - lower) / total_length * w) 106 107 # Draw background. 108 self.cache_bg_pixbuf.scale(middle_bg_pixbuf, w - side_width * 2, line_height) 109 draw_pixbuf(cr, left_bg_pixbuf, x, line_y) 110 draw_pixbuf(cr, self.cache_bg_pixbuf.get_cache(), x + side_width, line_y) 111 draw_pixbuf(cr, right_bg_pixbuf, x + w - side_width, line_y) 112 113 # Draw foreground. 114 if value > 0: 115 self.cache_fg_pixbuf.scale(middle_fg_pixbuf, value, line_height) 116 draw_pixbuf(cr, left_fg_pixbuf, x, line_y) 117 draw_pixbuf(cr, self.cache_fg_pixbuf.get_cache(), x + side_width, line_y) 118 draw_pixbuf(cr, right_fg_pixbuf, x + value, line_y) 119 120 # Draw drag point. 121 draw_pixbuf(cr, point_pixbuf, x + value - point_pixbuf.get_width() / 2, y) 122 123 return True
124
125 - def press_volume_progressbar(self, widget, event):
126 ''' 127 Internal callback for `button-press-event` signal. 128 ''' 129 # Init. 130 if is_left_button(event): 131 rect = widget.allocation 132 lower = self.get_adjustment().get_lower() 133 upper = self.get_adjustment().get_upper() 134 point_width = self.point_dpixbuf.get_pixbuf().get_width() 135 136 # Set value. 137 self.set_value(lower + ((event.x - point_width / 2) / (rect.width - point_width)) * (upper - lower)) 138 self.queue_draw() 139 140 return False
141 142 gobject.type_register(HScalebar) 143
144 -class VScalebar(gtk.VScale):
145 ''' 146 VScalebar. 147 148 @undocumented: expose_v_scalebar 149 @undocumented: press_progressbar 150 ''' 151
152 - def __init__(self, 153 upper_fg_dpixbuf, 154 upper_bg_dpixbuf, 155 middle_fg_dpixbuf, 156 middle_bg_dpixbuf, 157 bottom_fg_dpixbuf, 158 bottom_bg_dpixbuf, 159 point_dpixbuf, 160 ):
161 ''' 162 Initialize VScalebar class. 163 164 @param upper_fg_dpixbuf: Upper foreground pixbuf. 165 @param upper_bg_dpixbuf: Upper background pixbuf. 166 @param middle_fg_dpixbuf: Middle foreground pixbuf. 167 @param middle_bg_dpixbuf: Middle background pixbuf. 168 @param bottom_fg_dpixbuf: Bottom foreground pixbuf. 169 @param bottom_bg_dpixbuf: Bottom background pixbuf. 170 @param point_dpixbuf: Pointer pixbuf. 171 ''' 172 gtk.VScale.__init__(self) 173 174 self.set_draw_value(False) 175 self.set_range(0, 100) 176 self.__has_point = True 177 self.set_inverted(True) 178 self.upper_fg_dpixbuf = upper_fg_dpixbuf 179 self.upper_bg_dpixbuf = upper_bg_dpixbuf 180 self.middle_fg_dpixbuf = middle_fg_dpixbuf 181 self.middle_bg_dpixbuf = middle_bg_dpixbuf 182 self.bottom_fg_dpixbuf = bottom_fg_dpixbuf 183 self.bottom_bg_dpixbuf = bottom_bg_dpixbuf 184 self.point_dpixbuf = point_dpixbuf 185 self.cache_bg_pixbuf = CachePixbuf() 186 self.cache_fg_pixbuf = CachePixbuf() 187 188 self.set_size_request(self.point_dpixbuf.get_pixbuf().get_height(), -1) 189 190 self.connect("expose-event", self.expose_v_scalebar) 191 self.connect("button-press-event", self.press_progressbar)
192
193 - def expose_v_scalebar(self, widget, event):
194 ''' 195 Internal callback for `expose-event` signal. 196 ''' 197 cr = widget.window.cairo_create() 198 rect = widget.allocation 199 200 # Init pixbuf. 201 upper_fg_pixbuf = self.upper_fg_dpixbuf.get_pixbuf() 202 upper_bg_pixbuf = self.upper_bg_dpixbuf.get_pixbuf() 203 middle_fg_pixbuf = self.middle_fg_dpixbuf.get_pixbuf() 204 middle_bg_pixbuf = self.middle_bg_dpixbuf.get_pixbuf() 205 bottom_fg_pixbuf = self.bottom_fg_dpixbuf.get_pixbuf() 206 bottom_bg_pixbuf = self.bottom_bg_dpixbuf.get_pixbuf() 207 point_pixbuf = self.point_dpixbuf.get_pixbuf() 208 209 upper_value = self.get_adjustment().get_upper() 210 lower_value = self.get_adjustment().get_lower() 211 total_length = max(upper_value - lower_value, 1) 212 point_width = point_pixbuf.get_width() 213 point_height = point_pixbuf.get_height() 214 215 line_width = upper_bg_pixbuf.get_width() 216 side_height = upper_bg_pixbuf.get_height() 217 218 x, y, w, h = rect.x, rect.y + point_height, rect.width, rect.height - point_height - point_height / 2 219 line_x = x + (point_width - line_width / 1.5) / 2 220 point_y = h - int((self.get_value() - lower_value ) / total_length * h) 221 value = int((self.get_value() - lower_value ) / total_length * h) 222 223 self.cache_bg_pixbuf.scale(middle_bg_pixbuf, line_width, h - side_height * 2 + point_height / 2) 224 draw_pixbuf(cr, upper_bg_pixbuf, line_x, y - point_height / 2) 225 draw_pixbuf(cr, self.cache_bg_pixbuf.get_cache(), line_x, y + side_height - point_height / 2) 226 draw_pixbuf(cr, bottom_bg_pixbuf, line_x, y + h - side_height) 227 228 if value > 0: 229 self.cache_fg_pixbuf.scale(middle_fg_pixbuf, line_width, value) 230 draw_pixbuf(cr, self.cache_fg_pixbuf.get_cache(), line_x, y + point_y - side_height) 231 draw_pixbuf(cr, bottom_fg_pixbuf, line_x, y + h - side_height) 232 233 if self.get_value() == upper_value: 234 draw_pixbuf(cr, upper_fg_pixbuf, line_x, y - point_height / 2) 235 236 if self.__has_point: 237 draw_pixbuf(cr, point_pixbuf, x, y + point_y - side_height / 2 - point_height / 2) 238 239 return True
240
241 - def press_progressbar(self, widget, event):
242 ''' 243 Internal callback for `button-press-event` signal. 244 ''' 245 if is_left_button(event): 246 rect = widget.allocation 247 lower_value = self.get_adjustment().get_lower() 248 upper_value = self.get_adjustment().get_upper() 249 point_height = self.point_dpixbuf.get_pixbuf().get_height() 250 self.set_value(upper_value - ((event.y - point_height / 2) / (rect.height - point_height)) * (upper_value - lower_value) ) 251 self.queue_draw() 252 253 return False
254
255 - def set_has_point(self, value):
256 ''' 257 Set has point. 258 ''' 259 self.__has_point = value
260
261 - def get_has_point(self):
262 ''' 263 Get has point. 264 ''' 265 return self.__has_point 266 267 gobject.type_register(VScalebar) 268