From b3ccaa5fc487a497566d7694239a5784d2230e69 Mon Sep 17 00:00:00 2001 From: "Christopher T. Johnson" Date: Sun, 14 Apr 2024 18:14:04 -0400 Subject: [PATCH] Impliment self replication/copy --- lib/definition.py | 22 ++++++++++++++-------- plugins/merriam-webster.py | 7 +++---- 2 files changed, 17 insertions(+), 12 deletions(-) diff --git a/lib/definition.py b/lib/definition.py index d448d10..a642883 100644 --- a/lib/definition.py +++ b/lib/definition.py @@ -1,5 +1,5 @@ import re -from typing import Any, Optional, cast, overload +from typing import Any, Optional, Self, cast, overload import re from PyQt6.QtCore import QMargins, QPoint, QRect, QSize, QUrl, Qt, pyqtSignal from PyQt6.QtGui import QColor, QFont, QFontMetrics, QMouseEvent, QPaintEvent, QPainter, QResizeEvent, QTextOption, QTransform, QBrush @@ -9,14 +9,20 @@ class Fragment: """A fragment of text to be displayed""" def __init__( - self, - text: str, - font: QFont, - audio: str = "", - color: Optional[QColor] = None, - asis: bool = False, + self, + which: str|Self, + font: QFont|None = None, + audio: str = "", + color: Optional[QColor] = None, + asis: bool = False, ) -> None: - self._text = text + if isinstance(which, Fragment): + for k,v in which.__dict__.items(): + self.__dict__[k] = v + return + self._text:str = which + if font is None: + raise TypeError("Missing required parameter 'font'") self._font = font self._audio: QUrl = QUrl(audio) self._align = QTextOption( diff --git a/plugins/merriam-webster.py b/plugins/merriam-webster.py index 60fbf51..3c15a03 100644 --- a/plugins/merriam-webster.py +++ b/plugins/merriam-webster.py @@ -1,4 +1,3 @@ -import copy from importlib.abc import InspectLoader from PyQt6.QtGui import QColor, QFont from trycast import trycast @@ -601,7 +600,7 @@ def parseText(frag: Fragment) -> list[Fragment]: results.append(frag) return results if start > 0: - newFrag = copy.copy(frag) + newFrag = Fragment(frag) newFrag.setText(text[:start]) results.append(newFrag) frag.setText(text[start:]) @@ -623,7 +622,7 @@ def parseText(frag: Fragment) -> list[Fragment]: end = text.find("}") token = text[1:end] frag.setText(text[end + 1 :]) - newFrag = copy.copy(frag) + newFrag = Fragment(frag) oldFont = QFont(frag.font()) if token == "bc": results.append(Fragment(": ", boldFont, color=baseColor)) @@ -729,7 +728,7 @@ def parseText(frag: Fragment) -> list[Fragment]: wref = fields[1] else: raise NotImplementedError(f"Unknown code: {token} in {org}") - newFrag = copy.copy(frag) + newFrag = Fragment(frag) newFrag.setText(htext) newFrag.setWRef(wref) newFrag.setTarget(target)