class AnimationSpinner(QLabel): def __init__(self, parent=None): QLabel.__init__(self, parent) brightness = parent.palette().color(QPalette.Window).valueF() self._bw = brightness < 0.5 and 1 or 0 self._steps = 12 self._setup() self._isRunning = False self.animationTimer = None def _setup(self): steps = self._steps anglestep = 360. / steps fillstep = 0.6 / (steps - 1) self._fillsteps = [0.71 - i * fillstep for i in range(steps)] self._coords = [coordinates(8, 8, 6, anglestep*i) for i in range(steps)] self._path = QPainterPath() self._path.addRoundedRect(0, 0, 4, 2, 1, 1) def start(self): self.animationTimer = QTimer(self) self.connect(self.animationTimer, SIGNAL("timeout()"), self.run) self.animationTimer.start(35) self._isRunning = True def stop(self): if self.animationTimer is not None: self.animationTimer.stop() self.animationTimer = None self._isRunning = False self.repaint() def run(self): self.repaint() self._fillsteps = self._fillsteps[1:] + [self._fillsteps[0]] def paintEvent(self, event): if self._isRunning: anglestep = 360. / self._steps fillsteps = self._fillsteps factor = min(self.width(), self.height()) / 16. bw = self._bw p = QPainter(self) p.setRenderHint(QPainter.Antialiasing, True) p.scale(factor, factor) p.setPen(Qt.NoPen) for i in range(self._steps): x1, y1 = self._coords[i] c = fillsteps[self._steps - 1 - i] a = anglestep * i p.setBrush(QBrush(QColor.fromRgbF(bw, bw, bw, c))) p.save() p.translate(x1 - 2, y1 - 1) p.translate(2, 1) p.rotate(a) p.translate(-2, -1) p.drawPath(self._path) p.restore()
def setup_display(self, x_offset, y_offset): path = QPainterPath() self.rect = QRectF(x_offset + 0, y_offset + 0, COMMIT_WIDTH, COMMIT_HEIGHT) path.addRoundedRect(self.rect, 10, 10) self.font = QFont() self.font.setFamily("Helvetica") self.font.setPointSize(FONT_SIZE) path.addText( x_offset + (COMMIT_WIDTH - len(self.commit.name()) * (FONT_SIZE - 4)) / 2 + 4, y_offset + (COMMIT_HEIGHT + FONT_SIZE) / 2 , self.font, QString(self.commit.name())) return path
def path_from_graphics(graphics): """ Return a constructed `QPainterPath` for a KEGG pathway graphics element. """ path = QPainterPath() x, y, w, h = [int(graphics.get(c, 0)) for c in ["x", "y", "width", "height"]] type = graphics.get("type", "rectangle") if type == "rectangle": path.addRect(QRectF(x - w / 2, y - h / 2, w, h)) elif type == "roundrectangle": path.addRoundedRect(QRectF(x - w / 2, y - h / 2, w, h), 10, 10) elif type == "circle": path.addEllipse(QRectF(x - w / 2, y - h / 2, w, h)) else: ValueError("Unknown graphics type %r." % type) return path
def path_from_graphics(graphics): """ Return a constructed `QPainterPath` for a KEGG pathway graphics element. """ path = QPainterPath() x, y, w, h = [int(graphics.get(c, 0)) for c in ["x", "y", "width", "height"]] type = graphics.get("type", "rectangle") if type == "rectangle": path.addRect(QRectF(x - w / 2, y - h / 2, w, h)) elif type == "roundrectangle": path.addRoundedRect(QRectF(x - w / 2, y - h / 2, w, h), 10, 10) elif type == "circle": path.addEllipse(QRectF(x - w / 2, y - h / 2, w, h)) else: ValueError("Unknown graphics type %r." % type) return path
def paintEvent(self, event): painter = QPainter(self) btnRect = self.geometry() iconRect = self.iconSize() color = QColor(Qt.black) if self.hovered: color = self.color if self.pressed: color = self.color.darker(120) painter.setPen(QPen(QColor(Qt.lightGray), 2)) outline = QPainterPath() outline.addRoundedRect(0, 0, btnRect.width(), btnRect.height(), 0, 0) painter.setOpacity(1) painter.drawPath(outline) painter.setBrush(QBrush(color)) painter.setOpacity(self.opacity) painter_path = QPainterPath() painter_path.addRoundedRect(1, 1, btnRect.width() - 2, btnRect.height() - 2, 0, 0) if self.hovered: painter.setClipPath(painter_path) painter.drawRoundedRect(1, 1, btnRect.width() - 2, btnRect.height() - 2, 0, 0) painter.setOpacity(1) iconPos, textPos = self.calIconTextPos(btnRect, iconRect) # 重画文本 if not self.text().isNull(): painter.setFont(self.font()) painter.setPen(QPen(QColor(Qt.black), 2)) painter.drawText(textPos.x(), textPos.y(), textPos.width(), textPos.height(), Qt.AlignCenter, self.text()) # 重画图标 if not self.icon().isNull(): painter.drawPixmap(iconPos, QPixmap(self.icon().pixmap(self.iconSize())))
def pixmap(self, mode=QIcon.Normal, state=QIcon.Off): pixmap = self.icon().pixmap(self.iconSize(), mode, state) if pixmap.isNull(): return pixmap size = max(pixmap.width(), pixmap.height()) offset_x = (size - pixmap.width())/2 offset_y = (size - pixmap.height())/2 new_pixmap = QPixmap(size, size) new_pixmap.fill(Qt.transparent) path = QPainterPath() path.addRoundedRect(0, 0, size, size, 3.7, 3.7) painter = QPainter(new_pixmap) painter.setRenderHint(QPainter.Antialiasing, True) painter.setCompositionMode(QPainter.CompositionMode_SourceOver) painter.setClipPath(path) painter.drawPixmap(offset_x, offset_y, pixmap) painter.end() return new_pixmap
def paintEvent(self,event): painter = QPainter(self) btnRect = self.geometry() color = QColor(Qt.black) if self.hovered: color = self.color if self.pressed: color = self.color.darker(120) painter.setBrush(QBrush(color)) painter_path = QPainterPath() painter_path.addRoundedRect(1, 1, btnRect.width() - 2, btnRect.height() - 2, 0, 0) if self.hovered: painter.setPen(QPen(color,2)) outline = QPainterPath() outline.addRoundedRect(0, 0, btnRect.width(), btnRect.height(), 0, 0) painter.setOpacity(1) painter.drawPath(outline) painter.setClipPath(painter_path) painter.drawRoundedRect(1, 1, btnRect.width() - 2, btnRect.height() - 2, 0, 0) iconWidth = self.iconSize().width()*3/5 iconHeight = self.iconSize().height()*3/5 iconX = (btnRect.width()-iconWidth)/2 iconY = (btnRect.height()-iconHeight)/2 if self.pressed: iconX += 2 iconY += 2 iconPos = QRect() iconPos.setX(iconX) iconPos.setY(iconY) iconPos.setWidth(iconWidth) iconPos.setHeight(iconHeight) painter.drawPixmap(iconPos, QPixmap(self.icon().pixmap(self.iconSize())))
def paintEvent(self,event): return painter = QPainter(self) btnRect = self.geometry() iconRect = self.iconSize() color = QColor(Qt.black) if self.hovered: color = self.color if self.pressed: color = self.color.darker(120) painter.setPen(QPen(QColor(Qt.lightGray),2)) outline = QPainterPath() outline.addRoundedRect(0, 0, btnRect.width(), btnRect.height(), 0, 0) painter.setOpacity(1) painter.drawPath(outline) painter.setBrush(QBrush(color)) painter.setOpacity(self.opacity) painter_path = QPainterPath() painter_path.addRoundedRect(1, 1, btnRect.width() - 2, btnRect.height() - 2, 0, 0) if self.hovered: painter.setClipPath(painter_path) painter.drawRoundedRect(1, 1, btnRect.width() - 2, btnRect.height() - 2, 0, 0) painter.setOpacity(1) iconPos,textPos = self.calIconTextPos(btnRect, iconRect) # 重画文本 if not self.text().isNull(): painter.setFont(self.font()) painter.setPen(QPen(QColor(Qt.black),2)) painter.drawText(textPos.x(), textPos.y(), textPos.width(), textPos.height(), Qt.AlignCenter, self.text()) # 重画图标 if not self.icon().isNull(): painter.drawPixmap(iconPos, QPixmap(self.icon().pixmap(self.iconSize())))
class AnimationSpinner(QLabel): def __init__(self, parent=None): QLabel.__init__(self, parent) brightness = parent.palette().color(QPalette.Window).valueF() self._bw = brightness < 0.5 and 1 or 0 self._steps = 12 self._setup() self._isRunning = False self.animationTimer = None def _setup(self): steps = self._steps anglestep = 360. / steps fillstep = 0.6 / (steps - 1) self._fillsteps = [0.71 - i * fillstep for i in range(steps)] self._coords = [ coordinates(8, 8, 6, anglestep * i) for i in range(steps) ] self._path = QPainterPath() self._path.addRoundedRect(0, 0, 4, 2, 1, 1) def start(self): self.animationTimer = QTimer(self) self.connect(self.animationTimer, SIGNAL("timeout()"), self.run) self.animationTimer.start(35) self._isRunning = True def stop(self): if self.animationTimer is not None: self.animationTimer.stop() self.animationTimer = None self._isRunning = False self.repaint() def run(self): self.repaint() self._fillsteps = self._fillsteps[1:] + [self._fillsteps[0]] def paintEvent(self, event): if self._isRunning: anglestep = 360. / self._steps fillsteps = self._fillsteps factor = min(self.width(), self.height()) / 16. bw = self._bw p = QPainter(self) p.setRenderHint(QPainter.Antialiasing, True) p.scale(factor, factor) p.setPen(Qt.NoPen) for i in range(self._steps): x1, y1 = self._coords[i] c = fillsteps[self._steps - 1 - i] a = anglestep * i p.setBrush(QBrush(QColor.fromRgbF(bw, bw, bw, c))) p.save() p.translate(x1 - 2, y1 - 1) p.translate(2, 1) p.rotate(a) p.translate(-2, -1) p.drawPath(self._path) p.restore()