From a9a9160d3b74f1940f51b3c523d4f152eb9ade74 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=81=8D=E5=85=AE=E6=83=9A=E5=85=AE?= <101191390+HIllya51@users.noreply.github.com> Date: Wed, 6 Mar 2024 13:55:50 +0800 Subject: [PATCH] issues/558 --- .../LunaTranslator/LunaTranslator.py | 30 +++--- .../LunaTranslator/gui/textbrowser.py | 44 ++++++++- .../LunaTranslator/gui/translatorUI.py | 91 ++++++++++++------- .../translator/basetranslator.py | 17 +++- 4 files changed, 125 insertions(+), 57 deletions(-) diff --git a/LunaTranslator/LunaTranslator/LunaTranslator.py b/LunaTranslator/LunaTranslator/LunaTranslator.py index a123f732..415ff55e 100644 --- a/LunaTranslator/LunaTranslator/LunaTranslator.py +++ b/LunaTranslator/LunaTranslator/LunaTranslator.py @@ -43,7 +43,6 @@ class MAINUI() : self.lasttranslatorindex=0 self.translators={} self.cishus={} - self.iterresstatus={} self.reader=None self.textsource_p=None self.currentmd5='0' @@ -153,7 +152,7 @@ class MAINUI() : self.currentsignature=time.time() if type(text)==str: if text.startswith(''): - self.translation_ui.displayres.emit('',globalconfig['rawtextcolor'],text[len(''):],onlytrans,[]) + self.translation_ui.displayres.emit(dict(color=globalconfig['rawtextcolor'],res=text[len(''):],onlytrans=onlytrans)) self.currenttext=text self.currentread=text return @@ -250,7 +249,7 @@ class MAINUI() : _colork=k else: _colork='premt' - self.GetTranslationCallback(onlytrans,_colork,self.currentsignature,optimization_params,_showrawfunction,_showrawfunction_sig,text,res[k],embedcallback,False) + self.GetTranslationCallback(onlytrans,_colork,self.currentsignature,optimization_params,_showrawfunction,_showrawfunction_sig,text,res[k],embedcallback,0) except: print_exc() @@ -265,7 +264,6 @@ class MAINUI() : keys=list(self.translators.keys())+list(self.translators.keys()) keys=keys[self.lasttranslatorindex:self.lasttranslatorindex+_len] #print(keys,usenum,self.lasttranslatorindex) - self.iterresstatus.clear() for engine in keys: if engine not in self.premtalready: self.translators[engine].gettask((partial(self.GetTranslationCallback,onlytrans,engine,self.currentsignature, optimization_params,_showrawfunction,_showrawfunction_sig,text),text,text_solved,skip,embedcallback,is_auto_run,hira)) @@ -275,13 +273,13 @@ class MAINUI() : break - def GetTranslationCallback(self,onlytrans,classname,currentsignature,optimization_params,_showrawfunction,_showrawfunction_sig,contentraw,res,embedcallback,is_iter_res): + def GetTranslationCallback(self,onlytrans,classname,currentsignature,optimization_params,_showrawfunction,_showrawfunction_sig,contentraw,res,embedcallback,iter_res_status): if classname in self.usefultranslators: self.usefultranslators.remove(classname) if embedcallback is None and currentsignature!=self.currentsignature:return - embedtrans=self.GetTranslationCallback_(onlytrans,classname,currentsignature,optimization_params,_showrawfunction,_showrawfunction_sig,contentraw,res,is_iter_res) + embedtrans=self.GetTranslationCallback_(onlytrans,classname,currentsignature,optimization_params,_showrawfunction,_showrawfunction_sig,contentraw,res,iter_res_status) if embedtrans is None and len(self.usefultranslators)==0: @@ -289,7 +287,7 @@ class MAINUI() : if embedcallback and embedtrans is not None: embedcallback(embedtrans) - def GetTranslationCallback_(self,onlytrans,classname,currentsignature,optimization_params,_showrawfunction,_showrawfunction_sig,contentraw,res,is_iter_res): + def GetTranslationCallback_(self,onlytrans,classname,currentsignature,optimization_params,_showrawfunction,_showrawfunction_sig,contentraw,res,iter_res_status): if type(res)==str: @@ -311,19 +309,17 @@ class MAINUI() : _showrawfunction() if currentsignature==self.currentsignature and globalconfig['showfanyi']: - if is_iter_res: - if classname not in self.iterresstatus: - self.iterresstatus[classname]=1 - self.translation_ui.displayres.emit(globalconfig['fanyi'][classname]['name'],globalconfig['fanyi'][classname]['color'],res,onlytrans,[True,classname]) - else: - self.translation_ui.displayres.emit(globalconfig['fanyi'][classname]['name'],globalconfig['fanyi'][classname]['color'],res,onlytrans,[False,classname]) - else: - self.translation_ui.displayres.emit(globalconfig['fanyi'][classname]['name'],globalconfig['fanyi'][classname]['color'],res,onlytrans,[]) + displayreskwargs=dict(name=globalconfig['fanyi'][classname]['name'],color=globalconfig['fanyi'][classname]['color'],res=res,onlytrans=onlytrans) + if iter_res_status: + displayreskwargs.update(iter_context=(iter_res_status,classname)) + self.translation_ui.displayres.emit(displayreskwargs) try: - self.textsource.sqlqueueput((contentraw,classname,res)) - except:pass + if iter_res_status in (0,2):#0为普通,1为iter,2为iter终止,3为起始 + self.textsource.sqlqueueput((contentraw,classname,res)) + except: + pass if globalconfig['embedded']['as_fast_as_posible'] or classname==list(globalconfig['fanyi'])[globalconfig['embedded']['translator']]: diff --git a/LunaTranslator/LunaTranslator/gui/textbrowser.py b/LunaTranslator/LunaTranslator/gui/textbrowser.py index 2136ba60..ad6753a8 100644 --- a/LunaTranslator/LunaTranslator/gui/textbrowser.py +++ b/LunaTranslator/LunaTranslator/gui/textbrowser.py @@ -121,6 +121,7 @@ class Textbrowser( ): self.setselectable() self.blockcount=0 self.needdouble=False + self.iteryinyinglabelsave={} def setselectable(self): self.masklabel.setHidden(globalconfig['selectable']) self.toplabel2.setHidden(globalconfig['selectable'] and globalconfig['zitiyangshi']!=3) @@ -187,7 +188,7 @@ class Textbrowser( ): else: self.align=False - def append(self,x ,tag,origin ): + def append(self,x,tag,origin): if self.cleared: _space='' @@ -243,6 +244,44 @@ class Textbrowser( ): self.textcursor.setPosition(p1,QTextCursor.MoveAnchor) self.textcursor.setPosition(p2,QTextCursor.KeepAnchor) self.textcursor.removeSelectedText() + def showyinyingtext2(self,color,iter_context_class,pos,text): + if iter_context_class not in self.iteryinyinglabelsave: + self.iteryinyinglabelsave[iter_context_class]=[] + for label in self.iteryinyinglabelsave[iter_context_class]: + label.hide() + + + subtext=[] + subpos=[] + lastpos=None + posx=pos + for i in range(len(text)): + if text[i] =='\n':continue + self.textcursor.setPosition(posx) + posx+=1 + tl1=self.textbrowser.cursorRect(self.textcursor).topLeft() + if lastpos is None or tl1.y()!=lastpos.y(): + lastpos=tl1 + subpos.append(lastpos) + subtext.append('') + subtext[-1]+=text[i] + + + if (len(subtext))>len(self.iteryinyinglabelsave[iter_context_class]): + _newlabels=[QLabel(self.toplabel2) for i in range(len(subtext)-len(self.iteryinyinglabelsave[iter_context_class]))] + self.iteryinyinglabelsave[iter_context_class]+=_newlabels + for lb in _newlabels: + lb.setTextFormat(Qt.PlainText) + for i in range(len(subtext)): + _=self.iteryinyinglabelsave[iter_context_class][i] + _.move(subpos[i]) + _.setText(subtext[i]) + _.setFont(self.textbrowser.currentCharFormat().font()) + + _.setStyleSheet("color:{}; background-color:rgba(0,0,0,0)".format(globalconfig['miaobiancolor'])) + _.setGraphicsEffect(self.geteffect(globalconfig['fontsize'],color,globalconfig['shadowforce'])) + _.show() + def showyinyingtext(self,color ): linei=self.yinyingposline @@ -610,6 +649,9 @@ class Textbrowser( ): label.hide() for label in self.yinyinglabels: label.hide() + for klass,labels in self.iteryinyinglabelsave.items(): + for label in labels: + label.hide() # self.textbrowser.clear() # self.textbrowserback.clear() self.yinyingpos=0 diff --git a/LunaTranslator/LunaTranslator/gui/translatorUI.py b/LunaTranslator/LunaTranslator/gui/translatorUI.py index aaf821dc..31542890 100644 --- a/LunaTranslator/LunaTranslator/gui/translatorUI.py +++ b/LunaTranslator/LunaTranslator/gui/translatorUI.py @@ -28,8 +28,8 @@ from gui.rangeselect import moveresizegame ,rangeselct_function from gui.usefulwidget import resizableframeless from gui.dialog_savedgame import browserdialog class QUnFrameWindow(resizableframeless): - displayres = pyqtSignal(str,str,str ,bool,list) - displayraw1 = pyqtSignal(list, str,str,bool) + displayres=pyqtSignal(dict) + displayraw1=pyqtSignal(list, str,str,bool) displaystatus=pyqtSignal(str,str,bool,bool) showhideuisignal=pyqtSignal() hookfollowsignal=pyqtSignal(int,tuple) @@ -71,18 +71,22 @@ class QUnFrameWindow(resizableframeless): pass self.move(self.pos().x()+ other[0],self.pos().y()+ other[1]) - def showres(self,name,color,res,onlyshowhist,iter_res_info): + def showres(self,kwargs):#name,color,res,onlyshowhist,iter_context): try: - if len(iter_res_info): - starting,klass=iter_res_info - if not starting and klass in self.saveiterclasspointer: - if res=='\0':#清除前面的输出 - self.translate_text.deletebetween(self.saveiterclasspointer[klass]['start'],self.translate_text.getcurrpointer()) - else: - self.translate_text.insertatpointer(self.saveiterclasspointer[klass]['curr'],res) - self.saveiterclasspointer[klass]['curr']=self.translate_text.getcurrpointer() + name=kwargs.get('name','') + color=kwargs.get('color') + res=kwargs.get('res') + onlyshowhist=kwargs.get('onlyshowhist') + iter_context=kwargs.get('iter_context',None) + + if iter_context: + iter_res_status,iter_context_class=iter_context + if iter_res_status==2: #iter结束 + gobject.baseobject.transhis.getnewtranssignal.emit(name,res) return - gobject.baseobject.transhis.getnewtranssignal.emit(name,res) + else: + gobject.baseobject.transhis.getnewtranssignal.emit(name,res) + if onlyshowhist: return clear=name=='' @@ -90,15 +94,13 @@ class QUnFrameWindow(resizableframeless): _res=res[:globalconfig['maxoriginlength']]+'……' else: _res=res - + if globalconfig['showfanyisource']: _showtext=name+' '+_res else: _showtext=_res - self.showline(clear,[None,_showtext],color,1,False) + self.showline(clear=clear,text=_showtext,color=color,origin=False,iter_context=iter_context) - if len(iter_res_info) and starting: - self.saveiterclasspointer[klass]={'curr':self.translate_text.getcurrpointer()+len(_showtext),'start':self.translate_text.getcurrpointer()+len(_showtext)-len(res)} except: print_exc() def showraw(self,hira,res,color ,onlyshowhist): @@ -111,22 +113,30 @@ class QUnFrameWindow(resizableframeless): else: _res=res if globalconfig['isshowhira'] and globalconfig['isshowrawtext']: - self.showline(clear,[hira,_res],color , 2 ) + self.showline(clear=clear,text=_res,hira=hira,color=color) elif globalconfig['isshowrawtext']: - self.showline(clear,[hira,_res],color,1) + self.showline(clear=clear,text=_res,color=color) else: - self.showline(clear,None,None,1) + self.showline(clear=clear) gobject.baseobject.edittextui.getnewsentencesignal.emit(res) def showstatus(self,res,color,clear,origin): - self.showline(clear,[None,res],color,1,origin) - def showline (self,clear,res,color ,type_=1,origin=True): + self.showline(clear=clear,text=res,color=color,origin=origin) + def showline (self,**kwargs):# clear,res,color ,type_=1,origin=True): + clear=kwargs.get('clear',True) + origin=kwargs.get('origin',True) + text=kwargs.get('text',None) + color=kwargs.get('color',None) + hira=kwargs.get('hira',[]) + iter_context=kwargs.get('iter_context',None) + if clear: self.translate_text.clear() - self.translate_text.setnextfont(origin) - if res is None: + if text is None: return + self.translate_text.setnextfont(origin) + if globalconfig['showatcenter']: self.translate_text.setAlignment(Qt.AlignCenter) else: @@ -142,20 +152,33 @@ class QUnFrameWindow(resizableframeless): elif globalconfig['zitiyangshi'] ==0: self.translate_text.simplecharformat(color) elif globalconfig['zitiyangshi'] ==3: - self.translate_text.simplecharformat(color) - if type_==1: - self.translate_text.append (res[1],[],origin) - else: - self.translate_text.append (res[1],res[0],origin) - if globalconfig['zitiyangshi'] ==3: - self.translate_text.showyinyingtext(color ) - if (globalconfig['usesearchword'] or globalconfig['usecopyword'] or globalconfig['show_fenci'] ) and res[0]: + self.translate_text.simplecharformat(color) + + if iter_context: + iter_res_status,iter_context_class=iter_context + if iter_res_status==3: + self.translate_text.append(text,hira,origin) + self.saveiterclasspointer[iter_context_class]={'curr':self.translate_text.getcurrpointer()+len(text),'start':self.translate_text.getcurrpointer()} + else: + self.translate_text.deletebetween(self.saveiterclasspointer[iter_context_class]['start'],self.saveiterclasspointer[iter_context_class]['curr']) + self.translate_text.insertatpointer(self.saveiterclasspointer[iter_context_class]['start'],text) + self.saveiterclasspointer[iter_context_class]['curr']=self.translate_text.getcurrpointer() + + if globalconfig['zitiyangshi'] ==3: + self.translate_text.showyinyingtext2(color,iter_context_class,self.saveiterclasspointer[iter_context_class]['start'],text) + return + + else: + self.translate_text.append(text,hira,origin) + if globalconfig['zitiyangshi'] ==3: + self.translate_text.showyinyingtext(color) + if (globalconfig['usesearchword'] or globalconfig['usecopyword'] or globalconfig['show_fenci'] ) and hira: def callback(word): if globalconfig['usecopyword'] : winsharedutils.clipboard_set(word) if globalconfig['usesearchword']: gobject.baseobject.searchwordW.getnewsentencesignal.emit(word) - self.translate_text.addsearchwordmask(res[0],res[1],callback ) + self.translate_text.addsearchwordmask(hira,text,callback) if globalconfig['autodisappear']: @@ -165,7 +188,7 @@ class QUnFrameWindow(resizableframeless): if flag: self.show_() self.lastrefreshtime=time.time() - self.autohidestart=True + self.autohidestart=True def autohidedelaythread(self): while True: if globalconfig['autodisappear'] and self.autohidestart: @@ -313,7 +336,7 @@ class QUnFrameWindow(resizableframeless): windows.SetForegroundWindow(int(self.winId())) def showEvent(self, a0 ) -> None: if self.isfirstshow: - self.showline(True,[None,_TR('欢迎使用')],'',1,False) + self.showline(clear=True,text=_TR('欢迎使用'),origin=False) showAction = QAction(_TR("&显示"), self, triggered = self.show_and_enableautohide) diff --git a/LunaTranslator/LunaTranslator/translator/basetranslator.py b/LunaTranslator/LunaTranslator/translator/basetranslator.py index c09fb014..2270e15f 100644 --- a/LunaTranslator/LunaTranslator/translator/basetranslator.py +++ b/LunaTranslator/LunaTranslator/translator/basetranslator.py @@ -261,19 +261,26 @@ class basetrans(commonbase): collectiterres=[] def __callback(_,is_iter_res): if self.needzhconv: - _=zhconv.convert(_, 'zh-tw' ) - callback(_,embedcallback,is_iter_res) - collectiterres.append(_) + _=zhconv.convert(_, 'zh-tw' ) + if _=='\0':#清除前面的输出 + collectiterres.clear() + pass + else: + collectiterres.append(_) + callback(''.join(collectiterres),embedcallback,is_iter_res) + if isinstance(res,types.GeneratorType): def _iterget(): rid=self.requestid + __callback('',3) for _res in res: if self.requestid!=rid:break - __callback(_res,True) + __callback(_res,1) + __callback('',2) timeoutfunction(_iterget,checktutukufunction=checktutukufunction ) else: - __callback(res,False) + __callback(res,0) self.cachesetatend(contentsolved,''.join(collectiterres)) except Exception as e: if self.using and globalconfig['showtranexception']: