Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
HIllya51 committed Jan 19, 2025
1 parent b89ad72 commit 6509c18
Show file tree
Hide file tree
Showing 11 changed files with 158 additions and 56 deletions.
18 changes: 8 additions & 10 deletions py/LunaTranslator/LunaTranslator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -260,7 +260,7 @@ def displayinfomessage(self, text, infotype):
if infotype == "<notrans>":
self.translation_ui.displayres.emit(
dict(
color=globalconfig["rawtextcolor"],
color=SpecialColor.RawTextColor,
res=text,
clear=True,
)
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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: (
Expand Down Expand Up @@ -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),
)
Expand Down Expand Up @@ -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),
)
Expand Down
3 changes: 2 additions & 1 deletion py/LunaTranslator/gui/edittext.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
getIconButton,
IconButton,
)
from gui.textbrowser import TranslateColor
from gui.dynalang import LPushButton, LMainWindow
from gui.setting_textinput import loadvalidtss

Expand Down Expand Up @@ -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"),
)
Expand Down
1 change: 1 addition & 0 deletions py/LunaTranslator/gui/setting_cishu.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions py/LunaTranslator/gui/setting_display_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
9 changes: 4 additions & 5 deletions py/LunaTranslator/gui/setting_translate.py
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -23,7 +23,7 @@
makegrid,
makesubtab_lazy,
makescrollgrid,
IconButton
IconButton,
)
from gui.dynalang import LPushButton, LLabel, LAction
from gui.setting_about import offlinelinks
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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,
Expand Down
68 changes: 66 additions & 2 deletions py/LunaTranslator/gui/textbrowser.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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(
Expand All @@ -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,
Expand Down
19 changes: 8 additions & 11 deletions py/LunaTranslator/gui/translatorUI.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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

Expand All @@ -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)

Expand All @@ -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)
Expand Down
2 changes: 0 additions & 2 deletions py/LunaTranslator/rendertext/somefunctions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Loading

0 comments on commit 6509c18

Please sign in to comment.