|
|
from PyQt5.QtWidgets import QWidget
|
|
|
from PyQt5.Qt import QPixmap, QPainter, QPoint, QPen, QColor, QSize
|
|
|
from PyQt5.QtCore import Qt
|
|
|
|
|
|
|
|
|
class PaintBoard(QWidget):
|
|
|
def __init__(self, Parent=None):
|
|
|
super().__init__(Parent)
|
|
|
|
|
|
# 先初始化数据,再初始化界面
|
|
|
self.__InitData()
|
|
|
self.__InitView()
|
|
|
|
|
|
|
|
|
def __InitData(self):
|
|
|
# 画板尺寸
|
|
|
self.__size = QSize(560, 560)
|
|
|
|
|
|
# 新建QPixmap作为画板,尺寸为__size
|
|
|
self.__board = QPixmap(self.__size)
|
|
|
|
|
|
# 画板背景填充色(默认为白色)
|
|
|
self.__board.fill(Qt.white)
|
|
|
|
|
|
# 画板内容(默认为空)
|
|
|
self.__IsEmpty = True
|
|
|
|
|
|
# 橡皮擦模式(默认关闭)
|
|
|
self.EraserMode = False
|
|
|
|
|
|
# 上一次鼠标位置
|
|
|
self.__lastPos = QPoint(0, 0)
|
|
|
|
|
|
# 当前的鼠标位置
|
|
|
self.__currentPos = QPoint(0, 0)
|
|
|
|
|
|
# 新建绘图工具
|
|
|
self.__painter = QPainter()
|
|
|
|
|
|
# 画笔粗细(默认为 8)
|
|
|
self.__thickness = 20
|
|
|
|
|
|
# 画笔颜色(默认为黑色)
|
|
|
self.__penColor = QColor("black")
|
|
|
|
|
|
# 获取颜色列表
|
|
|
self.__colorList = QColor.colorNames()
|
|
|
|
|
|
|
|
|
def __InitView(self):
|
|
|
# 设置界面的尺寸
|
|
|
self.setFixedSize(self.__size)
|
|
|
|
|
|
|
|
|
def Clear(self):
|
|
|
# 清空画板
|
|
|
self.__board.fill(Qt.white)
|
|
|
self.update()
|
|
|
self.__IsEmpty = True
|
|
|
|
|
|
|
|
|
def ChangePenColor(self, color="black"):
|
|
|
# 改变画笔颜色
|
|
|
self.__penColor = QColor(color)
|
|
|
|
|
|
|
|
|
def ChangePenThickness(self, thickness=20):
|
|
|
# 改变画笔粗细
|
|
|
self.__thickness = thickness
|
|
|
|
|
|
|
|
|
def IsEmpty(self):
|
|
|
# 返回画板是否为空
|
|
|
return self.__IsEmpty
|
|
|
|
|
|
|
|
|
def SetContentFromQPixmap(self, pixmap):
|
|
|
# 设置画板内容为给定的QPixmap
|
|
|
if pixmap.size() != self.__size:
|
|
|
# 如果传入的pixmap尺寸与画板尺寸不一致,则调整其尺寸
|
|
|
self.__board = pixmap.scaled(self.__size)
|
|
|
else:
|
|
|
self.__board = pixmap
|
|
|
self.__IsEmpty = False # 画板不再为空
|
|
|
self.update() # 更新显示
|
|
|
|
|
|
|
|
|
def GetContentAsQImage(self):
|
|
|
# 获取画板内容(返回QImage)
|
|
|
image = self.__board.toImage()
|
|
|
return image
|
|
|
|
|
|
|
|
|
def paintEvent(self, paintEvent):
|
|
|
# 绘图事件
|
|
|
# 绘图时必须使用QPainter的实例,此处为__painter
|
|
|
# 绘图在begin()函数与end()函数间进行
|
|
|
# begin(param)的参数要指定绘图设备,即把图画在哪里
|
|
|
# drawPixmap用于绘制QPixmap类型的对象
|
|
|
self.__painter.begin(self)
|
|
|
|
|
|
# 0,0为绘图的左上角起点的坐标,__board即要绘制的图
|
|
|
self.__painter.drawPixmap(0, 0, self.__board)
|
|
|
self.__painter.end()
|
|
|
|
|
|
|
|
|
def mousePressEvent(self, mouseEvent):
|
|
|
# 鼠标按下时,获取鼠标的当前位置保存为上一次位置
|
|
|
self.__currentPos = mouseEvent.pos()
|
|
|
self.__lastPos = self.__currentPos
|
|
|
|
|
|
|
|
|
def mouseMoveEvent(self, mouseEvent):
|
|
|
# 鼠标移动时,更新当前位置,并在上一个位置和当前位置间画线
|
|
|
self.__currentPos = mouseEvent.pos()
|
|
|
self.__painter.begin(self.__board)
|
|
|
|
|
|
if self.EraserMode == False:
|
|
|
# 非橡皮擦模式
|
|
|
self.__painter.setPen(QPen(self.__penColor, self.__thickness)) # 设置画笔颜色,粗细
|
|
|
else:
|
|
|
# 橡皮擦模式下画笔为纯白色,粗细不变
|
|
|
self.__painter.setPen(QPen(Qt.white, self.__thickness))
|
|
|
|
|
|
# 画线
|
|
|
self.__painter.drawLine(self.__lastPos, self.__currentPos)
|
|
|
self.__painter.end()
|
|
|
self.__lastPos = self.__currentPos
|
|
|
|
|
|
self.update() # 更新显示
|
|
|
|
|
|
|
|
|
def mouseReleaseEvent(self, mouseEvent):
|
|
|
self.__IsEmpty = False # 画板不再为空
|