-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
384 lines (293 loc) · 11.6 KB
/
main.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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
from re import sub
from kivy.app import App
from kivy.clock import Clock
from kivy.core.text import LabelBase
from kivy.garden.matplotlib.backend_kivyagg import FigureCanvasKivyAgg
from kivy.garden.navigationdrawer import NavigationDrawer
from kivy.lang import Builder
from kivy.uix.anchorlayout import AnchorLayout
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.codeinput import CodeInput, TextInput
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.gridlayout import GridLayout
from kivy.uix.label import Label
from kivy.uix.relativelayout import RelativeLayout
from kivy.uix.scrollview import ScrollView
from kivy.uix.button import Button
from matplotlib.pyplot import gcf
from numpy import ndarray
from pygments.styles import get_style_by_name
from kivy.clock import Clock
from lexer import PycalcLexer, PycalcStyle
from newlang import State
from kivy.uix.popup import Popup
from matplotlib.figure import Figure
from kivy.storage.jsonstore import JsonStore
from functools import partial
Builder.load_file('./main.kv')
state = State()
lexer = PycalcLexer()
lexer.state = state
style = PycalcStyle
FONTS = [
{
"name": "VeraMono",
"fn_regular": "fonts/VeraMono.ttf",
"fn_bold": "fonts/VeraMono-Bold.ttf",
"fn_italic": "fonts/VeraMono-Italic.ttf",
"fn_bolditalic": "fonts/VeraMono-Bold-Italic.ttf"
}
]
for font in FONTS:
LabelBase.register(**font)
class ScrollableText(ScrollView):
codeinput_touced_ind = [-1, -1]
def __init__(self, **kwargs):
super(ScrollableText, self).__init__(**kwargs)
self.layout = BoxLayout(orientation='vertical',
spacing=10, size_hint_y=None)
self.layout.bind(minimum_height=self.layout.setter('height'))
self.add_new_codeline()
self.add_widget(self.layout)
def add_new_codeline(self, ind=0):
def create_clock(widget, touch):
if not isinstance(widget, CodeInput):
return
if not widget.collide_point(*touch.pos):
return
callback = partial(self.add_line_before, widget)
Clock.schedule_once(callback, 1)
touch.ud['event'] = callback
def delete_clock(widget, touch):
if not isinstance(widget, CodeInput):
return
if 'event' in touch.ud:
Clock.unschedule(touch.ud['event'])
del touch.ud['event']
codeinput = PycalcCodeInput(lexer=lexer, style=style)
codeinput.bind(on_text_validate=lambda _: self.code_interpret(codeinput))
codeinput.bind(
on_touch_down=create_clock,
on_touch_up=delete_clock
)
codeinput.focus = True
if ind == 0:
codeinput.ind = len(self.layout.children)
else:
codeinput.ind = len(self.layout.children) - ind
self.layout.add_widget(codeinput, index=ind)
def add_line_before(self, codeinput, *args):
if codeinput == self.layout.children[0]:
return
ind = codeinput.ind
for wid in self.layout.children:
if hasattr(wid, 'ind') and wid.ind >= ind:
wid.ind += 2
self.add_new_codeline(len(self.layout.children)-ind)
def reinterpret_line(self, codeinput):
if len(codeinput.text.strip()) == 0:
if hasattr(codeinput, 'out'):
self.layout.remove_widget(codeinput.out)
self.layout.remove_widget(codeinput)
for wid in list(reversed(self.layout.children))[codeinput.ind:]:
if hasattr(wid, 'ind'):
wid.ind -= 2
return
res, err = state.parse(codeinput.text)
if hasattr(codeinput, 'out'):
self.print_output(codeinput.out, res, err)
else:
self.make_output(codeinput, res, err)
def print_output(self, co, res, err):
self._hide_widget(co, False)
co.clear_widgets()
if err:
co.text = str(err)
co.color = (1, 0, 0, 1)
else:
if isinstance(res, ndarray):
co.text = ' ' + sub(r'[\[\]]', '', str(res))
elif isinstance(res, Figure):
plot_widget = FigureCanvasKivyAgg(res)
plot_widget.size = self.size
co.size = plot_widget.size
co.add_widget(plot_widget)
return
else:
co.text = str(res)
co.color = (1, 1, 1, 1)
co.text_size = self.width, None
co.size_update()
self._hide_widget(co, True)
def code_interpret(self, codeinput):
global state
global lexer
if codeinput != self.layout.children[0]:
state = State()
lexer.state = state
for ci in reversed(self.layout.children):
if isinstance(ci, CodeInput) and ci != self.layout.children[0]:
self.reinterpret_line(ci)
ci._trigger_refresh_text()
if hasattr(codeinput, 'out'):
self.open_output(codeinput.out)
return
if len(codeinput.text.strip()) == 0:
return
res, err = state.parse(codeinput.text)
self.make_output(codeinput, res, err)
self.add_new_codeline()
def make_output(self, codeinput, res, err):
codeoutput = CodeOutput()
codeinput.bind(on_touch_down=lambda *_: self.open_output(codeoutput))
self.print_output(codeoutput, res, err)
self.open_output(codeoutput)
codeinput.out = codeoutput
ind = len(self.layout.children) - codeinput.ind - 1
self.layout.add_widget(codeoutput, ind)
def open_output(self, codeoutput):
for c in self.layout.children:
if isinstance(c, CodeOutput):
self._hide_widget(c, True)
self._hide_widget(codeoutput, False)
def _hide_widget(self, wid, dohide=True):
if hasattr(wid, 'saved_attrs'):
if not dohide:
wid.height, wid.size_hint_y, wid.opacity, wid.disabled = wid.saved_attrs
del wid.saved_attrs
elif dohide:
wid.saved_attrs = wid.height, wid.size_hint_y, wid.opacity, wid.disabled
wid.height, wid.size_hint_y, wid.opacity, wid.disabled = 0, None, 0, True
class AppView(BoxLayout):
pass
class CodeOutput(Label):
def size_update(self, **kwargs):
self.texture_update()
self.size = self.texture_size
class PycalcCodeInput(CodeInput):
pass
class CalcPyApp(App):
def build(self):
self.store = JsonStore('scripts.json')
self.appview = NavigationDrawer()
self.appview.anim_type = 'reveal_below_anim'
side_panel = BoxLayout(orientation='vertical')
title = Label(text='Pycalc')
title.size = self.appview.width, 30
title.size_hint_y = None
side_panel.add_widget(title)
new_btn = Button(text='New Script')
new_btn.size = self.appview.width, 50
new_btn.size_hint_y = None
new_btn.bind(on_press=lambda _: self.new_script())
side_panel.add_widget(new_btn)
s_in = TextInput()
s_in.multiline = False
s_in.size = self.appview.width, 50
s_in.size_hint_y = None
def show_s_in():
s_in.height, s_in.opacity, s_in.disabled = 50, 1, False
save_btn.height, save_btn.opacity, save_btn.disabled = 0, 0, True
def focus():
s_in.focus = True
Clock.schedule_once(lambda dt: focus(), 0.1)
def hide_s_in(x = False):
if x:
return
s_in.height, s_in.opacity, s_in.disabled = 0, 0, True
save_btn.height, save_btn.opacity, save_btn.disabled = 50, 1, False
save_btn = Button(text='Save Script')
save_btn.size = self.appview.width, 50
save_btn.size_hint_y = None
save_btn.bind(on_press=lambda _: show_s_in())
side_panel.add_widget(save_btn)
s_in.bind(on_text_validate= lambda instance: self.save_script(instance.text.strip()))
s_in.bind(focused=lambda _, x: hide_s_in(x))
hide_s_in()
side_panel.add_widget(s_in)
load_btn = Button(text='Load Script')
load_btn.size = self.appview.width, 50
load_btn.size_hint_y = None
load_btn.bind(on_press=lambda _: self.load_script())
side_panel.add_widget(load_btn)
self.appview.add_widget(side_panel)
self.main_panel = ScrollableText()
self.appview.add_widget(self.main_panel)
return self.appview
def new_script(self):
global state
global lexer
state = State()
lexer.state = state
self.main_panel.layout.clear_widgets()
self.main_panel.add_new_codeline()
def save_script(self, name):
lines = []
for wid in reversed(self.main_panel.layout.children):
if isinstance(wid, CodeInput) and len(wid.text) > 0:
lines.append(wid.text)
if len(lines) > 0:
self.store.put(name, data=lines)
def load_script(self):
popup = Popup(title='Load script')
def want_delete(btn, choice_callback, _):
def delete(name):
self.store.delete(name)
btn.height, btn.opacity, btn.disabled = 0, 0, True
saved_text = btn.text
callback = lambda _: delete(saved_text)
def lost_focus(_):
btn.text = saved_text
btn.color = (1, 1, 1, 1)
btn.unbind(on_press=callback)
btn.bind(on_release=choice_callback)
btn.text = 'delete?'
btn.color = (1, 0, 0, 1)
btn.bind(on_press=callback)
Clock.schedule_once(lost_focus, 2)
btn.unbind(on_release=choice_callback)
def create_clock(widget, touch, choice_callback, *args):
if not isinstance(widget, Button):
return
if not widget.collide_point(*touch.pos):
return
callback = partial(want_delete, widget, choice_callback)
Clock.schedule_once(callback, 0.5)
touch.ud['event'] = callback
def delete_clock(widget, touch, *args):
if not isinstance(widget, Button):
return
if 'event' in touch.ud:
Clock.unschedule(touch.ud['event'])
del touch.ud['event']
def choice(name):
self.new_script()
data = self.store.get(name)['data']
for line in data:
codeinput = self.main_panel.layout.children[0]
codeinput.text = line
self.main_panel.code_interpret(codeinput)
popup.dismiss()
scroll_content = ScrollView()
content = BoxLayout(orientation='vertical', size_hint_y=None)
content.bind(minimum_height=content.setter('height'))
scroll_content.add_widget(content)
for item in self.store:
btn = Button(text=item)
btn.size= content.width, 50
btn.size_hint_y = None
choice_callback = lambda _:choice(item)
btn.bind(
on_touch_down=partial(create_clock, choice_callback=choice_callback),
on_touch_up=delete_clock)
btn.bind(on_release=choice_callback)
content.add_widget(btn)
close_btn = Button(text='Cancel')
close_btn.size_hint = 0.3, None
close_btn.height = 50
close_btn.bind(on_press=popup.dismiss)
content.add_widget(close_btn)
popup.content = scroll_content
popup.open()
if __name__ == '__main__':
CalcPyApp().run()