Last active
August 29, 2015 13:57
-
-
Save rizo/9519871 to your computer and use it in GitHub Desktop.
A snippet with an implementation of arrow graphics item in Qt4.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Arrow(QGraphicsItem): | |
| def __init__(self, parent, position, angle, size = 10.0): | |
| QGraphicsItem.__init__(self) | |
| self._parent = parent | |
| self.setFlag(QGraphicsItem.ItemIsSelectable, False) | |
| self.setFlag(QGraphicsItem.ItemIsMovable, False) | |
| self.position = self.mapToScene(position) | |
| self.angle = angle | |
| self.size = size | |
| self.selected = False | |
| self.inner_offset = size / 2.0 | |
| self.setZValue(20) | |
| self.adjust() | |
| def type(self): | |
| return self._parent.type() | |
| def diagramObject(self): | |
| return self._parent | |
| def parent(self): | |
| return self._parent | |
| def polygon(self): | |
| return self._polygon | |
| def setPolygon(self, polygon): | |
| self._polygon = polygon | |
| def updateParams(self, pos = None, angle = None): | |
| if (pos is None and angle is None): | |
| Debug.error("`Graphics.Items.Arrow`: No new parameters provided!") | |
| if (pos): | |
| self.position = pos | |
| if (angle): | |
| self.angle = angle | |
| self.adjust() | |
| def adjust(self): | |
| "Adjusts the arrow position and angle." | |
| fp = self.position # Arrow's fixing point on the spline. | |
| angle = math.radians(self.angle) # Arrow's direction angle (in radians). | |
| # Arrow's shape points calculation (side points: p1, p2 and inner point: ip). | |
| p1 = fp + \ | |
| QPointF(math.sin(angle + math.pi + math.pi/3) * self.size, | |
| math.cos(angle + math.pi + math.pi/3) * self.size) | |
| p2 = fp + \ | |
| QPointF(math.sin(angle - math.pi/3) * self.size, | |
| math.cos(angle - math.pi/3) * self.size) | |
| ip = fp + \ | |
| QPointF(math.sin(math.pi + math.pi/2 + angle) * self.inner_offset, | |
| math.cos(math.pi + math.pi/2 + angle) * self.inner_offset) | |
| # Create and set the new polygon for the arrow item. | |
| arrow_polygon = QPolygonF([fp, p1, ip, p2]) | |
| self.setPolygon(arrow_polygon) | |
| def paint(self, painter, option, widget): | |
| if (self.polygon() is None): | |
| print self, "- Error: No polygon is set for drawing! Aborting painting." | |
| return | |
| painter.setClipRect(option.exposedRect) | |
| pen = QPen() | |
| pen.setColor(QColor(150,150,150)) | |
| pen.setStyle(Qt.DotLine) | |
| painter.setPen(pen) | |
| painter.setBrush(Qt.black) | |
| painter.drawPolygon(self.polygon()) | |
| def boundingRect(self): | |
| return self.polygon().boundingRect() | |
| def shape(self): | |
| path = QPainterPath() | |
| path.addPolygon(self.polygon()) | |
| return path |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment