class Hints(QGraphicsItem): def __init__(self, x_offset, y_offset): super(Hints, self).__init__() self.x_offset = x_offset self.y_offset = y_offset self.path = None self.setup_display() def setup_display(self, step=0): steps = ["Press Alt: matching commits mode", "Keep pressing Alt and hover over a commit", "That way you can see all the commits that have the same name"] self.path = QPainterPath() self.font = QFont() self.font.setFamily("Helvetica") self.font.setPointSize(FONT_SIZE) self.path.addText( self.x_offset, self.y_offset, self.font, QString(steps[step])) def boundingRect(self): return self.path.boundingRect() def shape(self): return self.path def paint(self, painter, option, widget=None): painter.setPen(Qt.NoPen) painter.setBrush(QBrush(BLACK)) painter.drawPath(self.path)
class DataPipe(QtDeclarative.QDeclarativeItem): def __init__(self, color): super(DataPipe, self).__init__() self.color = color self.sourcePoint = QPoint(0,0,0) self.destinationPoint = QPoint(0,0,0) self.rect = QRectF(0, -20, 30, 40) self.path = QPainterPath() self.change = 1 self.angle = 0 def setSourcePoint(self,srcp): self.sourcePoint = srcp def setDestinationPoint(self,dstp): self.destinationPoint = dstp def boundingRect(self): return self.path.boundingRect() def shape(self): return self.path def paint(self, painter, option, widget=None): painter.setPen(QPen(QColor(0,0,0))) painter.setBrush(QBrush(self.color)) painter.pen().setWidth(4); painter.setBrush(Qt.NoBrush); painter.drawPath(self.path); self.path.moveTo(sourcePoint); self.path.lineTo(self.destinationPoint); #self.path.cubicTo(cp1.x(),cp1.y(),cp2.x(),cp2.y(),destPoint.x(),destPoint.y()); painter.drawPath(self.path); def advance(self, phase): if phase == 0: matrix = self.matrix() matrix.reset() self.setMatrix(matrix) self.angle += self.change * random.random() if self.angle > 4.5: self.change = -1 self.angle -= 0.00001 elif self.angle < -4.5: self.change = 1 self.angle += 0.00001 elif phase == 1: self.rotate(self.angle)
def paint (self, painter, style, widget=None): assert isinstance(painter, QPainter) if self.isSelected(): brush = QBrush(Qt.green) else: brush = QBrush(Qt.white) pen = QPen(Qt.black) circle_path = QPainterPath() circle_path.addEllipse(self.boundingRect()) painter.fillPath(circle_path, brush) painter.strokePath(circle_path, pen) text_path = QPainterPath() text_path.addText(0, 0, QFont(), str(self.node)) box = text_path.boundingRect() text_path.translate(-box.center()) painter.fillPath(text_path, QBrush(Qt.black))
def overlay_for(pt1, pt2, frequency): # Construct the line-geometry, we'll use this to construct the ellipsoid line = QLineF(pt1, pt2) # Determine the radius for the ellipsoid radius = fresnel_radius(line.length(), frequency) # Draw the ellipsoid zone = QPainterPath() zone.addEllipse(QPointF(0., 0.), line.length() / 2, radius) # Rotate the ellipsoid - same angle as the line transform = QTransform() transform.rotate(-line.angle()) zone = transform.map(zone) # Center the zone over the line lc = QRectF(pt1, pt2).center() zc = zone.boundingRect().center() zone.translate(lc.x() - zc.x(), lc.y() - zc.y()) return line, zone
class Arrow(QGraphicsObject, QGraphicsItem): def __init__(self, x_offset, y_offset, parent=None): super(Arrow, self).__init__(parent=parent) self._parent = parent self.color = BLACK self.path = QPainterPath() self.setAcceptDrops(True) self.setAcceptHoverEvents(True) self.x_offset = x_offset self.y_offset = y_offset # self.rect = QRectF(x_offset + ARROW_BASE_X, # y_offset - ARROW_HEIGHT, # ARROW_WIDTH, ARROW_HEIGHT) # self.path.addRect(self.rect) self.setup_display() def setup_display(self): polygon = QPolygonF( [QPointF(self.x_offset + ARROW_BASE_X, COMMIT_HEIGHT + ARROW_HEIGHT + self.y_offset), QPointF(self.x_offset + ARROW_BASE_X, COMMIT_HEIGHT + ARROW_HEIGHT + self.y_offset - 20), QPointF(self.x_offset + ARROW_BASE_X - ARROW_TIP_WIDTH / 2, COMMIT_HEIGHT + ARROW_HEIGHT + self.y_offset - 19), QPointF(self.x_offset + ARROW_BASE_X + ARROW_BASE_WIDTH / 2, COMMIT_HEIGHT + ARROW_HEIGHT + self.y_offset - ARROW_HEIGHT), QPointF(self.x_offset + ARROW_BASE_X + ARROW_BASE_WIDTH + ARROW_TIP_WIDTH / 2, COMMIT_HEIGHT + ARROW_HEIGHT + self.y_offset - 19), QPointF(self.x_offset + ARROW_BASE_X + ARROW_BASE_WIDTH, COMMIT_HEIGHT + ARROW_HEIGHT + self.y_offset - 20), QPointF(self.x_offset + ARROW_BASE_X + ARROW_BASE_WIDTH, COMMIT_HEIGHT + ARROW_HEIGHT + self.y_offset), ] ) self.path.addPolygon(polygon) # def hoverMoveEvent(self, event): # print "hover arrow move" # # def hoverEnterEvent(self, event): # print "hover arrow enter" # # def hoverLeaveEvent(self, event): # print "hover arrow leave" def dragEnterEvent(self, event): print "Enter" def dragLeaveEvent(self, event): print "Leave" def dragMoveEvent(self, event): print "Move" def dropEvent(self, event): # print dir(event.source()) # print dir(event) # print dir(event.mimeData()) # First string is commit hash, second is the branch self._parent.emit(SIGNAL("commitItemInserted(QString*, QString*)"), QString(event.mimeData().text()), self._parent.branch) print "Drop" def paint(self, painter, option, widget=None): painter.setPen(Qt.NoPen) painter.setBrush(QBrush(self.color)) painter.drawPath(self.path) def boundingRect(self): return self.path.boundingRect() def shape(self): return self.path