Source code for qiskit_metal.renderers.renderer_mpl.extensions.animated_text
# -*- coding: utf-8 -*-# This code is part of Qiskit.## (C) Copyright IBM 2017, 2021.## This code is licensed under the Apache License, Version 2.0. You may# obtain a copy of this license in the LICENSE.txt file in the root directory# of this source tree or at http://www.apache.org/licenses/LICENSE-2.0.## Any modifications or derivative works of this code must retain this# copyright notice, and modified files need to carry a notice indicating# that they have been altered from the originals.""""""importmatplotlib.pyplotaspltfromPySide6.QtCoreimportQTimerfrom...._gui.utility._handle_qt_messagesimportslot_catch_error
[docs]classAnimatedText():"""Class that animates text."""def__init__(self,ax:plt.Axes,text:str,canvas,kw:dict=None,anim_start=0.9,anim_dt_ms=25,anim_delta=-0.0005,anim_stop=0,anim_accel=-0.0005,start=True,loc=[0.5,0.5]):""" Args: ax (plt.Axes): The axis. text (str): Text to animate. canvas (canvas): The canvas. kw (dict): The parameters. Defaults to None. anim_start (float): Animation start. Defaults to 0.9. anim_dt_ms (int): Animation dt in miliseconds. Defaults to 25. anim_delta (float): Animation delta. Defaults to -0.0005. anim_stop (int): Animation stop. Defaults to 0. anim_accel (float): Animation acceleration. Defaults to -0.0005. start (bool): Whether or not to start. Defaults to True. loc (list): Location. Defaults to [0.5, 0.5]. """self.canvas=canvasself.ax=axself.anim_value=anim_startself.anim_delta=anim_deltaself.anim_dt_ms=anim_dt_msself.anim_stop=anim_stopself.anim_accel=anim_accelself.anim_veloc=0# MPL text# Create the textkw={**dict(fontsize=35,fontweight='bold',va='center',ha='center',alpha=anim_start,transform=ax.transAxes,color='#00356B',zorder=200),**(kwifkwelse{})}self.text=ax.text(*loc,text,**kw)# Timerself.timer=QTimer()self.timer.timeout.connect(self.timer_tick)ifstart:self.start()
[docs]defstart(self):"""Start the timer."""self.timer.start(self.anim_dt_ms)
[docs]defstop(self):"""Stop the timer."""self.timer.start()
[docs]@slot_catch_error()deftimer_tick(self):"""Tick the timer."""# Update anim position valueself.anim_veloc+=self.anim_accel# acceleration on vellcoity updateself.anim_value+=self.anim_delta+self.anim_veloc# update position# print(f'tick {self.anim_value}')ifself.anim_value>=self.anim_stop:# one sidedself.text.set_alpha(self.anim_value)ifself.textnotinself.ax.texts:# if the axis is cleared and redrawnself.ax.texts.append(self.text)self.canvas.refresh()# refresht the canvaselse:self.timer.stop()self.timer.deleteLater()# Remove artistself.text.figure=self.ax.figureifself.textinself.ax.texts:self.ax.texts.remove(self.text)ifself.text.axes:try:# remove text from axisself.text.remove()exceptValueErrorase:# could raise ValueError: list.remove(x): x not in listpassself.canvas.refresh()# refresht the canvas