Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
HIllya51 committed Dec 1, 2024
1 parent b6d6058 commit 4cbe764
Showing 1 changed file with 99 additions and 19 deletions.
118 changes: 99 additions & 19 deletions py/LunaTranslator/rendertext/textbrowser.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from PyQt5.QtGui import QMouseEvent
from qtsymbols import *
from myutils.config import globalconfig, static_data
from rendertext.somefunctions import dataget
Expand Down Expand Up @@ -52,6 +53,64 @@ def leaveEvent(self, a0) -> None:
return super().leaveEvent(a0)


class QTextBrowser_1(QTextBrowser):
def __init__(self, parent) -> None:
super().__init__(parent)
self.pr = None
self.prpos = QPoint()
self.selectionChanged.connect(self.selectcg)

def selectcg(self):
self.pr = self.textCursor().selectedText()

def getcurrlabel(self, ev: QMouseEvent):
for label in self.parent().searchmasklabels:
if not label.geometry().contains(ev.pos()):
continue
return label

def mousePressEvent(self, ev: QMouseEvent):
self.prpos = ev.pos()
return super().mousePressEvent(ev)

def mouseReleaseEvent(self, event: QMouseEvent) -> None:
try:
label = self.getcurrlabel(event)
if label and label.refmask.callback:
if event.button() == Qt.MouseButton.LeftButton:
if self.prpos == event.pos() or not self.pr:
label.refmask.callback(False)
elif event.button() == Qt.MouseButton.RightButton:
if not self.pr:
label.refmask.callback(True)
except:
pass
return super().mouseReleaseEvent(event)

def mouseMoveEvent(self, ev: QMouseEvent):
for label in self.parent().searchmasklabels:
if label.geometry().contains(ev.pos()):
continue
try:
label.refmask.setStyleSheet("background-color: rgba(0,0,0,0.01);")
label.company.refmask.setStyleSheet(
"background-color: rgba(0,0,0,0.01);"
)
except:
pass
targetlabel = self.getcurrlabel(ev)
if targetlabel:
try:
targetlabel.refmask.setStyleSheet("background-color: rgba(0,0,0,0.5);")
targetlabel.company.refmask.setStyleSheet(
"background-color: rgba(0,0,0,0.5);"
)
except:
pass

return super().mouseMoveEvent(ev)


class TextBrowser(QWidget, dataget):
contentsChanged = pyqtSignal(QSize)
dropfilecallback = pyqtSignal(str)
Expand Down Expand Up @@ -125,6 +184,23 @@ def showmenu(self, p):

def __init__(self, parent) -> None:
super().__init__(parent)

self.savetaglabels = []
self.searchmasklabels_clicked = []
self.searchmasklabels_clicked2 = []
self.searchmasklabels = []
self.backcolorlabels = []

self.yinyinglabels = []
self.yinyinglabels_idx = 0

self.yinyingposline = 0
self.lastcolor = None
self.iteryinyinglabelsave = {}
self.saveiterclasspointer = {}
self.extra_height = 0
self.cleared = True

self.setAcceptDrops(True)
self.atback_color = QLabel(self)
self.atback_color.setMouseTracking(True)
Expand All @@ -133,7 +209,7 @@ def __init__(self, parent) -> None:

self.toplabel2 = QLabel(self)
self.toplabel2.setMouseTracking(True)
self.textbrowser = QTextBrowser(self)
self.textbrowser = QTextBrowser_1(self)
self.textbrowser.setContextMenuPolicy(Qt.ContextMenuPolicy.CustomContextMenu)
self.textbrowser.customContextMenuRequested.connect(self.showmenu)

Expand Down Expand Up @@ -168,20 +244,6 @@ def __init__(self, parent) -> None:
self.masklabel_top = QLabel(self)
self.masklabel_top.setMouseTracking(True)
# self.masklabel_bottom.setStyleSheet('background-color:red')
self.savetaglabels = []
self.searchmasklabels_clicked = []
self.searchmasklabels = []
self.backcolorlabels = []

self.yinyinglabels = []
self.yinyinglabels_idx = 0

self.yinyingposline = 0
self.lastcolor = None
self.iteryinyinglabelsave = {}
self.saveiterclasspointer = {}
self.extra_height = 0
self.cleared = True
self.resets1()

def resets1(self):
Expand All @@ -202,6 +264,8 @@ def resets(self):
label.hide()
for label in self.searchmasklabels_clicked:
label.hide()
for label in self.searchmasklabels_clicked2:
label.hide()
for label in self.savetaglabels:
label.hide()

Expand Down Expand Up @@ -652,17 +716,26 @@ def _add_searchlabel(self, isfenciclick, isshow_fenci, labeli, pos1, word, color
ql = QLabel(self.atback_color)
ql.setMouseTracking(True)
self.searchmasklabels.append(ql)

ql = Qlabel_c(self.textbrowser)
ql_1 = QLabel(ql)
ql_1.setMouseTracking(True)
ql.refmask = ql_1
ql_1.setStyleSheet("background-color: rgba(0,0,0,0.01);")
self.searchmasklabels_clicked.append(ql_1)
ql = Qlabel_c(self.masklabel)
ql.setMouseTracking(True)
ql.setStyleSheet("background-color: rgba(0,0,0,0.01);")
self.searchmasklabels_clicked.append(ql)
self.searchmasklabels_clicked2.append(ql)
if isfenciclick:
self.searchmasklabels_clicked[labeli].setGeometry(*pos1)
self.searchmasklabels_clicked[labeli].setGeometry(0, 0, pos1[2], pos1[3])
self.searchmasklabels_clicked[labeli].show()
self.searchmasklabels_clicked[labeli].callback = functools.partial(
gobject.baseobject.clickwordcallback, word
)
self.searchmasklabels_clicked2[labeli].setGeometry(*pos1)
self.searchmasklabels_clicked2[labeli].show()
self.searchmasklabels_clicked2[labeli].callback = functools.partial(
gobject.baseobject.clickwordcallback, word
)
if isshow_fenci and color:
self.searchmasklabels[labeli].setGeometry(*pos1)
self.searchmasklabels[labeli].setStyleSheet(
Expand Down Expand Up @@ -702,8 +775,15 @@ def addsearchwordmask(self, isshow_fenci, isfenciclick, x):
self.searchmasklabels_clicked[labeli].company = (
self.searchmasklabels_clicked[labeli - 1]
)
self.searchmasklabels_clicked2[labeli - 1].company = (
self.searchmasklabels_clicked2[labeli]
)
self.searchmasklabels_clicked2[labeli].company = (
self.searchmasklabels_clicked2[labeli - 1]
)
else:
self.searchmasklabels_clicked[labeli].company = None
self.searchmasklabels_clicked2[labeli].company = None
labeli += 1

def _getfh(self, half, origin=True, getfm=False):
Expand Down

0 comments on commit 4cbe764

Please sign in to comment.