-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathQDrawWidget.py
83 lines (66 loc) · 2.64 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
"""
Original draw widget from GRIPS was slightly adjusted: constructor call updated so it could be promoted, some setters
and getters defined for cleaner access to the data.
"""
import sys
from PyQt5 import QtCore, QtWidgets, QtGui
class QDrawWidget(QtWidgets.QWidget):
# constructor and super call had to be slightly adjusted so this widget could be used as a "Promote to" - option
def __init__(self, *args, **kwargs):
super(QDrawWidget, self).__init__(*args, **kwargs)
# self.resize(800, 800)
self.setFocusPolicy(QtCore.Qt.StrongFocus)
self.custom_filter = None
self.drawing = False
self.grid = True
self.points = []
self.setMouseTracking(True) # only get events when button is pressed
self.setWindowTitle('Drawable')
def set_custom_filter(self, point_filter):
self.custom_filter = point_filter
def get_current_points(self):
return self.points
def reset_current_points(self):
self.points = []
def mousePressEvent(self, ev):
if ev.button() == QtCore.Qt.LeftButton:
self.drawing = True
self.points = []
self.update()
elif ev.button() == QtCore.Qt.RightButton:
try:
if self.custom_filter:
# custom_filter needs to be implemented outside!
self.points = self.custom_filter(self.points)
else:
sys.stderr.write("\nCustom filter for the draw widget isn't set!")
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()