def createPoly(self, n_points, radius, center_x, center_y): polygon = QPolygonF() w = 360 / n_points # angle per step for i in range(n_points): # add the points of polygon t = w * i x = radius * np.cos(np.deg2rad(t)) y = radius * np.sin(np.deg2rad(t)) polygon.append(QtCore.QPointF(center_x + x, center_y + y)) return polygon
class StarRating(object): """ Handle the actual painting of the stars themselves. """ def __init__(self, starCount=1, maxStarCount=5): self.starCount = starCount self.maxStarCount = maxStarCount # Create the star shape we'll be drawing. self.starPolygon = QPolygonF() self.starPolygon.append(QPointF(1.0, 0.5)) for i in range(1, 5): self.starPolygon.append(QPointF(0.5 + 0.5 * cos(0.8 * i * pi), 0.5 + 0.5 * sin(0.8 * i * pi))) # Create the diamond shape we'll show in the editor self.diamondPolygon = QPolygonF() diamondPoints = [QPointF(0.4, 0.5), QPointF(0.5, 0.4), QPointF(0.6, 0.5), QPointF(0.5, 0.6), QPointF(0.4, 0.5)] self.diamondPolygon.append(diamondPoints) def sizeHint(self): """ Tell the caller how big we are. """ return PAINTING_SCALE_FACTOR * QSize(self.maxStarCount, 1) def paint(self, painter, rect, palette, isEditable=False): """ Paint the stars (and/or diamonds if we're in editing mode). """ painter.save() painter.setRenderHint(QPainter.Antialiasing, True) painter.setPen(Qt.NoPen) if isEditable: painter.setBrush(palette.highlight()) else: painter.setBrush(palette.windowText()) yOffset = (rect.height() - PAINTING_SCALE_FACTOR) / 2 painter.translate(rect.x(), rect.y() + yOffset) painter.scale(PAINTING_SCALE_FACTOR, PAINTING_SCALE_FACTOR) for i in range(self.maxStarCount): if i < self.starCount: painter.drawPolygon(self.starPolygon, Qt.WindingFill) elif isEditable: painter.drawPolygon(self.diamondPolygon, Qt.WindingFill) painter.translate(1.0, 0.0) painter.restore()