一个使用python的pyqt5库的贝塞尔曲线绘图程序


一个使用python的pyqt5库的贝塞尔曲线绘图程序

自己写的,写了好几天,不大的一个小程序,实现了像PS一样使用贝塞尔曲线画图的功能,不过不能导出图片,需要的同志自己加吧,我比较菜,写的程序里有很多奇怪的地方,以及偷懒的修改bug的方法,以及残留和未发现的各种bug,使用方式的话我在b站上发了视频,标题就叫《自制一个使用贝塞尔曲线的画图程序》,因为写文章的时候还没发出来,所以不放链接了。程序是为了一些其他的用途,顺便把代码挂出来了,分享一下pyqt5的学习经验,如果有帮助那还挺好的,不过我参考了很多其他更有帮助的文章和程序,也许有特定需求的人会收到更多帮助吧。


一、程序的思路

先创建了贝塞尔曲线对象,包含它所需要的各种参数,然后对应于图形化界面编程库pyqt5的各种按钮、点击等操作进行对贝塞尔曲线的调整和绘制即可。(有点懒不想写更具体了,对不起,毕竟程序就在下面,里面也有一些注释但是不多)

二、程序

代码如下(示例):

import sys
import numpy as np
from PyQt5.QtWidgets import QApplication, QWidget, QToolTip, QPushButton, QMessageBox, QSlider, QColorDialog
from PyQt5.QtGui import QIcon, QFont, QDrag, QPainter, QPainterPath, QPen, QBrush, QColor
from PyQt5.QtCore import QCoreApplication, Qt, QMimeData, QPoint


def achange_x(x, scale, x_position):
    return 740+(x-x_position*14+7)*scale*90


def achange_y(y, scale, y_position):
    return 410-(y-y_position*8+4)*scale*90


def dechange_x(x, scale, x_position):
    return (x-740)/(scale*90)+x_position*14-7


def dechange_y(y, scale, y_position):
    return -(y-410)/(scale*90)+y_position*8-4


class BCurve(object):

    def __init__(self):
        self.points = []
        self.handles1 = []
        self.handles2 = []

        self.circle = False

    def add_point(self, positionx, positiony):
        if len(self.points) > 0:
            if not self.circle:
                self.points.append([positionx, positiony, 0])
                self.handles1.append([0, 0, 0])
                self.handles2.append([0, 0, 0])
            else:
                self.circle = True
                self.handles1.append([0, 0, 0])
                self.handles2.append([0, 0, 0])
        else:
            self.points.append([positionx, positiony, 0])

    def delete_point(self, num):
        if len(self.points) > 0:
            self.points.pop(num)
            try:
                if num < len(self.points) and not self.circle:
                    self.handles1.pop(num)
                elif self.circle:
                    self.handles1.pop(num)
                elif num == len(self.points):
                    self.handles1.pop(num-1)
            except:
                pass
            try:
                if num > 0 and not self.circle:
                    self.handles2.pop(num-1)
                elif self.circle:
                    self.handles2.pop(num-1)
                elif num == 0:
                    self.handles2.pop(num)
            except:
                pass
        else:
            pass

    def get_point(self, num):
        return self.points[num]

    def get_handles1(self, num):
        return self.handles1[num]

    def get_handles2(self, num):
        return self.points[num-1]

    def set_point(self, num, positionx, positiony):
        self.points[num][0] = positionx
        self.points[num][1] = positiony

    def set_handles1(self, num, positionx, positiony, band=True):
        self.handles1[num][0] = positionx
        self.handles1[num][1] = positiony
        if (band and num > 0) or (band and self.circle):
            try:
                self.handles2[num-1][0] = -positionx
                self.handles2[num-1][1] = -positiony
            except:
                pass

    def set_handles2(self, num, positionx, positiony, band=True):
        self.handles2[num - 1][0] = positionx
        self.handles2[num - 1][1] = positiony
        if (band and num < len(self.handles2)) or (band and self.circle):
            try:
                self.handles1[num][0] = -positionx
                self.handles1[num][1] = -positiony
            except:
                pass

    def reset_circle(self):
        self.circle = False

    def circlular_curve(self):
        if not self.circle:
            self.circle = True
            self.add_point(self.points[0][0], self.points[0][1])


