This commit is contained in:
恍兮惚兮 2024-11-03 14:14:26 +08:00
parent 2cb8a09c64
commit 16cd0fd6e2
6 changed files with 85 additions and 70 deletions

View File

@ -494,7 +494,7 @@ def setTabTwo_lazy(self, basel):
), ),
"", "",
"显示翻译器名称", "显示翻译器名称",
D_getsimpleswitch(globalconfig, "showfanyisource"), D_getsimpleswitch(globalconfig, "showfanyisource", callback=lambda x:gobject.baseobject.translation_ui.translate_text.textbrowser.showhidetranslatorname(x)),
"", "",
"翻译请求间隔(s)", "翻译请求间隔(s)",
D_getspinbox( D_getspinbox(

View File

@ -55,6 +55,7 @@ class Textbrowser(QFrame):
self.textbrowser.show() self.textbrowser.show()
self.textbrowser.setselectable(globalconfig["selectable"]) self.textbrowser.setselectable(globalconfig["selectable"])
self.textbrowser.showhideorigin(globalconfig["isshowrawtext"]) self.textbrowser.showhideorigin(globalconfig["isshowrawtext"])
self.textbrowser.showhidetranslatorname(globalconfig["showfanyisource"])
self.textbrowser.showhidetranslate(globalconfig["showfanyi"]) self.textbrowser.showhidetranslate(globalconfig["showfanyi"])
self.refreshcontent() self.refreshcontent()
@ -76,17 +77,21 @@ class Textbrowser(QFrame):
self.trace = [] self.trace = []
self.loadinternal() self.loadinternal()
def iter_append(self, iter_context_class, origin, atcenter, text, color): def iter_append(self, iter_context_class, origin, atcenter, name, text, color):
self.trace.append((1, (iter_context_class, origin, atcenter, text, color)))
self.cleared = False
self.textbrowser.iter_append(iter_context_class, origin, atcenter, text, color)
def append(self, origin, atcenter, text, tag, flags, color):
self.trace.append( self.trace.append(
(0, (origin, atcenter, text, copy.deepcopy(tag), flags, color)) (1, (iter_context_class, origin, atcenter, name, text, color))
) )
self.cleared = False self.cleared = False
self.textbrowser.append(origin, atcenter, text, tag, flags, color) self.textbrowser.iter_append(
iter_context_class, origin, atcenter, name, text, color
)
def append(self, origin, atcenter, name, text, tag, flags, color):
self.trace.append(
(0, (origin, atcenter, name, text, copy.deepcopy(tag), flags, color))
)
self.cleared = False
self.textbrowser.append(origin, atcenter, name, text, tag, flags, color)
def clear(self): def clear(self):
self.cleared = True self.cleared = True

View File

@ -326,13 +326,10 @@ class TranslatorWindow(resizableframeless):
iter_context = kwargs.get("iter_context", None) iter_context = kwargs.get("iter_context", None)
clear = kwargs.get("clear", False) clear = kwargs.get("clear", False)
if globalconfig["showfanyisource"]:
_showtext = name + " " + res
else:
_showtext = res
self.showline( self.showline(
name=name,
clear=clear, clear=clear,
text=_showtext, text=res,
color=color, color=color,
origin=False, origin=False,
iter_context=iter_context, iter_context=iter_context,
@ -386,6 +383,7 @@ class TranslatorWindow(resizableframeless):
return "\n".join(newlines) return "\n".join(newlines)
def showline(self, **kwargs): # clear,res,color ,type_=1,origin=True): def showline(self, **kwargs): # clear,res,color ,type_=1,origin=True):
name = kwargs.get("name", "")
clear = kwargs.get("clear", True) clear = kwargs.get("clear", True)
origin = kwargs.get("origin", True) origin = kwargs.get("origin", True)
text = kwargs.get("text", None) text = kwargs.get("text", None)
@ -407,12 +405,13 @@ class TranslatorWindow(resizableframeless):
iter_res_status = 0 iter_res_status = 0
if iter_res_status: if iter_res_status:
self.translate_text.iter_append( self.translate_text.iter_append(
iter_context_class, origin, atcenter, text, color iter_context_class, origin, atcenter,name, text, color
) )
else: else:
self.translate_text.append( self.translate_text.append(
origin, origin,
atcenter, atcenter,
name,
text, text,
hira, hira,
flags, flags,

View File

@ -199,6 +199,9 @@ class TextBrowser(QWidget, dataget):
c.setCharFormat(f) c.setCharFormat(f)
self.textbrowser.setTextCursor(c) self.textbrowser.setTextCursor(c)
def showhidetranslatorname(self, show):
self.parent().refreshcontent()
def showhidetranslate(self, show): def showhidetranslate(self, show):
self.parent().refreshcontent() self.parent().refreshcontent()
@ -212,6 +215,11 @@ class TextBrowser(QWidget, dataget):
return True return True
return False return False
def checkaddname(self, name, text):
if name and globalconfig["showfanyisource"]:
text = name + " " + text
return text
def __findsame(self, s1, s2): def __findsame(self, s1, s2):
i = 0 i = 0
while i < len(s1) and i < len(s2): while i < len(s1) and i < len(s2):
@ -220,9 +228,10 @@ class TextBrowser(QWidget, dataget):
i += 1 i += 1
return i return i
def iter_append(self, iter_context_class, origin, atcenter, text, color): def iter_append(self, iter_context_class, origin, atcenter, name, text, color):
if self.checkskip(origin): if self.checkskip(origin):
return return
text = self.checkaddname(name, text)
if iter_context_class not in self.saveiterclasspointer: if iter_context_class not in self.saveiterclasspointer:
self._textbrowser_append(origin, atcenter, "", [], color) self._textbrowser_append(origin, atcenter, "", [], color)
self.saveiterclasspointer[iter_context_class] = { self.saveiterclasspointer[iter_context_class] = {
@ -266,9 +275,10 @@ class TextBrowser(QWidget, dataget):
) )
self.cleared = False self.cleared = False
def append(self, origin, atcenter, text, tag, flags, color): def append(self, origin, atcenter, name, text, tag, flags, color):
if self.checkskip(origin): if self.checkskip(origin):
return return
text = self.checkaddname(name, text)
if len(tag): if len(tag):
isshowhira, isshow_fenci, isfenciclick = flags isshowhira, isshow_fenci, isfenciclick = flags
font = self._createqfont(origin) font = self._createqfont(origin)

View File

@ -164,37 +164,12 @@
} }
return ele return ele
} }
let begin = 0; let begin1 = "";
let end = text.length - 1; let end1 = "";
for (; (begin < text.length) && (text[begin] == ' '); begin++) { begin1 += "&nbsp;" }
for (; (end >= 0) && (text[end] == ' '); end--) { end1 += "&nbsp;"; }
if ((end == (text.length - 1)) && (begin == 0))
return ele
let wrap = document.createElement('div') let wrap = document.createElement('div')
wrap.style.whiteSpace = 'pre-wrap'
wrap.appendChild(ele)
if (args.atcenter) { if (args.atcenter) {
wrap.style.textAlign = "center" wrap.style.textAlign = "center"
} }
if (begin != 0) {
let __1 = document.createElement('div')
__1.innerHTML = begin1
__1.style.display = 'inline-block'
commoninit_font_align_height(__1, args)
wrap.appendChild(__1)
}
let _ww = document.createElement('div')
_ww.appendChild(ele)
_ww.style.display = 'inline-block'
wrap.appendChild(_ww)
if ((end != -1) && (end != (text.length - 1))) {
//全是空格
let __1 = document.createElement('div')
__1.innerHTML = end1
__1.style.display = 'inline-block'
commoninit_font_align_height(__1, args)
wrap.appendChild(__1)
}
return wrap return wrap
} }
function dispatch_text_style_inlineblock(style, styleargs, text, args) { function dispatch_text_style_inlineblock(style, styleargs, text, args) {
@ -204,13 +179,33 @@
div.appendChild(l) div.appendChild(l)
return div return div
} }
function dispatch_text_style(style, styleargs, text, args) { function addnameblock(style, styleargs, index, name, text, args) {
if (index == 0 && name.length) {
let userawhtml = args.userawhtml
args.userawhtml = false
let div = dispatch_text_style_line(style, styleargs, name + " ", args)
let divx = document.createElement('div')
divx.classList.add('lunatranslator_translator_name')
divx.appendChild(div)
args.userawhtml = userawhtml
let div2 = document.createElement('div')
div2.appendChild(divx)
let textdiv = dispatch_text_style_inlineblock(style, styleargs, text, args)
div2.appendChild(textdiv)
if (args.atcenter) {
div2.style.textAlign = "center"
}
return div2
}
return dispatch_text_style_line(style, styleargs, text, args)
}
function dispatch_text_style(style, styleargs, name, text, args) {
if (!text.includes('\n')) if (!text.includes('\n'))
return dispatch_text_style_line(style, styleargs, text, args) return addnameblock(style, styleargs, 0, name, text, args)
let lines = text.split('\n') let lines = text.split('\n')
let wrap = document.createElement('div') let wrap = document.createElement('div')
lines.forEach(line => { lines.forEach((line, index) => {
wrap.appendChild(dispatch_text_style_line(style, styleargs, line, args)) wrap.appendChild(addnameblock(style, styleargs, index, name, line, args))
}) })
return wrap return wrap
} }
@ -219,7 +214,14 @@
</style> </style>
<style id="maybeshowtranslate"> <style id="maybeshowtranslate">
</style> </style>
<style id="maybeshowtranslatorname">
</style>
<script> <script>
function _showhidetranslatorname(show) {
let css = ['.lunatranslator_translator_name{ display: none; }', '.lunatranslator_translator_name{ display: inline-block; }'][show]
document.getElementById('maybeshowtranslatorname').innerHTML = css;
safe_calllunaheightchange(document.getElementById(rootdivid).offsetHeight)
}
function _showhidetranslate(show) { function _showhidetranslate(show) {
let css = ['.lunatranslator_translate{ display: none; }', '.lunatranslator_translate{ display: block; }'][show] let css = ['.lunatranslator_translate{ display: none; }', '.lunatranslator_translate{ display: block; }'][show]
document.getElementById('maybeshowtranslate').innerHTML = css; document.getElementById('maybeshowtranslate').innerHTML = css;
@ -241,13 +243,14 @@
} }
document.getElementById(rootdivid).appendChild(div); document.getElementById(rootdivid).appendChild(div);
} }
function _create_internal_text(style, styleargsjson, _id, textu, argsjson) { function _create_internal_text(style, styleargsjson, _id, nameu, textu, argsjson) {
let args = JSON.parse(decodeURIComponent(argsjson))
let styleargs = JSON.parse(decodeURIComponent(styleargsjson))
let text = decodeURIComponent(textu)
let innerele = dispatch_text_style(style, styleargs, text, args)
let ele = document.getElementById(_id) let ele = document.getElementById(_id)
ele.innerHTML = '' ele.innerHTML = ''
let args = JSON.parse(decodeURIComponent(argsjson))
let styleargs = JSON.parse(decodeURIComponent(styleargsjson))
let name = decodeURIComponent(nameu)
let text = decodeURIComponent(textu)
let innerele = dispatch_text_style(style, styleargs, name, text, args)
ele.appendChild(innerele) ele.appendChild(innerele)
safe_calllunaheightchange(document.getElementById(rootdivid).offsetHeight) safe_calllunaheightchange(document.getElementById(rootdivid).offsetHeight)
} }
@ -385,6 +388,7 @@
//api //api
showhideorigin = _showhideorigin showhideorigin = _showhideorigin
showhidetranslate = _showhidetranslate showhidetranslate = _showhidetranslate
showhidetranslatorname = _showhidetranslatorname
create_div_line_id = _create_div_line_id; create_div_line_id = _create_div_line_id;
create_internal_text = _create_internal_text; create_internal_text = _create_internal_text;
create_internal_rubytext = _create_internal_rubytext; create_internal_rubytext = _create_internal_rubytext;

View File

@ -135,6 +135,9 @@ class TextBrowser(QWidget, dataget):
def showhideorigin(self, show): def showhideorigin(self, show):
self.debugeval(f'showhideorigin("{int(show)}")') self.debugeval(f'showhideorigin("{int(show)}")')
def showhidetranslatorname(self, show):
self.debugeval(f'showhidetranslatorname("{int(show)}")')
def create_div_line_id(self, _id, origin): def create_div_line_id(self, _id, origin):
self.debugeval(f'create_div_line_id("{_id}","{int(origin)}")') self.debugeval(f'create_div_line_id("{_id}","{int(origin)}")')
@ -149,12 +152,13 @@ class TextBrowser(QWidget, dataget):
html = quote(html) html = quote(html)
self.debugeval(f'set_extra_html("{html}")') self.debugeval(f'set_extra_html("{html}")')
def create_internal_text(self, style, styleargs, _id, text, args): def create_internal_text(self, style, styleargs, _id, name, text, args):
name = quote(name)
text = quote(text) text = quote(text)
args = quote(json.dumps(args)) args = quote(json.dumps(args))
styleargs = quote(json.dumps(styleargs)) styleargs = quote(json.dumps(styleargs))
self.debugeval( self.debugeval(
f'create_internal_text("{style}","{styleargs}","{_id}","{text}","{args}");' f'create_internal_text("{style}","{styleargs}","{_id}","{name}","{text}","{args}");'
) )
self._qweb_query_h() self._qweb_query_h()
@ -221,14 +225,14 @@ class TextBrowser(QWidget, dataget):
# native api end # native api end
def iter_append(self, iter_context_class, origin, atcenter, text, color): def iter_append(self, iter_context_class, origin, atcenter, name, text, color):
if iter_context_class not in self.saveiterclasspointer: if iter_context_class not in self.saveiterclasspointer:
_id = self.createtextlineid(origin) _id = self.createtextlineid(origin)
self.saveiterclasspointer[iter_context_class] = _id self.saveiterclasspointer[iter_context_class] = _id
_id = self.saveiterclasspointer[iter_context_class] _id = self.saveiterclasspointer[iter_context_class]
self._webview_append(_id, origin, atcenter, text, [], [], color) self._webview_append(_id, origin, atcenter, name, text, [], [], color)
def createtextlineid(self, origin): def createtextlineid(self, origin):
@ -236,10 +240,10 @@ class TextBrowser(QWidget, dataget):
self.create_div_line_id(_id, origin) self.create_div_line_id(_id, origin)
return _id return _id
def append(self, origin, atcenter, text, tag, flags, color): def append(self, origin, atcenter, name, text, tag, flags, color):
_id = self.createtextlineid(origin) _id = self.createtextlineid(origin)
self._webview_append(_id, origin, atcenter, text, tag, flags, color) self._webview_append(_id, origin, atcenter, name, text, tag, flags, color)
def measureH(self, font_family, font_size): def measureH(self, font_family, font_size):
font = QFont() font = QFont()
@ -258,7 +262,9 @@ class TextBrowser(QWidget, dataget):
]["webview"][0] ]["webview"][0]
return currenttype return currenttype
def _webview_append(self, _id, origin, atcenter, text: str, tag, flags, color): def _webview_append(
self, _id, origin, atcenter, name: str, text: str, tag, flags, color
):
fmori, fsori, boldori = self._getfontinfo(origin) fmori, fsori, boldori = self._getfontinfo(origin)
fmkana, fskana, boldkana = self._getfontinfo_kana() fmkana, fskana, boldkana = self._getfontinfo_kana()
kanacolor = self._getkanacolor() kanacolor = self._getkanacolor()
@ -301,15 +307,6 @@ class TextBrowser(QWidget, dataget):
userawhtml = text.startswith(sig) userawhtml = text.startswith(sig)
if userawhtml: if userawhtml:
text = text[len(sig) :] text = text[len(sig) :]
else:
if sig in text:
# 显示名称时。不管了,就这样吧
text = text.replace(sig, "")
userawhtml = True
if len(text) == 0:
userawhtml = True
text = "<br>"
args = dict( args = dict(
atcenter=atcenter, atcenter=atcenter,
@ -321,7 +318,7 @@ class TextBrowser(QWidget, dataget):
userawhtml=userawhtml, userawhtml=userawhtml,
) )
self.create_internal_text(style, styleargs, _id, text, args) self.create_internal_text(style, styleargs, _id, name, text, args)
def clear(self): def clear(self):