forked from NuAoA/AnnouncementWindow
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathWindow.py
More file actions
325 lines (288 loc) · 13.4 KB
/
Copy pathWindow.py
File metadata and controls
325 lines (288 loc) · 13.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
import sys
if sys.version_info.major == 2:
import Tkinter
import tkFileDialog
import tkColorChooser
import tkFont
elif sys.version_info.major == 3:
import tkinter as Tkinter
import tkinter.filedialog as tkFileDialog
import tkinter.colorchooser as tkColorChooser
import tkinter.font as tkFont
else:
raise UserWarning("unknown python version?!")
import re
import tkFontChooser
import Config
import Editor
import Filters
import WordColor
import GamelogReader
import util
import os
import TagConfig
from collections import OrderedDict
# import psutil,time
def dict_to_font(dict_):
return tkFont.Font(family=dict_["family"], size=dict_["size"], weight=dict_["weight"], slant=dict_["slant"], overstrike=dict_["overstrike"], underline=dict_["underline"])
class announcement_window(Tkinter.Frame):
def __init__(self, parent, id_):
Tkinter.Frame.__init__(self, parent)
self.parent = parent
self.id = id_
self.show_tags = False
self.index_dict = {}
Filters.expressions.add_window(self.id)
self.customFont = dict_to_font(self.parent.gui_data['font_w%s' % self.id])
self.config_gui = None
self.vsb_pos = 1.0
self.init_text_window()
self.init_pulldown()
def init_text_window(self):
self.text = Tkinter.Text(self, bg="black", wrap="word", font=self.customFont)
self.vsb = Tkinter.Scrollbar(self, orient="vertical", command=self.text.yview)
self.text.configure(yscrollcommand=self.vsb.set)
self.vsb.pack(side="right", fill="y")
self.text.config(cursor="")
self.text.pack(side="left", fill="both", expand=True)
self.text.bind(util.mouse_buttons.right, self.popup)
# link methods
self.insert = self.text.insert
self.delete = self.text.delete
self.get = self.text.get
self.index = self.text.index
self.search = self.text.search
self.tag_add = self.text.tag_add
self.tag_config = self.text.tag_config
self.tag_delete = self.text.tag_delete
self.tag_names = self.text.tag_names
self.tag_cget = self.text.tag_cget
self.config = self.text.config
self.yview = self.text.yview
def init_pulldown(self):
self.pulldown = Tkinter.Menu(self, tearoff=0)
bg = "white"
if util.platform.win or util.platform.osx:
bg = "SystemMenu"
self.pulldown.add_command(label="Window %d" % self.id, activebackground=bg, activeforeground="black")
self.pulldown.add("separator")
self.pulldown.add_command(label="Change Font", command=self.edit_font)
self.pulldown.add_command(label="Toggle Tags", command=self.toggle_tags)
self.pulldown.add_command(label="Clear Window", command=self.clear_window)
def popup(self, event):
if self.focus_get() is not None:
self.pulldown.tk_popup(event.x_root, event.y_root)
def toggle_tags(self):
self.show_tags = not self.show_tags
self.config(state="normal")
self.gen_tags()
self.config(state="disabled")
def edit_font(self):
tup = tkFontChooser.askChooseFont(self.parent, defaultfont=self.customFont)
if tup is not None:
self.customFont = tkFont.Font(font=tup)
self.parent.gui_data['font_w%s' % self.id] = self.customFont.actual()
self.config(font=self.customFont)
def close_config_gui(self):
self.config_gui.destroy()
self.config_gui = None
Filters.expressions.save_filter_data()
Filters.expressions.reload()
self.parent.gen_tags()
def clear_window(self):
self.config(state="normal")
self.delete('1.0', "end")
self.gen_tags(clear_index_dict=True)
self.config(state="disabled")
def gen_tags(self, clear_index_dict=False):
"""Generate the tkinter tags for coloring
"""
self.vsb_pos = (self.vsb.get()[1])
colordict=Config.settings.word_color_dict
for group_ in Filters.expressions.groups.items():
# Group Coloring
group = group_[1]
for category_ in group.categories.items():
category = category_[1]
tag_name = "%s.%s" % (group.group, category.category)
# set_elide =
self.tag_config('%s.elide' % tag_name, foreground="#FFF", elide=not (self.show_tags and category.get_show(self.id)))
self.tag_config(tag_name, foreground=group.color, elide=not category.get_show(self.id))
if clear_index_dict:
self.index_dict[tag_name] = 0
elif not (tag_name in self.index_dict):
self.index_dict[tag_name] = 0
for color in colordict:
# Word Coloring
self.tag_config(color, foreground=colordict[color][0], background=colordict[color][1])
if self.vsb_pos == 1.0:
self.yview("end")
def insert_ann(self, ann):
def insert():
anngroup=ann.get_group()
anncat=ann.get_category()
tag_name = "%s.%s" % (anngroup, anncat)
self.insert("end", "[%s][%s] " % (anngroup, anncat), '%s.elide' % tag_name)
regex=r"(\b"+'\\b|\\b'.join(WordColor.wd.get_all_group_words(anngroup))+"\\b)"
tokenized = re.split(regex, ann.get_text())
for token in tokenized:
hlwordcolor=WordColor.wd.get_colorname(token,anngroup)
self.insert("end", "%s" % token, tag_name)
if hlwordcolor:
start="end-"+str(1+len(token))+"c"
end="end-1c"
self.tag_add(hlwordcolor,start,end)
self.trim_announcements(tag_name)
if ann.get_show(self.id):
insert()
elif Config.settings.save_hidden_announcements:
insert()
def trim_announcements(self, tag_name):
if Config.settings.trim_announcements[self.id]:
self.index_dict[tag_name] += 1
if self.index_dict[tag_name] > Config.settings.trim_announcements[self.id]:
index = int(float(self.text.index('%s.first' % tag_name)))
self.delete("%d.0" % index, "%d.0" % (index + 1))
class main_gui(Tkinter.Tk):
def __init__(self):
Tkinter.Tk.__init__(self)
self.iconbitmap(Config.settings.icon_path)
self.title("Announcement Window+")
self.protocol('WM_DELETE_WINDOW', self.clean_exit)
self.pack_propagate(False)
self.config(bg="Gray", height=700, width=640)
self.customFont = tkFont.Font(family='Lao UI', size=10)
self.gui_data = Config.settings.load_gui_data()
self.gamelog = GamelogReader.gamelog()
self.connect()
self.announcement_windows = OrderedDict([])
self.cpu_max = {}
self.py = None
if self.gui_data is None:
self.gui_data = {"sash_place":int(700 / 3.236), "font_w0":self.customFont.actual(), "font_w1":self.customFont.actual()}
self.locked = False
self.init_menu()
self.init_windows()
self.gen_tags()
# self.parallel()
self.get_announcements(old=Config.settings.load_previous_announcements)
self.pack_announcements()
def init_menu(self):
self.menu = Tkinter.Menu(self, tearoff=0)
options_menu = Tkinter.Menu(self.menu, tearoff=0)
options_menu.add_command(label="Filter Configuration", command=self.config_gui)
options_menu.add_command(label="Edit filters.txt", command=self.open_filters)
options_menu.add_command(label="Reload wordcolor.txt", command=WordColor.wd.reload)
options_menu.add_command(label="Reload filters.txt", command=Filters.expressions.reload)
options_menu.add_command(label="Reload Settings", command=self.reload_settings)
self.settings_menu = Tkinter.Menu(self.menu, tearoff=0)
self.settings_menu.add_command(label="Set Directory", command=self.askpath)
self.settings_menu.add_command(label="Lock Window", command=self.lock_window)
self.menu.add_cascade(label="Settings", menu=self.settings_menu)
self.menu.add_separator()
self.menu.add_cascade(label="Options", menu=options_menu)
# self.menu.add_command(label="Dump CPU info",command = self.dump_info)
self.config(menu=self.menu)
def connect(self):
if not self.gamelog.connect():
# TODO: add dialog when gamelog is not found
pass
def dump_info(self):
print('CPU-MAX:%f' % max(self.cpu_max["CPU"]))
print('CPU-AVG:%f' % (sum(self.cpu_max["CPU"]) / len(self.cpu_max["CPU"])))
print('MEM-MAX:%f MB' % max(self.cpu_max["MEM"]))
print('MEM-AVG:%f MB' % (sum(self.cpu_max["MEM"]) / len(self.cpu_max["MEM"])))
self.cpu_max["CPU"] = []
self.cpu_max["MEM"] = []
def init_windows(self):
self.panel = Tkinter.PanedWindow(self, orient="vertical", sashwidth=5)
self.panel.pack(fill="both", expand=1)
for i in range(0, 2):
self.announcement_windows[i] = announcement_window(self, i)
self.panel.add(self.announcement_windows[0])
self.panel.add(self.announcement_windows[1])
self.panel.update_idletasks()
self.panel.sash_place(0, 0, self.gui_data["sash_place"]) # TODO: update to support multiple sashes
def gen_tags(self):
Filters.expressions.reload()
for announcement_win in self.announcement_windows.items():
announcement_win[1].config(state="normal")
announcement_win[1].gen_tags()
announcement_win[1].config(state="disabled")
def clean_exit(self):
self.gui_data["sash_place"] = self.panel.sash_coord(0)[1]
Config.settings.save_gui_data(self.gui_data)
self.destroy()
def reload_settings(self):
Config.settings.load()
self.gen_tags()
def edit_filters(self):
Editor.TextEditor(Config.settings.filters_path)
def open_filters(self):
Editor.native_open(Config.settings.filters_path)
def config_gui(self):
Filters.expressions.reload()
TagConfig.MainDialog(self)
self.gen_tags()
def askpath(self):
path = Config.settings.get_gamelog_path()
if os.path.isfile(path):
new_path = tkFileDialog.askopenfilename(initialfile=path, parent=self, filetypes=[('text files', '.txt')], title="Locate DwarfFortress/gamelog.txt")
else:
new_path = tkFileDialog.askopenfilename(initialdir=path, parent=self, filetypes=[('text files', '.txt')], title="Locate DwarfFortress/gamelog.txt")
if os.path.isfile(new_path):
Config.settings.set_gamelog_path(new_path)
Config.settings.save()
self.connect()
def lock_window(self):
self.locked = not self.locked
if util.platform.win:
# Window decorations are not restored correctly on OS X when unlocking
self.overrideredirect(self.locked)
self.wm_attributes("-topmost", self.locked)
tog_ = 'Unlock Window' if self.locked else 'Lock Window'
self.settings_menu.entryconfig(self.settings_menu.index('end'), label=tog_)
def get_announcements(self, old=False):
if old:
new_announcements = self.gamelog.get_old_announcements()
else:
new_announcements = self.gamelog.new()
if new_announcements:
for announcement_win in self.announcement_windows.items():
announcement_win[1].vsb_pos = (announcement_win[1].vsb.get()[1]) # Jumps to end of list if the users scrollbar is @ end of list, otherwise holds current position
announcement_win[1].text.config(state="normal")
for ann in new_announcements:
for announcement_win in self.announcement_windows.items():
announcement_win[1].insert_ann(ann)
for announcement_win in self.announcement_windows.items():
if announcement_win[1].vsb_pos == 1.0:
announcement_win[1].yview("end")
announcement_win[1].text.config(state="disabled")
self.after(1000, self.get_announcements)
def pack_announcements(self):
for announcement_win in self.announcement_windows.items():
announcement_win[1].text.pack(side="top", fill="both", expand=True)
# Why doesn't this always move to the end when you launch with setting: load_previous_announcements = True ??
announcement_win[1].yview("end")
#===========================================================================
# def parallel(self): #TODO: remove
# def find_process():
# for pid in psutil.pids():
# if psutil.Process(pid).name() == "python.exe":
# if abs(psutil.Process(pid).create_time()-time.time()) < 5:
# #was made less than 5 seconds ago
# return psutil.Process(pid)
# if self.py is None:
# self.py = find_process()
# self.cpu_max["CPU"] = []
# self.cpu_max["MEM"] = []
# self.py.cpu_percent()
# else:
# self.cpu_max["MEM"].append(self.py.memory_info().rss/(1000*1024))
# self.cpu_max["CPU"].append(self.py.cpu_percent())
# self.after(5000,self.parallel)
#
#===========================================================================
if __name__ == "__main__":
app = main_gui()
app.mainloop()