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
16
15
logger = logging.getLogger('qreator')
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
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)
144
def set_pixel_size(self, barcode_orig_size):
148
while pixel_size * barcode_orig_size < self.barcode_size:
151
while ((pixel_size * barcode_orig_size > self.barcode_size) or \
152
((pixel_size * barcode_orig_size) % 2 != 0)):
158
self.pixel_size = pixel_size
160
140
def on_drawingarea1_draw(self, widget, ctx, data=None):
161
141
text = self.ui.entry1.get_text()
165
145
# Create the QR code
166
version, size, im = qrencode.encode(text)
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),
173
im = im.convert('RGBA')
176
im = ImageOps.expand(im, border=(self.barcode_size - im.size[0]) / 2,
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
187
self.surface = cairo.ImageSurface.create_for_data(bytearr,
147
self.surface = qr_code.encode_to_cairo(text)
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)
197
154
# Set the surface as the context's source
198
155
ctx.set_source_surface(self.surface)