-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathgeo_logic.py
executable file
·338 lines (301 loc) · 13 KB
/
geo_logic.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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
#!/usr/bin/python3
import gi as gtk_import
gtk_import.require_version("Gtk", "3.0")
from gi.repository import Gtk, Gdk, GLib
import numpy as np
from geo_object import *
from parse import Parser, type_to_c
import primitive_pred
from collections import defaultdict
from tool_step import ToolStep, proof_checker
from file_chooser import select_file_open, select_file_save, add_svg_filters
from stop_watch import print_times, StopWatch
from itertools import islice
from viewport import Viewport
from graphical_env import GraphicalEnv
from movable_tools import MovableTool
from basic_tools import load_tools, ImportedTools
from gtool import ObjSelector, GTool, GToolMove, GToolHide
from gtool_general import GToolDict
from gtool_label import GToolLabel
from gtool_logic import GToolReason
from gtool_constr import ComboPoint, ComboLine, ComboPerpLine, ComboCircle, ComboCircumCircle
from toolbar import ToolBar
from step_list import StepList
from logical_core import LogicalCore
class GeoLogic(Gtk.Window):
def __init__(self):
super(GeoLogic, self).__init__()
self.imported_tools = load_tools("macros.gl")
self.env = GraphicalEnv(self.imported_tools)
self.vis = self.env.vis
self.general_tools = GToolDict(self.imported_tools.tool_dict)
self.keyboard_capture = None
menu_items = (
("Undo", self.undo, "<Control>z"),
("Redo", self.redo, "<Control><Shift>z"),
("Reset", self.restart, "<Control>n"),
("Open...", self.load, "<Control>o"),
("Save", self.save, "<Control>s"),
("Save as...", self.save_as, "<Control><Shift>s"),
("Export SVG...", self.export_svg, "<Control><Shift>e"),
("Print numerical values...", self.print_num_values, "<Control>p"),
("Quit", self.on_exit, "<Control>q"),
)
gtools = (
ComboPoint(),
ComboLine(), ComboPerpLine(),
ComboCircle(), ComboCircumCircle(),
GToolMove(), GToolHide(), GToolLabel(),
GToolReason(),
)
self.key_to_gtool = dict(
(gtool.get_key_shortcut(), gtool)
for gtool in gtools
)
vbox = Gtk.VBox()
self.add(vbox)
self.toolbar = ToolBar(menu_items, gtools, self.general_tools)
vbox.pack_start(self.toolbar, False, False, 0)
hpaned = Gtk.HPaned()
vbox.add(hpaned)
self.step_list = StepList(self.env)
hpaned.pack1(self.step_list, False, True)
self.viewport = Viewport(self.env, self)
self.viewport.set_tool(ComboPoint())
hpaned.pack2(self.viewport.darea, True, False)
hpaned.set_position(250)
self.viewport.click_hook = self.toolbar.entry.unselect
self.toolbar.set_entry_unselect(self.viewport.darea.grab_focus)
self.toolbar.change_tool_hook = self.viewport.set_tool
self.add_accel_group(self.toolbar.accelerators)
def update_progress_bar(done, size):
if size == 0: self.progress_bar.set_fraction(0)
else: self.progress_bar.set_fraction(done / size)
return False
def update_progress_bar_idle(done, size):
GLib.idle_add(update_progress_bar, done, size)
proof_checker.paralelize(progress_hook = update_progress_bar_idle)
self.progress_bar = Gtk.ProgressBar(show_text=False)
vbox.pack_end(self.progress_bar, False, False, 0)
self.connect("key-press-event", self.on_key_press)
self.connect("key-release-event", self.on_key_release)
self.connect("delete-event", self.on_exit)
self.resize(1000, 600)
self.set_position(Gtk.WindowPosition.CENTER)
self.set_events(Gdk.EventMask.KEY_PRESS_MASK |
Gdk.EventMask.KEY_RELEASE_MASK)
self.show_all()
self.update_title(None)
# changes the default filename for saving and the title of the window
def update_title(self, fname):
self.default_fname = fname
title = "GeoLogic"
if fname is not None:
title = "{} -- {}".format(title, fname.split('/')[-1])
self.set_title(title)
def reset_view(self):
if self.vis.view_changed:
self.viewport.gtool.reset()
self.viewport.darea.queue_draw()
def undo(self):
proof_checker.disable()
self.env.pop_step()
self.reset_view()
proof_checker.enable()
def redo(self):
self.env.redo()
self.reset_view()
def restart(self):
#print("RESTART")
self.update_title(None)
self.viewport.reset_tool()
self.env.set_steps((), ())
self.viewport.reset_zoom()
self.reset_view()
def load(self):
fname = select_file_open(self)
self.load_file(fname)
def save(self):
fname = self.default_fname
if fname is None: fname = select_file_save(self)
self.save_file(fname)
def save_as(self):
fname = select_file_save(self)
self.save_file(fname)
def export_svg(self):
fname = select_file_save(
self, "Export SVG", folder = "pictures",
add_filters = add_svg_filters,
)
if fname is not None: self.viewport.export_svg(fname)
def on_exit(self, *args):
print_times()
Gtk.main_quit()
def load_file(self, fname):
self.viewport.reset_tool()
if fname is None: return
self.update_title(fname)
parser = Parser(self.imported_tools.tool_dict)
parser.parse_file(fname, axioms = True)
loaded_tool = parser.tool_dict['_', ()]
steps = loaded_tool.assumptions
names = [
loaded_tool.var_to_name_proof[x]
for x in range(len(loaded_tool.var_to_name_proof))
]
if loaded_tool.implications:
goals, proof = loaded_tool.implications, loaded_tool.proof
else: goals, proof = None, None
self.env.set_steps(steps, names = names,
goals = goals, proof = proof)
# hide objects
visible = set(loaded_tool.result) # old format
if visible:
for gi in range(len(self.vis.gi_to_hidden)):
self.vis.gi_to_hidden[gi] = gi not in visible
for gi,name in enumerate(names): # new format
if ("hide__{}".format(name), ()) in parser.tool_dict:
self.vis.gi_to_hidden[gi] = True
# set labels
for gi,name in enumerate(names): # new format
label_pos_tool = parser.tool_dict.get(("label__{}".format(name), ()), None)
if label_pos_tool is not None:
self.vis.gi_label_show[gi] = True
logic = LogicalCore(basic_tools = self.imported_tools)
pos_l = label_pos_tool.run((), (), logic, 0)
pos_n = [logic.num_model[x] for x in pos_l]
t = self.vis.gi_to_type(gi)
if t == Point:
point, = pos_n
position = point.a
print(position)
elif t == Line:
position = tuple(d.x for d in pos_n)
elif t == Circle:
ang, offset = pos_n
position = ang.data*2, offset.x
else:
print("Warning: save label: unexpected type {} of {}".format(t, name))
continue
self.vis.gi_label_position[gi] = position
self.vis.refresh()
# viewport zoom and position
view_data_tool = parser.tool_dict.get(('view__data', ()), None)
if view_data_tool is None:
self.viewport.set_zoom((375, 277), 1)
else:
logic = LogicalCore(basic_tools = self.imported_tools)
anchor_l, zoom_l = view_data_tool.run((), (), logic, 0)
anchor = logic.num_model[anchor_l].a
zoom = logic.num_model[zoom_l].x
self.viewport.set_zoom(anchor, zoom)
self.reset_view()
def save_file(self, fname):
if fname is None: return
print("saving to '{}'".format(fname))
self.update_title(fname)
with open(fname, 'w') as f:
#visible = [
# "{}:{}".format(
# self.env.gi_to_name[gi],
# type_to_c[self.vis.gi_to_type(gi)]
# )
# for gi,hidden in enumerate(self.vis.gi_to_hidden)
# if not hidden
#]
#f.write('_ -> {}\n'.format(' '.join(sorted(visible))))
f.write('_ ->\n')
def write_step(step):
tokens = [self.env.gi_to_name[x] for x in step.local_outputs]
tokens.append('<-')
tokens.append(step.tool.name)
tokens.extend(map(str, step.hyper_params))
tokens.extend(self.env.gi_to_name[x] for x in step.local_args)
f.write(' '+' '.join(tokens)+'\n')
if self.env.goals is None:
for step in self.env.steps: write_step(step)
else:
for step in self.env.steps[:self.env.min_steps]: write_step(step)
f.write(' THEN\n')
for step in self.env.goals: write_step(step)
f.write(' PROOF\n')
for step in self.env.steps[self.env.min_steps:]: write_step(step)
# hidden objects and labels
f.write("\n")
for gi, (name, hidden, label_show, label_pos) in enumerate(zip(
self.env.gi_to_name, self.vis.gi_to_hidden, self.vis.gi_label_show, self.vis.gi_label_position)):
if hidden: f.write("hide__{} ->\n".format(name))
if label_show:
t = self.vis.gi_to_type(gi)
if t == Point:
label_attrs = [('pos', 'P', 'free_point', tuple(map(float, label_pos)))]
elif t == Line:
pos, offset = label_pos
label_attrs = [
('pos', 'D', 'custom_ratio', (float(pos), 0.)),
('offset', 'D', 'custom_ratio', (float(offset), 0.)),
]
elif t == Circle:
direction, offset = label_pos
label_attrs = [
('direction', 'A', 'custom_angle', (float(direction/2),)),
('offset', 'D', 'custom_ratio', (float(offset), 0.)),
]
else:
print("Warning: save label: unexpected type {} of {}".format(t, name))
continue
label_attr_names = [
"{}:{}".format(attr_name, t) for attr_name, t, _, _ in label_attrs
]
f.write("label__{} -> {}\n".format(name, ' '.join(label_attr_names)))
for attr_name, _, attr_constructor, attr_values in label_attrs:
f.write(" {} <- {} {}\n".format(attr_name, attr_constructor,
' '.join(map(str, attr_values))))
# writing current zoom and position
f.write("\n")
f.write("view__data -> anchor:P zoom:D\n")
f.write(" anchor <- free_point {} {}\n".format(*self.viewport.view_center))
f.write(" zoom <- custom_ratio {} 0.\n".format(self.viewport.scale))
self.reset_view()
def on_key_press(self,w,e):
if self.keyboard_capture is not None:
return self.keyboard_capture(e)
keyval = e.keyval
keyval_name = Gdk.keyval_name(keyval)
modifier = Gdk.ModifierType.CONTROL_MASK | Gdk.ModifierType.SHIFT_MASK
if e.state & modifier: return False
if self.toolbar.entry.is_focus(): return
if keyval_name == "Return":
self.toolbar.entry.select()
return True
elif keyval_name == "Escape":
self.viewport.gtool.reset()
if self.viewport.vis.view_changed:
self.viewport.darea.queue_draw()
return True
elif keyval_name in self.key_to_gtool:
gtool = self.key_to_gtool[keyval_name]
self.toolbar.select_tool_button(gtool)
self.reset_view()
return True
elif keyval_name.startswith("Shift"):
self.viewport.update_shift_pressed(True)
self.viewport.darea.queue_draw()
return False
else:
#print(keyval_name)
return False
def on_key_release(self,w,e):
keyval_name = Gdk.keyval_name(e.keyval)
if keyval_name.startswith("Shift"):
self.viewport.update_shift_pressed(False)
self.viewport.darea.queue_draw()
def print_num_values(self):
for gi,li in enumerate(self.env.step_env.local_to_global):
name = self.env.gi_to_name[gi]
num_obj = self.env.logic.num_model[li]
print(name, num_obj)
if __name__ == "__main__":
win = GeoLogic()
Gtk.main()