Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f67a50c

Browse files
committed
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.
1 parent 8d3e5ee commit f67a50c

1 file changed

Lines changed: 81 additions & 51 deletions

File tree

Tools/pynche/StripViewer.py

Lines changed: 81 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
import string
12
from Tkinter import *
23
import Pmw
34
import ColorDB
45

6+
57
class LeftArrow:
68
_ARROWWIDTH = 30
79
_ARROWHEIGHT = 15
@@ -73,14 +75,14 @@ class StripWidget(Pmw.MegaWidget):
7375
_NUMCHIPS = 40
7476

7577
def __init__(self, parent=None, **kw):
76-
options = (('color', (128, 128, 128), self.__set_color),
77-
('delegate', None, None),
78-
('chipwidth', self._CHIPWIDTH, Pmw.INITOPT),
78+
options = (('color', (128, 128, 128), self.__set_color),
79+
('delegate', None, self.__set_delegate),
80+
('chipwidth', self._CHIPWIDTH, Pmw.INITOPT),
7981
('chipheight', self._CHIPHEIGHT, Pmw.INITOPT),
80-
('numchips', self._NUMCHIPS, Pmw.INITOPT),
81-
('generator', None, Pmw.INITOPT),
82-
('axis', None, Pmw.INITOPT),
83-
('label', '', Pmw.INITOPT),
82+
('numchips', self._NUMCHIPS, Pmw.INITOPT),
83+
('generator', None, Pmw.INITOPT),
84+
('axis', None, Pmw.INITOPT),
85+
('label', '', Pmw.INITOPT),
8486
)
8587
self.defineoptions(kw, options)
8688

@@ -116,12 +118,13 @@ def __init__(self, parent=None, **kw):
116118
x = 1
117119
y = 30
118120
for c in range(self.__numchips):
121+
color = 'grey'
119122
rect = self.__canvas.create_rectangle(
120123
x, y, x+chipwidth, y+chipheight,
121-
fill='grey', outline='grey')
124+
fill=color, outline=color)
122125

123126
x = x + chipwidth + 1 # for outline
124-
chips.append(rect)
127+
chips.append(color)
125128

