Package dtk :: Package ui :: Module theme

Source Code for Module dtk.ui.theme

  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 skin_config import skin_config 
 24  from utils import eval_file, get_parent_dir, create_directory 
 25  import gtk 
 26  import os 
 27   
28 -class DynamicColor(object):
29 ''' 30 Dynamic color. 31 ''' 32
33 - def __init__(self, color):
34 ''' 35 Initialize DynamicColor. 36 37 @param color: Initialize color. 38 ''' 39 self.update(color)
40
41 - def update(self, color):
42 ''' 43 Update color. 44 45 @param color: Color value. 46 ''' 47 self.color = color
48
49 - def get_color(self):
50 ''' 51 Get color. 52 53 @return: Return current color value. 54 ''' 55 return self.color
56
57 -class DynamicAlphaColor(object):
58 ''' 59 Dynamic alpha color. 60 ''' 61
62 - def __init__(self, color_info):
63 ''' 64 Initialize DynamicAlphaColor class. 65 66 @param color_info: Color information, format as (hex_color, alpha) 67 ''' 68 self.update(color_info)
69
70 - def update(self, color_info):
71 ''' 72 Update color_info with given value. 73 ''' 74 (self.color, self.alpha) = color_info
75
76 - def get_color_info(self):
77 ''' 78 Get color info. 79 80 @return: Return color information, format as (hex_color, alpha) 81 ''' 82 return (self.color, self.alpha)
83
84 - def get_color(self):
85 ''' 86 Get color. 87 88 @return: Return hex color string. 89 ''' 90 return self.color
91
92 - def get_alpha(self):
93 ''' 94 Get alpha value. 95 96 @return: Return alpha value. 97 ''' 98 return self.alpha
99
100 -class DynamicShadowColor(object):
101 ''' 102 Dynamic shadow color. 103 ''' 104
105 - def __init__(self, color_info):
106 ''' 107 Initialize DynamicShadowColor class. 108 109 @param color_info: Color information, format as: 110 111 >>> [(color_position_1, (hex_color_1, color_alpha_1), 112 >>> (color_position_2, (hex_color_2, color_alpha_2), 113 >>> (color_position_3, (hex_color_3, color_alpha_3)] 114 ''' 115 self.update(color_info)
116
117 - def update(self, color_info):
118 ''' 119 Update color with given value. 120 121 @param color_info: Color information, format as: 122 123 >>> [(color_position_1, (hex_color_1, color_alpha_1), 124 >>> (color_position_2, (hex_color_2, color_alpha_2), 125 >>> (color_position_3, (hex_color_3, color_alpha_3)] 126 ''' 127 self.color_info = color_info
128
129 - def get_color_info(self):
130 ''' 131 Get color information. 132 133 @return: Return color information, format as: 134 135 >>> [(color_position_1, (hex_color_1, color_alpha_1), 136 >>> (color_position_2, (hex_color_2, color_alpha_2), 137 >>> (color_position_3, (hex_color_3, color_alpha_3)] 138 ''' 139 return self.color_info
140
141 -class DynamicPixbuf(object):
142 ''' 143 Dynamic pixbuf. 144 ''' 145
146 - def __init__(self, filepath):
147 ''' 148 Initialize DynamicPixbuf class. 149 150 @param filepath: Dynamic pixbuf filepath. 151 ''' 152 self.update(filepath)
153
154 - def update(self, filepath):
155 ''' 156 Update filepath with given value. 157 158 @param filepath: Dynamic pixbuf filepath. 159 ''' 160 self.pixbuf = gtk.gdk.pixbuf_new_from_file(filepath)
161
162 - def get_pixbuf(self):
163 ''' 164 Get pixbuf. 165 ''' 166 return self.pixbuf
167
168 -class Theme(object):
169 ''' 170 Theme. 171 172 @undocumented: get_ticker 173 ''' 174
175 - def __init__(self, 176 system_theme_dir, 177 user_theme_dir):
178 ''' 179 Initialize Theme class. 180 181 @param system_theme_dir: Default theme directory. 182 @param user_theme_dir: User's theme save directory, generic is ~/.config/project-name/theme 183 ''' 184 # Init. 185 self.system_theme_dir = system_theme_dir 186 self.user_theme_dir = user_theme_dir 187 self.theme_info_file = "theme.txt" 188 self.ticker = 0 189 self.pixbuf_dict = {} 190 self.color_dict = {} 191 self.alpha_color_dict = {} 192 self.shadow_color_dict = {} 193 194 # Create directory if necessarily. 195 for theme_dir in [self.system_theme_dir, self.user_theme_dir]: 196 create_directory(theme_dir)
197
198 - def load_theme(self):
199 ''' 200 Load theme. 201 ''' 202 self.theme_name = skin_config.theme_name 203 204 # Scan dynamic theme_info file. 205 theme_info = eval_file(self.get_theme_file_path(self.theme_info_file)) 206 207 # Init dynamic colors. 208 for (color_name, color) in theme_info["colors"].items(): 209 self.color_dict[color_name] = DynamicColor(color) 210 211 # Init dynamic alpha colors. 212 for (color_name, color_info) in theme_info["alpha_colors"].items(): 213 self.alpha_color_dict[color_name] = DynamicAlphaColor(color_info) 214 215 # Init dynamic shadow colors. 216 for (color_name, color_info) in theme_info["shadow_colors"].items(): 217 self.shadow_color_dict[color_name] = DynamicShadowColor(color_info) 218 219 # Add in theme list of skin_config. 220 skin_config.add_theme(self)
221
222 - def get_theme_file_path(self, filename):
223 ''' 224 Get theme file path with given theme name. 225 226 @return: Return filepath of theme. 227 ''' 228 theme_file_dir = None 229 for theme_dir in [self.system_theme_dir, self.user_theme_dir]: 230 if os.path.exists(theme_dir): 231 if self.theme_name in os.listdir(os.path.expanduser(theme_dir)): 232 theme_file_dir = theme_dir 233 break 234 235 if theme_file_dir: 236 return os.path.join(theme_file_dir, self.theme_name, filename) 237 else: 238 return None
239
240 - def get_pixbuf(self, path):
241 ''' 242 Get pixbuf with given relative path. 243 244 @param path: Image relative filepath to theme. 245 246 @return: Return pixbuf with given relative path. 247 ''' 248 # Just init pixbuf_dict when first load some pixbuf. 249 if not self.pixbuf_dict.has_key(path): 250 self.pixbuf_dict[path] = DynamicPixbuf(self.get_theme_file_path("image/%s" % (path))) 251 252 return self.pixbuf_dict[path]
253
254 - def get_color(self, color_name):
255 ''' 256 Get color with given dynmaic color. 257 258 @param color_name: DynamicColor name from theme.txt. 259 260 @return: Return color with given dynamic color. 261 ''' 262 return self.color_dict[color_name]
263
264 - def get_alpha_color(self, color_name):
265 ''' 266 Get color with given dynmaic alpha color. 267 268 @param color_name: DynamicAlphaColor name from theme.txt. 269 270 @return: Return color with given dynamic alpha color. 271 ''' 272 return self.alpha_color_dict[color_name]
273
274 - def get_shadow_color(self, color_name):
275 ''' 276 Get color with given dynmaic shadow color. 277 278 @param color_name: DynamicShadowColor name from theme.txt. 279 280 @return: Return color with given dynamic shadow color. 281 ''' 282 return self.shadow_color_dict[color_name]
283
284 - def get_ticker(self):
285 ''' 286 Internal function to get ticker. 287 ''' 288 return self.ticker
289
290 - def change_theme(self, new_theme_name):
291 ''' 292 Change theme with given new theme name. 293 294 @param new_theme_name: New theme name. 295 ''' 296 # Update ticker. 297 self.ticker += 1 298 299 # Change theme name. 300 self.theme_name = new_theme_name 301 302 # Update dynmaic pixbuf. 303 for (path, pixbuf) in self.pixbuf_dict.items(): 304 pixbuf.update(self.get_theme_file_path("image/%s" % (path))) 305 306 # Update dynamic colors. 307 theme_info = eval_file(self.get_theme_file_path(self.theme_info_file)) 308 309 for (color_name, color) in theme_info["colors"].items(): 310 self.color_dict[color_name].update(color) 311 312 # Update dynamic alpha colors. 313 for (color_name, color_info) in theme_info["alpha_colors"].items(): 314 self.alpha_color_dict[color_name].update(color_info) 315 316 # Update shadow colors. 317 for (color_name, color_info) in theme_info["shadow_colors"].items(): 318 self.shadow_color_dict[color_name].update(color_info)
319 320 # Init. 321 ui_theme = Theme(os.path.join(get_parent_dir(__file__, 2), "theme"), 322 os.path.expanduser("~/.config/deepin-ui/theme")) 323