Several optimizations:

self.__chips now contains the list of rgbtuple values for the
    chips named i - 1 (Tkinter counts from 1, we count from zero).
    The chip number was just the index + 1.  This means color lookup
    need not do an itemcget(), it can just index into __chips.

    instead of calling __canvas.itemconfigure(), we glom up a huge Tcl
    script and call tk.eval() directly.  Actually we do many appends
    to a Python list, then string.join() them together into one huge
    string.  This reduces the overhead of Tkinter but making one fast
    call to Tcl.
This commit is contained in:
Barry Warsaw 1998-02-18 00:05:59 +00:00
parent 8d3e5ee552
commit f67a50c2e1
1 changed files with 80 additions and 50 deletions

View File

@ -1,7 +1,9 @@
import string
from Tkinter import *
import Pmw
import ColorDB
class LeftArrow:
_ARROWWIDTH = 30
_ARROWHEIGHT = 15
@ -73,14 +75,14 @@ class StripWidget(Pmw.MegaWidget):
_NUMCHIPS = 40
def __init__(self, parent=None, **kw):
options = (('color', (128, 128, 128), self.__set_color),
('delegate', None, None),
('chipwidth', self._CHIPWIDTH, Pmw.INITOPT),
options = (('color', (128, 128, 128), self.__set_color),
('delegate', None, self.__set_delegate),
('chipwidth', self._CHIPWIDTH, Pmw.INITOPT),
('chipheight', self._CHIPHEIGHT, Pmw.INITOPT),
('numchips', self._NUMCHIPS, Pmw.INITOPT),
('generator', None, Pmw.INITOPT),
('axis', None, Pmw.INITOPT),
('label', '', Pmw.INITOPT),
('numchips', self._NUMCHIPS, Pmw.INITOPT),
('generator', None, Pmw.INITOPT),
('axis', None, Pmw.INITOPT),
('label', '', Pmw.INITOPT),
)
self.defineoptions(kw, options)
@ -116,12 +118,13 @@ class StripWidget(Pmw.MegaWidget):
x = 1
y = 30
for c in range(self.__numchips):
color = 'grey'
rect = self.__canvas.create_rectangle(
x, y, x+chipwidth, y+chipheight,
fill='grey', outline='grey')
fill=color, outline=color)
x = x + chipwidth + 1 # for outline
chips.append(rect)
chips.append(color)
# create the string tag
self.__label = self.__canvas.create_text(
@ -140,24 +143,37 @@ class StripWidget(Pmw.MegaWidget):
self.__axis = self['axis']
assert self.__axis in (0, 1, 2)
self.initialiseoptions(StripWidget)
self.__delegate = self['delegate']
def __set_color(self):
rgbtuple = self['color']
self.set_color(self, rgbtuple)
def __arrow_x(self, chipnum):
coords = self.__canvas.coords(self.__chips[chipnum])
coords = self.__canvas.coords(chipnum+1)
assert coords
x0, y0, x1, y1 = coords
return (x1 + x0) / 2.0
def __select_chip(self, event=None):
chip = self.__canvas.find_closest(event.x, event.y)
delegate = self['delegate']
if chip and delegate:
color = self.__canvas.itemcget(chip, 'fill')
chip = self.__canvas.find_closest(event.x, event.y)[0]
if chip and self.__delegate:
color = self.__chips[chip-1]
rgbtuple = ColorDB.rrggbb_to_triplet(color)
delegate.set_color(self, rgbtuple)
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)
def __set_delegate(self):
self.__delegate = self['delegate']
#
@ -166,39 +182,53 @@ class StripWidget(Pmw.MegaWidget):
def set_color(self, obj, rgbtuple):
red, green, blue = rgbtuple
if self.__generator:
i = 0
chip = 0
for t in self.__generator(self.__numchips, rgbtuple):
rrggbb = ColorDB.triplet_to_rrggbb(t)
self.__canvas.itemconfigure(self.__chips[i],
fill=rrggbb,
outline=rrggbb)
tred, tgreen, tblue = t
if tred <= red and tgreen <= green and tblue <= blue:
chip = i
i = i + 1
# get the arrow's text
coloraxis = rgbtuple[self.__axis]
text = repr(coloraxis)
assert self.__generator
i = 1
chip = 0
chips = self.__chips = []
tclcmd = []
for t in self.__generator(self.__numchips, rgbtuple):
rrggbb = ColorDB.triplet_to_rrggbb(t)
chips.append(rrggbb)
## self.__canvas.itemconfigure(i,
## fill=rrggbb,
## outline=rrggbb)
tclcmd.append(self.__canvas._w)
tclcmd.append('itemconfigure')
tclcmd.append(`i`)
tclcmd.append('-fill')
tclcmd.append(rrggbb)
tclcmd.append('-outline')
tclcmd.append(rrggbb)
tclcmd.append('\n')
tred, tgreen, tblue = t
if tred <= red and tgreen <= green and tblue <= blue:
chip = i
i = i + 1
# call the raw tcl script
script = string.join(tclcmd, ' ')
self.__canvas.tk.eval(script)
# 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))
self.__rightarrow.move_to(-100)
else:
# use the right chip
self.__rightarrow.set_text(text)
self.__rightarrow.move_to(self.__arrow_x(chip))
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(self.__chips[chip],
outline=outline)
# 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)