1
1
# -*- Mode: Python; coding: utf-8; indent-tabs-mode: nil; tab-width: 4 -*-
3
3
# Copyright (C) 2012 David Planella <david.planella@ubuntu.com>
4
# This program is free software: you can redistribute it and/or modify it
5
# under the terms of the GNU General Public License version 3, as published
4
# This program is free software: you can redistribute it and/or modify it
5
# under the terms of the GNU General Public License version 3, as published
6
6
# by the Free Software Foundation.
8
# This program is distributed in the hope that it will be useful, but
9
# WITHOUT ANY WARRANTY; without even the implied warranties of
10
# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
8
# This program is distributed in the hope that it will be useful, but
9
# WITHOUT ANY WARRANTY; without even the implied warranties of
10
# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
11
11
# PURPOSE. See the GNU General Public License for more details.
13
# You should have received a copy of the GNU General Public License along
13
# You should have received a copy of the GNU General Public License along
14
14
# with this program. If not, see <http://www.gnu.org/licenses/>.
79
79
# after having clicked once on an iconview icon
80
80
self.iconview_activation_delay = 200
82
# Initialize color editing
83
self.current_color_bg = Gdk.Color(65535, 65535, 65535) # White
84
self.previous_color_bg = Gdk.Color(65535, 65535, 65535)
85
self.current_color_fg = Gdk.Color(0, 0, 0) # Black
86
self.previous_color_fg = Gdk.Color(0, 0, 0)
82
88
# Add an initial text, so that there is an initial QR code
83
89
self.qr_code_placeholder = 'http://launchpad.net/qreator'
294
300
pixbuf = self.get_pixbuf_from_drawing_area()
295
301
self.clipboard.set_image(pixbuf)
303
def on_toolbuttonEdit_clicked(self, widget, data=None):
304
if widget.get_active():
305
self.ui.toolbar2.show()
307
self.ui.toolbar2.hide()
309
##########################################
311
def set_selector_colors(self):
312
'''Preloads the color selector with the current and previous
314
colorsel = self.ui.color_selection_dialog.get_color_selection()
315
if self.ui.radiobuttonfg.get_active():
316
colorsel.set_current_color(self.current_color_fg)
317
colorsel.set_previous_color(self.previous_color_fg)
318
elif self.ui.radiobuttonbg.get_active():
319
colorsel.set_current_color(self.current_color_bg)
320
colorsel.set_previous_color(self.previous_color_bg)
322
def on_toolbuttonColorSelector_clicked(self, widget, data=None):
323
'''Loads the color selector dialog and saves the chosen
324
current and previous QR code colors'''
325
colorsel = self.ui.color_selection_dialog.get_color_selection()
327
self.set_selector_colors()
329
colordiag = self.ui.color_selection_dialog
330
response = colordiag.run()
332
# It seems that when invoked from Glade, the color selector dialog
333
# returns an integer instead of Gtk.ResponseType.OK, and we need to
334
# set this integer explicitly in the .ui file. The 'OK' button
337
if self.ui.radiobuttonfg.get_active():
338
self.current_color_fg = colorsel.get_current_color()
339
self.previous_color_fg = colorsel.get_previous_color()
340
elif self.ui.radiobuttonbg.get_active():
341
self.current_color_bg = colorsel.get_current_color()
342
self.previous_color_bg = colorsel.get_previous_color()
344
self.update_qr_code(self.qr_code_placeholder)
346
self.ui.color_selection_dialog.hide()
348
def on_radiobuttonfg_toggled(self, widget, data=None):
349
'''Loads the foreground color in the selector when the foreground
350
radio button is toggled'''
351
self.set_selector_colors()
353
def on_radiobuttonbg_toggled(self, widget, data=None):
354
'''Loads the foreground color in the selector when the background
355
radio button is toggled'''
356
self.set_selector_colors()
358
def on_toolbuttonColorSwap_clicked(self, widget, data=None):
359
'''Swaps the QR code's foreground and background colors'''
361
self.previous_color_bg = self.current_color_bg
362
self.previous_color_fg = self.current_color_fg
364
self.current_color_bg = self.current_color_fg
365
self.current_color_fg = self.previous_color_bg
367
self.update_qr_code(self.qr_code_placeholder)
369
def on_toolbuttonColorReset_clicked(self, widget, data=None):
370
'''Reset QR code to defaults. At this point only the colors
371
can be edited, so effectively it resets the foreground and
372
background colors to black and white, respectively'''
373
self.current_color_bg = Gdk.Color(65535, 65535, 65535) # White
374
self.previous_color_bg = Gdk.Color(65535, 65535, 65535)
375
self.current_color_fg = Gdk.Color(0, 0, 0) # Black
376
self.previous_color_fg = Gdk.Color(0, 0, 0)
378
self.update_qr_code(self.qr_code_placeholder)
297
380
##########################################
299
382
def update_qr_code(self, text):
353
436
if not text == '':
354
437
## Create the QR code
355
438
qr_code = QRCode() # output_size = int(min(width, height) * 0.9))
356
self.surface = qr_code.encode(text, QRCodeOutput.CAIRO_SURFACE)
440
# Note that we need to convert the 16-bit RGB values from
441
# Gdk.Color to the 8-bit RGB values that PIL expects
442
self.surface = qr_code.encode(text,
444
self.current_color_fg.red / 256,
445
self.current_color_fg.green / 256,
446
self.current_color_fg.blue / 256),
448
self.previous_color_fg.red / 256,
449
self.previous_color_fg.green / 256,
450
self.previous_color_fg.blue / 256),
452
self.current_color_bg.red / 256,
453
self.current_color_bg.green / 256,
454
self.current_color_bg.blue / 256),
456
self.previous_color_bg.red / 256,
457
self.previous_color_bg.green / 256,
458
self.previous_color_bg.blue / 256),
459
output_type=QRCodeOutput.CAIRO_SURFACE)
358
461
# Center the image in the drawing area
359
462
centered_width, centered_height = \