1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
|
# -*- Mode: Python; coding: utf-8; indent-tabs-mode: nil; tab-width: 4 -*-
### BEGIN LICENSE
# Copyright (C) 2012 David Planella <david.planella@ubuntu.com>
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License version 3, as published
# by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranties of
# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
# PURPOSE. See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program. If not, see <http://www.gnu.org/licenses/>.
### END LICENSE
import os
import tempfile
import atexit
import cairo
import math
from gi.repository import Gtk, Gdk, GdkPixbuf, GObject # pylint: disable=E0611
import logging
logger = logging.getLogger('qreator')
from qreator_lib.i18n import _
from qreator_lib import Window
from qreator_lib.helpers import get_media_file
from QRCode import QRCode
from QRCode import QRCodeOutput
from qreator.qrcodes.QRCodeType import QRCodeType
COL_DESC = 0
COL_PIXBUF = 1
COL_ID = 2
PAGE_NEW = 0
#PAGE_HISTORY = 1
PAGE_ABOUT = 1
PAGE_QR = 2
# See qreator_lib.Window.py for more details about how this class works
class QreatorWindow(Window):
__gtype_name__ = "QreatorWindow"
def finish_initializing(self, builder): # pylint: disable=E1002
"""Set up the main window"""
super(QreatorWindow, self).finish_initializing(builder)
# Code for other initialization actions should be added here.
# Initialize the clipboard
self.clipboard = Gtk.Clipboard.get(Gdk.SELECTION_CLIPBOARD)
# Initialize the style for the main toolbar
context = self.ui.toolbar1.get_style_context()
context.add_class(Gtk.STYLE_CLASS_PRIMARY_TOOLBAR)
# Initialize the Cairo surface that will contain the QR code
self.surface = None
# Hide the notebook tabs
self.ui.notebook1.set_show_tabs(False)
# Initialize about dialog
self.init_about_dialog()
# Initialize the QR types icon view
self.init_qr_types()
# Load the background texture in the QR code page
self.texture = cairo.ImageSurface.create_from_png(
get_media_file("pattern.png"))
# Time to wait in milliseconds before switching to the next view
# after having clicked once on an iconview icon
self.iconview_activation_delay = 200
# Add an initial text, so that there is an initial QR code
self.qr_code_placeholder = 'http://launchpad.net/qreator'
# Prepare the qrcode for drag n drop
self.tempfilepath = None
def on_drag_data_get(widget, drag_context, selection_data,
info, time):
if self.tempfilepath is None:
handle, self.tempfilepath = tempfile.mkstemp(
suffix=".png", prefix="qreator_")
pixbuf = self.get_pixbuf_from_drawing_area()
pixbuf.savev(self.tempfilepath, 'png', [], [])
selection_data.set_uris(["file://{}".format(self.tempfilepath),])
return True
def remove_tempfile(path):
if os.path.exists(path):
os.remove(path)
return False
def on_drag_end(widget, drag_context):
atexit.register(remove_tempfile, self.tempfilepath)
self.tempfilepath = None
self.ui.qr_drawingarea.drag_source_set(Gdk.ModifierType.BUTTON1_MASK,
[], Gdk.DragAction.COPY)
self.ui.qr_drawingarea.drag_source_set_target_list(None)
self.ui.qr_drawingarea.drag_source_add_uri_targets()
self.ui.qr_drawingarea.connect("drag_data_get", on_drag_data_get)
self.ui.qr_drawingarea.connect("drag_end", on_drag_end)
def init_qr_types(self):
# Set up the QR types shown in the main icon view
self.ui.qr_types_view.set_text_column(COL_DESC)
self.ui.qr_types_view.set_pixbuf_column(COL_PIXBUF)
# By importing the different qrcode dataformats in the __init__ file,
# they have automatically registered themselves in the base class.
self.qr_types = [d(self.update_qr_code) for d in QRCodeType.dataformats]
self.qr_types_store = Gtk.ListStore(str, GdkPixbuf.Pixbuf, int)
# ^ desc, icon, id ^
self.qr_types_store.set_sort_column_id(COL_DESC,
Gtk.SortType.ASCENDING)
self.ui.qr_types_view.set_model(self.qr_types_store)
self.fill_qr_types_store()
self.curr_height = 0
self.curr_width = 0
for qr_type in self.qr_types:
qr_type.widget.grid.hide() # keep the window from growing with each qrtype
self.ui.qr_input_box.add(qr_type.widget.grid)
qr_type.widget.on_prepared()
def fill_qr_types_store(self):
self.qr_types_store.clear()
for qr_id, qr_type in enumerate(self.qr_types):
icon = GdkPixbuf.Pixbuf.new_from_file(qr_type.icon_path)
self.qr_types_store.append([qr_type.description,
icon,
qr_id])
def on_qreator_window_check_resize(self, widget):
'''We need this function to fix the issue described at
http://bit.ly/LW94BO whereby the number of columns of the icon view
widget stays fixed at the number set for the initial width of the
window'''
# Get the new size
new_width = widget.get_size()[0]
new_height = widget.get_size()[1]
# If the size has changed...
if(new_width != self.curr_width or new_height != self.curr_height):
# Remember new size
self.curr_width = new_width
self.curr_height = new_height
# and refill iconviews with icons to adjust columns number
self.fill_qr_types_store()
def init_about_dialog(self):
# Initialize about dialog
about = Gtk.AboutDialog()
about.set_program_name("Qreator")
about.set_copyright(
"Copyright (c) 2012 David Planella" +
"\nhttp://about.me/david.planella")
about.set_website("https://launchpad.net/qreator")
#FIXME: the version should be picked up from setup.py
about.set_version('12.05.7-dev')
about.set_authors([
'David Planella <david.planella@ubuntu.com>',
'Michael Hall <mhall119@ubuntu.com>',
'Andrew Starr-Bochicchio <andrewsomething@ubuntu.com>',
'Stefan Schwarzburg <stefan.schwarzburg@googlemail.com>',
'Jef Spaleta <jspaleta@fedoraproject.org>',
'Fong LH <fonglh@gmail.com>',
])
about.set_license(_('Distributed under the GPL v3 license.') +
'\nhttp://www.opensource.org/licenses/gpl-3.0.html')
# Workaround for bug #1031657, remove David from translation credits
# unless the locale is Catalan
import locale
default_locale, encoding = locale.getdefaultlocale()
# Gtk.AboutDialog does not recognize https and would not render the
# markup otherwise
credits = _("translator-credits").replace('https', 'http')
if default_locale.split('_')[0] != 'ca':
credits = credits.replace('David Planella' + ' ' +
'http://launchpad.net/~dpm', '').strip()
about.set_translator_credits(credits)
box = self.ui.about_box
about.vbox.reparent(box)
# Get rid of the 'Close' button
for button in about.action_area.get_children():
if button.get_property('label') == 'gtk-close':
button.destroy()
##########################################
def on_toolbuttonNew_clicked(self, widget, data=None):
'''Shows the home page'''
self.ui.qr_types_view.unselect_all()
self.ui.notebook1.set_current_page(PAGE_NEW)
def on_toolbuttonHistory_clicked(self, widget, data=None):
'''Shows the history page'''
pass # self.ui.notebook1.set_current_page(PAGE_HISTORY)
def on_toolbuttonAbout_clicked(self, widget, data=None):
'''Shows the about page'''
self.ui.notebook1.set_current_page(PAGE_ABOUT)
##########################################
def on_qr_types_view_selection_changed(self, widget):
if len(widget.get_selected_items()) > 0:
GObject.idle_add(self.on_qr_types_view_item_activated,
widget, widget.get_selected_items()[0])
def on_qr_types_view_item_activated(self, widget, item):
'''Loads the UI for the appropriate QR type'''
model = widget.get_model()
qr_id = model[item][COL_ID]
self.switch_qrcode_view(qr_id)
def switch_qrcode_view(self, qr_id):
self.qr_types[qr_id].widget.on_activated()
def switch_callback(page):
self.ui.notebook1.set_current_page(page)
return False # stop this callback from being called again
GObject.timeout_add(self.iconview_activation_delay, switch_callback,
PAGE_QR)
for child in self.ui.qr_input_box.get_children():
child.hide()
self.ui.qr_input_box.get_children()[qr_id].show()
##########################################
def get_pixbuf_from_drawing_area(self):
window = self.ui.qr_drawingarea.get_window()
src_x, src_y = self.get_centered_coordinates(self.ui.qr_drawingarea,
self.surface)
image_height = self.surface.get_height()
image_width = self.surface.get_width()
return Gdk.pixbuf_get_from_window(window, src_x, src_y,
image_width, image_height)
##########################################
def on_toolbuttonSave_clicked(self, widget, data=None):
if not self.surface:
return
dialog = Gtk.FileChooserDialog(_("Please choose a file"), self,
Gtk.FileChooserAction.SAVE,
(Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL,
Gtk.STOCK_SAVE, Gtk.ResponseType.OK))
filter_png = Gtk.FileFilter()
filter_png.set_name(_("PNG images"))
filter_png.add_mime_type("image/png")
dialog.add_filter(filter_png)
response = dialog.run()
if response == Gtk.ResponseType.OK:
# We cannot write directly from the surface, as the
# Surface.write_to_png() method writes the image in the original
# size returned by qrencode (i.e. non-scaled), and the
# SurfacePattern does not have any methods to write to disk.
# So we read the contents from the Gtk.DrawingArea, put them into
# a Gdk.Pixbuf and use its 'savev' method to write to disk.
pixbuf = self.get_pixbuf_from_drawing_area()
pixbuf.savev(dialog.get_filename(), 'png', [], [])
dialog.destroy()
def on_toolbuttonCopy_clicked(self, widget, data=None):
if not self.surface:
return
pixbuf = self.get_pixbuf_from_drawing_area()
self.clipboard.set_image(pixbuf)
##########################################
def update_qr_code(self, text):
self.qr_code_placeholder = text
self.ui.qr_drawingarea.queue_draw()
def get_centered_coordinates(self, drawing_area, surface):
drawing_area_height = drawing_area.get_allocated_height()
drawing_area_width = drawing_area.get_allocated_width()
image_height = surface.get_height()
image_width = surface.get_width()
return (drawing_area_width / 2 - image_width / 2,
drawing_area_height / 2 - image_height / 2)
def on_qr_drawingarea_draw(self, widget, ctx, data=None):
text = self.qr_code_placeholder
## Fill the background
# Create a rounded rectanble
x = 0.0
y = 0.0
width = widget.get_allocated_width()
height = widget.get_allocated_height()
aspect = 1.0
corner_radius = height / 50.0
radius = corner_radius / aspect
degrees = math.pi / 180.0
ctx.new_sub_path()
ctx.arc(x + width - radius, y + radius, radius, -90 * degrees,
0 * degrees)
ctx.arc(x + width - radius, y + height - radius, radius, 0 * degrees,
90 * degrees)
ctx.arc(x + radius, y + height - radius, radius, 90 * degrees,
180 * degrees)
ctx.arc(x + radius, y + radius, radius, 180 * degrees, 270 * degrees)
ctx.close_path()
# Fill the rounded rectangle with a linear gradient
lg = cairo.LinearGradient(0.0, 0.0, 500.0, 500.0)
lg.add_color_stop_rgba(0, 0.27, 0.27, 0.27, 1) # 1,1,1 is white
lg.add_color_stop_rgba(1, 0.22, 0.22, 0.22, 1)
ctx.set_source(lg)
ctx.fill()
# Load a texture and overlay it to the gradient, tiled
pattern = cairo.SurfacePattern(self.texture)
pattern.set_extend(cairo.EXTEND_REPEAT)
ctx.set_source(pattern)
ctx.paint()
if not text == '':
## Create the QR code
qr_code = QRCode() # output_size = int(min(width, height) * 0.9))
self.surface = qr_code.encode(text, QRCodeOutput.CAIRO_SURFACE)
# Center the image in the drawing area
centered_width, centered_height = \
self.get_centered_coordinates(widget, self.surface)
ctx.translate(centered_width, centered_height)
# Set the surface as the context's source
ctx.set_source_surface(self.surface)
# Render the image
ctx.paint()
|