From 6509c186278b4c54f09bb351dc0913c743e1519e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=81=8D=E5=85=AE=E6=83=9A=E5=85=AE?= <1173718158@qq.com> Date: Sun, 19 Jan 2025 22:50:29 +0800 Subject: [PATCH] . --- py/LunaTranslator/LunaTranslator.py | 18 +++-- py/LunaTranslator/gui/edittext.py | 3 +- py/LunaTranslator/gui/setting_cishu.py | 1 + py/LunaTranslator/gui/setting_display_text.py | 1 + py/LunaTranslator/gui/setting_translate.py | 9 ++- py/LunaTranslator/gui/textbrowser.py | 68 ++++++++++++++++++- py/LunaTranslator/gui/translatorUI.py | 19 +++--- py/LunaTranslator/rendertext/somefunctions.py | 2 - py/LunaTranslator/rendertext/textbrowser.py | 33 ++++++--- py/LunaTranslator/rendertext/webview.html | 26 +++++-- py/LunaTranslator/rendertext/webview.py | 34 +++++++--- 11 files changed, 158 insertions(+), 56 deletions(-) diff --git a/py/LunaTranslator/LunaTranslator.py b/py/LunaTranslator/LunaTranslator.py index 2cc011b6b5d..e60bc5fdf1f 100644 --- a/py/LunaTranslator/LunaTranslator.py +++ b/py/LunaTranslator/LunaTranslator.py @@ -51,7 +51,7 @@ from myutils.audioplayer import series_audioplayer from gui.dynalang import LAction, LMenu from gui.setting_textinput_ocr import showocrimage -from gui.textbrowser import TextType +from gui.textbrowser import TextType, SpecialColor, TranslateColor class MAINUI: @@ -260,7 +260,7 @@ def displayinfomessage(self, text, infotype): if infotype == "": self.translation_ui.displayres.emit( dict( - color=globalconfig["rawtextcolor"], + color=SpecialColor.RawTextColor, res=text, clear=True, ) @@ -346,9 +346,7 @@ def textgetmethod_1( if len(text) > globalconfig["maxlength"]: text = text[: globalconfig["maxlength"]] + "……" - self.translation_ui.displayraw1.emit( - dict(text=text, color=globalconfig["rawtextcolor"]) - ) + self.translation_ui.displayraw1.emit(text) return try: @@ -376,8 +374,7 @@ def textgetmethod_1( self.dispatchoutputer(text) _showrawfunction_unsafe = functools.partial( - self.translation_ui.displayraw1.emit, - dict(text=text, color=globalconfig["rawtextcolor"]), + self.translation_ui.displayraw1.emit, text ) self.thishastranslated = globalconfig["showfanyi"] _showrawfunction = lambda: ( @@ -484,10 +481,11 @@ def analyzecontent(self, text_solved, optimization_params): def _delaypreparefixrank(self, _showrawfunction, real_fix_rank): _showrawfunction() for engine in real_fix_rank: - colorx = globalconfig["fanyi"].get(engine, globalconfig["fanyi"]["premt"]) + if engine not in globalconfig: + engine = "premt" displayreskwargs = dict( name="", - color=colorx["color"], + color=TranslateColor(engine), res="", iter_context=(1, engine), ) @@ -583,7 +581,7 @@ def GetTranslationCallback( ): displayreskwargs = dict( name=apiname, - color=globalconfig["fanyi"][classname]["color"], + color=TranslateColor(classname), res=res, iter_context=(iter_res_status, classname), ) diff --git a/py/LunaTranslator/gui/edittext.py b/py/LunaTranslator/gui/edittext.py index cabb024fa10..bf5e017792e 100644 --- a/py/LunaTranslator/gui/edittext.py +++ b/py/LunaTranslator/gui/edittext.py @@ -9,6 +9,7 @@ getIconButton, IconButton, ) +from gui.textbrowser import TranslateColor from gui.dynalang import LPushButton, LMainWindow from gui.setting_textinput import loadvalidtss @@ -157,7 +158,7 @@ def submitfunction(self): ) displayreskwargs = dict( name=globalconfig["fanyi"]["realtime_edit"]["name"], - color=globalconfig["fanyi"]["realtime_edit"]["color"], + color=TranslateColor("realtime_edit"), res=text, iter_context=(1, "realtime_edit_directvis_fakeclass"), ) diff --git a/py/LunaTranslator/gui/setting_cishu.py b/py/LunaTranslator/gui/setting_cishu.py index f4ab98706b9..7c607c1f9a9 100644 --- a/py/LunaTranslator/gui/setting_cishu.py +++ b/py/LunaTranslator/gui/setting_cishu.py @@ -244,6 +244,7 @@ def setTabcishu_l(self): globalconfig, "jiamingcolor", self.jiamingcolor_b, + callback=gobject.baseobject.translation_ui.translate_text.textbrowser.setcolors, ), name="jiamingcolor_b", parent=self, diff --git a/py/LunaTranslator/gui/setting_display_text.py b/py/LunaTranslator/gui/setting_display_text.py index 1f5b420aa13..36d820c02c2 100644 --- a/py/LunaTranslator/gui/setting_display_text.py +++ b/py/LunaTranslator/gui/setting_display_text.py @@ -336,6 +336,7 @@ def xianshigrid_style(self): globalconfig, "rawtextcolor", self.original_color_button, + callback=gobject.baseobject.translation_ui.translate_text.textbrowser.setcolors, ), name="original_color_button", parent=self, diff --git a/py/LunaTranslator/gui/setting_translate.py b/py/LunaTranslator/gui/setting_translate.py index fac26af648d..56092ac5b59 100644 --- a/py/LunaTranslator/gui/setting_translate.py +++ b/py/LunaTranslator/gui/setting_translate.py @@ -1,6 +1,6 @@ from qtsymbols import * import functools, os -import gobject, qtawesome, uuid, shutil +import gobject, uuid, shutil from myutils.config import globalconfig, translatorsetting from myutils.utils import ( selectdebugfile, @@ -23,7 +23,7 @@ makegrid, makesubtab_lazy, makescrollgrid, - IconButton + IconButton, ) from gui.dynalang import LPushButton, LLabel, LAction from gui.setting_about import offlinelinks @@ -324,9 +324,7 @@ def createmanybtn(self, countnum, btnplus): btn = IconButton("fa.question") if btnplus == "offline": btn.clicked.connect( - lambda: os.startfile( - dynamiclink("{docs_server}/{lang}/offlinellm.html") - ) + lambda: os.startfile(dynamiclink("{docs_server}/{lang}/offlinellm.html")) ) elif btnplus == "api": btn.clicked.connect( @@ -384,6 +382,7 @@ def initsome11(self, l, label=None, btnplus=False): None, self, "fanyicolor_" + fanyi, + callback=gobject.baseobject.translation_ui.translate_text.textbrowser.setcolors, ), ), last, diff --git a/py/LunaTranslator/gui/textbrowser.py b/py/LunaTranslator/gui/textbrowser.py index 93011f67af7..e76705a0db7 100644 --- a/py/LunaTranslator/gui/textbrowser.py +++ b/py/LunaTranslator/gui/textbrowser.py @@ -13,6 +13,68 @@ class TextType: Error_translator = 4 +class ColorControl: + RAW_TEXT_COLOR = 0 + TS_COLOR = 1 + ERROR_COLOR = 2 + COLOR_DEFAULT = 2 + KANA_COLOR = 3 + + def __init__(self, T, klass=None): + self.type = T + self.klass = klass + + def get(self): + if self.type == self.RAW_TEXT_COLOR: + return globalconfig["rawtextcolor"] + if self.type == self.KANA_COLOR: + return globalconfig["jiamingcolor"] + if self.type == self.ERROR_COLOR: + return "red" + if self.type == self.COLOR_DEFAULT: + return "black" + if self.type == self.TS_COLOR: + return globalconfig["fanyi"].get(self.klass, {}).get("color", "black") + + def asklass(self): + if self.type == self.RAW_TEXT_COLOR: + return "ColorControl_RAW_TEXT_COLOR" + if self.type == self.KANA_COLOR: + return "ColorControl_KANA_COLOR" + if self.type == self.ERROR_COLOR: + return "ColorControl_ERROR_COLOR" + if self.type == self.COLOR_DEFAULT: + return "ColorControl_COLOR_DEFAULT" + if self.type == self.TS_COLOR: + return "ColorControl_TS_COLOR_{}".format(self.klass) + + def _tuple_(self): + if self.klass: + return (self.type, self.klass) + return self.type + + def __repr__(self): + return str(self._tuple_()) + + def __hash__(self): + return self._tuple_().__hash__() + + def __eq__(self, value): + return self._tuple_() == value._tuple_() + + +class TranslateColor(ColorControl): + def __init__(self, klass): + super().__init__(ColorControl.TS_COLOR, klass) + + +class SpecialColor: + RawTextColor = ColorControl(ColorControl.RAW_TEXT_COLOR) + ErrorColor = ColorControl(ColorControl.ERROR_COLOR) + DefaultColor = ColorControl(ColorControl.COLOR_DEFAULT) + KanaColor = ColorControl(ColorControl.KANA_COLOR) + + class Textbrowser(QFrame): contentsChanged = pyqtSignal(QSize) dropfilecallback = pyqtSignal(str) @@ -87,7 +149,9 @@ def __init__(self, parent): self.trace = [] self.loadinternal() - def iter_append(self, iter_context_class, texttype: TextType, name, text, color): + def iter_append( + self, iter_context_class, texttype: TextType, name, text, color: ColorControl + ): self.trace.append((1, (iter_context_class, texttype, name, text, color))) self.cleared = False self.textbrowser.iter_append( @@ -99,7 +163,7 @@ def checkaddname(self, name, text): text = name + " " + text return text - def append(self, texttype: TextType, name, text, tag, flags, color): + def append(self, texttype: TextType, name, text, tag, flags, color: ColorControl): self.trace.append( ( 0, diff --git a/py/LunaTranslator/gui/translatorUI.py b/py/LunaTranslator/gui/translatorUI.py index 6fbaacd78f4..8289f8d9ee3 100644 --- a/py/LunaTranslator/gui/translatorUI.py +++ b/py/LunaTranslator/gui/translatorUI.py @@ -27,7 +27,7 @@ ) from gui.setting_about import doupdate from gui.dialog_memory import dialog_memory -from gui.textbrowser import Textbrowser, TextType +from gui.textbrowser import Textbrowser, TextType, SpecialColor, TranslateColor from gui.rangeselect import rangeselct_function from gui.usefulwidget import resizableframeless, getQMessageBox, findnearestscreen from gui.edittext import edittrans @@ -272,7 +272,7 @@ class TranslatorWindow(resizableframeless): displaylink = pyqtSignal(str) displaymessagebox = pyqtSignal(str, str) displayres = pyqtSignal(dict) - displayraw1 = pyqtSignal(dict) + displayraw1 = pyqtSignal(str) displaystatus = pyqtSignal(str, int) showhideuisignal = pyqtSignal() toolbarhidedelaysignal = pyqtSignal() @@ -373,12 +373,9 @@ def showres(self, kwargs): except: print_exc() - def showraw(self, kwargs): - text = kwargs.get("text") - color = kwargs.get("color") - + def showraw(self, text): + color = SpecialColor.RawTextColor clear = True - hira = [] isshowhira = isshow_fenci = isfenciclick = False @@ -400,13 +397,13 @@ def showraw(self, kwargs): def showstatus(self, res, t: TextType): if t == TextType.Info: - color = globalconfig["rawtextcolor"] + color = SpecialColor.RawTextColor clear = True elif t == TextType.Error_origin: - color = "red" + color = SpecialColor.ErrorColor clear = True elif t == TextType.Error_translator: - color = "red" + color = SpecialColor.ErrorColor clear = False self.showline(clear=clear, text=res, color=color, texttype=t) @@ -427,7 +424,7 @@ def showline(self, **kwargs): # clear,res,color ,type_=1,origin=True): clear = kwargs.get("clear", True) texttype = kwargs.get("texttype", TextType.Origin) text = kwargs.get("text", None) - color = kwargs.get("color", "black") + color = kwargs.get("color", SpecialColor.DefaultColor) iter_context = kwargs.get("iter_context", None) hira = kwargs.get("hira", []) flags = kwargs.get("flags", None) diff --git a/py/LunaTranslator/rendertext/somefunctions.py b/py/LunaTranslator/rendertext/somefunctions.py index 9eecb3289ed..397e6001c52 100644 --- a/py/LunaTranslator/rendertext/somefunctions.py +++ b/py/LunaTranslator/rendertext/somefunctions.py @@ -40,5 +40,3 @@ def _getfontinfo_kana(self): fm, fs, bold = self._getfontinfo(TextType.Origin) return fm, fs * globalconfig["kanarate"], bold - def _getkanacolor(self): - return globalconfig["jiamingcolor"] diff --git a/py/LunaTranslator/rendertext/textbrowser.py b/py/LunaTranslator/rendertext/textbrowser.py index 916b32e8d0e..02f156c37e7 100644 --- a/py/LunaTranslator/rendertext/textbrowser.py +++ b/py/LunaTranslator/rendertext/textbrowser.py @@ -5,7 +5,7 @@ from traceback import print_exc from rendertext.textbrowser_imp.base import base from gui.dynalang import LAction -from gui.textbrowser import TextType +from gui.textbrowser import TextType, ColorControl, SpecialColor class Qlabel_c(QLabel): @@ -374,6 +374,9 @@ def showhideerror(self, show): def setfontstyle(self): pass + def setcolors(self): + pass + def checkskip(self, texttype: TextType): if (texttype in (TextType.Origin,)) and (not globalconfig["isshowrawtext"]): return True @@ -395,7 +398,9 @@ def __findsame(self, s1, s2): i += 1 return i - def iter_append(self, iter_context_class, texttype: TextType, text, color): + def iter_append( + self, iter_context_class, texttype: TextType, text, color: ColorControl + ): if self.checkskip(texttype): return if iter_context_class not in self.saveiterclasspointer: @@ -441,7 +446,7 @@ def iter_append(self, iter_context_class, texttype: TextType, text, color): ) self.cleared = False - def append(self, texttype: TextType, text, tag, flags, color): + def append(self, texttype: TextType, text, tag, flags, color: ColorControl): if self.checkskip(texttype): return if len(tag): @@ -461,7 +466,9 @@ def append(self, texttype: TextType, text, tag, flags, color): def _getqalignment(self, atcenter): return Qt.AlignmentFlag.AlignCenter if atcenter else Qt.AlignmentFlag.AlignLeft - def _textbrowser_append(self, texttype: TextType, text: str, tag: list, color): + def _textbrowser_append( + self, texttype: TextType, text: str, tag: list, color: ColorControl + ): self.textbrowser.document().blockSignals(True) font = self._createqfont(texttype) self._setnextfont(font, self.cleared) @@ -637,7 +644,9 @@ def _deletebetween(self, p1, p2): self.textcursor.setPosition(p2, QTextCursor.MoveMode.KeepAnchor) self.textcursor.removeSelectedText() - def _showyinyingtext2(self, color, iter_context_class, pos, text, font): + def _showyinyingtext2( + self, color: ColorControl, iter_context_class, pos, text, font + ): if iter_context_class not in self.iteryinyinglabelsave: self.iteryinyinglabelsave[iter_context_class] = [] for label in self.iteryinyinglabelsave[iter_context_class]: @@ -670,7 +679,7 @@ def _showyinyingtext2(self, color, iter_context_class, pos, text, font): ) _ = self.iteryinyinglabelsave[iter_context_class][i] if _.text() != subtext[i]: - _.setColor(color) + _.setColor(color.get()) _.setText(subtext[i]) _.setFont(font) _.adjustSize() @@ -724,7 +733,7 @@ def _showyinyingtext2(self, color, iter_context_class, pos, text, font): def maxvisheight(self): return QApplication.primaryScreen().virtualGeometry().height() * 2 - def _showyinyingtext(self, b1, b2, color, font): + def _showyinyingtext(self, b1, b2, color: ColorControl, font): linei = self.yinyingposline doc = self.textbrowser.document() @@ -752,7 +761,7 @@ def _showyinyingtext(self, b1, b2, color, font): self.yinyinglabels.append(self.currentclass(self.toplabel2)) _ = self.yinyinglabels[self.yinyinglabels_idx] self.yinyinglabels_idx += 1 - _.setColor(color) + _.setColor(color.get()) _.setText(block.text()[s : s + l]) _.setFont(font) _.adjustSize() @@ -762,7 +771,9 @@ def _showyinyingtext(self, b1, b2, color, font): linei += 1 self.yinyingposline = linei - def _add_searchlabel(self, isfenciclick, isshow_fenci, labeli, pos1, word, color): + def _add_searchlabel( + self, isfenciclick, isshow_fenci, labeli, pos1, word, color: ColorControl + ): if labeli >= len(self.searchmasklabels_clicked): ql = QLabel(self.atback_color) ql.setMouseTracking(True) @@ -788,7 +799,7 @@ def _add_searchlabel(self, isfenciclick, isshow_fenci, labeli, pos1, word, color gobject.baseobject.clickwordcallback, word ) if isshow_fenci and color: - style = "background-color: {};".format(color) + style = "background-color: {};".format(color.get()) else: style = "background:transparent" self.searchmasklabels[labeli].setGeometry(*pos1) @@ -924,7 +935,7 @@ def solvejiaminglabel(self, idx, word, font, fontori_m: QFontMetricsF, tl1, fha) if idx >= len(self.savetaglabels): self.savetaglabels.append(self.currentclass(self.atback2)) _: base = self.savetaglabels[idx] - color = self._getkanacolor() + color = SpecialColor.KanaColor.get() _.setColor(color) _.setText(word["hira"]) origin = word["orig_X"] diff --git a/py/LunaTranslator/rendertext/webview.html b/py/LunaTranslator/rendertext/webview.html index b198acb81db..12566926579 100644 --- a/py/LunaTranslator/rendertext/webview.html +++ b/py/LunaTranslator/rendertext/webview.html @@ -36,6 +36,7 @@ } + __PLACEHOLDER_EXTRA_HTML_