/
xkcd-color-browser.py
executable file
·169 lines (134 loc) · 5.91 KB
/
xkcd-color-browser.py
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
#!/usr/bin/env python3
# Shaji Khan [djshaji@gmail.com]
# 2016-08-09
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY 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, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301, USA.
#
from gi.repository import Gtk, Gdk, GdkPixbuf, GObject, GLib, Pango
import json, sys
class XKCDBrowser (Gtk.Window):
icon_theme = Gtk.IconTheme.get_default ()
ICON_SIZE = 128
shortcuts = {
Gdk.KEY_F10: lambda self: self.unmaximize () if self.is_maximized () else self.maximize (),
Gdk.KEY_F5: lambda self: self.reload (),
Gdk.KEY_Delete: lambda self: self.delete (),
Gdk.KEY_Escape: lambda self: self.destroy ()
}
def new_pixbuf (self, size):
# real ugly way to get an empty pixbuf
return self.icon_theme.load_icon (self.icon_theme.get_example_icon_name (), size, Gtk.IconLookupFlags.FORCE_SIZE)
def generate_icon (self, color, size = -1):
if size == -1:
size = self.ICON_SIZE
pixbuf = self.new_pixbuf (size)
pixbuf.fill (int (color [1:] + 'ff', base = 16))
return pixbuf.copy ()
def fill_store_thread (self, mythread, thread_number):
list_ = mythread.args ['list']
len_ = len (list_)
store = []
for x in range (thread_number * int (len_ / mythread.threads),
(thread_number + 1) * int (len_ / mythread.threads)):
store.append ([self.generate_icon (list_ [x][1]).copy (), list_ [x][0]])
mythread.queues [thread_number].put (store)
def item_activated (ui, self, path):
it = self.ui.store.get_iter (path)
name = self.ui.store.get (it, 1) [0]
p = Gtk.Popover.new (ui.toolbar)
image = Gtk.Image ()
image.set_from_pixbuf (ui.generate_icon (ui.xkcd_names [name], size = 400))
box = Gtk.VBox ()
box.pack_start (image, 1, 1, 0)
label = Gtk.Label (name + '\n' + ui.xkcd_names [name])
label.set_justify (2)
box.pack_start (label, 0, 0, 0)
p.add (box)
p.show_all ()
def fill_store (self):
for name in self.xkcd_names:
pixbuf = self.generate_icon (self.xkcd_names [name])
self.store.append ([pixbuf, name])
def hotkeys (self, window, event):
if event.keyval in self.shortcuts:
self.shortcuts [event.keyval] (self)
return True
def build_ui (self):
#self.header = Gtk.HeaderBar ()
self.set_title ('XKCD Color Survey Browser')
#self.header.set_subtitle ('xkcd.com/color/rgb')
#self.header.set_show_close_button (True)
#self.set_titlebar (self.header)
#self.set_decorated (False)
self.iconview = Gtk.IconView ()
self.iconview.ui = self
self.iconview.sw = Gtk.ScrolledWindow ()
self.iconview.sw.add (self.iconview)
self.master = Gtk.HBox ()
self.add (self.master)
self.toolbar = Gtk.Toolbar ()
self.box = Gtk.VBox ()
self.master.pack_start (self.box, 1, 1, 0)
#self.box.pack_start (self.header, 0, 0, 0)
self.box.pack_start (self.iconview.sw, 1, 1, 0)
self.box.pack_end (self.toolbar, 0, 1, 0)
self.store = Gtk.ListStore (GdkPixbuf.Pixbuf, str)
self.store.set_sort_column_id (1, 0)
self.fill_store ()
self.iconview.connect ('item-activated', self.item_activated)
self.iconview.set_model (self.store)
self.iconview.set_item_width (96)
self.iconview.set_pixbuf_column (0)
self.iconview.set_text_column (1)
self.connect ('key-press-event', self.hotkeys)
self.connect ('destroy', self.main_quit)
self.iconview.grab_focus ()
pango_font = Pango.FontDescription.from_string ("Ubuntu 27")
self.modify_font (pango_font)
self.set_size_request (800, 500)
self.show_all ()
def main (self):
try:
self.mainloop.run ()
except KeyboardInterrupt:
print ("Keyboard Interrupt")
self.main_quit ()
def __init__ (self, filename = None):
try:
if filename is None:
self.xkcd_names = json.loads (open ('xkcd-colors.json').read ())
else:
self.xkcd_names = json.loads (open (filename).read ())
except FileNotFoundError:
message = 'Download xkcd colors json file from <b>here</b>:\n\nhttp://<b>github.com/charlesreid1/xkcd-colors</b>/\nblob/master/xkcd-colors.json\n\nor http://<b><span size=\'x-large\'>xkcd.com</span></b>/color/rgb for more info\n\nusage:\n<b>{}</b> <i>xkcd_colors_json_file</i>'.format (sys.argv [0])
#print (message)
dialog = Gtk.MessageDialog (buttons = Gtk.ButtonsType.OK, message_format = message)
dialog.set_markup (message)
dialog.run ()
dialog.destroy ()
sys.exit ()
Gtk.Window.__init__ (self)
self.mainloop = GLib.MainLoop()
self.build_ui ()
def main_quit (self, *args):
self.mainloop.quit ()
def xkcd_browser (filename = None):
b = XKCDBrowser (filename)
b.main ()
if __name__ == '__main__':
if len (sys.argv) > 1:
xkcd_browser (sys.argv [1])
else:
xkcd_browser ()