Commit 59f283a8 authored by dmMaze's avatar dmMaze
Browse files

change focus policy of textitem

parent 8f6b8817
Loading
Loading
Loading
Loading
+8 −1
Original line number Diff line number Diff line
@@ -151,6 +151,7 @@ class Canvas(QGraphicsScene):
        self.addItem(self.txtblkShapeControl)

        self.scalefactor_changed.connect(self.onScaleFactorChanged)
        self.selectionChanged.connect(self.on_selection_changed)     
        self.stroke_path_item: StrokeItem = None

    def scaleUp(self):
@@ -226,6 +227,12 @@ class Canvas(QGraphicsScene):
        self.scaleFactorLabel.raise_()
        self.scaleFactorLabel.startFadeAnimation()

    def on_selection_changed(self):
        if self.txtblkShapeControl.isVisible():
            blk_item = self.txtblkShapeControl.blk_item
            if blk_item is not None and blk_item.isEditing():
                blk_item.endEdit()

    def keyPressEvent(self, event: QKeyEvent) -> None:
        if self.editing_textblkitem is not None:
            return super().keyPressEvent(event)
+1 −13
Original line number Diff line number Diff line
@@ -408,14 +408,6 @@ class FontFormatPanel(Widget):
    def restoreTextBlkItem(self):
        if self.active_format == self.global_format:
            self.global_format_changed.emit()
        else:
            blkitem = self.textblk_item
            self.restoring_textblk = True
            if blkitem:
                blkitem.startEdit()
                blkitem.setTextCursor(self.text_cursor)
                blkitem.scene().gv.setFocus(Qt.FocusReason.NoFocusReason)
            self.restoring_textblk = False

    def changingColor(self):
        self.focusOnColorDialog = True
@@ -530,17 +522,13 @@ class FontFormatPanel(Widget):
            elif focus_p:
                if focus_p == self or focus_p.parentWidget() == self:
                    focus_on_fmtoptions = True
            if focus_on_fmtoptions:
                self.text_cursor = QTextCursor(self.textblk_item.textCursor())
            else:
            if not focus_on_fmtoptions:
                self.textblk_item = None
                self.text_cursor = None
                self.set_active_format(self.global_format)
                self.fontfmtLabel.setText(self.global_fontfmt_str)
        else:
            if not self.restoring_textblk:
                blk_fmt = textblk_item.get_fontformat()
                self.textblk_item = textblk_item
                self.text_cursor = QTextCursor(self.textblk_item.textCursor())
                self.set_active_format(blk_fmt)
                self.fontfmtLabel.setText(f'TextBlock #{textblk_item.idx}')
 No newline at end of file
+4 −1
Original line number Diff line number Diff line
@@ -220,7 +220,8 @@ class TextBlkShapeControl(QGraphicsRectItem):
            return
        if self.blk_item is not None:
            self.blk_item.under_ctrl = False
            self.blk_item.setFocus(Qt.FocusReason.NoFocusReason)
            if self.blk_item.isEditing():
                self.blk_item.endEdit()
            self.blk_item.update()
            
        self.blk_item = blk_item
@@ -271,6 +272,8 @@ class TextBlkShapeControl(QGraphicsRectItem):

    def ctrlblockPressed(self):
        self.scene().clearSelection()
        if self.blk_item is not None:
            self.blk_item.endEdit()

    def paint(self, painter: QPainter, option: 'QStyleOptionGraphicsItem', widget = ...) -> None:
        painter.setCompositionMode(QPainter.RasterOp_NotDestination)
+13 −12
Original line number Diff line number Diff line
@@ -264,16 +264,6 @@ class TextBlkItem(QGraphicsTextItem):
                pos.setY(pos.y() + delta_x * np.sin(rad))
            self.setPos(pos)


    def focusOutEvent(self, event: QFocusEvent) -> None:
        self.end_edit.emit(self.idx)
        cursor = self.textCursor()
        cursor.clearSelection()
        self.setTextCursor(cursor)
        self.setTextInteractionFlags(Qt.NoTextInteraction)
        super().focusOutEvent(event)
        self.setCacheMode(QGraphicsItem.CacheMode.DeviceCoordinateCache)

    def documentLayout(self) -> VerticalTextDocumentLayout:
        return self.document().documentLayout()

@@ -357,13 +347,24 @@ class TextBlkItem(QGraphicsTextItem):
        option.state = QStyle.State_None
        super().paint(painter, option, widget)

    def startEdit(self):
    def startEdit(self) -> None:
        self.setCacheMode(QGraphicsItem.CacheMode.NoCache)

        self.setTextInteractionFlags(Qt.TextEditorInteraction)
        self.setFocus()
        self.begin_edit.emit(self.idx)

    def endEdit(self) -> None:
        self.end_edit.emit(self.idx)
        cursor = self.textCursor()
        cursor.clearSelection()
        self.setTextCursor(cursor)
        self.setTextInteractionFlags(Qt.NoTextInteraction)
        self.setCacheMode(QGraphicsItem.CacheMode.DeviceCoordinateCache)
        self.setFocus()

    def isEditing(self) -> bool:
        return self.textInteractionFlags() == Qt.TextEditorInteraction
    
    def mouseDoubleClickEvent(self, event: QGraphicsSceneMouseEvent) -> None:    
        self.startEdit()
        super().mouseDoubleClickEvent(event)