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

Skip to content

Commit 9d882bc

Browse files
committed
Initial revision
1 parent f64b405 commit 9d882bc

1 file changed

Lines changed: 135 additions & 0 deletions

File tree

Tools/pynche/StripViewer.py

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
from Tkinter import *
2+
import Pmw
3+
import ColorDB
4+
5+
class LeftArrow:
6+
_ARROWWIDTH = 30
7+
_ARROWHEIGHT = 15
8+
_YOFFSET = 13
9+
_TEXTYOFFSET = 1
10+
_TAG = ('leftarrow',)
11+
12+
def __init__(self, canvas, x):
13+
self._canvas = canvas
14+
self.__arrow, self.__text = self._create(x)
15+
self.move_to(x)
16+
17+
def _create(self, x):
18+
arrow = self._canvas.create_line(
19+
x, self._ARROWHEIGHT + self._YOFFSET,
20+
x, self._YOFFSET,
21+
x + self._ARROWWIDTH, self._YOFFSET,
22+
arrow='first',
23+
width=3.0,
24+
tags=self._TAG)
25+
text = self._canvas.create_text(
26+
x + self._ARROWWIDTH + 13,
27+
self._ARROWHEIGHT - self._TEXTYOFFSET,
28+
text='128')
29+
return arrow, text
30+
31+
def _x(self):
32+
coords = self._canvas.coords(self._TAG)
33+
assert coords
34+
return coords[0]
35+
36+
def move_to(self, x):
37+
deltax = x - self._x()
38+
self._canvas.move(self._TAG, deltax, 0)
39+
40+
41+
class RightArrow(LeftArrow):
42+
_TAG = ('rightarrow',)
43+
44+
def _create(self, x):
45+
arrow = self._canvas.create_line(
46+
x, self._YOFFSET,
47+
x + self._ARROWWIDTH, self._YOFFSET,
48+
x + self._ARROWWIDTH, self._ARROWHEIGHT + self._YOFFSET,
49+
arrow='last',
50+
width=3.0,
51+
tags=self._TAG)
52+
text = self._canvas.create_text(
53+
x - self._ARROWWIDTH - 20, # TBD: kludge
54+
self._ARROWHEIGHT - self._TEXTYOFFSET,
55+
text='128')
56+
return arrow, text
57+
58+
def _x(self):
59+
coords = self._canvas.bbox(self._TAG)
60+
assert coords
61+
return coords[2] - 6 # TBD: kludge
62+
63+
64+
65+
class StripWidget(Pmw.MegaWidget):
66+
_CHIPHEIGHT = 50
67+
_CHIPWIDTH = 10
68+
_NUMCHIPS = 40
69+
70+
def __init__(self, parent=None, **kw):
71+
options = (('color', (128, 128, 128), self.__set_color),
72+
('delegate', None, None),
73+
('chipwidth', self._CHIPWIDTH, Pmw.INITOPT),
74+
('chipheight', self._CHIPHEIGHT, Pmw.INITOPT),
75+
('numchips', self._NUMCHIPS, Pmw.INITOPT),
76+
('generator', None, Pmw.INITOPT),
77+
)
78+
self.defineoptions(kw, options)
79+
80+
Pmw.MegaWidget.__init__(self, parent)
81+
interiorarg = (self.interior(),)
82+
83+
# group component contains a convas containing a bunch of objects
84+
# (moveable arrow + text label, relief'd rectangle color chips)
85+
chipwidth = self.__chipwidth = self['chipwidth']
86+
chipheight = self.__chipheight = self['chipheight']
87+
numchips = self.__numchips = self['numchips']
88+
89+
canvaswidth = numchips * (chipwidth + 1)
90+
canvasheight = chipheight + 35
91+
92+
self.__canvas = Canvas(
93+
parent,
94+
width=canvaswidth,
95+
height=canvasheight)
96+
self.__canvas.pack()
97+
98+
# create the color strip
99+
chips = self.__chips = []
100+
x = 1
101+
y = 30
102+
for c in range(self.__numchips):
103+
rect = self.__canvas.create_rectangle(
104+
x, y, x+chipwidth, y+chipheight,
105+
fill='grey', outline='grey')
106+
107+
x = x + chipwidth + 1 # for outline
108+
chips.append(rect)
109+
110+
# create the arrow and text item
111+
chipx = self.__arrow_x(0)
112+
self.__leftarrow = LeftArrow(self.__canvas, chipx)
113+
114+
chipx = self.__arrow_x(len(chips) - 1)
115+
self.__rightarrow = RightArrow(self.__canvas, chipx)
116+
117+
self.__generator = self['generator']
118+
self.initialiseoptions(StripWidget)
119+
120+
def __set_color(self):
121+
rgbtuple = self['color']
122+
if self.__generator:
123+
i = 0
124+
for t in self.__generator(self.__numchips, rgbtuple):
125+
rrggbb = ColorDB.triplet_to_rrggbb(t)
126+
self.__canvas.itemconfigure(self.__chips[i],
127+
fill=rrggbb,
128+
outline=rrggbb)
129+
i = i + 1
130+
131+
def __arrow_x(self, chipnum):
132+
coords = self.__canvas.coords(self.__chips[chipnum])
133+
assert coords
134+
x0, y0, x1, y1 = coords
135+
return (x1 + x0) / 2.0

0 commit comments

Comments
 (0)