blob: 5bce412069ef387c7a754a7abcfd0db5ed7b17d7 [file] [log] [blame]
Guido van Rossum36ddc9e1990-10-31 11:24:22 +00001# Module 'StripChart'
2
Guido van Rossum36ddc9e1990-10-31 11:24:22 +00003import rect
Guido van Rossum0c89ec71990-11-05 19:44:31 +00004from Buttons import LabelAppearance, NoReactivity
Guido van Rossum36ddc9e1990-10-31 11:24:22 +00005
Guido van Rossum0c89ec71990-11-05 19:44:31 +00006# A StripChart doesn't really look like a label but it needs a base class.
7# LabelAppearance allows it to be disabled and hilited.
Guido van Rossum36ddc9e1990-10-31 11:24:22 +00008
Guido van Rossumce084481991-12-26 13:06:29 +00009class StripChart(LabelAppearance, NoReactivity):
Guido van Rossum36ddc9e1990-10-31 11:24:22 +000010 #
Guido van Rossum0c89ec71990-11-05 19:44:31 +000011 def define(self, (parent, scale)):
12 self.parent = parent
13 parent.addchild(self)
14 self.init_appearance()
Guido van Rossum36ddc9e1990-10-31 11:24:22 +000015 self.init_reactivity()
Guido van Rossum36ddc9e1990-10-31 11:24:22 +000016 self.ydata = []
17 self.scale = scale
18 self.resetbounds()
19 return self
20 #
Guido van Rossum7912b001991-04-21 19:27:28 +000021 def destroy(self):
22 self.parent = 0
23 #
Guido van Rossum36ddc9e1990-10-31 11:24:22 +000024 def setbounds(self, bounds):
25 LabelAppearance.setbounds(self, bounds)
26 self.resetbounds()
27 #
28 def resetbounds(self):
29 (left, top), (right, bottom) = self.bounds
30 self.width = right-left
31 self.height = bottom-top
32 excess = len(self.ydata) - self.width
33 if excess > 0:
34 del self.ydata[:excess]
35 elif excess < 0:
36 while len(self.ydata) < self.width:
37 self.ydata.insert(0, 0)
38 #
39 def append(self, y):
40 self.ydata.append(y)
41 excess = len(self.ydata) - self.width
42 if excess > 0:
43 del self.ydata[:excess]
Guido van Rossum0c89ec71990-11-05 19:44:31 +000044 if self.bounds <> rect.empty:
45 self.parent.scroll(self.bounds, (-excess, 0))
46 if self.bounds <> rect.empty:
Guido van Rossum36ddc9e1990-10-31 11:24:22 +000047 (left, top), (right, bottom) = self.bounds
48 i = len(self.ydata)
49 area = (left+i-1, top), (left+i, bottom)
Guido van Rossum0c89ec71990-11-05 19:44:31 +000050 self.draw(self.parent.begindrawing(), area)
Guido van Rossum36ddc9e1990-10-31 11:24:22 +000051 #
52 def draw(self, (d, area)):
Guido van Rossum36ddc9e1990-10-31 11:24:22 +000053 area = rect.intersect(area, self.bounds)
Guido van Rossumbdfcfcc1992-01-01 19:35:13 +000054 if area == rect.empty:
Guido van Rossum36ddc9e1990-10-31 11:24:22 +000055 return
56 d.cliprect(area)
57 d.erase(self.bounds)
58 (a_left, a_top), (a_right, a_bottom) = area
59 (left, top), (right, bottom) = self.bounds
60 height = bottom - top
61 i1 = a_left - left
62 i2 = a_right - left
63 for i in range(max(0, i1), min(len(self.ydata), i2)):
64 split = bottom-self.ydata[i]*height/self.scale
65 d.paint((left+i, split), (left+i+1, bottom))
66 if not self.enabled:
67 self.flipenable(d)
68 if self.hilited:
69 self.fliphilite(d)
70 d.noclip()