-
Notifications
You must be signed in to change notification settings - Fork 0
/
canvas.py
43 lines (33 loc) · 1.2 KB
/
canvas.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
from PySide import QtGui, QtCore
from draw_line import DrawLine
class Canvas(QtGui.QWidget):
def __init__(self):
super(Canvas, self).__init__()
self.currentImg = None
self.buf = None # buffer is what repaint use
self.paintTool = DrawLine()
self.current_location = QtCore.QPoint(20, 40)
self.initUI()
self.repaint()
def initUI(self):
hbox = QtGui.QHBoxLayout(self)
self.currentImg = QtGui.QPixmap('lenna.tif')
self.buf = self.currentImg.copy()
self.setLayout(hbox)
self.setGeometry(300, 300, 280, 170)
self.setWindowTitle('Lenna')
def mousePressEvent(self, e):
self.paintTool.begin(self.buf, e.pos())
def mouseMoveEvent(self, e):
if self.paintTool.isBegin:
self.buf = self.paintTool.process(e.pos())
self.repaint()
def mouseReleaseEvent(self, e):
self.paintTool.end()
# always separate buffer and currentImg
self.currentImg = self.buf.copy()
self.repaint()
def paintEvent(self, e):
if self.buf is not None:
p = QtGui.QPainter(self)
p.drawPixmap(self.buf.rect(), self.buf, self.buf.rect())