-
Notifications
You must be signed in to change notification settings - Fork 2
/
icondialog.py
242 lines (197 loc) · 9.37 KB
/
icondialog.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
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright (C) 2013 Ignacio Rodríguez <[email protected]>
#
# 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.
import shutil
from gi.repository import Gtk
from gi.repository import Gdk
from gi.repository import GdkPixbuf
from jarabe.journal.model import get_documents_path
from sugar3.graphics.toolbutton import ToolButton
from sugar3.graphics.toolbarbox import ToolbarBox
from sugar3.graphics import style
from gettext import gettext as _
import os
SUGAR_ARTWORK = [_('Actions'), _('Emblems'), _('Documents')]
def get_document_icons():
icons = os.listdir(get_documents_path())
icons_ = []
for icon in icons:
if icon.endswith('.svg'):
icons_.append(icon[:-4])
return icons_
SUGAR_ICONS = {
_('Actions'): ['media-playlist-repeat-insensitive',
'media-playlist-shuffle-insensitive',
'format-justify-left',
'cell-height',
'media-playback-stop-insensitive',
'select-all', 'format-columns-triple', 'column-insert',
'go-right', 'cell-format', 'format-justify-right', 'row-insert',
'entry-search', 'invite', 'format-text-underline', 'entry-stop',
'view-return', 'transfer-from-text-uri-list', 'cell-size', 'column-remove',
'insert-image', 'edit-clear', 'view-radial', 'view-lastedit',
'media-seek-forward-insensitive', 'row-remove', 'zoom-home',
'zoom-best-fit', 'media-playlist-repeat', 'media-eject-insensitive',
'view-fullscreen', 'format-text-leading', 'transfer-from-text-x-generic',
'select-none', 'toolbar-view', 'media-playback-pause', 'format-text-bold',
'media-playback-start-insensitive', 'go-home', 'view-freeform', 'go-next',
'transfer-from-image-x-generic', 'media-seek-backward', 'list-add',
'edit-description', 'toolbar-colors', 'cell-width',
'transfer-from-audio-x-generic', 'zoom-in', 'zoom-groups',
'media-seek-forward', 'go-up', 'view-list', 'format-justify-center',
'transfer-from', 'media-playback-pause-insensitive', 'media-playback-stop',
'go-previous', 'go-left', 'transfer-from-video-x-generic',
'media-playlist-shuffle', 'zoom-out', 'toolbar-edit', 'go-next-paired',
'system-logout', 'view-source', 'tray-hide', 'edit-copy', 'insert-table',
'view-size', 'format-justify-fill', 'go-down', 'format-columns-single',
'transfer-to-text-uri-list', 'activity-stop',
'transfer-to-audio-x-generic', 'view-box', 'zoom-original',
'edit-undo', 'document-send', 'view-refresh',
'document-save', 'system-shutdown', 'entry-refresh', 'dialog-cancel',
'system-search', 'transfer-to-image-x-generic',
'transfer-from-application-octet-stream',
'media-seek-backward-insensitive', 'dialog-ok', 'edit-redo',
'view-created', 'activity-start', 'format-text-size', 'view-triangle',
'entry-cancel', 'media-eject', 'edit-paste', 'tray-show',
'transfer-to-video-x-generic', 'transfer-to', 'view-details',
'system-restart', 'zoom-activity', 'media-record',
'transfer-to-text-x-generic', 'zoom-to-width', 'format-columns-double',
'format-text-italic', 'tray-favourite', 'list-remove',
'transfer-to-application-octet-stream', 'view-spiral',
'media-record-insensitive', 'edit-delete', 'toolbar-help',
'edit-duplicate', 'media-playback-start', 'zoom-neighborhood',
'go-previous-paired'],
_('Emblems'): ['emblem-busy', 'emblem-charging', 'emblem-downloads',
'emblem-favorite', 'emblem-locked', 'emblem-notification',
'emblem-outofrange', 'emblem-question', 'emblem-view-source',
'emblem-warning'],
_('Documents'): get_document_icons()
}
class IconDialog(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self)
self.ensure_sugar_structure()
self.theme = Gtk.IconTheme.get_default()
self.theme.append_search_path(get_documents_path())
self._icon = None
grid = Gtk.Grid()
self.x, self.y = (Gdk.Screen.width() / 1.5, Gdk.Screen.height() / 1.5)
self.set_size_request(self.x, self.y)
self.icons = None
toolbox = self.build_toolbar()
self.icons = self.build_scroll()
grid.attach(toolbox, 0, 1, 1, 1)
grid.attach(self.icons, 0, 2, 1, 1)
self.set_decorated(False)
self.set_skip_pager_hint(True)
self.set_skip_taskbar_hint(True)
self.set_position(Gtk.WindowPosition.CENTER_ALWAYS)
self.set_resizable(False)
self.set_modal(True)
self.add(grid)
self.show_all()
def ensure_sugar_structure(self):
user = os.path.expanduser("~")
icons = os.path.join(user, ".icons")
sugar = os.path.join(icons, "sugar")
scalable = os.path.join(sugar, "scalable")
emblems = os.path.join(scalable, "emblems")
self.end_path = emblems
sugar_theme = os.path.join(sugar, "index.theme")
theme_data = "[Icon Theme]\nName=Sugar\nComment=User-defined" \
" Sugar theme\n\nDirectories=scalable/device\n\n[scalable" \
"/device]\nSize=55\nMinSize=32\nMaxSize=256\n" \
"Context=Device\nType=Scalable"
if not os.path.exists(icons):
os.mkdir(icons)
if not os.path.exists(sugar):
os.mkdir(sugar)
if not os.path.exists(sugar_theme):
f = open(sugar_theme, 'w')
f.write(theme_data)
f.close()
if not os.path.exists(scalable):
os.mkdir(scalable)
if not os.path.exists(emblems):
os.mkdir(emblems)
def build_toolbar(self):
toolbox = ToolbarBox()
label = Gtk.Label("\t" + _('Select an icon'))
label.modify_fg(Gtk.StateType.NORMAL,
Gdk.color_parse('white'))
item = Gtk.ToolItem()
item.add(label)
close = ToolButton('entry-cancel')
close.connect('clicked', lambda x: self.destroy())
separator = Gtk.SeparatorToolItem()
separator.props.draw = False
separator.set_expand(True)
toolbox.toolbar.insert(item, -1)
toolbox.toolbar.insert(separator, -1)
toolbox.toolbar.insert(close, -1)
return toolbox
def get_icon(self):
return self._icon
def build_scroll(self):
scroll = Gtk.ScrolledWindow()
scroll.set_policy(Gtk.PolicyType.NEVER, Gtk.PolicyType.AUTOMATIC)
grid = Gtk.Grid()
current = 0
for icon in SUGAR_ARTWORK:
expander = self.build_icons(icon)
grid.attach(expander, 0, current, 1, 1)
current += 1
scroll.set_size_request(self.x, self.y)
scroll.add_with_viewport(grid)
return scroll
def build_icons(self, category):
icons = SUGAR_ICONS[category]
if len(icons) < 1:
return Gtk.EventBox()
store = Gtk.ListStore(GdkPixbuf.Pixbuf, str, str)
icon_view = Gtk.IconView.new_with_model(store)
icon_view.set_selection_mode(Gtk.SelectionMode.SINGLE)
icon_view.connect('selection-changed', self.set_icon, store)
icon_view.set_pixbuf_column(0)
icon_view.modify_bg(Gtk.StateType.NORMAL, Gdk.color_parse('#D5D5D5'))
for icon in icons:
info = self.theme.lookup_icon(icon, 55,
Gtk.IconLookupFlags.FORCE_SVG)
icon_path = info.get_filename()
if category == _('Documents'):
icon_path = os.path.join(get_documents_path(), icon + ".svg")
pixbuf = GdkPixbuf.Pixbuf.new_from_file_at_size(
icon_path, 55, 55)
store.insert(-1, [pixbuf, icon, icon_path])
expand = Gtk.Expander()
expand.set_label(category)
expand.add(icon_view)
return expand
def set_icon(self, widget, model):
try:
iter_ = model.get_iter(widget.get_selected_items()[0])
except:
return
icon_name = model.get(iter_, 1)[0]
icon_path = model.get(iter_, 2)[0]
self._icon = icon_name
if icon_path.startswith(get_documents_path()):
destination = os.path.join(self.end_path, icon_name + ".svg")
shutil.copy(icon_path, destination)
self.destroy()
def get_icon(self):
return self._icon