timezone_lib_list_page.py 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. # -*- coding: utf-8 -*-
  2. import os
  3. import subprocess
  4. import pygame
  5. from libs.roundrects import aa_round_rect
  6. ## local UI import
  7. from UI.constants import Width,Height,ICON_TYPES
  8. from UI.page import Page,PageSelector
  9. from UI.label import Label
  10. from UI.icon_item import IconItem
  11. from UI.util_funcs import midRect
  12. from UI.keys_def import CurKeys, IsKeyStartOrA, IsKeyMenuOrB
  13. from UI.multi_icon_item import MultiIconItem
  14. from UI.icon_pool import MyIconPool
  15. from UI.scroller import ListScroller
  16. from UI.skin_manager import MySkinManager
  17. from UI.lang_manager import MyLangManager
  18. from list_item import ListItem
  19. import myvars
  20. class TimeLibStack:
  21. def __init__(self):
  22. self.stack = list()
  23. def Push(self,data):
  24. if data not in self.stack:
  25. self.stack.append(data)
  26. return True
  27. return False
  28. def Pop(self):
  29. if len(self.stack)<=0:
  30. return None,False
  31. return self.stack.pop(),True
  32. def Last(self):
  33. idx = len(self.stack) -1
  34. if idx < 0:
  35. return "/usr/share/zoneinfo/posix"
  36. else:
  37. return self.stack[ idx ]
  38. def Length(self):
  39. return len(self.stack)
  40. class ListPageSelector(PageSelector):
  41. _BackgroundColor = MySkinManager.GiveColor('Front')
  42. def __init__(self):
  43. self._PosX = 0
  44. self._PosY = 0
  45. self._Height = 0
  46. self._Width = Width
  47. def AnimateDraw(self,x2,y2):
  48. pass
  49. def Draw(self):
  50. idx = self._Parent._PsIndex
  51. if idx < len(self._Parent._MyList):
  52. x = self._Parent._MyList[idx]._PosX+2
  53. y = self._Parent._MyList[idx]._PosY+1
  54. h = self._Parent._MyList[idx]._Height -3
  55. self._PosX = x
  56. self._PosY = y
  57. self._Height = h
  58. aa_round_rect(self._Parent._CanvasHWND,
  59. (x,y,self._Width-4,h),self._BackgroundColor,4,0,self._BackgroundColor)
  60. class TimezoneListPage(Page):
  61. _Icons = {}
  62. _Selector=None
  63. _FootMsg = ["Nav","","","Back","Select"]
  64. _MyList = []
  65. _SwapMyList = []
  66. _ListFont = MyLangManager.TrFont("notosanscjk15")
  67. _MyStack = None
  68. _Scroller = None
  69. _BGpng = None
  70. _BGwidth = 56
  71. _BGheight = 70
  72. def __init__(self):
  73. Page.__init__(self)
  74. self._Icons = {}
  75. self._CanvasHWND = None
  76. self._MyList = []
  77. self._SwapMyList = []
  78. self._MyStack = TimeLibStack()
  79. def buildDirectoryList(self, path):
  80. elements = [
  81. {
  82. 'name': f,
  83. 'file_path': os.path.join(path, f),
  84. 'is_file': os.path.isfile(os.path.join(path, f))
  85. }
  86. for f in os.listdir(path)
  87. ]
  88. return elements
  89. def SyncList(self,path):
  90. alist = self.buildDirectoryList(path)
  91. if not alist:
  92. print("buildDirectoryList empty")
  93. return
  94. self._MyList = []
  95. self._SwapMyList = []
  96. start_x = 0
  97. start_y = 0
  98. hasparent = 0
  99. if self._MyStack.Length() > 0:
  100. hasparent = 1
  101. li = ListItem()
  102. li._Parent = self
  103. li._PosX = start_x
  104. li._PosY = start_y
  105. li._Width = Width
  106. li._Fonts["normal"] = self._ListFont
  107. li._MyType = ICON_TYPES["DIR"]
  108. li.Init("[..]")
  109. self._MyList.append(li)
  110. for i,v in enumerate(sorted(alist)):
  111. li = ListItem()
  112. li._Parent = self
  113. li._PosX = start_x
  114. li._PosY = start_y + (i+hasparent)*ListItem._Height
  115. li._Width = Width
  116. li._Fonts["normal"] = self._ListFont
  117. li._MyType = ICON_TYPES["FILE"]
  118. if not v['is_file']:
  119. li._MyType = ICON_TYPES["DIR"]
  120. else:
  121. li._MyType = ICON_TYPES["FILE"]
  122. li.Init( v['name'] )
  123. li._Path = v["file_path"]
  124. self._MyList.append(li)
  125. for i in self._MyList:
  126. self._SwapMyList.append(i)
  127. def Init(self):
  128. self._PosX = self._Index * self._Screen._Width
  129. self._Width = self._Screen._Width
  130. self._Height = self._Screen._Height
  131. self._CanvasHWND = self._Screen._CanvasHWND
  132. ps = ListPageSelector()
  133. ps._Parent = self
  134. self._Ps = ps
  135. self._PsIndex = 0
  136. self.SyncList("/usr/share/zoneinfo/posix")
  137. icon_for_list = MultiIconItem()
  138. icon_for_list._ImgSurf = MyIconPool.GiveIconSurface("sys")
  139. icon_for_list._MyType = ICON_TYPES["STAT"]
  140. icon_for_list._Parent = self
  141. icon_for_list.Adjust(0,0,18,18,0)
  142. self._Icons["sys"] = icon_for_list
  143. self._BGpng = IconItem()
  144. self._BGpng._ImgSurf = MyIconPool.GiveIconSurface("empty")
  145. self._BGpng._MyType = ICON_TYPES["STAT"]
  146. self._BGpng._Parent = self
  147. self._BGpng.AddLabel("No timezones found on system!", MyLangManager.TrFont("varela22"))
  148. self._BGpng.SetLableColor(MySkinManager.GiveColor('Disabled'))
  149. self._BGpng.Adjust(0,0,self._BGwidth,self._BGheight,0)
  150. self._Scroller = ListScroller()
  151. self._Scroller._Parent = self
  152. self._Scroller._PosX = self._Width - 10
  153. self._Scroller._PosY = 2
  154. self._Scroller.Init()
  155. def Click(self):
  156. if len(self._MyList) == 0:
  157. return
  158. cur_li = self._MyList[self._PsIndex]
  159. if cur_li._MyType == ICON_TYPES["DIR"]:
  160. if cur_li._Path == "[..]":
  161. self._MyStack.Pop()
  162. self.SyncList( self._MyStack.Last() )
  163. self._PsIndex = 0
  164. else:
  165. self._MyStack.Push( self._MyList[self._PsIndex]._Path )
  166. self.SyncList( self._MyStack.Last() )
  167. self._PsIndex = 0
  168. if cur_li._MyType == ICON_TYPES["FILE"]: ## set the current timezone
  169. subprocess.call(['sudo', 'cp', cur_li._Path, '/etc/localtime'])
  170. #copyfile(cur_li._Path, '/etc/localtime')
  171. print("add" , cur_li._Path)
  172. self._Screen.Draw()
  173. self._Screen.SwapAndShow()
  174. def Rescan(self):
  175. self.SyncList("/usr/share/zoneinfo/posix")
  176. self._PsIndex = 0
  177. def KeyDown(self,event):
  178. if IsKeyMenuOrB(event.key):
  179. self.ReturnToUpLevelPage()
  180. self._Screen.Draw()
  181. self._Screen.SwapAndShow()
  182. if event.key == CurKeys["Up"]:
  183. self.ScrollUp()
  184. self._Screen.Draw()
  185. self._Screen.SwapAndShow()
  186. if event.key == CurKeys["Down"]:
  187. self.ScrollDown()
  188. self._Screen.Draw()
  189. self._Screen.SwapAndShow()
  190. if event.key == CurKeys["Right"]:
  191. self.FScrollDown(Step=5)
  192. self._Screen.Draw()
  193. self._Screen.SwapAndShow()
  194. if event.key == CurKeys["Left"]:
  195. self.FScrollUp(Step=5)
  196. self._Screen.Draw()
  197. self._Screen.SwapAndShow()
  198. if IsKeyStartOrA(event.key):
  199. self.Click()
  200. def Draw(self):
  201. self.ClearCanvas()
  202. if len(self._MyList) == 0:
  203. self._BGpng.NewCoord(self._Width/2,self._Height/2)
  204. self._BGpng.Draw()
  205. return
  206. else:
  207. if len(self._MyList) * ListItem._Height > self._Height:
  208. self._Ps._Width = self._Width - 11
  209. self._Ps.Draw()
  210. for i in self._MyList:
  211. if False:
  212. i._Active = True
  213. else:
  214. i._Active = False
  215. if i._PosY > self._Height + self._Height/2:
  216. break
  217. if i._PosY < 0:
  218. continue
  219. i.Draw()
  220. self._Scroller.UpdateSize( len(self._MyList)*ListItem._Height, self._PsIndex*ListItem._Height)
  221. self._Scroller.Draw()
  222. else:
  223. self._Ps._Width = self._Width
  224. self._Ps.Draw()
  225. for i in self._MyList:
  226. if False:
  227. i._Active = True
  228. else:
  229. i._Active = False
  230. if i._PosY > self._Height + self._Height/2:
  231. break
  232. if i._PosY < 0:
  233. continue
  234. i.Draw()