class BoxItemObject(QGraphicsObject): # As per the ArrowItemObject, except animate the opacity of the box. def __init__(self, rect): super(BoxItemObject, self).__init__() self.bi = BoxItem(rect, self) self.anim = QPropertyAnimation(self, b"opacity") def flash_undo(self): # translucent -> opaque -> clear. self.anim.setDuration(200) self.anim.setStartValue(16) self.anim.setKeyValueAt(0.5, 192) self.anim.setEndValue(0) self.anim.start() def flash_redo(self): # translucent -> opaque -> translucent. self.anim.setDuration(200) self.anim.setStartValue(16) self.anim.setKeyValueAt(0.5, 64) self.anim.setEndValue(16) self.anim.start() @pyqtProperty(int) def opacity(self): return self.bi.brush().color().alpha() @opacity.setter def opacity(self, value): self.bi.setBrush(QBrush(QColor(255, 255, 0, value)))
class EllipseItemObject(QGraphicsObject): # As per the ArrowItemObject - animate thickness of boundary def __init__(self, rect): super(EllipseItemObject, self).__init__() self.ei = EllipseItem(rect, self) self.anim = QPropertyAnimation(self, b"thickness") def flash_undo(self): self.anim.setDuration(200) self.anim.setStartValue(2) self.anim.setKeyValueAt(0.5, 8) self.anim.setEndValue(0) self.anim.start() def flash_redo(self): self.anim.setDuration(200) self.anim.setStartValue(2) self.anim.setKeyValueAt(0.5, 6) self.anim.setEndValue(2) self.anim.start() @pyqtProperty(int) def thickness(self): return self.ei.pen().width() @thickness.setter def thickness(self, value): self.ei.setPen(QPen(Qt.red, value))
class HighLightItemObject(QGraphicsObject): # As per the ArrowItemObject except animate the opacity of # the highlighter path def __init__(self, path): super(HighLightItemObject, self).__init__() self.hli = HighLightItem(path, self) self.anim = QPropertyAnimation(self, b"opacity") def flash_undo(self): self.anim.setDuration(200) self.anim.setStartValue(64) self.anim.setKeyValueAt(0.5, 192) self.anim.setEndValue(0) self.anim.start() def flash_redo(self): self.anim.setDuration(200) self.anim.setStartValue(64) self.anim.setKeyValueAt(0.5, 96) self.anim.setEndValue(64) self.anim.start() @pyqtProperty(int) def opacity(self): return self.hli.pen().color().alpha() @opacity.setter def opacity(self, value): self.hli.setPen(QPen(QColor(255, 255, 0, value), 50))
class Demo(QWidget): def __init__(self): super(Demo, self).__init__() self.resize(600, 600) self.btn = QPushButton('Bigger', self) self.btn.setObjectName('btn') self.btn.resize(100, 100) self.animation = QPropertyAnimation(self) # 指定动画作用的对象 self.animation.setTargetObject(self.btn) # b'size' 改变大小 b'pos' 改变位置 self.animation.setPropertyName(b'pos') # 设置动画持续时长 self.animation.setDuration(1000) # 设置位置关键帧 self.animation.setKeyValueAt(0, self.btn.pos() + QPoint(0, 0)) self.animation.setKeyValueAt(0.2, self.btn.pos() + QPoint(0, 15)) self.animation.setKeyValueAt(0.4, self.btn.pos() + QPoint(0, 0)) self.animation.setKeyValueAt(0.6, self.btn.pos() + QPoint(0, -15)) self.animation.setKeyValueAt(0.8, self.btn.pos() + QPoint(0, 0)) # self.animation.setKeyValueAt(1, self.btn.pos() + QPoint(0, 0)) # 设置动画循环次数 self.animation.setLoopCount(3) QMetaObject.connectSlotsByName(self) @pyqtSlot() def on_btn_clicked(self): # 启动动画 self.animation.start()
class DeleteObject(QGraphicsObject): def __init__(self, shape, fill=False): super().__init__() self.item = DeleteItem(shape, fill=fill, parent=self) self.anim_thick = QPropertyAnimation(self, b"thickness") self.anim_thick.setDuration(200) def flash_undo(self): """Undo animation: thin -> thick -> none.""" self.anim_thick.setStartValue(2) self.anim_thick.setKeyValueAt(0.5, 8) self.anim_thick.setEndValue(0) self.anim_thick.start() def flash_redo(self): """Redo animation: thin -> med -> thin.""" self.anim_thick.setStartValue(0) self.anim_thick.setKeyValueAt(0.5, 8) self.anim_thick.setEndValue(2) self.anim_thick.start() @pyqtProperty(float) def thickness(self): return self.item.thickness @thickness.setter def thickness(self, value): self.item.restyle(value)
def start(self): height = self.height() width = self.width() for i in range(self._ball_numbers): self._seq_animations.append(QSequentialAnimationGroup(self)) # 增加间隔 self._seq_animations[i].addPause(self.duration_pause * i) # 第一段 par_animation1 = QParallelAnimationGroup(self) ra = RoundAnimation(self._balls[i], b'pos', self) ra.setEasingCurve(self.ec) ra.setDuration(self.duration) ra.setStartValue(QPoint(2 * self.ball_radius, 2 * self.ball_radius)) ra.setEndValue(QPoint(width - 2 * self.ball_radius, height - 2 * self.ball_radius)) par_animation1.addAnimation(ra) pa = QPropertyAnimation(self._balls[i], b'_style', self) pa.setEasingCurve(self.ec) pa.setDuration(self.duration) pa.setStartValue(0) pa.setKeyValueAt(0.5, 255) pa.setEndValue(0) par_animation1.addAnimation(pa) self._seq_animations[i].addAnimation(par_animation1) # 增加间隔 self._seq_animations[i].addPause(self.duration_pause * (self._ball_numbers - i - 1)) for seq in self._seq_animations: seq.setLoopCount(-1) seq.start(QAbstractAnimation.DeleteWhenStopped)
class MyView(QGraphicsView): def __init__(self): super().__init__() self.initView() def initView(self): self.ball = Ball() self.ball.pos = QPointF(5, 50) self.animation = QPropertyAnimation(self.ball, b'pos') self.animation.setDuration(5000); self.animation.setStartValue(QPointF(5, 80)) for i in range(20): self.animation.setKeyValueAt(i/20, QPointF(i, math.sin(i))*30) self.animation.setEndValue(QPointF(570, 5)) self.scene = QGraphicsScene(self) self.scene.setSceneRect(120, -50, 250, 150) self.scene.addItem(self.ball.pixmap_item) self.setScene(self.scene) self.setWindowTitle("Sine wave animation") self.setRenderHint(QPainter.Antialiasing) self.setGeometry(300, 300, 700, 200) self.animation.start()
class MyForm(QDialog): def __init__(self): super().__init__() self.ui=Ui_Dialog() self.ui.setupUi(self) self.ui.pushButtonMoveCurve.clicked.connect(self.startAnimation) self.path = QPainterPath() self.path.moveTo(30, 30) self.path.cubicTo(30, 30, 80, 180, 180, 170) self.ui.labelpic.pos=QPointF(20,20) def paintEvent(self, e): qp = QPainter() qp.begin(self) qp.drawPath(self.path) qp.end() def startAnimation(self): self.anim = QPropertyAnimation(self.ui.labelpic, b'geometry') self.anim.setDuration(10000) self.anim.setStartValue(QPointF(20, 20)) positionValues = [n / 80 for n in range(0, 50)] for i in positionValues: self.anim.setKeyValueAt(i, self.path.pointAtPercent(i)) self.anim.setEndValue(QPointF(160, 150)) self.anim.start()
def reprise(pix): ## reposition pixitems to starting x,y, etc. node = Node(pix) node.pix.setOriginPt() sync = 1000 reprise = QPropertyAnimation(node, b'pos') reprise.setDuration(sync) reprise.setStartValue(node.pix.pos()) reprise.setEndValue(QPointF(pix.x, pix.y)) spin = QPropertyAnimation(node, b'rotate') spin.setDuration(sync) spin.setStartValue(node.pix.rotation) spin.setKeyValueAt(0.50, pix.rotation + random.randint(15, 45)) spin.setEndValue(pix.rotation) scale = QPropertyAnimation(node, b'scale') scale.setDuration(sync) scale.setStartValue(node.pix.scale) scale.setEndValue(pix.scale) opacity = QPropertyAnimation(node, b'opacity') opacity.setDuration(sync) opacity.setStartValue(node.pix.opacity()) opacity.setEndValue(1) group = QParallelAnimationGroup() group.addAnimation(reprise) group.addAnimation(spin) group.addAnimation(scale) group.addAnimation(opacity) return group
def load_animation2(window: object, property_name: bytes = b'windowOpacity') -> None: animation = QPropertyAnimation() animation.setTargetObject(window) # 设置动画目标对象 # 设置动画属性 # 注意:字节类型 # pos---位置动画---QPoint # size---大小动画---QSize # geometry----位置+大小动画----QRect # windowOpacity---窗口的透明度(0.0是透明的 1.0是不透明)---好像只适合顶层窗口 animation.setPropertyName(property_name) # animation.setStartValue(QPoint(0, 0)) # 设置开始位置---按钮的左上角位置 # animation.setEndValue(QPoint(300, 300)) # 设置结束位置 # animation.setStartValue(QSize(0, 0)) # 设置开始大小 # animation.setEndValue(QSize(300, 300)) # 设置结束大小 # animation.setStartValue(QRect(0, 0,100,100)) # 设置开始位置和大小 # animation.setEndValue(QRect(100,100,300, 300)) # 设置结束位置和大小 # 参数1 0.0到1.0 0.0表示开始点,1.0表示结束点 # 在动画的中间插入透明度0.2 animation.setStartValue(0.2) # 设置开始不透明 animation.setKeyValueAt(0.2, 0.4) # 在动画的某时间点插入一个值 animation.setKeyValueAt(0.4, 0.6) animation.setKeyValueAt(1, 1) # 在动画的结束点是不透明的 # 0透明, 1不透明 # animation.setEndValue(1) # 设置结束透明度 animation.setEndValue(TRANSPARENT) # 设置结束透明度 animation.setDirection(3000) # 设置动画单次时长---单位毫秒 animation.setEasingCurve(QEasingCurve.InQuad) # 设置动画的节奏 animation.start() # 动画开始---非阻塞
def bobble(pix): node = Node(pix) pos = node.pix.pos() node.pix.setOriginPt() sync = random.randint(4, 9) * 400 up = random.randint(5, 12) left = random.randint(5, 9) if random.randint(0, 1): up = up * -1 if random.randint(0, 1): left = left * -1 bobble = QPropertyAnimation(node, b'pos') bobble.setDuration(sync) bobble.setStartValue(pos) bobble.setKeyValueAt(0.25, pos + QPointF(-left, -up * 1.75)) bobble.setKeyValueAt(0.35, pos + QPointF(-left * 1.35, 0)) bobble.setKeyValueAt(0.45, pos + QPointF(0, up * 1.65)) bobble.setKeyValueAt(0.55, pos + QPointF(left * 1.35, 0)) bobble.setKeyValueAt(0.65, pos + QPointF(left, -up * 1.75)) bobble.setEndValue(pos) bobble.setLoopCount(-1) bobble.setEasingCurve(QEasingCurve.InOutCubic) return bobble
class LineItemObject(QGraphicsObject): # As per the ArrowItemObject def __init__(self, pti, ptf): super(LineItemObject, self).__init__() self.li = LineItem(pti, ptf, self) self.anim = QPropertyAnimation(self, b"thickness") def flash_undo(self): self.anim.setDuration(200) self.anim.setStartValue(2) self.anim.setKeyValueAt(0.5, 6) self.anim.setEndValue(0) self.anim.start() def flash_redo(self): self.anim.setDuration(200) self.anim.setStartValue(2) self.anim.setKeyValueAt(0.5, 4) self.anim.setEndValue(2) self.anim.start() @pyqtProperty(int) def thickness(self): return self.li.pen().width() @thickness.setter def thickness(self, value): self.li.setPen(QPen(Qt.red, value))
class ArrowItemObject(QGraphicsObject): # An object wrapper around the arrowitem to handle the # animation of its thickness def __init__(self, pti, ptf): super(ArrowItemObject, self).__init__() self.ai = ArrowItem(pti, ptf, self) self.anim = QPropertyAnimation(self, b"thickness") def flash_undo(self): # thin -> thick -> none. self.anim.setDuration(200) self.anim.setStartValue(2) self.anim.setKeyValueAt(0.5, 6) self.anim.setEndValue(0) self.anim.start() def flash_redo(self): # thin -> med -> thin. self.anim.setDuration(200) self.anim.setStartValue(2) self.anim.setKeyValueAt(0.5, 4) self.anim.setEndValue(2) self.anim.start() # Set and get thickness of the pen to draw the arrow. @pyqtProperty(int) def thickness(self): return self.ai.pen().width() @thickness.setter def thickness(self, value): self.ai.setPen(QPen(Qt.red, value))
class Window(QDialog): def __init__(self): super().__init__() self.dialog = uic.loadUi("demoAnimation2.ui", self) # načtení formuláře self.show() self.path = QPainterPath() self.path.moveTo(30, 30) self.path.cubicTo(30, 30, 80, 180, 180, 170) self.labelPic.pos = QPointF(20, 20) def paintEvent(self, e): qp = QPainter() qp.begin(self) qp.drawPath(self.path) qp.end() def startAnimation(self): self.anim = QPropertyAnimation( self.labelPic, b"pos") # vytvoření objektu třídy QPropertyAnimation self.anim.setDuration(4000) # prodlevy při animaci self.anim.setStartValue(QPointF(20, 20)) positionValues = [n / 80 for n in range(0, 50)] for i in positionValues: self.anim.setKeyValueAt(i, self.path.pointAtPercent(i)) self.anim.setEndValue(QPointF(160, 150)) self.anim.start()
class MyDialog(QDialog): def __init__(self): super().__init__() self.ui = Ui_Dialog() self.ui.setupUi(self) self.pixmap = QPixmap() self.pixmap.load('game_map.png') self.item = QGraphicsPixmapItem(self.pixmap) self.scene = QGraphicsScene() self.scene.addItem(self.item) self.ui.graphicsView.setScene(self.scene) self.ui.pushButton.clicked.connect(self.startanimation) self.path = QPainterPath() self.path.moveTo(30,30) self.path.cubicTo(30,30,80,180,180,170) self.ui.label.pos = QPointF(20,20) self.pos1 = [0,0] self.pos2 = [0,0] def mousePressEvent(self, event): if event.buttons() & QtCore.Qt.LeftButton: self.pos1[0] = event.x() self.pos1[1] = event.y() def mouseReleaseEvent(self, event): self.pos2[0] = event.x() self.pos2[1] = event.y() self.update() def paintEvent(self, event): width = self.pos2[0] - self.pos1[0] height = self.pos2[1] - self.pos1[1] rect = QRect(self.pos1[0],self.pos1[1],width,height) qp = QPainter() qp.begin(self) pen = QPen(Qt.red,3) pen.setStyle(Qt.DotLine) qp.setPen(pen) qp.drawArc(rect,0,360*16) qp.end() # def paintEvent(self,e): # qp = QPainter() # qp.begin(self) # qp.drawPath(self.path) # qp.end() def startanimation(self): self.anim = QPropertyAnimation(self.ui.label, b'pos') self.anim.setDuration(4000) self.anim.setStartValue(QPointF(20,20)) pos = [n/80 for n in range(0,50)] for i in pos: self.anim.setKeyValueAt(i,self.path.pointAtPercent(i)) self.anim.setEndValue(QPointF(160,150)) self.anim.start()
def translation_animation(self, obj, func, duration=1000): anim = QPropertyAnimation(obj, b'pos') anim.setDuration(duration) anim.setStartValue(QPointF(func(0)[0], func(0)[1])) anim.setEndValue(QPointF(func(1)[0], func(1)[1])) vals = [p / 100 for p in range(0, 101)] for i in vals: anim.setKeyValueAt(i, (QPointF(func(i)[0], func(i)[1]))) return anim
def shake_screen(self) -> None: a = QPropertyAnimation(self._stack, b'pos') a.setEasingCurve(QEasingCurve.InOutSine) a.setDuration(500) for step, offset in enumerate([0, 1, -2, 2, -1, 0]): a.setKeyValueAt(step * 0.2, self._stack.pos() + QPoint(offset * 40, 0)) a.start(QPropertyAnimation.DeleteWhenStopped) self._shakeanim = a
def show_error_animation(self): animation = QPropertyAnimation(self) animation.setTargetObject(self.login_widget) animation.setPropertyName(b"pos") animation.setKeyValueAt(0, self.login_widget.pos()) animation.setKeyValueAt(0.2, self.login_widget.pos() - QPoint(15, 0)) animation.setKeyValueAt(0.5, self.login_widget.pos()) animation.setKeyValueAt(0.7, self.login_widget.pos() - QPoint(-15, 0)) animation.setKeyValueAt(1, self.login_widget.pos()) animation.setDuration(200) animation.setLoopCount(5) animation.start(QAbstractAnimation.DeleteWhenStopped)
class ImageItemObject(QGraphicsObject): """A class which encapsulates the QImage. Used primarily for animation when undo or redo is performed. """ def __init__(self, midPt, image, scale, border, data): """ Initializes an new ImageItemObject. Args: midPt (QPoint): the point middle of the image. image (QImage): the image being added to the scene. scale (float): the scaling value, <1 decreases size, >1 increases. border (bool): True if the image has a border, false otherwise. data (str): Base64 data held in a string if the image had previously been json serialized. """ super(ImageItemObject, self).__init__() self.ci = ImageItem(midPt, image, self, scale, border, data) self.anim = QPropertyAnimation(self, b"thickness") self.border = border def flash_undo(self): """Animates the object in an undo sequence.""" self.anim.setDuration(200) if self.ci.border: self.anim.setStartValue(4) else: self.anim.setStartValue(0) self.anim.setKeyValueAt(0.5, 8) self.anim.setEndValue(0) self.anim.start() def flash_redo(self): """Animates the object in a redo sequence. """ self.anim.setDuration(200) self.anim.setStartValue(0) self.anim.setKeyValueAt(0.5, 8) if self.ci.border: self.anim.setEndValue(4) else: self.anim.setEndValue(0) self.anim.start() @pyqtProperty(int) def thickness(self): return self.ci.thick @thickness.setter def thickness(self, value): self.ci.thick = value self.ci.update()
def _perform_swap(self, a, b): from PyQt5.QtCore import QPointF, QPropertyAnimation elt_a = self.elements[a] elt_b = self.elements[b] # update positions pos_a = elt_a.pos pos_b = elt_b.pos # animate the exchange: move in an arc above/below a point halfway between pos_between = (pos_a + pos_b) / 2 pos_above = QPointF(pos_between.x(), -10) pos_below = QPointF(pos_between.x(), 50) anim_a = QPropertyAnimation(self.elements[a], b'pos') anim_b = QPropertyAnimation(self.elements[b], b'pos') anim_a.setDuration(400) anim_b.setDuration(400) anim_a.setKeyValueAt(0.5, pos_above) anim_b.setKeyValueAt(0.5, pos_below) anim_a.setKeyValueAt(1, pos_b) anim_b.setKeyValueAt(1, pos_a) anim_a.start() anim_b.start() self.animations.append(anim_a) self.animations.append(anim_b) # update elements list self.elements[a] = elt_b self.elements[b] = elt_a
class NotifyWidget(QWidget): def __init__(self): super().__init__() self.layout = QVBoxLayout(self) self.sub_widget = _NotifySubWidget(self) self.layout.addWidget(self.sub_widget) self.layout.setContentsMargins(0, 0, 0, 0) self._exit_shortcut = QShortcut(QKeySequence(Qt.Key_Escape), self) self._exit_shortcut.activated.connect(self.close) self.setWindowFlags(Qt.FramelessWindowHint | Qt.WindowStaysOnTopHint | Qt.Tool) self.setAttribute(Qt.WA_TranslucentBackground) self.setAttribute(Qt.WA_MacAlwaysShowToolWindow) self.resize(width, height) self.move( QApplication.primaryScreen().geometry().right() - self.width() - 20, 40) self.setLayout(self.layout) self._animation = QPropertyAnimation(self, b'windowOpacity') self._animation.setStartValue(0.8) self._animation.setKeyValueAt(0.4, 1) self._animation.setEndValue(0) self._animation.setDuration(5000) self._animation.finished.connect(self.close) def show(self): super().show() self._animation.start() def show_message(self, title, content, pixmap=None): if not self.isVisible(): self.show() self._animation.stop() self._animation.setCurrentTime(0) self._animation.start() self.sub_widget.set_title(title) self.sub_widget.set_content(content) pixmap = pixmap if pixmap else QPixmap(WINDOW_ICON) self.sub_widget.set_pixmap(pixmap) def enterEvent(self, event): self._animation.setCurrentTime(0)
def stageRight(node, pos, left, right): stage1 = QPropertyAnimation(node, b'pos') stage1.setDuration(random.randint(14, 23) * 75) val = (random.randint(7, 13) * 5) / 100 stage1.setStartValue(pos) stage1.setKeyValueAt(val, pos + QPointF(right / 2, 0)) stage1.setEndValue(pos + QPointF(right, 0)) stage2 = QPropertyAnimation(node, b'pos') stage2.setDuration(random.randint(14, 23) * 75) val = (random.randint(7, 13) * 5) / 100 stage2.setStartValue(pos + QPointF(-left, 0)) stage2.setKeyValueAt(val, pos + QPointF(-left / 2, 0)) stage2.setEndValue(pos) return stage1, stage2
class Example(QWidget): def __init__(self): super().__init__() self.initView() self.initAnimation() def initView(self): self.path = QPainterPath() self.path.moveTo(30, 30) self.path.cubicTo(30, 30, 200, 350, 350, 30) self.ball = Ball(self) self.ball.pos = QPointF(30, 30) self.setWindowTitle("Animation along curve") self.setGeometry(300, 300, 400, 300) self.show() def paintEvent(self, e): qp = QPainter() qp.begin(self) qp.setRenderHint(QPainter.Antialiasing) qp.drawPath(self.path) qp.end() def initAnimation(self): self.anim = QPropertyAnimation(self.ball, b'pos') self.anim.setDuration(7000) self.anim.setStartValue(QPointF(30, 30)) vals = [p/100 for p in range(0, 101)] for i in vals: self.anim.setKeyValueAt(i, self.path.pointAtPercent(i)) self.anim.setEndValue(QPointF(350, 30)) self.anim.start()
def rain(pix, node): node.pix.setOriginPt() pos = node.pix.pos() sync = random.randint(17, 31) * 50 y = int(pos.y()) ViewH = common["ViewH"] bottom = y + ViewH + node.pix.height * 2 top = y + node.pix.height * 2 rain1 = QPropertyAnimation(node, b'pos') rain1.setDuration(sync) rain1.setStartValue(pos) rain1.setEndValue(pos + QPointF(0, bottom)) opacity1 = QPropertyAnimation(node, b'opacity') opacity1.setDuration(sync) opacity1.setStartValue(node.pix.opacity()) opacity1.setKeyValueAt(.10, .50) opacity1.setEndValue(0) par1 = QParallelAnimationGroup() par1.addAnimation(rain1) par1.addAnimation(opacity1) rain2 = QPropertyAnimation(node, b'pos') rain2.setDuration(sync) rain2.setStartValue(pos + QPointF(0, -top)) rain2.setEndValue(pos) opacity2 = QPropertyAnimation(node, b'opacity') opacity2.setDuration(sync) opacity2.setStartValue(node.pix.opacity()) opacity2.setEndValue(.85) par2 = QParallelAnimationGroup() par2.addAnimation(rain2) par2.addAnimation(opacity2) rain = QSequentialAnimationGroup() rain.addAnimation(par1) rain.addAnimation(par2) rain.setLoopCount(-1) return rain
class NotifyWidget(QWidget): def __init__(self): super().__init__() self.layout = QVBoxLayout(self) self.sub_widget = _NotifySubWidget(self) self.layout.addWidget(self.sub_widget) self.layout.setContentsMargins(0, 0, 0, 0) self._exit_shortcut = QShortcut(QKeySequence(Qt.Key_Escape), self) self._exit_shortcut.activated.connect(self.close) self.setWindowFlags(Qt.FramelessWindowHint | Qt.WindowStaysOnTopHint | Qt.Tool) self.setAttribute(Qt.WA_TranslucentBackground) self.setAttribute(Qt.WA_MacAlwaysShowToolWindow) self.resize(width, height) self.move(QApplication.desktop().width() - self.width() - 20, 40) self.setLayout(self.layout) self._animation = QPropertyAnimation(self, b'windowOpacity') self._animation.setStartValue(0.8) self._animation.setKeyValueAt(0.4, 1) self._animation.setEndValue(0) self._animation.setDuration(5000) self._animation.finished.connect(self.close) def show(self): super().show() self._animation.start() def show_message(self, title, content, pixmap=None): if not self.isVisible(): self.show() self._animation.stop() self._animation.setCurrentTime(0) self._animation.start() self.sub_widget.set_title(title) self.sub_widget.set_content(content) pixmap = pixmap if pixmap else QPixmap(WINDOW_ICON) self.sub_widget.set_pixmap(pixmap) def enterEvent(self, event): self._animation.setCurrentTime(0)
class AnimateWindowOpacity: def __init__(self, widget: QtWidgets.QWidget, duration: int, start_value: float = 0.8, end_value: float = 1.0): self.widget = widget self.duration = duration self.animation = QPropertyAnimation(self.widget, b"windowOpacity") self.start_value, self.end_value = start_value, end_value self.setup_animation(self.start_value, self.end_value) def setup_animation(self, start_value: float = 0.0, end_value: float = 1.0, duration: int = 0): if not duration: duration = self.duration self.animation.setDuration(duration) self.animation.setKeyValueAt(0.0, start_value) self.animation.setKeyValueAt(1.0, end_value) self.animation.setEasingCurve(QEasingCurve.OutCubic) def fade_in(self, duration: int = 0): if self.widget.windowOpacity() >= self.end_value: return False self.setup_animation(self.start_value, self.end_value, duration) self.play() return True def fade_out(self, duration: int = 0): if self.widget.windowOpacity() <= self.start_value: return False self.setup_animation(self.end_value, self.start_value, duration) self.play() return True def play(self): if self.animation.state() != QAbstractAnimation.Running: self.animation.start()
def move_tile(self, dur, *args): """ Create move tile animation *args are time fraction, points in path either as (time, QPointF) or (time, x, y) """ if not self.pixmap_item.isVisible(): self.pixmap_item.setPos( QPointF(Cons.WINDOW_SIZE[0] / 2 - Cons.WIDTH / 2, Cons.WINDOW_SIZE[1])) self.pixmap_item.show() animation = QPropertyAnimation(self, b'pos') animation.setDuration(dur) for val in args: if isinstance(val[1], QPointF): point = val[1] else: point = QPointF(val[1], val[2]) animation.setKeyValueAt(val[0], point) self.animation = animation return self.animation
def setPaths(pix, anime, node): pos = node.pix.pos() sync = random.randint(73,173) * 100 ## very arbitrary path = QPropertyAnimation(node, b'pos') path.setDuration(sync) waypts = pathLoader(anime) if not waypts: return ## offset for origin pt - setOrigin wasn't working pt = getOffset(node.pix) path.setStartValue(waypts.pointAtPercent(0.0)-pt) for i in range(1, 99): path.setKeyValueAt(i/100.0, waypts.pointAtPercent(i/100.0)-pt) path.setEndValue(waypts.pointAtPercent(1.0)-pt) path.setLoopCount(-1) return path
def _create_flash_animation(self): if FLASH_COUNT.value <= 0 or \ FLASH_DURATION.value <= 0: return None minibuff_input = self.minibuffer.input() anim = QPropertyAnimation(minibuff_input, b"background_color") base = minibuff_input.property(b"background_color") flash_color = QColor(FLASH_COLOR.value) anim.setDuration(int(FLASH_DURATION.value * 1000)) step = 1. / (FLASH_COUNT.value * 2) pos = step colors = itertools.cycle((flash_color, base)) anim.setStartValue(base) while pos < 1: anim.setKeyValueAt(pos, next(colors)) pos += step anim.setEndValue(base) return anim
class Example(QGraphicsView): def __init__(self): super().__init__() self.initView() def initView(self): self.ball = Ball() self.anim = QPropertyAnimation(self.ball, b'pos') self.anim.setDuration(8000) self.anim.setStartValue(QPointF(5, 30)) self.anim.setKeyValueAt(0.3, QPointF(80, 30)) self.anim.setKeyValueAt(0.5, QPointF(200, 30)) self.anim.setKeyValueAt(0.8, QPointF(250, 250)) self.anim.setEndValue(QPointF(290, 30)) self.scene = QGraphicsScene(self) self.scene.setSceneRect(0, 0, 300, 300) self.scene.addItem(self.ball.pixmap_item) self.setScene(self.scene) self.setWindowTitle("Ball animation") self.setRenderHint(QPainter.Antialiasing) self.setGeometry(300, 300, 500, 350) self.anim.start() self.show()
class UQline(UQtxt): def __init__(self,*args,**kwargs): super().__init__(*args,**kwargs) kwargs = self._args(*args,**kwargs) redim = kwargs.get("redim",None) posi = kwargs.get("posi",None) size = kwargs.get("size",None) anim = kwargs.get("anim",True) w = 5 if redim == "w": self.resize(size,self.height()) if redim == "h": self.resize(self.width(),size) self.move(posi[0],posi[1]) ef = QGraphicsColorizeEffect(self) self.setGraphicsEffect(ef) self.__animation = QPropertyAnimation(ef,b"color") self.__animation.setDuration(2000) self.__animation.setKeyValueAt(0,QColor(0, 255, 0)) self.__animation.setKeyValueAt(0.5,QColor(255, 0, 0)) self.__animation.setKeyValueAt(1,QColor(0, 255, 0)) self.__animation.setLoopCount(-1) if anim: self.__animation.start() self.show() def load_anim(self): self.__animation.start() def stop_anim(self): self.__animation.stop()
def __init__(self, size, parent=None): super(PadNavigator, self).__init__(parent) self.form = Ui_Form() splash = SplashItem() splash.setZValue(1) pad = FlippablePad(size) flipRotation = QGraphicsRotation(pad) xRotation = QGraphicsRotation(pad) yRotation = QGraphicsRotation(pad) flipRotation.setAxis(Qt.YAxis) xRotation.setAxis(Qt.YAxis) yRotation.setAxis(Qt.XAxis) pad.setTransformations([flipRotation, xRotation, yRotation]) backItem = QGraphicsProxyWidget(pad) widget = QWidget() self.form.setupUi(widget) self.form.hostName.setFocus() backItem.setWidget(widget) backItem.setVisible(False) backItem.setFocus() backItem.setCacheMode(QGraphicsItem.ItemCoordinateCache) r = backItem.rect() backItem.setTransform(QTransform().rotate(180, Qt.YAxis).translate(-r.width()/2, -r.height()/2)) selectionItem = RoundRectItem(QRectF(-60, -60, 120, 120), QColor(Qt.gray), pad) selectionItem.setZValue(0.5) smoothSplashMove = QPropertyAnimation(splash, b'y') smoothSplashOpacity = QPropertyAnimation(splash, b'opacity') smoothSplashMove.setEasingCurve(QEasingCurve.InQuad) smoothSplashMove.setDuration(250) smoothSplashOpacity.setDuration(250) smoothXSelection = QPropertyAnimation(selectionItem, b'x') smoothYSelection = QPropertyAnimation(selectionItem, b'y') smoothXRotation = QPropertyAnimation(xRotation, b'angle') smoothYRotation = QPropertyAnimation(yRotation, b'angle') smoothXSelection.setDuration(125) smoothYSelection.setDuration(125) smoothXRotation.setDuration(125) smoothYRotation.setDuration(125) smoothXSelection.setEasingCurve(QEasingCurve.InOutQuad) smoothYSelection.setEasingCurve(QEasingCurve.InOutQuad) smoothXRotation.setEasingCurve(QEasingCurve.InOutQuad) smoothYRotation.setEasingCurve(QEasingCurve.InOutQuad) smoothFlipRotation = QPropertyAnimation(flipRotation, b'angle') smoothFlipScale = QPropertyAnimation(pad, b'scale') smoothFlipXRotation = QPropertyAnimation(xRotation, b'angle') smoothFlipYRotation = QPropertyAnimation(yRotation, b'angle') flipAnimation = QParallelAnimationGroup(self) smoothFlipScale.setDuration(500) smoothFlipRotation.setDuration(500) smoothFlipXRotation.setDuration(500) smoothFlipYRotation.setDuration(500) smoothFlipScale.setEasingCurve(QEasingCurve.InOutQuad) smoothFlipRotation.setEasingCurve(QEasingCurve.InOutQuad) smoothFlipXRotation.setEasingCurve(QEasingCurve.InOutQuad) smoothFlipYRotation.setEasingCurve(QEasingCurve.InOutQuad) smoothFlipScale.setKeyValueAt(0, 1.0) smoothFlipScale.setKeyValueAt(0.5, 0.7) smoothFlipScale.setKeyValueAt(1, 1.0) flipAnimation.addAnimation(smoothFlipRotation) flipAnimation.addAnimation(smoothFlipScale) flipAnimation.addAnimation(smoothFlipXRotation) flipAnimation.addAnimation(smoothFlipYRotation) setVariablesSequence = QSequentialAnimationGroup() setFillAnimation = QPropertyAnimation(pad, b'fill') setBackItemVisibleAnimation = QPropertyAnimation(backItem, b'visible') setSelectionItemVisibleAnimation = QPropertyAnimation(selectionItem, b'visible') setFillAnimation.setDuration(0) setBackItemVisibleAnimation.setDuration(0) setSelectionItemVisibleAnimation.setDuration(0) setVariablesSequence.addPause(250) setVariablesSequence.addAnimation(setBackItemVisibleAnimation) setVariablesSequence.addAnimation(setSelectionItemVisibleAnimation) setVariablesSequence.addAnimation(setFillAnimation) flipAnimation.addAnimation(setVariablesSequence) stateMachine = QStateMachine(self) splashState = QState(stateMachine) frontState = QState(stateMachine) historyState = QHistoryState(frontState) backState = QState(stateMachine) frontState.assignProperty(pad, "fill", False) frontState.assignProperty(splash, "opacity", 0.0) frontState.assignProperty(backItem, "visible", False) frontState.assignProperty(flipRotation, "angle", 0.0) frontState.assignProperty(selectionItem, "visible", True) backState.assignProperty(pad, "fill", True) backState.assignProperty(backItem, "visible", True) backState.assignProperty(xRotation, "angle", 0.0) backState.assignProperty(yRotation, "angle", 0.0) backState.assignProperty(flipRotation, "angle", 180.0) backState.assignProperty(selectionItem, "visible", False) stateMachine.addDefaultAnimation(smoothXRotation) stateMachine.addDefaultAnimation(smoothYRotation) stateMachine.addDefaultAnimation(smoothXSelection) stateMachine.addDefaultAnimation(smoothYSelection) stateMachine.setInitialState(splashState) anyKeyTransition = QEventTransition(self, QEvent.KeyPress, splashState) anyKeyTransition.setTargetState(frontState) anyKeyTransition.addAnimation(smoothSplashMove) anyKeyTransition.addAnimation(smoothSplashOpacity) enterTransition = QKeyEventTransition(self, QEvent.KeyPress, Qt.Key_Enter, backState) returnTransition = QKeyEventTransition(self, QEvent.KeyPress, Qt.Key_Return, backState) backEnterTransition = QKeyEventTransition(self, QEvent.KeyPress, Qt.Key_Enter, frontState) backReturnTransition = QKeyEventTransition(self, QEvent.KeyPress, Qt.Key_Return, frontState) enterTransition.setTargetState(historyState) returnTransition.setTargetState(historyState) backEnterTransition.setTargetState(backState) backReturnTransition.setTargetState(backState) enterTransition.addAnimation(flipAnimation) returnTransition.addAnimation(flipAnimation) backEnterTransition.addAnimation(flipAnimation) backReturnTransition.addAnimation(flipAnimation) columns = size.width() rows = size.height() stateGrid = [] for y in range(rows): stateGrid.append([QState(frontState) for _ in range(columns)]) frontState.setInitialState(stateGrid[0][0]) selectionItem.setPos(pad.iconAt(0, 0).pos()) for y in range(rows): for x in range(columns): state = stateGrid[y][x] rightTransition = QKeyEventTransition(self, QEvent.KeyPress, Qt.Key_Right, state) leftTransition = QKeyEventTransition(self, QEvent.KeyPress, Qt.Key_Left, state) downTransition = QKeyEventTransition(self, QEvent.KeyPress, Qt.Key_Down, state) upTransition = QKeyEventTransition(self, QEvent.KeyPress, Qt.Key_Up, state) rightTransition.setTargetState(stateGrid[y][(x + 1) % columns]) leftTransition.setTargetState(stateGrid[y][((x - 1) + columns) % columns]) downTransition.setTargetState(stateGrid[(y + 1) % rows][x]) upTransition.setTargetState(stateGrid[((y - 1) + rows) % rows][x]) icon = pad.iconAt(x, y) state.assignProperty(xRotation, "angle", -icon.x() / 6.0) state.assignProperty(yRotation, "angle", icon.y() / 6.0) state.assignProperty(selectionItem, "x", icon.x()) state.assignProperty(selectionItem, "y", icon.y()) frontState.assignProperty(icon, "visible", True) backState.assignProperty(icon, "visible", False) setIconVisibleAnimation = QPropertyAnimation(icon, b'visible') setIconVisibleAnimation.setDuration(0) setVariablesSequence.addAnimation(setIconVisibleAnimation) scene = QGraphicsScene(self) scene.setBackgroundBrush(QBrush(QPixmap(":/images/blue_angle_swirl.jpg"))) scene.setItemIndexMethod(QGraphicsScene.NoIndex) scene.addItem(pad) scene.setSceneRect(scene.itemsBoundingRect()) self.setScene(scene) sbr = splash.boundingRect() splash.setPos(-sbr.width() / 2, scene.sceneRect().top() - 2) frontState.assignProperty(splash, "y", splash.y() - 100.0) scene.addItem(splash) self.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff) self.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff) self.setMinimumSize(50, 50) self.setViewportUpdateMode(QGraphicsView.FullViewportUpdate) self.setCacheMode(QGraphicsView.CacheBackground) self.setRenderHints(QPainter.Antialiasing | QPainter.SmoothPixmapTransform | QPainter.TextAntialiasing) if QGLFormat.hasOpenGL(): self.setViewport(QGLWidget(QGLFormat(QGL.SampleBuffers))) stateMachine.start()