Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # -*- coding: utf-8 -*-
- from graphics import *
- import appuifw,time,e32,topwindow,copy,os,re
- import struct,zlib,tempfile,base64,math,bisect
- from array import array
- from key_codes import *
- canvasUnnormal=[]
- mainCanvas=None
- def getCanvas(*arg,**kw):
- global mainCanvas
- mainCanvas=appuifw.Canvas(*arg,**kw)
- return mainCanvas
- class canvas:
- def __init__(self,redraw_callback=None,event_callback=None,resize_callback=None,screen='normal',appmenu=[],mouse=2):
- self.inited=0
- if mouse==2:mouse=(not appuifw.touch_enabled())
- self.mouseEnabled=mouse
- self.mouseX=0
- self.mouseY=0
- self.appmenu=appmenu
- self.lock=e32.Ao_lock()
- self.old=(appuifw.app.body,appuifw.app.exit_key_handler,appuifw.app.menu,appuifw.app.screen)
- self.menu=(u'show canvas',self.show)
- self.screen=screen
- #self.text=appuifw.Text()
- self.redrawTimer=e32.Ao_timer()
- self.lastRedraw=0
- appuifw.app.screen=self.screen
- appuifw.app.menu=appmenu
- appuifw.app.exit_key_handler=self.hide
- self.redraw_callback=redraw_callback
- self.event_callback=event_callback
- self.resize_callback=resize_callback
- self.canvas=appuifw.Canvas(self.redraw,self.event,self.resize)
- self.img=Image.new(self.canvas.size,mode='RGB')
- size0=self.canvas.size
- self.size0=(size0,self.screen)
- appuifw.app.body=self.canvas
- size1=appuifw.app.body.size
- self.case=(size0==size1)
- self.bindDict=dict()
- if not self.case:
- self.canvas=appuifw.Canvas(self.redraw,self.event,self.resize)
- self.inited=1
- self.hide()
- if self.mouseEnabled:
- self.bind((EStdKeyUpArrow,1),lambda:self.mouseMove('up',1))
- self.bind((EStdKeyDownArrow,1),lambda:self.mouseMove('down',1))
- self.bind((EStdKeyLeftArrow,1),lambda:self.mouseMove('left',1))
- self.bind((EStdKeyRightArrow,1),lambda:self.mouseMove('right',1))
- self.bind((EStdKeyUpArrow,2),lambda:self.mouseMove('release',2))
- self.bind((EStdKeyDownArrow,2),lambda:self.mouseMove('release',2))
- self.bind((EStdKeyLeftArrow,2),lambda:self.mouseMove('release',2))
- self.bind((EStdKeyRightArrow,2),lambda:self.mouseMove('release',2))
- self.lock.wait()
- def mouseMove(self,action,ty=1):
- if ty==2:self.mouseSpeed=1
- try:
- if self.mouseSpeed<=30:self.mouseSpeed+=2
- speed=self.mouseSpeed
- except:
- self.mouseSpeed=1
- speed=self.mouseSpeed
- w,h=self.canvas.size
- if action=='up':self.mouseY+=-speed
- elif action=='down':self.mouseY+=speed
- elif action=='left':self.mouseX+=-speed
- elif action=='right':self.mouseX+=speed
- if self.mouseX<0:self.mouseX=0
- if self.mouseY<0:self.mouseY=0
- if self.mouseX>=w:self.mouseX=w-1
- if self.mouseY>=h:self.mouseY=h-1
- self.redraw(None)
- def bind(self,key,fun):
- self.bindDict[key]=fun
- def redraw(self,rect):
- if callable(self.redraw_callback):self.redraw_callback(self,None)
- else:self.canvas.blit(self.img)
- if self.mouseEnabled:
- self.canvas.blit(systemMouse[0],target=(self.mouseX-7,self.mouseY-7),mask=systemMouse[1])
- def event(self,ev):
- if type(ev) is dict and 'scancode' in ev and (ev['scancode'],ev['type']) in self.bindDict:
- self.bindDict[(ev['scancode'],ev['type'])]()
- if callable(self.event_callback):self.event_callback(self,ev)
- def resize(self,size):
- #if size!=self.size0[0] and appuifw.app.screen==self.size0[1]:self.canvas=getCanvas(self.redraw,self.event,self.resize)
- if self.resize_callback!=None:
- try:self.resize_callback(self,size)
- except:pass
- def show(self,fun=None):
- appuifw.app.screen=self.screen
- if self.case:
- appuifw.app.body=self.canvas
- else:
- appuifw.app.body=self.canvas
- self.canvas=appuifw.Canvas(self.redraw,self.event,self.resize)
- appuifw.app.menu=self.appmenu
- appuifw.app.exit_key_handler=self.hide
- if callable(fun):fun(self)
- try:self.lock.wait()
- except:pass
- def switch(self):pass
- def hide(self):
- appuifw.app.screen=self.old[3]
- self.redrawTimer.cancel()
- if self.case:pass
- else:appuifw.app.body=self.canvas
- appuifw.app.exit_key_handler=self.old[1]
- appuifw.app.menu=self.old[2]
- appuifw.app.body=self.old[0]
- try:self.lock.signal()
- except:pass
- def drawMouse(width,color0=0xff00,color1=0):
- i=Image.new((width,width))
- iMask=Image.new(i.size,mode='1')
- i.clear(0)
- iMask.clear(0)
- w=iRound(max(width*0.16,1))
- i=color_fill(i,(color0,color1,2))
- #i.ellipse( (0,0,width,width),fill=0)
- #i.ellipse((w,w,width-w,width-w),fill=0xeee4aa)
- cx,cy=centerPoint(0,0,width,width)
- iMask.ellipse((0,0,width,width),fill=0xffffff)
- iMask.ellipse((w,w,width-w,width-w),fill=0)
- iMask.ellipse((cx-w,cy-w,cx+w,cy+w),fill=0xffffff)
- return (i,iMask)
- def getRoute():
- cv=canvas(mouse=2)
- bg,top=systemConfig['background'],systemConfig['top']
- cv.img.clear(bg)
- mid=mid_color(bg,top)
- cv.mid,cv.top,cv.bg=mid,top,bg
- imgBottom=Image.new(cv.img.size)
- imgBottom.clear(cv.bg)
- imgTop=Image.new(cv.img.size)
- imgMask=Image.new(imgTop.size,mode='1')
- imgTop.clear(top)
- imgMask.clear(0)
- points=[]
- cv.routeType=None #line,curve,polygon,curve polygon
- def redraw(cv=cv,points=points):
- imgMask.clear(0)
- #imgTop.clear(cv.top)
- if cv.routeType==None:
- for p in points:imgMask.point(p,0xffffff,width=5)
- elif cv.routeType==0:imgMask.line(points,0xffffff)
- elif cv.routeType==1:imgMask.line(tuple(genCurveRoutePoints(points)),0xffffff)
- elif cv.routeType==2:imgMask.polygon(points,fill=0xffffff)
- elif cv.routeType==3:imgMask.polygon(tuple(genCurvePolygonPoints(points)),fill=0xffffff)
- cv.img.blit(imgBottom)
- cv.img.blit(imgTop,mask=imgMask)
- cv.redraw(None)
- def showMenu(cv=cv,bg=bg,top=top,mid=mid,points=points,selection=None):
- pos=cv.mouseX
- cv.mouseX=-1
- menu=[u'line',u'curve',u'polygon',u'curve polygon',u'rotate',u'color',u'points',u'quit']
- if selection==None:selection=selection_handler(menu,u'action')
- if selection==None:return 0
- elif selection==len(menu)-1:
- cv.hide()
- return 0
- if selection<4:
- cv.routeType=selection
- elif selection==6:
- cv.routeType=None
- elif selection==5:
- color=color_select()[0]
- if color==None:
- cv.mouseX=pos
- return 0
- else:
- cv.top=color
- imgTop.clear(cv.top)
- elif selection==4:
- d=appuifw.query(u'degrees of rotation','float')
- if d==None:d=0
- radians=math.radians(d)
- cx,cy=centerPoint(max((x[0] for x in points)),max((x[1] for x in points)),min((x[0] for x in points)),min((x[1] for x in points)))
- points[:]=[pointRotate(x[0],x[1],radians,cx,cy) for x in points]
- cv.mouseX=pos
- redraw()
- def addPoints(cv=cv,bg=bg,top=top,mid=mid,points=points):
- w,h=cv.img.size
- w=iRound(w*0.75)
- h=iRound(h*0.75)
- if not 0<=cv.mouseX<w or not 0<=cv.mouseY<h:return 0
- pos=(cv.mouseX,cv.mouseY)
- cv.img.clear(bg)
- points.append(pos)
- redraw()
- def init(self):
- appuifw.app.exit_key_handler=showMenu
- self.mouseX=-1
- cv.bind((EScancodeSelect,2),addPoints)
- cv.show(init)
- del cv
- return points
- class columnsCanvas(object):
- def __init__(self,c=4,screen='large',bg=0xeee4aa,top=0,eventCallback=None):
- self.cv=canvas(screen=screen,mouse=2)
- self.eventCallback=eventCallback
- self.cv.top,self.cv.mid,self.cv.bg=top,mid_color(top,bg),bg
- self.cv.dragLog=[]
- self.cv.mouseX=-1
- self.cv.img.clear(self.cv.mid)
- w,h=self.cv.img.size
- self.cv.columns=c
- self.cv.column=(float(w)/c,float(h)/c)
- self.cv.iColumn=(int(w/c)-1,int(h/c)-1)
- self.cv.iMain=(int(w-1-float(w)/c),int(h-1-float(h)/c))
- self.cv.tempColumn=[Image.new(self.cv.iColumn,mode='RGB'),Image.new(self.cv.iColumn,mode='1')]
- self.cv.tempMain=[Image.new(self.cv.iMain,mode='RGB'),Image.new(self.cv.iMain,mode='1')]
- self.cv.imgMain=Image.new(self.cv.iMain)
- self.cv.imgMain.clear(self.cv.bg)
- self.cv.img.clear(self.cv.top)
- columns(self.cv.img,self.cv.column,self.cv.mid)
- self.cv.img.blit(self.cv.imgMain,target=self.getPos(0))
- #self.cv.img.rectangle((self.cv.column,self.cv.img.size),fill=self.cv.bg)
- self.show=self.cv.show
- self.hide=self.cv.hide
- self.cv.event_callback=self.event
- def getSize(self,n):
- if n==0:return self.cv.iMain
- else:return self.cv.iColumn
- def logDrag(self,pos):
- if self.cv.dragLog==[] or pos!=self.cv.dragLog[-1]:self.cv.dragLog.append(pos)
- def clearDragLog(self):
- self.cv.dragLog[:]=[]
- def getPos(self,n):
- cw,ch=self.cv.column
- w,h=self.cv.img.size
- if n==0:return (int(cw)+1,1)
- elif 1<=n<=self.cv.columns:return (1,int(ch*(n-1))+1)
- elif self.cv.columns<n<self.cv.columns*2:
- n=n-self.cv.columns
- h=int(h-ch)+1
- return (int(cw*n)+1,h)
- def getFocus(self,pos):
- x,y=pos
- if x<0 or y<0:return None
- w,h=self.cv.img.size
- cw,ch=self.cv.column
- if cw<x<w and 0<=y<h-ch:return 0
- elif 0<=x<cw:return int(y/ch)+1
- elif h-ch<y<=h and cw<x<w:return int(x/cw)+self.cv.columns
- def getTopBottom(self,n):
- x,y=self.getPos(n)
- w,h=self.getSize(n)
- return (x,y,x+w,y+h)
- def inFocus(self,pos,n):
- ax,ay,bx,by=self.getTopBottom(n)
- x,y=pos
- return ax<=x<bx and ay<=y<by
- def event(self,obj,ev):
- if 'scancode' in ev and (ev['scancode'],ev['type'])[0]==EScancodeSelect:
- pos=(self.cv.mouseX,self.cv.mouseY)
- ty=ev['type']
- elif 'pos' in ev:
- pos=ev['pos']
- ty=ev['type']
- else:return 0
- if ty in (2,258):self.clearDragLog()
- elif ty in (3,263) and self.inFocus(pos,0):self.logDrag(pos)
- focus=self.getFocus(pos)
- if focus==None:return 0
- if callable(self.eventCallback):
- return self.eventCallback((focus,pos,ty))
- tx=u'%s,%s,%s'%(focus,pos,ty)
- self.cv.img.rectangle((self.getTopBottom(0)),fill=self.cv.bg)
- self.cv.img.text((self.cv.column[0]*2,self.cv.column[1]*2),tx,self.cv.top,font=('normal',19))
- if len(self.cv.dragLog)>1:self.cv.img.line(self.cv.dragLog,self.cv.mid)
- self.cv.redraw(None)
- def focusRedrawCommands(self,n,commands):
- px,py=self.getPos(n)
- width,height=self.getSize(n)
- cor=(px,py,px+width,py+height)
- drawCommands(self.cv.img,commands,cor)
- self.cv.redraw(None)
- return 0
- def focusRedraw(self,n,img,mask=None,pos='0w,0h'):
- ipos,size=self.getPos(n),self.getSize(n)
- px,py=posUnpack(size[0],size[1],pos)
- if n==0:i0,i1=self.cv.tempMain
- else:i0,i1=self.cv.tempColumn
- i1.clear(0)
- w,h=img.size
- i1.rectangle((px,py,px+w,py+h),fill=0xffffff)
- if mask!=None:i1.blit(mask,target=(px,py))
- i0.blit(img,target=(px,py))
- self.cv.img.blit(i0,target=(ipos),mask=i1)
- self.cv.redraw(None)
- def focusRedrawResize(self,n,img,keepaspect=1):
- size,pos=self.getSize(n),self.getPos(n)
- if size==img.size:self.cv.img.blit(img,target=pos)
- else:self.cv.img.blit(img.resize(size,keepaspect=keepaspect),target=pos)
- self.cv.redraw(None)
- def circle(img,pos,r,*arg,**kw):
- x,y=pos
- cor=(x-r,y-r,x+r,y+r)
- img.ellipse(cor,*arg,**kw)
- def drawPad(background=0xeee4aa,top=0):
- c=columnsCanvas(10,screen='full')
- c.bg=background
- c.top=top
- c.commandsPadding=[]
- c.commandLinePadding=[None,[],None,None]
- def redraw(c=c):
- color=c.top
- c.cv.img.blit(c.cv.imgMain,target=c.getPos(0))
- ty,data,fill,fillstyle=c.commandLinePadding
- if ty=='points':
- for p in data:
- c.cv.img.point(p,color)
- elif ty!=None and data!=[]:
- points=posPack(c.cv.img.size[0],c.cv.img.size[1],data)
- commandLine=[ty,[points,c.top],dict(fill=fill),fillstyle]
- try:drawCommands(c.cv.img,[commandLine])
- except:pass
- for p in data:c.cv.img.point(p,0x777255,width=5)
- c.cv.redraw(None)
- def event(ev,c=c):
- focus,pos,ty=ev
- tys=('line','curve','bezier','points',None,'polygon','curvePolygon')
- if ty in (2,258):
- if 1<=focus<8:
- c.commandLinePadding[0]=tys[focus-1]
- elif focus==8:
- try:c.commandLinePadding[1].pop()
- except:pass
- elif focus==10:
- if c.commandLinePadding[2]==None:c.commandLinePadding[2]=0
- else:c.commandLinePadding[2]=None
- elif focus==11:
- f=[[u'Color #1','color',c.bg],
- [u'Color #2','color',c.top],
- [u'Fill Method','combo',[[u'Y',u'X',u'X and Y',u'From center'],0]]]
- if c.commandLinePadding[3]!=None:
- c1,c2,c3=c.commandLinePadding[3]
- f[0][2]=c1
- f[1][2]=c2
- f[2][2][1]=c3
- result=form_handler(f,u'Fill style')
- del f
- if result!=None:
- fillstyle=(result[0][2],result[1][2],result[2][2][1])
- c.commandLinePadding[3]=fillstyle
- redraw()
- if focus==0 and ty in (2,258):
- px,py=c.getPos(0)
- if c.commandLinePadding[1]==[] or c.commandLinePadding[1][-1]!=pos:c.commandLinePadding[1].append(pos)
- redraw()
- elif ty in (2,258):
- if focus==c.cv.columns*2-1:c.hide()
- elif focus==c.cv.columns*2-2:
- c.eventCallback=lambda a:1
- color=color_select(u'Select top color')
- if color!=None:
- c.top=color[0]
- c.focusRedrawCommands(c.cv.columns*2-2,[['clear',[color[0]]]])
- redraw()
- e32.ao_sleep(0.1)
- c.eventCallback=event
- c.eventCallback=event
- c.focusRedrawCommands(1,[
- ['line',['0.1w,0.5h,0.2w,0.2h,0.6w,0.7h,0.9w,0.5h',0xffff00,]]
- ])
- c.focusRedrawCommands(2,[
- ['curve',['0.1w,0.9h,0.6w,0.2h,0.9w,0.9h',0xffff00]],
- ['line',['0.1w,0.9h,0.6w,0.2h,0.9w,0.9h',0xffff]]
- ])
- c.focusRedrawCommands(3,[
- ['line',['0.1w,0.9h,0.25w,0.3h,0.8w,0.1h,0.9w,0.9h',0xffff]],
- ['bezier',['0.1w,0.9h,0.25w,0.3h,0.8w,0.1h,0.9w,0.9h',0xffff00]]
- ])
- c.focusRedrawCommands(4,[
- ['ellipse',['0.45w,0.45h,0.55w,0.55h',0xffff00,0xffff00]]
- ])
- c.focusRedrawCommands(5,[
- ['line',['0.2w,0.5h,0.7w,0.5h',0xffff00]],
- ['polygon',['0.6w,0.4h,0.8w,0.5h,0.6w,0.6h',0xffff00,0xffff00]],
- ['rectangle',['0.2w,0.42h,0.36w,0.58h',0xffff00,0xffff00]]
- ])
- c.focusRedrawCommands(6,[
- ['polygon',['0.15w,0.15h,0.5w,0.5h,0.8w,0.15h,0.8w,0.8h,0.15w,0.8h',0xffff00]]
- ])
- c.focusRedrawCommands(7,[
- ['curvePolygon',['0.2w,0.8h,0.8w,0.8h,0.6w,0.3h,0.3w,0.3h',0xffff00]]
- ])
- c.focusRedrawCommands(8,(
- ('line',['0.2w,0.2h,0.8w,0.8h',0xCa00]),
- ('line',['0.8w,0.2h,0.2w,0.8h',0xCa00])
- ))
- c.focusRedrawCommands(9,[['line',['0.1w,0.35h,0.3w,0.7h,0.8w,0.2h',0xca00]]])
- c.focusRedrawCommands(10,(('rectangle',['0.2w,0.2h,0.8w,0.8h',0xca00,0xCa00]),))
- c.focusRedrawCommands(11,[['rectangle',['0.2w,0.2h,0.8w,0.8h'],{},(0xca00,0,2)]])
- c.focusRedrawCommands(12,[
- ['arc',['0.2w,0.2h,0.8w,0.8h',math.pi/2,0,0xca00]],
- ['polygon',['0.65w,0.55h,0.85w,0.55h,0.8w,0.4h',
- 0xca00,0xca00]]
- ])
- c.focusRedrawCommands(16,(
- ('rectangle',['0.2w,0.2h,0.8w,0.8h',0xeee4aa]),
- ))
- c.focusRedrawCommands(17,(
- ('ellipse',['0.2w,0.2h,0.8w,0.8h',0xeee4aa]),
- ))
- c.focusRedrawCommands(18,[
- ['rectangle',['0.2w,0.2h,0.8w,0.4h'],dict(fill=0xff0000)],
- ['rectangle',['0.2w,0.4h,0.8w,0.6h'],dict(fill=0xff00)],
- ['rectangle',['0.2w,0.6h,0.8w,0.8h'],dict(fill=0xff)],
- ])
- c.focusRedrawResize(19,logo[0],0)
- return c
- def gen_logo(size=100,bg=0xeee4aa,top=0):
- img=Image.new((size,size))
- imask=Image.new(img.size,mode='1')
- imask.clear(0)
- #mid=mid_color(bg,top)
- mid=0x777255
- img.clear(mid)
- circle(img,(size*0.25,size*0.25),size*0.25,fill=top)
- circle(img,(size*0.75,size*0.75),size*0.25,fill=top)
- circle(img,(size*0.25,size*0.25),size*0.15,fill=bg)
- circle(img,(size*0.75,size*0.75),size*0.15,fill=bg)
- img.line((size*0.25,size*0.5,size*0.25,size),top,width=int(max(size*0.03,1)))
- img.line((0,size*0.75,size*0.5,size*0.75),top,width=int(max(size*0.03,1)))
- circle(img,(size*0.25,size*0.75),size*0.15,fill=top)
- circle(img,(size*0.25,size*0.75),size*0.08,fill=bg)
- img.rectangle((size*0.4,0,size,size*0.6),mid,fill=bg)
- img.rectangle((size*0.4,0,size,size*0.18),fill=mid)
- ##
- circle(imask,(size*0.25,size*0.25),size*0.25,fill=0xffffff)
- circle(imask,(size*0.75,size*0.75),size*0.25,fill=0xffffff)
- imask.line((size*0.25,size*0.5,size*0.25,size),0xffffff,width=int(max(size*0.03,1)))
- imask.line((0,size*0.75,size*0.5,size*0.75),0xffffff,width=int(max(size*0.03,1)))
- circle(imask,(size*0.25,size*0.75),size*0.15,fill=0xffffff)
- imask.rectangle((size*0.4,0,size,size*0.6),fill=0xffffff)
- return (img,imask)
- #*#*
- def web_logo(size=100,bg=0xeee4aa,top=0):
- img=Image.new((size,size))
- imask=Image.new(img.size,mode='1')
- imask.clear(0)
- #mid=mid_color(bg,top)
- mid=0x777255
- img.clear(mid)
- img.line((size*0.25,0,size*0.25,size),top,width=int(max(size*0.03,1)))
- img.line((0,size*0.75,size,size*0.75),top,width=int(max(size*0.03,1)))
- img.line((size*0.75,0,size*0.75,size),top,width=int(max(size*0.03,1)))
- img.line((0,size*0.25,size,size*0.25),top,width=int(max(size*0.03,1)))
- circle(img,(size*0.25,size*0.25),size*0.25,fill=top)
- circle(img,(size*0.75,size*0.75),size*0.25,fill=top)
- circle(img,(size*0.25,size*0.25),size*0.15,fill=bg)
- circle(img,(size*0.75,size*0.75),size*0.15,fill=bg)
- circle(img,(size*0.25,size*0.75),size*0.15,fill=top)
- circle(img,(size*0.25,size*0.75),size*0.08,fill=bg)
- circle(img,(size*0.75,size*0.25),size*0.15,fill=top)
- circle(img,(size*0.75,size*0.25),size*0.08,fill=bg)
- ##
- imask.line((size*0.25,0,size*0.25,size),0xffffff,width=int(max(size*0.03,1)))
- imask.line((0,size*0.75,size,size*0.75),0xffffff,width=int(max(size*0.03,1)))
- imask.line((size*0.75,0,size*0.75,size),0xffffff,width=int(max(size*0.03,1)))
- imask.line((0,size*0.25,size,size*0.25),0xffffff,width=int(max(size*0.03,1)))
- circle(imask,(size*0.25,size*0.25),size*0.25,fill=0xffffff)
- circle(imask,(size*0.75,size*0.75),size*0.25,fill=0xffffff)
- circle(imask,(size*0.75,size*0.25),size*0.15,fill=0xffffff)
- circle(imask,(size*0.25,size*0.75),size*0.15,fill=0xffffff)
- return (img,imask)
- logo=web_logo(64)
- def columns(img,column,color=0xffffff,width=1):
- if type(img) in (tuple,list):
- img=Image.new(size,mode='1')
- img.clear(0)
- color=0xffffff
- xl,yl=column
- w,h=img.size
- cols=int(w/xl)
- lines=int(h/yl)
- for x in xrange(cols+2):
- cor=[int(x) for x in (x*xl-width*0.5,0,x*xl-width*0.5,h)]
- img.line(cor,color,width=width)
- for y in xrange(lines+2):
- cor=[int(x) for x in (0,y*yl-width*0.5,w,y*yl-width*0.5)]
- img.line(cor,color,width=width)
- return img
- def in_layout(pos,layout):
- try:
- x0,y0=pos
- w,h=layout[0]
- x1,y1=layout[1]
- return x1<=x0<=x1+w and y1<=y0<=y1+h
- except:return 0
- def in_line(a,b,c):
- try:
- x1,y1=a
- x2,y2=b
- x3,y3=c
- ux,uy=(x2-x1,y2-y1)
- vx,vy=(x3-x1,y3-y1)
- return ux*vy==uy*vx
- except:return 0
- def num_to_rgb(color):
- if type(color) is tuple:return color
- r=int(color/0x10000)
- g=int((color-r*0x10000)/0x100)
- b=color-r*0x10000-g*0x100
- return (r,g,b)
- def rgb_to_num(color):
- if type(color) in (tuple,list) and len(color)==3 and (type(color[0]),type(color[1]),type(color[2]))==(int,int,int):
- r,g,b=color
- return r*0x10000+g*0x100+b
- def mid_color(c1,c2):
- if type(c1) not in (tuple,list):c1=num_to_rgb(c1)
- if type(c2) not in (tuple,list):c2=num_to_rgb(c2)
- return rgb_to_num(map(lambda a,b:(a+b)/2,c1,c2))
- def image_fill(fill,bottom):
- fx,fy=fill.size
- w,h=bottom.size
- if 0 in (fx,fy,w,h):return bottom
- xt,yt=(w/fx+(w/fx!=float(w)/fx),h/fy+(h/fy!=float(h)/fy))
- bottom.blit(fill)
- xpos=1
- if (fx,fy)==(w,h):return bottom
- for x in xrange(1,xt):
- xamount=xpos*(xpos*2<xt)+(xt-xpos)*(xpos*2>=xt)
- bottom.blit(bottom,target=(xpos*fx,0),source=(0,0,xamount*fx,fy))
- xpos+=xamount
- if xpos>=xt:break
- ypos=1
- for y in xrange(1,yt):
- yamount=ypos*(ypos*2<yt)+(yt-ypos)*(ypos*2>=yt)
- bottom.blit(bottom,target=(0,ypos*fy),source=(0,0,w,fy*yamount))
- ypos+=yamount
- if ypos>=yt:break
- return bottom
- def color_fill(img,styles):
- if type(img) is tuple or type(img) is list:img=Image.new(img)
- color0,color1,style=styles
- w,h=img.size
- if color0==color1:
- img.clear(color0)
- return img
- elif style==0:
- r0,g0,b0=num_to_rgb(color0)
- r1,g1,b1=num_to_rgb(color1)
- length=h
- each_add=[(r1-r0)/float(length),(g1-g0)/float(length),(b1-b0)/float(length)]
- for x in xrange(length):
- color_fill=(int(each_add[0]*x+r0),int(each_add[1]*x+g0),int(each_add[2]*x+b0))
- cor=(0,x,w,x)
- img.line(cor,color_fill,width=1)
- return img
- elif style==1:
- r0,g0,b0=num_to_rgb(color0)
- r1,g1,b1=num_to_rgb(color1)
- length=w
- each_add=[(r1-r0)/float(length),(g1-g0)/float(length),(b1-b0)/float(length)]
- for x in xrange(length):
- color_fill=(int(each_add[0]*x+r0),int(each_add[1]*x+g0),int(each_add[2]*x+b0))
- cor=(x,0,x,h)
- img.line(cor,color_fill,width=1)
- return img
- elif style==2:
- r0,g0,b0=num_to_rgb(color0)
- r1,g1,b1=num_to_rgb(color1)
- if w>=h:length=int(w)
- else:length=int(h)
- each_add=[(r1-r0)/float(length),(g1-g0)/float(length),(b1-b0)/float(length)]
- for x in xrange(length):
- color_fill=(int(each_add[0]*x+r0),int(each_add[1]*x+g0),int(each_add[2]*x+b0))
- cor=(0,x*2,x*2,0)
- img.line(cor,color_fill,width=2)
- return img
- elif style==3:
- r0,g0,b0=num_to_rgb(color0)
- r1,g1,b1=num_to_rgb(color1)
- if w>=h:length=int(w)
- else:length=int(h)
- each_add=[(r1-r0)/float(length),(g1-g0)/float(length),(b1-b0)/float(length)]
- for x in xrange(length):
- color_fill=(int(each_add[0]*x+r0),int(each_add[1]*x+g0),int(each_add[2]*x+b0))
- cor=(w/2-x,h/2-x,w/2+x,h/2+x)
- img.ellipse(cor,color_fill,width=3)
- return img
- else:return img
- def colorFill(img,style,cor=None):
- if cor!=None:
- return color_fill(img,style)
- else:
- a,b,c,d=cor
- w,h=c-a,d-b
- i=color_fill((w,h),style)
- img.blit(i,target=(a,b))
- del i
- return img
- def break_text(txt,width,font,bp=0):
- page=list()
- point=list()
- img=Image.new((1,1))
- length=len(txt)
- for x in xrange(length):
- tx=txt[x]
- measure=img.measure_text(tx[:200],font,width)
- page.append(tx[:measure[2]])
- if bp:point.append(x)
- if len(tx)>measure[2]:
- rest=tx[measure[2]:]
- measure=img.measure_text(rest[:200],font,width)
- while measure[2]<len(rest):
- page.append(rest[:measure[2]])
- if bp:point.append(x)
- rest=rest[measure[2]:]
- measure=img.measure_text(rest,font,width)
- if len(rest)>0:
- page.append(rest)
- if bp:point.append(x)
- if x<length-1:page[-1]+='\n'
- del img
- if bp:return (page,point)
- else:return page
- def round_mask(size,rsize):
- mask=Image.new(size,mode='1')
- mask.clear(0xffffff)
- rmask=Image.new(rsize,mode='1')
- rmask.clear(0)
- rmask.ellipse(((0,0),rmask.size),0xffffff,fill=0xffffff)
- hx=rsize[0]/2
- hy=rsize[1]/2
- mask.blit(rmask,target=(0,0),source=((0,0),(hx,hy)))
- mask.blit(rmask,target=(mask.size[0]-hx,0),source=((hx,0),(rmask.size[0],hy)))
- mask.blit(rmask,target=(0,mask.size[1]-hy),source=((0,hy),(hx,rmask.size[1])))
- mask.blit(rmask,target=(mask.size[0]-hx,mask.size[1]-hy),source=((hx,hy),rmask.size))
- return mask
- def get_filename(value=u'',note=u'input the name'):
- rt=appuifw.query(note,'text',value)
- if rt in (None,u''):return None
- chars=u'<>/\|"*?:'
- for x in rt:
- if x in chars:
- appuifw.note(u'name can\'t contain this chars :\n%s'%chars,'error')
- return None
- return rt
- def multi_select(obj,ft=None):
- if len(obj.ls)==0:return None
- config=obj.config()
- if callable(ft)==0:
- selection=multi_selection_handler(obj.ls,u"multi select",**config)
- else:
- ks=list()
- ls=list()
- for x in xrange(len(obj.ls)):
- if ft(obj.ls[x]):
- ks.append(x)
- ls.append(obj.ls[x])
- selection=multi_selection_handler(ls,u"multi select",**config)
- if selection!=None:
- selection=tuple([ks[x] for x in selection])
- del ks
- del ls
- return selection
- def get_fg(obj):
- if len(obj.ls)==0:
- obj.ls=[u'']
- obj.bp=[0]
- obj.focus=obj.focus1=0
- return 0
- elif obj.focus==None:
- obj.focus=obj.focus1=0
- return 0
- elif obj.focus>=len(obj.ls):
- obj.focus=len(obj.ls)-1
- obj.focus1=0
- return obj.focus
- elif obj.focus1==None:
- obj.focus1=0
- return obj.focus
- else:
- maxFocus1=min(obj.lines-1,len(obj.ls)-obj.focus-1)
- if obj.focus1>=maxFocus1:obj.focus1=maxFocus1
- if obj.focus1<0:obj.focus1=0
- return obj.focus+obj.focus1
- def get_fg_text(obj):
- fg=get_fg(obj)
- if fg==None:return None
- return obj.ls[fg]
- def insert_fg(obj,tx):
- fg=get_fg(obj)
- if fg==None:fg=0
- if type(obj.ls) is not list:obj.ls=list(obj)
- obj.ls.insert(fg,tx)
- force_refresh(obj)
- def add_fg(obj,tx):
- fg=get_fg(obj)
- if fg==None:fg=0
- if type(obj.ls) is not list:obj.ls=list(obj)
- try:
- obj.ls.insert(fg+1,tx)
- except:
- obj.ls.insert(fg,tx)
- force_refresh(obj)
- obj.page_control('linedown')
- def set_fg(obj,tx):
- fg=get_fg(obj)
- if fg==None:fg=0
- if type(obj.ls) is not list:obj.ls=list(obj)
- obj.ls[fg]=tx
- force_refresh(obj)
- def del_fg(obj):
- fg=get_fg(obj)
- if fg==None:return 0
- if type(obj.ls) is not list:obj.ls=list(obj)
- del obj.ls[fg]
- force_refresh(obj)
- obj.page_control('focusup')
- def force_refresh(obj,title=1):
- fg=get_fg(obj)
- if title:obj.changed['title']=1
- obj.changed['main']=1
- obj.changed['focus']=None
- obj.changed['focus1']=None
- obj.redraw(None)
- def image_base64(img,ty='png'):
- if type(img) in (unicode,str):
- if img.lower().endswith('jpg') or img.lower().endswith('jpeg'):ty='jpg'
- else:ty='jpg'
- temp_path=img
- data=open(temp_path,'rb').read()
- else:
- temp_path=tempfile.mktemp('.%s'%ty)
- img.save(temp_path)
- data=open(temp_path,'rb').read()
- os.remove(temp_path)
- return '''data:image/%s;base64,%s'''%(ty,base64.encodestring(data))
- def list_select(self,ls,title,callback=None,init=None,background=0xeee4aa,top=0,show_lines=1,font=('normal',24),header_font=('normal',24),mode='select',search_control=None,appmenu=[],multiline=0,left=2,bp=[],left_draw=None,search_select='select',line_draw=None,focus=0,focus1=0):
- def page_control(self,way,ty=None):
- last=(self.focus,self.focus1)
- l=len(self.ls)
- lines=self.lines
- self.this_line[1]=0
- #self.dtimer.cancel()
- if self.scrolling==3:return page_auto_scroll(self)
- self.scrolling=0
- if way=='lineup':
- self.focus1+=-1
- if self.focus1<0:self.focus+=-1
- if self.focus<0:self.focus=len(self.ls)-1
- if way=='scroll':
- yamount=(self.drag_log[-1][1]-self.drag_log[0][1])/self.font[1]
- if self.changed['scroll']==None:
- self.changed['scroll']=self.focus
- self.scrolling=5
- self.changed['scrolling']=time.time()
- self.focus=self.changed['scroll']-yamount
- speed=-yamount*(time.time()-self.changed['scrolling'])
- if speed<-self.lines:speed=-self.lines
- if speed>self.lines:speed=self.lines
- if abs(yamount)>self.lines*4/10:
- self.changed['speed']=speed
- elif way=='focusup':self.focus+=-1
- elif way=='focusdown':self.focus+=1
- elif way=='linedown':
- self.focus1+=1
- fg=min(l-self.focus-1,self.lines-1)
- if self.focus1>fg:self.focus+=1
- if self.focus>=len(self.ls):self.focus=0
- elif way=='pageup':self.focus+=-lines
- elif way=='pagedown':self.focus+=lines
- if self.focus>=l-1:self.focus=l-1
- if self.focus<0:self.focus=0
- fg=min(l-self.focus-1,self.lines-1)
- if self.focus1>=fg:self.focus1=fg
- if self.focus1<0:self.focus1=0
- self.changed['focus']=last[0]
- self.changed['focus1']=last[1]
- if last!=(self.focus,self.focus1) or self.changed['main']:
- self.changed['main']=1
- if (self.focus!=last[0] or self.focus1!=last[1]) and self.focus!=None and self.focus1!=None:
- if len(self.bp)==0:
- try:appuifw.app.title=u'%s/%s'%(self.focus+self.focus1+1,len(self.ls))
- except:pass
- else:
- try:appuifw.app.title=u'%s/%s'%(self.bp[self.focus+self.focus1]+1,self.bp[-1]+1)
- except:pass
- self.redraw(None)
- if self.focus!=last[0]:
- self.dtimer.cancel()
- self.scrolling=0
- if self.scrolling!=3 and self.main.measure_text(self.this_line[0],self.font)[0][2]+self.left>=self.main.size[0]:
- self.dtimer.cancel()
- self.scrolling=1
- self.dtimer.after(0.7,lambda:line_auto_scroll(self))
- if way=='select':
- try:selected(self,self.focus+self.focus1)
- except:pass
- def search_handler(self):
- qr=9
- key=None
- #if callable(self.search_control):return self.search_control(self)
- if len(self.search[1])>0:
- tx=u'%s(%s/%s)'%(self.search[2],self.search[0]+1,len(self.search[1]))
- if len(self.bp)==0:qr=appuifw.popup_menu([u'next',u'previous',u'new search'],tx)
- else:
- qr=appuifw.popup_menu([u'next',u'previous',u'new search',u'replace'],tx)
- if qr==None:return 0
- elif qr==0:self.search[0]+=1
- elif qr==1:self.search[0]+=-1
- l=len(self.search[1])
- if self.search[0]>=l:self.search[0]=0
- if self.search[0]<0:self.search[0]=l-1
- if qr in (0,1):
- self.focus=self.search[1][self.search[0]]
- self.focus1=0
- if qr==9 and len(self.bp)>0:
- qr=appuifw.popup_menu([u'new search',u'replace'],u'next step')
- if qr==0:qr=2
- elif qr==1:qr=3
- elif len(self.bp)==0 and qr==9:qr=2
- if qr==3:
- f=get_replace(**self.config())
- if f==None:return 0
- else:
- try:
- self.ls,self.bp=break_text((
- f(u''.join(self.ls))
- ).splitlines(),self.main.size[0]-1-self.left-self.bar_size,self.font,1)
- except:pass
- del f
- elif qr==2:
- sfilter=get_filter(title=u'search',font=self.font,background=self.bg,top=self.top)
- if sfilter==None:return 0
- if len(self.ls)==0 and self.search[0]==[]:
- self.search=[0,[],'']
- return 0
- self.search[1]=list()
- self.search[2]=u'search'
- ls=[]
- if self.bp==[]:target=self.ls
- else:target=''.join(self.ls).split(u'\n')
- for x in xrange(len(target)):
- if sfilter(target[x]):
- if self.bp==[]:
- ls.append(target[x])
- self.search[1].append(x)
- else:
- ls.append(target[x])
- self.search[1].append(self.bp.index(x,x))
- selection=None
- if len(self.search[1])>0:
- if self.bp==[]:ls=map(lambda x:target[x],self.search[1])
- title=u'search'
- config=self.config()
- config['show_lines']=1
- if callable(self.left_draw):
- fun=lambda son,pos,img:self.left_draw(son,self.search[1][pos],img)
- def init(son,self=self):
- son.marked=self.marked
- son.form=self.form
- config['left_draw']=fun
- config['init']=init
- if callable(self.line_draw):
- fun1=lambda son,pos,img:self.left_draw(son,self.search[1][pos],img)
- def init1(son,self=self):
- son.marked=self.marked
- son.form=self.form
- config['line_draw']=fun1
- config['init']=init1
- selection=selection_handler(ls,title,**config)
- if selection!=None:
- self.focus=self.search[1][selection]
- self.search[0]=selection
- if self.search_select=='select':
- self.focus1=0
- return selected(self,self.focus)
- else:
- if len(self.search[1])==0:appuifw.note(u'no match result')
- if len(self.bp)==0:
- try:appuifw.app.title=u'%s/%s'%(self.focus+self.focus1+1,len(self.ls))
- except:pass
- else:
- try:appuifw.app.title=u'%s/%s'%(self.bp[self.focus+self.focus1]+1,self.bp[-1]+1)
- except:pass
- force_refresh(self)
- self.len=len(ls)
- self.focus=focus
- self.left_draw=left_draw
- if left=='font':left=font[1]
- self.left=left
- self.bp=bp
- self.page_control=lambda *arg,**kw:page_control(self,*arg,**kw)
- self.line_draw=line_draw
- self.multiline=multiline
- self.focus1=focus1
- self.form=[]
- self.search_select=search_select
- self.callback=callback
- self.init=init
- self.lastFg=None
- self.fgHook=None
- self.marked=[]
- if callable(search_control):self.search_control=lambda:search_control(self)
- else:self.search_control=lambda:search_handler(self)
- self.selected=None
- self.mode=mode
- self.bg=background
- self.top=top
- self.contentType='list'
- newmenu=list()
- self.config=lambda:{'show_lines':self.show_lines,'font':self.font,'background':self.bg,'top':self.top,'left':self.left}
- self.content=lambda:{'ls':''.join(self.ls),'title':self.title,'focus':self.focus,'focus1':self.focus1}
- self.wrap=lambda:{'show_lines':self.show_lines,'font':self.font,'background':self.bg,'top':self.top,'left':self.left,'ls':ls,'title':self.title,'bp':self.bp}
- #
- if len(appmenu)>0:
- init=lambda x:(x[0],lambda:x[1](self))
- for x in appmenu:
- if type(x[1]) is tuple:m=(x[0],tuple(map(init,x[1])))
- else:m=init(x)
- newmenu.append(m)
- self.appmenu=newmenu
- self.show_lines=show_lines
- self.mid=mid_color(self.bg,self.top)
- self.font=(font[0],max((font[1],9)))
- self.header_font=('normal',min((24,font[1])))
- self.changed={'menu':1,'title':1,'main':1,'focus':None,'focus1':None,'main_mask':1,'main_top':1,'scroll':None,'scrolling':0,'speed':0}
- self.img.clear(self.bg)
- self.title=title
- self.left_zone=Image.new((self.left,self.font[1]))
- self.header=color_fill((self.img.size[0],self.header_font[1]),(self.mid,self.mid,1))
- #self.header.text((5,self.header_font[1]-1),self.title,self.bg,self.header_font)
- global logo
- self.logo=Image.new((self.header_font[1],self.header_font[1]))
- self.logo.clear(self.mid)
- self.logo.blit(logo[0].resize(self.logo.size),mask=logo[1].resize(self.logo.size))
- self.ls=ls
- self.search=[0,[],'']
- self.img.blit(self.header)
- self.bmenu=Image.new((self.img.size[0],min(32,self.img.size[1]/10)))
- self.mfocus=None
- bar_size=max(12,min(8,font[1]/2))
- self.bar_size=bar_size
- self.scroll_bar=color_fill((bar_size,self.img.size[1]-self.header_font[1]-self.bmenu.size[1]-bar_size*2),(self.bg,self.mid,1))
- self.scroll_tap=color_fill((bar_size,bar_size/2),(self.top,self.mid,0))
- self.main=Image.new((self.img.size[0]-bar_size,self.img.size[1]-self.bmenu.size[1]-self.header_font[1]),mode='RGB')
- self.p_temp=Image.new(self.main.size,mode='RGB')
- self.line_zone=Image.new((self.main.size[0]-self.left,self.font[1]))
- self.lines=self.main.size[1]/self.font[1]
- self.main.clear(self.bg)
- self.lastkey=(None,None)
- self.lastclick=(None,None,time.time())
- self.drag_log=list()
- self.drag_temp=list()
- self.dtimer=e32.Ao_timer()
- self.main_mask=Image.new(self.main.size,mode='1')
- self.main_top=Image.new(self.main.size)
- self.main_top.clear(self.top)
- self.main_mask.clear(0)
- self.line_mask=Image.new((self.main.size[0]-self.left,self.font[1]),mode='1')
- self.line_top=Image.new(self.line_mask.size)
- self.line_bottom=Image.new(self.line_mask.size)
- self.line_top.clear(self.top)
- self.line_bottom.clear(self.bg)
- self.line_mask.clear(0)
- self.ttimer=e32.Ao_timer()
- self.this_page=[[],None,None]
- self.this_line=[u'',0]
- self.scrolling=0
- self.focus3=0
- self.last_page=None
- list_select_control(self)
- #def list_select_control(self):
- def quit(self):
- self.ttimer.cancel()
- self.dtimer.cancel()
- self.hide()
- self.quit=lambda:quit(self)
- def search_handler(self):
- qr=9
- key=None
- #if callable(self.search_control):return self.search_control(self)
- if len(self.search[1])>0:
- tx=u'%s(%s/%s)'%(self.search[2],self.search[0]+1,len(self.search[1]))
- if len(self.bp)==0:qr=appuifw.popup_menu([u'next',u'previous',u'new search'],tx)
- else:
- qr=appuifw.popup_menu([u'next',u'previous',u'new search',u'replace'],tx)
- if qr==None:return 0
- elif qr==0:self.search[0]+=1
- elif qr==1:self.search[0]+=-1
- l=len(self.search[1])
- if self.search[0]>=l:self.search[0]=0
- if self.search[0]<0:self.search[0]=l-1
- if qr in (0,1):
- self.focus=self.search[1][self.search[0]]
- self.focus1=0
- if qr==9 and len(self.bp)>0:
- qr=appuifw.popup_menu([u'new search',u'replace'],u'next step')
- if qr==0:qr=2
- elif qr==1:qr=3
- elif len(self.bp)==0 and qr==9:qr=2
- if qr==3:
- f=get_replace(**self.config())
- if f==None:return 0
- else:
- try:
- self.ls,self.bp=break_text((
- f(u''.join(self.ls))
- ).splitlines(),self.main.size[0]-1-self.left-self.bar_size,self.font,1)
- except:pass
- del f
- elif qr==2:
- sfilter=get_filter(title=u'search',font=self.font,background=self.bg,top=self.top)
- if sfilter==None:return 0
- if len(self.ls)==0 and self.search[0]==[]:
- self.search=[0,[],'']
- return 0
- self.search[1]=list()
- self.search[2]=u'search'
- ls=[]
- if self.bp==[]:target=self.ls
- else:target=''.join(self.ls).split(u'\n')
- for x in xrange(len(target)):
- if sfilter(target[x]):
- if self.bp==[]:
- ls.append(target[x])
- self.search[1].append(x)
- else:
- ls.append(target[x])
- self.search[1].append(self.bp.index(x,x))
- selection=None
- if len(self.search[1])>0:
- if self.bp==[]:ls=map(lambda x:target[x],self.search[1])
- title=u'search'
- config=self.config()
- config['show_lines']=1
- if callable(self.left_draw):
- fun=lambda son,pos,img:self.left_draw(son,self.search[1][pos],img)
- def init(son,self=self):
- son.marked=self.marked
- son.form=self.form
- config['left_draw']=fun
- config['init']=init
- if callable(self.line_draw):
- fun1=lambda son,pos,img:self.left_draw(son,self.search[1][pos],img)
- def init1(son,self=self):
- son.marked=self.marked
- son.form=self.form
- config['line_draw']=fun1
- config['init']=init1
- selection=selection_handler(ls,title,**config)
- if selection!=None:
- self.focus=self.search[1][selection]
- self.search[0]=selection
- if self.search_select=='select':
- self.focus1=0
- return selected(self,self.focus)
- else:
- if len(self.search[1])==0:appuifw.note(u'no match result')
- if len(self.bp)==0:
- try:appuifw.app.title=u'%s/%s'%(self.focus+self.focus1+1,len(self.ls))
- except:pass
- else:
- try:appuifw.app.title=u'%s/%s'%(self.bp[self.focus+self.focus1]+1,self.bp[-1]+1)
- except:pass
- force_refresh(self)
- def menu_switch(self):
- if self.mfocus==None:self.mfocus=0
- else:self.mfocus=0
- self.redraw(None)
- def app_title(tx):
- try:appuifw.app.title=tx
- except:pass
- ##**#
- def selected(self,selection):
- l=len(self.ls)-1
- if len(self.ls)==0:selected=None
- elif selection>=l:selected=l
- elif selection<0:selected=0
- else:selected=selection
- if self.callback==None:
- self.selected=selected
- self.quit()
- else:
- self.callback(self,selected)
- def event(self,ev):
- if ev.has_key('scancode'):key_control(self,ev)
- elif ev.has_key('pos'):touch_control(self,ev)
- if callable(self.fgHook):
- fg=get_fg(self)
- if self.lastFg!=fg:self.fgHook(self)
- self.lastFg=fg
- def click_handler(self,pos):
- is_click=(len(self.drag_log)==1 and pos==self.drag_log[0])
- is_drag=(is_click==0)
- in_main=(self.header_font[1]<=pos[1]<self.header_font[1]+self.font[1]*min(self.lines,len(self.ls)-self.focus))
- in_scroll=(self.img.size[0]-self.bar_size*2<=pos[0]<self.img.size[0] and self.header_font[1]<=pos[1]<self.img.size[1]-self.bmenu.size[1])
- in_main2=(self.header_font[1]<=pos[1]<self.header_font[1]+self.main.size[1] and in_scroll==0)
- in_menu=(0<=pos[0]<self.img.size[0] and self.img.size[1]-self.bmenu.size[1]<=pos[1]<self.img.size[1])
- self.drag_log=[]
- def drag_record(self,pos,case=0):
- try:
- if self.drag_log[-1]==pos:pass
- elif len(self.drag_log)==1:self.drag_log.append(pos)
- elif len(self.drag_log)>1 and in_line(self.drag_log[-2],self.drag_log[-1],pos):self.drag_log[-1]=pos
- else:self.drag_log.append(pos)
- except:self.drag_log.append(pos)
- def touch_control(self,ev):
- last=(self.focus,self.focus1)
- #last=(self.changed['focus'],self.changed['focus1'])
- pos,ty=(ev['pos'],ev['type'])
- in_main=(self.header_font[1]<=pos[1]<self.header_font[1]+self.font[1]*min(self.lines,len(self.ls)-self.focus))
- in_scroll=(self.img.size[0]-self.bar_size*2<=pos[0]<self.img.size[0] and self.header_font[1]<=pos[1]<self.img.size[1]-self.bmenu.size[1])
- in_main2=(self.header_font[1]<=pos[1]<self.header_font[1]+self.main.size[1] and in_scroll==0)
- in_menu=(0<=pos[0]<self.img.size[0] and self.img.size[1]-self.bmenu.size[1]<=pos[1]<self.img.size[1])
- if in_main and ty==257 and in_scroll==0:
- fg=(pos[1]-self.header_font[1])/self.font[1]
- t=time.time()
- self.focus1=fg
- if self.scrolling in (0,1) and fg==self.lastclick[0] and t-self.lastclick[2]<2:
- self.page_control('select')
- self.lastclick=(fg,None,t)
- #if self.scrolling not in (0,1):self.lastclick=(fg,None,0)
- self.changed['focus1']=None
- elif in_scroll and len(self.ls)>0:
- if pos[1]<self.bar_size+self.header_font[1]:self.focus=self.focus1
- elif self.img.size[1]-self.bmenu.size[1]>pos[1]>=self.header_font[1]+self.bar_size+self.scroll_bar.size[1]:self.focus=len(self.ls)-1
- else:
- fg=int(float(pos[1]-self.header_font[1]-self.bar_size)/self.scroll_bar.size[1]*len(self.ls))
- self.focus=fg
- elif in_main2 and ty!=257:
- drag_record(self,pos)
- yamount=(self.drag_log[-1][1]-self.drag_log[0][1])/self.font[1]
- if yamount!=0:self.page_control('scroll')
- elif in_menu and ty==257:
- ml=float(len(self.menu_callback))
- mfocus=int(pos[0]/(self.img.size[0]/int(ml)))
- mfocus0=(pos[0]/(self.img.size[0]/ml))-mfocus
- if 0<=mfocus<len(self.menu_callback):self.menu_callback[mfocus]()
- if self.scrolling==3 and ty==257:
- self.scrolling=0
- self.changed['speed']=0
- self.changed['scrolling']=0
- self.changed['scroll']=None
- self.changed['main']=1
- self.changed['focus1']=None
- if ty==258:
- self.changed['main']=1
- self.changed['focus1']=None
- self.redraw(None)
- if self.changed['speed']!=0 and ty==2580:
- self.scrolling=3
- self.changed['scroll']=self.focus
- self.changed['scrolling']=time.time()
- return page_auto_scroll(self)
- if ty==257:
- self.drag_log=[]
- self.drag_temp=[]
- self.scrolling=0
- self.changed['scroll']=None
- self.changed['scrolling']=0
- self.changed['speed']=0
- self.page_control('')
- if last!=(self.focus,self.focus1):
- self.changed['main']=1
- self.changed['focus'],self.changed['focus1']=last
- if len(self.bp)==0:
- try:appuifw.app.title=u'%s/%s'%(self.focus+self.focus1+1,len(self.ls))
- except:pass
- else:
- try:
- appuifw.app.title=u'%s/%s'%(self.bp[self.focus+self.focus1]+1,self.bp[-1]+1)
- except:pass
- self.redraw(None)
- self.page_control('')
- def key_control(self,ev):
- ty,key=(ev['type'],ev['scancode'])
- if not self.mouseEnabled:return 0
- if key==EScancodeSelect:
- if ty==1:t=257
- elif ty==2:t=258
- elif ty==3:return 0
- return touch_control(self,dict(type=t,pos=(self.mouseX,self.mouseY)))
- elif key==EScancodeDownArrow and ty==1 and self.mouseY>=self.canvas.size[1]-1:
- return page_control(self,'linedown')
- elif key==EScancodeUpArrow and ty==1 and self.mouseY<=0:
- return page_control(self,'lineup')
- elif key==EScancodeLeftArrow and ty==1 and self.mouseX<=0:
- return page_control(self,'focusdown')
- elif key==EScancodeRightArrow and ty==1 and self.mouseX>=self.canvas.size[0]-1:
- return page_control(self,'focusup')
- elif ty==2 and 49<=key<58:
- fg=key-49
- try:self.menu_callback[fg]()
- except:pass
- return 0
- else:return 0
- if ty==2:
- self.scrolling=0
- self.changed['main']=1
- self.changed['focus1']=None
- self.redraw(None)
- if self.lastkey==(None,None):
- if ty==2:self.lastkey=(key,ty)
- elif ty==1 and key==EScancodeUpArrow:
- self.page_control('lineup')
- elif ty==1 and key==EScancodeDownArrow:
- self.page_control('linedown')
- elif ty==2 and key==EScancodeLeftArrow:
- if self.mfocus==None:self.page_control('pageup')
- else:
- self.mfocus+=-1
- if self.mfocus<0:self.mfocus=len(self.menu_callback)-1
- if self.mfocus>=len(self.menu_callback):self.mfocus=0
- self.redraw(None)
- elif ty==2 and key==EScancodeRightArrow:
- if self.mfocus==None:self.page_control('pagedown')
- else:
- self.mfocus+=1
- if self.mfocus<0:self.mfocus=len(self.menu_callback)-1
- if self.mfocus>=len(self.menu_callback):self.mfocus=0
- self.redraw(None)
- elif key==EScancodeSelect:
- if self.lastkey[0]==key and self.lastkey[1]==1 and ty==2:
- if self.mfocus==None or len(self.menu_callback)==0:pass
- elif 0<=self.mfocus<len(self.menu_callback):self.menu_callback[self.mfocus]()
- self.lastkey=(key,ty)
- if ty==2:
- self.changed['main']=1
- self.changed['focus1']=None
- self.page_control('')
- def page_auto_scroll(self):
- self.dtimer.cancel()
- if len(self.bp)>0:
- self.scrolling=0
- return 0
- self.scrolling=3
- if self.changed['speed']!=0:
- last=self.focus
- last1=self.focus1
- self.focus=int(self.changed['scroll']+(time.time()-self.changed['scrolling'])*self.changed['speed'])
- l=len(self.ls)
- if self.focus>=l-1:self.focus=l-1
- if self.focus<0:self.focus=0
- fg=min(l-self.focus-1,self.lines-1)
- if self.focus1>=fg:self.focus1=fg
- if self.focus1<0:self.focus1=0
- self.changed['focus']=last
- self.changed['focus1']=self.focus1
- if last!=self.focus:
- self.changed['main']=1
- self.redraw(None)
- self.dtimer.after(0.05,lambda:page_auto_scroll(self))
- if (self.focus==0 and self.changed['speed']<0) or (self.focus==len(self.ls)-1 and self.changed['speed']>0):
- self.dtimer.cancel()
- self.scrolling=0
- self.changed['scroll']=None
- self.changed['speed']=0
- def line_auto_scroll(self):
- self.ttimer.cancel()
- if self.scrolling==3:return page_auto_scroll(self)
- if len(self.this_line)==2:
- self.this_line.append(Image.new((self.main.size[0]-self.left,self.font[1])))
- self.this_line.append(time.time())
- max_chars=self.main.size[0]*3/self.font[1]
- self.this_line[2].clear(self.top)
- t0=self.this_line[3]
- t1=time.time()
- t2=t1-t0
- pos=(0,self.focus1*self.font[1]-1)
- self.this_line[1]+=t2*5
- if len(self.this_line[0])-self.this_line[1]>2:
- tx=self.this_line[0][int(self.this_line[1]):]
- tx+=u' --- '+self.this_line[0]
- else:
- self.this_line[1]=0
- tx=self.this_line[0]
- tpos=((int(self.this_line[1])-self.this_line[1]+1)*self.font[1],self.font[1]-1)
- if tpos[0]>=1:tpos=(0,self.font[1]-1)
- self.this_line[2].text(tpos,tx[:max_chars],self.bg,self.font)
- self.img.blit(self.this_line[2],target=(self.left,pos[1]+self.header_font[1]))
- self.this_line[3]=time.time()
- self.dtimer.cancel()
- self.dtimer.after(1.0/30,lambda:self.redraw(None))
- self.scrolling=1
- def page_redraw(self,force=0):
- fg=get_fg(self)
- if self.changed['main']==0 and self.focus==self.changed['focus'] and self.changed['focus1']==self.focus1 and force==0 and self.changed['focus']!=None:return 0
- max_chars=self.main.size[0]*5/self.font[1]
- if (self.multiline==0 and len(self.ls)>0):
- low,hi=self.focus,min(self.focus+self.lines,len(self.ls))
- t=self.ls[fg]
- if t.endswith('\n'):t=t[:-1]
- if t.endswith('\r'):t=t[:-1]
- t=t.translate(editor_dict)
- self.this_line[0]=t
- if 1 or self.changed['focus']!=self.focus or self.changed['focus']==None:
- self.main.clear(self.bg)
- draw_map=xrange(low,hi)
- for y in draw_map:
- x=y-low
- pos=(self.left,(x+1)*self.font[1]-1)
- t=u'%s'%self.ls[y]
- if t.endswith('\n'):t=t[:-1]
- if t.endswith('\r'):t=t[:-1]
- t=t.translate(editor_dict)
- if callable(self.line_draw):
- self.line_zone=self.line_draw(self,self.focus+x,self.line_zone)
- self.main.blit(self.line_zone,target=(self.left,pos[1]-self.font[1]+1))
- else:self.main.text(pos,t[:max_chars],self.top,self.font)
- if callable(self.left_draw):
- try:
- self.left_zone=self.left_draw(self,self.focus+x,self.left_zone)
- except:self.left_zone.clear(self.top)
- self.main.blit(self.left_zone,target=(0,pos[1]-self.font[1]+1))
- if self.bp!=[]:
- try:
- line0=self.bp[fg]
- line1=self.bp[self.focus+x]
- if line0==line1:self.main.line((0,pos[1],self.main.size[0],pos[1]),self.mid,width=1)
- except:pass
- elif self.show_lines:self.main.line((0,pos[1],self.main.size[0],pos[1]),self.mid,width=1)
- self.changed['focus']=self.focus
- else:self.this_line[0]=u''
- if len(self.ls)==0:self.main.clear(self.bg)
- self.img.blit(self.main,target=(0,self.header_font[1]))
- if (self.mode=='select' and len(self.ls)>0 and self.scrolling==0) and not callable(self.line_draw):
- pos=(self.left,self.font[1]*self.focus1+self.header_font[1])
- self.line_top.clear(self.top)
- self.line_top.text((0,self.font[1]-1),self.this_line[0][:max_chars],self.bg,self.font)
- self.img.blit(self.line_top,target=pos)
- self.changed['focus1']=self.focus1
- self.changed['main']=0
- def set_title(self,title=None):
- if title==None:title=self.title
- self.title=u'%s'%title
- self.changed['title']=1
- self.redraw(None)
- def img_redraw(self):
- if self.changed['title']:
- self.header.clear(self.mid)
- self.header.blit(self.logo)
- self.header.text((self.header_font[1]+5,self.header_font[1]-1),self.title,self.bg,self.header_font)
- self.img.blit(self.header)
- self.changed['title']=0
- self.img.blit(self.scroll_bar,target=(self.main.size[0],self.header_font[1]+self.font[1]/2))
- if self.changed['menu']:
- self.draw_menu()
- self.img.blit(self.bmenu,target=(0,self.img.size[1]-self.bmenu.size[1]))
- self.changed['menu']=0
- def redraw(self,rect):
- if self.scrolling in (0,3,5):page_redraw(self)
- #if self.scrolling==3:page_auto_scroll(self)
- img_redraw(self)
- self.canvas.blit(self.img)
- if self.scrolling==1 and not callable(self.line_draw):line_auto_scroll(self)
- if len(self.ls)>0:
- self.canvas.blit(self.scroll_tap,target=(self.main.size[0],self.header_font[1]+self.bar_size/2+(self.focus+0.0)/len(self.ls)*(self.main.size[1]-self.bar_size*3/2)))
- if self.mfocus!=None:
- l=float(len(self.menu_callback))
- self.canvas.rectangle((self.mfocus*self.img.size[0]/l,self.img.size[1]-self.bmenu.size[1],(self.mfocus+1)*self.img.size[0]/l,self.img.size[1]),self.top,width=self.bmenu.size[1]*2/10)
- self.event_callback=event
- self.redraw_callback=redraw
- menuDefault(self)
- if callable(self.init):self.init(self)
- self.setTitle=lambda title:set_title(self,title)
- self.imenu=self.appmenu
- appuifw.app.menu=self.appmenu
- self.mouseX=15
- self.mouseY=self.font[1]+self.font[1]/2
- self.redraw(None)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement