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
|
# -*- Mode: Python; coding: utf-8; indent-tabs-mode: nil; tab-width: 4 -*-
### BEGIN LICENSE
# This file is in the public domain
### END LICENSE
import gettext
from gettext import gettext as _
gettext.textdomain('qreator')
import gi
gi.require_version('Gdk', '3.0')
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, Gdk, GLib # pylint: disable=E0611
import cairo
import logging
logger = logging.getLogger('qreator')
from qreator_lib import Window
from qreator.AboutQreatorDialog import AboutQreatorDialog
from qreator.PreferencesQreatorDialog import PreferencesQreatorDialog
import qrencode
import array
import Image
COL_TEXT = 0
COL_PIXBUF = 1
COL_ID = 2
PAGE_HOME = 0
PAGE_HISTORY = 1
PAGE_SETTINGS = 2
PAGE_ABOUT = 3
PAGE_URL = 4
# 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)
self.AboutDialog = AboutQreatorDialog
self.PreferencesDialog = PreferencesQreatorDialog
# Code for other initialization actions should be added here.
self.clipboard = Gtk.Clipboard.get(Gdk.SELECTION_CLIPBOARD)
self.scale_factor = 8
self.surface = None
self.ui.iconview1.set_text_column(COL_TEXT)
self.ui.iconview1.set_pixbuf_column(COL_PIXBUF)
self.ui.notebook1.set_show_tabs(False)
self.ui.entry1.connect('changed', self.on_entry1_changed)
## alpha is starting transparency
self.alpha = 0.25
## delta is amount to increase alpha
self.delta = 0.01
def on_toolbuttonHome_clicked(self, widget, data=None):
self.ui.notebook1.set_current_page(PAGE_HOME)
def on_toolbuttonHistory_clicked(self, widget, data=None):
self.ui.notebook1.set_current_page(PAGE_HISTORY)
def on_toolbuttonSettings_clicked(self, widget, data=None):
self.ui.notebook1.set_current_page(PAGE_SETTINGS)
def on_toolbuttonAbout_clicked(self, widget, data=None):
self.ui.notebook1.set_current_page(PAGE_ABOUT)
def on_iconview1_item_activated(self, widget, item):
self.ui.notebook1.set_current_page(PAGE_URL)
#model = widget.get_model()
#id = model[item][COL_ID]
def get_pixbuf_from_drawing_area(self):
window = self.ui.drawingarea1.get_window()
src_x, src_y = self.get_centered_coordinates(self.ui.drawingarea1,
self.surface)
image_height = self.surface.get_height() * self.scale_factor
image_width = self.surface.get_width() * self.scale_factor
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 on_entry1_changed(self, widget, data=None):
self.alpha = 0.25
self.ui.drawingarea1.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 fadeImage(self):
self.ui.drawingarea1.queue_draw()
def on_drawingarea1_draw(self, widget, ctx, data=None):
text = self.ui.entry1.get_text()
if text == '':
return
version, size, im = qrencode.encode(text)
# --------------- Cairo surface conversion ---------------------------
# See http://cairographics.org/pythoncairopil/ and
# http://mail.python.org/pipermail/image-sig/2005-October/003604.html
# http://mail.gnome.org/archives/gtkmm-list/2007-May/msg00111.html
# http://pygame.org/wiki/CairoPygame
# Other:
# http://stackoverflow.com/questions/8761887/scrolling-gtkdrawingarea
# http://docs.clutter-project.org/docs/clutter-cookbook/1.0/textures-crossfade.html
im = im.resize((168, 168),
Image.NEAREST)
#im = im.resize((self.scale_factor * size, self.scale_factor * size),
# Image.NEAREST)
im = im.convert('RGBA')
bytearr = array.array('B', im.tostring())
height, width = im.size
self.surface = cairo.ImageSurface.create_for_data(bytearr,
cairo.FORMAT_ARGB32,
width, height,
width * 4)
# Center the image
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()
ctx.paint_with_alpha(self.alpha)
self.alpha += self.delta
if self.alpha >= 1:
return False
else:
GLib.timeout_add(25, self.fadeImage)
|