forked from rameshbabuvfx/smart_review_tool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdrawingWidget.py
161 lines (138 loc) · 4.52 KB
/
drawingWidget.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
from PySide2.QtWidgets import *
from PySide2.QtCore import *
from PySide2.QtGui import *
class DrawingWidget(QWidget):
def __init__(self):
"""
Init method.
"""
super(DrawingWidget, self).__init__()
self.label = QLabel(self)
self.image = QPixmap(800, 600)
self.image.fill(Qt.transparent)
self.label.setPixmap(self.image)
self.label.resize(self.image.width(), self.image.height())
self.setCursor(QCursor(Qt.CrossCursor))
self.paint_image = QPixmap(self.image.size())
self.paint_image.fill(Qt.transparent)
self.last_point = None
self.pen_color = "#000000"
self.pen_size = 3
self.eraser = False
self.eraser_size = 50
self.paint_label = QLabel(self)
self.paint_label.setGeometry(self.label.geometry())
def set_image_label(self, image_path):
"""
Sets image in image label.
:param Str image_path: Path of the image.
:return: None.
"""
self.image = image_path
self.paint_image = QPixmap(self.image.size())
self.paint_image.fill(Qt.transparent)
self.label.setPixmap(self.image)
self.label.resize(self.image.width(), self.image.height())
self.paint_label.setPixmap(self.paint_image)
self.paint_label.setGeometry(self.label.rect())
self.setMinimumSize(self.image.size())
self.update()
def set_pen_color(self, color):
"""
Sets the pen color.
:param Str color: Color value in rgb.
:return: None.
"""
self.pen_color = color
def set_pen_size(self, size):
"""
Sets pen size.
:param Int size: Size of pen.
:return: None.
"""
self.pen_size = size
def mousePressEvent(self, event):
"""
Enables drawing mode.
:param event: Event.
:return: None.
"""
if event.button() == Qt.LeftButton:
self.last_point = self.label.mapFromParent(event.pos())
self.drawing = True
else:
super().mousePressEvent(event)
def mouseMoveEvent(self, event):
"""
Draws and Erases the paint strokes on image.
:param event: Event.
:return: None.
"""
if self.drawing:
end_point = self.label.mapFromParent(event.pos())
painter = QPainter(self.paint_image)
painter.setRenderHint(QPainter.Antialiasing)
painter.setPen(
QPen(
QColor(self.pen_color),
self.pen_size,
Qt.SolidLine,
Qt.RoundCap,
Qt.RoundJoin
)
)
if self.eraser:
rect = QRect(QPoint(), self.eraser_size * QSize())
rect.moveCenter(event.pos())
painter.save()
painter.setCompositionMode(QPainter.CompositionMode_Clear)
painter.eraseRect(rect)
painter.restore()
else:
line = QLine(self.last_point, end_point)
painter.drawLine(line)
self.last_point = end_point
self.update()
def mouseReleaseEvent(self, event):
"""
Disables the drawing mode.
:param event: Event.
:return: None.
"""
if event.button() == Qt.LeftButton:
self.last_point = None
self.drawing = False
else:
super().mouseReleaseEvent(event)
def paintEvent(self, event):
"""
update the pixmap label.
:param event: Event.
:return: None.
"""
self.paint_label.setPixmap(self.paint_image)
def change_mode(self, eraser):
"""
Changes the cursor as per mode.
:param Bool eraser: status of eraser
:return: None.
"""
self.eraser = eraser
if self.eraser:
pixmap = QPixmap(QSize(1, 1) * self.eraser_size)
pixmap.fill(Qt.transparent)
painter = QPainter(pixmap)
painter.setPen(QPen(Qt.black, 2))
painter.setBrush(QBrush(Qt.DiagCrossPattern))
painter.drawRect(pixmap.rect())
painter.end()
cursor = QCursor(pixmap)
self.setCursor(cursor)
else:
self.setCursor(QCursor(Qt.CrossCursor))
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
tool = DrawingWidget()
tool.show()
sys.exit(app.exec_())