~thomas-deruyter-3/qreator/qreator

« back to all changes in this revision

Viewing changes to qreator/QreatorWindow.py

  • Committer: David Planella
  • Date: 2012-01-22 09:32:24 UTC
  • Revision ID: david.planella@ubuntu.com-20120122093224-k7martvnj1mdhjjn
Moved the QR generation code to a separate module, independent of the UI

Show diffs side-by-side

added added

removed removed

Lines of Context:
11
11
gi.require_version('Gdk', '3.0')
12
12
gi.require_version('Gtk', '3.0')
13
13
from gi.repository import Gtk, Gdk # pylint: disable=E0611
14
 
import cairo
15
14
import logging
16
15
logger = logging.getLogger('qreator')
17
16
 
19
18
from qreator.AboutQreatorDialog import AboutQreatorDialog
20
19
from qreator.PreferencesQreatorDialog import PreferencesQreatorDialog
21
20
 
22
 
import qrencode
23
 
import array
24
 
import Image
25
 
import ImageOps
 
21
from QRCode import QRCode as QRCode
26
22
 
27
23
COL_TEXT = 0
28
24
COL_PIXBUF = 1
54
50
 
55
51
        # Code for other initialization actions should be added here.
56
52
        self.clipboard = Gtk.Clipboard.get(Gdk.SELECTION_CLIPBOARD)
57
 
        self.barcode_size = BarcodeSize.SMALL
58
 
        self.pixel_size = 4
 
53
        context = self.ui.toolbar1.get_style_context()
 
54
        context.add_class(Gtk.STYLE_CLASS_PRIMARY_TOOLBAR)
59
55
        self.surface = None
60
56
        self.ui.iconview1.set_text_column(COL_TEXT)
61
57
        self.ui.iconview1.set_pixbuf_column(COL_PIXBUF)
141
137
        return (drawing_area_width / 2 - image_width / 2,
142
138
                drawing_area_height / 2 - image_height / 2)
143
139
 
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
159
 
 
160
140
    def on_drawingarea1_draw(self, widget, ctx, data=None):
161
141
        text = self.ui.entry1.get_text()
162
142
        if text == '':
163
143
            return
164
144
 
165
145
        # Create the QR code
166
 
        version, size, im = qrencode.encode(text)
167
 
 
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
181
 
        # See http://cairographics.org/pythoncairopil/ and
182
 
        # http://mail.gnome.org/archives/gtkmm-list/2007-May/msg00111.html
183
 
        bytearr = array.array('B', im.tostring("raw", "BGRA", 0, 1))
184
 
        height, width = im.size
185
 
        print im.size
186
 
 
187
 
        self.surface = cairo.ImageSurface.create_for_data(bytearr,
188
 
                                                     cairo.FORMAT_ARGB32,
189
 
                                                     width, height,
190
 
                                                     width * 4)
 
146
        qr_code = QRCode()
 
147
        self.surface = qr_code.encode_to_cairo(text)
191
148
 
192
149
        # Center the image in the drawing area
193
150
        centered_width, centered_height = \
194
151
            self.get_centered_coordinates(widget, self.surface)
195
152
        ctx.translate(centered_width, centered_height)
196
 
        
 
153
 
197
154
        # Set the surface as the context's source
198
155
        ctx.set_source_surface(self.surface)
199
156