Commit b3efb008 authored by dmMaze's avatar dmMaze
Browse files

support draw rectangle

parent 5c871a9c
Loading
Loading
Loading
Loading
+0 −1
Original line number Diff line number Diff line
<?xml version="1.0" standalone="no"?><!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"><svg t="1620959014259" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="1383" xmlns:xlink="http://www.w3.org/1999/xlink" width="200" height="200"><defs><style type="text/css"></style></defs><path d="M832.2 127.9v766H192.7v-766h639.5m0-64H192.7c-35.3 0-64 28.7-64 64v766c0 35.3 28.7 64 64 64h639.5c35.3 0 64-28.7 64-64v-766c0-35.4-28.7-64-64-64z" fill="#697187" p-id="1384"></path><path d="M733.039 824.168v-32h97.9v32z" fill="#697187" p-id="1385"></path><path d="M896.2 958.3H425.4v-176c0-35.3 28.7-64 64-64h406.8c35.3 0 64 28.7 64 64v112c0 35.4-28.7 64-64 64z" fill="#697187" p-id="1386"></path><path d="M567 771c34.4 0 51.8 14.6 51.8 43.8 0 29.4-17.4 44.2-52.2 44.2h-35.4v54.8h-23.4V771H567z m-35.8 68h34c10.4 0 18-2 22.8-5.6 4.8-3.8 7.2-10 7.2-18.6 0-8.6-2.6-14.6-7.4-18.2-4.8-3.8-12.4-5.6-22.6-5.6h-34v48zM728.6 778.6c10 7.4 15.8 19 17.2 34.6h-23.2c-2-8.8-5.8-15.2-11-19-5.2-4-13.2-5.8-23.6-5.8-9 0-15.8 1.2-20.6 3.8-6 3-8.8 8-8.8 14.8 0 6 3.2 11 10 14.6 3 1.6 11.2 4.6 24.4 8.8 19.4 6 31.8 10.8 37.4 14 12.4 7.4 18.6 17.6 18.6 30.8 0 12.8-5 23-15 30.4-10 7.2-24.2 11-42.4 11-17.6 0-31.4-3.4-41.2-10.2-12-8.4-18.6-21.6-19.6-39.8H654c1.6 10.8 5.4 18.6 11.6 23.2 5.6 4 14.2 6.2 26 6.2 10.4 0 18.8-1.8 24.8-5.2 6-3.6 9.2-8.2 9.2-14.2 0-7.6-4.6-13.6-13.4-18-2.8-1.4-12-4.6-27.8-9.4-17.6-5.6-28.6-9.6-32.8-12-11-6.6-16.4-16.2-16.4-28.8s5.2-22.6 16-29.8c10-7 22.6-10.4 38-10.4 16.8 0 30 3.4 39.4 10.4zM821.2 771c23.2 0 40.6 6.4 52.6 19.2 11.4 12 17.2 29.4 17.2 52.2 0 22.6-5.8 40-17.2 52.2-12 12.8-29.4 19.2-52.6 19.2H769V771h52.2z m-28.8 122.8h24.4c17.8 0 30.8-4.2 39-12.4 8-8.4 12-21.4 12-39 0-18-4-31.2-12-39.2-8.2-8.2-21.2-12.2-39-12.2h-24.4v102.8z" fill="#697187" p-id="1387"></path><path d="M434.7 281.4c61.6 0 92.3 26.1 92.3 78.6 0 52.9-31.1 79.3-93 79.3h-67V540h-39.5V281.4h107.2zM367 405.6h64.4c19.6 0 33.7-3.6 42.7-10.9 8.7-7.2 13.4-18.8 13.4-34.8s-4.7-27.5-13.8-34c-9.1-7.2-23.2-10.9-42.4-10.9H367v90.6zM688.1 344.8c14.4 10.7 22.8 27.4 24.8 49.9h-33.5c-2.9-12.7-8.4-21.9-15.9-27.4-7.5-5.8-19-8.4-34.1-8.4-13 0-22.8 1.7-29.7 5.5-8.7 4.3-12.7 11.5-12.7 21.4 0 8.7 4.6 15.9 14.4 21.1 4.3 2.3 16.2 6.6 35.2 12.7 28 8.7 45.9 15.6 54 20.2 17.9 10.7 26.8 25.4 26.8 44.4 0 18.5-7.2 33.2-21.6 43.9-14.4 10.4-34.9 15.9-61.2 15.9-25.4 0-45.3-4.9-59.4-14.7-17.3-12.1-26.8-31.2-28.3-57.4h33.5c2.3 15.6 7.8 26.8 16.7 33.5 8.1 5.8 20.5 8.9 37.5 8.9 15 0 27.1-2.6 35.8-7.5 8.7-5.2 13.3-11.8 13.3-20.5 0-11-6.6-19.6-19.3-26-4-2-17.3-6.6-40.1-13.6-25.4-8.1-41.3-13.9-47.3-17.3-15.9-9.5-23.7-23.4-23.7-41.6 0-18.2 7.5-32.6 23.1-43 14.4-10.1 32.6-15 54.8-15 24.3 0 43.3 4.9 56.9 15z" fill="#697187" p-id="1388"></path></svg>
 No newline at end of file
