Attachment 'clipper_path.py'
Download 1 #!/usr/bin/env python
2
3 import sys
4 from PyQt4.QtCore import *
5 from PyQt4.QtGui import *
6
7 class Window(QWidget):
8
9 def __init__(self):
10
11 QWidget.__init__(self)
12 self.largest_rect = QRect(50, 50, 400, 400)
13
14 self.clip_rect = QRect(50, 50, 400, 400)
15 self.dragging = None
16 self.drag_offset = QPoint()
17 self.handle_offsets = (
18 QPoint(8, 8), QPoint(-1, 8), QPoint(8, -1), QPoint(-1, -1)
19 )
20
21 self.path = QPainterPath()
22 self.path.moveTo(100, 250)
23 font = QFont()
24 font.setPixelSize(80)
25 self.path.addText(100, 300, font, "Clipping")
26
27 self.polygon = QPolygon([QPoint(250, 100), QPoint(400, 250),
28 QPoint(250, 400), QPoint(100, 250),
29 QPoint(250, 100)])
30
31 def paintEvent(self, event):
32
33 painter = QPainter()
34 painter.begin(self)
35 painter.fillRect(event.rect(), QBrush(Qt.white))
36 painter.setRenderHint(QPainter.Antialiasing)
37 painter.setPen(QPen(QBrush(Qt.red), 1, Qt.DashLine))
38 painter.drawRect(self.largest_rect)
39 painter.setPen(QPen(Qt.black))
40 painter.drawRect(self.clip_rect)
41 for i in range(4):
42 painter.drawRect(self.corner(i))
43
44 path = QPainterPath()
45 path.addRect(QRectF(self.clip_rect))
46 polygon_path = QPainterPath()
47 polygon_path.addPolygon(QPolygonF(self.polygon))
48 painter.drawPath(path.intersected(polygon_path))
49 painter.setBrush(QBrush(Qt.blue))
50 painter.drawPath(path.intersected(self.path))
51 painter.end()
52
53 def corner(self, number):
54
55 if number == 0:
56 return QRect(self.clip_rect.topLeft() - self.handle_offsets[0], QSize(8, 8))
57 elif number == 1:
58 return QRect(self.clip_rect.topRight() - self.handle_offsets[1], QSize(8, 8))
59 elif number == 2:
60 return QRect(self.clip_rect.bottomLeft() - self.handle_offsets[2], QSize(8, 8))
61 elif number == 3:
62 return QRect(self.clip_rect.bottomRight() - self.handle_offsets[3], QSize(8, 8))
63
64 def mousePressEvent(self, event):
65
66 for i in range(4):
67 rect = self.corner(i)
68 if rect.contains(event.pos()):
69 self.dragging = i
70 self.drag_offset = rect.topLeft() - event.pos()
71 break
72 else:
73 self.dragging = None
74
75 def mouseMoveEvent(self, event):
76
77 if self.dragging is None:
78 return
79
80 left = self.largest_rect.left()
81 right = self.largest_rect.right()
82 top = self.largest_rect.top()
83 bottom = self.largest_rect.bottom()
84
85 point = event.pos() + self.drag_offset + self.handle_offsets[self.dragging]
86 point.setX(max(left, min(point.x(), right)))
87 point.setY(max(top, min(point.y(), bottom)))
88
89 if self.dragging == 0:
90 self.clip_rect.setTopLeft(point)
91 elif self.dragging == 1:
92 self.clip_rect.setTopRight(point)
93 elif self.dragging == 2:
94 self.clip_rect.setBottomLeft(point)
95 elif self.dragging == 3:
96 self.clip_rect.setBottomRight(point)
97
98 self.update()
99
100 def mouseReleaseEvent(self, event):
101
102 self.dragging = None
103
104 def sizeHint(self):
105 return QSize(500, 500)
106
107
108 if __name__ == "__main__":
109
110 app = QApplication(sys.argv)
111 window = Window()
112 window.show()
113 sys.exit(app.exec_())
Attached Files
To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.You are not allowed to attach a file to this page.