-
Notifications
You must be signed in to change notification settings - Fork 385
/
quickchoice.py
354 lines (283 loc) · 12.1 KB
/
quickchoice.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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
# This file is part of MyPaint.
# Copyright (C) 2013-2018 by the MyPaint Development Team.
#
# 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.
"""Widgets and popup dialogs for making quick choices"""
## Imports
from __future__ import division, print_function
import abc
from lib.gibindings import Gtk
from .pixbuflist import PixbufList
from . import brushmanager
from . import brushselectionwindow
from . import widgets
from . import spinbox
from . import windowing
from lib.observable import event
import gui.colortools
from lib.pycompat import add_metaclass
## Module consts
_DEFAULT_PREFS_ID = u"default"
## Interfaces
@add_metaclass(abc.ABCMeta)
class Advanceable:
"""Interface for choosers which can be advanced by pressing keys.
Advancing happens if the chooser is already visible and its key is
pressed again. This can happen repeatedly. The actual action
performed is up to the implementation: advancing some some choosers
may move them forward through pages of alternatives, while other
choosers may actually change a brush setting as they advance.
"""
@abc.abstractmethod
def advance(self):
"""Advances the chooser to the next page or choice.
Choosers should remain open when their advance() method is
invoked. The actual action performed is up to the concrete
implementation: see the class docs.
"""
## Class defs
class QuickBrushChooser (Gtk.VBox):
"""A quick chooser widget for brushes"""
## Class constants
_PREFS_KEY_TEMPLATE = u"brush_chooser.%s.selected_group"
ICON_SIZE = 48
## Method defs
def __init__(self, app, prefs_id=_DEFAULT_PREFS_ID):
"""Initialize"""
Gtk.VBox.__init__(self)
self.app = app
self.bm = app.brushmanager
self._prefs_key = self._PREFS_KEY_TEMPLATE % (prefs_id,)
active_group_name = app.preferences.get(self._prefs_key, None)
model = self._make_groups_sb_model()
self.groups_sb = spinbox.ItemSpinBox(model, self._groups_sb_changed_cb,
active_group_name)
active_group_name = self.groups_sb.get_value()
brushes = self.bm.get_group_brushes(active_group_name)
self.brushlist = PixbufList(
brushes, self.ICON_SIZE, self.ICON_SIZE,
namefunc=brushselectionwindow.managedbrush_namefunc,
pixbuffunc=brushselectionwindow.managedbrush_pixbuffunc,
idfunc=brushselectionwindow.managedbrush_idfunc
)
self.brushlist.dragging_allowed = False
self.bm.groups_changed += self._groups_changed_cb
self.bm.brushes_changed += self._brushes_changed_cb
self.brushlist.item_selected += self._item_selected_cb
scrolledwin = Gtk.ScrolledWindow()
scrolledwin.set_policy(Gtk.PolicyType.NEVER, Gtk.PolicyType.ALWAYS)
scrolledwin.add(self.brushlist)
w = int(self.ICON_SIZE * 4.5)
h = int(self.ICON_SIZE * 5.0)
scrolledwin.set_min_content_width(w)
scrolledwin.set_min_content_height(h)
scrolledwin.get_child().set_size_request(w, h)
self.pack_start(self.groups_sb, False, False, 0)
self.pack_start(scrolledwin, True, True, 0)
self.set_spacing(widgets.SPACING_TIGHT)
def _item_selected_cb(self, pixbuf_list, brush):
"""Internal: call brush_selected event when an item is chosen"""
self.brush_selected(brush)
@event
def brush_selected(self, brush):
"""Event: a brush was selected
:param brush: The newly chosen brush
"""
def _make_groups_sb_model(self):
"""Internal: create the model for the group choice spinbox"""
group_names = sorted(self.bm.groups.keys())
model = []
for name in group_names:
label_text = brushmanager.translate_group_name(name)
model.append((name, label_text))
return model
def _groups_changed_cb(self, bm):
"""Internal: update the spinbox model at the top of the widget"""
model = self._make_groups_sb_model()
self.groups_sb.set_model(model)
# In case the group has been deleted and recreated, we do this:
group_name = self.groups_sb.get_value()
group_brushes = self.bm.groups.get(group_name, [])
self.brushlist.itemlist = group_brushes
self.brushlist.update()
# See https://github.com/mypaint/mypaint/issues/654
def _brushes_changed_cb(self, bm, brushes):
"""Internal: update the PixbufList if its group was changed."""
# CARE: this might be called in response to the group being deleted.
# Don't recreate it by accident.
group_name = self.groups_sb.get_value()
group_brushes = self.bm.groups.get(group_name)
if brushes is group_brushes:
self.brushlist.update()
def _groups_sb_changed_cb(self, group_name):
"""Internal: update the list of brush icons when the group changes"""
self.app.preferences[self._prefs_key] = group_name
group_brushes = self.bm.groups.get(group_name, [])
self.brushlist.itemlist = group_brushes
self.brushlist.update()
def advance(self):
"""Advances to the next page of brushes."""
self.groups_sb.next()
class BrushChooserPopup (windowing.ChooserPopup):
"""Speedy brush chooser popup"""
def __init__(self, app, prefs_id=_DEFAULT_PREFS_ID):
"""Initialize.
:param gui.application.Application app: main app instance
:param unicode prefs_id: prefs identifier for the chooser
The prefs identifier forms part of preferences key which store
layout and which page of the chooser is selected. It should
follow the same syntax rules as Python simple identifiers.
"""
windowing.ChooserPopup.__init__(
self,
app = app,
actions = [
'ColorChooserPopup',
'ColorChooserPopupFastSubset',
'BrushChooserPopup',
],
config_name = "brush_chooser.%s" % (prefs_id,),
)
self._chosen_brush = None
self._chooser = QuickBrushChooser(app, prefs_id=prefs_id)
self._chooser.brush_selected += self._brush_selected_cb
bl = self._chooser.brushlist
bl.connect("button-release-event", self._brushlist_button_release_cb)
self.add(self._chooser)
def _brush_selected_cb(self, chooser, brush):
"""Internal: update the response brush when an icon is clicked"""
self._chosen_brush = brush
def _brushlist_button_release_cb(self, *junk):
"""Internal: send an accept response on a button release
We only send the response (and close the dialog) on button release to
avoid accidental dabs with the stylus.
"""
if self._chosen_brush is not None:
bm = self.app.brushmanager
bm.select_brush(self._chosen_brush)
self.hide()
self._chosen_brush = None
def advance(self):
"""Advances to the next page of brushes."""
self._chooser.advance()
class QuickColorChooser (Gtk.VBox):
"""A quick chooser widget for colors"""
## Class constants
_PREFS_KEY_TEMPLATE = u"color_chooser.%s.selected_adjuster"
_ALL_ADJUSTER_CLASSES = [
gui.colortools.HCYWheelTool,
gui.colortools.HSVWheelTool,
gui.colortools.PaletteTool,
gui.colortools.HSVCubeTool,
gui.colortools.HSVSquareTool,
gui.colortools.ComponentSlidersTool,
gui.colortools.RingsColorChangerTool,
gui.colortools.WashColorChangerTool,
gui.colortools.CrossedBowlColorChangerTool,
]
_SINGLE_CLICK_ADJUSTER_CLASSES = [
gui.colortools.PaletteTool,
gui.colortools.WashColorChangerTool,
gui.colortools.CrossedBowlColorChangerTool,
]
def __init__(self, app, prefs_id=_DEFAULT_PREFS_ID, single_click=False):
Gtk.VBox.__init__(self)
self._app = app
self._spinbox_model = []
self._adjs = {}
self._pages = []
mgr = app.brush_color_manager
if single_click:
adjuster_classes = self._SINGLE_CLICK_ADJUSTER_CLASSES
else:
adjuster_classes = self._ALL_ADJUSTER_CLASSES
for page_class in adjuster_classes:
name = page_class.__name__
page = page_class()
self._pages.append(page)
self._spinbox_model.append((name, page.tool_widget_title))
self._adjs[name] = page
page.set_color_manager(mgr)
if page_class in self._SINGLE_CLICK_ADJUSTER_CLASSES:
page.connect_after(
"button-release-event",
self._ccwidget_btn_release_cb,
)
self._prefs_key = self._PREFS_KEY_TEMPLATE % (prefs_id,)
active_page = app.preferences.get(self._prefs_key, None)
sb = spinbox.ItemSpinBox(self._spinbox_model, self._spinbox_changed_cb,
active_page)
active_page = sb.get_value()
self._spinbox = sb
self._active_adj = self._adjs[active_page]
self.pack_start(sb, False, False, 0)
self.pack_start(self._active_adj, True, True, 0)
self.set_spacing(widgets.SPACING_TIGHT)
def _spinbox_changed_cb(self, page_name):
self._app.preferences[self._prefs_key] = page_name
self.remove(self._active_adj)
new_adj = self._adjs[page_name]
self._active_adj = new_adj
self.pack_start(self._active_adj, True, True, 0)
self._active_adj.show_all()
def _ccwidget_btn_release_cb(self, ccwidget, event):
"""Internal: fire "choice_completed" after clicking certain widgets"""
self.choice_completed()
return False
@event
def choice_completed(self):
"""Event: a complete selection was made
This is emitted by button-release events on certain kinds of colour
chooser page. Not every page in the chooser emits this event, because
colour is a three-dimensional quantity: clicking on a two-dimensional
popup can't make a complete choice of colour with most pages.
The palette page does emit this event, and it's the default.
"""
def advance(self):
"""Advances to the next color selector."""
self._spinbox.next()
class ColorChooserPopup (windowing.ChooserPopup):
"""Speedy color chooser dialog"""
def __init__(self, app, prefs_id=_DEFAULT_PREFS_ID, single_click=False):
"""Initialize.
:param gui.application.Application app: main app instance
:param unicode prefs_id: prefs identifier for the chooser
:param bool single_click: limit to just the single-click adjusters
The prefs identifier forms part of preferences key which store
layout and which page of the chooser is selected. It should
follow the same syntax rules as Python simple identifiers.
"""
windowing.ChooserPopup.__init__(
self,
app = app,
actions = [
'ColorChooserPopup',
'ColorChooserPopupFastSubset',
'BrushChooserPopup',
],
config_name = u"color_chooser.%s" % (prefs_id,),
)
self._chooser = QuickColorChooser(
app,
prefs_id=prefs_id,
single_click=single_click,
)
self._chooser.choice_completed += self._choice_completed_cb
self.add(self._chooser)
def _choice_completed_cb(self, chooser):
"""Internal: close when a choice is (fully) made
Close the dialog on button release only to avoid accidental dabs
with the stylus.
"""
self.hide()
def advance(self):
"""Advances to the next color selector."""
self._chooser.advance()
## Classes: interface registration
Advanceable.register(QuickBrushChooser)
Advanceable.register(QuickColorChooser)
Advanceable.register(BrushChooserPopup)
Advanceable.register(ColorChooserPopup)