class CombinerIcon(ProjectItemIcon): _SHAKE_FACTOR = 0.05 def __init__(self, toolbox, x, y, project_item, icon): """View icon for the Design View. Args: toolbox (ToolBoxUI): QMainWindow instance x (float): Icon x coordinate y (float): Icon y coordinate project_item (ProjectItem): Item icon (str): icon resource path """ super().__init__(toolbox, x, y, project_item, icon, icon_color=QColor("#990000"), background_color=QColor("#ffcccc")) self.time_line = QTimeLine() self.time_line.setLoopCount(0) # loop forever self.time_line.setFrameRange(0, 10) self.time_line.setDirection(QTimeLine.Backward) self.time_line.valueChanged.connect( self._handle_time_line_value_changed) self.time_line.stateChanged.connect( self._handle_time_line_state_changed) self._svg_item_pos = self.svg_item.pos() @Slot(float) def _handle_time_line_value_changed(self, value): rect = self.svg_item.sceneBoundingRect() width = rect.width() height = rect.height() x = random.uniform(-self._SHAKE_FACTOR, self._SHAKE_FACTOR) * width y = random.uniform(-self._SHAKE_FACTOR, self._SHAKE_FACTOR) * height self.svg_item.setPos(self._svg_item_pos + QPointF(x, y)) @Slot("QTimeLine::State") def _handle_time_line_state_changed(self, new_state): if new_state == QTimeLine.NotRunning: self.svg_item.setPos(self._svg_item_pos) def start_animation(self): """Start the animation that plays when the Combiner associated to this GraphicsItem is running. """ if self.time_line.state() == QTimeLine.Running: return self.time_line.start() def stop_animation(self): """Stop animation""" if self.time_line.state() != QTimeLine.Running: return self.time_line.stop()
class ToolIcon(ProjectItemIcon): def __init__(self, toolbox, x, y, project_item, icon): """Tool icon for the Design View. Args: toolbox (ToolBoxUI): QMainWindow instance x (float): Icon x coordinate y (float): Icon y coordinate project_item (ProjectItem): Item icon (str): icon resource path """ super().__init__( toolbox, x, y, project_item, icon, icon_color=QColor("red"), background_color=QColor("#ffe6e6") ) self.time_line = QTimeLine() self.time_line.setLoopCount(0) # loop forever self.time_line.setFrameRange(0, 10) self.time_line.setDuration(1200) self.time_line.setDirection(QTimeLine.Backward) self.time_line.valueChanged.connect(self._handle_time_line_value_changed) self.time_line.stateChanged.connect(self._handle_time_line_state_changed) self._svg_item_pos = self.svg_item.pos() rect = self.svg_item.sceneBoundingRect() self._anim_transformation_origin_point_y = -0.75 * rect.height() self._anim_delta_x_factor = 0.5 * rect.width() @Slot(float) def _handle_time_line_value_changed(self, value): angle = value * 45.0 self.svg_item.setRotation(angle) delta_y = 0.5 * self.svg_item.sceneBoundingRect().height() delta = QPointF(self._anim_delta_x_factor * value, delta_y) self.svg_item.setPos(self._svg_item_pos + delta) @Slot("QTimeLine::State") def _handle_time_line_state_changed(self, new_state): if new_state == QTimeLine.Running: self.svg_item.setTransformOriginPoint(0, self._anim_transformation_origin_point_y) elif new_state == QTimeLine.NotRunning: self.svg_item.setTransformOriginPoint(0, 0) self.svg_item.setPos(self._svg_item_pos) self.svg_item.setRotation(0) def start_animation(self): """Starts the item execution animation. """ if self.time_line.state() == QTimeLine.Running: return self.time_line.start() def stop_animation(self): """Stop animation""" if self.time_line.state() != QTimeLine.Running: return self.time_line.stop()