timezone_lib_list_page.py 9.4 KB

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