Package dtk :: Package ui :: Module listview_preview_pixbuf

Source Code for Module dtk.ui.listview_preview_pixbuf

 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 utils import get_content_size 
24  import cairo 
25  import gtk 
26  import pango 
27  import sys 
28   
29  # Below import must at end, otherwise will got ImportError 
30  from draw import draw_vlinear, draw_text 
31   
32 -def render_pixbuf(widget, event, input_args):
33 ''' 34 Render and save pixbuf. 35 36 @param widget: Gtk.Widget instance. 37 @param event: Expose event. 38 @param input_args: Input arguments as format: (select_num, vlinear_color, text_color, filepath). 39 ''' 40 # Init. 41 (select_num, vlinear_color, text_color, filepath) = input_args 42 43 cr = widget.window.cairo_create() 44 rect = widget.allocation 45 num_pixbuf = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, True, 8, rect.width, rect.height) 46 47 # Draw background. 48 cr.set_operator(cairo.OPERATOR_OVER) 49 draw_vlinear(cr, rect.x, rect.y, rect.width, rect.height, eval(vlinear_color)) 50 51 # Draw text. 52 draw_text(cr, select_num, rect.x, rect.y, rect.width, rect.height, text_color=text_color, 53 alignment=pango.ALIGN_CENTER) 54 55 # Render pixbuf from drawing area. 56 num_pixbuf.get_from_drawable( 57 widget.window, widget.get_colormap(), 0, 0, 0, 0, 58 rect.width, rect.height).save(filepath, "png") 59 60 # Exit after generate png file. 61 gtk.main_quit()
62 63 if __name__ == "__main__": 64 # Get input arguments. 65 input_args = sys.argv[1::] 66 (select_num, vlinear_color, text_color, filepath) = input_args 67 68 # Init. 69 num_padding_x = 8 70 num_padding_y = 1 71 (num_width, num_height) = get_content_size(select_num) 72 pixbuf_width = num_width + num_padding_x * 2 73 pixbuf_height = num_height + num_padding_y * 2 74 75 # Create window. 76 window = gtk.Window(gtk.WINDOW_POPUP) 77 window.set_colormap(gtk.gdk.Screen().get_rgba_colormap()) 78 window.move(-pixbuf_width, -pixbuf_height) # move out of screen 79 window.set_default_size(pixbuf_width, pixbuf_height) 80 window.connect( 81 "expose-event", 82 lambda w, e: render_pixbuf(w, e, input_args)) 83 84 window.show_all() 85 86 gtk.main() 87