1 | n/a | """ListViewer class. |
---|
2 | n/a | |
---|
3 | n/a | This class implements an input/output view on the color model. It lists every |
---|
4 | n/a | unique color (e.g. unique r/g/b value) found in the color database. Each |
---|
5 | n/a | color is shown by small swatch and primary color name. Some colors have |
---|
6 | n/a | aliases -- more than one name for the same r/g/b value. These aliases are |
---|
7 | n/a | displayed in the small listbox at the bottom of the screen. |
---|
8 | n/a | |
---|
9 | n/a | Clicking on a color name or swatch selects that color and updates all other |
---|
10 | n/a | windows. When a color is selected in a different viewer, the color list is |
---|
11 | n/a | scrolled to the selected color and it is highlighted. If the selected color |
---|
12 | n/a | is an r/g/b value without a name, no scrolling occurs. |
---|
13 | n/a | |
---|
14 | n/a | You can turn off Update On Click if all you want to see is the alias for a |
---|
15 | n/a | given name, without selecting the color. |
---|
16 | n/a | """ |
---|
17 | n/a | |
---|
18 | n/a | from tkinter import * |
---|
19 | n/a | import ColorDB |
---|
20 | n/a | |
---|
21 | n/a | ADDTOVIEW = 'Color %List Window...' |
---|
22 | n/a | |
---|
23 | n/a | class ListViewer: |
---|
24 | n/a | def __init__(self, switchboard, master=None): |
---|
25 | n/a | self.__sb = switchboard |
---|
26 | n/a | optiondb = switchboard.optiondb() |
---|
27 | n/a | self.__lastbox = None |
---|
28 | n/a | self.__dontcenter = 0 |
---|
29 | n/a | # GUI |
---|
30 | n/a | root = self.__root = Toplevel(master, class_='Pynche') |
---|
31 | n/a | root.protocol('WM_DELETE_WINDOW', self.withdraw) |
---|
32 | n/a | root.title('Pynche Color List') |
---|
33 | n/a | root.iconname('Pynche Color List') |
---|
34 | n/a | root.bind('<Alt-q>', self.__quit) |
---|
35 | n/a | root.bind('<Alt-Q>', self.__quit) |
---|
36 | n/a | root.bind('<Alt-w>', self.withdraw) |
---|
37 | n/a | root.bind('<Alt-W>', self.withdraw) |
---|
38 | n/a | # |
---|
39 | n/a | # create the canvas which holds everything, and its scrollbar |
---|
40 | n/a | # |
---|
41 | n/a | frame = self.__frame = Frame(root) |
---|
42 | n/a | frame.pack() |
---|
43 | n/a | canvas = self.__canvas = Canvas(frame, width=160, height=300, |
---|
44 | n/a | borderwidth=2, relief=SUNKEN) |
---|
45 | n/a | self.__scrollbar = Scrollbar(frame) |
---|
46 | n/a | self.__scrollbar.pack(fill=Y, side=RIGHT) |
---|
47 | n/a | canvas.pack(fill=BOTH, expand=1) |
---|
48 | n/a | canvas.configure(yscrollcommand=(self.__scrollbar, 'set')) |
---|
49 | n/a | self.__scrollbar.configure(command=(canvas, 'yview')) |
---|
50 | n/a | self.__populate() |
---|
51 | n/a | # |
---|
52 | n/a | # Update on click |
---|
53 | n/a | self.__uoc = BooleanVar() |
---|
54 | n/a | self.__uoc.set(optiondb.get('UPONCLICK', 1)) |
---|
55 | n/a | self.__uocbtn = Checkbutton(root, |
---|
56 | n/a | text='Update on Click', |
---|
57 | n/a | variable=self.__uoc, |
---|
58 | n/a | command=self.__toggleupdate) |
---|
59 | n/a | self.__uocbtn.pack(expand=1, fill=BOTH) |
---|
60 | n/a | # |
---|
61 | n/a | # alias list |
---|
62 | n/a | self.__alabel = Label(root, text='Aliases:') |
---|
63 | n/a | self.__alabel.pack() |
---|
64 | n/a | self.__aliases = Listbox(root, height=5, |
---|
65 | n/a | selectmode=BROWSE) |
---|
66 | n/a | self.__aliases.pack(expand=1, fill=BOTH) |
---|
67 | n/a | |
---|
68 | n/a | def __populate(self): |
---|
69 | n/a | # |
---|
70 | n/a | # create all the buttons |
---|
71 | n/a | colordb = self.__sb.colordb() |
---|
72 | n/a | canvas = self.__canvas |
---|
73 | n/a | row = 0 |
---|
74 | n/a | widest = 0 |
---|
75 | n/a | bboxes = self.__bboxes = [] |
---|
76 | n/a | for name in colordb.unique_names(): |
---|
77 | n/a | exactcolor = ColorDB.triplet_to_rrggbb(colordb.find_byname(name)) |
---|
78 | n/a | canvas.create_rectangle(5, row*20 + 5, |
---|
79 | n/a | 20, row*20 + 20, |
---|
80 | n/a | fill=exactcolor) |
---|
81 | n/a | textid = canvas.create_text(25, row*20 + 13, |
---|
82 | n/a | text=name, |
---|
83 | n/a | anchor=W) |
---|
84 | n/a | x1, y1, textend, y2 = canvas.bbox(textid) |
---|
85 | n/a | boxid = canvas.create_rectangle(3, row*20+3, |
---|
86 | n/a | textend+3, row*20 + 23, |
---|
87 | n/a | outline='', |
---|
88 | n/a | tags=(exactcolor, 'all')) |
---|
89 | n/a | canvas.bind('<ButtonRelease>', self.__onrelease) |
---|
90 | n/a | bboxes.append(boxid) |
---|
91 | n/a | if textend+3 > widest: |
---|
92 | n/a | widest = textend+3 |
---|
93 | n/a | row += 1 |
---|
94 | n/a | canvheight = (row-1)*20 + 25 |
---|
95 | n/a | canvas.config(scrollregion=(0, 0, 150, canvheight)) |
---|
96 | n/a | for box in bboxes: |
---|
97 | n/a | x1, y1, x2, y2 = canvas.coords(box) |
---|
98 | n/a | canvas.coords(box, x1, y1, widest, y2) |
---|
99 | n/a | |
---|
100 | n/a | def __onrelease(self, event=None): |
---|
101 | n/a | canvas = self.__canvas |
---|
102 | n/a | # find the current box |
---|
103 | n/a | x = canvas.canvasx(event.x) |
---|
104 | n/a | y = canvas.canvasy(event.y) |
---|
105 | n/a | ids = canvas.find_overlapping(x, y, x, y) |
---|
106 | n/a | for boxid in ids: |
---|
107 | n/a | if boxid in self.__bboxes: |
---|
108 | n/a | break |
---|
109 | n/a | else: |
---|
110 | n/a | ## print 'No box found!' |
---|
111 | n/a | return |
---|
112 | n/a | tags = self.__canvas.gettags(boxid) |
---|
113 | n/a | for t in tags: |
---|
114 | n/a | if t[0] == '#': |
---|
115 | n/a | break |
---|
116 | n/a | else: |
---|
117 | n/a | ## print 'No color tag found!' |
---|
118 | n/a | return |
---|
119 | n/a | red, green, blue = ColorDB.rrggbb_to_triplet(t) |
---|
120 | n/a | self.__dontcenter = 1 |
---|
121 | n/a | if self.__uoc.get(): |
---|
122 | n/a | self.__sb.update_views(red, green, blue) |
---|
123 | n/a | else: |
---|
124 | n/a | self.update_yourself(red, green, blue) |
---|
125 | n/a | self.__red, self.__green, self.__blue = red, green, blue |
---|
126 | n/a | |
---|
127 | n/a | def __toggleupdate(self, event=None): |
---|
128 | n/a | if self.__uoc.get(): |
---|
129 | n/a | self.__sb.update_views(self.__red, self.__green, self.__blue) |
---|
130 | n/a | |
---|
131 | n/a | def __quit(self, event=None): |
---|
132 | n/a | self.__root.quit() |
---|
133 | n/a | |
---|
134 | n/a | def withdraw(self, event=None): |
---|
135 | n/a | self.__root.withdraw() |
---|
136 | n/a | |
---|
137 | n/a | def deiconify(self, event=None): |
---|
138 | n/a | self.__root.deiconify() |
---|
139 | n/a | |
---|
140 | n/a | def update_yourself(self, red, green, blue): |
---|
141 | n/a | canvas = self.__canvas |
---|
142 | n/a | # turn off the last box |
---|
143 | n/a | if self.__lastbox: |
---|
144 | n/a | canvas.itemconfigure(self.__lastbox, outline='') |
---|
145 | n/a | # turn on the current box |
---|
146 | n/a | colortag = ColorDB.triplet_to_rrggbb((red, green, blue)) |
---|
147 | n/a | canvas.itemconfigure(colortag, outline='black') |
---|
148 | n/a | self.__lastbox = colortag |
---|
149 | n/a | # fill the aliases |
---|
150 | n/a | self.__aliases.delete(0, END) |
---|
151 | n/a | try: |
---|
152 | n/a | aliases = self.__sb.colordb().aliases_of(red, green, blue)[1:] |
---|
153 | n/a | except ColorDB.BadColor: |
---|
154 | n/a | self.__aliases.insert(END, '<no matching color>') |
---|
155 | n/a | return |
---|
156 | n/a | if not aliases: |
---|
157 | n/a | self.__aliases.insert(END, '<no aliases>') |
---|
158 | n/a | else: |
---|
159 | n/a | for name in aliases: |
---|
160 | n/a | self.__aliases.insert(END, name) |
---|
161 | n/a | # maybe scroll the canvas so that the item is visible |
---|
162 | n/a | if self.__dontcenter: |
---|
163 | n/a | self.__dontcenter = 0 |
---|
164 | n/a | else: |
---|
165 | n/a | ig, ig, ig, y1 = canvas.coords(colortag) |
---|
166 | n/a | ig, ig, ig, y2 = canvas.coords(self.__bboxes[-1]) |
---|
167 | n/a | h = int(canvas['height']) * 0.5 |
---|
168 | n/a | canvas.yview('moveto', (y1-h) / y2) |
---|
169 | n/a | |
---|
170 | n/a | def save_options(self, optiondb): |
---|
171 | n/a | optiondb['UPONCLICK'] = self.__uoc.get() |
---|
172 | n/a | |
---|
173 | n/a | def colordb_changed(self, colordb): |
---|
174 | n/a | self.__canvas.delete('all') |
---|
175 | n/a | self.__populate() |
---|