class FaderWidget(QWidget): def __init__(self, old_widget, new_widget): QWidget.__init__(self, new_widget) self.pixmap_opacity = 1.0 self.old_pixmap = QPixmap(new_widget.size()) old_widget.render(self.old_pixmap) self.timeline = QTimeLine() self.timeline.valueChanged.connect(self.animate) self.timeline.finished.connect(self.close) self.timeline.setDuration(230) self.timeline.start() self.resize(new_widget.size()) self.show() def paintEvent(self, event): painter = QPainter() painter.begin(self) painter.setOpacity(self.pixmap_opacity) painter.drawPixmap(0, 0, self.old_pixmap) painter.end() def animate(self, value): self.pixmap_opacity = 1.0 - value self.repaint()
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()
class ImporterExporterAnimation: def __init__(self, item, duration=2000, count=5, percentage_size=0.24, x_shift=0): """Initializes animation stuff. Args: item (QGraphicsItem): The item on top of which the animation should play. """ self._item = item self.cubes = [QGraphicsTextItem("\uf1b2", item) for i in range(count)] self.opacity_at_value_path = QPainterPath(QPointF(0.0, 0.0)) self.opacity_at_value_path.lineTo(QPointF(0.01, 1.0)) self.opacity_at_value_path.lineTo(QPointF(0.5, 1.0)) self.opacity_at_value_path.lineTo(QPointF(1.0, 0.0)) self.time_line = QTimeLine() self.time_line.setLoopCount(0) # loop forever self.time_line.setFrameRange(0, 10) self.time_line.setDuration(duration) self.time_line.setCurveShape(QTimeLine.LinearCurve) self.time_line.valueChanged.connect( self._handle_time_line_value_changed) self.time_line.stateChanged.connect( self._handle_time_line_state_changed) font = QFont('Font Awesome 5 Free Solid') item_rect = item.rect() cube_size = percentage_size * 0.875 * item_rect.height() font.setPixelSize(cube_size) rect = item_rect.translated(-0.5 * cube_size + x_shift, -cube_size) end = rect.center() ctrl = end - QPointF(0, 0.6 * rect.height()) lower, upper = 0.2, 0.8 starts = [lower + i * (upper - lower) / count for i in range(count)] starts = [ rect.topLeft() + QPointF(start * rect.width(), 0) for start in starts ] self.paths = [QPainterPath(start) for start in starts] for path in self.paths: path.quadTo(ctrl, end) self.offsets = [i / count for i in range(count)] for cube in self.cubes: cube.setFont(font) cube.setDefaultTextColor("#003333") cube.setTransformOriginPoint(cube.boundingRect().center()) cube.hide() cube.setOpacity(0) @Slot(float) def _handle_time_line_value_changed(self, value): for cube, offset, path in zip(self.cubes, self.offsets, self.paths): value = (offset + value) % 1.0 opacity = self.opacity_at_value_path.pointAtPercent(value).y() cube.setOpacity(opacity) percent = self.percent(value) point = path.pointAtPercent(percent) angle = percent * 360.0 cube.setPos(point) cube.setRotation(angle) @Slot("QTimeLine::State") def _handle_time_line_state_changed(self, new_state): if new_state == QTimeLine.Running: random.shuffle(self.offsets) for cube in self.cubes: cube.show() elif new_state == QTimeLine.NotRunning: for cube in self.cubes: cube.hide() def start(self): """Starts the animation.""" if self.time_line.state() == QTimeLine.Running: return self.time_line.start() @staticmethod def percent(value): raise NotImplementedError() def stop(self): """Stops the animation""" self.time_line.stop()
class ImportExportAnimation: def __init__(self, parent_item, src_item, dst_item, duration=2000): """Initializes animation stuff. Args: parent_item (QGraphicsItem): The item on top of which the animation should play. src_item (QGraphicsItem): The source item. dst_item (QGraphicsItem): The destination item. duration (int. optional): The desired duration of each loop in milliseconds, defaults to 1000. """ self._parent_item = parent_item self.src_item = src_item self.dst_item = dst_item font = QFont('Font Awesome 5 Free Solid') size = 0.875 * round(parent_item.rect().height() / 2) font.setPixelSize(size) self.src_item.setFont(font) self.dst_item.setFont(font) self.src_opacity_effect = QGraphicsOpacityEffect() self.src_item.setGraphicsEffect(self.src_opacity_effect) self.dst_opacity_effect = QGraphicsOpacityEffect() self.dst_item.setGraphicsEffect(self.dst_opacity_effect) self.timer = QTimeLine() self.timer.setLoopCount(0) # loop forever self.timer.setFrameRange(0, 10) self.timer.valueChanged.connect(self._handle_timer_value_changed) self.timer.setDuration(duration) self.src_animation = QGraphicsItemAnimation() self.src_animation.setItem(self.src_item) self.src_animation.setTimeLine(self.timer) self.dst_animation = QGraphicsItemAnimation() self.dst_animation.setItem(self.dst_item) self.dst_animation.setTimeLine(self.timer) @Slot(float) def _handle_timer_value_changed(self, value): self.src_opacity_effect.setOpacity(1.0 - 2 * value) self.dst_opacity_effect.setOpacity(2 * value - 1.0) def start(self): """Starts the animation.""" rect = self._parent_item.rect() dx = self.src_item.boundingRect().width() dy = self.dst_item.boundingRect().height() rect.adjust(0, 0, -dx, -dy) src, dst = rect.topLeft(), rect.bottomRight() vec = dst - src self.src_item.setParentItem(self._parent_item) self.dst_item.setParentItem(self._parent_item) self.src_item.setPos(src) self.dst_item.setPos(src) self.src_opacity_effect.setOpacity(0.0) self.dst_opacity_effect.setOpacity(0.0) for i in range(100): step = i / 100.0 self.src_animation.setPosAt(step, src + vec * step) self.dst_animation.setPosAt(step, src + vec * step) self.timer.start() def stop(self): """Stops the animation""" self.timer.stop() self.src_item.setParentItem(None) self.dst_item.setParentItem(None) self.src_item.scene().removeItem(self.src_item) self.dst_item.scene().removeItem(self.dst_item) self.timer.setCurrentTime(999)