Mostly working!
This commit is contained in:
@@ -16,10 +16,16 @@ from PyQt6.QtGui import (
|
||||
QTextLayout,
|
||||
QTextOption,
|
||||
)
|
||||
from lib.sounds import SoundOff
|
||||
from PyQt6.QtWidgets import QWidget
|
||||
from trycast import trycast
|
||||
|
||||
class MyPointF(QPointF):
|
||||
def __repr__(self):
|
||||
return f"({self.x()}, {self.y()})"
|
||||
|
||||
def __str__(self):
|
||||
return self.__repr__()
|
||||
|
||||
class Fragment:
|
||||
"""A fragment of text to be displayed"""
|
||||
|
||||
@@ -117,12 +123,17 @@ class Fragment:
|
||||
painter.brush().setColor(Qt.GlobalColor.green)
|
||||
for fmt in self._layout.formats():
|
||||
if fmt.format.isAnchor():
|
||||
#text = self._layout.text()[fmt.start:fmt.start+fmt.length]
|
||||
runs = self._layout.glyphRuns(fmt.start, fmt.length)
|
||||
bb = runs[0].boundingRect()
|
||||
bb.moveTo(bb.topLeft() + self._layout.position())
|
||||
painter.drawRect(bb)
|
||||
#print(f"({bb.left()}-{bb.right()}, {bb.top()}-{bb.bottom()}): {text}")
|
||||
url = QUrl(fmt.format.anchorHref())
|
||||
if url.scheme() == 'audio':
|
||||
painter.setPen(QColor('red'))
|
||||
radius = (bb.topLeft() - bb.bottomLeft()).manhattanLength()/4
|
||||
painter.drawRoundedRect(bb, radius,radius)
|
||||
else:
|
||||
painter.setPen(QColor("blue"))
|
||||
#painter.drawRect(bb)
|
||||
|
||||
painter.restore()
|
||||
return QSize(int(size.width()), int(size.height()))
|
||||
@@ -434,12 +445,16 @@ class Clickable(TypedDict):
|
||||
fmt: QTextCharFormat
|
||||
|
||||
class Definition(QWidget):
|
||||
pronounce = pyqtSignal(str)
|
||||
|
||||
pronounce = pyqtSignal(QUrl)
|
||||
alert = pyqtSignal()
|
||||
newWord = pyqtSignal(str)
|
||||
def __init__(
|
||||
self, word: Optional[Any] = None, *args: Any, **kwargs: Any
|
||||
) -> None:
|
||||
super(Definition, self).__init__(*args, **kwargs)
|
||||
self._sound = SoundOff()
|
||||
self.pronounce.connect(self._sound.playSound)
|
||||
self.alert.connect(self._sound.alert)
|
||||
self._word = word
|
||||
if word is not None:
|
||||
self.setWord(word)
|
||||
@@ -450,7 +465,6 @@ class Definition(QWidget):
|
||||
lines: list[Line] = word.get_def()
|
||||
assert lines is not None
|
||||
self._lines = lines
|
||||
self._buttons: list[Clickable] = []
|
||||
base = 0
|
||||
|
||||
for line in self._lines:
|
||||
@@ -474,12 +488,23 @@ class Definition(QWidget):
|
||||
def mousePressEvent(self, event: Optional[QMouseEvent]) -> None:
|
||||
if not event:
|
||||
return super().mousePressEvent(event)
|
||||
print(f"mousePressEvent: {event.position()}")
|
||||
for clk in self._buttons:
|
||||
if clk["bb"].contains(event.position()):
|
||||
print("inside")
|
||||
self._downClickable = clk
|
||||
return
|
||||
position = MyPointF(event.position())
|
||||
print(f"mousePressEvent: {position}")
|
||||
for line in self._lines:
|
||||
for frag in line.getLine():
|
||||
layout = frag.layout()
|
||||
for fmtRng in layout.formats():
|
||||
if fmtRng.format.isAnchor():
|
||||
runs = layout.glyphRuns(fmtRng.start, fmtRng.length)
|
||||
assert len(runs) == 1
|
||||
bb = runs[0].boundingRect()
|
||||
bb.moveTo(bb.topLeft() + layout.position())
|
||||
if bb.contains(event.position()):
|
||||
self._downClickable = {
|
||||
'bb': bb,
|
||||
'fmt': fmtRng.format,
|
||||
'frag': frag,
|
||||
}
|
||||
return super().mousePressEvent(event)
|
||||
|
||||
def mouseReleaseEvent(self, event: Optional[QMouseEvent]) -> None:
|
||||
@@ -490,9 +515,15 @@ class Definition(QWidget):
|
||||
):
|
||||
print(f"mousePressPseudoEvent: {event.position()}")
|
||||
clk = self._downClickable
|
||||
bb = clk['bb']
|
||||
print(f"({bb.left()}-{bb.right()}, {bb.top()}-{bb.bottom()})", clk["fmt"].anchorHref(),)
|
||||
#self.pronounce.emit(audio)
|
||||
url = QUrl(clk['fmt'].anchorHref())
|
||||
if url.scheme() == 'audio':
|
||||
url.setScheme('https')
|
||||
self.pronounce.emit(url)
|
||||
elif url.scheme() == 'word':
|
||||
self.newWord.emit(url.path())
|
||||
else:
|
||||
print(f"{clk['fmt'].anchorHref()}: {url.scheme()}")
|
||||
self.alert.emit()
|
||||
self._downClickable = None
|
||||
return
|
||||
self._downClickable = None
|
||||
@@ -510,29 +541,7 @@ class Definition(QWidget):
|
||||
#
|
||||
# All text on this line needs to be on the same baseline
|
||||
#
|
||||
buildButtons = (len(self._buttons) < 1)
|
||||
|
||||
assert self._lines is not None
|
||||
for line in self._lines:
|
||||
line.paintEvent(painter)
|
||||
if not buildButtons:
|
||||
continue
|
||||
for frag in line.getLine():
|
||||
for fmtRng in frag.layout().formats():
|
||||
if fmtRng.format.isAnchor():
|
||||
runs = frag.layout().glyphRuns(fmtRng.start,fmtRng.start+fmtRng.length)
|
||||
run = runs[0]
|
||||
bb = run.boundingRect()
|
||||
bb.moveTo(bb.topLeft() + frag.layout().position())
|
||||
self._buttons.append(
|
||||
{
|
||||
'bb': bb,
|
||||
'frag': frag,
|
||||
'fmt': fmtRng.format,
|
||||
}
|
||||
)
|
||||
painter.setPen(QColor("cyan"))
|
||||
for click in self._buttons:
|
||||
if click['fmt'].isAnchor():
|
||||
painter.drawRect(click['bb'])
|
||||
return
|
||||
|
||||
Reference in New Issue
Block a user