ยปCore Development>Code coverage>Tools/pynche/ListViewer.py

Python code coverage for Tools/pynche/ListViewer.py

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