-
Notifications
You must be signed in to change notification settings - Fork 0
/
mouse_drag.py
116 lines (89 loc) · 3.26 KB
/
mouse_drag.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
# Copied from: https://www.pythonguis.com/faq/use-mouse-drag-to-change-the-width-of-a-rectangle/
import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
FREE_STATE = 1
BUILDING_SQUARE = 2
BEGIN_SIDE_EDIT = 3
END_SIDE_EDIT = 4
CURSOR_ON_BEGIN_SIDE = 1
CURSOR_ON_END_SIDE = 2
class MyWidget(QLabel):
sig_mouse_released = pyqtSignal(int, int, int, int)
def __init__(self):
super().__init__()
self.begin = QPoint()
self.end = QPoint()
self.state = FREE_STATE
self.setMouseTracking(True)
self.free_cursor_on_side = 0
self.activate_selection = False
def paintEvent(self, event):
super().paintEvent(event)
qp = QPainter(self)
br = QBrush(QColor(100, 10, 10, 40))
qp.setBrush(br)
qp.drawRect(QRect(self.begin, self.end))
if not self.free_cursor_on_side:
return
qp.setPen(QPen(Qt.black, 5, Qt.DashLine))
if self.free_cursor_on_side == CURSOR_ON_BEGIN_SIDE:
end = QPoint(self.end)
end.setX(self.begin.x())
qp.drawLine(self.begin, end)
elif self.free_cursor_on_side == CURSOR_ON_END_SIDE:
begin = QPoint(self.begin)
begin.setX(self.end.x())
qp.drawLine(self.end, begin)
def cursor_on_side(self, e_pos) -> int:
if not self.begin.isNull() and not self.end.isNull():
y1, y2 = sorted([self.begin.y(), self.end.y()])
if y1 <= e_pos.y() <= y2:
# 5 resolution, more easy to pick than 1px
if abs(self.begin.x() - e_pos.x()) <= 5:
return CURSOR_ON_BEGIN_SIDE
elif abs(self.end.x() - e_pos.x()) <= 5:
return CURSOR_ON_END_SIDE
return 0
def mousePressEvent(self, event):
if not self.activate_selection:
return
side = self.cursor_on_side(event.pos())
if side == CURSOR_ON_BEGIN_SIDE:
self.state = BEGIN_SIDE_EDIT
elif side == CURSOR_ON_END_SIDE:
self.state = END_SIDE_EDIT
else:
self.state = BUILDING_SQUARE
self.begin = event.pos()
self.end = event.pos()
self.update()
def applye_event(self, event):
if self.state == BUILDING_SQUARE:
self.end = event.pos()
elif self.state == BEGIN_SIDE_EDIT:
self.begin.setX(event.x())
elif self.state == END_SIDE_EDIT:
self.end.setX(event.x())
def mouseMoveEvent(self, event):
if self.state == FREE_STATE:
self.free_cursor_on_side = self.cursor_on_side(event.pos())
if self.free_cursor_on_side:
self.setCursor(Qt.SizeHorCursor)
else:
self.unsetCursor()
self.update()
else:
self.applye_event(event)
self.update()
def mouseReleaseEvent(self, event):
self.applye_event(event)
self.state = FREE_STATE
self.sig_mouse_released.emit(self.begin.x(), self.begin.y(), self.end.x(), self.end.y())
if __name__ == '__main__':
app = QApplication(sys.argv)
window = MyWidget()
window.show()
app.aboutToQuit.connect(app.deleteLater)
sys.exit(app.exec_())