6 # Load this script into the Tcl interpreter and call it in
7 # StripWidget.set_color(). This is about as fast as it can be with the
8 # current _tkinter.c interface, which doesn't support Tcl Objects.
10 proc setcolor {canv colors} {
13 $canv itemconfigure $i -fill $c -outline $c
33 def __init__(self
, canvas
, x
):
35 self
.__arrow
, self
.__text
= self
._create
(x
)
39 arrow
= self
._canvas
.create_line(
40 x
, self
._ARROWHEIGHT
+ self
._YOFFSET
,
42 x
+ self
._ARROWWIDTH
, self
._YOFFSET
,
46 text
= self
._canvas
.create_text(
47 x
+ self
._ARROWWIDTH
+ 13,
48 self
._ARROWHEIGHT
- self
._TEXTYOFFSET
,
54 coords
= self
._canvas
.coords(self
._TAG
)
59 deltax
= x
- self
._x
()
60 self
._canvas
.move(self
._TAG
, deltax
, 0)
62 def set_text(self
, text
):
63 self
._canvas
.itemconfigure(self
.__text
, text
=text
)
66 class RightArrow(LeftArrow
):
67 _TAG
= ('rightarrow',)
70 arrow
= self
._canvas
.create_line(
72 x
+ self
._ARROWWIDTH
, self
._YOFFSET
,
73 x
+ self
._ARROWWIDTH
, self
._ARROWHEIGHT
+ self
._YOFFSET
,
77 text
= self
._canvas
.create_text(
78 x
- self
._ARROWWIDTH
+ 15, # TBD: kludge
79 self
._ARROWHEIGHT
- self
._TEXTYOFFSET
,
85 coords
= self
._canvas
.bbox(self
._TAG
)
87 return coords
[2] - 6 # TBD: kludge
91 class StripWidget(Pmw
.MegaWidget
):
96 def __init__(self
, parent
=None, **kw
):
97 self
.__lastchip
= None
99 options
= (('color', (128, 128, 128), self
.__set
_color
),
100 ('delegate', None, self
.__set
_delegate
),
101 ('chipwidth', self
._CHIPWIDTH
, Pmw
.INITOPT
),
102 ('chipheight', self
._CHIPHEIGHT
, Pmw
.INITOPT
),
103 ('numchips', self
._NUMCHIPS
, Pmw
.INITOPT
),
104 ('generator', None, Pmw
.INITOPT
),
105 ('axis', None, Pmw
.INITOPT
),
106 ('label', '', Pmw
.INITOPT
),
108 self
.defineoptions(kw
, options
)
110 Pmw
.MegaWidget
.__init
__(self
, parent
)
111 interiorarg
= (self
.interior(),)
113 # group component contains a convas containing a bunch of objects
114 # (moveable arrow + text label, relief'd rectangle color chips)
115 chipwidth
= self
.__chipwidth
= self
['chipwidth']
116 chipheight
= self
.__chipheight
= self
['chipheight']
117 numchips
= self
.__numchips
= self
['numchips']
119 canvaswidth
= numchips
* (chipwidth
+ 1)
120 canvasheight
= chipheight
+ 43 # TBD: Kludge
122 # create the canvas and pack it
123 canvas
= self
.__canvas
= Canvas(
132 canvas
.bind('<ButtonPress-1>', self
.__select
_chip
)
133 canvas
.bind('<ButtonRelease-1>', self
.__select
_chip
)
134 canvas
.bind('<B1-Motion>', self
.__select
_chip
)
136 # Load a proc into the Tcl interpreter. This is used in the
137 # set_color() method to speed up setting the chip colors.
138 canvas
.tk
.eval(TCLPROC
)
140 # create the color strip
141 chips
= self
.__chips
= []
145 for c
in range(self
.__numchips
):
147 rect
= canvas
.create_rectangle(
148 x
, y
, x
+chipwidth
, y
+chipheight
,
149 fill
=color
, outline
=color
,
152 x
= x
+ chipwidth
+ 1 # for outline
155 # create the string tag
156 self
.__label
= canvas
.create_text(
157 3, y
+ chipheight
+ 8,
161 # create the arrow and text item
162 chipx
= self
.__arrow
_x
(0)
163 self
.__leftarrow
= LeftArrow(canvas
, chipx
)
165 chipx
= self
.__arrow
_x
(len(chips
) - 1)
166 self
.__rightarrow
= RightArrow(canvas
, chipx
)
168 self
.__generator
= self
['generator']
169 self
.__axis
= self
['axis']
170 assert self
.__axis
in (0, 1, 2)
171 self
.initialiseoptions(StripWidget
)
172 self
.__delegate
= self
['delegate']
173 self
.__update
_while
_dragging
= 0
175 def __set_color(self
):
176 rgbtuple
= self
['color']
177 self
.set_color(self
, rgbtuple
)
179 def __arrow_x(self
, chipnum
):
180 coords
= self
.__canvas
.coords(chipnum
+1)
182 x0
, y0
, x1
, y1
= coords
183 return (x1
+ x0
) / 2.0
185 def __select_chip(self
, event
=None):
189 canvas
= self
.__canvas
190 chip
= canvas
.find_overlapping(x
, y
, x
, y
)
191 if chip
and (1 <= chip
[0] <= self
.__numchips
):
192 color
= self
.__chips
[chip
[0]-1]
193 rgbtuple
= ColorDB
.rrggbb_to_triplet(color
)
194 etype
= int(event
.type)
195 if (etype
== BTNUP
or self
.__update
_while
_dragging
):
197 self
.__delegate
.set_color(self
, rgbtuple
)
199 # just track the arrows
200 self
.__trackarrow
(chip
[0], rgbtuple
)
203 def __set_delegate(self
):
204 self
.__delegate
= self
['delegate']
207 def __trackarrow(self
, chip
, rgbtuple
):
208 # invert the last chip
209 if self
.__lastchip
is not None:
210 color
= self
.__canvas
.itemcget(self
.__lastchip
, 'fill')
211 self
.__canvas
.itemconfigure(self
.__lastchip
, outline
=color
)
212 self
.__lastchip
= chip
214 # get the arrow's text
215 coloraxis
= rgbtuple
[self
.__axis
]
216 text
= repr(coloraxis
)
218 # move the arrow, and set it's text
221 self
.__leftarrow
.set_text(text
)
222 self
.__leftarrow
.move_to(self
.__arrow
_x
(chip
-1))
223 self
.__rightarrow
.move_to(-100)
226 self
.__rightarrow
.set_text(text
)
227 self
.__rightarrow
.move_to(self
.__arrow
_x
(chip
-1))
228 self
.__leftarrow
.move_to(-100)
229 # and set the chip's outline
230 pmwrgb
= ColorDB
.triplet_to_pmwrgb(rgbtuple
)
231 b
= Pmw
.Color
.rgb2brightness(pmwrgb
)
236 self
.__canvas
.itemconfigure(chip
, outline
=outline
)
243 def set_color(self
, obj
, rgbtuple
):
244 red
, green
, blue
= rgbtuple
245 assert self
.__generator
248 chips
= self
.__chips
= []
250 tk
= self
.__canvas
.tk
252 for t
in self
.__generator
(self
.__numchips
, rgbtuple
):
253 rrggbb
= ColorDB
.triplet_to_rrggbb(t
)
255 tred
, tgreen
, tblue
= t
256 if tred
<= red
and tgreen
<= green
and tblue
<= blue
:
259 # call the raw tcl script
260 colors
= string
.join(chips
)
261 tk
.eval('setcolor %s {%s}' % (self
.__canvas
._w
, colors
))
263 # move the arrows around
264 self
.__trackarrow
(chip
, rgbtuple
)
266 def set_update_while_dragging(self
, flag
):
267 self
.__update
_while
_dragging
= flag