-
Notifications
You must be signed in to change notification settings - Fork 0
/
edit_translated.py
executable file
·234 lines (194 loc) · 7.75 KB
/
edit_translated.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
# create a window to edit text and a button copy text to clipboard
# using PySide6 and can auto adapt to the size of the text
import sys
import json
import logging
from PySide6.QtWidgets import (
QApplication,
QPushButton,
QGridLayout,
QTextEdit,
QSlider,
QLabel,
QSpinBox,
QDialog,
QStatusBar,
QCheckBox,
)
from PySide6.QtGui import QFont
from PySide6.QtCore import QSize, Qt, Slot
from myLib.copy import getText
from myLib.normalize_str import removeNewline, removeReturn, removeSpace
from myLib.translate import trans
from myLib.pidHandle import killPIDByID, getPID
from myLib.TTS import tts_process
from myLib.xdotool import press_key, focus_window
def normalize(text: str) -> str:
text = removeSpace(removeReturn(removeNewline(text)))
return text
class Window(QDialog):
speed = 0
player = ""
tts_mode = ""
tts_pid = [0, 0]
font_size = 0
editor_window_id = 0
do_after_close = "no"
def __init__(self):
super().__init__()
# config_path = os.environ["DATA_PATH"] + "/My-utilities/config.json"
config_path = "/media/lucifer/STORAGE/IMPORTANT/My-utilities/config.json"
# config_path = "/media/lucifer/DATA/My-utilities/config.json"
with open(config_path, "r", encoding="utf-8") as file:
config = json.load(file)["edit"]
if config["editor_window_id"] == 0:
logging.debug("Config editor PID first")
sys.exit()
self.speed = config["speed"]
self.player = config["player"]
self.tts_mode = config["tts_mode"]
self.font_size = config["font_size"]
self.editor_window_id = config["editor_window_id"]
self.do_after_close = config["do_after_close"]
self.init_ui()
def init_ui(self):
self.text = normalize(getText())
self.trans = trans(
source_lang="en",
target_lang="vi",
source_text=self.text,
)
# set the window size and title
self.setGeometry(900, 300, 750, round(750 / 1.618))
self.setWindowTitle("Edit translation")
# create textbox to edit text with default text
self.text_edit = QTextEdit(self)
self.text_edit.setFont(QFont("Arial", self.font_size))
# set background color to Solarized dark
self.text_edit.setStyleSheet(
"background-color: #002b36; color: #839496; border: 1px solid #586e75"
)
self.text_edit.setText(self.trans)
# create copy button in green color
self.copy_button = QPushButton("Copy", self)
self.copy_button.setFont(QFont("Ubuntu", 20))
self.copy_button.clicked.connect(self.copy_content)
self.copy_button.setFixedSize(QSize(100, round(100 / 1.618)))
self.copy_button.setStyleSheet("background-color: green")
self.speak_button = QPushButton("Speak", self)
self.speak_button.setFont(QFont("Ubuntu", 14))
self.speak_button.clicked.connect(self._speak)
self.speak_button.setFixedSize(QSize(60, round(60 / 1.618)))
self.close_button = QPushButton("Close", self)
self.close_button.setFont(QFont("Ubuntu", 20))
self.close_button.clicked.connect(self.close_window)
self.close_button.setFixedSize(QSize(100, round(100 / 1.618)))
self.close_button.setStyleSheet("background-color: red")
# add reset button to reset the content
self.reset_button = QPushButton("Reset", self)
self.reset_button.setFont(QFont("Ubuntu", 20))
self.reset_button.clicked.connect(self.reset_content)
self.reset_button.setFixedSize(QSize(100, round(100 / 1.618)))
self.reset_button.setStyleSheet("background-color: orange")
# add a horizontal slider to change the speed
self.speed_slider = QSlider(Qt.Horizontal)
self.speed_slider.setMinimum(10)
self.speed_slider.setMaximum(30)
self.speed_slider.setValue(int(self.speed * 10))
self.speed_slider.setTickInterval(1)
self.speed_slider.valueChanged.connect(self.update_speed)
# add a label to show the speed continuously
self.speed_label = QLabel("Speed: " + str(self.speed), self)
self.speed_label.setFont(QFont("Ubuntu", 15))
# add a box with minus and plus button to change font size
self.font_size_box = QSpinBox()
self.font_size_box.setMaximum(20)
self.font_size_box.setMinimum(5)
self.font_size_box.setValue(13)
self.font_size_box.setFixedSize(QSize(80, round(80 / 1.618)))
self.font_size_box.valueChanged.connect(self.update_font_size)
# add label to show the font size
self.font_size_label = QLabel("Font:")
self.font_size_label.setFont(QFont("Ubuntu", 14))
# add label disable/enable action after close
self.do_after_close_label = QLabel("Do After Close?")
self.do_after_close_label.setFont(QFont("Ubuntu", 14))
# add a checkbox to diable/enable change font size
self.font_size_checkbox = QCheckBox()
# add a checkbox to disable/enable action after close
self.do_after_close_checkbox = QCheckBox()
# add status bar to resize the window
self.statusbar = QStatusBar()
# create a grid layout
self.grid = QGridLayout(self)
self.grid.addWidget(self.text_edit, 1, 0, 1, 9)
self.grid.addWidget(self.copy_button, 3, 0, 1, 1)
self.grid.addWidget(self.close_button, 3, 1, 1, 1)
self.grid.addWidget(self.speed_slider, 2, 1, 1, 1)
self.grid.addWidget(self.speak_button, 2, 2, 1, 1)
self.grid.addWidget(self.reset_button, 3, 2, 1, 1)
self.grid.addWidget(self.font_size_box, 3, 3, 1, 2)
self.grid.addWidget(self.speed_label, 2, 0, 1, 1)
self.grid.addWidget(self.font_size_label, 2, 3, 1, 1)
self.grid.addWidget(self.font_size_checkbox, 2, 4, 1, 1)
self.grid.addWidget(self.statusbar, 3, 5, 1, 1)
@Slot()
def reset_content(self):
self.text_edit.setText(self.trans)
@Slot()
def update_font_size(self):
size = self.font_size_box.value()
is_on = self.font_size_checkbox.isChecked()
if size > 0 and is_on:
self.text_edit.setFont(QFont("Ubuntu", size))
@Slot()
def update_speed(self):
self.speed = float(self.speed_slider.value()) / 10
self.speed_label.setText("Speed: " + str(self.speed))
@Slot()
def copy_content(self):
logging.info("Copied")
# copy the text in textbox to clipboard
self.clipboard = QApplication.clipboard()
self.clipboard.setText(self.text_edit.toPlainText())
def _after_close(self):
# focus to editor window
focus_window(self.editor_window_id)
press_key("Escape")
press_key("shift+g")
press_key("o")
press_key("enter")
press_key("Escape")
# paste content
press_key("p")
# press_key("ctrl+v")
@Slot()
def close_window(self):
killPIDByID(self.tts_pid[0])
killPIDByID(self.tts_pid[1])
self.close()
if self.do_after_close == "yes":
self._after_close()
@Slot()
def _speak(self):
logging.info("Speaking")
self.tts_pid[0] = getPID(self.player)
if self.tts_pid[0]:
killPIDByID(self.tts_pid[0])
killPIDByID(self.tts_pid[1])
else:
if (
tts_process(
text=self.text_edit.toPlainText(),
mode=self.tts_mode,
player=self.player,
speed=self.speed,
)
is not None
):
self.tts_pid[1]
if __name__ == "__main__":
app = QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec())