forked from SUSYUSTC/MathTranslate
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFilePage.py
More file actions
84 lines (67 loc) · 3.11 KB
/
FilePage.py
File metadata and controls
84 lines (67 loc) · 3.11 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
import os
import re
import threading
from kivy.clock import Clock
from kivy.uix.popup import Popup
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from Dialog import LoadDialog, SavePathDialog, TranslationDialog
from Translate import translate_texfile
from mathtranslate.config import config
class FilePage(BoxLayout):
def __init__(self, **kwargs):
self.file_path = None
self.output_path = None
self.config = config
super().__init__(**kwargs)
def back_index(*args):
App.get_running_app().screen_manager.current = "Index_page"
App.get_running_app().screen_manager.transition.direction = 'right'
def open_dialog_select_texfile(self):
cwdir = self.config.default_loading_dir
content = LoadDialog(load=self.select_texfile, cancel=self.dismiss_popup, cwdir=cwdir)
self._popup = Popup(title="Load Latex File", content=content, size_hint=(.9, .9))
self._popup.open()
def select_texfile(self, path, filename):
self.file_path = filename
basename = os.path.basename(filename)
dirname = os.path.dirname(filename)
self.ids.loaded_filename.text = f'Loaded file: {basename}'
self.config.set_variable_4ui(self.config.default_loading_dir_path, dirname)
self.config.load()
self.dismiss_popup()
def open_dialog_select_savepath(self):
if self.file_path is None:
return
dirname = os.path.dirname(self.file_path)
filename = os.path.join(dirname, 'translate.tex')
content = SavePathDialog(load=self.select_savepath, cancel=self.dismiss_popup, file=filename, dirname=dirname, default_filename='translate.tex')
self._popup = Popup(title="Output File Path Setting", content=content, size_hint=(.9, .9))
self._popup.open()
def select_savepath(self, output_path):
self.dismiss_popup()
self.output_path = output_path
self.ids.output_path.text = f'Output file: {self.output_path}'
def translate(self):
# do not translate if input or output file not specified
if (self.file_path is None) or (self.output_path is None):
return
thread = threading.Thread(target=translate_texfile, args=(self.file_path, self.output_path))
thread.start()
content = TranslationDialog(cancel=self.dismiss_popup)
self._popup = Popup(title="Translation output", content=content, size_hint=(.9, .9))
self._popup.open()
def update_progress(dt):
# replace \r to original
text = open(self.config.log_file, 'rb').read().decode('utf-8')
text = text.replace('\r\n', '\n')
normal_text = re.sub('.*\r', '', text)
content.ids.translation_output.text = normal_text
Clock.schedule_interval(update_progress, 0.1)
def check_finish(dt):
if not thread.is_alive():
content.ids.translation_button_close.disabled = False
content.ids.translation_button_close.text = 'Close'
Clock.schedule_interval(check_finish, 0.1)
def dismiss_popup(self):
self._popup.dismiss()