diff --git a/LunaTranslator/LunaTranslator/LunaTranslator.py b/LunaTranslator/LunaTranslator/LunaTranslator.py index f6a14b92..b2aa6e02 100644 --- a/LunaTranslator/LunaTranslator/LunaTranslator.py +++ b/LunaTranslator/LunaTranslator/LunaTranslator.py @@ -492,12 +492,13 @@ class MAINUI: == False ): continue - _hira = importlib.import_module("hiraparse." + name).hira + _hira = importlib.import_module("hiraparse." + name) + _hira = getattr(_hira, name) break try: if _hira: - self.hira_ = _hira() + self.hira_ = _hira(name) else: self.hira_ = None except: @@ -583,22 +584,7 @@ class MAINUI: except: return - class cishuwrapper: - def __init__(self, _type) -> None: - self._ = _type() - - @threader - def search(self, sentence): - try: - res = self._.search(sentence) - if res is None or res == "": - return - self.callback(res) - except: - pass - - _ = cishuwrapper(aclass) - return _ + return aclass(type_) def onwindowloadautohook(self): textsourceusing = globalconfig["sourcestatus2"]["texthook"]["use"] diff --git a/LunaTranslator/LunaTranslator/cishu/cishubase.py b/LunaTranslator/LunaTranslator/cishu/cishubase.py new file mode 100644 index 00000000..4793f1a5 --- /dev/null +++ b/LunaTranslator/LunaTranslator/cishu/cishubase.py @@ -0,0 +1,41 @@ +from myutils.config import globalconfig +from myutils.wrapper import threader + + +class cishubase: + def init(self): + pass + + def search(self, word): + return word + + def __init__(self, typename) -> None: + self.typename = typename + self.callback = print + self.needinit = True + try: + self.init() + self.needinit = False + except: + pass + + @threader + def safesearch(self, sentence): + try: + if self.needinit: + self.init() + self.needinit = False + try: + res = self.search(sentence) + except: + self.needinit = True + + if res is None or res == "": + return + self.callback(res) + except: + pass + + @property + def config(self): + return globalconfig["cishu"][self.typename]["args"] diff --git a/LunaTranslator/LunaTranslator/cishu/edict.py b/LunaTranslator/LunaTranslator/cishu/edict.py index a91bacb8..50032c6a 100644 --- a/LunaTranslator/LunaTranslator/cishu/edict.py +++ b/LunaTranslator/LunaTranslator/cishu/edict.py @@ -1,14 +1,14 @@ -from myutils.config import globalconfig import sqlite3, os import winsharedutils, re from myutils.utils import argsort, autosql +from cishu.cishubase import cishubase -class edict: - def __init__(self): +class edict(cishubase): + def init(self): self.sql = None try: - path = globalconfig["cishu"]["edict"]["path"] + path = self.config["path"] if os.path.exists(path): self.sql = autosql(sqlite3.connect(path, check_same_thread=False)) except: diff --git a/LunaTranslator/LunaTranslator/cishu/edict2.py b/LunaTranslator/LunaTranslator/cishu/edict2.py index 6dc65603..9da10afc 100644 --- a/LunaTranslator/LunaTranslator/cishu/edict2.py +++ b/LunaTranslator/LunaTranslator/cishu/edict2.py @@ -1,15 +1,15 @@ -from myutils.config import globalconfig import winsharedutils, os import re from myutils.utils import argsort from traceback import print_exc +from cishu.cishubase import cishubase -class edict2: - def __init__(self): +class edict2(cishubase): + def init(self): self.sql = None try: - path = globalconfig["cishu"]["edict2"]["path"] + path = self.config["path"] if os.path.exists(path): with open(path, "r", encoding="euc-jp") as ff: _ = ff.read() diff --git a/LunaTranslator/LunaTranslator/cishu/goo.py b/LunaTranslator/LunaTranslator/cishu/goo.py index 3af25d91..581cb1bf 100644 --- a/LunaTranslator/LunaTranslator/cishu/goo.py +++ b/LunaTranslator/LunaTranslator/cishu/goo.py @@ -2,9 +2,10 @@ import requests from urllib.parse import quote import re from myutils.proxy import getproxy +from cishu.cishubase import cishubase -class goo: +class goo(cishubase): def search(self, word): url = "https://dictionary.goo.ne.jp/srch/all/{}/m1u/".format(quote(word)) diff --git a/LunaTranslator/LunaTranslator/cishu/linggesi.py b/LunaTranslator/LunaTranslator/cishu/linggesi.py index 08a88738..65f38609 100644 --- a/LunaTranslator/LunaTranslator/cishu/linggesi.py +++ b/LunaTranslator/LunaTranslator/cishu/linggesi.py @@ -1,35 +1,28 @@ -from myutils.config import globalconfig from myutils.utils import autosql import sqlite3 import winsharedutils import os +from cishu.cishubase import cishubase -class linggesi: - def __init__(self): +class linggesi(cishubase): + def init(self): self.sql = None try: if ( - os.path.exists( - os.path.join(globalconfig["cishu"]["linggesi"]["path"], "ja-zh.db") - ) - == False - or os.path.exists( - os.path.join( - globalconfig["cishu"]["linggesi"]["path"], "ja-zh-gbk.db" - ) - ) + os.path.exists(os.path.join(self.config["path"], "ja-zh.db")) == False + or os.path.exists(os.path.join(self.config["path"], "ja-zh-gbk.db")) == False ): return self.sql = autosql( sqlite3.connect( - os.path.join(globalconfig["cishu"]["linggesi"]["path"], "ja-zh.db"), + os.path.join(self.config["path"], "ja-zh.db"), check_same_thread=False, ) ) self.sql2 = sqlite3.connect( - os.path.join(globalconfig["cishu"]["linggesi"]["path"], "ja-zh-gbk.db"), + os.path.join(self.config["path"], "ja-zh-gbk.db"), check_same_thread=False, ) except: diff --git a/LunaTranslator/LunaTranslator/cishu/mojidict.py b/LunaTranslator/LunaTranslator/cishu/mojidict.py index 1e8cf939..b480fb66 100644 --- a/LunaTranslator/LunaTranslator/cishu/mojidict.py +++ b/LunaTranslator/LunaTranslator/cishu/mojidict.py @@ -1,8 +1,10 @@ import requests from myutils.proxy import getproxy +from cishu.cishubase import cishubase -class mojidict: + +class mojidict(cishubase): def search(self, word): try: response = requests.post( diff --git a/LunaTranslator/LunaTranslator/cishu/weblio.py b/LunaTranslator/LunaTranslator/cishu/weblio.py index ff6044e4..262916ca 100644 --- a/LunaTranslator/LunaTranslator/cishu/weblio.py +++ b/LunaTranslator/LunaTranslator/cishu/weblio.py @@ -1,11 +1,12 @@ import requests from urllib.parse import quote +from cishu.cishubase import cishubase from myutils.proxy import getproxy import re -class weblio: +class weblio(cishubase): def search(self, word): url = "https://www.weblio.jp/content/" + quote(word) diff --git a/LunaTranslator/LunaTranslator/cishu/xiaoxueguan.py b/LunaTranslator/LunaTranslator/cishu/xiaoxueguan.py index bcaa7917..f23c69c6 100644 --- a/LunaTranslator/LunaTranslator/cishu/xiaoxueguan.py +++ b/LunaTranslator/LunaTranslator/cishu/xiaoxueguan.py @@ -1,14 +1,15 @@ -from myutils.config import globalconfig import sqlite3, os import winsharedutils from myutils.utils import argsort, autosql +from cishu.cishubase import cishubase -class xiaoxueguan: - def __init__(self): + +class xiaoxueguan(cishubase): + def init(self): self.sql = None try: - path = globalconfig["cishu"]["xiaoxueguan"]["path"] + path = self.config["path"] if os.path.exists(path): self.sql = autosql(sqlite3.connect(path, check_same_thread=False)) except: diff --git a/LunaTranslator/LunaTranslator/cishu/youdao.py b/LunaTranslator/LunaTranslator/cishu/youdao.py index 7b1d05f7..c4dd6e43 100644 --- a/LunaTranslator/LunaTranslator/cishu/youdao.py +++ b/LunaTranslator/LunaTranslator/cishu/youdao.py @@ -3,9 +3,10 @@ import requests from urllib.parse import quote import re from myutils.proxy import getproxy +from cishu.cishubase import cishubase -class youdao: +class youdao(cishubase): @property def srclang(self): diff --git a/LunaTranslator/LunaTranslator/gui/dialog_savedgame.py b/LunaTranslator/LunaTranslator/gui/dialog_savedgame.py index d60ba3d4..ddf9de26 100644 --- a/LunaTranslator/LunaTranslator/gui/dialog_savedgame.py +++ b/LunaTranslator/LunaTranslator/gui/dialog_savedgame.py @@ -310,6 +310,7 @@ class tagitem(QWidget): super().__init__() tagLayout = QHBoxLayout() tagLayout.setContentsMargins(0, 0, 0, 0) + tagLayout.setSpacing(0) self._type = _type key = (tag, _type, refdata) self.setLayout(tagLayout) diff --git a/LunaTranslator/LunaTranslator/gui/selecthook.py b/LunaTranslator/LunaTranslator/gui/selecthook.py index f7ba0ad6..3efcae91 100644 --- a/LunaTranslator/LunaTranslator/gui/selecthook.py +++ b/LunaTranslator/LunaTranslator/gui/selecthook.py @@ -520,7 +520,7 @@ class hookselect(closeashidewindow): savehook_new_data[gobject.baseobject.textsource.pname][ "hooktypeasname" ].__setitem__, - json.dumps(key), + json.dumps(gobject.baseobject.textsource.serialkey(key)), ), ), ) diff --git a/LunaTranslator/LunaTranslator/gui/settingpage_cishu.py b/LunaTranslator/LunaTranslator/gui/settingpage_cishu.py index 7b20f26b..d945c42d 100644 --- a/LunaTranslator/LunaTranslator/gui/settingpage_cishu.py +++ b/LunaTranslator/LunaTranslator/gui/settingpage_cishu.py @@ -1,6 +1,6 @@ import functools, os from myutils.config import globalconfig, _TRL -from gui.inputdialog import getsomepath1, autoinitdialog +from gui.inputdialog import autoinitdialog, autoinitdialog_items from gui.usefulwidget import ( getcolorbutton, yuitsu_switch, @@ -18,83 +18,54 @@ def gethiragrid(self): grids = [] i = 0 - self.ocrswitchs = {} + self.hiraswitchs = {} line = [] for name in globalconfig["hirasetting"]: _f = "./LunaTranslator/hiraparse/{}.py".format(name) if os.path.exists(_f) == False: continue - - line += [ - ((globalconfig["hirasetting"][name]["name"]), 5), - getsimpleswitch( - globalconfig["hirasetting"][name], - "use", - parent=self, - name=name, + if "args" in globalconfig["hirasetting"][name]: + items = autoinitdialog_items(globalconfig["hirasetting"][name]) + _3 = getcolorbutton( + globalconfig, + "", callback=functools.partial( - yuitsu_switch, + autoinitdialog, self, - globalconfig["hirasetting"], - "hiraswitchs", - name, - gobject.baseobject.starthira, + globalconfig["hirasetting"][name]["name"], + 800, + items, ), - pair="hiraswitchs", - ), - ] - items = [] - for key in globalconfig["hirasetting"][name]: - if key == "path": - items.append( - { - "t": "file", - "l": globalconfig["hirasetting"][name]["name"], - "d": globalconfig["hirasetting"][name], - "k": "path", - "dir": True, - } - ) - elif key == "token": - items.append( - { - "t": "lineedit", - "l": globalconfig["hirasetting"][name]["token_name"], - "d": globalconfig["hirasetting"][name], - "k": "token", - } - ) - elif key == "codec": - items.append( - { - "t": "combo", - "l": "codec", - "d": globalconfig["hirasetting"][name], - "k": "codec", - "list": ["utf8", "shiftjis"], - } - ) - if len(items): - items.append({"t": "okcancel", "callback": gobject.baseobject.starthira}) - line += [ - getcolorbutton( - globalconfig, - "", - callback=functools.partial( - autoinitdialog, - self, - globalconfig["hirasetting"][name]["name"], - 800, - items, - ), - icon="fa.gear", - constcolor="#FF69B4", - ) - ] + icon="fa.gear", + constcolor="#FF69B4", + ) else: - line += [""] + _3 = "" + + line += [ + ((globalconfig["hirasetting"][name]["name"]), 6), + ( + getsimpleswitch( + globalconfig["hirasetting"][name], + "use", + name=name, + parent=self, + callback=functools.partial( + yuitsu_switch, + self, + globalconfig["hirasetting"], + "hiraswitchs", + name, + gobject.baseobject.starthira, + ), + pair="hiraswitchs", + ), + 1, + ), + _3, + ] if i % 3 == 2: grids.append(line) line = [] @@ -112,7 +83,7 @@ def setTabcishu_l(self): [ [("分词&假名分析器", 10)], [ - ("日语注音方案", 5), + ("日语注音方案", 6), ( getsimplecombobox( _TRL(["平假名", "片假名", "罗马音"]), @@ -128,7 +99,7 @@ def setTabcishu_l(self): [], [], [ - ("点击单词查词", 5), + ("点击单词查词", 6), (getsimpleswitch(globalconfig, "usesearchword"), 1), getcolorbutton( globalconfig, @@ -138,11 +109,11 @@ def setTabcishu_l(self): constcolor="#FF69B4", ), "", - ("点击单词复制", 5), + ("点击单词复制", 6), (getsimpleswitch(globalconfig, "usecopyword"), 1), ], [ - ("使用原型查询", 5), + ("使用原型查询", 6), (getsimpleswitch(globalconfig, "usewordorigin"), 1), ], [], @@ -156,33 +127,28 @@ def setTabcishu_l(self): _f = "./LunaTranslator/cishu/{}.py".format(cishu) if os.path.exists(_f) == False: continue + + items = autoinitdialog_items(globalconfig["cishu"][cishu]) + line += [ - (globalconfig["cishu"][cishu]["name"], 5), + (globalconfig["cishu"][cishu]["name"], 6), getsimpleswitch( globalconfig["cishu"][cishu], "use", callback=functools.partial(gobject.baseobject.startxiaoxueguan, cishu), ), - ( - getcolorbutton( - globalconfig, - "", - callback=functools.partial( - getsomepath1, - self, - globalconfig["cishu"][cishu]["name"], - globalconfig["cishu"][cishu], - "path", - globalconfig["cishu"][cishu]["name"], - functools.partial(gobject.baseobject.startxiaoxueguan, cishu), - globalconfig["cishu"][cishu]["isdir"], - globalconfig["cishu"][cishu]["filter"], - ), - icon="fa.gear", - constcolor="#FF69B4", - ) - if "path" in globalconfig["cishu"][cishu] - else "" + getcolorbutton( + globalconfig, + "", + callback=functools.partial( + autoinitdialog, + self, + globalconfig["cishu"][cishu]["name"], + 800, + items, + ), + icon="fa.gear", + constcolor="#FF69B4", ), ] diff --git a/LunaTranslator/LunaTranslator/gui/settingpage_ocr.py b/LunaTranslator/LunaTranslator/gui/settingpage_ocr.py index 062b25e1..c0cc5b3f 100644 --- a/LunaTranslator/LunaTranslator/gui/settingpage_ocr.py +++ b/LunaTranslator/LunaTranslator/gui/settingpage_ocr.py @@ -16,7 +16,6 @@ def getocrgrid(self): grids = [] i = 0 - lendict = len(list(globalconfig["ocr"].keys())) self.ocrswitchs = {} line = [] diff --git a/LunaTranslator/LunaTranslator/gui/showocrimage.py b/LunaTranslator/LunaTranslator/gui/showocrimage.py index 1055117e..8cbec6e0 100644 --- a/LunaTranslator/LunaTranslator/gui/showocrimage.py +++ b/LunaTranslator/LunaTranslator/gui/showocrimage.py @@ -65,4 +65,6 @@ class showocrimage(closeashidewindow): self.originimage = originimage self.img1 = QPixmap.fromImage(originimage) self.img2 = QPixmap.fromImage(solved) + self.img1.setDevicePixelRatio(self.devicePixelRatioF()) + self.img2.setDevicePixelRatio(self.devicePixelRatioF()) self.showimg() diff --git a/LunaTranslator/LunaTranslator/gui/showword.py b/LunaTranslator/LunaTranslator/gui/showword.py index 723adc97..c8bc066b 100644 --- a/LunaTranslator/LunaTranslator/gui/showword.py +++ b/LunaTranslator/LunaTranslator/gui/showword.py @@ -1,4 +1,3 @@ -from PyQt5.QtCore import Qt from PyQt5.QtWidgets import ( QWidget, QHBoxLayout, @@ -7,24 +6,21 @@ from PyQt5.QtWidgets import ( QLineEdit, QPlainTextEdit, QFormLayout, - QAction, QSizePolicy, - QStylePainter, - QStyleOptionTab, - QStyle, QPushButton, QTextEdit, - QTabWidget,QFileDialog, + QTabWidget, + QFileDialog, QTabBar, QLabel, ) +from PyQt5.QtGui import QPixmap, QImage from traceback import print_exc import requests, json -from PyQt5.QtCore import Qt, pyqtSignal -from PyQt5.QtGui import QCursor -import qtawesome, functools, os, re, base64 -import threading, gobject, uuid -from myutils.config import globalconfig, _TR, _TRL, static_data +from PyQt5.QtCore import pyqtSignal, Qt +import qtawesome, functools, os, base64 +import gobject, uuid +from myutils.config import globalconfig, _TR, static_data import myutils.ankiconnect as anki from gui.usefulwidget import ( closeashidewindow, @@ -33,8 +29,8 @@ from gui.usefulwidget import ( getboxlayout, getspinbox, getlineedit, - saveposwindow, - getsimpleswitch,getcolorbutton, + getsimpleswitch, + getcolorbutton, tabadd_lazy, ) from myutils.wrapper import threader @@ -42,16 +38,22 @@ from myutils.ocrutil import imageCut, ocr_run from gui.rangeselect import rangeselct_function -class AnkiWindow(closeashidewindow): +class AnkiWindow(QWidget): setcurrenttext = pyqtSignal(str) __ocrsettext = pyqtSignal(str) refreshhtml = pyqtSignal() def langdu(self): if gobject.baseobject.reader: - self.audiopath.setText(gobject.baseobject.reader.syncttstofile( - self.wordedit.text() - )) + self.audiopath.setText( + gobject.baseobject.reader.syncttstofile(self.currentword) + ) + + def langdu2(self): + if gobject.baseobject.reader: + self.audiopath_sentence.setText( + gobject.baseobject.reader.syncttstofile(self.example.toPlainText()) + ) @threader def asyncocr(self, fname): @@ -63,20 +65,26 @@ class AnkiWindow(closeashidewindow): fname = "./cache/ocr/cropforanki.png" os.makedirs("./cache/ocr", exist_ok=True) img.save(fname) + self.editpath.setText("") self.editpath.setText(os.path.abspath(fname)) self.asyncocr(fname) rangeselct_function(self, ocroncefunction, False, False) - def __init__(self, parent) -> None: - super().__init__(parent, globalconfig, "ankiwindow") + def __init__(self) -> None: + super().__init__() self.setWindowTitle("Anki Connect") - + self.currentword = "" self.tabs = QTabWidget() self.tabs.addTab(self.createaddtab(), _TR("添加")) tabadd_lazy(self.tabs, "设置", self.creatsetdtab) tabadd_lazy(self.tabs, "模板", self.creattemplatetab) - self.setCentralWidget(self.tabs) + + l = QHBoxLayout() + l.setContentsMargins(0, 0, 0, 0) + l.setSpacing(0) + l.addWidget(self.tabs) + self.setLayout(l) self.refreshhtml.connect(self.refreshhtmlfunction) self.tabs.currentChanged.connect(self.ifshowrefresh) @@ -127,6 +135,7 @@ class AnkiWindow(closeashidewindow): layout = QHBoxLayout() layout.setContentsMargins(0, 0, 0, 0) + layout.setSpacing(0) wid = QWidget() wid.setLayout(layout) @@ -139,7 +148,6 @@ class AnkiWindow(closeashidewindow): layout.addLayout( getboxlayout( [ - QLabel(_TR("编辑")), edittemptab, getboxlayout([revertbtn, savebtn], makewidget=True), ], @@ -152,7 +160,7 @@ class AnkiWindow(closeashidewindow): self.htmlbrowser.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding) layout.addLayout( getboxlayout( - [QLabel(_TR("预览")), self.previewtab, self.htmlbrowser], + [self.previewtab, self.htmlbrowser], lc=QVBoxLayout, margin0=True, ) @@ -179,7 +187,7 @@ class AnkiWindow(closeashidewindow): object.setPlainText(text) def loadfileds(self): - word = self.wordedit.text() + word = self.currentword explain = json.dumps(gobject.baseobject.searchwordW.generate_explains()) remarks = self.remarks.toHtml() example = self.example.toPlainText() @@ -205,12 +213,24 @@ class AnkiWindow(closeashidewindow): if len(self.audiopath.text()): with open(self.audiopath.text(), "rb") as image_file: encoded_string2 = base64.b64encode(image_file.read()).decode("utf-8") - encoded_string2 = ''.format( + encoded_string2 = """""".format( encoded_string2 ) else: encoded_string2 = "" - fields = {"audio": encoded_string2, "image": encoded_string} + if len(self.audiopath_sentence.text()): + with open(self.audiopath_sentence.text(), "rb") as image_file: + encoded_string3 = base64.b64encode(image_file.read()).decode("utf-8") + encoded_string3 = """""".format( + encoded_string3 + ) + else: + encoded_string3 = "" + fields = { + "audio": encoded_string2, + "audio_sentence": encoded_string3, + "image": encoded_string, + } return fields def saveedits(self): @@ -236,13 +256,17 @@ class AnkiWindow(closeashidewindow): _TR("DeckName"), getlineedit(globalconfig["ankiconnect"], "DeckName") ) layout.addRow( - _TR("ModelName"), getlineedit(globalconfig["ankiconnect"], "ModelName") + _TR("ModelName"), getlineedit(globalconfig["ankiconnect"], "ModelName2") ) layout.addRow( _TR("allowDuplicate"), getsimpleswitch(globalconfig["ankiconnect"], "allowDuplicate"), ) + layout.addRow( + _TR("autoUpdateModel"), + getsimpleswitch(globalconfig["ankiconnect"], "autoUpdateModel"), + ) return wid @@ -252,42 +276,94 @@ class AnkiWindow(closeashidewindow): wid.setLayout(layout) soundbutton = QPushButton(qtawesome.icon("fa.music"), "") soundbutton.clicked.connect(self.langdu) + soundbutton2 = QPushButton(qtawesome.icon("fa.music"), "") + soundbutton2.clicked.connect(self.langdu2) cropbutton = QPushButton(qtawesome.icon("fa.crop"), "") cropbutton.clicked.connect(self.crop) - self.wordedit = QLineEdit() - self.wordedit.textEdited.connect(self.reset) - layout.addLayout( - getboxlayout([QLabel(_TR("Word")), self.wordedit]) - ) + + self.audiopath = QLineEdit() + self.audiopath.setReadOnly(True) + self.audiopath_sentence = QLineEdit() + self.audiopath_sentence.setReadOnly(True) + self.editpath = QLineEdit() + self.editpath.setReadOnly(True) + self.viewimagelabel = QLabel() + self.editpath.textChanged.connect(self.wrappedpixmap) self.example = QPlainTextEdit() - layout.addWidget(QLabel(_TR("例句"))) - layout.addWidget(self.example) self.remarks = QTextEdit() - layout.addWidget(QLabel(_TR("备注"))) - layout.addWidget(self.remarks) + layout.addLayout( + getboxlayout( + [ + getboxlayout( + [ + getboxlayout( + [QLabel(_TR("例句")), self.example], + QVBoxLayout, + margin0=True, + ), + getboxlayout( + [QLabel(_TR("备注")), self.remarks], + QVBoxLayout, + margin0=True, + ), + ], + QVBoxLayout, + ), + getboxlayout( + [ + getboxlayout( + [ + QLabel(_TR("语音")), + self.audiopath, + soundbutton, + getcolorbutton( + "", + "", + functools.partial(self.selectaudio), + icon="fa.gear", + constcolor="#FF69B4", + ), + ] + ), + getboxlayout( + [ + QLabel(_TR("语音_例句")), + self.audiopath_sentence, + soundbutton2, + getcolorbutton( + "", + "", + functools.partial(self.selectaudio2), + icon="fa.gear", + constcolor="#FF69B4", + ), + ] + ), + getboxlayout( + [ + QLabel(_TR("截图")), + self.editpath, + cropbutton, + getcolorbutton( + "", + "", + functools.partial(self.selectimage), + icon="fa.gear", + constcolor="#FF69B4", + ), + ] + ), + self.viewimagelabel, + ], + QVBoxLayout, + ), + ] + ) + ) self.tagsedit = QLineEdit() layout.addLayout(getboxlayout([QLabel(_TR("Tags(split by |)")), self.tagsedit])) - - self.audiopath = QLineEdit() - self.audiopath.setReadOnly(True) - layout.addLayout(getboxlayout([QLabel(_TR("语音")),self.audiopath,soundbutton,getcolorbutton( - "", - "", - functools.partial(self.selectaudio), - icon="fa.gear", - constcolor="#FF69B4", - )])) - - self.editpath = QLineEdit() - self.editpath.setReadOnly(True) - layout.addLayout(getboxlayout([QLabel(_TR("截图")),self.editpath,cropbutton,getcolorbutton( - "", - "", - functools.partial(self.selectimage), - icon="fa.gear", - constcolor="#FF69B4", - )])) + btn = QPushButton(_TR("添加")) btn.clicked.connect(self.errorwrap) layout.addWidget(btn) @@ -298,24 +374,46 @@ class AnkiWindow(closeashidewindow): self.reset("") return wid + + def wrappedpixmap(self, src): + pix = QPixmap.fromImage(QImage(src)) + rate = self.devicePixelRatioF() + pix.setDevicePixelRatio(rate) + if ( + pix.width() > self.viewimagelabel.width() + or pix.height() > self.viewimagelabel.height() + ): + pix = pix.scaled(self.viewimagelabel.size() * rate, Qt.KeepAspectRatio) + self.viewimagelabel.setPixmap(pix) + def selectimage(self): f = QFileDialog.getOpenFileName() res = f[0] if res != "": self.editpath.setText(res) + def selectaudio(self): f = QFileDialog.getOpenFileName() res = f[0] if res != "": self.audiopath.setText(res) + + def selectaudio2(self): + f = QFileDialog.getOpenFileName() + res = f[0] + if res != "": + self.audiopath_sentence.setText(res) + def reset(self, text): - self.wordedit.setText(text) + self.currentword = text if text and len(text): self.ruby = json.dumps(gobject.baseobject.translation_ui.parsehira(text)) else: self.ruby = "" self.editpath.clear() self.audiopath.clear() + self.audiopath_sentence.clear() + def errorwrap(self): try: self.addanki() @@ -345,9 +443,10 @@ class AnkiWindow(closeashidewindow): return model_htmlfront, model_htmlback, model_css def addanki(self): + autoUpdateModel = globalconfig["ankiconnect"]["autoUpdateModel"] allowDuplicate = globalconfig["ankiconnect"]["allowDuplicate"] anki.global_port = globalconfig["ankiconnect"]["port"] - ModelName = globalconfig["ankiconnect"]["ModelName"] + ModelName = globalconfig["ankiconnect"]["ModelName2"] DeckName = globalconfig["ankiconnect"]["DeckName"] model_htmlfront, model_htmlback, model_css = self.tryloadankitemplates() try: @@ -370,19 +469,24 @@ class AnkiWindow(closeashidewindow): ], ) except anki.AnkiModelExists: - model = anki.Model(ModelName) - model.updateStyling(model_css) - model.updateTemplates( - { - "LUNACARDTEMPLATE1": { - "Front": model_htmlfront, - "Back": model_htmlback, + if autoUpdateModel: + model = anki.Model(ModelName) + model.updateStyling(model_css) + model.updateTemplates( + { + "LUNACARDTEMPLATE1": { + "Front": model_htmlfront, + "Back": model_htmlback, + } } - } - ) + ) media = [] tempfiles = [] - for k, _ in [("audio", self.audiopath.text()), ("image", self.editpath.text())]: + for k, _ in [ + ("audio", self.audiopath.text()), + ("audio_sentence", self.audiopath_sentence.text()), + ("image", self.editpath.text()), + ]: if len(_): media.append( [ @@ -403,49 +507,47 @@ class AnkiWindow(closeashidewindow): self.loadfileds(), allowDuplicate, tags, - media[0], - media[1], + media[0] + media[1], + media[2], ) class searchwordW(closeashidewindow): getnewsentencesignal = pyqtSignal(str, bool) - searchthreadsignal = pyqtSignal(str, dict, str) showtabsignal = pyqtSignal(str, str) def __init__(self, parent): super(searchwordW, self).__init__(parent, globalconfig, "sw_geo") - self.ankiwindow = AnkiWindow(self) + self.ankiwindow = AnkiWindow() self.setupUi() # self.setWindowFlags(self.windowFlags()&~Qt.WindowMinimizeButtonHint) self.getnewsentencesignal.connect(self.getnewsentence) self.setWindowTitle(_TR("查词")) def showresfun(self, k, res): - first = res.split("
")[0] + self.cache_results[k] = res - self.textbs[k].insertHtml(first) - self.textbs[k].firsttext = self.textbs[k].toPlainText() - self.textbs[k].insertHtml(res[len(first) :]) - - scrollbar = self.textbs[k].verticalScrollBar() - scrollbar.setValue(0) - self.tab.setTabVisible(self._k.index(k), True) + thisp = globalconfig["cishu"][k]["args"]["priority"] + idx = 0 + for kk in self.tabks: + if globalconfig["cishu"][kk]["args"]["priority"] >= thisp: + idx += 1 + self.tabks.insert(idx, k) + self.tab.insertTab(idx, _TR(globalconfig["cishu"][k]["name"])) def setupUi(self): self.setWindowIcon(qtawesome.icon("fa.search")) self.showtabsignal.connect(self.showresfun) - self.centralWidget = QWidget(self) + ww = QWidget(self) self.setWindowIcon(qtawesome.icon("fa.gear")) - self.hboxlayout = QHBoxLayout(self.centralWidget) self.vboxlayout = QVBoxLayout() - + ww.setLayout(self.vboxlayout) self.searchlayout = QHBoxLayout() self.vboxlayout.addLayout(self.searchlayout) self.searchtext = QLineEdit() - # self.searchtext.setFont(font) + self.searchtext.textChanged.connect(self.ankiwindow.reset) self.searchlayout.addWidget(self.searchtext) searchbutton = QPushButton(qtawesome.icon("fa.search"), "") # _TR("搜索")) @@ -457,38 +559,41 @@ class searchwordW(closeashidewindow): self.searchlayout.addWidget(soundbutton) ankiconnect = QPushButton(qtawesome.icon("fa.adn"), "") - ankiconnect.clicked.connect(self.ankiwindow.show) + ankiconnect.clicked.connect(self.onceaddankiwindow) self.searchlayout.addWidget(ankiconnect) - self.tab = QTabWidget(self) + self.tab = QTabBar(self) + self.tab.currentChanged.connect( + lambda idx: self.textOutput.setHtml(self.cache_results[self.tabks[idx]]) + ) + self.tabks = [] + self.setCentralWidget(ww) - self.vboxlayout.addWidget(self.tab) - self.hboxlayout.addLayout(self.vboxlayout) - self.setCentralWidget(self.centralWidget) - - self.textbs = {} - - _k = [] - _name = [] - for cishu in globalconfig["cishu"]: - _name.append(globalconfig["cishu"][cishu]["name"]) - _k.append(cishu) - self._k = _k - _name = _TRL(_name) - - for i in range(len(_name)): - - textOutput = QTextBrowser(self) - # textOutput.setFont(font) - textOutput.setUndoRedoEnabled(False) - textOutput.setReadOnly(True) - textOutput.setOpenLinks(False) - self.tab.addTab(textOutput, _name[i]) - self.tab.setTabVisible(i, False) - - self.textbs[self._k[i]] = textOutput + textOutput = QTextBrowser(self) + textOutput.setUndoRedoEnabled(False) + textOutput.setReadOnly(True) + textOutput.setOpenLinks(False) + self.textOutput = textOutput + self.cache_results = {} self.hiding = True - self.searchthreadsignal.connect(self.searchthread) + self.addankiwindowidx = 0 + + tablayout = QVBoxLayout() + tablayout.addWidget(self.tab) + tablayout.addWidget(textOutput) + tablayout.setContentsMargins(0, 0, 0, 0) + tablayout.setSpacing(0) + self.vboxlayout.addLayout(tablayout) + + def onceaddankiwindow(self): + if self.addankiwindowidx == 0: + self.vboxlayout.addWidget(self.ankiwindow) + else: + if self.addankiwindowidx % 2 == 0: + self.ankiwindow.show() + else: + self.ankiwindow.hide() + self.addankiwindowidx += 1 def langdu(self): if gobject.baseobject.reader: @@ -496,13 +601,18 @@ class searchwordW(closeashidewindow): def generate_explains(self): res = [] - for i in range(len(self._k)): - if len(self.textbs[self._k[i]].toPlainText()) == 0: + tabks = [] + for k, v in self.cache_results.items(): + if len(v) == 0: continue - - res.append( - {"source": self._k[i], "content": self.textbs[self._k[i]].toHtml()} - ) + thisp = globalconfig["cishu"][k]["args"]["priority"] + idx = 0 + for i in tabks: + if i >= thisp: + idx += 1 + k = _TR(globalconfig["cishu"][k]["name"]) + tabks.append(thisp) + res.insert(idx, {"source": k, "content": v}) return res def getnewsentence(self, sentence, append): @@ -513,22 +623,14 @@ class searchwordW(closeashidewindow): self.search(sentence) - def searchthread(self, k, _mp, sentence): - - _mp[k].callback = functools.partial(self.showtabsignal.emit, k) - _mp[k].search(sentence) - def search(self, sentence): if sentence == "": return self.ankiwindow.setcurrenttext.emit(sentence) - _mp = {} - _mp.update(gobject.baseobject.cishus) - - for k in self._k: - self.tab.setTabVisible(self._k.index(k), False) - self.textbs[k].clear() - if k in _mp: - threading.Thread( - target=self.searchthreadsignal.emit, args=(k, _mp, sentence) - ).start() + for i in range(self.tab.count()): + self.tab.removeTab(0) + self.tabks.clear() + self.cache_results.clear() + for k, cishu in gobject.baseobject.cishus.items(): + cishu.callback = functools.partial(self.showtabsignal.emit, k) + cishu.safesearch(sentence) diff --git a/LunaTranslator/LunaTranslator/gui/translatorUI.py b/LunaTranslator/LunaTranslator/gui/translatorUI.py index f5221153..06e29710 100644 --- a/LunaTranslator/LunaTranslator/gui/translatorUI.py +++ b/LunaTranslator/LunaTranslator/gui/translatorUI.py @@ -2,13 +2,12 @@ import time import functools import threading import os, sys -from PyQt5.QtCore import QT_VERSION_STR import windows, importlib from traceback import print_exc from PyQt5.QtCore import Qt, pyqtSignal import qtawesome from PyQt5.QtCore import pyqtSignal, Qt, QSize -from PyQt5.QtGui import QCursor, QColor +from PyQt5.QtGui import QCursor from PyQt5.QtWidgets import QLabel, QPushButton, QSystemTrayIcon import gobject from myutils.wrapper import threader, trypass @@ -152,25 +151,8 @@ class QUnFrameWindow(resizableframeless): hira = [] try: if gobject.baseobject.hira_: - hira = gobject.baseobject.hira_.fy(text) - for _1 in range(len(hira)): - _ = len(hira) - 1 - _1 - if globalconfig["hira_vis_type"] == 0: - hira[_]["hira"] = hira[_]["hira"].translate(self.castkata2hira) - elif globalconfig["hira_vis_type"] == 1: - hira[_]["hira"] = hira[_]["hira"].translate(self.casthira2kata) - elif globalconfig["hira_vis_type"] == 2: - __kanas = [ - static_data["hira"] + ["っ"], - static_data["kata"] + ["ッ"], - ] - target = static_data["roma"] + ["-"] - for _ka in __kanas: - for __idx in range(len(_ka)): - _reverse_idx = len(_ka) - 1 - __idx - hira[_]["hira"] = hira[_]["hira"].replace( - _ka[_reverse_idx], target[_reverse_idx] - ) + hira = gobject.baseobject.hira_.parseparse(text) + except: print_exc() return hira @@ -624,8 +606,7 @@ class QUnFrameWindow(resizableframeless): self.tray.setIcon(icon) showintab(int(self.winId()), globalconfig["showintab"]) self.isfirstshow = True - if QT_VERSION_STR != "5.5.1": - self.setAttribute(Qt.WA_TranslucentBackground) + self.setAttribute(Qt.WA_TranslucentBackground) self.setAttribute(Qt.WA_ShowWithoutActivating, True) self.showintab = globalconfig["showintab"] self.setWindowTitle("LunaTranslator") @@ -655,12 +636,6 @@ class QUnFrameWindow(resizableframeless): self.quitf_signal.connect(self.close) self.fullsgame_signal.connect(self._fullsgame) - self.castkata2hira = str.maketrans( - static_data["allkata"], static_data["allhira"] - ) - self.casthira2kata = str.maketrans( - static_data["allhira"], static_data["allkata"] - ) self.fullscreenmanager_busy = False self.isletgamefullscreened = False self.fullscreenmanager = None diff --git a/LunaTranslator/LunaTranslator/gui/usefulwidget.py b/LunaTranslator/LunaTranslator/gui/usefulwidget.py index 743354d0..5c9508ad 100644 --- a/LunaTranslator/LunaTranslator/gui/usefulwidget.py +++ b/LunaTranslator/LunaTranslator/gui/usefulwidget.py @@ -9,6 +9,7 @@ from PyQt5.QtWidgets import ( QSizePolicy, QHBoxLayout, QWidget, + QLayout, ) from webviewpy import ( @@ -507,7 +508,10 @@ def selectcolor( def getboxlayout(widgets, lc=QHBoxLayout, margin0=False, makewidget=False): cp_layout = lc() for w in widgets: - cp_layout.addWidget(w) + if isinstance(w, QWidget): + cp_layout.addWidget(w) + elif isinstance(w, QLayout): + cp_layout.addLayout(w) if margin0: cp_layout.setContentsMargins(0, 0, 0, 0) if makewidget: diff --git a/LunaTranslator/LunaTranslator/hiraparse/basehira.py b/LunaTranslator/LunaTranslator/hiraparse/basehira.py new file mode 100644 index 00000000..cd4a7317 --- /dev/null +++ b/LunaTranslator/LunaTranslator/hiraparse/basehira.py @@ -0,0 +1,59 @@ +from myutils.config import globalconfig, static_data +from traceback import print_exc + + +class basehira: + def init(self): + pass + + def parse(self, text): + return [] + + def __init__(self, typename) -> None: + self.typename = typename + self.castkata2hira = str.maketrans( + static_data["allkata"], static_data["allhira"] + ) + self.casthira2kata = str.maketrans( + static_data["allhira"], static_data["allkata"] + ) + self.needinit = True + self.init() + self.needinit = False + + @property + def config(self): + return globalconfig["hirasetting"][self.typename]["args"] + + def parseparse(self, text): + hira = [] + try: + if self.needinit: + self.init() + self.needinit = False + try: + hira = self.parse(text) + except Exception as e: + self.needinit = True + raise e + for _1 in range(len(hira)): + _ = len(hira) - 1 - _1 + if globalconfig["hira_vis_type"] == 0: + hira[_]["hira"] = hira[_]["hira"].translate(self.castkata2hira) + elif globalconfig["hira_vis_type"] == 1: + hira[_]["hira"] = hira[_]["hira"].translate(self.casthira2kata) + elif globalconfig["hira_vis_type"] == 2: + __kanas = [ + static_data["hira"] + ["っ"], + static_data["kata"] + ["ッ"], + ] + target = static_data["roma"] + ["-"] + for _ka in __kanas: + for __idx in range(len(_ka)): + _reverse_idx = len(_ka) - 1 - __idx + hira[_]["hira"] = hira[_]["hira"].replace( + _ka[_reverse_idx], target[_reverse_idx] + ) + except: + print_exc() + return hira diff --git a/LunaTranslator/LunaTranslator/hiraparse/latin.py b/LunaTranslator/LunaTranslator/hiraparse/latin.py index 10c10f0d..2b2bb993 100644 --- a/LunaTranslator/LunaTranslator/hiraparse/latin.py +++ b/LunaTranslator/LunaTranslator/hiraparse/latin.py @@ -1,8 +1,9 @@ -class hira: - def __init__(self) -> None: - pass +from hiraparse.basehira import basehira - def fy(self, text): + +class latin(basehira): + + def parse(self, text): _x = [] i = 0 for _ in text.split(" "): diff --git a/LunaTranslator/LunaTranslator/hiraparse/mecab.py b/LunaTranslator/LunaTranslator/hiraparse/mecab.py index 7c00b897..e8f8ecae 100644 --- a/LunaTranslator/LunaTranslator/hiraparse/mecab.py +++ b/LunaTranslator/LunaTranslator/hiraparse/mecab.py @@ -1,22 +1,21 @@ -from myutils.config import globalconfig import winsharedutils import os +from hiraparse.basehira import basehira -class hira: - def __init__(self) -> None: - hirasettingbase = globalconfig["hirasetting"] - mecabpath = hirasettingbase["mecab"]["path"] +class mecab(basehira): + def init(self) -> None: + mecabpath = self.config["path"] if os.path.exists(mecabpath): self.kks = winsharedutils.mecabwrap( mecabpath ) # fugashi.Tagger('-r nul -d "{}" -Owakati'.format(mecabpath)) - def fy(self, text): + def parse(self, text): start = 0 result = [] - codec = ["utf8", "shiftjis"][globalconfig["hirasetting"]["mecab"]["codec"]] + codec = ["utf8", "shiftjis"][self.config["codec"]] for node, fields in self.kks.parse( text, codec ): # self.kks.parseToNodeList(text): @@ -49,7 +48,6 @@ class hira: if origorig is None: origorig = orig - start += l hira = kana # .translate(self.h2k) diff --git a/LunaTranslator/LunaTranslator/hiraparse/mojinlt.py b/LunaTranslator/LunaTranslator/hiraparse/mojinlt.py index bc48cf66..dfb6ea75 100644 --- a/LunaTranslator/LunaTranslator/hiraparse/mojinlt.py +++ b/LunaTranslator/LunaTranslator/hiraparse/mojinlt.py @@ -1,14 +1,10 @@ -from myutils.config import globalconfig - from myutils.proxy import getproxy import requests +from hiraparse.basehira import basehira -class hira: - def __init__(self) -> None: - pass - - def fy(self, text): +class mojinlt(basehira): + def parse(self, text): def nlt(text, token): try: @@ -37,4 +33,4 @@ class hira: except: return [] - return nlt(text, globalconfig["hirasetting"]["mojinlt"]["token"]) + return nlt(text, self.config["Moji NLT Token"]) diff --git a/LunaTranslator/LunaTranslator/textsource/texthook.py b/LunaTranslator/LunaTranslator/textsource/texthook.py index 60beaac3..0f10c46d 100644 --- a/LunaTranslator/LunaTranslator/textsource/texthook.py +++ b/LunaTranslator/LunaTranslator/textsource/texthook.py @@ -460,10 +460,14 @@ class texthook(basetext): return True + def serialkey(self, key): + hc, hn, tp = key + return (tp.processId, tp.addr, tp.ctx, tp.ctx2, hn, hc) + def serialselectedhook(self): xx = [] - for hc, hn, tp in self.selectedhook: - xx.append((tp.processId, tp.addr, tp.ctx, tp.ctx2, hn, hc)) + for key in self.selectedhook: + xx.append(self.serialkey(key)) return xx def checkisusingembed(self, address, ctx1, ctx2): diff --git a/LunaTranslator/LunaTranslator/translator/baidu_dev.py b/LunaTranslator/LunaTranslator/translator/baidu_dev.py index a2017bae..35456e80 100644 --- a/LunaTranslator/LunaTranslator/translator/baidu_dev.py +++ b/LunaTranslator/LunaTranslator/translator/baidu_dev.py @@ -1,5 +1,4 @@ from translator.basetranslator_dev import basetransdev -from urllib.parse import quote class TS(basetransdev): @@ -18,15 +17,12 @@ class TS(basetransdev): def translate(self, content): self.Runtime_evaluate( - "document.getElementsByClassName('textarea-clear-btn')[0].click()" + """document.querySelector("#editor-text > div.AZLVLJHb > div.Ssl84aLh > span").click()""" ) - - self.Page_navigate( - "https://fanyi.baidu.com/#{}/{}/{}".format( - self.srclang, self.tgtlang, quote(content) - ) + self.Runtime_evaluate( + """document.querySelector("#editor-text > div.AZLVLJHb > div.Ssl84aLh > div > div > div").click()""" + ) + self.send_keys(content) + return self.wait_for_result( + """document.querySelector("#trans-selection").innerText""" ) - res = self.wait_for_result( - "document.querySelector('div.output-bd')===null?'':document.querySelector('div.output-bd').innerText" - ).replace("\n\n", "\n") - return res diff --git a/LunaTranslator/LunaTranslator/translator/basetranslator_dev.py b/LunaTranslator/LunaTranslator/translator/basetranslator_dev.py index afb1e08c..90f38fa8 100644 --- a/LunaTranslator/LunaTranslator/translator/basetranslator_dev.py +++ b/LunaTranslator/LunaTranslator/translator/basetranslator_dev.py @@ -40,28 +40,21 @@ class basetransdev(basetrans): if self.using == False: return self._id += 1 - try: - if ws is None: - ws = self.ws - ws.send(json.dumps({"id": self._id, "method": method, "params": params})) - res = ws.recv() - except: - print_exc() - self._createtarget() - time.sleep(1) - return self._SendRequest(method, params, ws) + + if ws is None: + ws = self.ws + ws.send(json.dumps({"id": self._id, "method": method, "params": params})) + res = ws.recv() + res = json.loads(res) try: return res["result"] except: - print(res) - if ( - res["method"] == "Inspector.detached" - and res["params"]["reason"] == "target_closed" - ): - self._createtarget() - return self._SendRequest(method, params, ws) - + if res['error']['code']==-32600: + self._id+=1 + self._SendRequest(method,params,ws) + else: + raise def _createtarget(self): if self.using == False: return diff --git a/LunaTranslator/LunaTranslator/translator/dev_baidu_ai.py b/LunaTranslator/LunaTranslator/translator/dev_baidu_ai.py deleted file mode 100644 index 35456e80..00000000 --- a/LunaTranslator/LunaTranslator/translator/dev_baidu_ai.py +++ /dev/null @@ -1,28 +0,0 @@ -from translator.basetranslator_dev import basetransdev - - -class TS(basetransdev): - target_url = "https://fanyi.baidu.com/mtpe-individual/multimodal#/" - - def langmap(self): - return { - "es": "spa", - "ko": "kor", - "fr": "fra", - "ja": "jp", - "cht": "cht", - "vi": "vie", - "uk": "ukr", - } - - def translate(self, content): - self.Runtime_evaluate( - """document.querySelector("#editor-text > div.AZLVLJHb > div.Ssl84aLh > span").click()""" - ) - self.Runtime_evaluate( - """document.querySelector("#editor-text > div.AZLVLJHb > div.Ssl84aLh > div > div > div").click()""" - ) - self.send_keys(content) - return self.wait_for_result( - """document.querySelector("#trans-selection").innerText""" - ) diff --git a/LunaTranslator/files/anki/back.html b/LunaTranslator/files/anki/back.html index 86a223b7..8a0501bd 100644 --- a/LunaTranslator/files/anki/back.html +++ b/LunaTranslator/files/anki/back.html @@ -1,6 +1,9 @@ -
{{audio}}
+
+
{{audio}}
+
{{audio_sentence}}
+
-
+
-
+
{{example_sentence}}
{{image}}
-
- {{remarks}} -
-
@@ -46,11 +45,22 @@ -
+
{{example_sentence}}
@@ -22,6 +25,13 @@