~dpm/qreator/snap

« back to all changes in this revision

Viewing changes to qreator/QreatorWindow.py

  • Committer: David Planella
  • Date: 2012-01-21 10:58:53 UTC
  • Revision ID: david.planella@ubuntu.com-20120121105853-6h8pe064envjm5l9
Set up automatic resizing of images through PIL, and 3 standard output image sizes. Fixed RGBA > BGRA conversion going from PIL to Cairo

Show diffs side-by-side

added added

removed removed

Lines of Context:
10
10
import gi
11
11
gi.require_version('Gdk', '3.0')
12
12
gi.require_version('Gtk', '3.0')
13
 
from gi.repository import Gtk, Gdk, GLib # pylint: disable=E0611
 
13
from gi.repository import Gtk, Gdk # pylint: disable=E0611
14
14
import cairo
15
15
import logging
16
16
logger = logging.getLogger('qreator')
22
22
import qrencode
23
23
import array
24
24
import Image
 
25
import ImageOps
25
26
 
26
27
COL_TEXT = 0
27
28
COL_PIXBUF = 1
34
35
PAGE_URL = 4
35
36
 
36
37
 
 
38
class BarcodeSize(object):
 
39
    SMALL = 120
 
40
    MEDIUM = 230
 
41
    LARGE = 350
 
42
 
 
43
 
37
44
# See qreator_lib.Window.py for more details about how this class works
38
45
class QreatorWindow(Window):
39
46
    __gtype_name__ = "QreatorWindow"
47
54
 
48
55
        # Code for other initialization actions should be added here.
49
56
        self.clipboard = Gtk.Clipboard.get(Gdk.SELECTION_CLIPBOARD)
50
 
        self.scale_factor = 8
 
57
        self.barcode_size = BarcodeSize.SMALL
 
58
        self.pixel_size = 4
51
59
        self.surface = None
52
60
        self.ui.iconview1.set_text_column(COL_TEXT)
53
61
        self.ui.iconview1.set_pixbuf_column(COL_PIXBUF)
54
62
 
55
63
        self.ui.notebook1.set_show_tabs(False)
56
64
        self.ui.entry1.connect('changed', self.on_entry1_changed)
57
 
        
58
 
        ## alpha is starting transparency
59
 
        self.alpha = 0.25
60
 
        ## delta is amount to increase alpha
61
 
        self.delta = 0.01
62
65
 
63
66
    def on_toolbuttonHome_clicked(self, widget, data=None):
64
67
        self.ui.notebook1.set_current_page(PAGE_HOME)
82
85
 
83
86
        src_x, src_y = self.get_centered_coordinates(self.ui.drawingarea1,
84
87
                                                     self.surface)
85
 
        image_height = self.surface.get_height() * self.scale_factor
86
 
        image_width = self.surface.get_width() * self.scale_factor
 
88
        image_height = self.surface.get_height()
 
89
        image_width = self.surface.get_width()
87
90
 
88
91
        return Gdk.pixbuf_get_from_window(window, src_x, src_y,
89
92
                                          image_width, image_height)
126
129
        self.clipboard.set_image(pixbuf)
127
130
 
128
131
    def on_entry1_changed(self, widget, data=None):
129
 
        self.alpha = 0.25
130
132
        self.ui.drawingarea1.queue_draw()
131
133
 
132
134
    def get_centered_coordinates(self, drawing_area, surface):
139
141
        return (drawing_area_width / 2 - image_width / 2,
140
142
                drawing_area_height / 2 - image_height / 2)
141
143
 
142
 
    def fadeImage(self):
143
 
        self.ui.drawingarea1.queue_draw()
 
144
    def set_pixel_size(self, barcode_orig_size):
 
145
    
 
146
        pixel_size = 1
 
147
        
 
148
        while pixel_size * barcode_orig_size < self.barcode_size:
 
149
            pixel_size += 1
 
150
 
 
151
        while ((pixel_size * barcode_orig_size > self.barcode_size) or \
 
152
                ((pixel_size * barcode_orig_size) % 2 != 0)):
 
153
            pixel_size -= 1
 
154
        
 
155
        if pixel_size < 0:
 
156
            pixel_size = 1
 
157
            
 
158
        self.pixel_size = pixel_size
144
159
 
145
160
    def on_drawingarea1_draw(self, widget, ctx, data=None):
146
161
        text = self.ui.entry1.get_text()
147
162
        if text == '':
148
163
            return
149
164
 
 
165
        # Create the QR code
150
166
        version, size, im = qrencode.encode(text)
151
167
 
152
 
        # --------------- Cairo surface conversion ---------------------------
 
168
        # Resize the original image
 
169
        self.set_pixel_size(size)
 
170
        print self.pixel_size, size
 
171
        im = im.resize((self.pixel_size * size, self.pixel_size * size),
 
172
                       Image.NEAREST)
 
173
        im = im.convert('RGBA')
 
174
 
 
175
        # Add a border
 
176
        im = ImageOps.expand(im, border=(self.barcode_size - im.size[0]) / 2,
 
177
                             fill='white')
 
178
        
 
179
        # Convert the Python PIL image to a Cairo surface.
 
180
        # Notice the conversion to BGRA that Cairo expects
153
181
        # See http://cairographics.org/pythoncairopil/ and
154
 
        # http://mail.python.org/pipermail/image-sig/2005-October/003604.html
155
182
        # http://mail.gnome.org/archives/gtkmm-list/2007-May/msg00111.html
156
 
        # http://pygame.org/wiki/CairoPygame
157
 
        # Other:
158
 
        # http://stackoverflow.com/questions/8761887/scrolling-gtkdrawingarea
159
 
        # http://docs.clutter-project.org/docs/clutter-cookbook/1.0/textures-crossfade.html
160
 
 
161
 
        im = im.resize((168, 168),
162
 
                       Image.NEAREST)
163
 
        #im = im.resize((self.scale_factor * size, self.scale_factor * size),
164
 
        #               Image.NEAREST)
165
 
        im = im.convert('RGBA')
166
 
        bytearr = array.array('B', im.tostring())
 
183
        bytearr = array.array('B', im.tostring("raw", "BGRA", 0, 1))
167
184
        height, width = im.size
 
185
        print im.size
168
186
 
169
187
        self.surface = cairo.ImageSurface.create_for_data(bytearr,
170
188
                                                     cairo.FORMAT_ARGB32,
171
189
                                                     width, height,
172
190
                                                     width * 4)
173
191
 
174
 
        # Center the image
 
192
        # Center the image in the drawing area
175
193
        centered_width, centered_height = \
176
194
            self.get_centered_coordinates(widget, self.surface)
177
195
        ctx.translate(centered_width, centered_height)
180
198
        ctx.set_source_surface(self.surface)
181
199
 
182
200
        # Render the image
183
 
        ##ctx.paint()
184
 
        ctx.paint_with_alpha(self.alpha)
185
 
        self.alpha += self.delta
186
 
 
187
 
        if self.alpha >= 1:
188
 
            return False
189
 
        else:
190
 
            GLib.timeout_add(25, self.fadeImage)
 
201
        ctx.paint()