forked from UPPERCASE-team-0/Text-editor-NoTex-1.8
-
Notifications
You must be signed in to change notification settings - Fork 0
/
texteditor.py
443 lines (334 loc) · 11.6 KB
/
texteditor.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
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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
import tkinter
from tkinter import *
from tkinter.scrolledtext import *
from tkinter import filedialog
from tkinter import messagebox
from tkinter.simpledialog import askstring
import os
import string
import threading
import time
import pygame, sys,random,time
from pygame.locals import *
root = tkinter.Tk(className=" NoTeX v1.8")
'''root.tk.call('wm', 'iconphoto', root._w, image)
root.iconbitmap(r'/home/crater/Desktop/DS_mini/icon.ico')
imgicon = PhotoImage(file=os.path.join(
'/home/crater/Desktop/DS_mini', 'icon.ico'))
root.tk.call('wm', 'iconphoto', root._w, imgicon)''' # Setting icon: Due for another day-> minutes spent = 25+10+30
# root.wm_iconbitmap('~/Desktop/DS_mini/icon.ico')
textPad = ScrolledText(root, width=80, height=50, bg="#DCEDC8")
textPad.configure(font=("sans-serif", 9, "normal"))
word = " "
main_stack = []
redo_stack = []
loadPercent = 0
def open_command():
file = filedialog.askopenfile(
parent=root, mode='rb', title='Select a file')
if file != None:
contents = file.read()
textPad.delete('1.0', "end")
textPad.insert('1.0', contents)
file.close()
def save_command():
file = filedialog.asksaveasfile(mode='w')
if file != None:
data = textPad.get('1.0', 'end-1c')
file.write(data)
file.close()
def exit_command():
global root
if messagebox.askokcancel("Quit", "Do you really want to quit?"):
root.destroy()
quit()
def about_command():
lab = messagebox.showinfo(
"About", "From the creators of UpperCase and Poseidon")
def new_command():
result = messagebox.askquestion(
"Save this!!", "Current changes unsaved. Do you want to Save?")
if result == 'yes':
save_command()
textPad.delete('1.0', "end")
else:
textPad.delete('1.0', 'end')
def cut_command():
textPad.event_generate("<<Cut>>")
def copy_command():
textPad.event_generate("<<Copy>>")
def paste_command():
textPad.event_generate("<<Paste>>")
#############################################################################
# all the charecters in the word tree will be stored in objects of the
# class node
#DATA structure 1
class node():
def __init__(self, key, parent):
self.key = key
self.parent = parent
self.children = []
self.ch_val = []
self.flag = 0
self.children_count = 0
rt = []
i_suggest_list = []
def make_word_tree():
wrd = [wd.rstrip('\n') for wd in open('words_alpha.txt')]
#wrd = ["hello","yashas","yash","yamini","apple","appmaker","vishal","vinayak"]
if(wrd != ""):
# making the master root with its children
for i in string.printable:
temp = node(i, None)
rt.append(temp)
# '\n' is not present in the above list , thus added this to handle an exception when the word just has \n
rt.append(node('\n', None))
# getting the word's starting root from the master root array
for w in wrd:
for ch in rt:
if(ch.key == w[0]):
temp_root = ch
break
# if the one charecter itself is a word then , append it
if(len(wrd) == 1):
temp_root.flag = 1
else:
for i in range(1, len(w)):
if(w[i] not in temp_root.ch_val):
temp = node(w[i], temp_root)
temp_root.ch_val.append(w[i])
temp_root.children.append(temp)
temp_root.children_count += 1
# used for debugging
#print("INSERTED ", w[i], " after ", temp_root.key)
else:
for temp in temp_root.children:
if(temp.key == w[i]):
temp_root = temp
# if the word is inserted
if(i == len(w)-1):
temp.flag = 1
# used for debugging
#print("inserted")
temp_root = temp
def autosuggest(i_word):
global i_suggest_list
# everythime a new word is entered , clear the suggestion list and update
# it again
for i in range(0, len(i_suggest_list)):
i_suggest_list.pop()
# getting the word initial root in the master root list
for i_temp in rt:
if(i_temp.key == i_word[0]):
i_temp_root = i_temp
break
i_adst = i_temp_root.key
i_cflag = 1
for i_i in range(1, len(i_word)):
# if entered word is there in the tree
if(i_word[i_i] in i_temp_root.ch_val):
i_adst = i_adst+i_word[i_i]
#print("added")
for i_temp in i_temp_root.children:
if(i_temp.key == i_word[i_i]):
i_temp_root = i_temp
else:
# if the entered word is not there in the word tree then dont show
# anything
i_cflag = 0
for i in range(0, len(i_suggest_list)):
i_suggest_list.pop()
i_suggest_list.append(i_word)
#print("didnt find")
# used for debugging
#print(i_temp_root.key)
# if the currently entered word is valid
if(i_cflag):
i_suggest_list.append(i_adst)
recursive_find(i_temp_root, i_adst[0:len(i_adst)-1])
return i_suggest_list
def recursive_find(i_temp_root, cur_st):
global i_suggest_list
cur_st = cur_st + i_temp_root.key
# if inbetween we get a flag then append that word to the final list
if(i_temp_root.flag == 1):
i_suggest_list.append(cur_st)
# propogate till we reach a leaf node
if(i_temp_root.children_count == 0):
return
else:
for i_temp in i_temp_root.children:
recursive_find(i_temp, cur_st)
#############################################################################
def check_change():
# The text prediction and spellcheck part
global predictions
txt = ""
txt = textPad.get('1.0', INSERT)
global predictions
if txt.split() != [] and txt[-1] != ' ':
global word
if word != txt.split()[-1]:
word = txt.split()[-1]
#print(word) # word to be sent to the text prediction function
# Undo and Redo part
status = textPad.get('1.0', 'end-1c')
if(len(main_stack) <= 100):
main_stack.append(status)
else:
main_stack.pop(0)
main_stack.append(status)
#############################
words = autosuggest(word.lower())
if(words != []):
words = words[1:]
#############################
predictions.delete(0, END)
predictions.insert(END, "Word: "+word)
for x in enumerate(words):
predictions.insert(END, str(x[0]+1)+" : "+x[1])
else:
predictions.delete(0, END)
root.update()
def undo_command():
if(len(main_stack) > 0):
redo_stack.append(main_stack.pop())
textPad.delete('1.0', 'end')
if(len(main_stack) > 0):
textPad.insert('1.0', main_stack[-1])
def redo_command():
if len(redo_stack) > 0:
content = redo_stack.pop()
main_stack.append(content)
textPad.delete('1.0', 'end')
textPad.insert('1.0', content)
def searchbox():
global searchword
global textPad
textPad.tag_remove('found', '1.0', END)
searchword = askstring("Search", "Enter the word to search:")
if searchword == None or searchword == " ":
return
# print('{!r}'.format(start_pos))
start_pos = '1.0'
while start_pos:
start_pos = textPad.search(
searchword, start_pos, nocase=1, stopindex=END)
if start_pos:
end_pos = '{}+{}c'.format(start_pos, len(searchword))
# print('{!r}'.format(end_pos))
textPad.tag_add('found', start_pos, end_pos)
start_pos = end_pos
textPad.tag_config('found', foreground='red')
def replace():
global replaceword
global textPad
global searchword
if searchword:
replaceword = askstring("Replace", "Enter the word to replace:")
if(replaceword == None):
textPad.tag_remove('found', '1.0', END)
return
'''
x = []
l = list(textPad.tag_ranges('found'))
l.reverse()
while l:
x.append([l.pop(), l.pop()])
for start, end in x:
textPad.delete(start, end)
textPad.insert(start, replaceword)
'''
lines = textPad.get('1.0', 'end-1c')
lines = lines.split('\n')
for i in range(len(lines)):
while searchword in lines[i]:
lines[i] = lines[i].replace(searchword, replaceword)
txt = '\n'.join(lines)
#print(searchword)
#print(replaceword)
#print(txt)
textPad.delete('1.0', END)
textPad.insert('1.0', txt)
#redo_stack.append(txt)
menu = Menu(root)
root.config(menu=menu)
filemenu = Menu(menu)
menu.add_cascade(label="File", menu=filemenu)
filemenu.add_command(label="New", command=new_command)
filemenu.add_command(label="Open", command=open_command)
filemenu.add_command(label="Save", command=save_command)
filemenu.add_separator()
filemenu.add_command(label="Exit", command=exit_command)
editmenu = Menu(menu)
menu.add_cascade(label="Edit", menu=editmenu)
editmenu.add_command(label="Cut", command=cut_command)
editmenu.add_command(label="Copy", command=copy_command)
editmenu.add_command(label="Paste", command=paste_command)
helpmenu = Menu(menu)
menu.add_cascade(label="Help", menu=helpmenu)
helpmenu.add_command(label="About", command=about_command)
blankmenu = Menu(menu, tearoff=0)
menu.add_cascade(label="".ljust(130), menu=blankmenu)
menu.add_command(label="Search", command=searchbox)
menu.add_command(label="Replace", command=replace)
menu.add_command(label="Undo", command=undo_command)
menu.add_command(label="Redo", command=redo_command)
textPad.grid(row=0, column=0, columnspan=2)
frame = Frame(root)
frame.grid(row=0, column=3)
scrollbar = Scrollbar(frame, orient=VERTICAL)
predictions = Listbox(frame, width=40, height=40,
bg="#BBDEFB", yscrollcommand=scrollbar.set)
scrollbar.config(command=predictions.yview)
scrollbar.pack(side=RIGHT, fill=Y)
predictions.pack(side=LEFT, fill=BOTH, expand=1)
#################
''''
def screenLoad():
pygame.init()
height=768
width=1366
surface=pygame.display.set_mode((width,height))
pygame.display.set_caption('NoteX v1.8')
font2=pygame.font.SysFont(None,60)
while not loadPercent:
drawtext('Loading....',font2,window,WINDOWWIDTH//2,WINDOWHEIGHT//4)
pygame.display.update()
pygame.quit()
sys.exit()
pass
class buildTreeThread(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
def run(self):
make_word_tree()
global loadPercent
loadPercent = 1
class showLoadingScreen(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
def run(self):
screenLoad()
def drawtext1(text,font,surface,x,y):
textobj=font.render(text,True,BLACK,YELLO)
textrect=textobj.get_rect()
textrect.centerx = x
textrect.centery = y
surface.blit(textobj,textrect)
return(textrect)
t1 = time.time()
build = buildTreeThread()
loadScreen = showLoadingScreen()
build.start()
loadScreen.start()
build.join()
loadScreen.join()
'''
#################
#print(time.time() - t1)
t1 = time.time()
make_word_tree( )
print("Tree building time : ",time.time() - t1)
while 1:
check_change()