Skip to content

Instantly share code, notes, and snippets.

@sklprogs
Created March 26, 2017 18:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sklprogs/b8a76084af2b38be31c2fd23147fba76 to your computer and use it in GitHub Desktop.
Save sklprogs/b8a76084af2b38be31c2fd23147fba76 to your computer and use it in GitHub Desktop.
Editor
#!/usr/bin/python3
import shared as sh
import sharedGUI as sg
class Editor:
def __init__(self):
self.Success = True
self.gui()
def synchronize(self,*args):
if self.Success:
no = self.w2.no_by_tk(tkpos=self.txt2.cursor())
self.w2._no = sh.Input(func_title='Editor.synchronize',val=no).integer()
sent_no = self.w2.sent_no()
sent_no = sh.Input(func_title='Editor.synchronize',val=sent_no).integer()
fw1, lw1 = self.w1.nos_by_sent_no(sent_no=sent_no)
fw2, lw2 = self.w2.nos_by_sent_no(sent_no=sent_no)
# note: may need to set 'Words._no' explicitly upon changing the 'tf-tl' algorithm
tfw1 = self.w1.words[fw1].tf()
tlw1 = self.w1.words[lw1].tl()
# note: may need to set 'Words._no' explicitly upon changing the 'tf-tl' algorithm
tfw2 = self.w2.words[fw2].tf()
tlw2 = self.w2.words[lw2].tl()
self.txt1.tag_add(tag_name='par',pos1tk=tfw1,pos2tk=tlw1)
self.txt2.tag_add(tag_name='par',pos1tk=tfw2,pos2tk=tlw2)
self.txt1.tag_config(tag_name='par',font='Monoserif 12 bold')
self.txt2.tag_config(tag_name='par',font='Monoserif 12 bold')
self.txt1.scroll(mark=tfw1)
self.txt2.scroll(mark=tfw2)
else:
sh.log.append('Editor.synchronize',sh.lev_warn,sh.globs['mes'].canceled)
def bindings(self):
sg.bind(obj=self.txt2,bindings=['<Up>','<Down>','<Left>','<Right>'],action=self.synchronize)
def reset(self,w1,w2):
self.reset_data(w1,w2)
self.reset_widget()
def reset_data(self,w1,w2):
self.w1 = w1
self.w2 = w2
if hasattr(self.w1,'Success') and self.w1.Success and hasattr(self.w2,'Success') and self.w2.Success:
self.w1._no = self.w2._no = 0
else:
self.Success = False
def reset_widget(self):
if self.Success:
self.txt1.reset_data()
self.txt1.reset_logic(words=self.w1)
self.txt2.reset_data()
self.txt2.reset_logic(words=self.w2)
self.fill()
self.txt2.focus()
else:
sh.log.append('Editor.reset_widget',sh.lev_warn,sh.globs['mes'].canceled)
def gui(self):
self.obj = sg.objs.new_top(Maximize=1)
self.txt1 = sg.TextBox(parent_obj=self.obj,Composite=1,side='left',font='Monoserif 12')
self.txt2 = sg.TextBox(parent_obj=self.obj,Composite=1,side='right',font='Monoserif 12')
self.title()
self.bindings()
self.close()
def fill(self):
if self.Success:
self.txt1.insert(text=self.w1._text_orig)
self.txt2.insert(text=self.w2._text_orig)
else:
sh.log.append('Editor.fill',sh.lev_warn,sh.globs['mes'].canceled)
def show(self,*args):
self.obj.show()
def close(self,*args):
self.obj.close()
def title(self,text=None):
if not text:
text = 'Editor:'
self.obj.title(text=text)
if __name__ == '__main__':
text1 = '''He broke the seal and glanced over the contents.
"Oh, come, it may prove to be something of interest, after all."
"Not social, then?"
"No, distinctly professional."
"And from a noble client?"
"One of the highest in England."
"My dear fellow. I congratulate you."
'''
text1 = text1.splitlines()
for i in range(len(text1)):
text1[i] = (text1[i] + ' ') * 10
text1 = '\n'.join(text1)
text2 = '''Он сломал печать и быстро просмотрел содержимое.
"Э, нет, здесь всё-таки может оказаться кое-что интересное."
"Значит, это не светское письмо?"
"Нет, сугубо деловое."
"От знатного клиента?"
"Одного из самых знатных в Англии."
"Поздравляю Вас, мой друг."
'''
text2 = text2.splitlines()
for i in range(len(text2)):
text2[i] = (text2[i] + ' ') * 10
text2 = '\n'.join(text2)
text1 = text1 * 5
text2 = text2 * 5
w1 = sh.Words(text=text1,OrigCyr=1,Auto=1)
w2 = sh.Words(text=text2,OrigCyr=0,Auto=1)
w1.sent_nos()
w2.sent_nos()
editor = Editor()
editor.reset(w1,w2)
editor.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment