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)",
D_getspinbox(

View File

@ -55,6 +55,7 @@ class Textbrowser(QFrame):
self.textbrowser.show()
self.textbrowser.setselectable(globalconfig["selectable"])
self.textbrowser.showhideorigin(globalconfig["isshowrawtext"])
self.textbrowser.showhidetranslatorname(globalconfig["showfanyisource"])
self.textbrowser.showhidetranslate(globalconfig["showfanyi"])
self.refreshcontent()
@ -76,17 +77,21 @@ class Textbrowser(QFrame):
self.trace = []
self.loadinternal()
def iter_append(self, iter_context_class, origin, atcenter, 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):
def iter_append(self, iter_context_class, origin, atcenter, name, text, color):
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.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):
self.cleared = True

View File

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

View File

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

View File

@ -164,37 +164,12 @@
}
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')
wrap.style.whiteSpace = 'pre-wrap'
wrap.appendChild(ele)
if (args.atcenter) {
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
}
function dispatch_text_style_inlineblock(style, styleargs, text, args) {
@ -204,13 +179,33 @@
div.appendChild(l)
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'))
return dispatch_text_style_line(style, styleargs, text, args)
return addnameblock(style, styleargs, 0, name, text, args)
let lines = text.split('\n')
let wrap = document.createElement('div')
lines.forEach(line => {
wrap.appendChild(dispatch_text_style_line(style, styleargs, line, args))
lines.forEach((line, index) => {
wrap.appendChild(addnameblock(style, styleargs, index, name, line, args))
})
return wrap
}
@ -219,7 +214,14 @@
</style>
<style id="maybeshowtranslate">
</style>
<style id="maybeshowtranslatorname">
</style>
<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) {
let css = ['.lunatranslator_translate{ display: none; }', '.lunatranslator_translate{ display: block; }'][show]
document.getElementById('maybeshowtranslate').innerHTML = css;
@ -241,13 +243,14 @@
}
document.getElementById(rootdivid).appendChild(div);
}
function _create_internal_text(style, styleargsjson, _id, 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)
function _create_internal_text(style, styleargsjson, _id, nameu, textu, argsjson) {
let ele = document.getElementById(_id)
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)
safe_calllunaheightchange(document.getElementById(rootdivid).offsetHeight)
}
@ -385,6 +388,7 @@
//api
showhideorigin = _showhideorigin
showhidetranslate = _showhidetranslate
showhidetranslatorname = _showhidetranslatorname
create_div_line_id = _create_div_line_id;
create_internal_text = _create_internal_text;
create_internal_rubytext = _create_internal_rubytext;

View File

@ -136,6 +136,9 @@ class TextBrowser(QWidget, dataget):
def showhideorigin(self, 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):
self.debugeval(f'create_div_line_id("{_id}","{int(origin)}")')
@ -149,12 +152,13 @@ class TextBrowser(QWidget, dataget):
html = quote(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)
args = quote(json.dumps(args))
styleargs = quote(json.dumps(styleargs))
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()
@ -221,14 +225,14 @@ class TextBrowser(QWidget, dataget):
# 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:
_id = self.createtextlineid(origin)
self.saveiterclasspointer[iter_context_class] = _id
_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):
@ -236,10 +240,10 @@ class TextBrowser(QWidget, dataget):
self.create_div_line_id(_id, origin)
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)
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):
font = QFont()
@ -258,7 +262,9 @@ class TextBrowser(QWidget, dataget):
]["webview"][0]
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)
fmkana, fskana, boldkana = self._getfontinfo_kana()
kanacolor = self._getkanacolor()
@ -301,15 +307,6 @@ class TextBrowser(QWidget, dataget):
userawhtml = text.startswith(sig)
if userawhtml:
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(
atcenter=atcenter,
@ -321,7 +318,7 @@ class TextBrowser(QWidget, dataget):
userawhtml=userawhtml,
)
self.create_internal_text(style, styleargs, _id, text, args)
self.create_internal_text(style, styleargs, _id, name, text, args)
def clear(self):