forked from sugarlabs/paint-activity
/
dialogs.py
254 lines (205 loc) · 8.11 KB
/
dialogs.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
243
244
245
246
247
248
249
250
251
252
253
254
#
# 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 St, Fifth Floor, Boston, MA 02110-1301 USA
from gettext import gettext as _
import os
import glob
from gi.repository import GObject
from gi.repository import Gtk
from gi.repository import Gdk
from gi.repository import GdkPixbuf
from sugar3.datastore import datastore
from sugar3.graphics import style
from sugar3.graphics.toolbutton import ToolButton
from sugar3.graphics.icon import Icon
from sugar3.graphics.objectchooser import ObjectChooser
try:
from sugar3.graphics.objectchooser import FILTER_TYPE_GENERIC_MIME
except:
FILTER_TYPE_GENERIC_MIME = 'generic_mime'
STORE = None
JOURNAL_IMAGES = []
class _DialogWindow(Gtk.Window):
# A base class for a modal dialog window.
def __init__(self, icon_name, title):
super(_DialogWindow, self).__init__()
self.set_border_width(style.LINE_WIDTH)
width = Gdk.Screen.width() - style.GRID_CELL_SIZE * 2
height = Gdk.Screen.height() - style.GRID_CELL_SIZE * 2
self.set_size_request(width, height)
self.set_position(Gtk.WindowPosition.CENTER_ALWAYS)
self.set_decorated(False)
self.set_resizable(False)
self.set_modal(True)
vbox = Gtk.VBox()
self.add(vbox)
toolbar = _DialogToolbar(icon_name, title)
toolbar.connect('stop-clicked', self._stop_clicked_cb)
vbox.pack_start(toolbar, False, False, 0)
self.content_vbox = Gtk.VBox()
self.content_vbox.set_border_width(style.DEFAULT_SPACING)
vbox.add(self.content_vbox)
self.connect('realize', self._realize_cb)
def _stop_clicked_cb(self, source):
self.destroy()
def _realize_cb(self, source):
self.set_type_hint(Gdk.WindowTypeHint.DIALOG)
self.get_window().set_accept_focus(True)
class _DialogToolbar(Gtk.Toolbar):
# Displays a dialog window's toolbar, with title, icon, and close box.
__gsignals__ = {
'stop-clicked': (GObject.SignalFlags.RUN_LAST, None, ()),
}
def __init__(self, icon_name, title):
super(_DialogToolbar, self).__init__()
if icon_name is not None:
icon = Icon()
icon.set_from_icon_name(icon_name, Gtk.IconSize.LARGE_TOOLBAR)
self._add_widget(icon)
self._add_separator()
label = Gtk.Label(label=title)
self._add_widget(label)
self._add_separator(expand=True)
stop = ToolButton(icon_name='dialog-cancel')
stop.set_tooltip(_('Done'))
stop.connect('clicked', self._stop_clicked_cb)
self.add(stop)
def _add_separator(self, expand=False):
separator = Gtk.SeparatorToolItem()
separator.set_expand(expand)
separator.set_draw(False)
self.add(separator)
def _add_widget(self, widget):
tool_item = Gtk.ToolItem()
tool_item.add(widget)
self.add(tool_item)
def _stop_clicked_cb(self, button):
self.emit('stop-clicked')
class TuxStampDialog(_DialogWindow):
__gsignals__ = {
'stamp-selected': (
GObject.SIGNAL_RUN_FIRST,
GObject.TYPE_NONE,
(GObject.TYPE_STRING,
))}
def __init__(self, activity):
super(TuxStampDialog, self).__init__('tool-stamp', _('Select stamp'))
self._activity = activity
global JOURNAL_IMAGES
if not JOURNAL_IMAGES:
JOURNAL_IMAGES = self._activity._journal_images
global STORE
if not STORE:
STORE = self._create_model()
self._iconview = Gtk.IconView()
self._iconview.set_selection_mode(Gtk.SelectionMode.SINGLE)
self._iconview.set_model(STORE)
self._iconview.set_pixbuf_column(0)
self._iconview.connect('selection-changed', self._stamp_changed)
scrollwin = Gtk.ScrolledWindow()
scrollwin.set_policy(Gtk.PolicyType.NEVER, Gtk.PolicyType.AUTOMATIC)
self.content_vbox.pack_start(scrollwin, True, True, 0)
scrollwin.add(self._iconview)
scrollwin.show_all()
def _stamp_changed(self, widget):
items = widget.get_selected_items()
if not items:
return
iter_ = STORE.get_iter(items[0])
filepath = STORE.get(iter_, 1)[0]
if filepath == 'loadfromjournal':
self.hide()
try:
chooser = ObjectChooser(self._activity, what_filter='Image',
filter_type=FILTER_TYPE_GENERIC_MIME,
show_preview=True)
except:
# for compatibility with older versions
chooser = ObjectChooser(self._activity, what_filter='Image')
try:
result = chooser.run()
newfilepath = None
if result == Gtk.ResponseType.ACCEPT:
jobject = chooser.get_selected_object()
if jobject and jobject.file_path:
newfilepath = jobject.file_path
object_id = str(jobject.object_id)
if object_id not in JOURNAL_IMAGES:
JOURNAL_IMAGES.append(object_id)
pixbuf = GdkPixbuf.Pixbuf.new_from_file_at_size(
newfilepath,
50,
50)
STORE.append([pixbuf, newfilepath])
finally:
if not newfilepath:
self.show_all()
else:
self.emit('stamp-selected', newfilepath)
self.destroy()
chooser.destroy()
del chooser
else:
self.emit('stamp-selected', filepath)
self.destroy()
def _create_model(self):
tuxstamps = '/usr/share/tuxpaint/stamps'
store = Gtk.ListStore(GdkPixbuf.Pixbuf, str)
tuxavailable = True
if not os.path.exists(tuxstamps):
tuxavailable = False
filepaths = []
for object_id in JOURNAL_IMAGES:
if os.path.exists(object_id) and os.path.isfile(object_id):
filepaths.append(object_id)
continue
try:
obj = datastore.get(object_id)
fpath = obj.file_path
if os.path.exists(fpath):
filepaths.append(fpath)
except Exception:
pass
pixbuf = GdkPixbuf.Pixbuf.new_from_file_at_size(
'icons/loadfromjournal.svg',
50,
50)
store.append([pixbuf, 'loadfromjournal'])
for f in filepaths:
pixbuf = GdkPixbuf.Pixbuf.new_from_file_at_size(
f,
50,
50)
store.append([pixbuf, f])
if tuxavailable:
categories = []
dirs = os.listdir(tuxstamps)
for d in dirs:
d = os.path.join(tuxstamps, d)
if os.path.isdir(d):
categories.append(d)
for cat in categories:
patron = os.path.join(tuxstamps, cat)
for x in range(5):
patron = patron + "/*"
files = glob.iglob(patron + '.png')
for f in files:
pixbuf = GdkPixbuf.Pixbuf.new_from_file_at_size(
f,
50,
50)
store.append([pixbuf, f])
return store
def get_journal_images():
return JOURNAL_IMAGES