-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQDrawWidget.py
67 lines (53 loc) · 2.09 KB
/
QDrawWidget.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
from PyQt5 import QtWidgets, QtCore, QtGui
class QDrawWidget(QtWidgets.QWidget):
def __init__(self, width=800, height=800):
super().__init__()
self.resize(width, height)
self.setFocusPolicy(QtCore.Qt.StrongFocus)
self.drawing = False
self.grid = True
self.points = []
self.setMouseTracking(True) # only get events when button is pressed
self.initUI()
def initUI(self):
self.setWindowTitle('Drawable')
self.show()
def mousePressEvent(self, ev):
if ev.button() == QtCore.Qt.LeftButton:
self.drawing = True
self.points = []
self.update()
elif ev.button() == QtCore.Qt.RightButton:
try:
print("rechtsklick")
#self.points = custom_filter(self.points) # needs to be implemented outside!
except NameError:
pass
self.update()
def mouseReleaseEvent(self, ev):
if ev.button() == QtCore.Qt.LeftButton:
self.drawing = False
self.update()
def mouseMoveEvent(self, ev):
if self.drawing:
self.points.append((ev.x(), ev.y()))
self.update()
def poly(self, pts):
return QtGui.QPolygonF(map(lambda p: QtCore.QPointF(*p), pts))
def paintEvent(self, ev):
qp = QtGui.QPainter()
qp.begin(self)
qp.setBrush(QtGui.QColor(0, 0, 0))
qp.drawRect(ev.rect())
qp.setBrush(QtGui.QColor(20, 255, 190))
qp.setPen(QtGui.QColor(0, 155, 0))
qp.drawPolyline(self.poly(self.points))
for point in self.points:
qp.drawEllipse(point[0]-1, point[1] - 1, 2, 2)
if self.grid:
qp.setPen(QtGui.QColor(255, 100, 100, 20)) # semi-transparent
for x in range(0, self.width(), 20):
qp.drawLine(x, 0, x, self.height())
for y in range(0, self.height(), 20):
qp.drawLine(0, y, self.width(), y)
qp.end()