Advertisement
phjoe

class1

Feb 14th, 2015
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # checkbox, button, and threaded progress
  2. # joe, 15/01/2015
  3.  
  4. import appuifw as A
  5. import graphics as G
  6. import os
  7. from akntextutils import wrap_text_to_array as wt
  8.  
  9. class Wait:
  10.  def __init__(self,x,y,r):
  11.   self.x,self.y,self.r=x,y,r
  12.   self.cx,self.cy=self.x,self.y
  13.   self.dx,self.w=2,10
  14.  
  15.  def update(self,g):
  16.   if (self.x+self.dx > self.cx+self.r) or (self.x+self.dx < self.cx-self.r):
  17.    self.dx=-self.dx
  18.   self.x=self.x+self.dx
  19.   g.line((self.cx-self.r,self.cy,self.cx+self.r,self.cy),0xefefef,width=self.w)
  20.   g.point((self.x,self.y),0xdd0000,width=self.w)
  21.  
  22.  
  23. class Selector:
  24.  def __init__(self,scr,drive):
  25.   self.cx,self.cy=scr[0]/2,scr[1]/2
  26.   self.cur,self.selected,self.active=0,[],1
  27.   self.drive=drive  
  28.  
  29.  def draw(self,g):
  30.   w,p,fnt=14,10,'legend'
  31.   mx=(len(self.drive)*w) + ((len(self.drive)-1)*p)
  32.   px=self.cx-mx/2
  33.   for i in range(len(self.drive)):
  34.    g.text((px+(i*w)+(i*p)+4,self.cy-4),u'%s'%self.drive[i][0],0xffffff,fnt)
  35.    g.rectangle((px+(i*w)+(i*p),self.cy,px+(i*w)+(i*p)+w,self.cy+w),0xdadada,0xffffff)
  36.    if i in self.selected:
  37.     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)
  38.   if self.active:
  39.    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)
  40.  
  41. class Submit:
  42.  def __init__(self,scr,txt):
  43.   self.cx,self.cy=scr[0]/2,(scr[1]/2)+40
  44.   self.cur,self.active=0,0
  45.   self.text=txt
  46.  
  47.  def draw(self,g):
  48.   g.rectangle((self.cx-40,self.cy-10,self.cx+40,self.cy+10),0xdadada,0xffffff)
  49.   g.text((self.cx-35,self.cy+4),self.text,0,'legend')
  50.   if self.active:
  51.    g.rectangle((self.cx-41,self.cy-11,self.cx+41,self.cy+11),0x00dd00,width=2)
  52.  
  53.  
  54. class Remover:
  55.  def __init__(self):
  56.   self.run,self.lock,self.state=0,0,'main'
  57.   self.found,self.scanned,self.scanf,self.size=[],'',0,0
  58.   A.app.screen='full'
  59.   self.c=A.Canvas(redraw_callback=self._redraw,event_callback=self._event)
  60.   A.app.body=self.c
  61.   A.app.exit_key_handler=lambda:None
  62.  
  63.  def exit(self):
  64.   self.run,self.lock=0,0
  65.  
  66.  def _redraw(self,rect):
  67.   if img:self.c.blit(img)
  68.  
  69.  def _event(self,sc):
  70.   k=sc['scancode']
  71.   if self.lock:return
  72.   if self.state=='main':
  73.    if sc['type']==3 and k==165:self.exit()
  74.    if sc['type'] is not A.EEventKey:return
  75.    ob=objs[0]
  76.    if k in (15,54):
  77.      ob.cur+=1
  78.      if ob.cur>len(ob.drive)-1:
  79.       ob.cur=0
  80.    elif k in (14,52):
  81.      ob.cur-=1
  82.      if ob.cur<0:
  83.       ob.cur=len(ob.drive)-1
  84.    elif k in (167,53):
  85.     if ob.active:
  86.      if ob.cur not in ob.selected:
  87.       ob.selected.append(ob.cur)
  88.      else:
  89.       ob.selected.remove(ob.cur)
  90.     elif objs[1].active:
  91.      objs[1].active=0
  92.      self.found,self.scanned,self.scanf,self.size=[],'',0,0
  93.      self._thrd()
  94.      self.state='running'
  95.    elif k in (17,56):
  96.      objs[0].active=0
  97.      objs[1].active=1
  98.    elif k in (16,50):
  99.      objs[0].active=1
  100.      objs[1].active=0
  101.   elif self.state=='running':
  102.    if sc['type']==3 and k==165:objs[0].active,self.state=1,'main'
  103.    if sc['type'] is not A.EEventKey:return
  104.  
  105.  def _del_dir(self,path):
  106.   for i in os.listdir(path):
  107.    A.e32.ao_sleep(1e-04)
  108.    fp=path+'\\'+i
  109.    if os.path.isdir(fp):
  110.     self._del_dir(fp)
  111.     try:os.rmdir(fp)
  112.     except:pass
  113.    else:
  114.     self.size+=os.path.getsize(fp)
  115.     try:os.remove(fp)
  116.     except:pass
  117.  
  118.  def _clean(self,path):
  119.   for i in os.listdir(path):
  120.    A.e32.ao_sleep(1e-04)
  121.    fp=path+'\\'+i
  122.    if os.path.isdir(fp):
  123.     self.scanned=fp.decode('u8')
  124.     self.scanf+=1
  125.     if fp.lower().endswith('_palbtn'):
  126.      self.found.append(fp)
  127.      self._del_dir(fp)
  128.      try:os.rmdir(fp)
  129.      except:pass
  130.     else:
  131.      self._clean(fp)
  132.   A.e32.ao_sleep(1e-04)
  133.  
  134.  def _check(self):
  135.   dv=[objs[0].drive[x] for x in objs[0].selected]
  136.   dv.sort()
  137.   for d in dv:self._clean(d.encode('u8'))
  138.   self.lock=0
  139.  
  140.  def _thrd(self):
  141.   if len(objs[0].selected):
  142.    A.e32.ao_sleep(0)
  143.    import thread
  144.    try:
  145.     self.lock=1
  146.     thread.start_new_thread( self._check,())
  147.    except:pass
  148.  
  149.  def _rkey(self):
  150.   r,xr,yr=6,scr[0]-14,scr[1]-14
  151.   img.line((xr-r,yr-r,xr+r,yr+r),0xdd0000,width=4)
  152.   img.line((xr-r,yr+r,xr+r,yr-r),0xdd0000,width=4)
  153.  
  154.  def start(self):
  155.   self.run,th,fnt=1,14,'legend'
  156.   while self.run:
  157.    img.clear(0x222222)
  158.    if self.state=='main':
  159.     r,px,py=80,scr[0]/2,scr[1]/2
  160.     img.rectangle((px-r,py-r,px+r,py+r),fill=0x393939)
  161.     for ob in objs:
  162.      ob.draw(img)
  163.    else:
  164.     if len(objs[0].selected):
  165.      try:
  166.       if not self.lock:
  167.        txt=u'Done !\nScanned Folder: %d'%self.scanf
  168.        if len(self.found):
  169.         txt+='\n\n_PAlbTN Removed: %d\nRemoved Size: %s'%(len(self.found),fmtsize(self.size))
  170.        else:
  171.         txt+='\n\nNo _PAlbTN Found'
  172.        txt=wt(txt,fnt,scr[0])
  173.        for i in range(len(txt)):
  174.         img.text((scr[0]/2-tsize(txt[i],fnt)[0]/2,scr[1]/2+(i*th)),txt[i],0xefefef,fnt)
  175.        self._rkey()
  176.       else:
  177.        txt=u'Checking...\n%s' %self.scanned
  178.        txt=wt(txt,fnt,scr[0])
  179.        for i in range(len(txt)):
  180.         img.text((scr[0]/2-tsize(txt[i],fnt)[0]/2,scr[1]/2+(i*th)),txt[i],0xefefef,fnt)
  181.        wait.update(img)
  182.      except Exception,er:
  183.       txt=unicode(er)
  184.       txt=wt(txt,fnt,scr[0])
  185.       for i in range(len(txt)):
  186.        img.text((scr[0]/2-tsize(txt[i],fnt)[0]/2,scr[1]/2+(i*th)),txt[i],0xdd0000,fnt)
  187.       self._rkey()
  188.     else:
  189.      txt=u'No Drive Selected'
  190.      txt=wt(txt,fnt,scr[0])
  191.      for i in range(len(txt)):
  192.       img.text((scr[0]/2-tsize(txt[i],fnt)[0]/2,scr[1]/2+(i*th)),txt[i],0xdd0000,fnt)
  193.      self._rkey()
  194.    self._redraw(())
  195.    A.e32.ao_sleep(1e-04)
  196.  
  197.  
  198.  
  199.  
  200. scr=G.sysinfo.display_pixels()
  201. img=G.Image.new(scr)
  202. def fmtsize(num):
  203.  for x in ['bytes','KB','MB']:
  204.   if num < 1024.0:
  205.    return '%3.1f %s' %(num,x)
  206.   num/=1024.0
  207. def tsize(text,fnt):
  208.  if not isinstance(text,unicode):
  209.   text=unicode(text)
  210.  m=img.measure_text(text,font=fnt)[0]
  211.  w,h=(m[2]-m[0],m[3]-m[1])
  212.  return (w,h)
  213.  
  214.  
  215. wait=Wait(scr[0]/2,scr[1]-20,scr[0]/3)
  216. drive=[i for i in A.e32.drive_list() if not i.lower().startswith('z')]
  217. objs=[
  218.  Selector(scr,drive),
  219.  Submit(scr,u'Check...!')
  220.  ]
  221.  
  222. ap=Remover()
  223. ap.start()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement