1998-02-17 20:05:59 -04:00
|
|
|
|
import string
|
1998-02-12 15:51:57 -04:00
|
|
|
|
from Tkinter import *
|
|
|
|
|
import Pmw
|
|
|
|
|
import ColorDB
|
|
|
|
|
|
1998-02-18 12:22:22 -04:00
|
|
|
|
# Load this script into the Tcl interpreter and call it in
|
|
|
|
|
# StripWidget.set_color(). This is about as fast as it can be with the
|
|
|
|
|
# current _tkinter.c interface, which doesn't support Tcl Objects.
|
|
|
|
|
TCLPROC = '''\
|
|
|
|
|
proc setcolor {canv colors} {
|
|
|
|
|
set i 1
|
|
|
|
|
foreach c $colors {
|
|
|
|
|
$canv itemconfigure $i -fill $c -outline $c
|
|
|
|
|
incr i
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
'''
|
|
|
|
|
|
|
|
|
|
|
1998-02-17 20:05:59 -04:00
|
|
|
|
|
1998-02-12 15:51:57 -04:00
|
|
|
|
class LeftArrow:
|
|
|
|
|
_ARROWWIDTH = 30
|
|
|
|
|
_ARROWHEIGHT = 15
|
|
|
|
|
_YOFFSET = 13
|
|
|
|
|
_TEXTYOFFSET = 1
|
|
|
|
|
_TAG = ('leftarrow',)
|
|
|
|
|
|
|
|
|
|
def __init__(self, canvas, x):
|
|
|
|
|
self._canvas = canvas
|
|
|
|
|
self.__arrow, self.__text = self._create(x)
|
|
|
|
|
self.move_to(x)
|
|
|
|
|
|
|
|
|
|
def _create(self, x):
|
|
|
|
|
arrow = self._canvas.create_line(
|
|
|
|
|
x, self._ARROWHEIGHT + self._YOFFSET,
|
|
|
|
|
x, self._YOFFSET,
|
|
|
|
|
x + self._ARROWWIDTH, self._YOFFSET,
|
|
|
|
|
arrow='first',
|
|
|
|
|
width=3.0,
|
|
|
|
|
tags=self._TAG)
|
|
|
|
|
text = self._canvas.create_text(
|
|
|
|
|
x + self._ARROWWIDTH + 13,
|
|
|
|
|
self._ARROWHEIGHT - self._TEXTYOFFSET,
|
1998-02-13 17:28:47 -04:00
|
|
|
|
tags=self._TAG,
|
1998-02-12 15:51:57 -04:00
|
|
|
|
text='128')
|
|
|
|
|
return arrow, text
|
|
|
|
|
|
|
|
|
|
def _x(self):
|
|
|
|
|
coords = self._canvas.coords(self._TAG)
|
|
|
|
|
assert coords
|
|
|
|
|
return coords[0]
|
|
|
|
|
|
|
|
|
|
def move_to(self, x):
|
|
|
|
|
deltax = x - self._x()
|
|
|
|
|
self._canvas.move(self._TAG, deltax, 0)
|
|
|
|
|
|
1998-02-13 17:28:47 -04:00
|
|
|
|
def set_text(self, text):
|
|
|
|
|
self._canvas.itemconfigure(self.__text, text=text)
|
|
|
|
|
|
1998-02-12 15:51:57 -04:00
|
|
|
|
|
|
|
|
|
class RightArrow(LeftArrow):
|
|
|
|
|
_TAG = ('rightarrow',)
|
|
|
|
|
|
|
|
|
|
def _create(self, x):
|
|
|
|
|
arrow = self._canvas.create_line(
|
|
|
|
|
x, self._YOFFSET,
|
|
|
|
|
x + self._ARROWWIDTH, self._YOFFSET,
|
|
|
|
|
x + self._ARROWWIDTH, self._ARROWHEIGHT + self._YOFFSET,
|
|
|
|
|
arrow='last',
|
|
|
|
|
width=3.0,
|
|
|
|
|
tags=self._TAG)
|
|
|
|
|
text = self._canvas.create_text(
|
1998-02-13 17:28:47 -04:00
|
|
|
|
x - self._ARROWWIDTH + 15, # TBD: kludge
|
1998-02-12 15:51:57 -04:00
|
|
|
|
self._ARROWHEIGHT - self._TEXTYOFFSET,
|
1998-02-13 17:28:47 -04:00
|
|
|
|
text='128',
|
|
|
|
|
tags=self._TAG)
|
1998-02-12 15:51:57 -04:00
|
|
|
|
return arrow, text
|
|
|
|
|
|
|
|
|
|
def _x(self):
|
|
|
|
|
coords = self._canvas.bbox(self._TAG)
|
|
|
|
|
assert coords
|
|
|
|
|
return coords[2] - 6 # TBD: kludge
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class StripWidget(Pmw.MegaWidget):
|
|
|
|
|
_CHIPHEIGHT = 50
|
|
|
|
|
_CHIPWIDTH = 10
|
|
|
|
|
_NUMCHIPS = 40
|
|
|
|
|
|
|
|
|
|
def __init__(self, parent=None, **kw):
|
1998-02-17 20:05:59 -04:00
|
|
|
|
options = (('color', (128, 128, 128), self.__set_color),
|
|
|
|
|
('delegate', None, self.__set_delegate),
|
|
|
|
|
('chipwidth', self._CHIPWIDTH, Pmw.INITOPT),
|
1998-02-12 15:51:57 -04:00
|
|
|
|
('chipheight', self._CHIPHEIGHT, Pmw.INITOPT),
|
1998-02-17 20:05:59 -04:00
|
|
|
|
('numchips', self._NUMCHIPS, Pmw.INITOPT),
|
|
|
|
|
('generator', None, Pmw.INITOPT),
|
|
|
|
|
('axis', None, Pmw.INITOPT),
|
|
|
|
|
('label', '', Pmw.INITOPT),
|
1998-02-12 15:51:57 -04:00
|
|
|
|
)
|
|
|
|
|
self.defineoptions(kw, options)
|
|
|
|
|
|
|
|
|
|
Pmw.MegaWidget.__init__(self, parent)
|
|
|
|
|
interiorarg = (self.interior(),)
|
|
|
|
|
|
|
|
|
|
# group component contains a convas containing a bunch of objects
|
|
|
|
|
# (moveable arrow + text label, relief'd rectangle color chips)
|
|
|
|
|
chipwidth = self.__chipwidth = self['chipwidth']
|
|
|
|
|
chipheight = self.__chipheight = self['chipheight']
|
|
|
|
|
numchips = self.__numchips = self['numchips']
|
|
|
|
|
|
|
|
|
|
canvaswidth = numchips * (chipwidth + 1)
|
1998-02-16 23:09:40 -04:00
|
|
|
|
canvasheight = chipheight + 43 # TBD: Kludge
|
1998-02-12 15:51:57 -04:00
|
|
|
|
|
1998-02-16 23:09:40 -04:00
|
|
|
|
# create the canvas and pack it
|
1998-02-18 13:00:24 -04:00
|
|
|
|
canvas = self.__canvas = Canvas(
|
1998-02-12 15:51:57 -04:00
|
|
|
|
parent,
|
|
|
|
|
width=canvaswidth,
|
1998-02-16 23:09:40 -04:00
|
|
|
|
height=canvasheight,
|
|
|
|
|
## borderwidth=2,
|
|
|
|
|
## relief=GROOVE
|
|
|
|
|
)
|
|
|
|
|
|
1998-02-18 13:00:24 -04:00
|
|
|
|
canvas.pack()
|
|
|
|
|
canvas.bind('<ButtonRelease-1>', self.__select_chip)
|
|
|
|
|
canvas.bind('<B1-Motion>', self.__select_chip)
|
1998-02-12 15:51:57 -04:00
|
|
|
|
|
1998-02-18 12:22:22 -04:00
|
|
|
|
# Load a proc into the Tcl interpreter. This is used in the
|
|
|
|
|
# set_color() method to speed up setting the chip colors.
|
1998-02-18 13:00:24 -04:00
|
|
|
|
canvas.tk.eval(TCLPROC)
|
1998-02-18 12:22:22 -04:00
|
|
|
|
|
1998-02-12 15:51:57 -04:00
|
|
|
|
# create the color strip
|
|
|
|
|
chips = self.__chips = []
|
|
|
|
|
x = 1
|
|
|
|
|
y = 30
|
1998-02-18 13:00:24 -04:00
|
|
|
|
tags = ('chip',)
|
1998-02-12 15:51:57 -04:00
|
|
|
|
for c in range(self.__numchips):
|
1998-02-17 20:05:59 -04:00
|
|
|
|
color = 'grey'
|
1998-02-18 13:00:24 -04:00
|
|
|
|
rect = canvas.create_rectangle(
|
1998-02-12 15:51:57 -04:00
|
|
|
|
x, y, x+chipwidth, y+chipheight,
|
1998-02-18 13:00:24 -04:00
|
|
|
|
fill=color, outline=color,
|
|
|
|
|
tags=tags)
|
1998-02-12 15:51:57 -04:00
|
|
|
|
|
|
|
|
|
x = x + chipwidth + 1 # for outline
|
1998-02-17 20:05:59 -04:00
|
|
|
|
chips.append(color)
|
1998-02-12 15:51:57 -04:00
|
|
|
|
|
1998-02-16 23:09:40 -04:00
|
|
|
|
# create the string tag
|
1998-02-18 13:00:24 -04:00
|
|
|
|
self.__label = canvas.create_text(
|
1998-02-16 23:09:40 -04:00
|
|
|
|
3, y + chipheight + 8,
|
|
|
|
|
text=self['label'],
|
|
|
|
|
anchor=W)
|
|
|
|
|
|
1998-02-12 15:51:57 -04:00
|
|
|
|
# create the arrow and text item
|
|
|
|
|
chipx = self.__arrow_x(0)
|
1998-02-18 13:00:24 -04:00
|
|
|
|
self.__leftarrow = LeftArrow(canvas, chipx)
|
1998-02-12 15:51:57 -04:00
|
|
|
|
|
|
|
|
|
chipx = self.__arrow_x(len(chips) - 1)
|
1998-02-18 13:00:24 -04:00
|
|
|
|
self.__rightarrow = RightArrow(canvas, chipx)
|
1998-02-12 15:51:57 -04:00
|
|
|
|
|
|
|
|
|
self.__generator = self['generator']
|
1998-02-13 17:28:47 -04:00
|
|
|
|
self.__axis = self['axis']
|
|
|
|
|
assert self.__axis in (0, 1, 2)
|
1998-02-12 15:51:57 -04:00
|
|
|
|
self.initialiseoptions(StripWidget)
|
1998-02-17 20:05:59 -04:00
|
|
|
|
self.__delegate = self['delegate']
|
1998-02-12 15:51:57 -04:00
|
|
|
|
|
|
|
|
|
def __set_color(self):
|
|
|
|
|
rgbtuple = self['color']
|
1998-02-16 23:09:40 -04:00
|
|
|
|
self.set_color(self, rgbtuple)
|
|
|
|
|
|
|
|
|
|
def __arrow_x(self, chipnum):
|
1998-02-17 20:05:59 -04:00
|
|
|
|
coords = self.__canvas.coords(chipnum+1)
|
1998-02-16 23:09:40 -04:00
|
|
|
|
assert coords
|
|
|
|
|
x0, y0, x1, y1 = coords
|
|
|
|
|
return (x1 + x0) / 2.0
|
|
|
|
|
|
|
|
|
|
def __select_chip(self, event=None):
|
1998-02-18 13:00:24 -04:00
|
|
|
|
if self.__delegate:
|
|
|
|
|
x = event.x
|
|
|
|
|
y = event.y
|
|
|
|
|
canvas = self.__canvas
|
|
|
|
|
chip = canvas.find_overlapping(x, y, x, y)
|
|
|
|
|
if chip and (1 <= chip[0] <= self.__numchips):
|
|
|
|
|
color = self.__chips[chip[0]-1]
|
|
|
|
|
rgbtuple = ColorDB.rrggbb_to_triplet(color)
|
|
|
|
|
|
|
|
|
|
self.__delegate.set_color(self, rgbtuple)
|
|
|
|
|
## import profile
|
|
|
|
|
## import pstats
|
|
|
|
|
## import tempfile
|
|
|
|
|
## statfile = tempfile.mktemp()
|
|
|
|
|
## p = profile.Profile()
|
|
|
|
|
## p.runcall(self.__delegate.set_color, self, rgbtuple)
|
|
|
|
|
## p.dump_stats(statfile)
|
|
|
|
|
## s = pstats.Stats(statfile)
|
|
|
|
|
## s.strip_dirs().sort_stats('time').print_stats(10)
|
1998-02-17 20:05:59 -04:00
|
|
|
|
|
|
|
|
|
def __set_delegate(self):
|
|
|
|
|
self.__delegate = self['delegate']
|
1998-02-16 23:09:40 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#
|
|
|
|
|
# public interface
|
|
|
|
|
#
|
|
|
|
|
|
|
|
|
|
def set_color(self, obj, rgbtuple):
|
1998-02-13 17:28:47 -04:00
|
|
|
|
red, green, blue = rgbtuple
|
1998-02-17 20:05:59 -04:00
|
|
|
|
assert self.__generator
|
|
|
|
|
i = 1
|
|
|
|
|
chip = 0
|
|
|
|
|
chips = self.__chips = []
|
|
|
|
|
tclcmd = []
|
1998-02-18 12:22:22 -04:00
|
|
|
|
tk = self.__canvas.tk
|
1998-02-17 20:05:59 -04:00
|
|
|
|
for t in self.__generator(self.__numchips, rgbtuple):
|
|
|
|
|
rrggbb = ColorDB.triplet_to_rrggbb(t)
|
|
|
|
|
chips.append(rrggbb)
|
|
|
|
|
tred, tgreen, tblue = t
|
|
|
|
|
if tred <= red and tgreen <= green and tblue <= blue:
|
|
|
|
|
chip = i
|
|
|
|
|
i = i + 1
|
|
|
|
|
# call the raw tcl script
|
1998-02-18 12:22:22 -04:00
|
|
|
|
colors = string.join(chips)
|
|
|
|
|
tk.eval('setcolor %s {%s}' % (self.__canvas._w, colors))
|
1998-02-17 20:05:59 -04:00
|
|
|
|
|
|
|
|
|
# get the arrow's text
|
|
|
|
|
coloraxis = rgbtuple[self.__axis]
|
|
|
|
|
text = repr(coloraxis)
|
|
|
|
|
|
|
|
|
|
# move the arrow, and set it's text
|
|
|
|
|
if coloraxis <= 128:
|
|
|
|
|
# use the left chip
|
|
|
|
|
self.__leftarrow.set_text(text)
|
|
|
|
|
self.__leftarrow.move_to(self.__arrow_x(chip-1))
|
|
|
|
|
self.__rightarrow.move_to(-100)
|
|
|
|
|
else:
|
|
|
|
|
# use the right chip
|
|
|
|
|
self.__rightarrow.set_text(text)
|
|
|
|
|
self.__rightarrow.move_to(self.__arrow_x(chip-1))
|
|
|
|
|
self.__leftarrow.move_to(-100)
|
|
|
|
|
# and set the chip's outline
|
|
|
|
|
pmwrgb = ColorDB.triplet_to_pmwrgb(rgbtuple)
|
|
|
|
|
b = Pmw.Color.rgb2brightness(pmwrgb)
|
|
|
|
|
if b <= 0.5:
|
|
|
|
|
outline = 'white'
|
|
|
|
|
else:
|
|
|
|
|
outline = 'black'
|
|
|
|
|
self.__canvas.itemconfigure(chip, outline=outline)
|