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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
45b12307c695 Initial revision
drewp
parents:
diff changeset
1 from __future__ import division
45b12307c695 Initial revision
drewp
parents:
diff changeset
2 import Tkinter as tk
45b12307c695 Initial revision
drewp
parents:
diff changeset
3 from dispatch import dispatcher
45b12307c695 Initial revision
drewp
parents:
diff changeset
4
45b12307c695 Initial revision
drewp
parents:
diff changeset
5 class Zoomcontrol(object,tk.Canvas):
45b12307c695 Initial revision
drewp
parents:
diff changeset
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
45b12307c695 Initial revision
drewp
parents:
diff changeset
9 def maxtime():
45b12307c695 Initial revision
drewp
parents:
diff changeset
10 doc = "seconds at the right edge of the bar"
45b12307c695 Initial revision
drewp
parents:
diff changeset
11 def fget(self): return self._maxtime
45b12307c695 Initial revision
drewp
parents:
diff changeset
12 def fset(self, value):
45b12307c695 Initial revision
drewp
parents:
diff changeset
13 self._maxtime = value
45b12307c695 Initial revision
drewp
parents:
diff changeset
14 self.updatewidget()
45b12307c695 Initial revision
drewp
parents:
diff changeset
15 return locals()
45b12307c695 Initial revision
drewp
parents:
diff changeset
16 maxtime = property(**maxtime())
45b12307c695 Initial revision
drewp
parents:
diff changeset
17
45b12307c695 Initial revision
drewp
parents:
diff changeset
18 def start():
45b12307c695 Initial revision
drewp
parents:
diff changeset
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
45b12307c695 Initial revision
drewp
parents:
diff changeset
21 return locals()
45b12307c695 Initial revision
drewp
parents:
diff changeset
22 start = property(**start())
45b12307c695 Initial revision
drewp
parents:
diff changeset
23
45b12307c695 Initial revision
drewp
parents:
diff changeset
24 def end():
45b12307c695 Initial revision
drewp
parents:
diff changeset
25 def fget(self): return self._end
45b12307c695 Initial revision
drewp
parents:
diff changeset
26 def fset(self,v): self._end = min(self.maxtime,v)
45b12307c695 Initial revision
drewp
parents:
diff changeset
27 return locals()
45b12307c695 Initial revision
drewp
parents:
diff changeset
28 end = property(**end())
45b12307c695 Initial revision
drewp
parents:
diff changeset
29
45b12307c695 Initial revision
drewp
parents:
diff changeset
30
45b12307c695 Initial revision
drewp
parents:
diff changeset
31 def __init__(self,master,**kw):
45b12307c695 Initial revision
drewp
parents:
diff changeset
32 self.maxtime=370
45b12307c695 Initial revision
drewp
parents:
diff changeset
33 self.start=0
45b12307c695 Initial revision
drewp
parents:
diff changeset
34 self.end=20
45b12307c695 Initial revision
drewp
parents:
diff changeset
35 tk.Canvas.__init__(self,master,width=250,height=30,
45b12307c695 Initial revision
drewp
parents:
diff changeset
36 relief='raised',bd=1,bg='gray60',**kw)
45b12307c695 Initial revision
drewp
parents:
diff changeset
37 self.leftbrack = self.create_line(0,0,0,0,0,0,0,0,width=5)
45b12307c695 Initial revision
drewp
parents:
diff changeset
38 self.rightbrack = self.create_line(0,0,0,0,0,0,0,0,width=5)
45b12307c695 Initial revision
drewp
parents:
diff changeset
39 self.shade = self.create_rectangle(0,0,0,0,fill='gray70',outline=None)
45b12307c695 Initial revision
drewp
parents:
diff changeset
40 self.time = self.create_line(0,0,0,0,fill='red',width=2)
45b12307c695 Initial revision
drewp
parents:
diff changeset
41 self.updatewidget()
45b12307c695 Initial revision
drewp
parents:
diff changeset
42 self.bind("<Configure>",self.updatewidget)
45b12307c695 Initial revision
drewp
parents:
diff changeset
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
45b12307c695 Initial revision
drewp
parents:
diff changeset
65 dispatcher.connect(lambda: (self.start,self.end),"zoom area",weak=0)
45b12307c695 Initial revision
drewp
parents:
diff changeset
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
45b12307c695 Initial revision
drewp
parents:
diff changeset
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
45b12307c695 Initial revision
drewp
parents:
diff changeset
87 def input_time(self,val):
45b12307c695 Initial revision
drewp
parents:
diff changeset
88 t=val
45b12307c695 Initial revision
drewp
parents:
diff changeset
89 x=self.can_for_t(t)
45b12307c695 Initial revision
drewp
parents:
diff changeset
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
45b12307c695 Initial revision
drewp
parents:
diff changeset
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
45b12307c695 Initial revision
drewp
parents:
diff changeset
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
45b12307c695 Initial revision
drewp
parents:
diff changeset
105 new = self.can_for_t(getattr(self,attr)) + (ev.x - self.lastx)
45b12307c695 Initial revision
drewp
parents:
diff changeset
106 self.lastx = ev.x
45b12307c695 Initial revision
drewp
parents:
diff changeset
107 setattr(self,attr,self.t_for_can(new))
45b12307c695 Initial revision
drewp
parents:
diff changeset
108 self.updatewidget()
45b12307c695 Initial revision
drewp
parents:
diff changeset
109 dispatcher.send("zoom changed")
45b12307c695 Initial revision
drewp
parents:
diff changeset
110
45b12307c695 Initial revision
drewp
parents:
diff changeset
111 def offset():
45b12307c695 Initial revision
drewp
parents:
diff changeset
112 doc = "virtual attr that adjusts start and end together"
45b12307c695 Initial revision
drewp
parents:
diff changeset
113 def fget(self):
45b12307c695 Initial revision
drewp
parents:
diff changeset
114 return self.start
45b12307c695 Initial revision
drewp
parents:
diff changeset
115 def fset(self, value):
45b12307c695 Initial revision
drewp
parents:
diff changeset
116 d = self.end-self.start
45b12307c695 Initial revision
drewp
parents:
diff changeset
117 self.start = value
45b12307c695 Initial revision
drewp
parents:
diff changeset
118 self.end = self.start+d
45b12307c695 Initial revision
drewp
parents:
diff changeset
119 return locals()
45b12307c695 Initial revision
drewp
parents:
diff changeset
120 offset = property(**offset())
45b12307c695 Initial revision
drewp
parents:
diff changeset
121
45b12307c695 Initial revision
drewp
parents:
diff changeset
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
45b12307c695 Initial revision
drewp
parents:
diff changeset
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
45b12307c695 Initial revision
drewp
parents:
diff changeset
126
45b12307c695 Initial revision
drewp
parents:
diff changeset
127 def updatewidget(self,*args):
45b12307c695 Initial revision
drewp
parents:
diff changeset
128 """redraw pieces based on start/end"""
45b12307c695 Initial revision
drewp
parents:
diff changeset
129 if not hasattr(self,'created'): return
45b12307c695 Initial revision
drewp
parents:
diff changeset
130 y1,y2=3,self.winfo_height()-3
45b12307c695 Initial revision
drewp
parents:
diff changeset
131 lip = 6
45b12307c695 Initial revision
drewp
parents:
diff changeset
132 scan = self.can_for_t(self.start)
45b12307c695 Initial revision
drewp
parents:
diff changeset
133 ecan = self.can_for_t(self.end)
45b12307c695 Initial revision
drewp
parents:
diff changeset
134 self.coords(self.leftbrack,scan+lip,y1,scan,y1,scan,y2,scan+lip,y2)
45b12307c695 Initial revision
drewp
parents:
diff changeset
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
45b12307c695 Initial revision
drewp
parents:
diff changeset
137 self.delete("tics")
45b12307c695 Initial revision
drewp
parents:
diff changeset
138 lastx=-1000
45b12307c695 Initial revision
drewp
parents:
diff changeset
139 for t in range(0,int(self.maxtime)):
45b12307c695 Initial revision
drewp
parents:
diff changeset
140 x = self.can_for_t(t)
45b12307c695 Initial revision
drewp
parents:
diff changeset
141 if 0<x<self.winfo_width() and x-lastx>30:
45b12307c695 Initial revision
drewp
parents:
diff changeset
142 txt=str(t)
45b12307c695 Initial revision
drewp
parents:
diff changeset
143 if lastx==-1000:
45b12307c695 Initial revision
drewp
parents:
diff changeset
144 txt=txt+"sec"
45b12307c695 Initial revision
drewp
parents:
diff changeset
145 self.create_line(x,0,x,15,
45b12307c695 Initial revision
drewp
parents:
diff changeset
146 tags=('tics',))
45b12307c695 Initial revision
drewp
parents:
diff changeset
147 self.create_text(x,self.winfo_height()-1,anchor='s',
45b12307c695 Initial revision
drewp
parents:
diff changeset
148 text=txt,tags=('tics',),font='6x13')
45b12307c695 Initial revision
drewp
parents:
diff changeset
149 lastx = x