Mercurial > code > home > repos > light9
annotate flax/zoomcontrol.py @ 205:3905d3c92aaa
twisted mainloop, more row-change keys, xmlrpc fadesub command on port 8050
author | drewp |
---|---|
date | Sun, 10 Apr 2005 15:03:24 +0000 |
parents | 45b12307c695 |
children |
rev | line source |
---|---|
0 | 1 from __future__ import division |
2 import Tkinter as tk | |
3 from dispatch import dispatcher | |
4 | |
5 class Zoomcontrol(object,tk.Canvas): | |
6 | |
205
3905d3c92aaa
twisted mainloop, more row-change keys, xmlrpc fadesub command on port 8050
drewp
parents:
0
diff
changeset
|
7 mintime=-5 |
3905d3c92aaa
twisted mainloop, more row-change keys, xmlrpc fadesub command on port 8050
drewp
parents:
0
diff
changeset
|
8 |
0 | 9 def maxtime(): |
10 doc = "seconds at the right edge of the bar" | |
11 def fget(self): return self._maxtime | |
12 def fset(self, value): | |
13 self._maxtime = value | |
14 self.updatewidget() | |
15 return locals() | |
16 maxtime = property(**maxtime()) | |
17 | |
18 def start(): | |
19 def fget(self): return self._start | |
205
3905d3c92aaa
twisted mainloop, more row-change keys, xmlrpc fadesub command on port 8050
drewp
parents:
0
diff
changeset
|
20 def fset(self,v): self._start = max(self.mintime,v) |
0 | 21 return locals() |
22 start = property(**start()) | |
23 | |
24 def end(): | |
25 def fget(self): return self._end | |
26 def fset(self,v): self._end = min(self.maxtime,v) | |
27 return locals() | |
28 end = property(**end()) | |
29 | |
30 | |
31 def __init__(self,master,**kw): | |
32 self.maxtime=370 | |
33 self.start=0 | |
34 self.end=20 | |
35 tk.Canvas.__init__(self,master,width=250,height=30, | |
36 relief='raised',bd=1,bg='gray60',**kw) | |
37 self.leftbrack = self.create_line(0,0,0,0,0,0,0,0,width=5) | |
38 self.rightbrack = self.create_line(0,0,0,0,0,0,0,0,width=5) | |
39 self.shade = self.create_rectangle(0,0,0,0,fill='gray70',outline=None) | |
40 self.time = self.create_line(0,0,0,0,fill='red',width=2) | |
41 self.updatewidget() | |
42 self.bind("<Configure>",self.updatewidget) | |
43 | |
205
3905d3c92aaa
twisted mainloop, more row-change keys, xmlrpc fadesub command on port 8050
drewp
parents:
0
diff
changeset
|
44 if 0: |
3905d3c92aaa
twisted mainloop, more row-change keys, xmlrpc fadesub command on port 8050
drewp
parents:
0
diff
changeset
|
45 # works, but you have to stay in the widget while you drag |
3905d3c92aaa
twisted mainloop, more row-change keys, xmlrpc fadesub command on port 8050
drewp
parents:
0
diff
changeset
|
46 self.bind("<ButtonPress-1>",self.press) |
3905d3c92aaa
twisted mainloop, more row-change keys, xmlrpc fadesub command on port 8050
drewp
parents:
0
diff
changeset
|
47 self.tag_bind(self.leftbrack,"<B1-Motion>", |
3905d3c92aaa
twisted mainloop, more row-change keys, xmlrpc fadesub command on port 8050
drewp
parents:
0
diff
changeset
|
48 lambda ev: self.adjust(ev,'start')) |
3905d3c92aaa
twisted mainloop, more row-change keys, xmlrpc fadesub command on port 8050
drewp
parents:
0
diff
changeset
|
49 self.tag_bind(self.rightbrack,"<B1-Motion>", |
3905d3c92aaa
twisted mainloop, more row-change keys, xmlrpc fadesub command on port 8050
drewp
parents:
0
diff
changeset
|
50 lambda ev: self.adjust(ev,'end')) |
3905d3c92aaa
twisted mainloop, more row-change keys, xmlrpc fadesub command on port 8050
drewp
parents:
0
diff
changeset
|
51 self.tag_bind(self.shade,"<B1-Motion>", |
3905d3c92aaa
twisted mainloop, more row-change keys, xmlrpc fadesub command on port 8050
drewp
parents:
0
diff
changeset
|
52 lambda ev: self.adjust(ev,'offset')) |
3905d3c92aaa
twisted mainloop, more row-change keys, xmlrpc fadesub command on port 8050
drewp
parents:
0
diff
changeset
|
53 else: |
3905d3c92aaa
twisted mainloop, more row-change keys, xmlrpc fadesub command on port 8050
drewp
parents:
0
diff
changeset
|
54 # works better |
3905d3c92aaa
twisted mainloop, more row-change keys, xmlrpc fadesub command on port 8050
drewp
parents:
0
diff
changeset
|
55 # bind to buttonpress wasnt working, but Enter is good enough |
3905d3c92aaa
twisted mainloop, more row-change keys, xmlrpc fadesub command on port 8050
drewp
parents:
0
diff
changeset
|
56 self.tag_bind(self.leftbrack,"<Enter>", |
3905d3c92aaa
twisted mainloop, more row-change keys, xmlrpc fadesub command on port 8050
drewp
parents:
0
diff
changeset
|
57 lambda ev: self.press(ev,'start')) |
3905d3c92aaa
twisted mainloop, more row-change keys, xmlrpc fadesub command on port 8050
drewp
parents:
0
diff
changeset
|
58 self.tag_bind(self.shade,"<Enter>", |
3905d3c92aaa
twisted mainloop, more row-change keys, xmlrpc fadesub command on port 8050
drewp
parents:
0
diff
changeset
|
59 lambda ev: self.press(ev,'offset')) |
3905d3c92aaa
twisted mainloop, more row-change keys, xmlrpc fadesub command on port 8050
drewp
parents:
0
diff
changeset
|
60 self.tag_bind(self.rightbrack,"<Enter>", |
3905d3c92aaa
twisted mainloop, more row-change keys, xmlrpc fadesub command on port 8050
drewp
parents:
0
diff
changeset
|
61 lambda ev: self.press(ev,'end')) |
3905d3c92aaa
twisted mainloop, more row-change keys, xmlrpc fadesub command on port 8050
drewp
parents:
0
diff
changeset
|
62 self.bind("<B1-Motion>",self.adjust) |
3905d3c92aaa
twisted mainloop, more row-change keys, xmlrpc fadesub command on port 8050
drewp
parents:
0
diff
changeset
|
63 self.bind("<ButtonRelease-1>",self.release) |
3905d3c92aaa
twisted mainloop, more row-change keys, xmlrpc fadesub command on port 8050
drewp
parents:
0
diff
changeset
|
64 |
0 | 65 dispatcher.connect(lambda: (self.start,self.end),"zoom area",weak=0) |
66 dispatcher.connect(self.input_time,"input time") | |
205
3905d3c92aaa
twisted mainloop, more row-change keys, xmlrpc fadesub command on port 8050
drewp
parents:
0
diff
changeset
|
67 dispatcher.connect(lambda maxtime: (setattr(self,'maxtime',maxtime+15), |
3905d3c92aaa
twisted mainloop, more row-change keys, xmlrpc fadesub command on port 8050
drewp
parents:
0
diff
changeset
|
68 self.updatewidget()), |
3905d3c92aaa
twisted mainloop, more row-change keys, xmlrpc fadesub command on port 8050
drewp
parents:
0
diff
changeset
|
69 "max time",weak=0) |
3905d3c92aaa
twisted mainloop, more row-change keys, xmlrpc fadesub command on port 8050
drewp
parents:
0
diff
changeset
|
70 dispatcher.connect(self.zoom_about_mouse,"zoom about mouse") |
3905d3c92aaa
twisted mainloop, more row-change keys, xmlrpc fadesub command on port 8050
drewp
parents:
0
diff
changeset
|
71 dispatcher.connect(self.see_time,"see time") |
0 | 72 self.created=1 |
205
3905d3c92aaa
twisted mainloop, more row-change keys, xmlrpc fadesub command on port 8050
drewp
parents:
0
diff
changeset
|
73 def zoom_about_mouse(self,t,factor): |
3905d3c92aaa
twisted mainloop, more row-change keys, xmlrpc fadesub command on port 8050
drewp
parents:
0
diff
changeset
|
74 self.start = t - factor*(t-self.start) |
3905d3c92aaa
twisted mainloop, more row-change keys, xmlrpc fadesub command on port 8050
drewp
parents:
0
diff
changeset
|
75 self.end = t + factor*(self.end-t) |
3905d3c92aaa
twisted mainloop, more row-change keys, xmlrpc fadesub command on port 8050
drewp
parents:
0
diff
changeset
|
76 self.updatewidget() |
3905d3c92aaa
twisted mainloop, more row-change keys, xmlrpc fadesub command on port 8050
drewp
parents:
0
diff
changeset
|
77 dispatcher.send("zoom changed") |
3905d3c92aaa
twisted mainloop, more row-change keys, xmlrpc fadesub command on port 8050
drewp
parents:
0
diff
changeset
|
78 def see_time(self,t): |
3905d3c92aaa
twisted mainloop, more row-change keys, xmlrpc fadesub command on port 8050
drewp
parents:
0
diff
changeset
|
79 margin = (self.end-self.start)*.5 # centering is nicest |
3905d3c92aaa
twisted mainloop, more row-change keys, xmlrpc fadesub command on port 8050
drewp
parents:
0
diff
changeset
|
80 if t<self.start: |
3905d3c92aaa
twisted mainloop, more row-change keys, xmlrpc fadesub command on port 8050
drewp
parents:
0
diff
changeset
|
81 self.offset-=(self.start-t)+margin |
3905d3c92aaa
twisted mainloop, more row-change keys, xmlrpc fadesub command on port 8050
drewp
parents:
0
diff
changeset
|
82 if t>self.end: |
3905d3c92aaa
twisted mainloop, more row-change keys, xmlrpc fadesub command on port 8050
drewp
parents:
0
diff
changeset
|
83 self.offset+=(t-self.end)+margin |
3905d3c92aaa
twisted mainloop, more row-change keys, xmlrpc fadesub command on port 8050
drewp
parents:
0
diff
changeset
|
84 self.updatewidget() |
3905d3c92aaa
twisted mainloop, more row-change keys, xmlrpc fadesub command on port 8050
drewp
parents:
0
diff
changeset
|
85 dispatcher.send("zoom changed") |
3905d3c92aaa
twisted mainloop, more row-change keys, xmlrpc fadesub command on port 8050
drewp
parents:
0
diff
changeset
|
86 |
0 | 87 def input_time(self,val): |
88 t=val | |
89 x=self.can_for_t(t) | |
90 self.coords(self.time,x,0,x,self.winfo_height()) | |
205
3905d3c92aaa
twisted mainloop, more row-change keys, xmlrpc fadesub command on port 8050
drewp
parents:
0
diff
changeset
|
91 def press(self,ev,attr): |
3905d3c92aaa
twisted mainloop, more row-change keys, xmlrpc fadesub command on port 8050
drewp
parents:
0
diff
changeset
|
92 self.adjustingattr = attr |
3905d3c92aaa
twisted mainloop, more row-change keys, xmlrpc fadesub command on port 8050
drewp
parents:
0
diff
changeset
|
93 |
3905d3c92aaa
twisted mainloop, more row-change keys, xmlrpc fadesub command on port 8050
drewp
parents:
0
diff
changeset
|
94 def release(self,ev): |
3905d3c92aaa
twisted mainloop, more row-change keys, xmlrpc fadesub command on port 8050
drewp
parents:
0
diff
changeset
|
95 if hasattr(self,'adjustingattr'): del self.adjustingattr |
3905d3c92aaa
twisted mainloop, more row-change keys, xmlrpc fadesub command on port 8050
drewp
parents:
0
diff
changeset
|
96 if hasattr(self,'lastx'): del self.lastx |
3905d3c92aaa
twisted mainloop, more row-change keys, xmlrpc fadesub command on port 8050
drewp
parents:
0
diff
changeset
|
97 def adjust(self,ev,attr=None): |
0 | 98 |
205
3905d3c92aaa
twisted mainloop, more row-change keys, xmlrpc fadesub command on port 8050
drewp
parents:
0
diff
changeset
|
99 if not hasattr(self,'adjustingattr'): |
3905d3c92aaa
twisted mainloop, more row-change keys, xmlrpc fadesub command on port 8050
drewp
parents:
0
diff
changeset
|
100 return |
3905d3c92aaa
twisted mainloop, more row-change keys, xmlrpc fadesub command on port 8050
drewp
parents:
0
diff
changeset
|
101 attr = self.adjustingattr |
3905d3c92aaa
twisted mainloop, more row-change keys, xmlrpc fadesub command on port 8050
drewp
parents:
0
diff
changeset
|
102 |
0 | 103 if not hasattr(self,'lastx'): |
205
3905d3c92aaa
twisted mainloop, more row-change keys, xmlrpc fadesub command on port 8050
drewp
parents:
0
diff
changeset
|
104 self.lastx = ev.x |
0 | 105 new = self.can_for_t(getattr(self,attr)) + (ev.x - self.lastx) |
106 self.lastx = ev.x | |
107 setattr(self,attr,self.t_for_can(new)) | |
108 self.updatewidget() | |
109 dispatcher.send("zoom changed") | |
110 | |
111 def offset(): | |
112 doc = "virtual attr that adjusts start and end together" | |
113 def fget(self): | |
114 return self.start | |
115 def fset(self, value): | |
116 d = self.end-self.start | |
117 self.start = value | |
118 self.end = self.start+d | |
119 return locals() | |
120 offset = property(**offset()) | |
121 | |
122 def can_for_t(self,t): | |
205
3905d3c92aaa
twisted mainloop, more row-change keys, xmlrpc fadesub command on port 8050
drewp
parents:
0
diff
changeset
|
123 return (t-self.mintime)/(self.maxtime-self.mintime)*(self.winfo_width()-30)+20 |
0 | 124 def t_for_can(self,x): |
205
3905d3c92aaa
twisted mainloop, more row-change keys, xmlrpc fadesub command on port 8050
drewp
parents:
0
diff
changeset
|
125 return (x-20)/(self.winfo_width()-30)*(self.maxtime-self.mintime)+self.mintime |
0 | 126 |
127 def updatewidget(self,*args): | |
128 """redraw pieces based on start/end""" | |
129 if not hasattr(self,'created'): return | |
130 y1,y2=3,self.winfo_height()-3 | |
131 lip = 6 | |
132 scan = self.can_for_t(self.start) | |
133 ecan = self.can_for_t(self.end) | |
134 self.coords(self.leftbrack,scan+lip,y1,scan,y1,scan,y2,scan+lip,y2) | |
135 self.coords(self.rightbrack,ecan-lip,y1,ecan,y1,ecan,y2,ecan-lip,y2) | |
205
3905d3c92aaa
twisted mainloop, more row-change keys, xmlrpc fadesub command on port 8050
drewp
parents:
0
diff
changeset
|
136 self.coords(self.shade,scan+5,y1+lip,ecan-5,y2-lip) |
0 | 137 self.delete("tics") |
138 lastx=-1000 | |
139 for t in range(0,int(self.maxtime)): | |
140 x = self.can_for_t(t) | |
141 if 0<x<self.winfo_width() and x-lastx>30: | |
142 txt=str(t) | |
143 if lastx==-1000: | |
144 txt=txt+"sec" | |
145 self.create_line(x,0,x,15, | |
146 tags=('tics',)) | |
147 self.create_text(x,self.winfo_height()-1,anchor='s', | |
148 text=txt,tags=('tics',),font='6x13') | |
149 lastx = x |