Attachment 'clipper.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 painter.setClipRect(self.clip_rect)
45 painter.drawPolyline(self.polygon)
46 painter.setBrush(QBrush(Qt.blue))
47 painter.drawPath(self.path)
48 painter.end()
49
50 def corner(self, number):
51
52 if number == 0:
53 return QRect(self.clip_rect.topLeft() - self.handle_offsets[0], QSize(8, 8))
54 elif number == 1:
55 return QRect(self.clip_rect.topRight() - self.handle_offsets[1], QSize(8, 8))
56 elif number == 2:
57 return QRect(self.clip_rect.bottomLeft() - self.handle_offsets[2], QSize(8, 8))
58 elif number == 3:
59 return QRect(self.clip_rect.bottomRight() - self.handle_offsets[3], QSize(8, 8))
60
61 def mousePressEvent(self, event):
62
63 for i in range(4):
64 rect = self.corner(i)
65 if rect.contains(event.pos()):
66 self.dragging = i
67 self.drag_offset = rect.topLeft() - event.pos()
68 break
69 else:
70 self.dragging = None
71
72 def mouseMoveEvent(self, event):
73
74 if self.dragging is None:
75 return
76
77 left = self.largest_rect.left()
78 right = self.largest_rect.right()
79 top = self.largest_rect.top()
80 bottom = self.largest_rect.bottom()
81
82 point = event.pos() + self.drag_offset + self.handle_offsets[self.dragging]
83 point.setX(max(left, min(point.x(), right)))
84 point.setY(max(top, min(point.y(), bottom)))
85
86 if self.dragging == 0:
87 self.clip_rect.setTopLeft(point)
88 elif self.dragging == 1:
89 self.clip_rect.setTopRight(point)
90 elif self.dragging == 2:
91 self.clip_rect.setBottomLeft(point)
92 elif self.dragging == 3:
93 self.clip_rect.setBottomRight(point)
94
95 self.update()
96
97 def mouseReleaseEvent(self, event):
98
99 self.dragging = None
100
101 def sizeHint(self):
102 return QSize(500, 500)
103
104
105 if __name__ == "__main__":
106
107 app = QApplication(sys.argv)
108 window = Window()
109 window.show()
110 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.