From 28d333a113d5f4af88ce60fa4bab47f2cf376bda Mon Sep 17 00:00:00 2001 From: yaoxuanzhi Date: Mon, 23 Dec 2024 21:58:51 +0800 Subject: [PATCH] =?UTF-8?q?-=20=E4=B8=BA=E6=95=B0=E5=AD=97=E6=A0=87?= =?UTF-8?q?=E8=AE=B0=E5=9B=BE=E5=85=83=E6=96=B0=E5=A2=9E=E5=8F=8C=E5=87=BB?= =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=AF=B9=E8=AF=9D=E6=A1=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/base/__init__.py | 1 + src/base/text_modify_dialog.py | 46 ++++++++++++++++++++ src/canvas_editor/canvas_scene.py | 6 +++ src/canvas_item/canvas_number_marker_item.py | 18 +++++++- src/extend_widgets/bubble_tip.py | 1 + 5 files changed, 71 insertions(+), 1 deletion(-) create mode 100644 src/base/text_modify_dialog.py diff --git a/src/base/__init__.py b/src/base/__init__.py index ad103ef..f2de78a 100644 --- a/src/base/__init__.py +++ b/src/base/__init__.py @@ -3,3 +3,4 @@ from .pin_window import * from .app_dpi_helper import * from .ring_color_selector_dialog import RingColorSelectorDialog +from .text_modify_dialog import TextModifyDialog diff --git a/src/base/text_modify_dialog.py b/src/base/text_modify_dialog.py new file mode 100644 index 0000000..794cf54 --- /dev/null +++ b/src/base/text_modify_dialog.py @@ -0,0 +1,46 @@ +# coding:utf-8 +from PyQt5.QtCore import * +from PyQt5.QtGui import * +from PyQt5.QtWidgets import * +from qfluentwidgets import * + +class LineEditPlus(LineEdit): + finalTextChanged = pyqtSignal(str) + def __init__(self, oldText: str, parent=None): + super().__init__(parent) + self.oldText = oldText + self.setText(oldText) + self.setPlaceholderText(oldText) + # self.setClearButtonEnabled(True) + self.setAlignment(Qt.AlignCenter) + self.setValidator(QIntValidator()) + + def keyPressEvent(self, a0: QKeyEvent) -> None: + if a0.key() == Qt.Key_Return or a0.key() == Qt.Key_Enter: + self.finalTextChanged.emit(self.text()) + super().keyPressEvent(a0) + +class TextModifyDialog(MaskDialogBase): + finalTextChanged = pyqtSignal(str) + + def __init__(self, oldText: str, parent=None): + super().__init__(parent) + self.lineEdit = LineEditPlus(oldText) + self.lineEdit.finalTextChanged.connect(self.finalTextChanged) + self.contentLayout = QVBoxLayout(self.widget) + self.__initWidget() + + def __initWidget(self): + self.setShadowEffect(60, (0, 10), QColor(0, 0, 0, 80)) + # self.setMaskColor(QColor(0, 0, 0, 2)) # 几乎透明 + self.setMaskColor(QColor(0, 0, 0, 30)) # 半透明 + self.__initLayout() + + def __initLayout(self): + self._hBoxLayout.setContentsMargins(0, 0, 0, 0) + self.contentLayout.setContentsMargins(0, 0, 0, 0) + self.contentLayout.addWidget(self.lineEdit, 0, Qt.AlignCenter) + + def showEvent(self, e): + self.lineEdit.setFocus() + return super().showEvent(e) diff --git a/src/canvas_editor/canvas_scene.py b/src/canvas_editor/canvas_scene.py index afa9ef0..acc6861 100644 --- a/src/canvas_editor/canvas_scene.py +++ b/src/canvas_editor/canvas_scene.py @@ -290,6 +290,12 @@ def mousePressEvent(self, event: QGraphicsSceneMouseEvent): self.currentItem.setPos(targetPos) self.__completeDraw(self.currentItem) elif self.currentDrawActionEnum == DrawActionEnum.UseNumberMarker: + # 如果当前位置下已经存在一个数字标记图元,则不再创建新的 + for item0 in self.itemList: + if isinstance(item0, CanvasNumberMarkerItem): + if item0.sceneBoundingRect().contains(event.scenePos()): + return super().mousePressEvent(event) + self.currentItem = CanvasNumberMarkerItem(QRectF(0, 0, 20, 20)) self.__startDraw(self.currentItem) targetPos.setX( diff --git a/src/canvas_item/canvas_number_marker_item.py b/src/canvas_item/canvas_number_marker_item.py index 5236ce1..e9f2693 100644 --- a/src/canvas_item/canvas_number_marker_item.py +++ b/src/canvas_item/canvas_number_marker_item.py @@ -1,6 +1,6 @@ # coding=utf-8 from .canvas_util import * - +from base import * class CanvasNumberMarkerItem(QGraphicsRectItem): """ @@ -31,6 +31,22 @@ def applyShadow(self): self.shadowEffect.setOffset(5 * self.devicePixelRatio, 5 * self.devicePixelRatio) # 阴影的偏移量 self.setGraphicsEffect(self.shadowEffect) + def mouseDoubleClickEvent(self, event: QGraphicsSceneMouseEvent): + if(event.button() == Qt.MouseButton.LeftButton): + self.textDialog = TextModifyDialog(f"{self.index}", self.scene().views()[0]) + self.textDialog.finalTextChanged.connect(self.__finalTextChanged) + self.textDialog.exec() + return + return super().mouseDoubleClickEvent(event) + + def __finalTextChanged(self, text: str): + try: + self.textDialog.close() + self.index = int(text) + self.update() + except Exception as e: + pass + def removeShadow(self): self.setGraphicsEffect(None) delattr(self, "shadowEffect") diff --git a/src/extend_widgets/bubble_tip.py b/src/extend_widgets/bubble_tip.py index 35a2f68..dd2e1d7 100644 --- a/src/extend_widgets/bubble_tip.py +++ b/src/extend_widgets/bubble_tip.py @@ -251,6 +251,7 @@ def make(position: BubbleTipTailPosition): BubbleTipTailPosition.AUTO: AutoTailBubbleTipManager, BubbleTipTailPosition.TOP_LEFT_AUTO: TopLeftAutoTailBubbleTipManager, BubbleTipTailPosition.BOTTOM_LEFT_AUTO: BottomLeftAutoTailBubbleTipManager, + BubbleTipTailPosition.BOTTOM: BottomTailBubbleTipManager, } if position not in managers: