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
|
# -*- 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 cairo
import math
from gi.repository import Gtk, Gdk, GdkPixbuf # 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
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"))
# Add an initial text, so that there is an initial QR code
self.qr_code_placeholder = 'http://launchpad.net/qreator'
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)
from qrcodes.QRCodeText import QRCodeText
from qrcodes.QRCodeURL import QRCodeURL
from qrcodes.QRCodeLocation import QRCodeLocation
from qrcodes.QRCodeWifi import QRCodeWifi
from qrcodes.QRCodeSoftwareCenterApp import QRCodeSoftwareCenterApp
self.qr_types = [
QRCodeURL(self.update_qr_code, 'url.png', _('URL'), 0),
QRCodeText(self.update_qr_code, 'text.png', _('Text'), 1),
QRCodeLocation(self.update_qr_code, 'location.png',
_('Geolocation'), 2),
QRCodeWifi(self.update_qr_code, 'wifi.png', _('Wifi network'), 3),
QRCodeSoftwareCenterApp(self.update_qr_code, 'softwarecentre.png',
_('Ubuntu Software Center app'), 4),
]
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:
self.ui.qr_input_notebook.append_page(qr_type.widget.grid, None)
def fill_qr_types_store(self):
self.qr_types_store.clear()
for qr_type in self.qr_types:
icon = GdkPixbuf.Pixbuf.new_from_file(get_media_file(
qr_type.icon_path))
self.qr_types_store.append([qr_type.description,
icon,
qr_type.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")
about.set_version('12.05.6')
about.set_authors([
'David Planella <david.planella@ubuntu.com>',
'Michael Hall <mhall119@ubuntu.com>',
'Andrew Starr-Bochicchio <andrewsomething@ubuntu.com >',
])
about.set_license(_('Distributed under the GPL v3 license.') +
'\nhttp://www.opensource.org/licenses/gpl-3.0.html')
about.set_translator_credits(_("translator-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.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_item_activated(self, widget, item):
'''Loads the UI for the appropriate QR type'''
model = widget.get_model()
qr_id = model[item][COL_ID]
self.qr_types[qr_id].widget.on_activated()
self.ui.notebook1.set_current_page(PAGE_QR)
self.ui.qr_input_notebook.set_current_page(qr_id)
##########################################
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()
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()
|