libwin.py 6.2 KB

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