+0 −1
Original line number Diff line number Diff line
<?xml version="1.0" standalone="no"?><!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"><svg t="1620959014259" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="1383" xmlns:xlink="http://www.w3.org/1999/xlink" width="200" height="200"><defs><style type="text/css"></style></defs><path d="M832.2 127.9v766H192.7v-766h639.5m0-64H192.7c-35.3 0-64 28.7-64 64v766c0 35.3 28.7 64 64 64h639.5c35.3 0 64-28.7 64-64v-766c0-35.4-28.7-64-64-64z" fill="#b3b6bf" p-id="1384"></path><path d="M733.039 824.168v-32h97.9v32z" fill="#b3b6bf" p-id="1385"></path><path d="M896.2 958.3H425.4v-176c0-35.3 28.7-64 64-64h406.8c35.3 0 64 28.7 64 64v112c0 35.4-28.7 64-64 64z" fill="#b3b6bf" p-id="1386"></path><path d="M567 771c34.4 0 51.8 14.6 51.8 43.8 0 29.4-17.4 44.2-52.2 44.2h-35.4v54.8h-23.4V771H567z m-35.8 68h34c10.4 0 18-2 22.8-5.6 4.8-3.8 7.2-10 7.2-18.6 0-8.6-2.6-14.6-7.4-18.2-4.8-3.8-12.4-5.6-22.6-5.6h-34v48zM728.6 778.6c10 7.4 15.8 19 17.2 34.6h-23.2c-2-8.8-5.8-15.2-11-19-5.2-4-13.2-5.8-23.6-5.8-9 0-15.8 1.2-20.6 3.8-6 3-8.8 8-8.8 14.8 0 6 3.2 11 10 14.6 3 1.6 11.2 4.6 24.4 8.8 19.4 6 31.8 10.8 37.4 14 12.4 7.4 18.6 17.6 18.6 30.8 0 12.8-5 23-15 30.4-10 7.2-24.2 11-42.4 11-17.6 0-31.4-3.4-41.2-10.2-12-8.4-18.6-21.6-19.6-39.8H654c1.6 10.8 5.4 18.6 11.6 23.2 5.6 4 14.2 6.2 26 6.2 10.4 0 18.8-1.8 24.8-5.2 6-3.6 9.2-8.2 9.2-14.2 0-7.6-4.6-13.6-13.4-18-2.8-1.4-12-4.6-27.8-9.4-17.6-5.6-28.6-9.6-32.8-12-11-6.6-16.4-16.2-16.4-28.8s5.2-22.6 16-29.8c10-7 22.6-10.4 38-10.4 16.8 0 30 3.4 39.4 10.4zM821.2 771c23.2 0 40.6 6.4 52.6 19.2 11.4 12 17.2 29.4 17.2 52.2 0 22.6-5.8 40-17.2 52.2-12 12.8-29.4 19.2-52.6 19.2H769V771h52.2z m-28.8 122.8h24.4c17.8 0 30.8-4.2 39-12.4 8-8.4 12-21.4 12-39 0-18-4-31.2-12-39.2-8.2-8.2-21.2-12.2-39-12.2h-24.4v102.8z" fill="#b3b6bf" p-id="1387"></path><path d="M434.7 281.4c61.6 0 92.3 26.1 92.3 78.6 0 52.9-31.1 79.3-93 79.3h-67V540h-39.5V281.4h107.2zM367 405.6h64.4c19.6 0 33.7-3.6 42.7-10.9 8.7-7.2 13.4-18.8 13.4-34.8s-4.7-27.5-13.8-34c-9.1-7.2-23.2-10.9-42.4-10.9H367v90.6zM688.1 344.8c14.4 10.7 22.8 27.4 24.8 49.9h-33.5c-2.9-12.7-8.4-21.9-15.9-27.4-7.5-5.8-19-8.4-34.1-8.4-13 0-22.8 1.7-29.7 5.5-8.7 4.3-12.7 11.5-12.7 21.4 0 8.7 4.6 15.9 14.4 21.1 4.3 2.3 16.2 6.6 35.2 12.7 28 8.7 45.9 15.6 54 20.2 17.9 10.7 26.8 25.4 26.8 44.4 0 18.5-7.2 33.2-21.6 43.9-14.4 10.4-34.9 15.9-61.2 15.9-25.4 0-45.3-4.9-59.4-14.7-17.3-12.1-26.8-31.2-28.3-57.4h33.5c2.3 15.6 7.8 26.8 16.7 33.5 8.1 5.8 20.5 8.9 37.5 8.9 15 0 27.1-2.6 35.8-7.5 8.7-5.2 13.3-11.8 13.3-20.5 0-11-6.6-19.6-19.3-26-4-2-17.3-6.6-40.1-13.6-25.4-8.1-41.3-13.9-47.3-17.3-15.9-9.5-23.7-23.4-23.7-41.6 0-18.2 7.5-32.6 23.1-43 14.4-10.1 32.6-15 54.8-15 24.3 0 43.3 4.9 56.9 15z" fill="#b3b6bf" p-id="1388"></path></svg>
 No newline at end of file
+0 −1
Original line number Diff line number Diff line
<?xml version="1.0" standalone="no"?><!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"><svg t="1614063397044" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="2409" fill="#697187" xmlns:xlink="http://www.w3.org/1999/xlink" width="128" height="128"><defs><style type="text/css"></style></defs><path d="M768 102.4A153.6 153.6 0 0 1 921.6 256v512A153.6 153.6 0 0 1 768 921.6H256A153.6 153.6 0 0 1 102.4 768V256A153.6 153.6 0 0 1 256 102.4h512z m0 76.8h-16.009v192c0 21.197-17.169 38.4-38.4 38.4H310.41a38.4 38.4 0 0 1-38.4-38.4v-192H256a76.8 76.8 0 0 0-76.8 75.537V768c0 41.984 33.69 76.117 75.537 76.8H768a76.8 76.8 0 0 0 76.8-75.537V256a76.8 76.8 0 0 0-75.537-76.8H768z m-84.275 0H340.309v162.133h343.382V179.2z m-72.534 42.667a10.678 10.678 0 0 1 10.684 10.683v46.934c0 5.87-4.778 10.65-10.684 10.65h-46.933a10.65 10.65 0 0 1-10.65-10.684v-46.9c0-5.905 4.78-10.683 10.65-10.683h46.933z" p-id="2410" fill="#697187"></path></svg>
 No newline at end of file
+7 −1
Original line number Diff line number Diff line
@@ -90,6 +90,7 @@ class Canvas(QGraphicsScene):
    
    imgtrans_proj: ProjImgTrans = None
    painting_pen = QPen()
    painting_shape = 0
    erasing_pen = QPen()
    image_edit_mode = ImageEditMode.NONE
    alt_pressed = False
@@ -293,7 +294,7 @@ class Canvas(QGraphicsScene):
        if self.stroke_img_item is not None:
            self.stroke_img_item.startNewPoint(pos)
        else:
            self.stroke_img_item = StrokeImgItem(pen, pos, self.imgLayer.pixmap().size())
            self.stroke_img_item = StrokeImgItem(pen, pos, self.imgLayer.pixmap().size(), shape=self.painting_shape)
            if not erasing:
                self.stroke_img_item.setParentItem(self.baseLayer)
            else:
@@ -334,17 +335,21 @@ class Canvas(QGraphicsScene):
            
        elif self.creating_textblock:
            self.txtblkShapeControl.setRect(QRectF(self.create_block_origin, event.scenePos() / self.scale_factor).normalized())
        
        elif self.stroke_img_item is not None:
            if self.stroke_img_item.is_painting:
                pos = self.imgLayer.mapFromScene(event.scenePos())
                if self.erase_img_key is None:
                    # painting
                    self.stroke_img_item.lineTo(pos)
                else:
                    rect = self.stroke_img_item.lineTo(pos, update=False)
                    if rect is not None:
                        self.drawingLayer.update(rect)
        
        elif self.scale_tool_mode:
            self.scale_tool.emit(event.scenePos())
        
        elif self.rubber_band.isVisible() and self.rubber_band_origin is not None:
            self.rubber_band.setGeometry(QRectF(self.rubber_band_origin, event.scenePos()).normalized())
            sel_path = QPainterPath(self.rubber_band_origin)
@@ -353,6 +358,7 @@ class Canvas(QGraphicsScene):
                self.setSelectionArea(sel_path, deviceTransform=self.gv.viewportTransform())
            else:
                self.setSelectionArea(sel_path, Qt.ItemSelectionMode.IntersectsItemBoundingRect, self.gv.viewportTransform())
        
        return super().mouseMoveEvent(event)

    def mousePressEvent(self, event: QGraphicsSceneMouseEvent) -> None:
+75 −12
Original line number Diff line number Diff line
@@ -11,7 +11,7 @@ from utils.textblock_mask import canny_flood, connected_canny_flood
from utils.logger import logger

from .dl_manager import DLManager
from .image_edit import ImageEditMode, PixmapItem, StrokeImgItem
from .image_edit import ImageEditMode, PenShape, PixmapItem, StrokeImgItem
from .configpanel import InpaintConfigPanel
from .stylewidgets import Widget, SeparatorWidget, ColorPicker, PaintQSlider
from .canvas import Canvas
@@ -73,9 +73,22 @@ class InpaintPanel(Widget):
        thickness_layout.addWidget(self.thicknessSlider)
        thickness_layout.setSpacing(10)

        shape_label = ToolNameLabel(100, self.tr('Shape'))
        self.shapeCombobox = QComboBox(self)
        self.shapeCombobox.addItems([
            self.tr('Circle'),
            self.tr('Rectangle'),
            # self.tr('Triangle')
        ])
        self.shapeChanged = self.shapeCombobox.currentIndexChanged
        shape_layout = QHBoxLayout()
        shape_layout.addWidget(shape_label)
        shape_layout.addWidget(self.shapeCombobox)

        layout = QVBoxLayout(self)
        layout.setAlignment(Qt.AlignmentFlag.AlignTop)
        layout.addLayout(thickness_layout)
        layout.addLayout(shape_layout)
        layout.setSpacing(14)
        self.vlayout = layout

@@ -94,6 +107,10 @@ class InpaintPanel(Widget):
        self.inpainter_panel.needInpaintChecker.setVisible(True)
        return super().hideEvent(e)

    @property
    def shape(self):
        return self.shapeCombobox.currentIndex()


class PenConfigPanel(Widget):
    thicknessChanged = Signal(int)
@@ -125,10 +142,23 @@ class PenConfigPanel(Widget):
        thickness_layout.addWidget(self.thicknessSlider)
        thickness_layout.setSpacing(10)

        shape_label = ToolNameLabel(100, self.tr('Shape'))
        self.shapeCombobox = QComboBox(self)
        self.shapeCombobox.addItems([
            self.tr('Circle'),
            self.tr('Rectangle'),
            # self.tr('Triangle')
        ])
        self.shapeChanged = self.shapeCombobox.currentIndexChanged
        shape_layout = QHBoxLayout()
        shape_layout.addWidget(shape_label)
        shape_layout.addWidget(self.shapeCombobox)

        layout = QVBoxLayout(self)
        layout.setAlignment(Qt.AlignmentFlag.AlignTop)
        layout.addLayout(color_layout)
        layout.addLayout(thickness_layout)
        layout.addLayout(shape_layout)
        layout.setSpacing(20)

    def on_thickness_changed(self):
@@ -148,6 +178,10 @@ class PenConfigPanel(Widget):
        color[-1] = self.alphaSlider.value()
        self.colorChanged.emit(color)

    @property
    def shape(self):
        return self.shapeCombobox.currentIndex()


class RectPanel(Widget):
    dilate_ksize_changed = Signal()
@@ -263,6 +297,7 @@ class DrawingPanel(Widget):
        self.inpaintTool.checked.connect(self.on_use_inpainttool)
        self.inpaintConfigPanel = InpaintPanel(inpainter_panel)
        self.inpaintConfigPanel.thicknessChanged.connect(self.setInpaintToolWidth)
        self.inpaintConfigPanel.shapeChanged.connect(self.setInpaintShape)

        self.rectTool = DrawToolCheckBox()
        self.rectTool.setObjectName("DrawRectTool")
@@ -279,6 +314,7 @@ class DrawingPanel(Widget):
        self.penConfigPanel = PenConfigPanel()
        self.penConfigPanel.thicknessChanged.connect(self.setPenToolWidth)
        self.penConfigPanel.colorChanged.connect(self.setPenToolColor)
        self.penConfigPanel.shapeChanged.connect(self.setPenShape)

        toolboxlayout = QBoxLayout(QBoxLayout.Direction.LeftToRight)
        toolboxlayout.setAlignment(Qt.AlignmentFlag.AlignLeft)
@@ -325,6 +361,10 @@ class DrawingPanel(Widget):
        if self.isVisible():
            self.setInpaintCursor()

    def setInpaintShape(self, shape: int):
        self.setInpaintCursor()
        self.canvas.painting_shape = shape

    def setPenToolWidth(self, width):
        self.pentool_pen.setWidthF(width)
        self.erasing_pen.setWidthF(width)
@@ -340,6 +380,10 @@ class DrawingPanel(Widget):
        self.penConfigPanel.colorPicker.setPickerColor(color)
        self.penConfigPanel.alphaSlider.setValue(color.alpha())

    def setPenShape(self, shape: int):
        self.setPenCursor()
        self.canvas.painting_shape = shape

    def on_use_handtool(self) -> None:
        if self.currentTool is not None and self.currentTool != self.handTool:
            self.currentTool.setChecked(False)
@@ -354,6 +398,7 @@ class DrawingPanel(Widget):
        self.canvas.image_edit_mode = ImageEditMode.InpaintTool
        self.canvas.painting_pen = self.inpaint_pen
        self.canvas.erasing_pen = self.inpaint_pen
        self.canvas.painting_shape = self.inpaintConfigPanel.shape
        self.toolConfigStackwidget.setCurrentWidget(self.inpaintConfigPanel)
        if self.isVisible():
            self.canvas.gv.setDragMode(QGraphicsView.DragMode.NoDrag)
@@ -364,6 +409,7 @@ class DrawingPanel(Widget):
            self.currentTool.setChecked(False)
        self.currentTool = self.penTool
        self.canvas.painting_pen = self.pentool_pen
        self.canvas.painting_shape = self.penConfigPanel.shape
        self.canvas.erasing_pen = self.erasing_pen
        self.canvas.image_edit_mode = ImageEditMode.PenTool
        self.toolConfigStackwidget.setCurrentWidget(self.penConfigPanel)
@@ -385,7 +431,11 @@ class DrawingPanel(Widget):
        pc = self.pentool_pen.color()
        config.pentool_color = [pc.red(), pc.green(), pc.blue(), pc.alpha()]
        config.pentool_width = self.pentool_pen.widthF()
        config.pentool_shape = self.penConfigPanel.shape

        config.inpainter_width = self.inpaint_pen.widthF()
        config.inpainter_shape = self.penConfigPanel.shape

        if self.currentTool == self.handTool:
            config.current_tool = ImageEditMode.HandTool
        elif self.currentTool == self.inpaintTool:
@@ -401,10 +451,13 @@ class DrawingPanel(Widget):

    def set_config(self, config: DrawPanelConfig):
        self.setPenToolWidth(config.pentool_width)
        self.setPenToolColor(config.pentool_color)
        self.penConfigPanel.thicknessSlider.setValue(config.pentool_width)
        self.penConfigPanel.shapeCombobox.setCurrentIndex(config.pentool_shape)
        
        self.setInpaintToolWidth(config.inpainter_width)
        self.inpaintConfigPanel.thicknessSlider.setValue(config.inpainter_width)
        self.setPenToolColor(config.pentool_color)
        self.inpaintConfigPanel.shapeCombobox.setCurrentIndex(config.inpainter_shape)
        
        self.rectPanel.dilate_slider.setValue(config.recttool_dilate_ksize)
        self.rectPanel.autoChecker.setChecked(config.rectool_auto)
@@ -418,7 +471,7 @@ class DrawingPanel(Widget):
        elif config.current_tool == ImageEditMode.RectTool:
            self.rectTool.setChecked(True)

    def get_pen_cursor(self, pen_color: QColor = None, pen_size = None, draw_circle=True) -> QCursor:
    def get_pen_cursor(self, pen_color: QColor = None, pen_size = None, draw_shape=True, shape=PenShape.Circle) -> QCursor:
        cross_size = 31
        cross_len = cross_size // 4
        thickness = 3
@@ -441,11 +494,21 @@ class DrawingPanel(Widget):
        painter = QPainter(cur_pixmap)
        painter.setPen(pen)
        painter.setRenderHint(QPainter.RenderHint.Antialiasing)
        if draw_circle:
        if draw_shape:
            if shape == PenShape.Circle:
                painter.drawEllipse(cursor_center-pen_radius + thickness, 
                                    cursor_center-pen_radius + thickness, 
                                    pen_size - 2*thickness, 
                                    pen_size - 2*thickness)
            elif shape == PenShape.Rectangle:
                painter.drawRect(cursor_center-pen_radius + thickness, 
                                    cursor_center-pen_radius + thickness, 
                                    pen_size - 2*thickness, 
                                    pen_size - 2*thickness)
            else:
                raise NotImplementedError
            # elif shape == PenShape.Triangle:
                # painter.drawPolygon
        cross_left = (map_size  - 1 - cross_size) // 2 
        cross_right = map_size - cross_left

@@ -626,7 +689,7 @@ class DrawingPanel(Widget):
        self.setCrossCursor()
        
    def setCrossCursor(self):
        self.canvas.gv.setCursor(self.get_pen_cursor(draw_circle=False))
        self.canvas.gv.setCursor(self.get_pen_cursor(draw_shape=False))

    def on_scale_tool(self, pos: QPointF):
        if self.scale_tool_pos is None:
@@ -658,10 +721,10 @@ class DrawingPanel(Widget):
            self.setInpaintCursor()

    def setPenCursor(self):
        self.canvas.gv.setCursor(self.get_pen_cursor())
        self.canvas.gv.setCursor(self.get_pen_cursor(shape=self.penConfigPanel.shape))

    def setInpaintCursor(self):
        self.canvas.gv.setCursor(self.get_pen_cursor(INPAINT_BRUSH_COLOR, self.inpaint_pen.width()))
        self.canvas.gv.setCursor(self.get_pen_cursor(INPAINT_BRUSH_COLOR, self.inpaint_pen.width(), shape=self.inpaintConfigPanel.shape))

    def on_handchecker_changed(self):
        if self.handTool.isChecked():
Loading