126129
# create the string tag
127130
self.__label = self.__canvas.create_text(
@@ -140,24 +143,37 @@ def __init__(self, parent=None, **kw):
140143
self.__axis = self['axis']
141144
assert self.__axis in (0, 1, 2)
142145
self.initialiseoptions(StripWidget)
146+
self.__delegate = self['delegate']
143147

144148
def __set_color(self):
145149
rgbtuple = self['color']
146150
self.set_color(self, rgbtuple)
147151

148152
def __arrow_x(self, chipnum):
149-
coords = self.__canvas.coords(self.__chips[chipnum])
153+
coords = self.__canvas.coords(chipnum+1)
150154
assert coords
151155
x0, y0, x1, y1 = coords
152156
return (x1 + x0) / 2.0
153157

154158
def __select_chip(self, event=None):
155-
chip = self.__canvas.find_closest(event.x, event.y)
156-
delegate = self['delegate']
157-
if chip and delegate:
158-
color = self.__canvas.itemcget(chip, 'fill')
159+
chip = self.__canvas.find_closest(event.x, event.y)[0]
160+
if chip and self.__delegate:
161+
color = self.__chips[chip-1]
159162
rgbtuple = ColorDB.rrggbb_to_triplet(color)
160-
delegate.set_color(self, rgbtuple)
163+
self.__delegate.set_color(self, rgbtuple)
164+
165+
## import profile
166+
## import pstats
167+
## import tempfile
168+
## statfile = tempfile.mktemp()
169+
## p = profile.Profile()
170+
## p.runcall(self.__delegate.set_color, self, rgbtuple)
171+
## p.dump_stats(statfile)
172+
## s = pstats.Stats(statfile)
173+
## s.strip_dirs().sort_stats('time').print_stats(10)
174+
175+
def __set_delegate(self):
176+
self.__delegate = self['delegate']
161177

162178

163179
#
@@ -166,39 +182,53 @@ def __select_chip(self, event=None):
166182

167183
def set_color(self, obj, rgbtuple):
168184
red, green, blue = rgbtuple
169-
if self.__generator:
170-
i = 0
171-
chip = 0
172-
for t in self.__generator(self.__numchips, rgbtuple):
173-
rrggbb = ColorDB.triplet_to_rrggbb(t)
174-
self.__canvas.itemconfigure(self.__chips[i],
175-
fill=rrggbb,
176-
outline=rrggbb)
177-
tred, tgreen, tblue = t
178-
if tred <= red and tgreen <= green and tblue <= blue:
179-
chip = i
180-
i = i + 1
181-
# get the arrow's text
182-
coloraxis = rgbtuple[self.__axis]
183-
text = repr(coloraxis)
184-
185-
# move the arrow, and set it's text
186-
if coloraxis <= 128:
187-
# use the left chip
188-
self.__leftarrow.set_text(text)
189-
self.__leftarrow.move_to(self.__arrow_x(chip))
190-
self.__rightarrow.move_to(-100)
191-
else:
192-
# use the right chip
193-
self.__rightarrow.set_text(text)
194-
self.__rightarrow.move_to(self.__arrow_x(chip))
195-
self.__leftarrow.move_to(-100)
196-
# and set the chip's outline
197-
pmwrgb = ColorDB.triplet_to_pmwrgb(rgbtuple)
198-
b = Pmw.Color.rgb2brightness(pmwrgb)
199-
if b <= 0.5:
200-
outline = 'white'
201-
else:
202-
outline = 'black'
203-
self.__canvas.itemconfigure(self.__chips[chip],
204-
outline=outline)
185+
assert self.__generator
186+
i = 1
187+
chip = 0
188+
chips = self.__chips = []
189+
tclcmd = []
190+
for t in self.__generator(self.__numchips, rgbtuple):
191+
rrggbb = ColorDB.triplet_to_rrggbb(t)
192+
chips.append(rrggbb)
193+
## self.__canvas.itemconfigure(i,
194+
## fill=rrggbb,
195+
## outline=rrggbb)
196+
tclcmd.append(self.__canvas._w)
197+
tclcmd.append('itemconfigure')
198+
tclcmd.append(`i`)
199+
tclcmd.append('-fill')
200+
tclcmd.append(rrggbb)
201+
tclcmd.append('-outline')
202+
tclcmd.append(rrggbb)
203+
tclcmd.append('\n')
204+
tred, tgreen, tblue = t
205+
if tred <= red and tgreen <= green and tblue <= blue:
206+
chip = i
207+
i = i + 1
208+
# call the raw tcl script
209+
script = string.join(tclcmd, ' ')
210+
self.__canvas.tk.eval(script)
211+
212+
# get the arrow's text
213+
coloraxis = rgbtuple[self.__axis]
214+
text = repr(coloraxis)
215+
216+
# move the arrow, and set it's text
217+
if coloraxis <= 128:
218+
# use the left chip
219+
self.__leftarrow.set_text(text)
220+
self.__leftarrow.move_to(self.__arrow_x(chip-1))
221+
self.__rightarrow.move_to(-100)
222+
else:
223+
# use the right chip
224+
self.__rightarrow.set_text(text)
225+
self.__rightarrow.move_to(self.__arrow_x(chip-1))
226+
self.__leftarrow.move_to(-100)
227+
# and set the chip's outline
228+
pmwrgb = ColorDB.triplet_to_pmwrgb(rgbtuple)
229+
b = Pmw.Color.rgb2brightness(pmwrgb)
230+
if b <= 0.5:
231+
outline = 'white'
232+
else:
233+
outline = 'black'
234+
self.__canvas.itemconfigure(chip, outline=outline)

0 commit comments

Comments
 (0)