libwin.py 5.7 KB

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