libwin.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. #!/usr/bin/python3
  2. import os
  3. HOME = os.getenv('HOME')
  4. from lib.cprint import *
  5. import lib.showlib as showlib
  6. import __main__ as MAIN
  7. window_list_buffer = {}
  8. def save_window_geo(save_as=""):
  9. global window_list_buffer
  10. #cprint("save_window_geo as=",[save_as])
  11. error = 0
  12. if save_as:
  13. fname = save_as
  14. fpath=fname
  15. else:
  16. fname = showlib.current_show_name()
  17. fpath = showlib.SHOW_DIR + fname
  18. if not os.path.isdir(fpath):
  19. cprint(" -0 save_window_geo no dir:",fpath,color="red")
  20. error += 1
  21. fpath += "/gui.txt"
  22. #cprint(" fpath:",fpath)
  23. for k in window_list_buffer:
  24. window_list_buffer[k][0] = 0
  25. for k,win in MAIN.window_manager.windows.items():
  26. try:
  27. if not win:
  28. continue
  29. if "tk" not in dir(win):
  30. continue
  31. data = [0,k,'']
  32. if win.tk.winfo_exists():
  33. data[2] = win.tk.geometry()
  34. data[0] = 1
  35. if k == "MAIN":
  36. data[2] = "84x"+data[2].split("x")[-1]
  37. if k not in window_list_buffer:
  38. cprint(" -- new:win:pos",k.ljust(15," "),data) #,color="yellow")
  39. elif window_list_buffer[k][2] != data[2] and data[2]: #geo
  40. cprint(" -- update:win:pos",k.ljust(15," "),data) #,color="yellow")
  41. else:
  42. cprint(" -- ok:win:pos",k.ljust(15," "),data )#,color="green")
  43. else:
  44. if k in window_list_buffer:
  45. data = window_list_buffer[k]
  46. data[0] = 0
  47. cprint(" -- close:win:pos",0,k.ljust(15," "),data,color="red")
  48. window_list_buffer[k] = data
  49. if k in ["PATCH","FIXTURES","DIMMER","FIXTURE-EDITOR","CONFIG"]:
  50. window_list_buffer[k][0] = 0 # overwrite, default is closed.
  51. except Exception as e:
  52. cprint(" -1 Exception:",[k,e],color="red")
  53. lines = ""
  54. for k,data in window_list_buffer.items():
  55. try:
  56. if not data[2]:
  57. continue
  58. line ="{} {} {}\n"
  59. line = line.format(data[0],k,data[2])
  60. lines += line
  61. except Exception as e:
  62. cprint(" -2 Exception:",e,color="red")
  63. error += 1
  64. try:
  65. f = open(fpath,"w")
  66. f.write( lines )
  67. f.close() #f.flush()
  68. except Exception as e:
  69. cprint(" -3 Exception:",fpath,fname,e,color="red")
  70. error += 1
  71. if not error:
  72. cprint(" save_window_geo",fpath,"OK",color="green")
  73. return 1
  74. cprint(" save_window_geo",fpath,"FAIL",color="red")
  75. def save_window_geo_loop(): # like autosave
  76. def loop():
  77. time.sleep(20)
  78. try:
  79. while 1:
  80. save_window_geo()
  81. time.sleep(60)
  82. except Exception as e:
  83. cprint("save_loop",e)
  84. thread.start_new_thread(loop,())
  85. def get_cur_window_geo(_filter="",win=None):
  86. global window_list_buffer
  87. show = None
  88. k = _filter
  89. geo = ""
  90. #cprint("get_cur_window_geo",[_filter])
  91. if _filter in window_list_buffer:
  92. show,k,geo = window_list_buffer[_filter]
  93. cprint(" get_cur_window_geo",[show,k,geo],color="yellow")
  94. if win:
  95. win.tk.geometry(geo)
  96. return show,k,geo
  97. def read_window_geo():
  98. try:
  99. fname = showlib.current_show_path() + "/gui.txt"
  100. cprint("- fname:",fname)
  101. f = open(fname,"r")
  102. lines = f.readlines()
  103. f.close()
  104. out = []
  105. for line in lines:
  106. line = line.strip()
  107. #print(line)
  108. if " " in line:
  109. if line.count(" ") >= 2:
  110. show,name,geo = line.split(" ",2)
  111. elif line.count(" ") == 1:
  112. name,geo = line.split(" ",1)
  113. show = 1
  114. if "--easy" in sys.argv:
  115. if name not in ["MAIN","EXEC","SETUP","EXEC-BTN"]:
  116. show=0
  117. out.append([show,name,geo])
  118. return out
  119. except Exception as e:
  120. cprint("- load_window_geo 145 Exception:",e,color="red")
  121. return
  122. return []
  123. def split_window_show(lines,_filter=""):
  124. try:
  125. for show,name,geo in lines:
  126. #print( "wwWww "*10,[show,name,geo] )
  127. if _filter in name:
  128. return int(show)
  129. except Exception as e:
  130. cprint("- split_window_show 315 Exception:",e,color="red")
  131. def filter_window_geo(lines,_filter=""):
  132. try:
  133. for show,name,geo in lines:
  134. #print( "wwWww "*10,[show,name,geo] )
  135. if _filter in name:
  136. geo = geo.replace("+"," ")
  137. geo = geo.replace("x"," ")
  138. geo = geo.split()
  139. #print( "wwWww "*10,[show,name,geo] )
  140. if len(geo) == 4:
  141. #print( [show,name,geo] )
  142. args = {}
  143. args["width"] = int(geo[0])
  144. args["height"] = int(geo[1])
  145. args["left"] = int(geo[2])
  146. args["top"] = int(geo[3])
  147. return args
  148. except Exception as e:
  149. cprint("- filter_window_geo 341 Exception:",e,color="red")
  150. def load_window_geo(_filter=""):
  151. #print()
  152. global window_list_buffer
  153. #cprint()
  154. cprint(" load_window_geo",[_filter])
  155. try:
  156. lines = read_window_geo()
  157. data = {}
  158. for show,name,geo in lines:
  159. data[name] = [show,name,geo]
  160. window_list_buffer[name] = [show,name,geo]
  161. for name,win in MAIN.window_manager.windows.items():
  162. if not win:
  163. continue
  164. if name not in data:
  165. continue
  166. if _filter:
  167. if _filter != name:
  168. continue
  169. w = data[name][2]
  170. print(" set_win_pos","filter:",[_filter],"Name: {:<20}".format(name),w,win)
  171. try:
  172. win.tk.geometry(w)
  173. except Exception as e:
  174. cprint("- load_window_geo 544 Exception:",e,color="red")
  175. except Exception as e:
  176. cprint("- load_window_geo 335 Exception:",e,color="red")
  177. return