-
Notifications
You must be signed in to change notification settings - Fork 12
/
TagConfig.py
310 lines (253 loc) · 10.6 KB
/
TagConfig.py
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
import sys
if sys.version_info.major == 2:
import Tkinter
import tkColorChooser
import tkFont
elif sys.version_info.major == 3:
import tkinter as Tkinter
import tkinter.colorchooser as tkColorChooser
import tkinter.font as tkFont
else:
raise UserWarning("unknown python version?!")
import Filters
import Config
import util
import re
from functools import partial
LEFT = Tkinter.LEFT
RIGHT = Tkinter.RIGHT
CENTER = Tkinter.CENTER
# TODO list:
# -> Make it possible to add [groups] and [categories] along with regular expressions (currently you can only edit the expressions)
# -> add some color to the window
RE_MODIFIED = False
class ExpressionBar(Tkinter.Frame):
def __init__(self, parent, category, expression_index):
Tkinter.Frame.__init__(self, parent)
self.parent = parent
self.expression = category.re_expressions[expression_index]
self.index = expression_index
self.category = category
self.string_ = Tkinter.StringVar()
self.string_.set(self.expression.pattern)
modcommand = self.register(self.exp_modified)
self.entry = Tkinter.Entry(self, width=75, validate='key', validatecommand=(modcommand, '%P'), textvariable=self.string_)
self.entry.pack(side=LEFT)
label = Tkinter.Label(self, text=" ")
label.pack(side=RIGHT)
def exp_modified(self, text):
if text == self.expression.pattern:
return True
if type(text) is str:
global RE_MODIFIED
self.expression = re.compile(text)
self.category.re_expressions[self.index] = self.expression
RE_MODIFIED = True
return True
return False
class CategoryBar(Tkinter.Frame):
def __init__(self, parent, category, topparent, dialog):
Tkinter.Frame.__init__(self, parent)
self.parent = parent
self.topparent = topparent
self.dialog = dialog
self.category = category
self.is_grid = False
# self.config(background="green")
self.expand_button = Tkinter.Button(self, text="+", command=self.expand, width=1)
self.expand_button.grid(row=0, column=0, sticky='w')
frame = Tkinter.Frame(self, background="gray")
label = Tkinter.Label(frame, text=self.category.category, anchor="w", width=15, background='gray')
label.grid(row=0, column=0, sticky="w")
col_ = 1
for show_ in self.category.show.items():
window = show_[0]
show = show_[1]
cbutton = Tkinter.Checkbutton(frame, background="gray")
if show:
cbutton.select()
cbutton.config(command=partial(self.set_show, window, show, cbutton))
cbutton.grid(row=0, column=col_)
col_ += 1
frame.grid(row=0, column=1, sticky="w")
self.expression_frame = Tkinter.Frame(self)
for row_ in range(0, len(self.category.re_expressions)):
e_ = ExpressionBar(self.expression_frame, self.category, row_)
e_.grid(row=row_, column=0, sticky="w")
def set_show(self, window, currentVal, button):
self.category.show[window] = not self.category.show[window]
def expand(self):
if not self.is_grid:
self.expression_frame.grid(row=1, column=1, sticky='w')
self.expand_button.config(text="-")
else:
self.expression_frame.grid_forget()
self.expand_button.config(text="+")
self.is_grid = not self.is_grid
self.dialog.resize()
class GroupBar(Tkinter.Frame):
def __init__(self, parent, group, dialog):
Tkinter.Frame.__init__(self, parent)
self.parent = parent
self.group = group
self.dialog = dialog
self.is_grid = False
self.expand_button = Tkinter.Button(self, text="+", command=self.expand, width=1)
self.expand_button.grid(row=0, column=0, sticky='w')
frame = Tkinter.Frame(self)
label = Tkinter.Label(frame, text=group.group, anchor="w", width=15, pady=0)
label.grid(row=0, column=0)
self.color_button = Tkinter.Button(frame, text="Color", command=self.set_color, background=self.group.color)
self.color_button.grid(row=0, column=1)
frame.grid(row=0, column=1, sticky="w")
self.category_frame = Tkinter.Frame(self)
title_frame = Tkinter.Frame(self.category_frame)
frame = Tkinter.Frame(title_frame)
label_frame = Tkinter.Frame(frame, width=128, height=21, background="gray")
label_frame.pack_propagate(0)
label = Tkinter.Label(label_frame, text="Window Visibility:", anchor="e", background="gray")
label.pack(side=RIGHT)
label_frame.grid(row=0, column=0, sticky="w")
col_ = 1
# Need to get window count:
for category_ in self.group.categories.items():
category_name = category_[0]
category = category_[1]
for show_ in category.show.items():
window = show_[0]
show = show_[1]
win_frame = Tkinter.Frame(frame, width=28, height=21)
win_frame.pack_propagate(0)
win = Tkinter.Label(win_frame, text=str(window).rjust(3), anchor="w", background="gray")
win.pack(fill="both", expand=True)
win_frame.grid(row=0, column=col_)
col_ += 1
break
frame.grid(row=0, column=1, sticky="w")
title_frame.grid(row=0, column=0, sticky="w")
row_ = 1
for category_ in self.group.categories.items():
category_name = category_[0]
category = category_[1]
c_ = CategoryBar(self.category_frame, category, self.parent, dialog)
c_.grid(row=row_, column=0, sticky="w")
row_ += 1
def set_color(self):
new_color = tkColorChooser.askcolor(parent=self)[1]
if new_color is not None:
self.color_button.config(background=new_color)
self.group.set_color(new_color)
def expand(self):
if not self.is_grid:
self.category_frame.grid(row=1, column=1, sticky='w')
self.expand_button.config(text="-")
else:
self.category_frame.grid_forget()
self.expand_button.config(text="+")
self.is_grid = not self.is_grid
self.dialog.resize()
# Modified tkSimpleDialog
class MainDialog(Tkinter.Toplevel):
def __init__(self, parent, expressions=None):
Tkinter.Toplevel.__init__(self, parent)
self.parent = parent
self.expressions = expressions
# self.expressions.reload()
self.withdraw()
self.iconbitmap(Config.settings.icon_path)
if parent.winfo_viewable():
self.transient(parent)
self.resizable(0, 1)
self.title("Filter Configuration")
self.result = None
self.gen_body()
if not self.initial_focus:
self.initial_focus = self
self.protocol("WM_DELETE_WINDOW", self.cancel)
if self.parent is not None:
self.geometry("+%d+%d" % (parent.winfo_rootx() + 50, parent.winfo_rooty() + 50))
self.deiconify() # become visibile now
self.initial_focus.focus_set()
# wait for window to appear on screen before calling grab_set
self.wait_visibility()
self.grab_set()
self.wait_window(self)
def gen_body(self):
self.body_frame = Tkinter.Frame(self)
self.initial_focus = self.body(self.body_frame)
if not util.platform.osx:
menu = Tkinter.Menu(self)
menu.add_command(label="Accept", command=self.ok)
menu.add_command(label="Cancel", command=self.cancel)
self.config(menu=menu)
else:
frame = Tkinter.Frame(self.body_frame)
ok_button = Tkinter.Button(frame, text="Accept", command=self.ok) # , background=self.group.color)
cancel_button = Tkinter.Button(frame, text="Cancel", command=self.cancel) # .cancel, background=self.group.color)
ok_button.pack(side=LEFT)
cancel_button.pack(side=RIGHT)
frame.grid(row=0, column=1, sticky='sw')
# frame.grid(row=1, column=1, sticky="nsew")
self.body_frame.grid(row=1, column=1, sticky="nsew")
self.grid_columnconfigure(1, weight=1)
self.grid_rowconfigure(1, weight=1)
self.body_frame.grid_columnconfigure(1, weight=1)
self.body_frame.grid_rowconfigure(1, weight=1)
def body(self, master):
canvas = Tkinter.Canvas(master, borderwidth=0, width=168)
self.canvas = canvas
frame = Tkinter.Frame(canvas, borderwidth=0)
self.canvas_frame = frame
vsb = Tkinter.Scrollbar(master, orient="vertical", command=canvas.yview)
for i, group_ in enumerate(Filters.expressions.groups.items()):
group_name = group_[0]
group = group_[1]
g = GroupBar(frame, group, self)
g.grid(row=i, column=1, sticky="ew")
canvas.configure(yscrollcommand=vsb.set)
vsb.grid(row=1, column=2, sticky="ns")
canvas.grid(row=1, column=1, sticky="nsew")
canvas.create_window((0, 0), window=frame, anchor="nw")
canvas.update_idletasks()
self.minsize(frame.winfo_width(), 300)
self.maxsize(1080, frame.winfo_height())
self.resize()
def resize(self):
self.update_idletasks()
self.canvas.config(width=self.canvas_frame.winfo_width(), height=self.winfo_height(),
scrollregion=self.canvas.bbox('all'))
self.config(width=self.canvas.winfo_width())
def ok(self, event=None):
self.withdraw()
self.update_idletasks()
try:
self.apply()
finally:
self.cancel()
def cancel(self, event=None):
# put focus back to the parent window
if self.parent is not None:
self.parent.focus_set()
self.destroy()
Filters.expressions.reload()
def apply(self):
Filters.expressions.save_filter_data()
if RE_MODIFIED:
Filters.expressions.save_filter_expressions()
def destroy(self):
self.initial_focus = None
Tkinter.Toplevel.destroy(self)
# A test configuation:
if __name__ == '__main__':
def launch_dialog():
MainDialog(root)
# I need to call these or color&visibility data will not be loaded:
for i in range(0, 8):
Filters.expressions.add_window(i)
Filters.expressions.reload()
root = Tkinter.Tk()
frame = Tkinter.Frame(root)
frame.pack()
button = Tkinter.Button(frame, text="Open Editor", command=launch_dialog)
button.pack()
root.mainloop()