Package dtk :: Package ui :: Module categorybar

Source Code for Module dtk.ui.categorybar

  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 box import EventBox 
 24  from constant import DEFAULT_FONT_SIZE, BUTTON_PRESS, BUTTON_NORMAL, BUTTON_HOVER 
 25  from draw import draw_vlinear, draw_pixbuf, draw_text, expose_linear_background 
 26  from theme import ui_theme 
 27  from utils import get_content_size, propagate_expose 
 28  import gobject 
 29  import gtk 
 30   
31 -class Categorybar(EventBox):
32 ''' 33 Categorybar. 34 ''' 35
36 - def __init__(self, 37 items, 38 font_size=DEFAULT_FONT_SIZE, 39 padding_left=20, 40 padding_middle=10, 41 padding_right=25):
42 ''' 43 Initialize Categorybar class. 44 45 @param items: A list of category item, format: (icon_dpixbuf, content, click_callback) 46 ''' 47 # Init event box. 48 super(Categorybar, self).__init__() 49 self.category_index = 0 50 self.connect( 51 "expose-event", 52 lambda w, e: 53 expose_linear_background(w, e, ui_theme.get_shadow_color("categorybar_background").get_color_info())) 54 55 # Init category box. 56 self.category_item_box = gtk.VBox() 57 self.add(self.category_item_box) 58 59 # Init item. 60 if items: 61 icon_width = self.get_icon_width(items) 62 for (index, item) in enumerate(items): 63 category_item = CategoryItem(item, index, font_size, icon_width, padding_left, padding_middle, padding_right, 64 self.set_index, self.get_index) 65 self.category_item_box.pack_start(category_item) 66 67 # Show. 68 self.show_all()
69
70 - def set_index(self, index):
71 ''' 72 Set selected item index. 73 74 @param index: Index of selected item. 75 ''' 76 self.category_item_box.queue_draw() 77 self.category_index = index
78
79 - def get_index(self):
80 ''' 81 Get selected index. 82 83 @return: Return selected item index. 84 ''' 85 return self.category_index
86
87 - def get_icon_width(self, items):
88 ''' 89 Get icon width. 90 91 @param items: A list of category item, format: (icon_dpixbuf, content, click_callback) 92 ''' 93 icon_width = 0 94 for (icon_dpixbuf, content, _) in items: 95 if icon_dpixbuf: 96 icon_width = icon_dpixbuf.get_pixbuf().get_width() 97 break 98 99 return icon_width
100 101 gobject.type_register(Categorybar) 102
103 -class CategoryItem(gtk.Button):
104 ''' 105 CategoryItem class for use in CategoryBar. 106 107 @undocumented: wrap_category_item_clicked_action 108 @undocumented: expose_category_item 109 ''' 110
111 - def __init__(self, 112 item, 113 index, 114 font_size, 115 icon_width, 116 padding_left, 117 padding_middle, 118 padding_right, 119 set_index, 120 get_index):
121 ''' 122 Initialize CategoryItem class. 123 124 @param item: Category item, format: (item_dpixbuf, content, click_callback) 125 @param index: Category item index. 126 @param font_size: Font size. 127 @param icon_width: Icon width. 128 @param padding_left: Padding at left of item. 129 @param padding_middle: Padding between icon and font. 130 @param padding_right: Padding at right of item. 131 @param set_index: Set index callback. 132 @param get_index: Get index callback. 133 ''' 134 # Init. 135 gtk.Button.__init__(self) 136 self.font_size = font_size 137 self.index = index 138 self.set_index = set_index 139 self.get_index = get_index 140 self.padding_left = padding_left 141 self.padding_right = padding_right 142 (self.icon_dpixbuf, self.content, self.clicked_callback) = item 143 (content_width, font_height) = get_content_size(self.content, self.font_size) 144 145 # Init item button. 146 self.font_offset = 0 147 if icon_width == 0: 148 self.font_offset = 0 149 else: 150 self.font_offset = padding_middle + icon_width 151 self.set_size_request( 152 padding_left + self.font_offset + content_width + padding_right, 153 -1 154 ) 155 156 self.connect("expose-event", self.expose_category_item) 157 self.connect("clicked", lambda w: self.wrap_category_item_clicked_action())
158
160 ''' 161 Internal function, wrap clicked action. 162 ''' 163 if self.clicked_callback: 164 self.clicked_callback() 165 self.set_index(self.index)
166
167 - def expose_category_item(self, widget, event):
168 ''' 169 Internal function, callback for `expose-event` signal. 170 171 @param widget: Gtk.Widget instance. 172 @param event: Expose event. 173 @return: Always return True. 174 ''' 175 # Init. 176 cr = widget.window.cairo_create() 177 rect = widget.allocation 178 select_index = self.get_index() 179 font_color = ui_theme.get_color("category_item").get_color() 180 181 # Draw background. 182 if widget.state == gtk.STATE_NORMAL: 183 if select_index == self.index: 184 select_status = BUTTON_PRESS 185 else: 186 select_status = BUTTON_NORMAL 187 elif widget.state == gtk.STATE_PRELIGHT: 188 if select_index == self.index: 189 select_status = BUTTON_PRESS 190 else: 191 select_status = BUTTON_HOVER 192 elif widget.state == gtk.STATE_ACTIVE: 193 select_status = BUTTON_PRESS 194 195 if select_status == BUTTON_PRESS: 196 draw_vlinear(cr, rect.x, rect.y, rect.width, rect.height, 197 ui_theme.get_shadow_color("category_item_press").get_color_info()) 198 199 font_color = ui_theme.get_color("category_select_item").get_color() 200 elif select_status == BUTTON_HOVER: 201 draw_vlinear(cr, rect.x, rect.y, rect.width, rect.height, 202 ui_theme.get_shadow_color("category_item_hover").get_color_info()) 203 204 font_color = ui_theme.get_color("category_select_item").get_color() 205 206 # Draw navigate item. 207 category_item_pixbuf = self.icon_dpixbuf.get_pixbuf() 208 draw_pixbuf( 209 cr, category_item_pixbuf, 210 rect.x + self.padding_left, 211 rect.y + (rect.height - category_item_pixbuf.get_height()) / 2 212 ) 213 214 # Draw font. 215 draw_text(cr, self.content, 216 rect.x + self.padding_left + self.font_offset, 217 rect.y, 218 rect.width - self.padding_left - self.font_offset - self.padding_right, 219 rect.height, 220 self.font_size, 221 font_color, 222 ) 223 224 # Propagate expose to children. 225 propagate_expose(widget, event) 226 227 return True
228 229 gobject.type_register(CategoryItem) 230