From 60b64948f94d454f4463fbf0aef0585fbcb5d1b9 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, 22 May 2024 15:08:46 +0800 Subject: [PATCH] opti --- .../LunaTranslator/cishu/cishubase.py | 4 ++-- LunaTranslator/LunaTranslator/cishu/edict.py | 3 ++- LunaTranslator/LunaTranslator/cishu/edict2.py | 5 +++-- .../LunaTranslator/cishu/linggesi.py | 3 ++- .../LunaTranslator/cishu/xiaoxueguan.py | 3 ++- LunaTranslator/LunaTranslator/gui/showword.py | 13 ++++++++---- .../LunaTranslator/gui/usefulwidget.py | 21 +++++++++++++++---- 7 files changed, 37 insertions(+), 15 deletions(-) diff --git a/LunaTranslator/LunaTranslator/cishu/cishubase.py b/LunaTranslator/LunaTranslator/cishu/cishubase.py index 44a27b2b..83ba3821 100644 --- a/LunaTranslator/LunaTranslator/cishu/cishubase.py +++ b/LunaTranslator/LunaTranslator/cishu/cishubase.py @@ -20,7 +20,7 @@ class cishubase: pass @threader - def safesearch(self, sentence): + def safesearch(self, sentence, callback): try: if self.needinit: self.init() @@ -32,7 +32,7 @@ class cishubase: self.needinit = True if res and len(res): - self.callback(res) + callback(res) except: pass diff --git a/LunaTranslator/LunaTranslator/cishu/edict.py b/LunaTranslator/LunaTranslator/cishu/edict.py index c6d98763..bdd856d1 100644 --- a/LunaTranslator/LunaTranslator/cishu/edict.py +++ b/LunaTranslator/LunaTranslator/cishu/edict.py @@ -15,7 +15,8 @@ class edict(cishubase): pass def search(self, word): - + if not self.sql: + return x = self.sql.execute( "select text, entry_id from surface where text like ?", ("%{}%".format(word),), diff --git a/LunaTranslator/LunaTranslator/cishu/edict2.py b/LunaTranslator/LunaTranslator/cishu/edict2.py index 73d2e2e0..27734f9e 100644 --- a/LunaTranslator/LunaTranslator/cishu/edict2.py +++ b/LunaTranslator/LunaTranslator/cishu/edict2.py @@ -7,7 +7,7 @@ from cishu.cishubase import cishubase class edict2(cishubase): def init(self): - self.sql = None + self.save = None try: path = self.config["path"] if os.path.exists(path): @@ -25,7 +25,8 @@ class edict2(cishubase): print_exc() def search(self, word): - + if not self.save: + return dis = 9999 dis = [] savew = [] diff --git a/LunaTranslator/LunaTranslator/cishu/linggesi.py b/LunaTranslator/LunaTranslator/cishu/linggesi.py index 0cd23edd..0c032497 100644 --- a/LunaTranslator/LunaTranslator/cishu/linggesi.py +++ b/LunaTranslator/LunaTranslator/cishu/linggesi.py @@ -29,7 +29,8 @@ class linggesi(cishubase): pass def search(self, word): - + if not self.sql: + return mp = {} for sql in [self.sql, self.sql2]: x = sql.execute( diff --git a/LunaTranslator/LunaTranslator/cishu/xiaoxueguan.py b/LunaTranslator/LunaTranslator/cishu/xiaoxueguan.py index 812e1958..106e9f96 100644 --- a/LunaTranslator/LunaTranslator/cishu/xiaoxueguan.py +++ b/LunaTranslator/LunaTranslator/cishu/xiaoxueguan.py @@ -16,7 +16,8 @@ class xiaoxueguan(cishubase): pass def search(self, word): - + if not self.sql: + return x = self.sql.execute( "select word,explanation from xiaoxueguanrizhong where word like ?", ("%{}%".format(word),), diff --git a/LunaTranslator/LunaTranslator/gui/showword.py b/LunaTranslator/LunaTranslator/gui/showword.py index 09f72f13..33e3b332 100644 --- a/LunaTranslator/LunaTranslator/gui/showword.py +++ b/LunaTranslator/LunaTranslator/gui/showword.py @@ -735,7 +735,7 @@ class selectviewer(QWidget): class searchwordW(closeashidewindow): getnewsentencesignal = pyqtSignal(str, bool) - showtabsignal = pyqtSignal(str, str) + showtabsignal = pyqtSignal(float, str, str) def __init__(self, parent): super(searchwordW, self).__init__(parent, globalconfig, "sw_geo") @@ -745,7 +745,9 @@ class searchwordW(closeashidewindow): self.getnewsentencesignal.connect(self.getnewsentence) self.setWindowTitle(_TR("查词")) - def showresfun(self, k, res): + def showresfun(self, timestamp, k, res): + if self.current != timestamp: + return self.cache_results[k] = res thisp = globalconfig["cishu"][k]["args"]["priority"] @@ -853,6 +855,8 @@ class searchwordW(closeashidewindow): grabwindow(self.ankiwindow.editpath.setText) def search(self, sentence): + current = time.time() + self.current = current sentence = sentence.strip() if sentence == "": return @@ -863,5 +867,6 @@ class searchwordW(closeashidewindow): self.textOutput.clear() self.cache_results.clear() for k, cishu in gobject.baseobject.cishus.items(): - cishu.callback = functools.partial(self.showtabsignal.emit, k) - cishu.safesearch(sentence) + cishu.safesearch( + sentence, functools.partial(self.showtabsignal.emit, current, k) + ) diff --git a/LunaTranslator/LunaTranslator/gui/usefulwidget.py b/LunaTranslator/LunaTranslator/gui/usefulwidget.py index 6f923952..30ea64b9 100644 --- a/LunaTranslator/LunaTranslator/gui/usefulwidget.py +++ b/LunaTranslator/LunaTranslator/gui/usefulwidget.py @@ -627,15 +627,27 @@ class auto_select_webview(QWidget): def navigate(self, url): self._maybecreate() + self.clearcache() self.internal.navigate(url) def setHtml(self, html): self._maybecreate() - self.internal.setHtml(html) + self.clearcache() + if len(html) < 1 * 1024 * 1024: # 1mb + self.internal.setHtml(html) + else: + os.makedirs("cache/temp", exist_ok=True) + self.lastcachehtml = os.path.abspath( + "cache/temp/" + str(time.time()) + ".html" + ) + with open(self.lastcachehtml, "w", encoding="utf8") as ff: + ff.write(html) + print("file://" + self.lastcachehtml.replace("\\", "/")) + self.internal.navigate("file://" + self.lastcachehtml.replace("\\", "/")) - def navigate(self, url): - self._maybecreate() - self.internal.navigate(url) + def clearcache(self): + if self.lastcachehtml and os.path.exists(self.lastcachehtml): + os.remove(self.lastcachehtml) def __init__(self, parent) -> None: super().__init__(parent) @@ -647,6 +659,7 @@ class auto_select_webview(QWidget): layout.setContentsMargins(0, 0, 0, 0) self.setLayout(layout) self._maybecreate() + self.lastcachehtml = None def _maybecreate(self): if globalconfig["usewebview"] != self.contex: