Skip to content

Commit

Permalink
fix: wayland剪贴板
Browse files Browse the repository at this point in the history
  • Loading branch information
yuhldr committed Nov 23, 2024
1 parent cc4d5d1 commit dad9068
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 19 deletions.
5 changes: 2 additions & 3 deletions lfy/qt/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ def keyPressEvent(self, event):
event (_type_): _description_
"""
if event.modifiers() == Qt.KeyboardModifier.ControlModifier and event.key() == Qt.Key.Key_C:
print("no copy")
event.accept() # 阻止事件传播,防止触发 QClipboard 的复制
# 阻止事件传播,防止触发 QClipboard 的复制
event.accept()
else:
super().keyPressEvent(event)

Expand All @@ -38,5 +38,4 @@ def run(self):
span = 0.5 - (time.time() - start_)
if span > 0:
time.sleep(span)
print(result)
self.signal.emit(result)
4 changes: 2 additions & 2 deletions lfy/qt/lfy.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
PYTHON_DIR = '@PYTHON_DIR@'

LOCALE_DIR = '@LOCALE_DIR@'
PKGDATA_DIR = '@PKGDATA_DIR@'

if not os.path.exists(PKGDATA_DIR):

if not os.path.exists(SCHEMAS_DIR):
# 说明是pip这一类的
print(f"路径修正:{SCHEMAS_DIR}")
THIS_DIR, THIS_FILENAME = os.path.split(__file__)
Expand Down
18 changes: 4 additions & 14 deletions lfy/qt/translate.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
lang_n2j, server_key2i)
from lfy.api.server import Server
from lfy.qt import MyPlainTextEdit, MyThread
from lfy.utils import cal_md5, process_text
from lfy.utils import process_text
from lfy.utils.settings import Settings


Expand Down Expand Up @@ -90,7 +90,6 @@ def __init__(self):
self.my_thread = None
self.tray: QSystemTrayIcon = None
self.text_last = ""
self.img_md5 = ""

self.set_data()

Expand Down Expand Up @@ -162,13 +161,6 @@ def ocr_image(self, img_path):
Returns:
_type_: _description_
"""
md5_hash = cal_md5(img_path)
print(md5_hash)
# 防止wayland多次识别
if self.img_md5 == md5_hash:
return
self.img_md5 = md5_hash

def oo(_p=None):
_ok, text_from = self.server_o.ocr_image(img_path)
return (_ok, text_from)
Expand Down Expand Up @@ -201,12 +193,12 @@ def set_text_from_to(self, text):
self.text_last = text_from
self.cleanup_thread()

def update_translate(self, reload=True):
def update_translate(self):
"""无参数翻译
"""
self.translate_text(self.te_from.toPlainText(), reload)
self.translate_text(self.te_from.toPlainText())

def translate_text(self, text_from, reload=False):
def translate_text(self, text_from):
"""翻译
Returns:
Expand All @@ -215,8 +207,6 @@ def translate_text(self, text_from, reload=False):
if self.cb_del_wrapping.isChecked():
text_from = process_text(text_from)

if text_from == self.text_last and not reload:
return

if self.cb_add_old.isChecked():
text_from = self.text_last + text_from
Expand Down
15 changes: 15 additions & 0 deletions lfy/qt/tray.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from lfy import APP_NAME
from lfy.qt.preference import PreferenceWindow
from lfy.qt.translate import TranslateWindow
from lfy.utils import cal_md5


class TrayIcon(QSystemTrayIcon):
Expand Down Expand Up @@ -51,18 +52,32 @@ def __init__(
self.clipboard: QClipboard = self.app.clipboard()
self.clipboard.dataChanged.connect(self._on_clipboard_data_changed)

self.img_md5 = ""
self.text_last = ""

def _on_clipboard_data_changed(self):

if self.clipboard.mimeData().hasImage():
# 如果是图片,处理图片
image = self.clipboard.image()
if not image.isNull():
file_path = "/tmp/lfy.png"
image.save(file_path, "PNG")

md5_hash = cal_md5(file_path)
if self.img_md5 == md5_hash:
return
self.img_md5 = md5_hash

if not self.w.isVisible():
self.w.show()
self.w.ocr_image(file_path)
elif self.clipboard.mimeData().hasText():
text = self.clipboard.text()
if text == self.text_last:
return
self.text_last = text

if not self.w.isVisible():
self.w.show()
self.w.translate_text(text)
Expand Down

0 comments on commit dad9068

Please sign in to comment.