start_drag = [False]
setting_point = [None, None, None]


class App(QWidget):

    def __init__(self):
        self.curves = []
        self.stroke_color_list = []
        self.fill_color_list = []
        self.stroke_color_alpha_list = []
        self.fill_color_alpha_list = []
        self.mode = 'add_point' # delete_point\move_point\set_stroke_color\set_fill_color
        self.motion_curve = None # 当前操作中的贝塞尔曲线,新建后默认最新的曲线
        self.if_paint_grid = True # 是否绘制网格线
        self.scale = 1 # 图形缩放
        self.x_position = 0.5 # 横向位置
        self.y_position = 0.5 # 纵向位置
        self.if_set_single_handle = False # 设置单个柄点,不联动
        super().__init__()
        self.initUI()

    def initUI(self):
        self.setAcceptDrops(True)

        # 按钮设置
        buttongrid = QPushButton('绘制\n网格', self)
        buttongrid.setStyleSheet('''QPushButton{font-family:'黑体';font-size:15px;color:rgb(0,0,0,255);}
        QPushButton{background-color:rgb(255,255,255);}QPushButton:hover{background-color:rgb(255,255,200);}''')
        buttongrid.move(1500, 130)
        buttongrid.clicked[bool].connect(self.mode_add_grid)

        button1 = QPushButton('添加\n节点', self)
        button1.setStyleSheet('''QPushButton{font-family:'黑体';font-size:15px;color:rgb(0,0,0,255);}
        QPushButton{background-color:rgb(255,255,255);}QPushButton:hover{background-color:rgb(255,255,200);}''')
        button1.move(1500, 30)
        button1.clicked[bool].connect(self.mode_add_point)

        button2 = QPushButton('删除\n节点', self)
        button2.setStyleSheet('''QPushButton{font-family:'黑体';font-size:15px;color:rgb(0,0,0,255);}
        QPushButton{background-color:rgb(255,255,255);}QPushButton:hover{background-color:rgb(255,200,255);}''')
        button2.move(1580, 30)
        button2.clicked[bool].connect(self.mode_choose_point)

        button3 = QPushButton('控制\n节点', self)
        button3.setStyleSheet('''QPushButton{font-family:'黑体';font-size:15px;color:rgb(0,0,0,255);}
        QPushButton{background-color:rgb(255,255,255);}QPushButton:hover{background-color:rgb(200,255,255);}''')
        button3.move(1660, 30)
        button3.clicked[bool].connect(self.mode_move_point)

        button4 = QPushButton('描边\n颜色', self)
        button4.setStyleSheet('''QPushButton{font-family:'黑体';font-size:15px;color:rgb(0,0,0,255);}
        QPushButton{background-color:rgb(255,255,255);}QPushButton:hover{background-color:rgb(200,255,200);}''')
        button4.move(1740, 30)
        button4.clicked[bool].connect(self.set_stroke_color)

        button5 = QPushButton('填充\n颜色', self)
        button5.setStyleSheet('''QPushButton{font-family:'黑体';font-size:15px;color:rgb(0,0,0,255);}
        QPushButton{background-color:rgb(255,255,255);}QPushButton:hover{background-color:rgb(255,200,200);}''')
        button5.move(1820, 30)
        button5.clicked[bool].connect(self.set_fill_color)

        button6 = QPushButton('网格\n颜色', self)
        button6.setStyleSheet('''QPushButton{font-family:'黑体';font-size:15px;color:rgb(0,0,0,255);}
        QPushButton{background-color:rgb(255,255,255);}QPushButton:hover{background-color:rgb(255,200,200);}''')
        button6.move(1580, 130)
        button6.clicked[bool].connect(self.mode_set_grid_color)

        button7 = QPushButton('取消\n选中', self)
        button7.setStyleSheet('''QPushButton{font-family:'黑体';font-size:15px;color:rgb(0,0,0,255);}
        QPushButton{background-color:rgb(255,255,255);}QPushButton:hover{background-color:rgb(255,200,200);}''')
        button7.move(1660, 130)
        button7.clicked[bool].connect(self.choose_curve(None))

        button8 = QPushButton('闭合\n曲线', self)
        button8.setStyleSheet('''QPushButton{font-family:'黑体';font-size:15px;color:rgb(0,0,0,255);}
        QPushButton{background-color:rgb(255,255,255);}QPushButton:hover{background-color:rgb(255,200,200);}''')
        button8.move(1740, 130)
        button8.clicked[bool].connect(self.close_curve)

        button9 = QPushButton('新建\n分支', self)
        button9.setStyleSheet('''QPushButton{font-family:'黑体';font-size:15px;color:rgb(0,0,0,255);}
        QPushButton{background-color:rgb(255,255,255);}QPushButton:hover{background-color:rgb(255,200,200);}''')
        button9.move(1820, 130)
        button9.clicked[bool].connect(self.new_path)

        button10 = QPushButton('调节单个柄点', self)
        button10.setStyleSheet('''QPushButton{font-family:'黑体';font-size:30px;color:rgb(0,0,0,255);}
        QPushButton{background-color:rgb(255,255,255);}QPushButton:hover{background-color:rgb(255,200,200);}''')
        button10.setCheckable(True)
        button10.setGeometry(1500, 600, 390, 50)
        button10.clicked[bool].connect(self.set_single_handle)

        self.init_create_curve_button()

        # 滑块设置
        sld1 = QSlider(Qt.Horizontal, self)
        sld1.setFocusPolicy(Qt.NoFocus)
        sld1.setValue(0.5*100/8.5)
        sld1.setGeometry(1500, 230, 390, 50)
        sld1.setStyleSheet(
            "QSlider::handle:horizontal{width:40px;background-color:rgb(100,100,100);margin:-14px 0px -14px 0px;border-radius:17px;}"
            "QSlider::groove:horizontal{height:8px;background-color:rgb(219,219,219);}"
            "QSlider::add-page:horizontal{background-color:rgb(219,219,219);}"
            "QSlider::sub-page:horizontal{background-color:rgb(26,217,110);}")
        sld1.valueChanged[int].connect(self.change_scale)

        sld2 = QSlider(Qt.Horizontal, self)
        sld2.setFocusPolicy(Qt.NoFocus)
        sld2.setValue(50)
        sld2.setGeometry(1500, 280, 390, 50)
        sld2.setStyleSheet(
            "QSlider::handle:horizontal{width:40px;background-color:rgb(100,100,100);margin:-14px 0px -14px 0px;border-radius:17px;}"
            "QSlider::groove:horizontal{height:8px;background-color:rgb(219,219,219);}"
            "QSlider::add-page:horizontal{background-color:rgb(219,219,219);}"
            "QSlider::sub-page:horizontal{background-color:rgb(26,217,110);}")
        sld2.valueChanged[int].connect(self.change_x_position)

        sld3 = QSlider(Qt.Horizontal, self)
        sld3.setFocusPolicy(Qt.NoFocus)
        sld3.setValue(50)
        sld3.setGeometry(1500, 330, 390, 50)
        sld3.setStyleSheet(
            "QSlider::handle:horizontal{width:40px;background-color:rgb(100,100,100);margin:-14px 0px -14px 0px;border-radius:17px;}"
            "QSlider::groove:horizontal{height:8px;background-color:rgb(219,219,219);}"
            "QSlider::add-page:horizontal{background-color:rgb(219,219,219);}"
            "QSlider::sub-page:horizontal{background-color:rgb(26,217,110);}")
        sld3.valueChanged[int].connect(self.change_y_position)

        sld4 = QSlider(Qt.Horizontal, self)
        sld4.setFocusPolicy(Qt.NoFocus)
        sld4.setValue(50)
        sld4.setGeometry(1500, 400, 390, 50)
        sld4.setStyleSheet(
            "QSlider::handle:horizontal{width:40px;background-color:rgb(100,100,200);margin:-14px 0px -14px 0px;border-radius:17px;}"
            "QSlider::groove:horizontal{height:8px;background-color:rgb(219,219,219);}"
            "QSlider::add-page:horizontal{background-color:rgb(219,219,219);}"
            "QSlider::sub-page:horizontal{background-color:rgb(100,100,200);}")
        sld4.valueChanged[int].connect(self.set_stroke_alpha)

        sld5 = QSlider(Qt.Horizontal, self)
        sld5.setFocusPolicy(Qt.NoFocus)
        sld5.setValue(50)
        sld5.setGeometry(1500, 450, 390, 50)
        sld5.setStyleSheet(
            "QSlider::handle:horizontal{width:40px;background-color:rgb(200,100,100);margin:-14px 0px -14px 0px;border-radius:17px;}"
            "QSlider::groove:horizontal{height:8px;background-color:rgb(219,219,219);}"
            "QSlider::add-page:horizontal{background-color:rgb(219,219,219);}"
            "QSlider::sub-page:horizontal{background-color:rgb(200,100,100);}")
        sld5.valueChanged[int].connect(self.set_fill_alpha)

        self.setWindowTitle('当前模式')
        self.setGeometry(0, 30, 1924, 999)

    def set_single_handle(self):
        self.if_set_single_handle = not self.if_set_single_handle

    def set_stroke_color(self):
        col = QColorDialog.getColor()
        if self.motion_curve is not None:
            self.stroke_color_list[self.motion_curve] = col

    def set_fill_color(self):
        col = QColorDialog.getColor()
        if self.motion_curve is not None:
            self.fill_color_list[self.motion_curve] = col

    def set_stroke_alpha(self, value):
        self.stroke_color_alpha_list[self.motion_curve] = 255*value/100

    def set_fill_alpha(self, value):
        self.fill_color_alpha_list[self.motion_curve] = 255*value/100

    def new_path(self):
        # 新建分支按钮,新建一个分支
        try:
            if len(self.curves[self.motion_curve]) == 0:
                self.curves[self.motion_curve].append(BCurve())
            elif len(self.curves[self.motion_curve][-1].points) > 1:
                self.curves[self.motion_curve].append(BCurve())
        except:
            pass

    def close_curve(self):
        # 闭合曲线按钮,生成闭合曲线的最后一段
        try:
            self.curves[self.motion_curve][-1].circlular_curve()
        except:
            pass

    def change_scale(self, value):
        # 设置缩放
        self.scale = value*8.5/100+0.5

    def change_x_position(self, value):
        # 设置横向中心
        self.x_position = value/100

    def change_y_position(self, value):
        # 设置纵向中心
        self.y_position = 1-value/100

    def init_create_curve_button(self):
        # 初始化底端选择曲线和新建曲线的按钮
        po = [40, 830]
        self.curvesbutton = []
        self.tag = QPushButton('当前曲线', self)
        self.tag.setStyleSheet(
                '''QPushButton{font-family:'黑体';font-size:12px;color:rgb(0,0,0,255);}
                QPushButton{background-color:rgb(155,155,155);}QPushButton:hover{background-color:rgb(155,155,155);}''')
        self.tag.setGeometry(po[0]+80*22, po[1]+110, 70, 20)
        for i in range(22):
            button = QPushButton('曲线\n'+str(i+1), self)
            button.setStyleSheet(
                '''QPushButton{font-family:'宋体';font-size:25px;color:rgb(0,0,0,255);}
                QPushButton{background-color:rgb(255,255,255);}QPushButton:hover{background-color:rgb(255,255,255);}''')
            button.setGeometry(po[0], po[1], 70, 100)
            button.clicked[bool].connect(self.choose_curve(i))
            self.curvesbutton.append(button)
            po[0] += 80
        button = QPushButton('+', self)
        button.setStyleSheet(
            '''QPushButton{font-family:'宋体';font-size:25px;color:rgb(0,0,0,255);}
            QPushButton{background-color:rgb(205,205,255);}QPushButton:hover{background-color:rgb(200,200,200);}''')
        button.setGeometry(po[0], po[1], 70, 100)
        button.clicked[bool].connect(self.add_new_curve)
        self.curvesbutton.append(button)
        po[0] += 100

    def add_new_curve(self):
        # 赋予新建曲线按钮的函数
        if len(self.curves) < 21:
            self.curves.append([])
            self.fill_color_list.append(QColor(0, 0, 0, 255))
            self.stroke_color_list.append(QColor(0, 0, 0, 255))
            self.fill_color_alpha_list.append(100)
            self.stroke_color_alpha_list.append(255)
            self.curvesbutton[len(self.curves)-1].setStyleSheet(
                '''QPushButton{font-family:'宋体';font-size:25px;color:rgb(0,0,0,255);}
                QPushButton{background-color:rgb(205,205,255);}QPushButton:hover{background-color:rgb(170,170,230);}''')
            self.tag.setGeometry(-40 + 80 * len(self.curves), 830 + 110, 70, 20)
            self.motion_curve = len(self.curves)-1
        elif len(self.curves) == 21:
            self.curves.append([])
            self.fill_color_list.append(QColor(0, 0, 0, 255))
            self.stroke_color_list.append(QColor(0, 0, 0, 255))
            self.fill_color_alpha_list.append(255)
            self.stroke_color_alpha_list.append(255)
            self.curvesbutton[len(self.curves)-1].setStyleSheet(
                '''QPushButton{font-family:'宋体';font-size:25px;color:rgb(0,0,0,255);}
                QPushButton{background-color:rgb(205,205,255);}QPushButton:hover{background-color:rgb(170,170,230);}''')
            self.curvesbutton[-1].setStyleSheet(
                '''QPushButton{font-family:'宋体';font-size:25px;color:rgb(30,30,30,255);}
                QPushButton{background-color:rgb(255,255,255);}QPushButton:hover{background-color:rgb(255,255,255);}''')
            self.tag.setGeometry(-40 + 80 * len(self.curves), 830 + 110, 70, 20)
            self.motion_curve = len(self.curves)-1
        else:
            pass

    def choose_curve(self, num):
        # 赋予下面一排曲线按钮的函数
        def choose_curve_func():
            self.motion_curve = num
            if num is not None:
                self.tag.setGeometry(40+80*num, 830 + 110, 70, 20)
            else:
                self.tag.setGeometry(40 + 80 * 22, 830 + 110, 70, 20)
        return choose_curve_func

    def mode_add_grid(self):
        # 是否绘制网格
        self.if_paint_grid = not self.if_paint_grid

    def mode_add_point(self):
        # 设置当前模式为添加节点
        self.mode = 'add_point'
        self.setWindowTitle('添加节点')

    def mode_choose_point(self):
        # 设置当前模式为删除节点
        self.mode = 'delete_point'
        self.setWindowTitle('删除节点')

    def mode_move_point(self):
        # 设置当前模式为控制节点
        self.mode = 'move_point'
        self.setWindowTitle('控制节点')

    def mode_set_stroke_color(self):
        # 设置当前模式为描边颜色
        self.mode = 'set_stroke_color'
        self.setWindowTitle('描边颜色')

    def mode_set_fill_color(self):
        # 设置当前模式为填充颜色
        self.mode = 'set_fill_color'
        self.setWindowTitle('填充颜色')

    def mode_set_grid_color(self):
        # 设置网格颜色
        pass

    def check_click_on(self, position1, position2):
        if ((position1[0]-position2[0])**2+(position1[1]-position2[1])**2)**0.5 < 10:
            return True
        else:
            return False

    def paintsomedots(self, qp):

        qp.setRenderHint(QPainter.Antialiasing)

        for curves in range(len(self.curves)):
            path = QPainterPath()
            pencolor = self.stroke_color_list[curves]
            pencolor.setAlpha(self.stroke_color_alpha_list[curves])
            brushcolor = self.fill_color_list[curves]
            brushcolor.setAlpha(self.fill_color_alpha_list[curves])
            pen = QPen(pencolor, 2, Qt.SolidLine)
            qp.setPen(pen)
            qp.setBrush(brushcolor)
            for curve in self.curves[curves]:
                total = len(curve.handles1)
                total1 = len(curve.points)
                if total1 > 0:
                    path.moveTo(achange_x(curve.points[0][0], self.scale, self.x_position),
                                achange_y(curve.points[0][1], self.scale, self.y_position))
                for num in range(total):
                    path.cubicTo(achange_x(curve.points[num][0]+curve.handles1[num][0], self.scale, self.x_position),
                                 achange_y(curve.points[num][1]+curve.handles1[num][1], self.scale, self.y_position),
                                 achange_x(curve.points[(num+1)%total1][0]+curve.handles2[num][0], self.scale, self.x_position),
                                 achange_y(curve.points[(num+1)%total1][1]+curve.handles2[num][1], self.scale, self.y_position),
                                 achange_x(curve.points[(num+1)%total1][0], self.scale, self.x_position),
                                 achange_y(curve.points[(num+1)%total1][1], self.scale, self.y_position))
            qp.drawPath(path)

        pen = QPen(QColor(0, 0, 0), 2, Qt.SolidLine)
        qp.setPen(pen)
        qp.setBrush(QColor(255, 80, 0))
        if self.motion_curve is not None and len(self.curves)>self.motion_curve:
            for curve in self.curves[self.motion_curve]:
                total = len(curve.handles1)
                total1 = len(curve.points)
                for num in range(total1):
                    qp.drawArc(achange_x(curve.points[num][0], self.scale, self.x_position)-10,
                               achange_y(curve.points[num][1], self.scale, self.y_position)-10, 20, 20,
                               20, 360*16)
                for num in range(total):
                    qp.drawArc(achange_x(curve.points[num][0]+curve.handles1[num][0], self.scale, self.x_position)-5,
                               achange_y(curve.points[num][1]+curve.handles1[num][1], self.scale, self.y_position)-5, 10, 10,
                               10, 360*16)
                    qp.drawLine(achange_x(curve.points[num][0]+curve.handles1[num][0], self.scale, self.x_position),
                                achange_y(curve.points[num][1]+curve.handles1[num][1], self.scale, self.y_position),
                                achange_x(curve.points[num][0], self.scale, self.x_position),
                                achange_y(curve.points[num][1], self.scale, self.y_position))
                for num in range(total):
                    qp.drawArc(achange_x(curve.points[(num+1)%total1][0]+curve.handles2[num][0], self.scale, self.x_position)-5,
                               achange_y(curve.points[(num+1)%total1][1]+curve.handles2[num][1], self.scale, self.y_position)-5, 10, 10,
                               10, 360*16)
                    qp.drawLine(achange_x(curve.points[(num+1)%total1][0]+curve.handles2[num][0], self.scale, self.x_position),
                                achange_y(curve.points[(num+1)%total1][1]+curve.handles2[num][1], self.scale, self.y_position),
                                achange_x(curve.points[(num+1)%total1][0], self.scale, self.x_position),
                                achange_y(curve.points[(num+1)%total1][1], self.scale, self.y_position))

    def paint_grid(self, qp):
        # 绘制网格

        pen = QPen(Qt.lightGray, 2, Qt.SolidLine)
        qp.setPen(pen)
        for i in range(9):
            if i == 4:
                pen = QPen(Qt.lightGray, 4, Qt.SolidLine)
                qp.setPen(pen)
            qp.drawLine(achange_x(-7, self.scale, self.x_position), achange_y(-4+i, self.scale, self.y_position),
                        achange_x(7, self.scale, self.x_position), achange_y(-4+i, self.scale, self.y_position))
            if i == 4:
                pen = QPen(Qt.lightGray, 2, Qt.SolidLine)
                qp.setPen(pen)
        for i in range(15):
            if i == 7:
                pen = QPen(Qt.lightGray, 4, Qt.SolidLine)
                qp.setPen(pen)
            qp.drawLine(achange_x(-7+i, self.scale, self.x_position), achange_y(-4, self.scale, self.y_position),
                        achange_x(-7+i, self.scale, self.x_position), achange_y(4, self.scale, self.y_position))
            if i == 7:
                pen = QPen(Qt.lightGray, 2, Qt.SolidLine)
                qp.setPen(pen)

    def paintEvent(self, e):
        self.update()

        e.accept()

        qp = QPainter()
        qp.begin(self)
        if self.if_paint_grid:
            self.paint_grid(qp)
        self.paintsomedots(qp)
        qp.end()

    def mousePressEvent(self, e):
        e.accept()
        position = e.pos()

        if self.mode == 'add_point':
            if self.motion_curve is not None:
                if self.curves[self.motion_curve]:
                    if self.curves[self.motion_curve][-1].circle == False:
                        self.curves[self.motion_curve][-1].add_point(dechange_x(position.x(), self.scale, self.x_position),
                                                                     dechange_y(position.y(), self.scale, self.y_position))

        if self.mode == 'delete_point':
            if self.motion_curve is not None:
                if self.curves[self.motion_curve]:
                    curve_delete = None
                    num_delete = None
                    for curve in self.curves[self.motion_curve]:
                        for i in range(len(curve.points)):
                            if self.check_click_on([position.x(), position.y()],
                                                   [achange_x(curve.points[i][0], self.scale, self.x_position),
                                                    achange_y(curve.points[i][1], self.scale, self.y_position)]):
                                curve_delete = curve
                                num_delete = i
                    if curve_delete and num_delete is not None:
                        curve_delete.delete_point(num_delete)
                        print(3)
                        if len(curve_delete.points) == 0:
                            print(1)
                            curve_delete.reset_circle()
                            print(2)

        elif self.mode == 'move_point':
            if self.motion_curve is not None:
                if self.curves[self.motion_curve]:
                    for curve in self.curves[self.motion_curve]:
                        if not start_drag[0]:
                            for i in range(len(curve.handles1)):
                                if self.check_click_on([position.x(), position.y()],
                                                       [achange_x(curve.points[i][0]+curve.handles1[i][0], self.scale, self.x_position),
                                                        achange_y(curve.points[i][1]+curve.handles1[i][1], self.scale, self.y_position)]):
                                    start_drag[0] = True
                                    setting_point[0] = curve
                                    setting_point[1] = i
                                    setting_point[2] = 1
                        if not start_drag[0]:
                            for i in range(len(curve.handles2)):
                                if self.check_click_on([position.x(), position.y()],
                                                       [achange_x(curve.points[(i+1)%len(curve.points)][0]+curve.handles2[i][0], self.scale, self.x_position),
                                                        achange_y(curve.points[(i+1)%len(curve.points)][1]+curve.handles2[i][1], self.scale, self.y_position)]):
                                    start_drag[0] = True
                                    setting_point[0] = curve
                                    setting_point[1] = (i+1)%len(curve.points)
                                    setting_point[2] = 2
                        if not start_drag[0]:
                            for i in range(len(curve.points)):
                                if self.check_click_on([position.x(), position.y()],
                                                       [achange_x(curve.points[i][0], self.scale, self.x_position),
                                                        achange_y(curve.points[i][1], self.scale, self.y_position)]):
                                    start_drag[0] = True
                                    setting_point[0] = curve
                                    setting_point[1] = i
                                    setting_point[2] = 3

    def mouseReleaseEvent(self, e):
        e.accept()
        position = e.pos()

        start_drag[0] = False
        setting_point[0] = None
        setting_point[1] = None
        setting_point[2] = None

    def mouseMoveEvent(self, e):
        e.accept()
        position = e.pos()

        if start_drag[0]:
            if setting_point[0] is not None:
                if setting_point[2] == 1:
                    setting_point[0].set_handles1(setting_point[1],
                                                  -setting_point[0].points[setting_point[1]][0]+dechange_x(position.x(), self.scale, self.x_position),
                                                  -setting_point[0].points[setting_point[1]][1]+dechange_y(position.y(), self.scale, self.y_position),
                                                  band=not self.if_set_single_handle)
                elif setting_point[2] == 2:
                    setting_point[0].set_handles2(setting_point[1],
                                                  -setting_point[0].points[setting_point[1]][0]+dechange_x(position.x(), self.scale, self.x_position),
                                                  -setting_point[0].points[setting_point[1]][1]+dechange_y(position.y(), self.scale, self.y_position),
                                                  band=not self.if_set_single_handle)
                elif setting_point[2] == 3:
                    setting_point[0].set_point(setting_point[1],
                                               dechange_x(position.x(), self.scale, self.x_position),
                                               dechange_y(position.y(), self.scale, self.y_position))

    def closeEvent(self, event):
        reply = QMessageBox.question(self, '哎呀呀小家伙', '确定要退出吗?',
                                     QMessageBox.Yes | QMessageBox.No, QMessageBox.No)
        if reply == QMessageBox.Yes:
            event.accept()
        else:
            event.ignore()


if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = App()
    ex.show()
    app.exec_()


总结

额,还会看到这里吗?我溜了先,两个猫在打架,我看看热闹去。

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值