blob: 8c8a6eec9bfb30e440d0aadfe95a5faabbe460ba [file] [log] [blame]
Barry Warsaweb61fbd1998-10-02 16:06:27 +00001"""ListViewer class.
2
3This class implements an input/output view on the color model. It lists every
4unique color (e.g. unique r/g/b value) found in the color database. Each
5color is shown by small swatch and primary color name. Some colors have
6aliases -- more than one name for the same r/g/b value. These aliases are
7displayed in the small listbox at the bottom of the screen.
8
9Clicking on a color name or swatch selects that color and updates all other
10windows. When a color is selected in a different viewer, the color list is
11scrolled to the selected color and it is highlighted. If the selected color
12is an r/g/b value without a name, no scrolling occurs.
13
14You can turn off Update On Click if all you want to see is the alias for a
15given name, without selecting the color.
16"""
17
Barry Warsaw46670a21998-10-01 21:40:48 +000018from Tkinter import *
Barry Warsaw46670a21998-10-01 21:40:48 +000019import ColorDB
20
21class ListViewer:
22 def __init__(self, switchboard, parent=None):
23 self.__sb = switchboard
24 self.__lastbox = None
Barry Warsawb44056e1998-10-02 15:52:10 +000025 self.__dontcenter = 0
Barry Warsaw964fbbb1998-10-05 21:15:08 +000026 # GUI
Barry Warsaw46670a21998-10-01 21:40:48 +000027 root = self.__root = Toplevel(parent, class_='Pynche')
28 root.protocol('WM_DELETE_WINDOW', self.__withdraw)
Barry Warsaw7c51a9a1998-10-05 21:31:37 +000029 root.title('Pynche Color List')
Barry Warsaw46670a21998-10-01 21:40:48 +000030 root.iconname('Pynche Color List')
31 root.bind('<Alt-q>', self.__quit)
32 root.bind('<Alt-Q>', self.__quit)
Barry Warsaw964fbbb1998-10-05 21:15:08 +000033 root.bind('<Alt-w>', self.__withdraw)
34 root.bind('<Alt-W>', self.__withdraw)
Barry Warsaw46670a21998-10-01 21:40:48 +000035 #
36 # create the canvas which holds everything, and its scrollbar
37 #
Barry Warsaw399f10c1998-10-01 22:03:25 +000038 frame = self.__frame = Frame(root)
39 frame.pack()
Barry Warsawb44056e1998-10-02 15:52:10 +000040 canvas = self.__canvas = Canvas(frame, width=160, height=300,
41 borderwidth=2, relief=SUNKEN)
Barry Warsaw399f10c1998-10-01 22:03:25 +000042 self.__scrollbar = Scrollbar(frame)
Barry Warsaw46670a21998-10-01 21:40:48 +000043 self.__scrollbar.pack(fill=Y, side=RIGHT)
44 canvas.pack(fill=BOTH, expand=1)
45 canvas.configure(yscrollcommand=(self.__scrollbar, 'set'))
46 self.__scrollbar.configure(command=(canvas, 'yview'))
47 #
48 # create all the buttons
49 colordb = switchboard.colordb()
50 row = 0
Barry Warsaw46670a21998-10-01 21:40:48 +000051 widest = 0
52 bboxes = self.__bboxes = []
Barry Warsawb44056e1998-10-02 15:52:10 +000053 for name in colordb.unique_names():
Barry Warsaw46670a21998-10-01 21:40:48 +000054 exactcolor = ColorDB.triplet_to_rrggbb(colordb.find_byname(name))
55 canvas.create_rectangle(5, row*20 + 5,
56 20, row*20 + 20,
57 fill=exactcolor)
58 textid = canvas.create_text(25, row*20 + 13,
59 text=name,
60 anchor=W)
61 x1, y1, textend, y2 = canvas.bbox(textid)
62 boxid = canvas.create_rectangle(3, row*20+3,
63 textend+3, row*20 + 23,
64 outline='',
65 tags=(exactcolor,))
Barry Warsaw399f10c1998-10-01 22:03:25 +000066 canvas.bind('<ButtonRelease>', self.__onrelease)
Barry Warsaw46670a21998-10-01 21:40:48 +000067 bboxes.append(boxid)
68 if textend+3 > widest:
69 widest = textend+3
70 row = row + 1
71 canvheight = (row-1)*20 + 25
72 canvas.config(scrollregion=(0, 0, 150, canvheight))
73 for box in bboxes:
74 x1, y1, x2, y2 = canvas.coords(box)
75 canvas.coords(box, x1, y1, widest, y2)
Barry Warsawb44056e1998-10-02 15:52:10 +000076 #
77 # Update on click
78 self.__uoc = BooleanVar()
79 self.__uoc.set(1)
80 self.__uocbtn = Checkbutton(root,
81 text='Update on Click',
Barry Warsaw56af4441998-10-06 18:56:31 +000082 variable=self.__uoc,
83 command=self.__toggleupdate)
Barry Warsawb44056e1998-10-02 15:52:10 +000084 self.__uocbtn.pack(expand=1, fill=BOTH)
85 #
86 # alias list
87 self.__alabel = Label(root, text='Aliases:')
88 self.__alabel.pack()
89 self.__aliases = Listbox(root, height=5,
90 selectmode=BROWSE)
91 self.__aliases.pack(expand=1, fill=BOTH)
Barry Warsaw46670a21998-10-01 21:40:48 +000092
Barry Warsawb55c4ad1998-10-02 14:34:03 +000093 def __onrelease(self, event=None):
Barry Warsaw46670a21998-10-01 21:40:48 +000094 canvas = self.__canvas
95 # find the current box
96 x = canvas.canvasx(event.x)
97 y = canvas.canvasy(event.y)
98 ids = canvas.find_overlapping(x, y, x, y)
99 for boxid in ids:
100 if boxid in self.__bboxes:
101 break
102 else:
Barry Warsaw399f10c1998-10-01 22:03:25 +0000103## print 'No box found!'
Barry Warsaw46670a21998-10-01 21:40:48 +0000104 return
Barry Warsawb55c4ad1998-10-02 14:34:03 +0000105 tags = self.__canvas.gettags(boxid)
106 for t in tags:
107 if t[0] == '#':
108 break
Barry Warsaw46670a21998-10-01 21:40:48 +0000109 else:
Barry Warsawb55c4ad1998-10-02 14:34:03 +0000110## print 'No color tag found!'
111 return
112 red, green, blue = ColorDB.rrggbb_to_triplet(t)
Barry Warsawb44056e1998-10-02 15:52:10 +0000113 self.__dontcenter = 1
114 if self.__uoc.get():
115 self.__sb.update_views(red, green, blue)
116 else:
117 self.update_yourself(red, green, blue)
Barry Warsaw56af4441998-10-06 18:56:31 +0000118 self.__red, self.__green, self.__blue = red, green, blue
119
120 def __toggleupdate(self, event=None):
121 if self.__uoc.get():
122 self.__sb.update_views(self.__red, self.__green, self.__blue)
Barry Warsaw399f10c1998-10-01 22:03:25 +0000123
Barry Warsaw46670a21998-10-01 21:40:48 +0000124 def __quit(self, event=None):
Barry Warsaw45c8d341998-10-06 19:48:35 +0000125 self.__root.quit()
Barry Warsaw46670a21998-10-01 21:40:48 +0000126
127 def __withdraw(self, event=None):
128 self.__root.withdraw()
129
130 def deiconify(self, event=None):
131 self.__root.deiconify()
132
133 def update_yourself(self, red, green, blue):
Barry Warsawb55c4ad1998-10-02 14:34:03 +0000134 canvas = self.__canvas
135 # turn off the last box
136 if self.__lastbox:
137 canvas.itemconfigure(self.__lastbox, outline='')
138 # turn on the current box
139 colortag = ColorDB.triplet_to_rrggbb((red, green, blue))
140 canvas.itemconfigure(colortag, outline='black')
141 self.__lastbox = colortag
Barry Warsawb44056e1998-10-02 15:52:10 +0000142 # fill the aliases
143 self.__aliases.delete(0, END)
144 try:
145 aliases = self.__sb.colordb().aliases_of(red, green, blue)[1:]
146 except ColorDB.BadColor:
147 self.__aliases.insert(END, '<no matching color>')
148 return
149 if not aliases:
150 self.__aliases.insert(END, '<no aliases>')
151 else:
152 for name in aliases:
153 self.__aliases.insert(END, name)
154 # maybe scroll the canvas so that the item is visible
155 if self.__dontcenter:
156 self.__dontcenter = 0
157 else:
158 height = canvas['height']
159 ig, ig, ig, y1 = canvas.coords(colortag)
160 ig, ig, ig, y2 = canvas.coords(self.__bboxes[-1])
161 h = int(canvas['height']) * 0.5
162 canvas.yview('moveto', (y1-h) / y2)