-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEditor.py
More file actions
411 lines (371 loc) · 15.1 KB
/
Editor.py
File metadata and controls
411 lines (371 loc) · 15.1 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
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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
import Utility as Util
from Utility import *
import ConfigParser, tempfile, traceback
from glob import glob
from Constants import *
from collections import OrderedDict
from pygments.lexers import CythonLexer
from toast import toast
global gEditorLayout
#---------------------#
# CLASS : Editor
#---------------------#
class Editor(CodeInput):
ui = None
run_on_enter = None
dirty = False
parentTap = None
def __init__(self, ui, parentTap, *args, **kargs):
CodeInput.__init__(self, *args, **kargs)
self.ui = ui
self.old_text = ""
self.parentTap = parentTap
self.filename = ""
def setFilename(self, filename):
self.filename = filename
if self.parentTap:
if self.filename:
self.parentTap.text = os.path.split(filename)[1]
else:
self.parentTap.text = "Untitled"
if self.dirty:
self.parentTap.text += "*"
def setDirty(self, dirty):
if self.dirty != dirty:
self.dirty = dirty
if dirty:
self.parentTap.text += "*"
else:
if self.filename and self.parentTap:
self.parentTap.text = os.path.split(self.filename)[1]
else:
self.parentTap.text = "Untitled"
def loadFile(self, filename):
try:
# open file
f = open(filename, "r")
lines = list(f)
f.close()
self.old_text = self.text = "".join(lines)
self.setFilename(filename)
self.setDirty(False)
toast("Loaded : " + self.parentTap.text)
except:
toast("Failed to load the file : " + os.path.split(filename)[1])
log(traceback.format_exc())
return False
return True
def saveFile(self, force = False):
if self.dirty or force:
if self.filename:
try:
f = open(self.filename, "w")
f.write(self.text)
f.close()
self.setDirty(False)
# check already opened document then close
gEditorLayout.closeSameDocument(self)
toast("Saved : " + self.parentTap.text)
except:
toast("Failed to save the file : " + os.path.split(filename)[1])
log(traceback.format_exc())
else:
# untitled.document
gEditorLayout.setMode(szFileBrowserSaveAs)
def saveAsFile(self, filename):
def do_save():
self.filename = filename
self.saveFile(force = True)
# check overwrite
if self.filename != filename and os.path.isfile(filename):
gMyRoot.popup("File already exists. Overwrite?", os.path.split(filename)[1], do_save, None)
else:
do_save()
'''
def paste(self):
self.insert_text(gMyRoot.getClipboard())
if self.run_on_enter:
self.run_on_enter()
'''
def keyboard_on_key_down(self, window, keycode, text, modifiers):
TextInput.keyboard_on_key_down(self, window, keycode, text, modifiers)
enterKey = 13
backspaceKey = 8
key, key_str = keycode
# set dirty mark
if not self.dirty and self.old_text != self.text:
self.setDirty(True)
self.old_text = self.text
if self.run_on_enter and key in (enterKey, backspaceKey):
self.run_on_enter()
#---------------------#
# CLASS : EditorLayout
#---------------------#
class EditorLayout():
documentMap = OrderedDict({})
currentDocumentTap = None
textInputSV = None
editorInput = None
def __init__(self, ui):
global gEditorLayout
gEditorLayout = self
self.ui = ui
self.reFocusInputText = False
self.screen = Screen(name = szEditor)
# document list
height = kivy.metrics.dp(45)
self.documentTitleSV = ScrollView(orientation="horizontal", size_hint=(None, None), size=(W, height), pos=(0, H-height))
self.documentTitleLayout = BoxLayout(size_hint=(None, 1))
self.documentTitleSV.add_widget(self.documentTitleLayout)
self.screen.add_widget(self.documentTitleSV)
# menu layout
height = kivy.metrics.dp(35)
self.menuLayout = BoxLayout(size_hint=(1, None), height=height)
self.screen.add_widget(self.menuLayout)
self.menuDropDown = DropDown(size=(0,0), auto_dismiss=True)
btn_menu = Button(text="Menu", size_hint_y=None, height=height, background_color=darkGray)
def menuOpen(inst):
self.inputBoxForceFocus(False)
self.menuDropDown.open(inst)
btn_menu.bind(on_release = menuOpen)
btn_new = Button(text="New", size_hint_y=None, height=height, background_color=darkGray)
btn_new.bind(on_release = self.createDocument, on_press=self.menuDropDown.dismiss)
btn_open = Button(text="Open", size_hint_x=0.3, size_hint_y=None, height=height, background_color=darkGray)
btn_open.bind(on_release=lambda inst:self.setMode(szFileBrowserOpen), on_press=self.menuDropDown.dismiss)
btn_close = Button(text="Close", size_hint_y=None, height=height, background_color=darkGray)
btn_close.bind(on_release=lambda inst:self.closeDocument(self.editorInput), on_press=self.menuDropDown.dismiss)
btn_delete = Button(text="Delete", size_hint_y=None, height=height, background_color=darkGray)
btn_delete.bind(on_release=lambda inst:self.deleteDocument(self.editorInput), on_press=self.menuDropDown.dismiss)
btn_save = Button(text="Save", size_hint_y=None, height=height, background_color=darkGray)
btn_save.bind(on_release=lambda inst:self.editorInput.saveFile(), on_press=self.menuDropDown.dismiss)
btn_saveas = Button(text="Save As", size_hint_y=None, height=height, background_color=darkGray)
btn_saveas.bind(on_release=lambda inst:self.setMode(szFileBrowserSaveAs), on_press=self.menuDropDown.dismiss)
self.menuDropDown.add_widget(btn_new)
self.menuDropDown.add_widget(btn_open)
self.menuDropDown.add_widget(btn_close)
self.menuDropDown.add_widget(btn_save)
self.menuDropDown.add_widget(btn_saveas)
self.menuDropDown.add_widget(btn_delete)
self.menuLayout.add_widget(self.menuDropDown)
btn_undo = Button(text="Undo", background_color=darkGray)
btn_undo.bind(on_release=lambda inst:self.editorInput.do_undo())
btn_redo = Button(text="Redo", background_color=darkGray)
btn_redo.bind(on_release=lambda inst:self.editorInput.do_redo())
btn_run = Button(text="Run", background_color=darkGray)
btn_run.bind(on_release = self.runCode)
self.menuLayout.add_widget(btn_menu)
self.menuLayout.add_widget(btn_undo)
self.menuLayout.add_widget(btn_redo)
self.menuLayout.add_widget(btn_run)
# screen menu layout
self.screenMenuLayout = BoxLayout(orientation="horizontal", size_hint=(1, None), height="35dp")
btn_console = Button(text="Console", background_color=[1.5,0.8,0.8,2])
btn_editor = Button(text="Code Editor", background_color=[0.8,1.5,0.8,2])
btn_tutorial = Button(text="Python Tutorial", background_color=[0.8,0.8,1.5,2])
btn_console.bind(on_release=lambda inst:self.setMode(szConsole))
btn_tutorial.bind(on_release=lambda inst:self.setMode(szTutorial))
self.screenMenuLayout.add_widget(btn_console)
self.screenMenuLayout.add_widget(btn_editor)
self.screenMenuLayout.add_widget(btn_tutorial)
self.screen.add_widget(self.screenMenuLayout)
# load last opened document
self.load_config()
# create document
if len(self.documentMap) == 0:
self.createDocument()
self.refreshLayout()
def exit(self):
self.save_config()
def load_config(self):
if not os.path.isfile(configFile):
return
parser = ConfigParser.SafeConfigParser()
parser.read(configFile)
# load document section
doc_section = "Documents"
temp_section = "Tempfiles"
if parser.has_section(doc_section):
for opt in parser.options(doc_section):
filename = parser.get(doc_section, opt)
# open document
if os.path.isfile(filename):
self.open_file(filename)
# remove tempfile
if parser.has_option(temp_section, filename) and self.editorInput and self.editorInput.filename == filename:
originFileName = parser.get(temp_section, filename)
self.editorInput.setFilename(originFileName)
self.editorInput.setDirty(True)
# clear temp folder
for filename in glob(os.path.join(tempDirectory, "*")):
if os.path.splitext(filename)[1] == "":
try:
os.remove(filename)
except:
log(traceback.format_exc())
def save_config(self):
# make section
if len(self.documentMap) > 0:
parser = ConfigParser.SafeConfigParser()
doc_section = "Documents"
temp_section = "Tempfiles"
parser.add_section(doc_section)
parser.add_section(temp_section)
for i, fileTap in enumerate(self.documentMap):
editorInput = self.documentMap[fileTap][1]
# save temp
if editorInput.dirty:
try:
f = tempfile.NamedTemporaryFile(dir = tempDirectory, delete = False)
f.write(editorInput.text)
f.close()
parser.set(doc_section, 'filename%d' % i, f.name)
parser.set(temp_section, f.name, editorInput.filename)
except:
log(traceback.format_exc())
else:
parser.set(doc_section, 'filename%d' % i, editorInput.filename)
# save config file
with open(configFile, 'w') as f:
parser.write(f)
def createDocument(self, *args):
# add docjment tap
font_size = kivy.metrics.dp(16)
btn_tap = Button(text="Untitled", size_hint=(None,1), background_color = darkGray)
btn_tap.width = (len(btn_tap.text) + 2) * font_size
btn_tap.bind(on_release = self.changeDocument)
self.documentTitleLayout.width += btn_tap.width
self.documentTitleLayout.add_widget(btn_tap)
self.documentTitleSV.scroll_x = 1
# text input
editorInput = Editor(ui=self, parentTap=btn_tap, text = "text", lexer=CythonLexer(), multiline=True, size_hint=(2, None), font_name=defaultFont, auto_indent = True,
background_color=(.9, .9, .9, 1), font_size="14dp", padding_x="20dp", padding_y="15dp")
editorInput.height = editorInput.minimum_height
editorInput.text = ""
def refreshEditorInputSize(*args):
if editorInput.size[1] != editorInput.minimum_height:
self.refreshLayout()
editorInput.run_on_enter = refreshEditorInputSize
editorInput.bind(focus = self.inputBoxFocus)
# textinput scroll view
textInputSV = ScrollView(size_hint=(None, None), size = (W, editorInput.minimum_height))
textInputSV.add_widget(editorInput)
textInputSV.scroll_y = 0
# add to map
self.documentMap[btn_tap] = (textInputSV, editorInput)
# show document
self.changeDocument(btn_tap)
def closeDocument(self, editorInput, force = False):
if editorInput:
tap = editorInput.parentTap
if tap in self.documentMap:
def close():
tapIndex = self.documentMap.keys().index(tap)
scrollView = self.documentMap.pop(tap)[0]
if tap in self.documentTitleLayout.children:
self.documentTitleLayout.remove_widget(tap)
self.documentTitleLayout.width -= tap.width
if scrollView.parent:
scrollView.parent.remove_widget(scrollView)
if len(self.documentMap) == 0:
self.createDocument()
# if current doc, select next document
elif self.editorInput == editorInput:
tapIndex = min(tapIndex, len(self.documentMap) -1)
self.changeDocument(self.documentMap.keys()[tapIndex])
# do close
if force or not editorInput.dirty:
close()
elif editorInput.dirty:
gMyRoot.popup("File has unsaved changes.", "Really close file?", close, None)
def closeSameDocument(self, editorInput):
for fileTap in self.documentMap:
curEditorInput = self.documentMap[fileTap][1]
if curEditorInput != editorInput and curEditorInput.filename == editorInput.filename:
self.closeDocument(curEditorInput, force = True)
break
def deleteDocument(self, editorInput):
if not os.path.isfile(editorInput.filename):
return
filename = os.path.split(editorInput.filename)[1]
# delete file
def deleteFile():
try:
self.closeDocument(editorInput)
os.remove(editorInput.filename)
toast("Delete the file : " + filename)
except:
log(traceback.format_exc())
# ask delete?
gMyRoot.popup("Delete selected file?", filename, deleteFile, None)
def changeDocument(self, inst):
if inst in self.documentMap and inst != self.currentDocumentTap:
# remove keyboard
self.inputBoxForceFocus(False)
# old tap restore color
if self.currentDocumentTap:
self.currentDocumentTap.background_color = darkGray
# new tap color
inst.background_color = brightBlue
# set new tap to current tap
self.currentDocumentTap = inst
if self.textInputSV and self.textInputSV.parent:
self.textInputSV.parent.remove_widget(self.textInputSV)
self.textInputSV, self.editorInput = self.documentMap[inst]
self.screen.add_widget(self.textInputSV)
self.refreshLayout()
def open_file(self, filename):
try:
def loadFile():
result = self.editorInput.loadFile(filename)
if not result:
self.closeDocument(self.editorInput, True)
# check opened document
for fileTap in self.documentMap:
editorInput = self.documentMap[fileTap][1]
if editorInput.filename == filename:
self.changeDocument(fileTap)
if editorInput.dirty:
gMyRoot.popup("File has unsaved changes.", "Really open file?", loadFile, None)
break
else:
self.createDocument()
loadFile()
except:
log("open file error")
def save_as(self, filename):
self.editorInput.saveAsFile(filename)
def runCode(self, inst):
if self.editorInput.text.strip():
self.ui.onConsoleInput(self.editorInput, True)
self.setMode(szConsole)
def touchPrev(self):
if self.editorInput and self.editorInput.focus:
self.inputBoxForceFocus(False)
else:
self.setMode(szConsole)
def setMode(self, mode):
self.menuDropDown.dismiss()
self.reFocusInputText = False
self.inputBoxForceFocus(False)
self.ui.setMode(mode)
def inputBoxForceFocus(self, bFocus):
if self.editorInput and bFocus != self.editorInput.focus:
self.reFocusInputText = False
self.editorInput.focus = bFocus
def inputBoxFocus(self, inst, bFocus):
bAlwaysPreserveFocus = True
if not bFocus:
if self.reFocusInputText:
self.reFocusInputText = bAlwaysPreserveFocus
inst.focus = True
self.reFocusInputText = bAlwaysPreserveFocus
self.refreshLayout()
def refreshLayout(self):
keyboardHeight = gMyRoot.getKeyboardHeight() if self.editorInput.focus else 0
height = H - (keyboardHeight + self.menuLayout.height + self.screenMenuLayout.height + self.documentTitleSV.height + topMargin)
self.documentTitleSV.top = H - topMargin
self.screenMenuLayout.pos = (0, keyboardHeight)
self.menuLayout.pos = (0, self.screenMenuLayout.top)
self.textInputSV.pos = (0, self.menuLayout.top)
self.textInputSV.size = (W, height)
self.editorInput.height = max(height, self.editorInput.minimum_height)