Files
@ 224505b0c21e
Branch filter:
Location: light9/flax/zoomcontrol.py
224505b0c21e
3.9 KiB
text/x-python
smaller text
smaller text
better bounds updating (now only on mouse-up)
cooler demo
smaller text
better bounds updating (now only on mouse-up)
cooler demo
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 | from __future__ import division
import Tkinter as tk
from dispatch import dispatcher
class Zoomcontrol(object,tk.Canvas):
def maxtime():
doc = "seconds at the right edge of the bar"
def fget(self): return self._maxtime
def fset(self, value):
self._maxtime = value
self.updatewidget()
return locals()
maxtime = property(**maxtime())
def start():
def fget(self): return self._start
def fset(self,v): self._start = max(0,v)
return locals()
start = property(**start())
def end():
def fget(self): return self._end
def fset(self,v): self._end = min(self.maxtime,v)
return locals()
end = property(**end())
def __init__(self,master,**kw):
self.maxtime=370
self.start=0
self.end=20
tk.Canvas.__init__(self,master,width=250,height=30,
relief='raised',bd=1,bg='gray60',**kw)
self.leftbrack = self.create_line(0,0,0,0,0,0,0,0,width=5)
self.rightbrack = self.create_line(0,0,0,0,0,0,0,0,width=5)
self.shade = self.create_rectangle(0,0,0,0,fill='gray70',outline=None)
self.time = self.create_line(0,0,0,0,fill='red',width=2)
self.updatewidget()
self.bind("<Configure>",self.updatewidget)
self.bind("<ButtonPress-1>",lambda ev: setattr(self,'lastx',ev.x))
self.tag_bind(self.leftbrack,"<B1-Motion>",
lambda ev: self.adjust('start',ev))
self.tag_bind(self.rightbrack,"<B1-Motion>",
lambda ev: self.adjust('end',ev))
self.tag_bind(self.shade,"<B1-Motion>",
lambda ev: self.adjust('offset',ev))
dispatcher.connect(lambda: (self.start,self.end),"zoom area",weak=0)
dispatcher.connect(self.input_time,"input time")
dispatcher.connect(lambda maxtime: (setattr(self,'maxtime',maxtime),
self.updatewidget()),"max time",weak=0)
self.created=1
def input_time(self,val):
t=val
x=self.can_for_t(t)
self.coords(self.time,x,0,x,self.winfo_height())
def adjust(self,attr,ev):
if not hasattr(self,'lastx'):
return
new = self.can_for_t(getattr(self,attr)) + (ev.x - self.lastx)
self.lastx = ev.x
setattr(self,attr,self.t_for_can(new))
self.updatewidget()
dispatcher.send("zoom changed")
def offset():
doc = "virtual attr that adjusts start and end together"
def fget(self):
return self.start
def fset(self, value):
d = self.end-self.start
self.start = value
self.end = self.start+d
return locals()
offset = property(**offset())
def can_for_t(self,t):
return t/self.maxtime*(self.winfo_width()-30)+20
def t_for_can(self,x):
return (x-20)/(self.winfo_width()-30)*self.maxtime
def updatewidget(self,*args):
"""redraw pieces based on start/end"""
if not hasattr(self,'created'): return
y1,y2=3,self.winfo_height()-3
lip = 6
scan = self.can_for_t(self.start)
ecan = self.can_for_t(self.end)
self.coords(self.leftbrack,scan+lip,y1,scan,y1,scan,y2,scan+lip,y2)
self.coords(self.rightbrack,ecan-lip,y1,ecan,y1,ecan,y2,ecan-lip,y2)
self.coords(self.shade,scan+3,y1+lip,ecan-3,y2-lip)
self.delete("tics")
lastx=-1000
for t in range(0,int(self.maxtime)):
x = self.can_for_t(t)
if 0<x<self.winfo_width() and x-lastx>30:
txt=str(t)
if lastx==-1000:
txt=txt+"sec"
self.create_line(x,0,x,15,
tags=('tics',))
self.create_text(x,self.winfo_height()-1,anchor='s',
text=txt,tags=('tics',),font='6x13')
lastx = x
|