Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # checkbox, button, and threaded progress
- # joe, 15/01/2015
- import appuifw as A
- import graphics as G
- import os
- from akntextutils import wrap_text_to_array as wt
- class Wait:
- def __init__(self,x,y,r):
- self.x,self.y,self.r=x,y,r
- self.cx,self.cy=self.x,self.y
- self.dx,self.w=2,10
- def update(self,g):
- if (self.x+self.dx > self.cx+self.r) or (self.x+self.dx < self.cx-self.r):
- self.dx=-self.dx
- self.x=self.x+self.dx
- g.line((self.cx-self.r,self.cy,self.cx+self.r,self.cy),0xefefef,width=self.w)
- g.point((self.x,self.y),0xdd0000,width=self.w)
- class Selector:
- def __init__(self,scr,drive):
- self.cx,self.cy=scr[0]/2,scr[1]/2
- self.cur,self.selected,self.active=0,[],1
- self.drive=drive
- def draw(self,g):
- w,p,fnt=14,10,'legend'
- mx=(len(self.drive)*w) + ((len(self.drive)-1)*p)
- px=self.cx-mx/2
- for i in range(len(self.drive)):
- g.text((px+(i*w)+(i*p)+4,self.cy-4),u'%s'%self.drive[i][0],0xffffff,fnt)
- g.rectangle((px+(i*w)+(i*p),self.cy,px+(i*w)+(i*p)+w,self.cy+w),0xdadada,0xffffff)
- if i in self.selected:
- g.line((px+(i*w) + (i*p)+3,self.cy+7,px+(i*w)+(i*p)+6,self.cy+10,px+(i*w)+(i*p)+10,self.cy+2),0,width=2)
- if self.active:
- g.rectangle((px+(self.cur*w) + (self.cur*p)-2,self.cy-2,px+(self.cur*w)+(self.cur*p)+w+1,self.cy+w+1),0x00dd00,width=2)
- class Submit:
- def __init__(self,scr,txt):
- self.cx,self.cy=scr[0]/2,(scr[1]/2)+40
- self.cur,self.active=0,0
- self.text=txt
- def draw(self,g):
- g.rectangle((self.cx-40,self.cy-10,self.cx+40,self.cy+10),0xdadada,0xffffff)
- g.text((self.cx-35,self.cy+4),self.text,0,'legend')
- if self.active:
- g.rectangle((self.cx-41,self.cy-11,self.cx+41,self.cy+11),0x00dd00,width=2)
- class Remover:
- def __init__(self):
- self.run,self.lock,self.state=0,0,'main'
- self.found,self.scanned,self.scanf,self.size=[],'',0,0
- A.app.screen='full'
- self.c=A.Canvas(redraw_callback=self._redraw,event_callback=self._event)
- A.app.body=self.c
- A.app.exit_key_handler=lambda:None
- def exit(self):
- self.run,self.lock=0,0
- def _redraw(self,rect):
- if img:self.c.blit(img)
- def _event(self,sc):
- k=sc['scancode']
- if self.lock:return
- if self.state=='main':
- if sc['type']==3 and k==165:self.exit()
- if sc['type'] is not A.EEventKey:return
- ob=objs[0]
- if k in (15,54):
- ob.cur+=1
- if ob.cur>len(ob.drive)-1:
- ob.cur=0
- elif k in (14,52):
- ob.cur-=1
- if ob.cur<0:
- ob.cur=len(ob.drive)-1
- elif k in (167,53):
- if ob.active:
- if ob.cur not in ob.selected:
- ob.selected.append(ob.cur)
- else:
- ob.selected.remove(ob.cur)
- elif objs[1].active:
- objs[1].active=0
- self.found,self.scanned,self.scanf,self.size=[],'',0,0
- self._thrd()
- self.state='running'
- elif k in (17,56):
- objs[0].active=0
- objs[1].active=1
- elif k in (16,50):
- objs[0].active=1
- objs[1].active=0
- elif self.state=='running':
- if sc['type']==3 and k==165:objs[0].active,self.state=1,'main'
- if sc['type'] is not A.EEventKey:return
- def _del_dir(self,path):
- for i in os.listdir(path):
- A.e32.ao_sleep(1e-04)
- fp=path+'\\'+i
- if os.path.isdir(fp):
- self._del_dir(fp)
- try:os.rmdir(fp)
- except:pass
- else:
- self.size+=os.path.getsize(fp)
- try:os.remove(fp)
- except:pass
- def _clean(self,path):
- for i in os.listdir(path):
- A.e32.ao_sleep(1e-04)
- fp=path+'\\'+i
- if os.path.isdir(fp):
- self.scanned=fp.decode('u8')
- self.scanf+=1
- if fp.lower().endswith('_palbtn'):
- self.found.append(fp)
- self._del_dir(fp)
- try:os.rmdir(fp)
- except:pass
- else:
- self._clean(fp)
- A.e32.ao_sleep(1e-04)
- def _check(self):
- dv=[objs[0].drive[x] for x in objs[0].selected]
- dv.sort()
- for d in dv:self._clean(d.encode('u8'))
- self.lock=0
- def _thrd(self):
- if len(objs[0].selected):
- A.e32.ao_sleep(0)
- import thread
- try:
- self.lock=1
- thread.start_new_thread( self._check,())
- except:pass
- def _rkey(self):
- r,xr,yr=6,scr[0]-14,scr[1]-14
- img.line((xr-r,yr-r,xr+r,yr+r),0xdd0000,width=4)
- img.line((xr-r,yr+r,xr+r,yr-r),0xdd0000,width=4)
- def start(self):
- self.run,th,fnt=1,14,'legend'
- while self.run:
- img.clear(0x222222)
- if self.state=='main':
- r,px,py=80,scr[0]/2,scr[1]/2
- img.rectangle((px-r,py-r,px+r,py+r),fill=0x393939)
- for ob in objs:
- ob.draw(img)
- else:
- if len(objs[0].selected):
- try:
- if not self.lock:
- txt=u'Done !\nScanned Folder: %d'%self.scanf
- if len(self.found):
- txt+='\n\n_PAlbTN Removed: %d\nRemoved Size: %s'%(len(self.found),fmtsize(self.size))
- else:
- txt+='\n\nNo _PAlbTN Found'
- txt=wt(txt,fnt,scr[0])
- for i in range(len(txt)):
- img.text((scr[0]/2-tsize(txt[i],fnt)[0]/2,scr[1]/2+(i*th)),txt[i],0xefefef,fnt)
- self._rkey()
- else:
- txt=u'Checking...\n%s' %self.scanned
- txt=wt(txt,fnt,scr[0])
- for i in range(len(txt)):
- img.text((scr[0]/2-tsize(txt[i],fnt)[0]/2,scr[1]/2+(i*th)),txt[i],0xefefef,fnt)
- wait.update(img)
- except Exception,er:
- txt=unicode(er)
- txt=wt(txt,fnt,scr[0])
- for i in range(len(txt)):
- img.text((scr[0]/2-tsize(txt[i],fnt)[0]/2,scr[1]/2+(i*th)),txt[i],0xdd0000,fnt)
- self._rkey()
- else:
- txt=u'No Drive Selected'
- txt=wt(txt,fnt,scr[0])
- for i in range(len(txt)):
- img.text((scr[0]/2-tsize(txt[i],fnt)[0]/2,scr[1]/2+(i*th)),txt[i],0xdd0000,fnt)
- self._rkey()
- self._redraw(())
- A.e32.ao_sleep(1e-04)
- scr=G.sysinfo.display_pixels()
- img=G.Image.new(scr)
- def fmtsize(num):
- for x in ['bytes','KB','MB']:
- if num < 1024.0:
- return '%3.1f %s' %(num,x)
- num/=1024.0
- def tsize(text,fnt):
- if not isinstance(text,unicode):
- text=unicode(text)
- m=img.measure_text(text,font=fnt)[0]
- w,h=(m[2]-m[0],m[3]-m[1])
- return (w,h)
- wait=Wait(scr[0]/2,scr[1]-20,scr[0]/3)
- drive=[i for i in A.e32.drive_list() if not i.lower().startswith('z')]
- objs=[
- Selector(scr,drive),
- Submit(scr,u'Check...!')
- ]
- ap=Remover()
- ap.start()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement