libwin.py 5.3 KB

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