-
Notifications
You must be signed in to change notification settings - Fork 383
/
paletteview.py
1515 lines (1343 loc) · 53.3 KB
/
paletteview.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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# This file is part of MyPaint.
# Copyright (C) 2012-2019 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.
"""Viewer and editor widgets for palettes."""
# Editor ideas
# - "Insert lighter/darker copy of row".
# - repack palette (remove duplicates and blanks)
# - sort palette by approx. hue+chroma binning, then luma variations
## Imports
from __future__ import division, print_function
import math
import os
import re
import logging
from io import open
from lib.gibindings import Gdk
from lib.gibindings import Gtk
from lib.gibindings import GLib
import cairo
from lib.gettext import C_
from .util import clamp
from lib.palette import Palette
from lib.color import RGBColor
from lib.color import HCYColor
from lib.color import HSVColor
import gui.uicolor
from .adjbases import ColorAdjuster
from .adjbases import ColorAdjusterWidget
from .adjbases import ColorManager
from .adjbases import DATAPATH_PALETTES_SUBDIR
from .combined import CombinedAdjusterPage
from lib.pycompat import unicode
logger = logging.getLogger(__name__)
## Class defs
class PalettePage (CombinedAdjusterPage):
"""User-editable palette, as a `CombinedAdjuster` element."""
def __init__(self):
view = PaletteView()
view.grid.show_matched_color = True
view.can_select_empty = False
self._adj = view
self._edit_dialog = None
@classmethod
def get_properties_description(cls):
return C_(
"palette panel: properties button tooltip",
"Palette properties",
)
@classmethod
def get_page_icon_name(cls):
return "mypaint-tool-color-palette"
@classmethod
def get_page_title(cls):
return C_("palette panel tab tooltip title", "Palette")
@classmethod
def get_page_description(cls):
return C_(
"palette panel tab tooltip description",
"Set the color from a loadable, editable palette.",
)
def get_page_widget(self):
"""Page widget: returns the PaletteView adjuster widget itself."""
# FIXME: The PaletteNext and PalettePrev actions of the main
# app require access to the PaletteView itself.
return self._adj
def set_color_manager(self, manager):
CombinedAdjusterPage.set_color_manager(self, manager)
self._adj.set_color_manager(manager)
def show_properties(self):
if self._edit_dialog is None:
toplevel = self._adj.get_toplevel()
dialog = PaletteEditorDialog(toplevel, self.get_color_manager())
self._edit_dialog = dialog
self._edit_dialog.run()
class PaletteEditorDialog (Gtk.Dialog):
"""Dialog for editing, loading and saving the current palette."""
_UNTITLED_PALETTE_NAME = C_(
"palette editor dialog: palette name entry",
"Untitled Palette",
)
def __init__(self, parent, target_color_manager):
Gtk.Dialog.__init__(
self,
title=C_("palette editor dialog: title", "Palette Editor"),
transient_for=parent,
modal=True,
destroy_with_parent=True,
)
self.add_button(Gtk.STOCK_CANCEL, Gtk.ResponseType.REJECT)
self.add_button(Gtk.STOCK_OK, Gtk.ResponseType.ACCEPT)
self.set_position(Gtk.WindowPosition.MOUSE)
assert isinstance(target_color_manager, ColorManager)
#: ColorManager containing the palette to be edited.
self._target_color_manager = target_color_manager
view = PaletteView()
view.set_size_request(400, 300)
view.grid.show_matched_color = True
view.grid.can_select_empty = True
self._view = view
#: The working ColorManager, holding a working copy of the palette
#: to be edited.
self._mgr = ColorManager(prefs={},
datapath=target_color_manager.get_data_path())
self._mgr.set_color(RGBColor(1, 1, 1))
view.set_color_manager(self._mgr)
# Action buttons, positiopned down the right hand side
action_bbox = Gtk.VButtonBox()
load_btn = self._load_button = Gtk.Button(stock=Gtk.STOCK_OPEN)
save_btn = self._save_button = Gtk.Button(stock=Gtk.STOCK_SAVE)
add_btn = self._add_button = Gtk.Button(stock=Gtk.STOCK_ADD)
remove_btn = self._remove_button = Gtk.Button(stock=Gtk.STOCK_REMOVE)
clear_btn = self._clear_button = Gtk.Button(stock=Gtk.STOCK_CLEAR)
action_bbox.pack_start(load_btn, True, True, 0)
action_bbox.pack_start(save_btn, True, True, 0)
action_bbox.pack_start(add_btn, True, True, 0)
action_bbox.pack_start(remove_btn, True, True, 0)
action_bbox.pack_start(clear_btn, True, True, 0)
action_bbox.set_layout(Gtk.ButtonBoxStyle.START)
load_btn.connect("clicked", self._load_btn_clicked)
save_btn.connect("clicked", self._save_btn_clicked)
remove_btn.connect("clicked", self._remove_btn_clicked)
add_btn.connect("clicked", self._add_btn_clicked)
clear_btn.connect("clicked", self._clear_btn_clicked)
load_btn.set_tooltip_text(C_(
"palette editor dialog: action buttons: tooltips",
"Load from a GIMP palette file",
))
save_btn.set_tooltip_text(C_(
"palette editor dialog: action buttons: tooltips",
"Save to a GIMP palette file",
))
add_btn.set_tooltip_text(C_(
"palette editor dialog: action buttons: tooltips",
"Add a new empty swatch",
))
remove_btn.set_tooltip_text(C_(
"palette editor dialog: action buttons: tooltips",
"Remove the current swatch",
))
clear_btn.set_tooltip_text(C_(
"palette editor dialog: action buttons: tooltips",
"Remove all swatches",
))
# Button initial state and subsequent updates
remove_btn.set_sensitive(False)
self._mgr.palette.match_changed += self._palette_match_changed_cb
self._mgr.palette.info_changed += self._palette_changed_cb
self._mgr.palette.sequence_changed += self._palette_changed_cb
self._mgr.palette.color_changed += self._palette_changed_cb
# Palette name and number of entries
palette_details_hbox = Gtk.HBox()
palette_name_label = Gtk.Label(label=C_(
"palette editor dialog: palette name/title entry: label",
"Title:",
))
palette_name_label.set_tooltip_text(C_(
"palette editor dialog: palette name/title entry: tooltip",
"Name or description for this palette",
))
palette_name_entry = Gtk.Entry()
palette_name_entry.connect("changed", self._palette_name_changed_cb)
self._palette_name_entry = palette_name_entry
self._columns_adj = Gtk.Adjustment(
value=0, lower=0, upper=99,
step_increment=1, page_increment=1, page_size=0
)
self._columns_adj.connect("value-changed", self._columns_changed_cb)
columns_label = Gtk.Label(label=C_(
"palette editor dialog: number-of-columns spinbutton: title",
"Columns:"
))
columns_label.set_tooltip_text(C_(
"palette editor dialog: number-of-columns spinbutton: tooltip",
"Number of columns",
))
columns_spinbutton = Gtk.SpinButton(
adjustment=self._columns_adj,
climb_rate=1.5,
digits=0
)
palette_details_hbox.set_spacing(0)
palette_details_hbox.set_border_width(0)
palette_details_hbox.pack_start(palette_name_label, False, False, 0)
palette_details_hbox.pack_start(palette_name_entry, True, True, 6)
palette_details_hbox.pack_start(columns_label, False, False, 6)
palette_details_hbox.pack_start(columns_spinbutton, False, False, 0)
color_name_hbox = Gtk.HBox()
color_name_label = Gtk.Label(label=C_(
"palette editor dialog: color name entry: label",
"Color name:",
))
color_name_label.set_tooltip_text(C_(
"palette editor dialog: color name entry: tooltip",
"Current color's name",
))
color_name_entry = Gtk.Entry()
color_name_entry.connect("changed", self._color_name_changed_cb)
color_name_entry.set_sensitive(False)
self._color_name_entry = color_name_entry
color_name_hbox.set_spacing(6)
color_name_hbox.pack_start(color_name_label, False, False, 0)
color_name_hbox.pack_start(color_name_entry, True, True, 0)
palette_vbox = Gtk.VBox()
palette_vbox.set_spacing(12)
palette_vbox.pack_start(palette_details_hbox, False, False, 0)
palette_vbox.pack_start(view, True, True, 0)
palette_vbox.pack_start(color_name_hbox, False, False, 0)
# Dialog contents
# Main edit area to the left, buttons to the right
hbox = Gtk.HBox()
hbox.set_spacing(12)
hbox.pack_start(palette_vbox, True, True, 0)
hbox.pack_start(action_bbox, False, False, 0)
hbox.set_border_width(12)
self.vbox.pack_start(hbox, True, True, 0)
# Dialog vbox contents must be shown separately
for w in self.vbox:
w.show_all()
self.connect("response", self._response_cb)
self.connect("show", self._show_cb)
def _show_cb(self, widget, *a):
# Each time the dialog is shown, update with the target
# palette, for editing.
self.vbox.show_all()
palette = self._target_color_manager.palette
name = palette.get_name()
if not name:
name = self._ensure_valid_palette_name()
self._palette_name_entry.set_text(name)
self._columns_adj.set_value(palette.get_columns())
self._mgr.palette.update(palette)
def _palette_name_changed_cb(self, editable):
name = editable.get_chars(0, -1)
if not name:
name = "" # note: not None (it'll be stringified)
pal = self._mgr.palette
pal.name = unicode(name)
def _columns_changed_cb(self, adj):
ncolumns = int(adj.get_value())
pal = self._mgr.palette
pal.set_columns(ncolumns)
def _color_name_changed_cb(self, editable):
name = editable.get_chars(0, -1)
palette = self._mgr.palette
i = palette.match_position
if i is None:
return
old_name = palette.get_color_name(i)
if not name:
name = None
if name != old_name:
palette.set_color_name(i, name)
def _response_cb(self, widget, response_id):
if response_id == Gtk.ResponseType.ACCEPT:
palette = self._mgr.palette
target_palette = self._target_color_manager.palette
target_palette.update(palette)
self.hide()
return True
def _palette_match_changed_cb(self, palette):
col_name_entry = self._color_name_entry
i = palette.match_position
if i is not None:
col = palette[i]
if col is not None:
name = palette.get_color_name(i)
if name is None:
name = ""
col_name_entry.set_sensitive(True)
col_name_entry.set_text(name)
else:
col_name_entry.set_sensitive(False)
col_name_entry.set_text(C_(
"palette editor dialog: color name entry",
"Empty palette slot",
))
else:
col_name_entry.set_sensitive(False)
col_name_entry.set_text("")
self._update_buttons()
def _update_buttons(self):
palette = self._mgr.palette
emptyish = len(palette) == 0
if len(palette) == 1:
if palette[0] is None:
emptyish = True
can_save = not emptyish
can_clear = not emptyish
can_remove = True
if emptyish or self._mgr.palette.match_position is None:
can_remove = False
self._save_button.set_sensitive(can_save)
self._remove_button.set_sensitive(can_remove)
self._clear_button.set_sensitive(can_clear)
def _palette_changed_cb(self, palette, *args, **kwargs):
new_name = palette.get_name()
if new_name is None:
new_name = self._ensure_valid_palette_name()
old_name = self._palette_name_entry.get_chars(0, -1)
if old_name != new_name:
self._palette_name_entry.set_text(new_name)
self._columns_adj.set_value(palette.get_columns())
self._update_buttons()
def _add_btn_clicked(self, button):
palette = self._mgr.palette
i = palette.match_position
if i is None:
i = len(palette)
palette.append(None)
palette.match_position = i
else:
palette.insert(i, None)
def _remove_btn_clicked(self, button):
palette = self._mgr.palette
i = palette.match_position
if i >= 0 and i < len(palette):
palette.pop(i)
if len(palette) == 0:
palette.append(None)
def _load_btn_clicked(self, button):
preview = _PalettePreview()
manager = self._target_color_manager
datapath = manager.get_data_path()
palettes_dir = os.path.join(datapath, DATAPATH_PALETTES_SUBDIR)
palette = palette_load_via_dialog(
title=C_("palette load dialog: title", "Load palette"),
parent=self,
preview=preview,
shortcuts=[palettes_dir],
)
if palette is not None:
self._mgr.palette.update(palette)
self._ensure_valid_palette_name()
def _save_btn_clicked(self, button):
preview = _PalettePreview()
palette_save_via_dialog(
self._mgr.palette,
title=C_("palette save dialog: title", "Save palette"),
parent=self,
preview=preview,
)
def _clear_btn_clicked(self, button):
pal = self._mgr.palette
pal.clear()
self._ensure_valid_palette_name()
def _ensure_valid_palette_name(self):
pal = self._mgr.palette
if not pal.name:
pal.name = self._UNTITLED_PALETTE_NAME
return pal.name
class PaletteView (ColorAdjuster, Gtk.ScrolledWindow):
"""Scrollable view of a palette.
Palette entries can be clicked to select the color, and all instances of
the current shared color in the palette are highlighted.
"""
## Sizing constraint constants
_MIN_HEIGHT = 32
_MIN_WIDTH = 150
_MAX_NATURAL_HEIGHT = 300
_MAX_NATURAL_WIDTH = 300
def __init__(self):
Gtk.ScrolledWindow.__init__(self)
self.grid = _PaletteGridLayout()
self.set_policy(Gtk.PolicyType.NEVER, Gtk.PolicyType.AUTOMATIC)
self.add(self.grid)
def set_color_manager(self, mgr):
self.grid.set_color_manager(mgr)
ColorAdjuster.set_color_manager(self, mgr)
## Sizing boilerplate
# Reflect what the embedded grid widget tells us, but limit its natural
# size to something sensible. Huge palettes make huge grids...
def do_get_request_mode(self):
return self.grid.get_request_mode()
def do_get_preferred_width(self):
gminw, gnatw = self.grid.get_preferred_width()
minw = self._MIN_WIDTH
natw = min(gnatw, self._MAX_NATURAL_WIDTH)
return minw, max(minw, natw)
def do_get_preferred_height(self):
gminh, gnath = self.grid.get_preferred_height()
minh = self._MIN_HEIGHT
nath = min(gnath, self._MAX_NATURAL_HEIGHT)
return minh, max(minh, nath)
def do_get_preferred_width_for_height(self, height):
gminw, gnatw = self.grid.get_preferred_width_for_height(height)
minw = self._MIN_WIDTH
natw = min(gnatw, self._MAX_NATURAL_WIDTH)
return minw, max(minw, natw)
def do_get_preferred_height_for_width(self, width):
gminh, gnath = self.grid.get_preferred_height_for_width(width)
minh = self._MIN_HEIGHT
nath = min(gnath, self._MAX_NATURAL_HEIGHT)
return minh, max(minh, nath)
class _PalettePreview (Gtk.DrawingArea):
"""Preview-only palette view."""
_palette = None
def __init__(self):
Gtk.DrawingArea.__init__(self)
self.connect("draw", self._draw_cb)
self.set_size_request(128, 256)
def _draw_cb(self, widget, cr):
if self._palette is None:
return
alloc = widget.get_allocation()
w, h = alloc.width, alloc.height
s_max = 16 # min(w, h)
s_min = 4
ncolumns = self._palette.get_columns()
ncolors = len(self._palette)
if ncolors == 0:
return
if not ncolumns == 0:
s = w / ncolumns
s = clamp(s, s_min, s_max)
s = int(s)
if (s * ncolumns) > w:
ncolumns = 0
if ncolumns == 0:
s = math.sqrt((w * h) / ncolors)
s = clamp(s, s_min, s_max)
s = int(s)
ncolumns = max(1, int(w // s))
nrows = int(ncolors // ncolumns)
if ncolors % ncolumns != 0:
nrows += 1
nrows = max(1, nrows)
dx, dy = 0, 0
if (nrows * s) < h:
dy = int(h - (nrows * s)) // 2
if (ncolumns * s) < w:
dx = int(w - (ncolumns * s)) // 2
bg_color = _widget_get_bg_color(self)
_palette_render(self._palette, cr, rows=nrows, columns=ncolumns,
swatch_size=s, bg_color=bg_color,
offset_x=dx, offset_y=dy,
rtl=False)
def set_palette(self, palette):
self._palette = palette
self.queue_draw()
def _widget_get_bg_color(widget):
"""Valid background color from the first ancestor widget having one
Workaround for some widget arrangements in Adwaita for 3.14 having
null background colors. Fallback is a medium grey, which should be
acceptable with most styles.
"""
while widget is not None:
state = widget.get_state_flags()
style_context = widget.get_style_context()
bg_rgba = style_context.get_background_color(state)
if bg_rgba.alpha != 0:
return gui.uicolor.from_gdk_rgba(bg_rgba)
widget = widget.get_parent()
return RGBColor(0.5, 0.5, 0.5)
class _PaletteGridLayout (ColorAdjusterWidget):
"""The palette layout embedded in a scrolling PaletteView.
"""
## Class settings
IS_DRAG_SOURCE = True
HAS_DETAILS_DIALOG = True
STATIC_TOOLTIP_TEXT = C_(
"palette view",
"Color swatch palette.\nDrop colors here,\ndrag them to organize.",
)
ALLOW_HCY_TWEAKING = False # Interacts badly with menus
## Layout constants
_SWATCH_SIZE_MIN = 8
_SWATCH_SIZE_MAX = 50
_SWATCH_SIZE_NOMINAL = 20
_PREFERRED_COLUMNS = 5 #: Preferred width in cells for free-flow mode.
def __init__(self):
ColorAdjusterWidget.__init__(self)
evbox = Gtk.EventBox()
self.add(evbox)
# Sizing
s = self._SWATCH_SIZE_NOMINAL
self.set_size_request(s, s)
self.connect("size-allocate", self._size_alloc_cb)
#: Highlight the currently matched color
self.show_matched_color = False
#: User can click on empty slots
self.can_select_empty = False
# Current index
evbox.connect("button-press-event", self._button_press_cb)
evbox.connect_after("button-release-event", self._button_release_cb)
# Dragging
evbox.connect("motion-notify-event", self._motion_notify_cb)
evbox.add_events(Gdk.EventMask.POINTER_MOTION_MASK)
# Tooltips
self._tooltip_index = None
self.set_has_tooltip(True)
# Target markers while dragging or invoking the context menu
self._insert_target_index = None
# Cached layout details
self._rows = None
self._columns = None
self._last_palette_columns = None
self._swatch_size = self._SWATCH_SIZE_NOMINAL
def _size_alloc_cb(self, widget, alloc):
"""Caches layout details after size negotiation.
"""
width = alloc.width
height = alloc.height
ncolors, nrows, ncolumns = self._get_palette_dimensions()
if nrows and ncolumns:
# Fitted to the major dimension
size = int(min(width / ncolumns, height / nrows))
size = self._constrain_swatch_size(size)
else:
# Free-flowing
if ncolors > 0:
size = int(math.sqrt((width * height) / ncolors))
size = self._constrain_swatch_size(size)
ncolumns = max(1, min(ncolors, width // size))
nrows = max(1, int(ncolors // ncolumns))
if int(ncolors % ncolumns) > 0:
nrows += 1
if nrows * size > height or ncolumns * size > width:
size = max(1, min(int(height // nrows),
int(width // ncolumns)))
size = self._constrain_swatch_size(size)
ncolumns = max(1, min(ncolors, width // size))
nrows = max(1, int(ncolors // ncolumns))
if int(ncolors % ncolumns) > 0:
nrows += 1
else:
nrows = 0
ncolumns = 0
size = self._SWATCH_SIZE_NOMINAL
self._rows = nrows
self._columns = ncolumns
self._swatch_size = size
## Palette monitoring
def set_color_manager(self, mgr):
ColorAdjusterWidget.set_color_manager(self, mgr)
# Could be smarter about these: probably no need to redraw on
# every little change.
mgr.palette.info_changed += self._palette_changed_cb
mgr.palette.match_changed += self._palette_changed_cb
mgr.palette.sequence_changed += self._palette_changed_cb
mgr.palette.color_changed += self._palette_changed_cb
def _palette_changed_cb(self, palette, *args, **kwargs):
"""Called after each change made to the palette."""
# Determine if the layout has changed since the last time the palette
# was draw.
layout_changed = False
if None in (self._rows, self._columns):
logger.debug("layout changed: null preexisting layout info")
layout_changed = True
else:
if palette.columns != self._last_palette_columns:
layout_changed = True
logger.debug("layout changed: different number of columns")
else:
ncells = self._rows * self._columns
ncolors = len(palette)
if ncolors > ncells or ncolors <= ncells - self._columns:
logger.debug("layout changed: cannot fit palette into "
"currently calculated space")
layout_changed = True
# Queue a resize (and an implicit redraw) if the layout has changed,
# or just a redraw.
if layout_changed:
logger.debug("queuing full resize")
self._rows = None
self._columns = None
self.queue_resize()
self._insert_target_index = None
self._tooltip_index = None
else:
logger.debug("layout unchanged, queuing redraw")
self.queue_draw()
self._last_palette_columns = palette.columns
## Pointer event handling
def _motion_notify_cb(self, widget, event):
x, y = event.x, event.y
i = self.get_index_at_pos(x, y)
# Set the tooltip.
# Passing the tooltip through a value of None is necessary for its
# position on the screen to be updated to where the pointer is. Setting
# it to None, and then to the desired value must happen in two separate
# events for the tooltip window position update to be honoured.
if i is None:
# Not over a color, so use the static default
if self._tooltip_index not in (-1, -2):
# First such event: reset the tooltip.
self._tooltip_index = -1
self.set_has_tooltip(False)
self.set_tooltip_text("")
elif self._tooltip_index != -2:
# Second event over a non-color: set the tooltip text.
self._tooltip_index = -2
self.set_has_tooltip(True)
self.set_tooltip_text(self.STATIC_TOOLTIP_TEXT)
elif self._tooltip_index != i:
# Mouse pointer has moved to a different color, or away
# from the two states above.
if self._tooltip_index is not None:
# First event for this i: reset the tooltip.
self._tooltip_index = None
self.set_has_tooltip(False)
self.set_tooltip_text("")
else:
# Second event for this i: set the desired tooltip text.
self._tooltip_index = i
mgr = self.get_color_manager()
tip = mgr.palette.get_color_name(i)
color = mgr.palette.get_color(i)
if color is None:
tip = C_(
"palette view",
"Empty palette slot (drag a color here)",
)
elif tip is None or tip.strip() == "":
tip = "" # Anonymous colors don't get tooltips
self.set_has_tooltip(True)
self.set_tooltip_text(tip)
def _button_press_cb(self, widget, event):
"""Handle button presses."""
# The base class has a separate handler which
# changes the managed colour, so don't need to do that here.
if event.type != Gdk.EventType.BUTTON_PRESS:
return False
# Move the highlight
x, y = event.x, event.y
i = self.get_index_at_pos(x, y, nearest=False)
mgr = self.get_color_manager()
is_empty = mgr.palette.get_color(i) is None
if event.button == 1:
if not (is_empty and not self.can_select_empty):
mgr.palette.set_match_position(i)
mgr.palette.set_match_is_approx(False)
return False
# Button 3 shows a menu
if event.button != 3:
return False
self._popup_context_menu(event)
def _popup_context_menu(self, event):
x, y = event.x, event.y
i = self.get_index_at_pos(x, y, nearest=True, insert=True)
mx, my = self.get_position_for_index(i)
mx = event.x_root - x + mx + self._swatch_size
my = event.y_root - y + my + self._swatch_size
menu = self._get_context_menu(i)
menu.show_all()
menu.popup(
parent_menu_shell = None,
parent_menu_item = None,
func = lambda *a: (mx, my, True),
data = None,
button = event.button,
activate_time = event.time,
)
self._insert_target_index = i
self.queue_draw()
return False
def _get_empty_range(self, index):
"""Returns the populated start and end of a range of empty slots
Returns the indices of two populated swatches around the target
swatch, or None if there's no run of one or more empty slots
between them.
"""
start_index = None
end_index = None
palette = self.get_color_manager().palette
if palette[index] is not None:
return None
i = index
while i >= 0:
i -= 1
if palette[i] is not None:
start_index = i
break
i = index
while i < len(palette):
i += 1
if palette[i] is not None:
end_index = i
break
if None not in (start_index, end_index):
assert start_index < end_index
if start_index < end_index - 1:
return (start_index, end_index)
return None
def _get_context_menu(self, i):
menu = Gtk.Menu()
menu.connect_after("deactivate", self._context_menu_deactivate_cb)
palette = self.get_color_manager().palette
empty_range = self._get_empty_range(i)
item_defs = [
(
# TRANSLATORS: inserting gaps (empty color swatches)
C_("palette view: context menu", "Add Empty Slot"),
self._insert_empty_slot_cb,
True,
[i],
),
(
# TRANSLATORS: inserting gaps (empty color swatches)
C_("palette view: context menu", "Insert Row"),
self._insert_empty_row_cb,
True,
[i],
),
(
# TRANSLATORS: inserting gaps (empty color swatches)
C_("palette view: context menu", "Insert Column"),
self._insert_empty_column_cb,
bool(palette.get_columns()),
[i],
),
None,
(
# TRANSLATORS: Color interpolations
C_("palette view: context menu", "Fill Gap (RGB)"),
self._interpolate_empty_range_cb,
bool(empty_range),
[RGBColor, empty_range],
),
(
# TRANSLATORS: Color interpolations
C_("palette view: context menu", "Fill Gap (HCY)"),
self._interpolate_empty_range_cb,
bool(empty_range),
[HCYColor, empty_range],
),
(
# TRANSLATORS: Color interpolations
C_("palette view: context menu", "Fill Gap (HSV)"),
self._interpolate_empty_range_cb,
bool(empty_range),
[HSVColor, empty_range],
),
]
for item_def in item_defs:
if not item_def:
item = Gtk.SeparatorMenuItem()
else:
label_str, activate_cb, sensitive, args = item_def
item = Gtk.MenuItem()
item.set_label(label_str)
if activate_cb:
item.connect("activate", activate_cb, *args)
item.set_sensitive(sensitive)
menu.append(item)
menu.attach_to_widget(self)
return menu
def _button_release_cb(self, widget, event):
pass
## Context menu handlers
def _context_menu_deactivate_cb(self, menu):
self._insert_target_index = None
self.queue_draw()
GLib.idle_add(menu.destroy)
def _insert_empty_slot_cb(self, menuitem, target_i):
palette = self.get_color_manager().palette
palette.insert(target_i, None)
def _insert_empty_row_cb(self, menuitem, target_i):
row_start_i = (target_i // self._columns) * self._columns
palette = self.get_color_manager().palette
for i in range(palette.get_columns()):
palette.insert(row_start_i, None)
def _insert_empty_column_cb(self, menuitem, target_i):
palette = self.get_color_manager().palette
assert palette.get_columns(), \
"Can't insert columns into a free-flowing palette"
row_di = target_i % self._columns
columns_new = palette.get_columns() + 1
r = 0
while r * columns_new < len(palette):
i = r * columns_new + row_di
if i >= len(palette):
break
palette.insert(i, None)
r += 1
palette.set_columns(columns_new)
def _interpolate_empty_range_cb(self, menuitem, color_class, range):
i0, ix = range
palette = self.get_color_manager().palette
c0 = color_class(color=palette[i0])
cx = color_class(color=palette[ix])
nsteps = ix - i0 + 1
if nsteps < 3:
return
interpolated = list(c0.interpolate(cx, nsteps))
assert len(interpolated) == nsteps
interpolated.pop(0)
interpolated.pop(-1)
for i, c in enumerate(interpolated):
palette[i0 + 1 + i] = c
## Dimensions and sizing
@classmethod
def _constrain_swatch_size(cls, size):
size = min(cls._SWATCH_SIZE_MAX, max(cls._SWATCH_SIZE_MIN, size))
# Restrict to multiples of 2 for patterns, plus one for the border
if size % 2 == 0:
size -= 1
return size
def _get_palette_dimensions(self):
"""Normalized palette dimensions: (ncolors, nrows, ncolumns).
Row and columns figures are None if the layout is to be free-flowing.
"""
mgr = self.get_color_manager()
ncolumns = mgr.palette.get_columns()
ncolors = len(mgr.palette)
if ncolumns is None or ncolumns < 1:
nrows = None
ncolumns = None
else:
ncolumns = int(ncolumns)
if ncolors > 0:
ncolumns = min(ncolumns, ncolors)
nrows = max(1, int(ncolors // ncolumns))
if int(ncolors % ncolumns) > 0:
nrows += 1
else:
ncolumns = 1
nrows = 1
return (ncolors, nrows, ncolumns)
def do_get_request_mode(self):
"""GtkWidget size negotiation implementation
"""
ncolors, nrows, ncolumns = self._get_palette_dimensions()
mode = Gtk.SizeRequestMode.HEIGHT_FOR_WIDTH
if nrows and ncolumns:
if nrows > ncolumns:
mode = Gtk.SizeRequestMode.WIDTH_FOR_HEIGHT
return mode
def do_get_preferred_width(self):
"""GtkWidget size negotiation implementation.
"""
ncolors, nrows, ncolumns = self._get_palette_dimensions()
if ncolumns and ncolumns:
# Horizontal fit, assume rows <= columns
min_w = self._SWATCH_SIZE_MIN * ncolumns
nat_w = self._SWATCH_SIZE_NOMINAL * ncolumns
else:
# Free-flowing, across and then down
ncolumns = max(1, min(self._PREFERRED_COLUMNS, ncolors))
min_w = self._SWATCH_SIZE_MIN
nat_w = self._SWATCH_SIZE_NOMINAL * ncolumns
return min_w, max(min_w, nat_w)
def do_get_preferred_height_for_width(self, width):
"""GtkWidget size negotiation implementation.
"""
ncolors, nrows, ncolumns = self._get_palette_dimensions()
if nrows and ncolumns:
# Horizontal fit
swatch_size = self._constrain_swatch_size(int(width // ncolumns))
min_h = self._SWATCH_SIZE_MIN * nrows
nat_h = swatch_size * nrows
else:
# Free-flowing, across and then down
# Since s = sqrt((w*h)/n),
min_h = int(((self._SWATCH_SIZE_MIN ** 2) * ncolors) // width)
nat_h = int(((self._SWATCH_SIZE_NOMINAL ** 2) * ncolors) // width)
return min_h, max(min_h, nat_h)
def do_get_preferred_height(self):
"""GtkWidget size negotiation implementation.
"""
ncolors, nrows, ncolumns = self._get_palette_dimensions()
if nrows and ncolumns:
# Vertical fit, assume rows > columns
min_h = self._SWATCH_SIZE_MIN * nrows
nat_h = self._SWATCH_SIZE_NOMINAL * nrows
else:
# Height required for our own minimum width (note do_())
min_w, nat_w = self.do_get_preferred_width()
min_h, nat_h = self.do_get_preferred_height_for_width(min_w)
return min_h, max(min_h, nat_h)
def do_get_preferred_width_for_height(self, height):
"""GtkWidget size negotiation implementation.
"""
ncolors, nrows, ncolumns = self._get_palette_dimensions()
if nrows and ncolumns:
# Vertical fit
swatch_size = self._constrain_swatch_size(int(height // nrows))
min_w = self._SWATCH_SIZE_MIN * ncolumns
nat_w = swatch_size * ncolumns
else:
# Just the minimum and natural width (note do_())
min_w, nat_w = self.do_get_preferred_width()
return min_w, max(min_w, nat_w)
def _get_background_size(self):
# HACK. it's quicker for this widget to render in the foreground
return 1, 1