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