-
Notifications
You must be signed in to change notification settings - Fork 54
/
thesis-helper.py
282 lines (233 loc) · 10.2 KB
/
thesis-helper.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# @Time : 2019年11月4日16:56:33
# @Author : 穆华岭
# @Software: 毕业论文小助手
# @github :https://github.com/muhualing/
import os
import sys
from PyQt5.QtWebEngineWidgets import QWebEngineView
from PyQt5.QtCore import QUrl,pyqtSignal,QEvent,Qt
import platform
sysstr = platform.system()
is_win = is_linux = is_mac = False
if sysstr == "Windows":
is_win = True
elif sysstr == "Linux":
is_linux = True
elif sysstr == "Mac":
is_mac = True
print('System: %s' % sysstr)
from PyQt5.QtGui import QColor
from PyQt5.QtWidgets import (
QWidget, QHBoxLayout, QVBoxLayout, QMainWindow,
QGroupBox,QApplication, QLabel, QPlainTextEdit,
QComboBox, QAction, QMenuBar, QMenu, QFileDialog
)
from thesisUtils.controller import con
from thesisUtils.watch_clip import WatchClip
from thesisUtils.text_filter import TextFilter
from thesisUtils.LeftTabWidget import LeftTabWidget
from thesisUtils.configure import config
MAX_CHARACTERS = 5000
class WebView(QWebEngineView):
def __init__(self):
print('init webView')
super(WebView, self).__init__()
self._glwidget = None
self.pdf_js_path = "file:///" + os.path.join(os.getcwd(), "pdfjs", "web", "viewer.html")
pdf_path = "file:///" + os.path.join(os.getcwd(), "sample", "sample_2.pdf")
if sys.platform == "win32":
self.pdf_js_path = self.pdf_js_path.replace('\\', '/')
pdf_path = pdf_path.replace('\\', '/')
self.changePDF(pdf_path)
self.setAcceptDrops(True)
self.installEventFilter(self)
def dragEnterEvent(self,e):
"""
Detect mouse drag something into the view
:param e: Mouse event
:return: None
"""
if is_linux or is_mac:
if e.mimeData().hasFormat('text/plain') and e.mimeData().text()[-6:-2] == ".pdf":
e.accept()
else:
e.ignore()
elif is_win:
if e.mimeData().text()[-4:] == ".pdf":
e.accept()
else:
e.ignore()
# QMessageBox.about(self, "提示", "所选文件不是pdf格式的文件") 这行会卡死 不知道为啥
def dropEvent(self,e):
"""
Detect mouse release event the view and state before release is dragging
:param e: Mouse event
:return: None
"""
self.changePDF(e.mimeData().text())
def event(self, e):
"""
Detect child add event, as QWebEngineView do not capture mouse event directly,
the child layer _glwidget is implicitly added to QWebEngineView and we track mouse event through the glwidget
:param e: QEvent
:return: super().event(e)
"""
if self._glwidget is None:
if e.type() == QEvent.ChildAdded and e.child().isWidgetType():
print('child add')
self._glwidget = e.child()
self._glwidget.installEventFilter(self)
# if(e.type() == QEvent.ChildRemoved and e.child().isWidgetType()):
# # print('child removed')
# if(self._glwidget is not None):
# self._glwidget.removeEventFilter(self)
# if(e.type() == QEvent.Close):
# # print('close webView')
# self.removeEventFilter(self)
return super().event(e)
def eventFilter(self, source, event):
if event.type() == QEvent.MouseButtonRelease and source is self._glwidget:
con.pdfViewMouseRelease.emit()
return super().eventFilter(source, event)
def changePDF(self, pdf_path):
self.load(QUrl.fromUserInput('%s?file=%s' % (self.pdf_js_path, pdf_path)))
if sys.platform == 'win32' and 'sample' not in pdf_path:
if '/' in pdf_path:
config.set('history_pdf', pdf_path.split('/')[-1].split('.')[0], pdf_path)
else:
config.set('history_pdf', pdf_path.split('\\')[-1].split('.')[0], pdf_path)
config.write(open('CONFIG.ini', 'w'))
class MainWindow(QMainWindow):
def __init__(self):
super(MainWindow, self).__init__()
self.thread_my = WatchClip()
self.thread_my.start()
self.setWindowTitle("毕业论文小助手")
self.translate_ori = QPlainTextEdit()
# self.translate_ori.setTextBackgroundColor(QColor(127, 127, 127, 60))
# self.translate_ori.setTextColor(QColor(0, 0, 0, 0))
self.translate_ori.setStyleSheet("font: 12pt Roboto")
self.translate_res = QPlainTextEdit()
self.translate_res.setStyleSheet("font: 12pt Roboto")
self.selectable_text_size = ['8','9','10','11','12','13','14','15',]
self.text_size_combobox_ori = QComboBox()
self.text_size_combobox_ori.addItems(self.selectable_text_size)
self.text_size_combobox_ori.setCurrentIndex(4)
self.text_size_combobox_res = QComboBox()
self.text_size_combobox_res.addItems(self.selectable_text_size)
self.text_size_combobox_res.setCurrentIndex(4)
label1 = QLabel('字体大小:')
label1.setAlignment(Qt.AlignRight|Qt.AlignVCenter)
label2 = QLabel('字体大小:')
label2.setAlignment(Qt.AlignRight | Qt.AlignVCenter)
oriHboxLayout = QHBoxLayout()
oriHboxLayout.addWidget(QLabel('原文'))
oriHboxLayout.addWidget(label1)
oriHboxLayout.addWidget(self.text_size_combobox_ori)
oriHboxLayout.setStretch(0,6)
oriHboxLayout.setStretch(1,3)
oriHboxLayout.setStretch(2,3)
oriWidget = QWidget()
oriWidget.setLayout(oriHboxLayout)
resHboxLayout = QHBoxLayout()
resHboxLayout.addWidget(QLabel('译文'))
resHboxLayout.addWidget(label2)
resHboxLayout.addWidget(self.text_size_combobox_res)
resHboxLayout.setStretch(0,6)
resHboxLayout.setStretch(1,3)
resHboxLayout.setStretch(2,3)
resWidget = QWidget()
resWidget.setLayout(resHboxLayout)
self.filter = TextFilter()
vbox = QVBoxLayout()
vbox.addWidget(oriWidget)
vbox.addWidget(self.translate_ori)
vbox.addWidget(resWidget)
vbox.addWidget(self.translate_res)
gbox = QGroupBox()
gbox.setStyleSheet("font: 12pt Roboto")
gbox.setLayout(vbox)
self.pdfWrapper = WebView()
self.left_tab_widget = LeftTabWidget(self.pdfWrapper)
hBoxLayout = QHBoxLayout()
hBoxLayout.addWidget(self.left_tab_widget)
hBoxLayout.addWidget(self.pdfWrapper)
hBoxLayout.addWidget(gbox)
hBoxLayout.setStretch(0, 1)
hBoxLayout.setStretch(1, 40)
hBoxLayout.setStretch(2, 12)
widget = QWidget()
widget.setLayout(hBoxLayout)
self.setCentralWidget(widget)
self.recent_text = ""
self.showMaximized()
# create the menu bar
self.menu_bar = self.menuBar()
self.file = self.menu_bar.addMenu('File')
self.open_pdf = QAction('Open PDF', self)
self.open_pdf.setShortcut('Ctrl+O')
self.file.addAction(self.open_pdf)
self.open_dir = QAction('Open Folder', self)
self.open_dir.setShortcut('Ctrl+Shift+O')
self.file.addAction(self.open_dir)
# self.file is listening
self.file.triggered[QAction].connect(self.openDir)
def openDir(self, qaction):
if qaction.text() == 'Open PDF':
fd = QFileDialog.getOpenFileName(self, 'Choose a PDF', './', 'All(*.*);;PDF(*.pdf)', 'PDF(*.pdf)')
self.pdfWrapper.changePDF(fd[0])
elif qaction.text() == 'Open PDFs':
fd = QFileDialog.getOpenFileNames(self, 'Choose PDFs', './', 'All(*.*);;PDF(*.pdf)', 'PDF(*.pdf)')
print(fd)
elif qaction.text() == 'Open Folder':
fd = QFileDialog.getExistingDirectory(self, 'Choose a directory', './')
# update the roots in CONFIG.ini
config.set('local_pdf', 'roots', fd)
config.write(open('CONFIG.ini', 'w'))
# update the local_pdf view in UI
self.left_tab_widget.updateLocal()
def updateTranslation(self, cur_text):
self.translate_res.clear()
self.translate_res.setPlainText(cur_text)
def updateByMouseRelease(self):
# print('no seletion to translate')
if self.pdfWrapper.hasSelection():
to_translate_text = self.pdfWrapper.selectedText()
if len(to_translate_text) > MAX_CHARACTERS:
hint_str = '请选择少于%d个英文字符' % MAX_CHARACTERS
# print(hint_str)
self.translate_ori.setText(hint_str)
return
else:
if self.recent_text == to_translate_text:
# print('same as before, not new translate')
return
else:
hint_str = '正在翻译...'
filtered = self.filter.removeDashLine(to_translate_text)
# print(filtered)
self.recent_text = to_translate_text
self.translate_ori.setPlainText(filtered)
self.translate_res.setPlainText(hint_str)
# self.thread_my.setTranslateText(filtered)
def updateByTextEdit(self):
# print('TextEdited')
self.thread_my.setTranslateText(self.translate_ori.toPlainText())
def updateOriTextSizeByIndexChanged(self, index):
self.translate_ori.setStyleSheet("font: {0}pt Roboto".format(self.selectable_text_size[index]))
def updateResTextSizeByIndexChanged(self, index):
self.translate_res.setStyleSheet("font: {0}pt Roboto".format(self.selectable_text_size[index]))
def closeEvent(self, event):
self.thread_my.expired()
if __name__ == '__main__':
app = QApplication(sys.argv)
mainWindow = MainWindow()
mainWindow.show()
con.translationChanged.connect(mainWindow.updateTranslation)
con.pdfViewMouseRelease.connect(mainWindow.updateByMouseRelease)
mainWindow.translate_ori.textChanged.connect(mainWindow.updateByTextEdit)
mainWindow.text_size_combobox_ori.currentIndexChanged.connect(mainWindow.updateOriTextSizeByIndexChanged)
mainWindow.text_size_combobox_res.currentIndexChanged.connect(mainWindow.updateResTextSizeByIndexChanged)
sys.exit(app.exec_())